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); }
public void Insert(IEnumerable <Tuple <object, object> > kt) { //start node var startState = new BehaviorGraphNode(new OuterLoopBehaviorState()); _nodes.Add(startState); Debug.Assert(kt != null); BehaviorGraphNode source = startState; foreach (var tuple in kt) { var strategy = tuple.Item1 as string; var tsExpr = tuple.Item2 as List <TraceStepExpr>; Debug.Assert(strategy != null); Debug.Assert(tsExpr != null); var traceSteps = tsExpr.Select(tse => tse.TraceStep).ToList(); var outerEdgeProp = new OuterLoopEdgeProperty(strategy); var nodeState = new OuterLoopBehaviorState(traceSteps); var target = new BehaviorGraphNode(nodeState); _nodes.Add(target); var newEdge = new BehaviorGraphEdge(source, target, outerEdgeProp); source.OutEdges.Add(newEdge); target.InEdges.Add(newEdge); source = target; } }
public BehaviorGraphNode SearchOuterLoopNode(BehaviorGraphNode inputInnerNode) { var innerState = inputInnerNode.State as InnerLoopBehaviorState; Debug.Assert(innerState != null); Debug.Assert(inputInnerNode.SubGraph == null); foreach (BehaviorGraphNode outerNode in Nodes) { if (outerNode.SubGraph == null) { continue; } var internalNodes = outerNode.SubGraph.Nodes; foreach (BehaviorGraphNode innerNode in internalNodes) { bool result = innerNode.Equals(inputInnerNode); if (result) { return(outerNode); } } } return(null); }
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 InitSubGraph() { var outerState = _state as OuterLoopBehaviorState; if (outerState == null) return; var steps = outerState.Traces; if (steps == null || steps.Count == 0) return; _subGraph = new BehaviorGraph(); var ts1 = steps[0]; var startState = new BehaviorGraphNode(new InnerLoopBehaviorState(ts1.Source)); _subGraph.Nodes.Add(startState); BehaviorGraphNode source = startState; for(int i = 0; i < outerState.Traces.Count; i++) { var ts = outerState.Traces[i]; var nodeState = new InnerLoopBehaviorState(ts.Target); var target = new BehaviorGraphNode(nodeState); _subGraph.Nodes.Add(target); var innerEdgeProp = new InnerLoopEdgeProperty((string)ts.Rule, (string)ts.AppliedRule); var newEdge = new BehaviorGraphEdge(source, target, innerEdgeProp); source.OutEdges.Add(newEdge); target.InEdges.Add(newEdge); source = target; } }
//TODO public int CurrentSubGraphPathFinding(BehaviorGraphNode outerLoopNode) { var obj = SearchInnerLoopNode(outerLoopNode); var innerNode = obj as BehaviorGraphNode; var innerNodeLst = obj as List<BehaviorGraphNode>; BehaviorGraphNode sourceNode = null; if (innerNode != null) sourceNode = innerNode; if (innerNodeLst != null) sourceNode = innerNodeLst[0]; int parentIndex = SearchOuterLoopNodeIndex(sourceNode); int count = 0; while (sourceNode != null && sourceNode.OutEdges.Count != 0) { var nextObj = SearchNextInnerLoopNode(sourceNode); var nextNode = obj as BehaviorGraphNode; var nextNodeLst = obj as List<BehaviorGraphNode>; if (nextNodeLst != null) nextNode = nextNodeLst[0]; int outerLoopIndex = SearchOuterLoopNodeIndex(nextNode); if (outerLoopIndex != parentIndex) break; count++; } return count; }
public void Insert(IEnumerable<Tuple<object, object>> kt) { //start node var startState = new BehaviorGraphNode(new OuterLoopBehaviorState()); _nodes.Add(startState); Debug.Assert(kt != null); BehaviorGraphNode source = startState; foreach (var tuple in kt) { var strategy = tuple.Item1 as string; var tsExpr = tuple.Item2 as List<TraceStepExpr>; Debug.Assert(strategy != null); Debug.Assert(tsExpr != null); var traceSteps = tsExpr.Select(tse => tse.TraceStep).ToList(); var outerEdgeProp = new OuterLoopEdgeProperty(strategy); var nodeState = new OuterLoopBehaviorState(traceSteps); var target = new BehaviorGraphNode(nodeState); _nodes.Add(target); var newEdge = new BehaviorGraphEdge(source, target, outerEdgeProp); source.OutEdges.Add(newEdge); target.InEdges.Add(newEdge); source = target; } }
//multiple strategy public void Update(IEnumerable <Tuple <object, object> > traces) { Debug.Assert(Nodes[0] != null); BehaviorGraphNode source = Nodes[0]; foreach (var tuple in traces) { var strategy = tuple.Item1 as string; var tsExpr = tuple.Item2 as List <TraceStepExpr>; Debug.Assert(strategy != null); Debug.Assert(tsExpr != null); var traceSteps = tsExpr.Select(tse => tse.TraceStep).ToList(); var outerNode = SearchOuterLoopNode(strategy); if (outerNode == null) { var outerEdgeProp = new OuterLoopEdgeProperty(strategy); var nodeState = new OuterLoopBehaviorState(traceSteps); outerNode = new BehaviorGraphNode(nodeState); _nodes.Add(outerNode); var newEdge = new BehaviorGraphEdge(source, outerNode, outerEdgeProp); source.OutEdges.Add(newEdge); outerNode.InEdges.Add(newEdge); source = outerNode; } else { Update(tsExpr, outerNode); } } }
public int PathFinding(BehaviorGraphNode currentNode) { Debug.Assert(currentNode != null); int count = 0; while (currentNode.OutEdges.Count != 0) { currentNode = currentNode.OutEdges[0].Target; count++; } var parentNode = SearchOuterLoopNode(currentNode); if (parentNode == null) { return(0); } while (parentNode.OutEdges.Count != 0) { parentNode = parentNode.OutEdges[0].Target; count += parentNode.SubGraph.Nodes.Count - 1; //count += CurrentSubGraphPathFinding(parentNode); } return(count); }
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(IEnumerable <TraceStepExpr> steps, BehaviorGraphNode outerLoopNode) { Debug.Assert(outerLoopNode != null); foreach (var step in steps) { Update(step, outerLoopNode); } }
public static bool DeriveToWrongPath(this BehaviorGraphNode innerLoopNode) { foreach (var edge in innerLoopNode.OutEdges) { var edgeProp = edge.Property as InnerLoopEdgeProperty; Debug.Assert(edgeProp != null); if (!edgeProp.CorrectPath) { continue; } return(false); } return(true); }
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 List <string> SearchAllOuterEdgeInfos(int index) { var lst = new List <string>(); BehaviorGraphNode currentNode = Nodes[0]; var edgeProp = currentNode.OutEdges[index].Property as OuterLoopEdgeProperty; Debug.Assert(edgeProp != null); lst.Add(edgeProp.Strategy); currentNode = currentNode.OutEdges[index].Target; while (currentNode.OutEdges.Count != 0) { edgeProp = currentNode.OutEdges[0].Property as OuterLoopEdgeProperty; Debug.Assert(edgeProp != null); lst.Add(edgeProp.Strategy); currentNode = currentNode.OutEdges[0].Target; } return(lst); }
public int SearchOuterLoopNodeIndex(BehaviorGraphNode gn) { Debug.Assert(gn.SubGraph == null); var parentNode = SearchOuterLoopNode(gn); BehaviorGraphNode currentNode = Nodes[0]; int index = FindGoalIndex(); var edgeProp = currentNode.OutEdges[index].Property as OuterLoopEdgeProperty; Debug.Assert(edgeProp != null); currentNode = currentNode.OutEdges[index].Target; int counter = 1; if (currentNode.Equals(parentNode)) { return(counter); } while (currentNode.OutEdges.Count != 0) { counter++; edgeProp = currentNode.OutEdges[0].Property as OuterLoopEdgeProperty; Debug.Assert(edgeProp != null); currentNode = currentNode.OutEdges[0].Target; if (currentNode.Equals(parentNode)) { return(counter); } } return(counter); /* for (var i = 0; i < Nodes.Count; i++) * { * if (Nodes[i].Equals(parentNode)) * { * return i; * } * } * return -1;*/ }
//TODO public int CurrentSubGraphPathFinding(BehaviorGraphNode outerLoopNode) { var obj = SearchInnerLoopNode(outerLoopNode); var innerNode = obj as BehaviorGraphNode; var innerNodeLst = obj as List <BehaviorGraphNode>; BehaviorGraphNode sourceNode = null; if (innerNode != null) { sourceNode = innerNode; } if (innerNodeLst != null) { sourceNode = innerNodeLst[0]; } int parentIndex = SearchOuterLoopNodeIndex(sourceNode); int count = 0; while (sourceNode != null && sourceNode.OutEdges.Count != 0) { var nextObj = SearchNextInnerLoopNode(sourceNode); var nextNode = obj as BehaviorGraphNode; var nextNodeLst = obj as List <BehaviorGraphNode>; if (nextNodeLst != null) { nextNode = nextNodeLst[0]; } int outerLoopIndex = SearchOuterLoopNodeIndex(nextNode); if (outerLoopIndex != parentIndex) { break; } count++; } return(count); }
private void InitSubGraph() { var outerState = _state as OuterLoopBehaviorState; if (outerState == null) { return; } var steps = outerState.Traces; if (steps == null || steps.Count == 0) { return; } _subGraph = new BehaviorGraph(); var ts1 = steps[0]; var startState = new BehaviorGraphNode(new InnerLoopBehaviorState(ts1.Source)); _subGraph.Nodes.Add(startState); BehaviorGraphNode source = startState; for (int i = 0; i < outerState.Traces.Count; i++) { var ts = outerState.Traces[i]; var nodeState = new InnerLoopBehaviorState(ts.Target); var target = new BehaviorGraphNode(nodeState); _subGraph.Nodes.Add(target); var innerEdgeProp = new InnerLoopEdgeProperty((string)ts.Rule, (string)ts.AppliedRule); var newEdge = new BehaviorGraphEdge(source, target, innerEdgeProp); source.OutEdges.Add(newEdge); target.InEdges.Add(newEdge); source = target; } }
/// <summary> /// /// </summary> /// <param name="outerNode"></param> /// <returns>BehaviorGraphNode or List<BehaviorGraphNode></returns> public object SearchInnerLoopNode(BehaviorGraphNode outerNode, bool wrongPathFilter = true) { Debug.Assert(outerNode != null); if (outerNode.SubGraph == null || outerNode.SubGraph.Nodes.Count == 0) { return(null); } if (outerNode.SubGraph.Nodes.Count == 1) { return(Nodes[1].SubGraph.Nodes[0]); } var lst = new List <BehaviorGraphNode>(); foreach (BehaviorGraphNode node in outerNode.SubGraph.Nodes) { if (node.InEdges.Count == 0) { if (wrongPathFilter && node.DeriveToWrongPath()) { continue; } lst.Add(node); } } if (lst.Count == 1) { return(lst[0]); } if (lst.Count == 0) { if (outerNode.SubGraph.Nodes.Count != 0) { return(outerNode.SubGraph.Nodes[0]); } } return(lst); }
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 int PathFinding(BehaviorGraphNode currentNode) { Debug.Assert(currentNode != null); int count = 0; while (currentNode.OutEdges.Count != 0) { currentNode = currentNode.OutEdges[0].Target; count++; } var parentNode = SearchOuterLoopNode(currentNode); if (parentNode == null) return 0; while (parentNode.OutEdges.Count != 0) { parentNode = parentNode.OutEdges[0].Target; count += parentNode.SubGraph.Nodes.Count-1; //count += CurrentSubGraphPathFinding(parentNode); } return count; }
public int SearchOuterLoopNodeIndex(BehaviorGraphNode gn) { Debug.Assert(gn.SubGraph == null); var parentNode = SearchOuterLoopNode(gn); BehaviorGraphNode currentNode = Nodes[0]; int index = FindGoalIndex(); var edgeProp = currentNode.OutEdges[index].Property as OuterLoopEdgeProperty; Debug.Assert(edgeProp != null); currentNode = currentNode.OutEdges[index].Target; int counter = 1; if (currentNode.Equals(parentNode)) return counter; while (currentNode.OutEdges.Count != 0) { counter++; edgeProp = currentNode.OutEdges[0].Property as OuterLoopEdgeProperty; Debug.Assert(edgeProp != null); currentNode = currentNode.OutEdges[0].Target; if (currentNode.Equals(parentNode)) return counter; } return counter; /* for (var i = 0; i < Nodes.Count; i++) { if (Nodes[i].Equals(parentNode)) { return i; } } return -1;*/ }
private QueryFeedbackState QueryBehaviorGraph(object obj, out string msg, out object output) { msg = null; output = null; if (UserGraph == null) return QueryFeedbackState.QueryFailed; if ( UserGraph.Nodes.Count == 0 || UserGraph.Nodes.Count == 1) { msg = "TODO"; return QueryFeedbackState.QueryFailed; } /* if (!_queryProcessed) { _queryProcessed = true; output = UserGraph.SearchAllOuterEdgeInfos(); msg = AGTutorMessage.InputQuery; return QueryFeedbackState.TutorQueryStarted; }*/ if (obj != null) { return VerifyBehaviorGraph(obj, out msg, out output); } Debug.Assert(obj == null); Debug.Assert(_currentStateNode != null); object edgeInfo = null; BehaviorGraphNode nextNode = null; var nextTuple1 = UserGraph.SearchNextInnerLoopNode(_currentStateNode); if (nextTuple1 == null) // query-end { //partial checking BehaviorGraph.SolvingStatus ss = UserGraph.CheckSolvingStatus(); if (ss == BehaviorGraph.SolvingStatus.Complete) { msg = AGDemonstrationMessage.QueryEnd; return QueryFeedbackState.TutorQueryEnded; } else if (ss == BehaviorGraph.SolvingStatus.Partial) { int nextGoalIndex = UserGraph.FindGoalIndex(); _currentStateNode = UserGraph.RetrieveInitInnerNode(nextGoalIndex); nextTuple1 = UserGraph.SearchNextInnerLoopNode(_currentStateNode); if (nextTuple1 == null) { msg = AGDemonstrationMessage.QueryEnd; return QueryFeedbackState.TutorQueryEnded; } CurrentStepHintRequired = true; } else { msg = AGDemonstrationMessage.QueryEnd; return QueryFeedbackState.TutorQueryEnded; } } #region Path Selection var tuple11 = nextTuple1 as Tuple<object, object>; var tuple11Lst = nextTuple1 as List<Tuple<object, object>>; if (tuple11 != null) { edgeInfo = tuple11.Item1; nextNode = tuple11.Item2 as BehaviorGraphNode; //_currentStateNode = nextNode; } if (tuple11Lst != null) { var tupleTemp = tuple11Lst[0]; edgeInfo = tupleTemp.Item1; nextNode = tupleTemp.Item2 as BehaviorGraphNode; //_currentStateNode = nextNode; } #endregion if (_currentStepHintRequired) { if (edgeInfo != null) { var innerEdgeProp = edgeInfo as InnerLoopEdgeProperty; Debug.Assert(innerEdgeProp != null); msg = AGTutorMessage.QueryIntermediate; _currentStepHintRequired = false; //int parentIndex = UserGraph.SearchOuterLoopNodeIndex(_currentStateNode); int parentIndex = UserGraph.SearchOuterLoopNodeIndex(nextNode); var lst1 = UserGraph.SearchAllOuterEdgeInfos(); var tuple = new Tuple<object, object, object>(innerEdgeProp.MetaRule, lst1, parentIndex); output = tuple; } return QueryFeedbackState.TutorQueryProcessedHint; } Debug.Assert(nextNode != null); _currentStepHintRequired = true; var nodeState = nextNode.State as InnerLoopBehaviorState; Debug.Assert(nodeState != null); var expr = IKnowledgeGenerator.Generate(nodeState.UserKnowledge); var innerEdgeProp1 = edgeInfo as InnerLoopEdgeProperty; Debug.Assert(innerEdgeProp1 != null); var appliedRule = innerEdgeProp1.AppliedRule; //int parentIndex1 = UserGraph.SearchOuterLoopNodeIndex(_currentStateNode); int parentIndex1 = UserGraph.SearchOuterLoopNodeIndex(nextNode); var lst2 = UserGraph.SearchAllOuterEdgeInfos(); output = new Tuple<object, object, object, object>(appliedRule, expr, lst2, parentIndex1); CurrentStateNode = nextNode; return QueryFeedbackState.TutorQueryProcessedAnswer; }
//multiple strategy public void Update(IEnumerable<Tuple<object, object>> traces) { Debug.Assert(Nodes[0] != null); BehaviorGraphNode source = Nodes[0]; foreach (var tuple in traces) { var strategy = tuple.Item1 as string; var tsExpr = tuple.Item2 as List<TraceStepExpr>; Debug.Assert(strategy != null); Debug.Assert(tsExpr != null); var traceSteps = tsExpr.Select(tse => tse.TraceStep).ToList(); var outerNode = SearchOuterLoopNode(strategy); if (outerNode == null) { var outerEdgeProp = new OuterLoopEdgeProperty(strategy); var nodeState = new OuterLoopBehaviorState(traceSteps); outerNode = new BehaviorGraphNode(nodeState); _nodes.Add(outerNode); var newEdge = new BehaviorGraphEdge(source, outerNode, outerEdgeProp); source.OutEdges.Add(newEdge); outerNode.InEdges.Add(newEdge); source = outerNode; } else { Update(tsExpr, outerNode); } } }
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); } }
public BehaviorGraphNode SearchOuterLoopNode(BehaviorGraphNode inputInnerNode) { var innerState = inputInnerNode.State as InnerLoopBehaviorState; Debug.Assert(innerState != null); Debug.Assert(inputInnerNode.SubGraph == null); foreach (BehaviorGraphNode outerNode in Nodes) { if (outerNode.SubGraph == null) continue; var internalNodes = outerNode.SubGraph.Nodes; foreach (BehaviorGraphNode innerNode in internalNodes) { bool result = innerNode.Equals(inputInnerNode); if (result) return outerNode; } } return null; }
/// <summary> /// It can be in the same outer loop(sub-graph), or come back to /// the previous sub-graph, or even null /// if it is the first inner-loop node /// </summary> /// <param name="gn"></param> /// <param name="wrongPathFilter"></param> /// <returns>BehaviorGraphNode or List<BehaviorGraphNode></returns> public object SearchPrevInnerLoopNode(BehaviorGraphNode gn, bool wrongPathFilter = true) { var innerState = gn.State as InnerLoopBehaviorState; Debug.Assert(innerState != null); Debug.Assert(gn.SubGraph == null); if (gn.InEdges.Count != 0) { if (gn.InEdges.Count == 1) { return(gn.InEdges[0].Source); } Debug.Assert(gn.InEdges.Count > 1); if (wrongPathFilter) { var lst = new List <BehaviorGraphNode>(); foreach (var edge in gn.InEdges) { var edgeInfo = edge.Property as InnerLoopEdgeProperty; Debug.Assert(edgeInfo != null); if (edgeInfo.CorrectPath) { lst.Add(edge.Source); } } if (lst.Count == 1) { return(lst[0]); } return(lst); } else { var lst = new List <BehaviorGraphNode>(); foreach (var edge in gn.InEdges) { var edgeInfo = edge.Property as InnerLoopEdgeProperty; Debug.Assert(edgeInfo != null); lst.Add(edge.Source); } return(lst); } } Debug.Assert(gn.InEdges.Count == 0); BehaviorGraphNode outerLoopNode = SearchOuterLoopNode(gn); if (outerLoopNode == null) { return(null); } if (outerLoopNode.InEdges.Count == 0) { return(null); } if (outerLoopNode.InEdges.Count == 1) { var edge = outerLoopNode.InEdges[0]; var prevOuterLoopNode = edge.Source; return(SearchInnerLoopNode(prevOuterLoopNode)); } if (outerLoopNode.InEdges.Count > 1) { throw new Exception("TODO"); } return(null); }
/// <summary> /// /// </summary> /// <param name="outerNode"></param> /// <returns>BehaviorGraphNode or List<BehaviorGraphNode></returns> public object SearchInnerLoopNode(BehaviorGraphNode outerNode, bool wrongPathFilter = true) { Debug.Assert(outerNode != null); if (outerNode.SubGraph == null || outerNode.SubGraph.Nodes.Count == 0) return null; if (outerNode.SubGraph.Nodes.Count == 1) { return Nodes[1].SubGraph.Nodes[0]; } var lst = new List<BehaviorGraphNode>(); foreach (BehaviorGraphNode node in outerNode.SubGraph.Nodes) { if (node.InEdges.Count == 0) { if (wrongPathFilter && node.DeriveToWrongPath()) continue; lst.Add(node); } } if (lst.Count == 1) return lst[0]; if (lst.Count == 0) { if (outerNode.SubGraph.Nodes.Count != 0) { return outerNode.SubGraph.Nodes[0]; } } return lst; }
/// <summary> /// It can be in the same sub-graph, or the next sub-graph /// or null /// </summary> /// <param name="currentNode"></param> /// <param name="wrongPathFilter"></param> /// <returns>Tuple<InnerLoopEdgeProperty, BehaviorGraphNode> /// List<Tuple<InnerLoopEdgeProperty, BehaviorGraphNode>> /// </returns> public object SearchNextInnerLoopNode(BehaviorGraphNode currentNode) { if (currentNode == null) { return(null); } var innerState = currentNode.State as InnerLoopBehaviorState; Debug.Assert(innerState != null); //must be leaf node Debug.Assert(currentNode.SubGraph == null); if (currentNode.OutEdges.Count != 0) { var lst = new List <Tuple <object, object> >(); foreach (var tempEdge in currentNode.OutEdges) { var tempEdgeInfo = tempEdge.Property as InnerLoopEdgeProperty; Debug.Assert(tempEdgeInfo != null); if (!tempEdgeInfo.CorrectPath) { continue; } lst.Add(new Tuple <object, object>(tempEdgeInfo, tempEdge.Target)); } if (lst.Count == 0) { throw new Exception("Cannot reach here"); } if (lst.Count == 1) { return(lst[0]); } return(lst); } Debug.Assert(currentNode.OutEdges.Count == 0); //find parent node of current node var parentNode = SearchOuterLoopNode(currentNode); Debug.Assert(parentNode != null); if (parentNode.OutEdges.Count == 0) { return(null); } if (parentNode.OutEdges.Count == 1) { var edge = parentNode.OutEdges[0]; BehaviorGraphEdgeProperty edgeInfo = edge.Property; var nextParentNode = edge.Target; object obj = SearchInnerLoopNode(nextParentNode); var node = obj as BehaviorGraphNode; var lstNode = obj as List <BehaviorGraphNode>; if (node != null) { return(SearchNextInnerLoopNode(node)); //return new Tuple<object, object>(edgeInfo, node); } if (lstNode != null) { var lst = new List <Tuple <object, object> >(); foreach (BehaviorGraphNode bgn in lstNode) { return(SearchNextInnerLoopNode(bgn)); //var tuple = new Tuple<object, object>(edgeInfo, bgn) } } } if (parentNode.OutEdges.Count > 1) { throw new Exception("TODO"); } return(null); }
private void Update(IEnumerable<TraceStepExpr> steps, BehaviorGraphNode outerLoopNode) { Debug.Assert(outerLoopNode != null); foreach (var step in steps) { Update(step, outerLoopNode); } }
/// <summary> /// It can be in the same outer loop(sub-graph), or come back to /// the previous sub-graph, or even null /// if it is the first inner-loop node /// </summary> /// <param name="gn"></param> /// <param name="wrongPathFilter"></param> /// <returns>BehaviorGraphNode or List<BehaviorGraphNode></returns> public object SearchPrevInnerLoopNode(BehaviorGraphNode gn, bool wrongPathFilter = true) { var innerState = gn.State as InnerLoopBehaviorState; Debug.Assert(innerState != null); Debug.Assert(gn.SubGraph == null); if (gn.InEdges.Count != 0) { if (gn.InEdges.Count == 1) return gn.InEdges[0].Source; Debug.Assert(gn.InEdges.Count > 1); if (wrongPathFilter) { var lst = new List<BehaviorGraphNode>(); foreach (var edge in gn.InEdges) { var edgeInfo = edge.Property as InnerLoopEdgeProperty; Debug.Assert(edgeInfo != null); if(edgeInfo.CorrectPath) lst.Add(edge.Source); } if (lst.Count == 1) return lst[0]; return lst; } else { var lst = new List<BehaviorGraphNode>(); foreach (var edge in gn.InEdges) { var edgeInfo = edge.Property as InnerLoopEdgeProperty; Debug.Assert(edgeInfo != null); lst.Add(edge.Source); } return lst; } } Debug.Assert(gn.InEdges.Count == 0); BehaviorGraphNode outerLoopNode = SearchOuterLoopNode(gn); if (outerLoopNode == null) return null; if (outerLoopNode.InEdges.Count == 0) return null; if (outerLoopNode.InEdges.Count == 1) { var edge = outerLoopNode.InEdges[0]; var prevOuterLoopNode = edge.Source; return SearchInnerLoopNode(prevOuterLoopNode); } if (outerLoopNode.InEdges.Count > 1) { throw new Exception("TODO"); } return null; }
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); } } }
/// <summary> /// It can be in the same sub-graph, or the next sub-graph /// or null /// </summary> /// <param name="currentNode"></param> /// <param name="wrongPathFilter"></param> /// <returns>Tuple<InnerLoopEdgeProperty, BehaviorGraphNode> /// List<Tuple<InnerLoopEdgeProperty, BehaviorGraphNode>> /// </returns> public object SearchNextInnerLoopNode(BehaviorGraphNode currentNode) { if (currentNode == null) return null; var innerState = currentNode.State as InnerLoopBehaviorState; Debug.Assert(innerState != null); //must be leaf node Debug.Assert(currentNode.SubGraph == null); if (currentNode.OutEdges.Count != 0) { var lst = new List<Tuple<object, object>>(); foreach (var tempEdge in currentNode.OutEdges) { var tempEdgeInfo = tempEdge.Property as InnerLoopEdgeProperty; Debug.Assert(tempEdgeInfo != null); if (!tempEdgeInfo.CorrectPath) continue; lst.Add(new Tuple<object, object>(tempEdgeInfo, tempEdge.Target)); } if(lst.Count == 0) throw new Exception("Cannot reach here"); if (lst.Count == 1) return lst[0]; return lst; } Debug.Assert(currentNode.OutEdges.Count == 0); //find parent node of current node var parentNode = SearchOuterLoopNode(currentNode); Debug.Assert(parentNode != null); if (parentNode.OutEdges.Count == 0) return null; if (parentNode.OutEdges.Count == 1) { var edge = parentNode.OutEdges[0]; BehaviorGraphEdgeProperty edgeInfo = edge.Property; var nextParentNode = edge.Target; object obj = SearchInnerLoopNode(nextParentNode); var node = obj as BehaviorGraphNode; var lstNode = obj as List<BehaviorGraphNode>; if (node != null) { return SearchNextInnerLoopNode(node); //return new Tuple<object, object>(edgeInfo, node); } if (lstNode != null) { var lst = new List<Tuple<object, object>>(); foreach (BehaviorGraphNode bgn in lstNode) { return SearchNextInnerLoopNode(bgn); //var tuple = new Tuple<object, object>(edgeInfo, bgn) } } } if (parentNode.OutEdges.Count > 1) { throw new Exception("TODO"); } return null; }