Exemplo n.º 1
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);
  }
  private ListTerm[] coordinateGroup(
      int group, List<List<Vertex>> actualZones, List<Vertex> bestZone) {
    ListTerm positions = new ListTermImpl();
    ListTerm agents = new ListTermImpl();

    String[] missions;
    if (group == 1) {
      missions = new String[] {"mOccupyZone1", "mRepairZone1"};
    } else if (group == 2) {
      missions = new String[] {"mOccupyZone2", "mRepairZone2"};
    } else {
      missions = new String[] {"mOccupyZone1", "mRepairZone1", "mOccupyZone2", "mRepairZone2"};
    }

    List<Entity> coworkers = model.getCoworkersWithMission(Arrays.asList(missions));

    Graph graph = model.getGraph();

    List<Vertex> groupZone = null;
    if (bestZone != null && !bestZone.isEmpty()) {
      for (List<Vertex> zone : actualZones) {
        if (graph.hasAtLeastOneVertexOnZone(zone, bestZone)) {
          groupZone = zone;
          continue;
        }
      }
    } else if (actualZones != null && !actualZones.isEmpty()) {
      groupZone = actualZones.get(0);
    }

    List<Vertex> targets = new ArrayList<Vertex>();
    if (groupZone == null || groupZone.isEmpty()) {
      if (bestZone != null && !bestZone.isEmpty()) {
        targets.addAll(bestZone);
      }
    } else {
      if (bestZone != null && !bestZone.isEmpty()) {
        bestZone.removeAll(groupZone);
        targets.addAll(bestZone);
      }

      // zone neighbors
      List<Vertex> zoneNeighbors = model.getZoneNeighbors(groupZone);
      // order neighbors by vertex value
      Collections.sort(zoneNeighbors, comparator);

      targets.addAll(zoneNeighbors);
    }

    if (targets.isEmpty()) {
      return new ListTerm[] {agents, positions};
    }

    for (Entity coworker : coworkers) {
      if (coworker.getStatus().equals(Percept.STATUS_DISABLED)) {
        continue;
      }
      Vertex target = null;
      Vertex agentPosition = coworker.getVertex();
      if (bestZone.contains(agentPosition)
          && model.isFrontier(agentPosition)) { // the agent is part of the best zone's frontier
        List<Entity> agsOnSameVertex = model.getCoworkersOnSameVertex(coworker);
        if (!agsOnSameVertex.isEmpty()) { // if there are other agents on the same vertex
          boolean canMove = true;
          for (Entity ag : agsOnSameVertex) {
            if (ag.getId() > coworker.getId()) {
              canMove = false; // only the agent with the lower id can move
              break;
            }
          }
          if (!canMove) {
            continue;
          }
        } else {
          if (model.hasActiveCoworkersOnNeighbors(
              coworker)) { // go to a free neighbor if has two or more coworkers on neighbors
            Vertex neighbor = model.getFreeNeighborOutOfZone(agentPosition);
            if (null != neighbor) {
              agents.add(ASSyntax.createString(coworker.getName()));
              positions.add(ASSyntax.createString("vertex" + neighbor.getId()));
            }
            continue;
          } else {
            continue; // the agent must not move if he is in the frontier and has no other agent on
                      // the same vertex
          }
        }
      }

      if (null != targets && !targets.isEmpty()) {
        //				target = model.closerVertex(agentPosition, targets);
        target = targets.get(0);
        if (null != target) {
          targets.remove(target);
        }
      }

      if (null != target) {
        agents.add(ASSyntax.createString(coworker.getName()));
        positions.add(ASSyntax.createString("vertex" + target.getId()));
      }
    }
    return new ListTerm[] {agents, positions};
  }