public override bool IsConstant(Formula formula, out RDElement result) { if (formula.Parameters == null) { if (formula.OperationName == "BLACK") { result = new RDNumber(1); return(true); } if (formula.OperationName == "WHITE") { result = new RDNumber(2); return(true); } if (formula.OperationName == "DIFFERENT") { result = new RDNumber(1); return(true); } if (formula.OperationName == "SAME") { result = new RDNumber(2); return(true); } if (formula.OperationName == "UNDECIDED") { result = new RDNumber(0); return(true); } } return(base.IsConstant(formula, out result)); }
private bool FireSetters(Rule rule, Dictionary <string, RDElement> assginedValue) { bool successSet = false; foreach (Result r in rule.Results) { RDElement[] parameterValues = r.Parameters.Select(p => Calculate(p, assginedValue)).ToArray(); RDElement result = Calculate(r.AssignFormula, assginedValue); MethodInfo method = null; if (!setters.TryGetValue(r.SetterName, out method)) { throw new Exception("THEN部分左值出错:Setter未定义:" + r.SetterName); } List <object> input = (parameterValues as object[]).ToList(); input.Add(result); input.Add(FactPool); if ((bool)method.Invoke(Context.Setters, input.ToArray())) { tempSetterLog = string.Join(", ", tempAssginedValue.Select(kv => kv.Key + "=" + kv.Value.ToString())) + " => " + Environment.NewLine + r.SetterName + "(" + string.Join(",", (object[])parameterValues) + ") := " + result; successSet = true; } } return(successSet); }
private RDElement Calculate(Formula formula, Dictionary <string, RDElement> assginedValue) { if (formula.Parameters == null) { RDElement value = null; if (Context.Constant.IsConstant(formula, out value)) { return(value); } if (!assginedValue.TryGetValue(formula.OperationName, out value)) { throw new Exception("推理机错误:变量未定义:" + formula.OperationName); } return(value); } else { if (formula.OperationName == "And" && formula.Parameters.Length == 2) { RDBool left = Calculate(formula.Parameters[0], assginedValue) as RDBool; if (left.Data == true) { return(Calculate(formula.Parameters[1], assginedValue) as RDBool); } else { return(left); } } else if (formula.OperationName == "Or" && formula.Parameters.Length == 2) { RDBool left = Calculate(formula.Parameters[0], assginedValue) as RDBool; if (left.Data == false) { return(Calculate(formula.Parameters[1], assginedValue) as RDBool); } else { return(left); } } List <object> input = formula.Parameters.Select(p => (object)Calculate(p, assginedValue)).ToList(); input.Add(FactPool); MethodInfo method = null; if (!operations.TryGetValue(formula.OperationName, out method)) { throw new Exception("推理机错误:函数未定义:" + formula.OperationName); } RDElement result = method.Invoke(Context.Operations, input.ToArray()) as RDElement; return(result); } }