示例#1
0
  /**
   * This method uses the given InformationState to check the current state of this template. This
   * State contains which kind of preconditions are fulfilled (compares, indicators, and triggers)
   * and keeps track of the total number of non-fulfilled preconditions,
   *
   * @param is - the Current InformationState
   * @return
   */
  public TemplateState checkTemplate(InformationState is) {
    /* Initializing variables */
    TemplateState resultState;
    boolean comparesSatisfied = true;
    ;
    boolean indicatorsSatisfied = true;
    boolean triggersSatisfied = true;
    int nrMissing = 0;

    /* Check if all Compares are fulfilled */
    for (Precondition c : compares) {
      boolean result = c.isValid(is);
      if (!result) {
        comparesSatisfied = false;
        nrMissing++;
      }
    }

    /* Check if all Indicators are fulfilled */
    for (Precondition c : indicators) {
      boolean result = c.isValid(is);
      if (!result) {
        indicatorsSatisfied = false;
        nrMissing++;
      }
    }

    /* Check if all Triggers are fulfilled */
    for (Precondition c : triggers) {
      boolean result = c.isValid(is);
      if (!result) {
        triggersSatisfied = false;
        nrMissing++;
      }
    }

    /* Create a new TemplateState */
    resultState =
        new TemplateState(
            this,
            comparesSatisfied,
            indicatorsSatisfied,
            triggersSatisfied,
            (compares.size() + indicators.size() + triggers.size()),
            nrMissing);

    /* Determine which choice-blocks are chosen, and make a list of all Effects that have to be executed. */
    ArrayList<Effect> currEffects = new ArrayList<Effect>();
    Behaviour currBehaviour = null;

    if (nrMissing == 0) {
      for (ArrayList<Choice> option : optional) {
        double chance = random.nextDouble();
        for (Choice choice : option) {
          chance = chance - choice.chance;
          if (chance < 0) {
            currEffects.addAll(choice.effects);
            if (choice.behaviour != null) {
              currBehaviour = choice.behaviour;
              if (currBehaviour != null) {
                System.out.println("Overwriting Behaviour in template  " + id + "(" + name + ")");
              }
            }
            break;
          }
        }
      }
    }

    currEffects.addAll(effects);
    if (behaviour != null) {
      if (currBehaviour != null) {
        System.out.println("Overwriting Behaviour in template  " + id + "(" + name + ")");
      }
      currBehaviour = behaviour;
    }

    /* Put the list with all Effects and the Behaviour in the TemplateState, and return it */
    resultState.setEffects(currEffects);
    if (currBehaviour != null) {
      resultState.setBehaviour(currBehaviour);
    }

    return resultState;
  }