/** Processes current desires and breaks them down into subgoals using the censor component. */
  @Override
  protected Boolean processImpl(PlanParameter pp) {
    LOG.info("Run Default-Subgoal-Generation");
    Agent ag = pp.getActualPlan().getAgent();
    Desires des = ag.getComponent(Desires.class);

    boolean reval = processPersuadeOtherAgentsDesires(pp, ag);

    if (des != null) {
      Set<Desire> currentDesires;
      currentDesires = des.getDesiresByPredicate(GenerateOptionsOperator.prepareQueryProcessing);
      for (Desire d : currentDesires) {
        reval = reval || processQuery(d, pp, ag);
      }

      currentDesires = des.getDesiresByPredicate(GenerateOptionsOperator.prepareRevisionProcessing);
      for (Desire d : currentDesires) {
        reval = reval || processRevision(d, pp, ag);
      }

      currentDesires = des.getDesiresByPredicate(GenerateOptionsOperator.prepareUpdateProcessing);
      for (Desire d : currentDesires) {
        reval = reval || processUpdate(d, pp, ag);
      }

      currentDesires =
          des.getDesiresByPredicate(GenerateOptionsOperator.prepareScriptingProcessing);
      for (Desire d : currentDesires) {
        reval = reval || processScripting(d, pp, ag);
      }
    }
    return reval;
  }
  public boolean processScripting(Desire d, PlanParameter pp, Agent ag) {
    ScriptingComponent script = ag.getComponent(ScriptingComponent.class);
    Desires desires = ag.getComponent(Desires.class);
    desires.remove(d);
    List<Intention> intentions = script.getIntentions();
    String text = intentions.toString();

    int i = 0;
    for (Intention intention : intentions) {
      Desire des = new Desire(new FOLAtom(new Predicate("script" + i++)));
      desires.add(des);
      Subgoal sg = new Subgoal(ag, des);
      sg.newStack(intention);
      ag.getPlanComponent().addPlan(sg);
    }

    pp.report(
        "Add the new  actions '" + text + "' to the plan, chosen by desire: " + d.toString(),
        ag.getPlanComponent());
    return true;
  }
  /**
   * This is a helper method: Which searches for desires starting with the prefix 'v_'. It creates
   * RevisionRequests for such desires.
   *
   * @param pp The data-structure containing parameters for the operator.
   * @param ag The agent.
   * @return true if a new subgoal was created and added to the master-plan, false otherwise.
   */
  protected boolean processPersuadeOtherAgentsDesires(PlanParameter pp, Agent ag) {
    boolean reval = false;
    Desires desComp = ag.getComponent(Desires.class);
    if (desComp == null) return false;

    for (Desire desire : desComp.getDesires()) {
      // only add a plan if no plan for the desire exists.
      if (ag.getPlanComponent().countPlansFor(desire) > 0) continue;

      FolFormula formula = desire.getFormula();
      String atomStr = formula.toString().trim();
      boolean revisionDesire = atomStr.startsWith("r_");
      boolean queryDesire = atomStr.startsWith("q_");

      if (revisionDesire || queryDesire) {
        int si = formula.toString().indexOf("_") + 1;
        int li = formula.toString().indexOf("(", si);
        if (si == -1 || li == -1) continue;
        String recvName = formula.toString().substring(si, li);

        si = formula.toString().indexOf("(") + 1;
        li = formula.toString().lastIndexOf(")");
        if (si == -1 || li == -1) continue;
        String content = formula.toString().substring(si, li);

        LOG.info("'{}' wants '" + recvName + "' to believe: '{}'", ag.getName(), content);

        Subgoal sg = new Subgoal(ag, desire);
        FolParserB parser = new FolParserB(new StringReader(content));
        FOLAtom a = null;
        try {
          a = parser.atom(new FolSignature());
        } catch (ParseException e) {
          e.printStackTrace();
        }

        if (revisionDesire) {
          sg.newStack(new Revision(ag, recvName, a));
          ag.getPlanComponent().addPlan(sg);
          pp.report(
              "Add the new atomic action '"
                  + Revision.class.getSimpleName()
                  + "' to the plan, chosen by desire: "
                  + desire.toString(),
              ag.getPlanComponent());
        } else {
          sg.newStack(new Query(ag, recvName, a));
          ag.getPlanComponent().addPlan(sg);
          pp.report(
              "Add the new atomic action '"
                  + Query.class.getSimpleName()
                  + "' to the plan, chosen by desire: "
                  + desire.toString(),
              ag.getPlanComponent());
        }

        reval = true;
      }
    }
    return reval;
  }