Пример #1
0
        /// <summary>
        /// Determine whether there is an edge between given nodes
        /// </summary>
        /// <param name="node1"></param>
        /// <param name="edge"></param>
        /// <param name="node2"></param>
        /// <returns></returns>
        public bool HasEdge(NodeReference node1, string edge, NodeReference node2)
        {
            foreach (var layer in _layers)
            {
                var edges = layer.Edges(node1, node2);
                if (edges.Contains(edge))
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #2
0
        /// <summary>
        /// Determine whether node is contained in currrent or previous segments.
        /// </summary>
        /// <param name="node">Tested node.</param>
        /// <returns><c>true</c> whether node is contained, <c>false</c> otherwise.</returns>
        internal bool Contains(NodeReference node)
        {
            if (Node.Equals(node))
            {
                return(true);
            }

            if (PreviousSegment == null)
            {
                return(false);
            }

            return(PreviousSegment.Contains(node));
        }
Пример #3
0
        internal Trace(NodeReference currentNode, IEnumerable <Trace> previousTraces)
        {
            CurrentNode    = currentNode;
            PreviousTraces = previousTraces;

            var initialNodes = new HashSet <NodeReference>();

            foreach (var trace in previousTraces)
            {
                initialNodes.UnionWith(trace.InitialNodes);
            }

            InitialNodes = initialNodes.ToArray();
        }
Пример #4
0
        private IEnumerable <NodeReference> getTargets(NodeReference node, string edge, Dictionary <NodeReference, Dictionary <string, List <NodeReference> > > edges)
        {
            Dictionary <string, List <NodeReference> > targetCandidates;
            List <NodeReference> targets;

            if (
                edges.TryGetValue(node, out targetCandidates) &&
                targetCandidates.TryGetValue(edge, out targets)
                )
            {
                return(targets);
            }

            return(Enumerable.Empty <NodeReference>());
        }
Пример #5
0
        /// <summary>
        /// Edges between given nodes (both side edges).
        /// </summary>
        /// <param name="node1"></param>
        /// <param name="node2"></param>
        /// <returns></returns>
        public IEnumerable <Edge> BetweenEdges(NodeReference node1, NodeReference node2)
        {
            foreach (var layer in _layers)
            {
                foreach (var edge in layer.Edges(node1, node2))
                {
                    yield return(Edge.Outcoming(edge));
                }

                foreach (var edge in layer.Edges(node2, node1))
                {
                    yield return(Edge.Incoming(edge));
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Get children of given node (incoming and outcoming).
        /// </summary>
        /// <param name="node"></param>
        /// <param name="maxWidth"></param>
        /// <returns></returns>
        public IEnumerable <Tuple <Edge, NodeReference> > GetNeighbours(NodeReference node, int maxWidth)
        {
            foreach (var layer in _layers)
            {
                foreach (var pair in layer.Incoming(node).Take(maxWidth))
                {
                    yield return(Tuple.Create(Edge.Incoming(pair.Key), pair.Value));
                }

                foreach (var pair in layer.Outcoming(node).Take(maxWidth))
                {
                    yield return(Tuple.Create(Edge.Outcoming(pair.Key), pair.Value));
                }
            }
        }
Пример #7
0
        private IEnumerable <KeyValuePair <string, NodeReference> > getEdges(NodeReference node, Dictionary <NodeReference, Dictionary <string, List <NodeReference> > > edges)
        {
            Dictionary <string, List <NodeReference> > targets;

            if (!edges.TryGetValue(node, out targets))
            {
                yield break;
            }

            //enumerate all incoming edges
            foreach (var targetPair in targets)
            {
                foreach (var target in targetPair.Value)
                {
                    yield return(new KeyValuePair <string, NodeReference>(targetPair.Key, target));
                }
            }
        }
Пример #8
0
        /// <inheritdoc/>
        protected internal override IEnumerable <string> Edges(NodeReference from, NodeReference to)
        {
            Dictionary <string, List <NodeReference> > targets;

            if (!_outEdges.TryGetValue(from, out targets))
            {
                yield break;
            }

            //enumerate all edge connections
            foreach (var targetPair in targets)
            {
                if (targetPair.Value.Contains(to))
                {
                    yield return(targetPair.Key);
                }
            }
        }
Пример #9
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));
                }
            }
        }
Пример #10
0
        private void addEdge(NodeReference from, string edge, NodeReference to, Dictionary <NodeReference, Dictionary <string, List <NodeReference> > > edges)
        {
            Dictionary <string, List <NodeReference> > edgeNodes;

            if (
                !edges.TryGetValue(from, out edgeNodes)
                )
            {
                edges[from] = edgeNodes = new Dictionary <string, List <NodeReference> >();
            }

            List <NodeReference> nodes;

            if (
                !edgeNodes.TryGetValue(edge, out nodes)
                )
            {
                edgeNodes[edge] = nodes = new List <NodeReference>();
            }

            nodes.Add(to);
        }
Пример #11
0
 abstract internal protected IEnumerable <NodeReference> Outcoming(NodeReference fromNode, string edge);
Пример #12
0
 /// <inheritdoc/>
 protected internal override IEnumerable <NodeReference> Outcoming(NodeReference fromNode, string edge)
 {
     return(getTargets(fromNode, edge, _outEdges));
 }
Пример #13
0
        /// <summary>
        /// Get paths between given nodes.
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="maxLength"></param>
        /// <param name="maxWidth"></param>
        /// <returns></returns>
        public IEnumerable <KnowledgePath> GetPaths(NodeReference from, NodeReference to, int maxLength, int maxWidth)
        {
            if (from == null || to == null)
            {
                yield break;
            }

            var currentQueue = new Queue <PathSegment>();
            var visitedNodes = new HashSet <NodeReference>();

            var startSegment = new PathSegment(null, null, from);

            if (from.Equals(to))
            {
                yield return(new KnowledgePath(startSegment));

                yield break;
            }


            //starting node
            currentQueue.Enqueue(startSegment);
            //delimiter - for counting path length
            currentQueue.Enqueue(null);

            visitedNodes.Add(from);
            visitedNodes.Add(to);

            var currentPathLength = 0;

            while (currentQueue.Count > 0 && currentPathLength < maxLength)
            {
                var currentSegment = currentQueue.Dequeue();
                if (currentSegment == null)
                {
                    ++currentPathLength;
                    //add next delimiter
                    currentQueue.Enqueue(null);
                    continue;
                }

                //test if we can get into end node
                foreach (var edge in BetweenEdges(currentSegment.Node, to))
                {
                    var segment = new PathSegment(currentSegment, edge, to);
                    yield return(new KnowledgePath(segment));
                }

                //explore next children
                foreach (var childPair in GetNeighbours(currentSegment.Node, maxWidth))
                {
                    var edge  = childPair.Item1;
                    var child = childPair.Item2;
                    if (!visitedNodes.Add(child))
                    {
                        //this node has already been visited
                        continue;
                    }

                    var childSegment = new PathSegment(currentSegment, edge, child);
                    currentQueue.Enqueue(childSegment);
                }
            }
        }
Пример #14
0
 /// <inheritdoc/>
 protected internal override IEnumerable <NodeReference> Incoming(NodeReference toNode, string edge)
 {
     return(getTargets(toNode, edge, _inEdges));
 }
Пример #15
0
 /// <summary>
 /// Create new path by prepending given node and edge to current path.
 /// </summary>
 /// <param name="node"></param>
 /// <param name="edge"></param>
 /// <param name="isOutcome"></param>
 /// <returns></returns>
 internal KnowledgePath PrependBy(NodeReference node, Edge edge)
 {
     return(new KnowledgePath(
                new[] { node }.Concat(_nodes),
                new[] { edge }.Concat(_edges)));
 }
Пример #16
0
 /// <summary>
 /// Add edge to the layer.
 /// </summary>
 /// <param name="from">From node.</param>
 /// <param name="edge">Edge between from and to nodes.</param>
 /// <param name="to">To node.</param>
 public void AddEdge(NodeReference from, string edge, NodeReference to)
 {
     addEdge(from, edge, to, _outEdges);
     addEdge(to, edge, from, _inEdges);
 }
Пример #17
0
 internal Trace GetTrace(NodeReference node)
 {
     return(_traceIndex[node]);
 }
Пример #18
0
 /// <summary>
 /// Adds node to the pattern.
 /// </summary>
 /// <param name="node">The node to be added.</param>
 public void AddNode(NodeReference node)
 {
     _groupNodes.Add(node);
 }
Пример #19
0
 abstract internal protected IEnumerable <NodeReference> Incoming(NodeReference toNode, string edge);
Пример #20
0
 /// <inheritdoc/>
 protected internal override IEnumerable <KeyValuePair <string, NodeReference> > Outcoming(NodeReference node)
 {
     return(getEdges(node, _outEdges));
 }
Пример #21
0
 abstract internal protected IEnumerable <KeyValuePair <string, NodeReference> > Outcoming(NodeReference node);
Пример #22
0
 abstract internal protected IEnumerable <string> Edges(NodeReference from, NodeReference to);