Ejemplo n.º 1
0
  /**
   * @param agentName
   * @param actionName
   * @param parameters
   * @param unifier
   * @return Maps internal Jason agent actions to camel messages
   */
  public Object agentInternallyActed(
      String agentName, String actionName, List<Term> parameters, Unifier unifier) {
    Object actionsucceeded = true;
    Exchange exchange = endpoint.createExchange();

    // Agent action can only be processed by an endpoint of type "action"
    if (endpoint.getUriOption().contains("action")) {

      try {

        List<String> paramsAsStrings = new ArrayList<String>();
        for (Term t : parameters) {
          paramsAsStrings.add(t.toString());
        }
        sendActionToCamel(agentName, actionName, paramsAsStrings, exchange, "");

      } catch (Exception e) {

      } finally {

        // log exception if an exception occurred and was not handled
        if (exchange.getException() != null) {
          getExceptionHandler()
              .handleException("Error processing exchange", exchange, exchange.getException());
          actionsucceeded = false;
        } else {
          String ep = endpoint.getExchangePattern().toString();
          // Just send out camel message and inform success to agent
          if (ep.equals("InOnly")) actionsucceeded = true;

          // Unification is applicable to InOut exchanges only. This waits for the return message
          // from the exchange
          else if (ep.equals("InOut")) {
            if (exchange.hasOut()) {
              List<String[]> mappings = getResultHeaderSplitted();
              for (String[] mapping : mappings) {
                int unPos = Integer.parseInt(mapping[1]);
                String headerVal = mapping[0];

                // ArrayList<String> l = (ArrayList<String>)exchange.getIn().getHeader(headerVal);
                // Iterator<String> it = l.iterator();
                String unVal = exchange.getIn().getHeader(headerVal).toString();
                try {
                  unifier.unifies(parameters.get(unPos), ASSyntax.parseTerm(unVal));
                } catch (jason.asSyntax.parser.ParseException e) {
                  System.out.println(
                      "Error parsing result header from synchronous InOut action: " + unVal);
                  return false;
                }
              }
              return true;
            } else actionsucceeded = false;
          }
        }
      }
    }
    return actionsucceeded;
  }
Ejemplo n.º 2
0
 ListTerm deleteFromList(int index, ListTerm l) {
   ListTerm r = new ListTermImpl();
   ListTerm last = r;
   int i = 0;
   for (Term t : l) {
     if ((i++) != index) last = last.append(t.clone());
   }
   return r;
 }
Ejemplo n.º 3
0
 ListTerm deleteFromList(Term element, ListTerm l, Unifier un) {
   Unifier bak = un;
   ListTerm r = new ListTermImpl();
   ListTerm last = r;
   for (Term t : l) {
     boolean u = un.unifies(element, t);
     if (u) un = bak.clone();
     else last = last.append(t.clone());
   }
   return r;
 }
Ejemplo n.º 4
0
  public Proposition makeGround() {
    PropositionImpl ground = (PropositionImpl) this.clone();
    for (Term term : ground.getTerms()) {
      if (term.isVar() && !term.isGround()) {
        Term t = new Atom(term.toString().toLowerCase());
        jason.asSemantics.Unifier un = new jason.asSemantics.Unifier();
        if (un.unifies(t, term)) {
          term.apply(un);
        }
      }
    }

    return ground;
  }
Ejemplo n.º 5
0
  /**
   * Implement the method in the standard class for agent doctor to call. Executes the internal
   * action. It should return a Boolean or an Iterator. A true boolean return means that the IA was
   * successfully executed.
   *
   * @param ts TransitionSystem
   * @param un Unifier
   * @param args Term array
   * @return true or false
   * @throws Exception
   */
  @Override
  public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {

    // print a message to confirm action (appear in the debug window)
    ts.getAg().getLogger().info("Executing internal action 'robot.send'.");

    // loop through each argument e.g. if robot.send(Hello, Robot)
    // the arguments will be Hello and Robot.
    // in our doctor, we only use message "start" to wake up the robot
    for (Term t : args) {
      // use the static comms class to send the message to the robot
      comm.write(t.toString());
    }

    // everything ok, so return true
    return true;
  }
Ejemplo n.º 6
0
  /**
   * @param jasonAction
   * @return Maps external Jason agent actions to camel messages
   */
  public boolean agentActed(JasonAction jasonAction) {
    boolean actionsucceeded = true;
    Exchange exchange = endpoint.createExchange();
    if (endpoint.getUriOption().contains("action")) {
      try {
        ActionExec action = jasonAction.getAction();

        String agName = jasonAction.getAgName();
        List<String> paramsAsStrings = new ArrayList<String>();
        for (Term t : action.getActionTerm().getTerms()) paramsAsStrings.add(t.toString());

        // extract annotations
        List<Term> ann = action.getActionTerm().getAnnots();
        String annots = "";
        if (ann != null) annots = ann.toString(); // StringUtils.join(ann, ',');    	
        exchange.getIn().setBody(action.getActionTerm().toString(), String.class);
        sendActionToCamel(
            agName, action.getActionTerm().getFunctor(), paramsAsStrings, exchange, annots);

      } catch (Exception e) {

      } finally {

        // log exception if an exception occurred and was not handled
        if (exchange.getException() != null) {
          getExceptionHandler()
              .handleException("Error processing exchange", exchange, exchange.getException());
          actionsucceeded = false;
        } else {
          String ep = endpoint.getExchangePattern().toString();

          // Just send out camel message and inform success to agent
          if (ep.equals("InOnly")) actionsucceeded = true;
          else if (ep.equals("InOut")) {
            if (exchange.getOut() != null) actionsucceeded = true;
            else actionsucceeded = false;
          }
        }
      }
    }
    return actionsucceeded;
  }