Exemplo n.º 1
0
 public static DialogueKBFormula processGoalExpression(DialogueKBFormula f, String goalVarName)
     throws Exception {
   HashMap<DialogueKBFormula, DialogueKBFormula> subs =
       new HashMap<DialogueKBFormula, DialogueKBFormula>();
   subs.put(DialogueKBFormula.create(".", null), DialogueKBFormula.create(goalVarName, null));
   return f.substitute(subs);
 }
Exemplo n.º 2
0
 public static DialogueOperatorEffect createIncrementForVariable(
     String var, DialogueKBFormula increment) throws Exception {
   DialogueKBFormula varf = DialogueKBFormula.create(var, null);
   Collection<DialogueKBFormula> args = new ArrayList<DialogueKBFormula>();
   args.add(varf);
   args.add(increment);
   DialogueKBFormula incf = DialogueKBFormula.create("+", args);
   return DialogueOperatorEffect.createAssignment(varf, incf, false);
 }
Exemplo n.º 3
0
  public static DialogueOperatorEffect createAssertion(DialogueKBFormula left) throws Exception {
    DialogueOperatorEffect f = new DialogueOperatorEffect();
    left = left.normalize();

    if (left.isConstant()) {
      f.type = EffectType.ASSIGNMENT;
      f.left = left;
      f.right = DialogueKBFormula.trueFormula;
      return f;
    } else if (left.isNegatedFormula()) {
      DialogueKBFormula child = (DialogueKBFormula) left.getFirstChild();
      if (child.isConstant()) {
        f.type = EffectType.ASSIGNMENT;
        f.left = child;
        f.right = DialogueKBFormula.falseFormula;
        return f;
      }
    }

    f.type = EffectType.ASSERTION;
    if (left.isNegatedFormula()) {
      left = (DialogueKBFormula) left.getFirstChild();
      f.right = DialogueKBFormula.falseFormula;
    } else {
      f.right = DialogueKBFormula.trueFormula;
    }
    f.left = left;
    return f;
  }
Exemplo n.º 4
0
  public static DialogueOperatorEffect getEffectExpr(Node c, NamedNodeMap att) throws Exception {
    boolean isSwapOut = c.getNodeName().toLowerCase().equals(XMLConstants.SWAPOUTID);
    boolean isInterrupt = c.getNodeName().toLowerCase().equals(XMLConstants.INTERRUPTID);
    boolean isSend = c.getNodeName().toLowerCase().equals(XMLConstants.SENDID);
    if (isSwapOut) return createSwapOut();
    else if (isInterrupt) return createInterrupt();
    else if (isSend) {
      Node send = att.getNamedItem(XMLConstants.IDID);
      if (send != null) {
        String sendEventName = send.getNodeValue();
        return createSend(sendEventName);
      }
    } else {
      Node nodeExpr = att.getNamedItem(XMLConstants.EXPRID);
      Node nodeGoal = att.getNamedItem(XMLConstants.GOALID);
      if (nodeGoal != null) {
        String goalName = nodeGoal.getNodeValue();
        if (StringUtils.isEmptyString(goalName))
          throw new Exception(
              "invalid goal effect: '" + XMLUtils.domNode2String(c, true, false) + "'");
        String varGoalName = buildVarNameForGoal(goalName);

        String valueString =
            (nodeExpr != null) ? StringUtils.cleanupSpaces(nodeExpr.getNodeValue()) : null;
        DialogueKBFormula value;
        if (StringUtils.isEmptyString(valueString)) value = DialogueKBFormula.parse(varGoalName);
        else value = DialogueKBFormula.parse(valueString);

        value = processGoalExpression(value, varGoalName);

        return DialogueOperatorEffect.createGoal(goalName, value);
      } else if (nodeExpr != null) {
        DialogueOperatorEffect e = DialogueOperatorEffect.parse(nodeExpr.getNodeValue());
        DialogueKBFormula f = null;
        if (e == null)
          throw new Exception(
              "Effect '"
                  + XMLUtils.domNode2String(c, true, false)
                  + "' returned an empty expression for effects (only assertions and assignments).");
        else if (e.isAssertion()
            && (((f = e.getAssertedFormula()) == null) || f.isConjunction() || f.isDisjunction()))
          throw new Exception("Invalid assertion: " + f);
        else if (e.isAssignment() && (e.getAssignedVariable() == null))
          throw new Exception("Invalid assignmend: " + f);
        else return e;
      }
    }
    return null;
  }
Exemplo n.º 5
0
 public Set<String> doesEffectUseAllKnownVariables(
     DialogueKBInterface is, Map<String, DialogueKBFormula> localVars) {
   Set<DialogueKBFormula> variables = null;
   variables = extractAllNamesUsed();
   if (isAssignment()) {
     if (variables == null) variables = new HashSet<DialogueKBFormula>();
     variables.add(getAssignedVariable());
   }
   Set<String> ret = null;
   if (variables != null) {
     for (DialogueKBFormula var : variables) {
       String vName = var.getName();
       if (!is.hasVariableNamed(vName, ACCESSTYPE.AUTO_OVERWRITEAUTO)
           && ((localVars == null) || !localVars.containsKey(vName))) {
         if (ret == null) ret = new HashSet<String>();
         ret.add(vName);
       }
     }
   }
   return ret;
 }
Exemplo n.º 6
0
  public static DialogueOperatorEffect createAssignment(
      DialogueKBFormula left, Object value, boolean parseValue) throws Exception {
    if (!left.isConstant()) throw new Exception("Invalid left hand side of assignment: " + left);
    if (value != null) {
      if (value instanceof String) {
        String rightString = ((String) value).toLowerCase();
        if (rightString.equals("true")) return DialogueOperatorEffect.createAssertion(left);
        else if (rightString.equals("false"))
          return DialogueOperatorEffect.createAssertion(left.negate());
      } else if (value instanceof DialogueKBFormula) {
        if (((DialogueKBFormula) value).isConstant()) {
          String rightString = ((DialogueKBFormula) value).getName();
          if (rightString.equals("true")) return DialogueOperatorEffect.createAssertion(left);
          else if (rightString.equals("false"))
            return DialogueOperatorEffect.createAssertion(left.negate());
        }
      } else if (value instanceof Boolean) {
        if ((Boolean) value) return DialogueOperatorEffect.createAssertion(left);
        else return DialogueOperatorEffect.createAssertion(left.negate());
      }
    }

    DialogueOperatorEffect f = new DialogueOperatorEffect();
    f.type = EffectType.ASSIGNMENT;
    f.left = left;
    if (value instanceof DialogueKBFormula) {
      DialogueKBFormula right = (DialogueKBFormula) value;
      // if (!right.isNumericFormula() && !right.isConstant()) throw new Exception("Right hand side
      // of assignment is not a numeric expression or constant: "+right);
      f.right = right;
    } else {
      if (parseValue && ((value instanceof String) || (value instanceof Number))) {
        try {
          if (value != null) {
            if (value instanceof String && !DialogueKBFormula.isStringConstant((String) value)) {
              value = DialogueKBFormula.generateStringConstantFromContent((String) value);
            }
            DialogueKBFormula nv = DialogueKBFormula.parse(value.toString());
            f.right = nv;
          }
        } catch (Exception e) {
          f.value = value;
        }
      } else f.value = value;
    }
    return f;
  }
Exemplo n.º 7
0
 public Set<DialogueKBFormula> extractAllNamesUsed() {
   if (isAssignment()) {
     Object thing = getAssignedExpression();
     if (thing instanceof DialogueKBFormula) {
       DialogueKBFormula r = (DialogueKBFormula) thing;
       return r.extractAllNamesUsed();
     } else return null;
   } else if (isAssertion()) {
     DialogueKBFormula f = getAssertedFormula();
     return f.extractAllNamesUsed();
   } else if (isGoalAchievement()) {
     DialogueKBFormula f = getGoalValue();
     return f.extractAllNamesUsed();
   } else if (isImplication()) {
     Set<DialogueKBFormula> resa = getAntecedent().extractAllNamesUsed();
     Set<DialogueKBFormula> resc = getConsequent().extractAllNamesUsed();
     Set<DialogueKBFormula> ret = null;
     if (resa != null) ret = resa;
     if (resc != null) {
       if (getConsequent().isAssignment()) {
         resc.add(getConsequent().getAssignedVariable());
       }
       if (ret != null) ret.addAll(resc);
       else ret = resc;
     }
     return ret;
   } else if (isAssignmentList()) {
     Set<DialogueKBFormula> res = null;
     for (DialogueOperatorEffect x : getAssignmentList()) {
       Set<DialogueKBFormula> newNames = x.extractAllNamesUsed();
       if (newNames != null) {
         if (res == null) res = newNames;
         else res.addAll(newNames);
       }
     }
     return res;
   } else return null;
 }
Exemplo n.º 8
0
 public DialogueKBFormula getAssignedVariable() {
   if (isAssignment() && left != null && left.isVariable()) return left;
   else return null;
 }
Exemplo n.º 9
0
 public Boolean getAssertionSign() {
   if (isAssertion()) return right.isTrivialTruth();
   else return null;
 }
Exemplo n.º 10
0
 public static DialogueOperatorEffect createIncrementForVariable(String var, Number val)
     throws Exception {
   DialogueKBFormula increment = DialogueKBFormula.create(val + "", null);
   return DialogueOperatorEffect.createIncrementForVariable(var, increment);
 }
Exemplo n.º 11
0
 public static DialogueOperatorEffect createAssignment(String varName, Object value)
     throws Exception {
   return createAssignment(DialogueKBFormula.create(varName, null), value, true);
 }