public void Test_OneStrategy_OneTraceStep() { //strategy1 : 1+1->2 var expr1 = new Term(Expression.Add, new List<object>{1,1}); var ts = new TraceStep(expr1, 2, "null", "meta-rule todo", "rule todo"); var tsExpr = new TraceStepExpr(ts); var lst = new List<TraceStepExpr>() { tsExpr}; var tuple = new Tuple<object, object>("strategy1", lst); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.True(node0.OutEdges.Count == 1); var edgeInfo = node0.OutEdges[0].Property as OuterLoopEdgeProperty; Assert.NotNull(edgeInfo); Assert.True(edgeInfo.Strategy.Equals("strategy1")); Assert.NotNull(node1.SubGraph); Assert.True(node1.InEdges.Count == 1); Assert.True(node1.OutEdges.Count == 0); Assert.True(node1.SubGraph.Nodes.Count == 2); ///////////////////////////////////////////////////// //Search Test var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); int count = graph.PathFinding(initNode); Assert.True(count == 1); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple2 = nextObj as Tuple<object,object>; Assert.NotNull(tuple2); var nextNode = tuple2.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 0); var prevObj = graph.SearchPrevInnerLoopNode(nextNode) as BehaviorGraphNode; Assert.NotNull(prevObj); count = graph.PathFinding(prevObj); Assert.True(count == 1); }
public override void GenerateSolvingTrace() { if (IsSelected) { var traces = _shapeSymbol.Traces; if (traces.Count == 0) { return; } var lstTuples = new List <Tuple <object, object> >(); for (var i = 0; i < traces.Count; i++) { var strategy = traces[i].Item1 as string; var steps = traces[i].Item2 as List <TraceStep>; Debug.Assert(steps != null); var gSteps = new List <TraceStepExpr>(); foreach (TraceStep ts in steps) { var tse = new TraceStepExpr(ts); gSteps.Add(tse); } var gTuple = new Tuple <object, object>(strategy, gSteps); lstTuples.Add(gTuple); } AutoTrace = lstTuples; return; } if (RenderKnowledge == null) { return; } foreach (var temp in RenderKnowledge) { if (temp.IsSelected) { temp.GenerateSolvingTrace(); } } }
public override void GenerateSolvingTrace() { if (IsSelected) { var traces = _goal.Traces; if (traces.Count == 0) return; var lstTuples = new List<Tuple<object, object>>(); for (var i = 0; i < traces.Count; i++) { var strategy = traces[i].Item1 as string; var steps = traces[i].Item2 as List<TraceStep>; Debug.Assert(steps != null); var gSteps = new List<TraceStepExpr>(); foreach (TraceStep ts in steps) { var tse = new TraceStepExpr(ts); gSteps.Add(tse); } var gTuple = new Tuple<object, object>(strategy, gSteps); lstTuples.Add(gTuple); } AutoTrace = lstTuples; return; } if (RenderKnowledge == null) return; foreach (var temp in RenderKnowledge) { if (temp.IsSelected) { temp.GenerateSolvingTrace(); } } }
public bool Match(TraceStepExpr tsExpr) { var ts = tsExpr.TraceStep; Debug.Assert(ts != null); BehaviorGraphNode gn1 = SearchInnerLoopNode(ts.Source); BehaviorGraphNode gn2 = SearchInnerLoopNode(ts.Target); if (gn1 == null && gn2 == null) return false; return true; }
private void ConnectRootNode(TraceStepExpr tsExpr, BehaviorGraphNode parentNode) { Debug.Assert(parentNode != null); var newNodeProp1 = new InnerLoopBehaviorState(tsExpr.TraceStep.Source); var newNode1 = new BehaviorGraphNode(newNodeProp1); var newNodeProp2 = new InnerLoopBehaviorState(tsExpr.TraceStep.Target); var newNode2 = new BehaviorGraphNode(newNodeProp2); InnerLoopEdgeProperty newEdgeProp; if (tsExpr.TraceStep.WrongStep) { newEdgeProp = new InnerLoopEdgeProperty(); } else { newEdgeProp = new InnerLoopEdgeProperty((string)tsExpr.TraceStep.Rule, (string)tsExpr.TraceStep.AppliedRule); } var newEdge = new BehaviorGraphEdge(newNode1, newNode2, newEdgeProp); newNode1.OutEdges.Add(newEdge); newNode2.InEdges.Add(newEdge); parentNode.SubGraph.Nodes.Add(newNode1); parentNode.SubGraph.Nodes.Add(newNode2); }
private void ConnectNextNode(TraceStepExpr tsExpr, BehaviorGraphNode gn2) { var newNodeProp = new InnerLoopBehaviorState(tsExpr.TraceStep.Source); var newNode = new BehaviorGraphNode(newNodeProp); InnerLoopEdgeProperty newEdgeProp; if (tsExpr.TraceStep.WrongStep) { newEdgeProp = new InnerLoopEdgeProperty(); } else { newEdgeProp = new InnerLoopEdgeProperty((string)tsExpr.TraceStep.Rule, (string)tsExpr.TraceStep.AppliedRule); } var newEdge = new BehaviorGraphEdge(newNode, gn2, newEdgeProp); /*foreach (var edge in gn2.InEdges) { var prevNode = edge.Source; prevNode.OutEdges.Add(newEdge); }*/ gn2.InEdges.Add(newEdge); newNode.OutEdges.Add(newEdge); BehaviorGraphNode parentNode = SearchOuterLoopNode(gn2); parentNode.SubGraph.Nodes.Add(newNode); }
private void ConnectTwoNodes(TraceStepExpr tsExpr, BehaviorGraphNode gn1, BehaviorGraphNode gn2) { bool findTarget = false; foreach (var edge in gn1.OutEdges) { if (edge.Target == null) continue; if (edge.Target.Equals(gn2)) { findTarget = true; } } if (!findTarget) { InnerLoopEdgeProperty newEdgeProp1; if (tsExpr.TraceStep.WrongStep) { newEdgeProp1 = new InnerLoopEdgeProperty(); } else { newEdgeProp1 = new InnerLoopEdgeProperty((string)tsExpr.TraceStep.Rule, (string)tsExpr.TraceStep.AppliedRule); } var newEdge1 = new BehaviorGraphEdge(gn1, gn2, newEdgeProp1); gn1.OutEdges.Add(newEdge1); gn2.InEdges.Add(newEdge1); } }
private void Update(TraceStepExpr tsExpr, BehaviorGraphNode outerLoopNode = null) { var ts = tsExpr.TraceStep; Debug.Assert(ts != null); BehaviorGraphNode gn1 = SearchInnerLoopNode(ts.Source); BehaviorGraphNode gn2 = SearchInnerLoopNode(ts.Target); if (gn1 != null) { if (gn2 != null) { ConnectTwoNodes(tsExpr, gn1, gn2); } else { ConnectPrevNode(tsExpr, gn1); } } else { Debug.Assert(gn1 == null); if (gn2 != null) { ConnectNextNode(tsExpr, gn2); } else { if (outerLoopNode == null) return; ConnectRootNode(tsExpr, outerLoopNode); } } }
public void Test_OneStrategy_OneTraceStep_Author() { //1+1->2 var expr1 = new Term(Expression.Add, new List<object> { 1, 1 }); var ts = new TraceStep(expr1, 2, "null", "meta-rule todo", "rule todo"); var tsExpr = new TraceStepExpr(ts); var lst = new List<TraceStepExpr>() { tsExpr }; var tuple = new Tuple<object, object>("strategy1", lst); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 2); ///////////////////////////////////////////// //4-1->2 var expr2 = new Term(Expression.Add, new List<object> { 4, 1 }); // User Input, //wrong step var ts1 = new TraceStep(expr2, 2); var ts1Expr = new TraceStepExpr(ts1); var tsLst = new List<TraceStepExpr>() { ts1Expr }; var tuple2 = new Tuple<object, object>("strategy1", tsLst); var lst44 = new List<Tuple<object, object>>() { tuple2 }; bool matchResult = graph.Match(lst44); Assert.True(matchResult); graph.Update(lst44); Assert.True(graph.Nodes.Count == 2); Assert.True(node1.SubGraph.Nodes.Count == 3); ///////////////////////////////////////////// //test search var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); var innerState = initNode.State as InnerLoopBehaviorState; Assert.NotNull(innerState); Assert.True(innerState.UserKnowledge.ToString().Equals("1+1")); Assert.True(initNode.InEdges.Count == 0); Assert.True(initNode.OutEdges.Count == 1); int count = graph.PathFinding(initNode); Assert.True(count == 1); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple22 = nextObj as Tuple<object, object>; Assert.NotNull(tuple22); var nextNode = tuple22.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 0); }
public void Test_OneStrategy_MultiTraceStep_UserInput() { var expr1 = new Term(Expression.Add, new List<object> { 1, 1, 1 }); var eq1 = new Equation(expr1, 20); var expr2 = new Term(Expression.Add, new List<object> { 2, 1 }); var ts1 = new TraceStep(eq1, expr2, null, "meta-rule todo", "rule todo"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(expr2, 2, null, "meta-rule todo", "rule todo"); var ts2Expr = new TraceStepExpr(ts2); var lst = new List<TraceStepExpr>() { ts1Expr, ts2Expr }; var tuple = new Tuple<object, object>("strategy1", lst); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); ///////////////////////////////////////////// // User Input var userExpr1 = new Term(Expression.Add, new List<object> { 1, 1, 1 }); var userEq1 = new Equation(userExpr1, 20); var node = graph.SearchInnerLoopNode(userEq1); Assert.NotNull(node); var userExpr2 = new Term(Expression.Add, new List<object> { 1, 3 }); node = graph.SearchInnerLoopNode(userExpr2); Assert.Null(node); }
public void Test_OneStrategy_MultiTraceStep_Author() { var expr1 = new Term(Expression.Add, new List<object> { 1, 1, 1 }); var expr2 = new Term(Expression.Add, new List<object> { 2, 1 }); var ts1 = new TraceStep(expr1, expr2, null, "meta-rule todo", "rule todo"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(expr2, 2, null, "meta-rule todo", "rule todo"); var ts2Expr = new TraceStepExpr(ts2); var lst = new List<TraceStepExpr>() { ts1Expr, ts2Expr }; var tuple = new Tuple<object, object>("strategy2", lst); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); ///////////////////////////////////////////// // User Input, //2 steps trace var expr3 = new Term(Expression.Add, new List<object> { 1, 2 }); var ts3 = new TraceStep(expr1, expr3, null, "meta-rule todo", "rule todo"); var ts3Expr = new TraceStepExpr(ts3); var ts4 = new TraceStep(expr3, 2, null, "meta-rule todo", "rule todo"); var ts4Expr = new TraceStepExpr(ts4); var lst2 = new List<TraceStepExpr>() { ts3Expr, ts4Expr }; var tuple2 = new Tuple<object, object>("strategy1", lst2); var lstStrategy2 = new List<Tuple<object, object>>(); lstStrategy2.Add(tuple2); //Under the same strategy graph.Update(lstStrategy2); Assert.True(graph.Nodes.Count == 3); Assert.True(node1.SubGraph.Nodes.Count == 3); }
public void Test_MultiStrategy_1() { //strategy1: 1->2, 2->3 var ts1 = new TraceStep(1, 2, "null", "TODO1", "TODO2"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(2, 3, "null", "TODO2", "TODO3"); var ts2Expr = new TraceStepExpr(ts2); var lst1 = new List<TraceStepExpr>() { ts1Expr, ts2Expr }; var tuple1 = new Tuple<object, object>("strategy1", lst1); //strategy2: 2->3, 3->5, 5->7 var ts3 = new TraceStep(2, 3, null, "TODO", "TODO1"); var ts3Expr = new TraceStepExpr(ts3); var ts4 = new TraceStep(3, 5, null, "TODO2", "TODO5"); var ts4Expr = new TraceStepExpr(ts4); var ts5 = new TraceStep(5, 7, null,"Test1", "test23"); var ts5Expr = new TraceStepExpr(ts5); var lst2 = new List<TraceStepExpr>() {ts3Expr, ts4Expr, ts5Expr}; var tuple2 = new Tuple<object, object>("strategy2", lst2); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple1); lstStrategy.Add(tuple2); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 3); var node1 = graph.Nodes[1]; Assert.True(node1.OutEdges.Count == 1); var node2 = graph.Nodes[2]; Assert.True(node2.InEdges.Count == 1); Assert.True(node2.OutEdges.Count == 0); Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); Assert.NotNull(node2.SubGraph); Assert.True(node2.SubGraph.Nodes.Count == 4); ///////////////////////////////////////////////////// //Search Test var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); int count = graph.PathFinding(initNode); Assert.True(count == 5); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple5 = nextObj as Tuple<object, object>; Assert.NotNull(tuple5); var nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 4); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple<object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 3); var index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 1); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple<object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 2); index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 2); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple<object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 1); index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 2); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple<object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 0); index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 2); var prevNode = graph.SearchPrevInnerLoopNode(nextNode) as BehaviorGraphNode; Assert.NotNull(prevNode); count = graph.PathFinding(prevNode); Assert.True(count == 1); var strateties = graph.SearchAllOuterEdgeInfos(); Assert.True(strateties.Count == 2); }
public void Test_OneStrategy_MultiTraceStep() { var expr1 = new Term(Expression.Add, new List<object> { 1, 1 , 1}); var expr2 = new Term(Expression.Add, new List<object> {2, 1} ); var ts1 = new TraceStep(expr1, expr2, "null", "meta-rule todo", "rule todo"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(expr2, 2, "null", "meta-rule todo", "rule todo"); var ts2Expr = new TraceStepExpr(ts2); var lst = new List<TraceStepExpr>() { ts1Expr,ts2Expr}; var tuple = new Tuple<object, object>("strategy1", lst); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); Assert.True(node1.OutEdges.Count == 0); ///////////////////////////////////////////////////// //Search Test var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); int count = graph.PathFinding(initNode); Assert.True(count == 2); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple2 = nextObj as Tuple<object, object>; Assert.NotNull(tuple2); var nextNode = tuple2.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 1); var nextObj2 = graph.SearchNextInnerLoopNode(nextNode); var tuple3 = nextObj2 as Tuple<object, object>; Assert.NotNull(tuple3); var nextNode2 = tuple3.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode2); count = graph.PathFinding(nextNode2); Assert.True(count == 0); }