Ejemplo n.º 1
0
  /**
   * Compose write and append print updates. The type write or append as well as the order of the
   * updates are taken into account.
   *
   * @param compAPI
   */
  private void composeAppend(PluginCompositionAPI compAPI) {
    for (Location l : compAPI.getAffectedLocations()) {
      if (!FILE_OUTPUT_FUNC_NAME.equals(l.name)) continue;
      LinkedList<Element> elems1 = new LinkedList<>();
      LinkedList<Element> elems2 = new LinkedList<>();
      Set<Element> contributingAgents = new HashSet<Element>();
      Set<ScannerInfo> contributingNodes = new HashSet<ScannerInfo>();
      String action = APPEND_ACTION;

      // if the second set does not have a basic update,
      // add all the updates from the first set as well
      if (!compAPI.isLocUpdatedWithActions(2, l, Update.UPDATE_ACTION)) {
        for (Update update : compAPI.getLocUpdates(1, l)) {
          if (APPEND_ACTION.equals(update.action) || WRITE_ACTION.equals(update.action)) {
            Element value = update.value;
            if (!(update.value instanceof ListElement))
              value = new ListElement(Arrays.asList(new Element[] {value}));
            ListElement list = (ListElement) value;
            elems1.addAll(list.getList());
            contributingAgents.addAll(update.agents);
            contributingNodes.addAll(update.sources);
          }
          if (WRITE_ACTION.equals(update.action)) action = WRITE_ACTION;
        }
      }
      for (Update update : compAPI.getLocUpdates(2, l)) {
        if (APPEND_ACTION.equals(update.action) || WRITE_ACTION.equals(update.action)) {
          Element value = update.value;
          if (!(update.value instanceof ListElement))
            value = new ListElement(Arrays.asList(new Element[] {value}));
          ListElement list = (ListElement) value;
          elems2.addAll(list.getList());
          contributingAgents.addAll(update.agents);
          contributingNodes.addAll(update.sources);
        }
        if (WRITE_ACTION.equals(update.action)) {
          action = WRITE_ACTION;
          elems1.clear();
        }
      }
      if (!elems1.isEmpty() || !elems2.isEmpty()) {
        LinkedList<Element> outputResult = elems1;
        if (outputResult.isEmpty()) outputResult = elems2;
        else if (!elems2.isEmpty()) {
          outputResult = new LinkedList<>();
          outputResult.addAll(elems1);
          outputResult.addAll(elems2);
        }
        compAPI.addComposedUpdate(
            new Update(
                l,
                new ListElement(new ArrayList<Element>(outputResult)),
                action,
                contributingAgents,
                contributingNodes),
            this);
      }
    }
  }
Ejemplo n.º 2
0
  private void composePrint(PluginCompositionAPI compAPI) {
    String outputResult1 = "";
    String outputResult2 = "";
    Set<Element> contributingAgents = new HashSet<Element>();
    Set<ScannerInfo> contributingNodes = new HashSet<ScannerInfo>();

    // First, add all the updates in the second set
    for (Update u : compAPI.getLocUpdates(2, PRINT_OUTPUT_FUNC_LOC)) {
      if (u.action.equals(PRINT_ACTION)) {
        if (!outputResult2.isEmpty()) outputResult2 += '\n';
        outputResult2 += u.value.toString();
        contributingAgents.addAll(u.agents);
        contributingNodes.addAll(u.sources);
      } else compAPI.addComposedUpdate(u, this);
    }

    // if the second set does not have a basic update,
    // add all the updates from the first set as well
    if (!compAPI.isLocUpdatedWithActions(2, PRINT_OUTPUT_FUNC_LOC, Update.UPDATE_ACTION)) {
      for (Update u : compAPI.getLocUpdates(1, PRINT_OUTPUT_FUNC_LOC)) {
        if (u.action.equals(PRINT_ACTION)) {
          if (!outputResult1.isEmpty()) outputResult1 += '\n';
          outputResult1 += u.value.toString();
          contributingAgents.addAll(u.agents);
          contributingNodes.addAll(u.sources);
        } else compAPI.addComposedUpdate(u, this);
      }
    }
    if (!outputResult1.isEmpty() || !outputResult2.isEmpty()) {
      String outputResult = outputResult1;
      if (outputResult.isEmpty()) outputResult = outputResult2;
      else if (!outputResult2.isEmpty()) outputResult = outputResult1 + '\n' + outputResult2;
      compAPI.addComposedUpdate(
          new Update(
              PRINT_OUTPUT_FUNC_LOC,
              new StringElement(outputResult),
              PRINT_ACTION,
              contributingAgents,
              contributingNodes),
          this);
    }
  }