/** Test of addMessageProperty method, of class UserMessage. */
  @Test
  public void test13_AddMessageProperty() {
    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;

    assertEquals(1, tps.size());
    assertEquals(2, tps.get(0).getMessageProperties().size());

    em.getTransaction().begin();
    instance = tps.get(0);
    instance.addMessageProperty(new Property(T_PROP_3_NAME, T_PROP_3_VALUE));
    em.getTransaction().commit();

    tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();

    assertEquals(1, tps.size());
    Collection<IProperty> properties = tps.get(0).getMessageProperties();

    assertEquals(3, properties.size());
    for (IProperty p : properties) {
      if (p.getName().equals(T_PROP_1_NAME)) {
        assertEquals(T_PROP_1_VALUE, p.getValue());
        assertNull(p.getType());
      } else if (p.getName().equals(T_PROP_2_NAME)) {
        assertEquals(T_PROP_2_VALUE, p.getValue());
        assertEquals(T_PROP_2_TYPE, p.getType());
      } else {
        assertEquals(T_PROP_3_NAME, p.getName());
        assertEquals(T_PROP_3_VALUE, p.getValue());
        assertNull(p.getType());
      }
    }
  }
  /** Test if setReceiver correctly changes the Receiver */
  @Test
  public void test08_ChangeReceiver() {
    em.getTransaction().begin();
    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;
    TradingPartner receiver = new TradingPartner(T_SENDER2_ID, T_SENDER2_ROLE);

    assertTrue(tps.size() == 1);

    instance = tps.get(0);

    instance.setReceiver(receiver);

    em.persist(receiver);
    em.persist(instance);
    em.getTransaction().commit();

    em.getTransaction().begin();
    tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();

    instance = tps.get(0);

    assertEquals(T_SENDER2_ROLE, instance.getReceiver().getRole());

    Iterator<IPartyId> pids = instance.getReceiver().getPartyIds().iterator();
    if (pids.hasNext()) {
      assertEquals(T_SENDER2_ID, pids.next().getId());
    } else
      // There should be a partyid
      fail("Not retrieved the correct Sender");

    em.getTransaction().commit();
  }
Example #3
0
 @Override
 public void execute(Message message) {
   if (!(message instanceof UserMessage)) return;
   UserMessage userMessage = (UserMessage) message;
   MessageHandler messageHandler = bot.getMessageHandler();
   messageHandler.sendMessage(Tools.getCorrectTarget(userMessage), userMessage.getMessage());
 }
  /** Test of setMPC method, of class UserMessage. */
  @Test
  public void test01_SetMPC() {
    UserMessage instance = new UserMessage();

    instance.setMPC(T_MPC_1);

    em.getTransaction().begin();
    em.persist(instance);
    em.getTransaction().commit();
  }
  /** Test of getCollaborationInfo method, of class UserMessage. */
  @Test
  public void test10_GetCollaborationInfo() {
    em.getTransaction().begin();
    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;

    assertTrue(tps.size() == 1);
    instance = tps.get(0);

    assertEquals(T_COLLABINFO_SVC, instance.getCollaborationInfo().getService().getName());
    assertEquals(T_COLLABINFO_ACTION, instance.getCollaborationInfo().getAction());
    assertEquals(T_COLLABINFO_PMODE, instance.getCollaborationInfo().getAgreement().getPModeId());
  }
  /** Test of setRefToMessageId method inherited from MessageUnit */
  @Test
  public void test21_SetRefToMessageId() {
    em.getTransaction().begin();
    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;

    assertTrue(tps.size() == 1);
    instance = tps.get(0);

    instance.setRefToMessageId(T_REF_MSG_ID);

    em.getTransaction().commit();
  }
  /** Test of setTimestamp method inherited from MessageUnit */
  @Test
  public void test17_SetTimestamp() {
    em.getTransaction().begin();
    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;

    assertTrue(tps.size() == 1);
    instance = tps.get(0);

    instance.setTimestamp(T_TIMESTAMP);

    em.getTransaction().commit();
  }
  /** Test of getPMode method inherited from MessageUnit */
  @Test
  public void test27_GetPMode() {
    em.getTransaction().begin();
    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;

    assertTrue(tps.size() == 1);
    instance = tps.get(0);

    assertEquals(T_PMODE_ID, instance.getPMode());

    em.getTransaction().commit();
  }
  /** Test of getStates method inherited from MessageUnit */
  @Test
  public void test25_GetProcessingStates() {
    em.getTransaction().begin();
    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;

    assertTrue(tps.size() == 1);
    instance = tps.get(0);

    assertTrue(instance.getProcessingStates().size() == 2);
    assertEquals(T_PROCSTATE_2.getName(), instance.getProcessingStates().get(0).getName());
    assertEquals(T_PROCSTATE_1.getName(), instance.getProcessingStates().get(1).getName());

    em.getTransaction().commit();
  }
  /** Test of addProcessingState method inherited from MessageUnit */
  @Test
  public void test23_AddProcessingState() {
    em.getTransaction().begin();
    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;

    assertTrue(tps.size() == 1);
    instance = tps.get(0);

    instance.addProcessingState(T_PROCSTATE_1);

    em.persist(T_PROCSTATE_1);

    em.getTransaction().commit();
  }
  /** Test of setReceiver method, of class UserMessage. */
  @Test
  public void test06_SetReceiver() {
    em.getTransaction().begin();
    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;
    TradingPartner receiver = new TradingPartner(T_RECEIVER1_ID, T_RECEIVER1_ROLE);

    assertTrue(tps.size() == 1);

    instance = tps.get(0);

    instance.setReceiver(receiver);

    em.persist(receiver);
    em.persist(instance);
    em.getTransaction().commit();
  }
  /** Test of setCollaborationInfo method, of class UserMessage. */
  @Test
  public void test09_SetCollaborationInfo() {
    em.getTransaction().begin();
    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;
    CollaborationInfo colInfo =
        new CollaborationInfo(T_COLLABINFO_SVC, T_COLLABINFO_ACTION, T_COLLABINFO_PMODE);

    assertTrue(tps.size() == 1);

    instance = tps.get(0);

    instance.setCollaborationInfo(colInfo);

    em.persist(instance);
    em.getTransaction().commit();
  }
  /** Test of setMessageProperties method, of class UserMessage. */
  @Test
  public void test11_SetMessageProperties() {
    em.getTransaction().begin();

    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;

    assertTrue(tps.size() == 1);
    instance = tps.get(0);
    Collection<Property> props = new HashSet<Property>();

    props.add(new Property(T_PROP_1_NAME, T_PROP_1_VALUE));
    props.add(new Property(T_PROP_2_NAME, T_PROP_2_VALUE, T_PROP_2_TYPE));

    instance.setMessageProperties(props);

    em.persist(instance);
    em.getTransaction().commit();
  }
  /** Test of getReceiver method, of class UserMessage. */
  @Test
  public void test07_GetReceiver() {
    em.getTransaction().begin();
    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance;

    assertTrue(tps.size() == 1);
    instance = tps.get(0);

    assertEquals(T_RECEIVER1_ROLE, instance.getReceiver().getRole());

    Iterator<IPartyId> pids = instance.getReceiver().getPartyIds().iterator();
    if (pids.hasNext()) {
      assertEquals(T_RECEIVER1_ID, pids.next().getId());
    } else
      // There should be a partyid
      fail("Not retrieved the correct Sender");

    em.getTransaction().commit();
  }
  /** Test of isDelivered query */
  @Test
  public void test28_isDeliveredQuery() {
    em.getTransaction().begin();

    List<UserMessage> tps = em.createQuery("from UserMessage", UserMessage.class).getResultList();
    UserMessage instance = tps.get(0);

    String msgId = instance.getMessageId();

    instance.setProcessingState(new ProcessingState(ProcessingStates.DELIVERED));

    em.persist(instance);

    em.getTransaction().commit();

    Boolean result =
        new Boolean(
            em.createNamedQuery("UserMessage.isDelivered", String.class)
                .setParameter("msgId", msgId)
                .getSingleResult());

    assertTrue(result);
  }
  private void loadData(String filePath, final int[] inFactors, final int[] outFactors)
      throws IOException {
    FactorCollection factorCollection = FactorCollection.instance();
    final String factorDelimiter = StaticData.instance().getFactorDelimiter();

    VERBOSE.v(2, "Loading global lexical model from file " + filePath + "\n");

    // m_inputFactors = new FactorMask(inFactors);
    // m_outputFactors = new FactorMask(outFactors);

    BufferedReader inFile = new BufferedReader(new FileReader(filePath));
    // reading in data one line at a time
    int lineNum = 0;
    String line;
    while ((line = inFile.readLine()) != null) {
      ++lineNum;
      String token[] = Util.tokenize(line, " ");

      if (token.length != 3) // format checking
      {
        StringBuilder errorMessage = new StringBuilder();
        errorMessage
            .append("Syntax error at ")
            .append(filePath)
            .append(":")
            .append(lineNum)
            .append('\n')
            .append(line)
            .append('\n');
        UserMessage.add(errorMessage.toString());
        System.exit(0);
      }

      // create the output word
      Word outWord = new Word();
      String[] factorString = Util.tokenize(token[0], factorDelimiter);
      for (int i = 0; i < outFactors.length; i++) {
        final FactorDirection direction = FactorDirection.Output;
        final int factorType = outFactors[i];
        final Factor factor = factorCollection.addFactor(direction, factorType, factorString[i]);
        outWord.setFactor(factorType, factor);
      }

      // create the input word
      Word inWord = new Word();
      factorString = Util.tokenize(token[1], factorDelimiter);
      for (int i = 0; i < inFactors.length; i++) {
        final FactorDirection direction = FactorDirection.Input;
        final int factorType = inFactors[i];
        final Factor factor = factorCollection.addFactor(direction, factorType, factorString[i]);
        inWord.setFactor(factorType, factor);
      }

      // maximum entropy feature score
      float score = Float.valueOf(token[2]);

      // std::cerr << "storing word " << *outWord << " " << *inWord <<
      // " " << score << endl;

      // store feature in hash
      Map<Word, Float> dh = m_hash.get(outWord);
      if (dh == null) {
        dh = new HashMap<Word, Float>();
        dh.put(inWord, score);
        m_hash.put(outWord, dh);
      } else {
        dh.put(inWord, score);
        outWord = null;
      }
    }
  }
Example #17
0
  public void checkCommand() {
    try {
      // Create inputstream
      BufferedReader fromClient =
          new BufferedReader(new InputStreamReader(socket.getInputStream()));
      // Create outputstream
      PrintWriter toClient = new PrintWriter(socket.getOutputStream(), true);

      toClient.println("Welcome to Chatroom! ");

      String inputLine; // String read from Client
      String outputLine; // String send to Client

      HandleMessage listener =
          new HandleMessage(
              socket, users.getName(index)); // to listen if it has message sent by other users
      new Thread(listener).start(); // Start the thread

      // Create UserMessage instance, to store and get message passing among users
      UserMessage usermessage = new UserMessage();

      // Create TimeNoActive instance, to find if user did nothing for a period time
      TimeNoActive timer_noactive = new TimeNoActive(socket);
      // Begin to detect no action
      timer_noactive.execute();

      toClient.println("> ");
      // name = users.getName(index);  // name of the current user

      // Begin to serve the clients and accept commands
      while ((inputLine = fromClient.readLine()) != null) {
        // System.out.println(inputLine);

        // Command "whoelse", to see who are now in the chatroom
        if (inputLine.equals("whoelse")) {
          // int size = users.getSize();   // get the size of the current users
          for (int j = 0; j < size; ++j) {
            if (users.getRecord(j) && j != index) toClient.println(users.getName(j));
          }

          timer_noactive.reset();
          // timer_noactive.execute();

        }

        // Command "wholasthr", to see who logined in last hour
        else if (inputLine.equals("wholasthr")) {
          // int size = users.getSize();
          for (int j = 0; j < size; ++j) {
            if (users.getLastHour(j) && j != index) toClient.println(users.getName(j));
          }

          timer_noactive.reset();
          // timer_noactive.execute();

        }

        // Command "logout"
        else if (inputLine.equals("logout")) {
          users.resetRecord(index); // clear the record for the user
          break;
        }

        // Command "broadcast"
        else if (inputLine.length() > 9 && inputLine.substring(0, 9).equals("broadcast")) {
          // String name = users.getName(index);       // get the user name
          String message = inputLine.substring(9); // get the message after broadcast
          // int size = users.getSize();  // get the size of current users
          for (int i = 0; i < size; ++i) {
            if (users.getRecord(i) && i != index) {
              usermessage.addMessage(message, name, users.getName(i));
            }
          }

          timer_noactive.reset();
          // timer_noactive.execute();

        }

        // Command "Private"
        else if (inputLine.length() > 7 && inputLine.substring(0, 7).equals("message")) {
          int i = 7;
          while (inputLine.charAt(i) == ' ' && i < inputLine.length()) // Ignore the blank " "
          i++;

          // int size = users.getSize();
          int j = 0;
          for (j = 0; j < size; ++j) {
            if (users.getRecord(j)
                && inputLine.indexOf(users.getName(j)) == i) // It does not detect names like "win"
            break;
          } // and "windows", just return first find

          if (j == size) {
            toClient.println("This user is not online or not exist ");
          } else {
            String reciever = users.getName(j);
            String message =
                inputLine.substring(
                    i + reciever.length()); // message will stay after "private" and "name"
            usermessage.addMessage(message, name, reciever);
          }

          timer_noactive.reset();
          // timer_noactive.execute();

        }
        // Wrong command
        else {
          toClient.println("Wrong Command ");

          timer_noactive.reset();
          // timer_noactive.execute();
        }

        toClient.println("> ");
      }

      toClient.println("logout"); // act as a flag to stop the reciever of client

    } catch (IOException e) {
      System.err.println(e);
    }
  }
  /** Test of numOfRetransmits query */
  @Test
  public void test29_numOfRetransmitsQuery() {

    em.getTransaction().begin();
    UserMessage instance = new UserMessage();

    instance.setMessageId(T_MSG_ID_1);
    instance.setProcessingState(new ProcessingState(ProcessingStates.SUBMITTED));
    instance.setProcessingState(new ProcessingState(ProcessingStates.READY_TO_PUSH));
    instance.setProcessingState(new ProcessingState(ProcessingStates.PROCESSING));
    instance.setProcessingState(new ProcessingState(ProcessingStates.AWAITING_RECEIPT));

    em.persist(instance);

    em.getTransaction().commit();

    Long numberOfRetransmits = null;

    try {
      numberOfRetransmits =
          em.createNamedQuery("UserMessage.numOfRetransmits", Long.class)
              .setParameter("msgId", T_MSG_ID_1)
              .getSingleResult();

    } catch (NoResultException nr) {
      fail();
    }

    assertEquals(1, numberOfRetransmits.intValue());

    em.getTransaction().begin();

    instance.setProcessingState(new ProcessingState(ProcessingStates.READY_TO_PUSH));
    instance.setProcessingState(new ProcessingState(ProcessingStates.PROCESSING));
    instance.setProcessingState(new ProcessingState(ProcessingStates.AWAITING_RECEIPT));
    instance.setProcessingState(new ProcessingState(ProcessingStates.READY_TO_PUSH));
    instance.setProcessingState(new ProcessingState(ProcessingStates.PROCESSING));
    instance.setProcessingState(new ProcessingState(ProcessingStates.AWAITING_RECEIPT));

    em.merge(instance);

    em.getTransaction().commit();

    try {
      numberOfRetransmits =
          em.createNamedQuery("UserMessage.numOfRetransmits", Long.class)
              .setParameter("msgId", T_MSG_ID_1)
              .getSingleResult();

    } catch (NoResultException nr) {
      fail();
    }

    assertEquals(3, numberOfRetransmits.intValue());
  }