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