@Override
  public Object execute(TransitionSystem ts, Unifier un, Term[] terms) throws Exception {
    ListTerm positions = new ListTermImpl();
    ListTerm agents = new ListTermImpl();

    model = ((CoordinatorArch) ts.getUserAgArch()).getModel();
    Graph graph = model.getGraph();

    model.coloringGraph();
    List<List<Vertex>> actualZones = graph.getZones();
    // order zones by vertex value
    Collections.sort(actualZones, zoneComparator);

    List<List<Vertex>> bestZones = graph.getBestZones(); // the best zones in the graph
    if (!bestZones.isEmpty()) {
      if (bestZones.size() == 2) {
        ListTerm[] listTerms = coordinateGroup(1, actualZones, bestZones.get(0));
        agents.addAll(listTerms[0]);
        positions.addAll(listTerms[1]);
        listTerms = coordinateGroup(2, actualZones, bestZones.get(1));
        agents.addAll(listTerms[0]);
        positions.addAll(listTerms[1]);
      } else {
        ListTerm[] listTerms = coordinateGroup(3, actualZones, bestZones.get(0));
        agents.addAll(listTerms[0]);
        positions.addAll(listTerms[1]);
      }

    } else {
      ListTerm[] listTerms = coordinateGroup(3, actualZones, null);
      agents.addAll(listTerms[0]);
      positions.addAll(listTerms[1]);
    }
    return un.unifies(terms[0], agents) & un.unifies(terms[1], positions);
  }
Пример #2
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;
 }
Пример #3
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;
  }
 public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
   if (args[0].isNumeric()) {
     int i = Integer.parseInt(args[0].toString());
     if (i == 1) return un.unifies((StringTerm) receiveMail(), args[1]);
   }
   return null;
 }
  @Override
  public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
    long time = System.currentTimeMillis();

    Term idTerm = args[0];
    return un.unifies(idTerm, JasonHelper.getTerm(time));
  }
Пример #6
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;
  }
Пример #7
0
  @Override
  public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
    checkArguments(args);

    if (args[0].isNumeric() && args[1].isString()) {
      return un.unifies(
          args[2], deleteFromString((int) ((NumberTerm) args[0]).solve(), (StringTerm) args[1]));
    }
    if (args[0].isNumeric() && args[1].isList()) {
      return un.unifies(
          args[2], deleteFromList((int) ((NumberTerm) args[0]).solve(), (ListTerm) args[1]));
    }
    if (args[0].isString() && args[1].isString()) {
      return un.unifies(args[2], deleteFromString((StringTerm) args[0], (StringTerm) args[1]));
    }

    // first element as term
    if (args[1].isList()) {
      return un.unifies(args[2], deleteFromList(args[0], (ListTerm) args[1], un.clone()));
    }
    throw new JasonException("Incorrect use of the internal action '.delete' (see documentation).");
  }
  @Override
  public Object execute(TransitionSystem ts, Unifier unifier, Term[] terms) throws Exception {
    Vertex position = MapAgent.getInstance().getVertex(terms[0].toString());
    Vertex destination = MapAgent.getInstance().getVertex(terms[1].toString());
    Term nextHopTerm = terms[2];

    Vertex nextHop = MapAgent.getInstance().getBestHopToVertex(position, destination);

    if (nextHop == null) {
      return false;
    } else {
      return unifier.unifies(nextHopTerm, JasonHelper.getTerm(nextHop.getIdentifier()));
    }
  }
Пример #9
0
  @Override
  public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
    // execute the internal action
    ListTerm in = (ListTerm) args[0];
    int count = (int) ((NumberTerm) args[1]).solve();

    ListTermImpl a = new ListTermImpl();

    for (Term t : in) {
      if (count <= 0) break;
      a.append(t);
      count--;
    }

    un.unifies(a, args[2]);

    // everything ok, so returns true
    return true;
  }
Пример #10
0
  @Override
  public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
    checkArguments(args);

    StringTerm st = (StringTerm) args[0];
    HouseModel hm = getTerrain(ts);
    if (hm == null) return false;
    String[] array = hm.planRoute(ts.getUserAgArch().getAgName(), st.getString());
    if (array == null) return false;

    ListTerm ln = new ListTermImpl();
    ListTerm tail = ln;

    for (String orientation : array) {
      tail = tail.append(ASSyntax.createString(orientation));
    }

    return un.unifies(args[1], ln);
  }
Пример #11
0
  @Override
  public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {

    NumberTerm size = (NumberTerm) args[0]; // tamanho da cartela
    NumberTerm max = (NumberTerm) args[1]; // TETO para um numero sorteado

    Term[] terms = new Term[1];
    terms[0] = args[1];

    box.clear();

    Random r = new Random();
    while (box.size() < size.solve()) box.add((int) r.evaluate(ts, terms) + 1);
    // box.add(r.nextInt((int)max.solve()) + 1);     //garantir a não repetição dos números da
    // cartela

    ListTerm result = new ListTermImpl();
    for (Integer i : box) {
      Term t = new NumberTermImpl(i);
      result.add(t); // converter a cartela em um feedback para o agente
    }

    return un.unifies(result, args[2]);
  }