示例#1
0
 public override TraversalBranch Next(PathExpander expander, TraversalContext context)
 {
     if (_relationships == null)
     {
         ExpandRelationships(expander);
     }
     while (_relationships.MoveNext())
     {
         Relationship relationship = _relationships.Current;
         if (relationship.Equals(_howIGotHere))
         {
             context.UnnecessaryRelationshipTraversed();
             continue;
         }
         _expandedCount++;
         Node node = relationship.GetOtherNode(_source);
         // TODO maybe an unnecessary instantiation. Instead pass in this+node+relationship to uniqueness check
         TraversalBranch next = NewNextBranch(node, relationship);
         if (context.IsUnique(next))
         {
             context.RelationshipTraversed();
             next.Initialize(expander, context);
             return(next);
         }
         else
         {
             context.UnnecessaryRelationshipTraversed();
         }
     }
     ResetRelationships();
     return(null);
 }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void testFilteredPathEvaluation()
        internal virtual void TestFilteredPathEvaluation()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.graphdb.PropertyContainer endNode = mock(org.neo4j.graphdb.Node.class);
            PropertyContainer endNode = mock(typeof(Node));
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.graphdb.PropertyContainer alternativeEndNode = mock(org.neo4j.graphdb.Node.class);
            PropertyContainer alternativeEndNode = mock(typeof(Node));
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.graphdb.Node startNode = mock(org.neo4j.graphdb.Node.class);
            Node            startNode         = mock(typeof(Node));
            Evaluator       evaluator         = mock(typeof(Evaluator));
            TraversalBranch branch            = mock(typeof(TraversalBranch));
            TraversalBranch alternativeBranch = mock(typeof(TraversalBranch));

            when(branch.GetEnumerator()).thenAnswer(new IteratorAnswer(endNode));
            when(alternativeBranch.GetEnumerator()).thenAnswer(new IteratorAnswer(alternativeEndNode));
            when(alternativeBranch.StartNode()).thenReturn(startNode);
            when(evaluator.Evaluate(Mockito.any(typeof(Path)))).thenReturn(Evaluation.INCLUDE_AND_CONTINUE);
            StandardBranchCollisionDetector collisionDetector = new StandardBranchCollisionDetector(evaluator, path => alternativeEndNode.Equals(path.endNode()) && startNode.Equals(path.startNode()));

            ICollection <Path> incoming            = collisionDetector.Evaluate(branch, Direction.INCOMING);
            ICollection <Path> outgoing            = collisionDetector.Evaluate(branch, Direction.OUTGOING);
            ICollection <Path> alternativeIncoming = collisionDetector.Evaluate(alternativeBranch, Direction.INCOMING);
            ICollection <Path> alternativeOutgoing = collisionDetector.Evaluate(alternativeBranch, Direction.OUTGOING);

            assertNull(incoming);
            assertNull(outgoing);
            assertNull(alternativeIncoming);
            assertEquals(1, alternativeOutgoing.Count);
        }
示例#3
0
 /*
  * For expansion sources for all nodes except the start node
  */
 internal TraversalBranchImpl(TraversalBranch parent, int depth, Node source, Relationship toHere)
 {
     this.ParentConflict          = parent;
     this._source                 = source;
     this._howIGotHere            = toHere;
     this._depthAndEvaluationBits = depth;
 }
        private IEnumerable <Node> GatherNodes(TraversalBranch first, TraversalBranch then)
        {
            // TODO Don't loop through them all up front
            LinkedList <Node> nodes  = new LinkedList <Node>();
            TraversalBranch   branch = first;

            while (branch.Length() > 0)
            {
                nodes.AddFirst(branch.EndNode());
                branch = branch.Parent();
            }
            if (_cachedStartNode == null && first == _start && branch.Length() >= 0)
            {
                _cachedStartNode = branch.EndNode();
            }
            nodes.AddFirst(branch.EndNode());
            branch = then.Parent();
            if (branch != null)
            {
                while (branch.Length() > 0)
                {
                    nodes.AddLast(branch.EndNode());
                    branch = branch.Parent();
                }
                if (branch.Length() >= 0)
                {
                    nodes.AddLast(branch.EndNode());
                }
            }
            if (_cachedStartNode == null && then == _start && branch != null && branch.Length() >= 0)
            {
                _cachedStartNode = branch.EndNode();
            }
            return(nodes);
        }
示例#5
0
        public override bool Equals(object obj)
        {
            if (obj == this)
            {
                return(true);
            }
            if (!(obj is TraversalBranch))
            {
                return(false);
            }

            TraversalBranch branch = this;
            TraversalBranch other  = ( TraversalBranch )obj;

            if (branch.Length() != other.Length())
            {
                return(false);
            }

            while (branch.Length() > 0)
            {
                if (!branch.LastRelationship().Equals(other.LastRelationship()))
                {
                    return(false);
                }
                branch = branch.Parent();
                other  = other.Parent();
            }
            return(true);
        }
        public override IEnumerator <PropertyContainer> Iterator()
        {
            // TODO Don't loop through them all up front
            LinkedList <PropertyContainer> entities = new LinkedList <PropertyContainer>();
            TraversalBranch branch = _start;

            while (branch.Length() > 0)
            {
                entities.AddFirst(branch.EndNode());
                entities.AddFirst(branch.LastRelationship());
                branch = branch.Parent();
            }
            entities.AddFirst(branch.EndNode());
            if (_cachedStartNode == null)
            {
                _cachedStartNode = branch.EndNode();
            }
            if (_end.length() > 0)
            {
                entities.AddLast(_end.lastRelationship());
                branch = _end.parent();
                while (branch.Length() > 0)
                {
                    entities.AddLast(branch.EndNode());
                    entities.AddLast(branch.LastRelationship());
                    branch = branch.Parent();
                }
                entities.AddLast(branch.EndNode());
            }
            return(entities.GetEnumerator());
        }
        private LinkedList <Relationship> GatherRelationships(TraversalBranch first, TraversalBranch then)
        {
            // TODO Don't loop through them all up front
            LinkedList <Relationship> relationships = new LinkedList <Relationship>();
            TraversalBranch           branch        = first;

            while (branch.Length() > 0)
            {
                relationships.AddFirst(branch.LastRelationship());
                branch = branch.Parent();
            }
            // We can might as well cache start node since we're right now there anyway
            if (_cachedStartNode == null && first == _start && branch.Length() >= 0)
            {
                _cachedStartNode = branch.EndNode();
            }
            branch = then;
            while (branch.Length() > 0)
            {
                relationships.AddLast(branch.LastRelationship());
                branch = branch.Parent();
            }
            if (_cachedStartNode == null && then == _start && branch.Length() >= 0)
            {
                _cachedStartNode = branch.EndNode();
            }
            return(relationships);
        }
        private IList <TraversalBranch> GatherOneLevel(IList <TraversalBranch> queue, TraversalContext metadata)
        {
            IList <TraversalBranch> level = new LinkedList <TraversalBranch>();
            int?depth = null;

            foreach (TraversalBranch source in queue)
            {
                if (depth == null)
                {
                    depth = source.Length();
                }
                else if (source.Length() != depth.Value)
                {
                    break;
                }

                while (true)
                {
                    TraversalBranch next = source.Next(_expander, metadata);
                    if (next == null)
                    {
                        break;
                    }
                    level.Add(next);
                }
            }
            return(level);
        }
示例#9
0
        public override TraversalBranch Next(TraversalContext metadata)
        {
            TraversalBranch result = null;

            while (result == null)
            {
                if (_current == null)
                {
                    _current = _superNodes.RemoveFirst();
                    if (_current == null)
                    {
                        return(null);
                    }
                }
                else if (_current.expanded() > 0 && _current.expanded() % _threshold == 0)
                {
                    _superNodes.AddLast(_current);
                    _current = _current.parent();
                    continue;
                }

                TraversalBranch next = _current.next(_expander, metadata);
                if (next == null)
                {
                    _current = _current.parent();
                    continue;
                }
                _current = next;
                result   = _current;
            }
            return(result);
        }
示例#10
0
 /*
  * For the start node branches
  */
 internal TraversalBranchImpl(TraversalBranch parent, Node source)
 {
     this.ParentConflict          = parent;
     this._source                 = source;
     this._howIGotHere            = null;
     this._depthAndEvaluationBits = 0;
 }
示例#11
0
        private TraversalBranch FindStartBranch()
        {
            TraversalBranch branch = this;

            while (branch.Length() > 0)
            {
                branch = branch.Parent();
            }
            return(branch);
        }
示例#12
0
 protected internal override Node fetchNextOrNull()
 {
     try
     {
         return(branch.length() >= 0 ? branch.endNode() : null);
     }
     finally
     {
         branch = branch.parent();
     }
 }
示例#13
0
 protected internal override Relationship fetchNextOrNull()
 {
     try
     {
         return(branch != null?branch.lastRelationship() : null);
     }
     finally
     {
         branch = branch != null?branch.parent() : null;
     }
 }
示例#14
0
            public override TraversalBranch Next(TraversalContext metadata)
            {
                // Exhaust current if not already exhausted
                while (true)
                {
                    TraversalBranch next = Current.next(Expander, metadata);
                    if (next == null)
                    {
                        break;
                    }

                    long      endNodeId = next.EndNode().Id;
                    Visit <P> stay      = Visits[endNodeId];

                    D cost        = outerInstance.CalculateValue(next);
                    P newPriority = outerInstance.AddPriority(next, CurrentAggregatedValue, cost);

                    bool newStay = stay == null;
                    if (newStay)
                    {
                        stay = new Visit <P>(newPriority);
                        Visits[endNodeId] = stay;
                    }
                    if (newStay || !outerInstance.interest.CanBeRuledOut(stay.VisitCount, newPriority, stay.Cost))
                    {
                        if (outerInstance.interest.Comparator().Compare(newPriority, stay.Cost) < 0)
                        {
                            stay.Cost = newPriority;
                        }
                        Queue.put(next, newPriority);
                    }
                }

                do
                {
                    // Pop the top from priorityMap
                    Entry <TraversalBranch, P> entry = Queue.pop();
                    if (entry != null)
                    {
                        Current = entry.Entity;
                        Visit <P> stay = Visits[Current.endNode().Id];
                        stay.VisitCount++;
                        if (outerInstance.interest.StillInteresting(stay.VisitCount))
                        {
                            CurrentAggregatedValue = entry.Priority;
                            return(Current);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                } while (true);
            }
        protected internal virtual bool IncludePath(Path path, TraversalBranch startPath, TraversalBranch endPath)
        {
            Evaluation eval = _evaluator.evaluate(path);

            if (!eval.continues())
            {
                startPath.Evaluation(eval);
                endPath.Evaluation(eval);
            }
            return(eval.includes());
        }
示例#16
0
        public override bool Check(TraversalBranch branch)
        {
            long id  = Type.getId(branch);
            bool add = _recentlyVisited.get(id) == null;

            if (add)
            {
                _recentlyVisited.put(id, _placeHolder);
            }
            return(add);
        }
示例#17
0
        public override bool Check(TraversalBranch branch)
        {
            int            level    = branch.Length();
            MutableLongSet levelIds = _idsPerLevel.get(level);

            if (levelIds == null)
            {
                levelIds = new LongHashSet();
                _idsPerLevel.put(level, levelIds);
            }
            return(levelIds.add(Type.getId(branch)));
        }
示例#18
0
        public override IEnumerable <Relationship> Relationships()
        {
            LinkedList <Relationship> relationships = new LinkedList <Relationship>();
            TraversalBranch           branch        = this;

            while (branch.Length() > 0)
            {
                relationships.AddFirst(branch.LastRelationship());
                branch = branch.Parent();
            }
            return(relationships);
        }
示例#19
0
            public BestFirstSelector(BestFirstSelectorFactory <P, D> outerInstance, TraversalBranch source, P startData, PathExpander expander)
            {
                this._outerInstance = outerInstance;

                if (!InstanceFieldsInitialized)
                {
                    InitializeInstanceFields();
                    InstanceFieldsInitialized = true;
                }
                this.Current = source;
                this.CurrentAggregatedValue = startData;
                this.Expander = expander;
            }
        internal BidirectionalTraversalBranchPath(TraversalBranch start, TraversalBranch end)
        {
            this._start = start;
            this._end   = end;

            // Most used properties: endNode and lastRelationship, so cache them right away (semi-expensive though).
            IEnumerator <PropertyContainer> endPathEntities = end.GetEnumerator();

//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
            this._endNode = ( Node )endPathEntities.next();
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
            this._lastRelationship = endPathEntities.hasNext() ? (Relationship)endPathEntities.next() : start.LastRelationship();
        }
示例#21
0
        public override IEnumerable <Node> Nodes()
        {
            LinkedList <Node> nodes  = new LinkedList <Node>();
            TraversalBranch   branch = this;

            while (branch.Length() > 0)
            {
                nodes.AddFirst(branch.EndNode());
                branch = branch.Parent();
            }
            nodes.AddFirst(branch.EndNode());
            return(nodes);
        }
示例#22
0
        public override IEnumerator <PropertyContainer> Iterator()
        {
            LinkedList <PropertyContainer> entities = new LinkedList <PropertyContainer>();
            TraversalBranch branch = this;

            while (branch.Length() > 0)
            {
                entities.AddFirst(branch.EndNode());
                entities.AddFirst(branch.LastRelationship());
                branch = branch.Parent();
            }
            entities.AddFirst(branch.EndNode());
            return(entities.GetEnumerator());
        }
示例#23
0
        public override bool Check(TraversalBranch source)
        {
            long idToCompare = Type.getId(source);

            while (source.Length() > 0)
            {
                source = source.Parent();
                if (Type.idEquals(source, idToCompare))
                {
                    return(false);
                }
            }
            return(true);
        }
示例#24
0
        public override int GetHashCode()
        {
            TraversalBranch branch   = this;
            int             hashCode = 1;

            while (branch.Length() > 0)
            {
                Relationship relationship = branch.LastRelationship();
                hashCode = 31 * hashCode + relationship.GetHashCode();
                branch   = branch.Parent();
            }
            if (hashCode == 1)
            {
                hashCode = EndNode().GetHashCode();
            }
            return(hashCode);
        }
示例#25
0
        public override TraversalBranch Next(TraversalContext metadata)
        {
            TraversalBranch branch        = NextBranchFromCurrentSelector(metadata, false);
            Entry           state         = StateForCurrentSelector;
            AtomicInteger   previousDepth = state.Depth;

            if (branch != null && branch.Length() == previousDepth.get())
            {               // Same depth as previous branch returned from this side.
                return(branch);
            }

            if (branch != null)
            {
                _totalDepth.set(CurrentSide(), branch.Length());
            }
            if ((_stopDescentOnResult && (metadata.NumberOfPathsReturned > 0)) || (_totalDepth.get() > (_maxDepth + 1)))
            {
                NextSelector();
                return(null);
            }

            if (branch != null)
            {
                previousDepth.set(branch.Length());
                state.Branch = branch;
            }
            BranchSelector  otherSelector = NextSelector();
            Entry           otherState    = StateForCurrentSelector;
            TraversalBranch otherBranch   = otherState.Branch;

            if (otherBranch != null)
            {
                otherState.Branch = null;
                return(otherBranch);
            }

            otherBranch = otherSelector.Next(metadata);
            if (otherBranch != null)
            {
                return(otherBranch);
            }
            else
            {
                return(branch);
            }
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Override @SuppressWarnings("unchecked") public java.util.Collection<org.neo4j.graphdb.Path> evaluate(org.neo4j.graphdb.traversal.TraversalBranch branch, org.neo4j.graphdb.Direction direction)
        public override ICollection <Path> Evaluate(TraversalBranch branch, Direction direction)
        {
            // [0] for paths from start, [1] for paths from end
            ICollection <TraversalBranch>[] pathsHere = _paths[branch.EndNode()];
            int index = direction.ordinal();

            if (pathsHere == null)
            {
                pathsHere = new System.Collections.ICollection[]
                {
                    new List <TraversalBranch>(),
                    new List <TraversalBranch>()
                };
                _paths[branch.EndNode()] = pathsHere;
            }
            pathsHere[index].Add(branch);

            // If there are paths from the other side then include all the
            // combined paths
            ICollection <TraversalBranch> otherCollections = pathsHere[index == 0 ? 1 : 0];

            if (otherCollections.Count > 0)
            {
                ICollection <Path> foundPaths = new List <Path>();
                foreach (TraversalBranch otherBranch in otherCollections)
                {
                    TraversalBranch startPath             = index == 0 ? branch : otherBranch;
                    TraversalBranch endPath               = index == 0 ? otherBranch : branch;
                    BidirectionalTraversalBranchPath path = new BidirectionalTraversalBranchPath(startPath, endPath);
                    if (IsAcceptablePath(path))
                    {
                        if (_returnedPaths.Add(path) && IncludePath(path, startPath, endPath))
                        {
                            foundPaths.Add(path);
                        }
                    }
                }

                if (foundPaths.Count > 0)
                {
                    return(foundPaths);
                }
            }
            return(null);
        }
示例#27
0
        public override TraversalBranch Next(TraversalContext metadata)
        {
            TraversalBranch branch        = NextBranchFromNextSelector(metadata, true);
            int?            previousDepth = StateForCurrentSelector;

            if (branch != null && branch.Length() == previousDepth.Value)
            {
                return(branch);
            }
            else
            {
                if (branch != null)
                {
                    StateForCurrentSelector = branch.Length();
                }
            }
            return(branch);
        }
        public override TraversalBranch Next(TraversalContext metadata)
        {
            TraversalBranch result = null;

            while (result == null)
            {
                if (_current == null)
                {
                    return(null);
                }
                TraversalBranch next = _current.next(_expander, metadata);
                if (next == null)
                {
                    _current = _current.parent();
                    continue;
                }
                _current = next;
                result   = _current;
            }
            return(result);
        }
        protected internal override Path FetchNextOrNull()
        {
            if (_foundPaths != null)
            {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                if (_foundPaths.hasNext())
                {
                    NumberOfPathsReturnedConflict++;
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                    Path next = _foundPaths.next();
                    return(next);
                }
                _foundPaths = null;
            }

            TraversalBranch result = null;

            while (true)
            {
                result = _selector.next(this);
                if (result == null)
                {
                    Close();
                    return(null);
                }
                IEnumerable <Path> pathCollisions = _collisionDetector.evaluate(result, _selector.currentSide());
                if (pathCollisions != null)
                {
                    _foundPaths = pathCollisions.GetEnumerator();
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                    if (_foundPaths.hasNext())
                    {
                        NumberOfPathsReturnedConflict++;
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                        return(_foundPaths.next());
                    }
                }
            }
        }
        public override TraversalBranch Next(TraversalContext metadata)
        {
            TraversalBranch result = null;

            while (result == null)
            {
                TraversalBranch next = _current.next(_expander, metadata);
                if (next != null)
                {
                    _queue.AddLast(next);
                    result = next;
                }
                else
                {
                    _current = _queue.RemoveFirst();
                    if (_current == null)
                    {
                        return(null);
                    }
                }
            }
            return(result);
        }