public IEnumerable <TraceNode> CreateAllTraces(int pathLengthLimit, ComposedGraph graph) { var worklist = new Queue <TraceNode>(); var allNodes = new List <TraceNode>(); worklist.Enqueue(Root); while (worklist.Count > 0) { var currentNode = worklist.Dequeue(); allNodes.Add(currentNode); if (currentNode.Path.Count() >= pathLengthLimit) { //current node cannot be extended continue; } //extend trace node according to all edges var edges = getEdges(currentNode, graph); //Console.WriteLine("A" + edges.Count()); foreach (var edge in edges) { if (edge.Inverse().Equals(currentNode.CurrentEdge)) { //we dont want to go back continue; } var nextNode = new TraceNode(currentNode, edge, graph); worklist.Enqueue(nextNode); } } return(allNodes); }
private KnowledgeRule createBestRule(IEnumerable <NodeReference> classifiedNodes, MultiTraceLog log) { var bestScore = Double.NegativeInfinity; TraceNode bestTraceNode = null; Trace bestTrace = null; IEnumerable <NodeReference> bestCoverage = null; //find best classification rule foreach (var traceNode in log.TraceNodes) { var debug = traceNode.ToString(); foreach (var trace in traceNode.Traces) { var coverage = trace.InitialNodes; var currentScore = getClassificationScore(classifiedNodes, coverage, trace); if (currentScore > bestScore) { bestCoverage = coverage; bestScore = currentScore; bestTraceNode = traceNode; bestTrace = trace; } } } if (bestCoverage == null) { return(null); } var yesNodes = bestCoverage.Intersect(classifiedNodes); var noNodes = classifiedNodes.Except(yesNodes); return(new KnowledgeRule(bestTraceNode.Path, bestTrace.CurrentNode, yesNodes, noNodes)); }
/// <summary> /// Create trace log for the given batch of nodes. /// </summary> /// <param name="nodeBatch">Nodes that will be traced.</param> public MultiTraceLog(IEnumerable <NodeReference> nodeBatch, ComposedGraph graph) { NodeBatch = nodeBatch.ToArray(); Root = new TraceNode(nodeBatch); var worklist = new Queue <TraceNode>(); var allNodes = new List <TraceNode>(); worklist.Enqueue(Root); while (worklist.Count > 0) { var currentNode = worklist.Dequeue(); allNodes.Add(currentNode); if (!currentNode.HasContinuation || currentNode.Path.Count() >= MaxPathLength) { //current node cannot be extended continue; } //extend trace node according to all edges var edges = getEdges(currentNode, graph); // Console.WriteLine("M"+edges.Count()); foreach (var edge in edges) { if (edge.Inverse().Equals(currentNode.CurrentEdge)) { //we dont want to go back continue; } var nextNode = new TraceNode(currentNode, edge, graph); worklist.Enqueue(nextNode); } } TraceNodes = allNodes; }
private IEnumerable <NodeReference> getCoverage(TraceNode node) { var bestTrace = getCoverageTrace(node); if (bestTrace == null) { return(Enumerable.Empty <NodeReference>()); } return(bestTrace.InitialNodes); }
private IEnumerable <Edge> getEdges(TraceNode node, ComposedGraph graph) { var edges = new HashSet <Edge>(); foreach (var currentNode in node.CurrentNodes) { var neighbours = graph.GetNeighbours(currentNode, Width).ToArray(); foreach (var neighbour in neighbours.ToArray()) { edges.Add(neighbour.Item1); } } return(edges); }
private static Trace getCoverageTrace(TraceNode node) { var maxSize = int.MinValue; Trace bestTrace = null; foreach (var trace in node.Traces) { var currentSize = trace.InitialNodes.Count(); if (currentSize > maxSize) { maxSize = currentSize; bestTrace = trace; } } return(bestTrace); }
internal TraceNode(TraceNode previousNode, Edge edge, ComposedGraph graph) { if (previousNode == null) { throw new ArgumentNullException("previousNode"); } if (edge == null) { throw new ArgumentNullException("edge"); } PreviousNode = previousNode; CurrentEdge = edge; VisitedNodes.UnionWith(previousNode.VisitedNodes); var startTime = DateTime.Now; var currentlyVisitedNodes = new HashSet <NodeReference>(); var traceTargetIndex = new Dictionary <NodeReference, List <Trace> >(); //fill index with previous nodes for each trace target foreach (var node in previousNode.CurrentNodes) { var previousTrace = previousNode.GetTrace(node); //check whether we still need to trace the previousTrace //if it contains all input nodes - we don't need to trace it further foreach (var target in graph.Targets(node, edge)) { if (VisitedNodes.Contains(target)) { continue; } currentlyVisitedNodes.Add(target); List <Trace> traces; if (!traceTargetIndex.TryGetValue(target, out traces)) { traceTargetIndex[target] = traces = new List <Trace>(); } traces.Add(previousTrace); } } VisitedNodes.UnionWith(currentlyVisitedNodes); var inputInitialNodes = new HashSet <NodeReference>(); //merge traces that points to same node and check saturation of nodes foreach (var pair in traceTargetIndex) { var trace = new Trace(pair.Key, pair.Value.Distinct()); _traceIndex.Add(pair.Key, trace); inputInitialNodes.UnionWith(trace.InitialNodes); } //If there is node, that is not saturated - we will trace path further (because it can add more information) var hasNonSaturatedTrace = _traceIndex.Any(p => p.Value.InitialNodes.Count() != inputInitialNodes.Count); HasContinuation = hasNonSaturatedTrace && Path.Count() < MultiTraceLog.MaxPathLength; var endTime = DateTime.Now; var duration = (endTime - startTime).TotalSeconds; // Console.WriteLine("Tracenode creation {0:0.000}s", duration); // Console.WriteLine("TraceNode {0},{1}", VisitedNodes.Count, _traceIndex.Count); }