コード例 #1
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;
  }