/// <summary> /// Fetch the next valid relationship. /// </summary> /// <returns> True is a valid relationship was found </returns> protected internal virtual bool FetchNext() { if (_onRelationship) { _onRelationship = RelationshipCursor.next(); } while (!_onRelationship) { _currentDirection++; if (_currentDirection >= _nDirections) { if (_types != null && _foundTypes >= _types.Length) { _onGroup = false; return(false); } LoopOnRelationship(); } if (!_onGroup) { return(false); } SetupCursors(); } return(true); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldTraverseRelationshipsOfGivenType() public virtual void ShouldTraverseRelationshipsOfGivenType() { // given using (NodeCursor node = cursors.allocateNodeCursor(), RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor(), RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor()) { int empty = 0; // when read.allNodesScan(node); while (node.Next()) { node.Relationships(group); bool none = true; while (group.next()) { none = false; Sizes degree = new Sizes(); group.Outgoing(relationship); while (relationship.next()) { assertEquals("node #" + node.NodeReference() + " relationship should have same label as group", group.Type(), relationship.Type()); degree.Outgoing++; } group.Incoming(relationship); while (relationship.next()) { assertEquals("node #" + node.NodeReference() + "relationship should have same label as group", group.Type(), relationship.Type()); degree.Incoming++; } group.Loops(relationship); while (relationship.next()) { assertEquals("node #" + node.NodeReference() + "relationship should have same label as group", group.Type(), relationship.Type()); degree.Loop++; } // then assertNotEquals("all", 0, degree.Incoming + degree.Outgoing + degree.Loop); assertEquals("node #" + node.NodeReference() + " outgoing", group.OutgoingCount(), degree.Outgoing); assertEquals("node #" + node.NodeReference() + " incoming", group.IncomingCount(), degree.Incoming); assertEquals("node #" + node.NodeReference() + " loop", group.LoopCount(), degree.Loop); assertEquals("node #" + node.NodeReference() + " all = incoming + outgoing - loop", group.TotalCount(), degree.Incoming + degree.Outgoing + degree.Loop); } if (none) { empty++; } } // then assertEquals("number of empty nodes", 1, empty); } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldFollowSpecificRelationship() public virtual void ShouldFollowSpecificRelationship() { // given using (NodeCursor node = cursors.allocateNodeCursor(), RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor(), RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor()) { // when - traversing from start to end read.singleNode(_start, node); assertTrue("access start node", node.Next()); node.Relationships(group); assertTrue("access relationship group", group.next()); group.Outgoing(relationship); assertTrue("access outgoing relationships", relationship.next()); // then assertEquals("source node", _start, relationship.SourceNodeReference()); assertEquals("target node", _end, relationship.TargetNodeReference()); assertEquals("node of origin", _start, relationship.OriginNodeReference()); assertEquals("neighbouring node", _end, relationship.NeighbourNodeReference()); assertEquals("relationship should have same label as group", group.Type(), relationship.Type()); assertFalse("only a single relationship", relationship.next()); group.Incoming(relationship); assertFalse("no incoming relationships", relationship.next()); group.Loops(relationship); assertFalse("no loop relationships", relationship.next()); assertFalse("only a single group", group.next()); // when - traversing from end to start read.singleNode(_end, node); assertTrue("access start node", node.Next()); node.Relationships(group); assertTrue("access relationship group", group.next()); group.Incoming(relationship); assertTrue("access incoming relationships", relationship.next()); // then assertEquals("source node", _start, relationship.SourceNodeReference()); assertEquals("target node", _end, relationship.TargetNodeReference()); assertEquals("node of origin", _end, relationship.OriginNodeReference()); assertEquals("neighbouring node", _start, relationship.NeighbourNodeReference()); assertEquals("relationship should have same label as group", group.Type(), relationship.Type()); assertFalse("only a single relationship", relationship.next()); group.Outgoing(relationship); assertFalse("no outgoing relationships", relationship.next()); group.Loops(relationship); assertFalse("no loop relationships", relationship.next()); assertFalse("only a single group", group.next()); } }
/// <summary> /// Counts the number of incoming relationships from node where the cursor is positioned. /// <para> /// NOTE: The number of incoming relationships also includes eventual loops. /// /// </para> /// </summary> /// <param name="nodeCursor"> a cursor positioned at the node whose relationships we're counting </param> /// <param name="cursors"> a factory for cursors </param> /// <returns> the number of incoming - including loops - relationships from the node </returns> public static int CountIncoming(NodeCursor nodeCursor, CursorFactory cursors) { if (nodeCursor.Dense) { using (RelationshipGroupCursor group = cursors.AllocateRelationshipGroupCursor()) { nodeCursor.Relationships(group); int count = 0; while (group.next()) { count += group.IncomingCount() + group.LoopCount(); } return(count); } } else { using (RelationshipTraversalCursor traversal = cursors.AllocateRelationshipTraversalCursor()) { int count = 0; nodeCursor.AllRelationships(traversal); while (traversal.next()) { if (traversal.TargetNodeReference() == nodeCursor.NodeReference()) { count++; } } return(count); } } }
/// <summary> /// Counts all the relationships of the given type from node where the cursor is positioned. /// </summary> /// <param name="nodeCursor"> a cursor positioned at the node whose relationships we're counting </param> /// <param name="cursors"> a factory for cursors </param> /// <param name="type"> the type of the relationship we're counting </param> /// <returns> the number relationships from the node with the given type </returns> public static int CountAll(NodeCursor nodeCursor, CursorFactory cursors, int type) { if (nodeCursor.Dense) { using (RelationshipGroupCursor group = cursors.AllocateRelationshipGroupCursor()) { nodeCursor.Relationships(group); int count = 0; while (group.next()) { if (group.Type() == type) { return(group.TotalCount()); } } return(count); } } else { using (RelationshipTraversalCursor traversal = cursors.AllocateRelationshipTraversalCursor()) { int count = 0; nodeCursor.AllRelationships(traversal); while (traversal.next()) { if (traversal.Type() == type) { count++; } } return(count); } } }
/// <summary> /// Counts the number of outgoing relationships of the given type from node where the cursor is positioned. /// <para> /// NOTE: The number of outgoing relationships also includes eventual loops. /// /// </para> /// </summary> /// <param name="nodeCursor"> a cursor positioned at the node whose relationships we're counting </param> /// <param name="cursors"> a factory for cursors </param> /// <param name="type"> the type of the relationship we're counting </param> /// <returns> the number of outgoing - including loops - relationships from the node with the given type </returns> public static int CountOutgoing(NodeCursor nodeCursor, CursorFactory cursors, int type) { if (nodeCursor.Dense) { using (RelationshipGroupCursor group = cursors.AllocateRelationshipGroupCursor()) { nodeCursor.Relationships(group); while (group.next()) { if (group.Type() == type) { return(group.OutgoingCount() + group.LoopCount()); } } return(0); } } else { using (RelationshipTraversalCursor traversal = cursors.AllocateRelationshipTraversalCursor()) { int count = 0; nodeCursor.AllRelationships(traversal); while (traversal.next()) { if (traversal.SourceNodeReference() == nodeCursor.NodeReference() && traversal.Type() == type) { count++; } } return(count); } } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldManageRandomTraversals() public virtual void ShouldManageRandomTraversals() { // given try { using (NodeCursor node = cursors.allocateNodeCursor(), RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor(), RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor()) { for (int i = 0; i < N_TRAVERSALS; i++) { // when long nodeId = _nodeIds[_random.Next(_nNodes)]; read.singleNode(nodeId, node); assertTrue("access root node", node.Next()); node.Relationships(group); assertFalse("single root", node.Next()); // then while (group.next()) { group.Incoming(relationship); while (relationship.next()) { assertEquals("incoming origin", nodeId, relationship.OriginNodeReference()); relationship.Neighbour(node); } group.Outgoing(relationship); while (relationship.next()) { assertEquals("outgoing origin", nodeId, relationship.OriginNodeReference()); relationship.Neighbour(node); } group.Loops(relationship); while (relationship.next()) { assertEquals("loop origin", nodeId, relationship.OriginNodeReference()); relationship.Neighbour(node); } } } } } catch (Exception t) { throw new Exception("Failed with random seed " + _seed, t); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: static java.util.Map<String,int> count(org.neo4j.internal.kernel.api.Transaction transaction, RelationshipTraversalCursor relationship) throws org.neo4j.internal.kernel.api.exceptions.KernelException internal static IDictionary <string, int> Count([email protected] transaction, RelationshipTraversalCursor relationship) { Dictionary <string, int> counts = new Dictionary <string, int>(); while (relationship.next()) { string key = ComputeKey(transaction, relationship); Counts.compute(key, (k, value) => value == null ? 1 : value + 1); } return(counts); }
/// <summary> /// Fetch the next valid relationship. /// </summary> /// <returns> True is a valid relationship was found </returns> protected internal virtual bool FetchNext() { if (_onRelationship || _firstNext) { _firstNext = false; do { _onRelationship = Cursor.next(); } while (_onRelationship && (!CorrectDirection() || !CorrectType())); } return(_onRelationship); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: static void assertCount(org.neo4j.internal.kernel.api.Transaction transaction, RelationshipTraversalCursor relationship, java.util.Map<String,int> expectedCounts, int expectedType, org.neo4j.graphdb.Direction direction) throws org.neo4j.internal.kernel.api.exceptions.KernelException internal static void AssertCount([email protected] transaction, RelationshipTraversalCursor relationship, IDictionary <string, int> expectedCounts, int expectedType, Direction direction) { string key = ComputeKey(transaction.Token().relationshipTypeName(expectedType), direction); int expectedCount = expectedCounts.getOrDefault(key, 0); int count = 0; while (relationship.next()) { assertEquals("same type", expectedType, relationship.Type()); count++; } assertEquals(format("expected number of relationships for key '%s'", key), expectedCount, count); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldTraverseTreeOfDepthThree() public virtual void ShouldTraverseTreeOfDepthThree() { using (NodeCursor node = cursors.allocateNodeCursor(), RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor(), RelationshipTraversalCursor relationship1 = cursors.allocateRelationshipTraversalCursor(), RelationshipTraversalCursor relationship2 = cursors.allocateRelationshipTraversalCursor()) { MutableLongSet leafs = new LongHashSet(); long total = 0; // when read.singleNode(_threeRoot, node); assertTrue("access root node", node.Next()); node.Relationships(group); assertFalse("single root", node.Next()); assertTrue("access group of root", group.next()); group.Incoming(relationship1); assertFalse("single group of root", group.next()); while (relationship1.next()) { relationship1.Neighbour(node); assertTrue("child level 1", node.Next()); node.Relationships(group); assertFalse("single node", node.Next()); assertTrue("group of level 1 child", group.next()); group.Incoming(relationship2); assertFalse("single group of level 1 child", group.next()); while (relationship2.next()) { leafs.add(relationship2.NeighbourNodeReference()); total++; } } // then assertEquals("total number of leaf nodes", _expectedTotal, total); assertEquals("number of distinct leaf nodes", _expectedUnique, leafs.size()); } }