Ejemplo n.º 1
0
        /// <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;
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        internal PathFactory(NodeReference targetNode, ComposedGraph graph, bool distinctEdges, int maxSearchWidth, int maxSearchDepth)
        {
            StartingNode    = targetNode;
            Graph           = graph;
            _maxSearchDepth = maxSearchDepth;
            _distinctEdges  = distinctEdges;

            _maxSearchWidth = maxSearchWidth;
            _segmentsToVisit.Enqueue(new PathSegment(null, null, StartingNode));
        }
Ejemplo n.º 4
0
        public MultiTraceLog2(IEnumerable <NodeReference> initialNodes, ComposedGraph graph, bool fullExpansion, int maxWidth, int maxDepth)
        {
            InitialNodes = initialNodes.ToArray();
            Root         = new TraceNode2(InitialNodes);

            var worklist = new Queue <TraceNode2>();

            worklist.Enqueue(Root);
            var allNodes = new List <TraceNode2>();

            var start2 = DateTime.Now;

            //       Console.WriteLine("MultiTraceLog2 START");
            while (worklist.Count > 0)
            {
                var node = worklist.Dequeue();
                allNodes.Add(node);
                if (node.TraceDepth >= maxDepth || (!fullExpansion && !node.HasContinuation))
                {
                    continue;
                }

                //targets available from the node
                var targets = node.GetTargets(graph, maxWidth).ToArray();

                // index targets according to initial nodes and edges
                var initialNodeTargetsIndex = new Dictionary <Edge, Dictionary <NodeReference, HashSet <NodeReference> > >();
                foreach (var target in targets)
                {
                    Dictionary <NodeReference, HashSet <NodeReference> > targetIndex;
                    if (!initialNodeTargetsIndex.TryGetValue(target.Item2, out targetIndex))
                    {
                        initialNodeTargetsIndex[target.Item2] = targetIndex = new Dictionary <NodeReference, HashSet <NodeReference> >();
                    }

                    HashSet <NodeReference> edgeTargets;
                    if (!targetIndex.TryGetValue(target.Item1, out edgeTargets))
                    {
                        targetIndex[target.Item1] = edgeTargets = new HashSet <NodeReference>();
                    }

                    edgeTargets.Add(target.Item3);
                }

                //construct trace nodes
                foreach (var edgePair in initialNodeTargetsIndex)
                {
                    var traceNode = new TraceNode2(node, edgePair.Key, edgePair.Value);
                    worklist.Enqueue(traceNode);
                }
            }

            //           Console.WriteLine("MultiTraceLog2 {0}s", (DateTime.Now - start2).TotalSeconds);
            TraceNodes = allNodes;
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        private void addChildren(NodeReference node, PathSegment previousSegment, ComposedGraph graph)
        {
            foreach (var edgeTuple in graph.GetNeighbours(node, _maxSearchWidth))
            {
                var edge  = edgeTuple.Item1;
                var child = edgeTuple.Item2;

                if (previousSegment != null && (previousSegment.Contains(child) || (_distinctEdges && previousSegment.Contains(edge.Name))))
                {
                    //the node has already been visited previously in the path
                    continue;
                }

                if (previousSegment.SegmentIndex < _maxSearchDepth)
                {
                    _segmentsToVisit.Enqueue(new PathSegment(previousSegment, edge, child));
                }
            }
        }
Ejemplo n.º 7
0
 public KnowledgeClassifier(ComposedGraph knowledge)
 {
     Knowledge = knowledge;
 }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
 public Group(ComposedGraph graph)
 {
     _graph = graph;
 }
Ejemplo n.º 10
0
 internal IEnumerable <Tuple <NodeReference, Edge, NodeReference> > GetTargets(ComposedGraph graph, int maxWidth)
 {
     foreach (var initialNodePair in _initialToTargetIndex)
     {
         foreach (var currentNode in initialNodePair.Value)
         {
             foreach (var target in graph.GetNeighbours(currentNode, maxWidth))
             {
                 yield return(Tuple.Create(initialNodePair.Key, target.Item1, target.Item2));
             }
         }
     }
 }