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; }
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; }