Example #1
0
 /**
  * Returns whether an abstract action is defined by some PC-rule, i.e. whether the abstract action
  * matches with the head of some PC-rule.
  *
  * @param a the abstract action
  * @return true if the action is defined by some rule, false otherwise
  */
 public boolean defines(AbstractAction a) {
   APLFunction plan = a.getPlan();
   for (PCrule r : rules) {
     APLFunction head = r.getHead();
     if (head.getName().equals(plan.getName())
         && head.getParams().size() == plan.getParams().size()) return true;
   }
   return false;
 }
Example #2
0
 /**
  * Selects an applicable PC-rule. A PC-rule is applicable if the head matches the
  * event/message/abstract action <code>a</code> and the guard is satisfied by the beliefs of the
  * module. Returns a copy of the rule with fresh (unique) variables such that is does not
  * interfere with the variables that already occur in the plan from which the abstract action is
  * called.
  *
  * @param beliefbase the beliefs
  * @param a the event/message/abstract action
  * @param unfreshVars the list variables that cannot be used anymore
  * @param theta the substitution for applying the rule
  * @return the selected PC-rule
  * @throws NoRuleException thrown if no rule is specified for <code>a</code>
  */
 public PCrule selectRule(
     Beliefbase beliefbase, APLFunction a, ArrayList<String> unfreshVars, SubstList<Term> theta)
     throws NoRuleException {
   boolean norulefound = true;
   for (PCrule pcrule : rules) {
     SubstList<Term> theta2 = new SubstList<Term>();
     PCrule variant = pcrule.getVariant(unfreshVars);
     Query guard = variant.getGuard().clone();
     APLFunction head = variant.getHead();
     if (a == null) continue; // TODO why???
     if (Unifier.unify(head, a.clone(), theta2)) {
       norulefound = false;
       guard.applySubstitution(theta2);
       if (beliefbase.doQuery(guard, theta2)) {
         theta.putAll(theta2);
         return variant;
       }
     }
   }
   if (norulefound) throw new NoRuleException();
   return null;
 }
Example #3
0
  /**
   * Executes this action.
   *
   * @param module the module to execute this action for.
   * @return true if the action succeeds, false otherwise.
   */
  public PlanResult execute(APLModule module) {
    Beliefbase beliefs = module.getBeliefbase();
    PCrulebase pcrules = module.getPCrulebase();
    plan.evaluateArguments();

    SubstList<Term> theta = new SubstList<Term>();

    PCrule rule;
    try {
      rule = pcrules.selectRule(beliefs, plan, getTopParent().getVariables(), theta, module);
    } catch (NoRuleException e) {
      return new PlanResult(this, PlanResult.FAILED);
    }

    if (rule != null) {
      PlanSeq p = rule.getBody().clone();
      p.applySubstitution(theta);

      parent.removeFirst();
      parent.addFirst(p);
      return new PlanResult(this, PlanResult.SUCCEEDED);
    } else return new PlanResult(this, PlanResult.FAILED);
  }