public static IKnowledge Generate(object obj) { var shape = obj as ShapeSymbol; var goal = obj as EqGoal; var equation = obj as Equation; if (shape != null) { return(new AGShapeExpr(ExprG.Generate(shape), shape)); } if (goal != null) { return(new AGPropertyExpr(ExprG.Generate(goal), goal)); } if (equation != null) { return(new AGEquationExpr(ExprG.Generate(equation), equation)); } var knowledge = new IKnowledge(ExprG.Generate(obj)); knowledge.Tag = obj; return(knowledge); }
public override void RetrieveRenderKnowledge() { if (_equation == null) { return; } if (_equation.CachedEntities.Count == 0) { return; } var lst = new ObservableCollection <IKnowledge>(); foreach (var cacheObj in _equation.CachedEntities) { var cacheShapeSymbol = cacheObj as ShapeSymbol; var cacheGoal = cacheObj as EqGoal; var cacheEq = cacheObj as Equation; if (cacheShapeSymbol != null) { throw new Exception("TODO"); /* starPadSDK.MathExpr.Expr expr = ExprG.Generate(cacheShapeSymbol); * var agShape = new AGShapeExpr(expr, cacheShapeSymbol); * lst.Add(agShape);*/ } else if (cacheGoal != null) { throw new Exception("TODO"); /* starPadSDK.MathExpr.Expr expr = ExprG.Generate(cacheGoal); * var agGoal = new AGPropertyExpr(expr, cacheGoal); * lst.Add(agGoal);*/ } else if (cacheEq != null) { starPadSDK.MathExpr.Expr expr = ExprG.Generate(cacheEq); var agEq = new AGEquationExpr(expr, cacheEq); lst.Add(agEq); } else { var boolValue = cacheObj as bool?; if (boolValue != null) { starPadSDK.MathExpr.Expr expr = ExprG.Generate(boolValue); var agEq = new IKnowledge(expr); lst.Add(agEq); } } } RenderKnowledge = lst; }
public static IKnowledge Generate(object obj) { var shape = obj as ShapeSymbol; var goal = obj as EqGoal; var equation = obj as Equation; if (shape != null) { return new AGShapeExpr(ExprG.Generate(shape), shape); } if (goal != null) { return new AGPropertyExpr(ExprG.Generate(goal), goal); } if (equation != null) { return new AGEquationExpr(ExprG.Generate(equation), equation); } var knowledge = new IKnowledge(ExprG.Generate(obj)); knowledge.Tag = obj; return knowledge; }
private bool EvalExprPatterns(Expr expr, object obj, ShapeType? st, out object output, bool userInput = false) { output = null; //non-deterministic Multi-Candidate selection var dict = obj as Dictionary<PatternEnum, object>; //ambiguity of input pattern if (dict != null) { EvalExprPatterns(expr, dict, out output); obj = output; } //deterministic var ss = obj as ShapeSymbol; if (ss != null) return EvalExprPatterns(expr, ss, out output, userInput); //deterministic var goal = obj as EqGoal; if (goal != null) return EvalExprPatterns(expr, goal, out output, userInput); var query = obj as Query; if (query != null) return EvalExprPatterns(expr, query, out output, userInput); var equation = obj as Equation; if (equation != null) return EvalExprPatterns(expr, equation, out output, userInput); //non-deterministic Constraint-Solving var str = obj as string; var gQuery = new Query(str, st); if (str != null) return EvalExprPatterns(expr, gQuery, out output, userInput); if (userInput) { var iknowledge = new IKnowledge(expr); iknowledge.Tag = obj; output = iknowledge; } return false; }
private bool GoalVerify(IKnowledge obj, EqGoal eqGoal, out string msg, out object output) { msg = AGTutorMessage.VerifyWrong; output = null; List<Tuple<object, object>> trace = null; var agPropExpr = new AGPropertyExpr(obj.Expr, eqGoal); agPropExpr.IsSelected = true; agPropExpr.GenerateSolvingTrace(); trace = agPropExpr.AutoTrace; BehaviorGraphNode node; if (trace == null || trace.Count == 0) { node = UserGraph.UpdateSolvingCache(agPropExpr); //node = UserGraph.SearchInnerLoopNode(eqGoal); if (node == null) return false; } if (trace != null) { bool matchResult = UserGraph.Match(trace); if (!matchResult) return false; //insert nodes UserGraph.Insert(trace); CurrentStateNode = UserGraph.SearchInnerLoopNode(obj); //update _currentStateNode; } else { CurrentStateNode = UserGraph.SearchInnerLoopNode(eqGoal); } /* var nextTuple1 = UserGraph.SearchNextInnerLoopNode(CurrentStateNode); if (nextTuple1 == null) // query-end { msg = AGTutorMessage.SolvedProblem; return true; } */ msg = AGTutorMessage.VerifyCorrect; return true; }
private bool ShapeVerify(IKnowledge obj, ShapeSymbol shape, out string msg, out object output) { msg = AGTutorMessage.VerifyWrong; output = null; List<Tuple<object, object>> trace = null; var agShapeExpr = new AGShapeExpr(obj.Expr, shape); agShapeExpr.IsSelected = true; agShapeExpr.GenerateSolvingTrace(); trace = agShapeExpr.AutoTrace; if (trace == null || trace.Count == 0) return false; /* var lastTuple = trace[trace.Count - 1] as Tuple<object, object>; var lastLst = lastTuple.Item2 as List<object>; Debug.Assert(lastLst != null); Debug.Assert(lastLst.Count != 0); var lastTs = lastLst[lastLst.Count - 1] as TraceStepExpr;*/ bool matchResult = UserGraph.Match(trace); //match and update if (!matchResult) return false; //insert nodes UserGraph.Insert(trace); CurrentStateNode = UserGraph.SearchInnerLoopNode(obj); //update _currentStateNode; //var nextTuple1 = UserGraph.SearchNextInnerLoopNode(CurrentStateNode); /* if (nextTuple1 == null) // query-end { msg = AGTutorMessage.SolvedProblem; return true; }*/ msg = AGTutorMessage.VerifyCorrect; return true; }
private void SurfaceVerify(IKnowledge source, IKnowledge obj, out string msg, out object output) { msg = AGTutorMessage.VerifyWrong; output = null; var eqGoalExpr = obj as AGPropertyExpr; if (eqGoalExpr != null) { GoalVerify(source, eqGoalExpr.Goal, out msg, out output); } var shapeExpr = obj as AGShapeExpr; if (shapeExpr != null) { ShapeVerify(source, shapeExpr.ShapeSymbol, out msg, out output); } /* var eqGoalLst = obj as List<object>; if (eqGoalLst != null) { foreach (var tempObj in eqGoalLst) { var gGoal = tempObj as AG; bool verified = GoalVerify(obj, gGoal, out msg, out output); if (verified) return; } }*/ }
private void DeepVerify(IKnowledge source, IKnowledge obj, out string msg, out object output) { msg = AGTutorMessage.VerifyWrong; object result; var shapeExpr = obj as AGShapeExpr; if (shapeExpr != null) { result = Reasoner.Instance.RelationValidate(shapeExpr.ShapeSymbol, out output); } else { result = Reasoner.Instance.RelationValidate(obj.Expr, out output); } if (result == null) return; var inputApproximateMatched = result as bool?; if (inputApproximateMatched != null) { if (inputApproximateMatched.Value) { msg = AGTutorMessage.VerifyCorrect; } return; } var trace = result as List<Tuple<object, object>>; if (trace != null) { bool matchResult = UserGraph.Match(trace); //match and update if (matchResult) { //insert nodes UserGraph.Insert(trace); CurrentStateNode = UserGraph.SearchInnerLoopNode(source); //update _currentStateNode; //Debug.Assert(_currentStateNode != null); msg = AGTutorMessage.VerifyCorrect; } } }
public void Verify(IKnowledge source, IKnowledge obj, out string msg, out object output) { output = null; object outobj; SurfaceVerify(source, obj, out msg, out outobj); //Equation Eval Trace if (msg != null && msg.Equals(AGTutorMessage.VerifyCorrect)) return; DeepVerify(source, obj, out msg, out outobj); //Relation Eval }
public BehaviorGraphNode UpdateSolvingCache(IKnowledge userKnowledge, bool isAdd = true) { var node = SearchInnerLoopNode(userKnowledge); if (node == null) return null; Debug.Assert(node.State.Level == BehaviorLevel.Inner); /* if (node.OutEdges.Count != 0) return node; Debug.Assert(node.OutEdges.Count == 0);*/ bool contained = SolvingCache.ContainsKey(node); //Debug.Assert(contained); if (!contained) return node; if (isAdd) { SolvingCache[node] = true; } else { SolvingCache[node] = false; } return node; }
private bool MatchIKnowledgeHeuristic(IKnowledge iKnowledge) { var shapeExpr = iKnowledge as AGShapeExpr; var propExpr = iKnowledge as AGPropertyExpr; var equationExpr = iKnowledge as AGEquationExpr; var queryExpr = iKnowledge as AGQueryExpr; if (shapeExpr != null) { bool result = shapeExpr.Equals(UserKnowledge); if (result) return true; result = UserKnowledge.ToString().Equals(shapeExpr.ShapeSymbol.ToString()); if (result) return true; return UserKnowledge.Equals(shapeExpr.ShapeSymbol); } if (propExpr != null) { return propExpr.Goal.ApproximateMatch(UserKnowledge); } if (equationExpr != null) { bool result = UserKnowledge.Equals(equationExpr.Equation); if (result) return true; result = equationExpr.Equation.ToString().Equals(UserKnowledge.ToString()); return result; } if (queryExpr != null) { var labelConstraint = queryExpr.QueryTag.Constraint1 as string; if (labelConstraint == null) return false; bool result = UserKnowledge.Equals(queryExpr.QueryTag); if (result) return true; var shapeSymbol = UserKnowledge as ShapeSymbol; if (shapeSymbol == null) return false; return shapeSymbol.ApproximateMatch(labelConstraint); } #region Partial Matching if (iKnowledge.Tag == null) return false; var term = UserKnowledge as Term; if (term != null) { return term.Equals(iKnowledge.Tag); } var equation = UserKnowledge as Equation; if (equation != null) { return equation.Lhs.Equals(iKnowledge.Tag); } #endregion return false; }
public override void RetrieveRenderKnowledge() { if (_equation == null) return; if (_equation.CachedEntities.Count == 0) return; var lst = new ObservableCollection<IKnowledge>(); foreach (var cacheObj in _equation.CachedEntities) { var cacheShapeSymbol = cacheObj as ShapeSymbol; var cacheGoal = cacheObj as EqGoal; var cacheEq = cacheObj as Equation; if (cacheShapeSymbol != null) { throw new Exception("TODO"); /* starPadSDK.MathExpr.Expr expr = ExprG.Generate(cacheShapeSymbol); var agShape = new AGShapeExpr(expr, cacheShapeSymbol); lst.Add(agShape);*/ } else if (cacheGoal != null) { throw new Exception("TODO"); /* starPadSDK.MathExpr.Expr expr = ExprG.Generate(cacheGoal); var agGoal = new AGPropertyExpr(expr, cacheGoal); lst.Add(agGoal);*/ } else if (cacheEq != null) { starPadSDK.MathExpr.Expr expr = ExprG.Generate(cacheEq); var agEq = new AGEquationExpr(expr, cacheEq); lst.Add(agEq); } else { var boolValue = cacheObj as bool?; if (boolValue != null) { starPadSDK.MathExpr.Expr expr = ExprG.Generate(boolValue); var agEq = new IKnowledge(expr); lst.Add(agEq); } } } RenderKnowledge = lst; }
private bool EvalExprPatterns(Expr expr, object obj, ShapeType?st, out object output, bool userInput = false) { output = null; //non-deterministic Multi-Candidate selection var dict = obj as Dictionary <PatternEnum, object>; //ambiguity of input pattern if (dict != null) { EvalExprPatterns(expr, dict, out output); obj = output; } //deterministic var ss = obj as ShapeSymbol; if (ss != null) { return(EvalExprPatterns(expr, ss, out output, userInput)); } //deterministic var goal = obj as EqGoal; if (goal != null) { return(EvalExprPatterns(expr, goal, out output, userInput)); } var query = obj as Query; if (query != null) { return(EvalExprPatterns(expr, query, out output, userInput)); } var equation = obj as Equation; if (equation != null) { return(EvalExprPatterns(expr, equation, out output, userInput)); } //non-deterministic Constraint-Solving var str = obj as string; var gQuery = new Query(str, st); if (str != null) { return(EvalExprPatterns(expr, gQuery, out output, userInput)); } if (userInput) { var iknowledge = new IKnowledge(expr); iknowledge.Tag = obj; output = iknowledge; } return(false); }