org.neo4j.graphdb.Node.getDegree()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(13.7k)|赞(0)|评价(0)|浏览(274)

本文整理了Java中org.neo4j.graphdb.Node.getDegree()方法的一些代码示例,展示了Node.getDegree()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Node.getDegree()方法的具体详情如下:
包路径:org.neo4j.graphdb.Node
类名称:Node
方法名:getDegree

Node.getDegree介绍

[英]Returns the number of relationships connected to this node regardless of direction or type. This operation is always O(1).
[中]返回连接到此节点的关系数,无论其方向或类型如何。此操作始终为O(1)。

代码示例

代码示例来源:origin: neo4j/neo4j

  1. @SuppressWarnings( "unchecked" )
  2. public Representation getNodeDegree( long nodeId, RelationshipDirection direction, Collection<String> types )
  3. throws NodeNotFoundException
  4. {
  5. Node node = node( nodeId );
  6. if ( types.isEmpty() )
  7. {
  8. return PropertiesRepresentation.value( node.getDegree( direction.internal ) );
  9. }
  10. else
  11. {
  12. int sum = 0;
  13. for ( String type : types )
  14. {
  15. sum += node.getDegree( RelationshipType.withName( type ), direction.internal );
  16. }
  17. return PropertiesRepresentation.value( sum );
  18. }
  19. }

代码示例来源:origin: neo4j/neo4j

  1. private void assertCounts( Node me, Map<RelType, int[]> expectedCounts )
  2. {
  3. assertEquals( totalCount( expectedCounts, Direction.BOTH ), me.getDegree() );
  4. assertEquals( totalCount( expectedCounts, Direction.BOTH ), me.getDegree( Direction.BOTH ) );
  5. assertEquals( totalCount( expectedCounts, Direction.OUTGOING ), me.getDegree( Direction.OUTGOING ) );
  6. assertEquals( totalCount( expectedCounts, Direction.INCOMING ), me.getDegree( Direction.INCOMING ) );
  7. for ( Map.Entry<RelType, int[]> entry : expectedCounts.entrySet() )
  8. {
  9. RelType type = entry.getKey();
  10. assertEquals( totalCount( entry.getValue(), Direction.BOTH ), me.getDegree( type ) );
  11. assertEquals( totalCount( entry.getValue(), Direction.OUTGOING ), me.getDegree( type, Direction.OUTGOING ) );
  12. assertEquals( totalCount( entry.getValue(), Direction.INCOMING ), me.getDegree( type, Direction.INCOMING ) );
  13. assertEquals( totalCount( entry.getValue(), Direction.BOTH ), me.getDegree( type, Direction.BOTH ) );
  14. }
  15. }

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void movingBilaterallyOfTheDenseNodeThresholdIsConsistent()
  3. {
  4. // GIVEN
  5. GraphDatabaseService db = databaseRule.getGraphDatabaseAPI();
  6. Node root;
  7. // WHEN
  8. try ( Transaction tx = db.beginTx() )
  9. {
  10. root = db.createNode();
  11. createRelationshipsOnNode( db, root, 100 );
  12. deleteRelationshipsFromNode( root, 80 );
  13. assertEquals( 20, root.getDegree() );
  14. assertEquals( 20, root.getDegree( Direction.OUTGOING ) );
  15. assertEquals( 0, root.getDegree( Direction.INCOMING ) );
  16. tx.success();
  17. }
  18. // THEN
  19. try ( Transaction tx = db.beginTx() )
  20. {
  21. assertEquals( 20, root.getDegree() );
  22. assertEquals( 20, root.getDegree( Direction.OUTGOING ) );
  23. assertEquals( 0, root.getDegree( Direction.INCOMING ) );
  24. tx.success();
  25. }
  26. }

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void withLoops()
  3. {
  4. // Just to make sure it doesn't work by accident what with ids aligning with count
  5. for ( int i = 0; i < 10; i++ )
  6. {
  7. getGraphDb().createNode().createRelationshipTo( getGraphDb().createNode(), MyRelTypes.TEST );
  8. }
  9. Node node = getGraphDb().createNode();
  10. assertEquals( 0, node.getDegree() );
  11. node.createRelationshipTo( node, MyRelTypes.TEST );
  12. assertEquals( 1, node.getDegree() );
  13. Node otherNode = getGraphDb().createNode();
  14. Relationship rel2 = node.createRelationshipTo( otherNode, MyRelTypes.TEST2 );
  15. assertEquals( 2, node.getDegree() );
  16. assertEquals( 1, otherNode.getDegree() );
  17. newTransaction();
  18. assertEquals( 2, node.getDegree() );
  19. Relationship rel3 = node.createRelationshipTo( node, MyRelTypes.TEST_TRAVERSAL );
  20. assertEquals( 3, node.getDegree() );
  21. assertEquals( 1, otherNode.getDegree() );
  22. rel2.delete();
  23. assertEquals( 2, node.getDegree() );
  24. assertEquals( 0, otherNode.getDegree() );
  25. rel3.delete();
  26. assertEquals( 1, node.getDegree() );
  27. }

代码示例来源:origin: neo4j/neo4j

  1. assertEquals( 100, source.getDegree() );
  2. assertEquals( 100, source.getDegree( Direction.OUTGOING ) );
  3. assertEquals( 0, source.getDegree( Direction.INCOMING ) );
  4. assertEquals( 25, source.getDegree( RelationshipType.withName( "Type0" ) ) );
  5. assertEquals( 25, source.getDegree( RelationshipType.withName( "Type1" ) ) );
  6. assertEquals( 25, source.getDegree( RelationshipType.withName( "Type2" ) ) );
  7. assertEquals( 25, source.getDegree( RelationshipType.withName( "Type3" ) ) );
  8. assertEquals( 100, sink.getDegree() );
  9. assertEquals( 0, sink.getDegree( Direction.OUTGOING ) );
  10. assertEquals( 100, sink.getDegree( Direction.INCOMING ) );
  11. assertEquals( 25, sink.getDegree( RelationshipType.withName( "Type0" ) ) );
  12. assertEquals( 25, sink.getDegree( RelationshipType.withName( "Type1" ) ) );
  13. assertEquals( 25, sink.getDegree( RelationshipType.withName( "Type2" ) ) );
  14. assertEquals( 25, sink.getDegree( RelationshipType.withName( "Type3" ) ) );
  15. tx.success();
  16. assertEquals( 100, source.getDegree() );
  17. assertEquals( 100, source.getDegree( Direction.OUTGOING ) );
  18. assertEquals( 0, source.getDegree( Direction.INCOMING ) );
  19. assertEquals( 25, source.getDegree( RelationshipType.withName( "Type0" ) ) );
  20. assertEquals( 25, source.getDegree( RelationshipType.withName( "Type1" ) ) );
  21. assertEquals( 25, source.getDegree( RelationshipType.withName( "Type2" ) ) );
  22. assertEquals( 25, source.getDegree( RelationshipType.withName( "Type3" ) ) );
  23. assertEquals( 100, sink.getDegree() );
  24. assertEquals( 0, sink.getDegree( Direction.OUTGOING ) );
  25. assertEquals( 100, sink.getDegree( Direction.INCOMING ) );
  26. assertEquals( 25, sink.getDegree( RelationshipType.withName( "Type0" ) ) );

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void deletingRelationshipsFromDenseNodeIsConsistent()
  3. {
  4. // GIVEN
  5. GraphDatabaseService db = databaseRule.getGraphDatabaseAPI();
  6. Node root;
  7. try ( Transaction tx = db.beginTx() )
  8. {
  9. root = db.createNode();
  10. createRelationshipsOnNode( db, root, 100 );
  11. tx.success();
  12. }
  13. try ( Transaction tx = db.beginTx() )
  14. {
  15. deleteRelationshipsFromNode( root, 80 );
  16. assertEquals( 20, root.getDegree() );
  17. assertEquals( 20, root.getDegree( Direction.OUTGOING ) );
  18. assertEquals( 0, root.getDegree( Direction.INCOMING ) );
  19. tx.success();
  20. }
  21. try ( Transaction tx = db.beginTx() )
  22. {
  23. assertEquals( 20, root.getDegree() );
  24. assertEquals( 20, root.getDegree( Direction.OUTGOING ) );
  25. assertEquals( 0, root.getDegree( Direction.INCOMING ) );
  26. tx.success();
  27. }
  28. }

代码示例来源:origin: neo4j/neo4j

  1. assertEquals( 100, root.getDegree() );
  2. assertEquals( 100, root.getDegree( Direction.OUTGOING ) );
  3. assertEquals( 0, root.getDegree( Direction.INCOMING ) );
  4. assertEquals( 25, root.getDegree( RelationshipType.withName( "Type0" ) ) );
  5. assertEquals( 25, root.getDegree( RelationshipType.withName( "Type1" ) ) );
  6. assertEquals( 25, root.getDegree( RelationshipType.withName( "Type2" ) ) );
  7. assertEquals( 25, root.getDegree( RelationshipType.withName( "Type3" ) ) );
  8. tx.success();
  9. assertEquals( 100, root.getDegree() );
  10. assertEquals( 100, root.getDegree( Direction.OUTGOING ) );
  11. assertEquals( 0, root.getDegree( Direction.INCOMING ) );
  12. assertEquals( 25, root.getDegree( RelationshipType.withName( "Type0" ) ) );
  13. assertEquals( 25, root.getDegree( RelationshipType.withName( "Type1" ) ) );
  14. assertEquals( 25, root.getDegree( RelationshipType.withName( "Type2" ) ) );
  15. assertEquals( 25, root.getDegree( RelationshipType.withName( "Type3" ) ) );
  16. tx.success();

代码示例来源:origin: neo4j/neo4j

  1. assertEquals( 0, node1.getDegree() );
  2. assertEquals( 0, node2.getDegree() );
  3. node1.createRelationshipTo( node2, MyRelTypes.TEST );
  4. assertEquals( 1, node1.getDegree() );
  5. assertEquals( 1, node2.getDegree() );
  6. node1.createRelationshipTo( getGraphDb().createNode(), MyRelTypes.TEST2 );
  7. assertEquals( 2, node1.getDegree() );
  8. assertEquals( 1, node2.getDegree() );
  9. newTransaction();
  10. assertEquals( 2, node1.getDegree() );
  11. assertEquals( 1, node2.getDegree() );
  12. assertEquals( i + 2 + 1, node1.getDegree() );
  13. assertEquals( i + 1 + 1, node2.getDegree() );
  14. if ( i % 10 == 0 )
  15. assertEquals( 1002, node1.getDegree() );
  16. assertEquals( 1002, node1.getDegree( Direction.BOTH ) );
  17. assertEquals( 502, node1.getDegree( Direction.OUTGOING ) );
  18. assertEquals( 500, node1.getDegree( Direction.INCOMING ) );
  19. assertEquals( 1, node1.getDegree( MyRelTypes.TEST2 ) );
  20. assertEquals( 1001, node1.getDegree( MyRelTypes.TEST ) );
  21. assertEquals( 1001, node1.getDegree( MyRelTypes.TEST, Direction.BOTH ) );
  22. assertEquals( 501, node1.getDegree( MyRelTypes.TEST, Direction.OUTGOING ) );
  23. assertEquals( 500, node1.getDegree( MyRelTypes.TEST, Direction.INCOMING ) );
  24. assertEquals( 1, node1.getDegree( MyRelTypes.TEST2, Direction.OUTGOING ) );
  25. assertEquals( 0, node1.getDegree( MyRelTypes.TEST2, Direction.INCOMING ) );
  26. newTransaction();

代码示例来源:origin: neo4j/neo4j

  1. private void assertDegrees( Node node )
  2. {
  3. for ( RelationshipType type : node.getRelationshipTypes() )
  4. {
  5. for ( Direction direction : Direction.values() )
  6. {
  7. long degree = node.getDegree( type, direction );
  8. long actualDegree = count( node.getRelationships( type, direction ) );
  9. assertEquals( actualDegree, degree );
  10. }
  11. }
  12. }

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void convertNodeToDense()
  3. {
  4. Node node = getGraphDb().createNode();
  5. EnumMap<MyRelTypes,Set<Relationship>> rels = new EnumMap<>( MyRelTypes.class );
  6. for ( MyRelTypes type : MyRelTypes.values() )
  7. {
  8. rels.put( type, new HashSet<>() );
  9. }
  10. int expectedRelCount = 0;
  11. for ( int i = 0; i < 6; i++, expectedRelCount++ )
  12. {
  13. MyRelTypes type = MyRelTypes.values()[i % MyRelTypes.values().length];
  14. Relationship rel = node.createRelationshipTo( getGraphDb().createNode(), type );
  15. rels.get( type ).add( rel );
  16. }
  17. newTransaction();
  18. for ( int i = 0; i < 1000; i++, expectedRelCount++ )
  19. {
  20. node.createRelationshipTo( getGraphDb().createNode(), MyRelTypes.TEST );
  21. }
  22. assertEquals( expectedRelCount, node.getDegree() );
  23. assertEquals( expectedRelCount, node.getDegree( Direction.BOTH ) );
  24. assertEquals( expectedRelCount, node.getDegree( Direction.OUTGOING ) );
  25. assertEquals( 0, node.getDegree( Direction.INCOMING ) );
  26. assertEquals( rels.get( MyRelTypes.TEST2 ), Iterables.asSet( node.getRelationships( MyRelTypes.TEST2 ) ) );
  27. assertEquals( join( rels.get( MyRelTypes.TEST_TRAVERSAL ), rels.get( MyRelTypes.TEST2 ) ),
  28. Iterables.asSet( node.getRelationships( MyRelTypes.TEST_TRAVERSAL, MyRelTypes.TEST2 ) ) );
  29. }

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

  1. private int getDegreeSafe(Node node, RelationshipType relType, Direction direction) {
  2. if (relType == null) {
  3. return node.getDegree(direction);
  4. }
  5. return node.getDegree(relType, direction);
  6. }

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void createDenseNodeWithLowThreshold()
  3. {
  4. newDb( 2 );
  5. // Create node with two relationships
  6. Node node;
  7. try ( Transaction tx = db.beginTx() )
  8. {
  9. node = db.createNode();
  10. node.createRelationshipTo( db.createNode(), MyRelTypes.TEST );
  11. node.createRelationshipTo( db.createNode(), MyRelTypes.TEST2 );
  12. assertEquals( 2, node.getDegree() );
  13. assertEquals( 1, node.getDegree( MyRelTypes.TEST ) );
  14. assertEquals( 1, node.getDegree( MyRelTypes.TEST2 ) );
  15. tx.success();
  16. }
  17. try ( Transaction tx = db.beginTx() )
  18. {
  19. node.createRelationshipTo( db.createNode(), MyRelTypes.TEST );
  20. tx.success();
  21. }
  22. db.shutdown();
  23. }

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

  1. private void add(Node node) {
  2. count++;
  3. int degree = node.getDegree();
  4. sumDegree += degree;
  5. if (degree > maxDegree) maxDegree = degree;
  6. if (degree < minDegree) minDegree = degree;
  7. for (Label label : node.getLabels()) labels.add(label.name());
  8. for (String key : node.getPropertyKeys()) properties.add(key);
  9. }
  10. Map<String,Object> toMap() {

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void shouldRollbackRelationshipOnFailure() throws Exception
  3. {
  4. long n1, n2;
  5. try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() )
  6. {
  7. n1 = graphDb.createNode().getId();
  8. n2 = graphDb.createNode().getId();
  9. tx.success();
  10. }
  11. try ( Transaction tx = beginTransaction() )
  12. {
  13. int label = tx.token().relationshipTypeGetOrCreateForName( "R" );
  14. tx.dataWrite().relationshipCreate( n1, label, n2 );
  15. tx.failure();
  16. }
  17. try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
  18. {
  19. assertEquals( 0, graphDb.getNodeById( n1 ).getDegree() );
  20. }
  21. }

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

  1. @UserFunction("apoc.node.degree.out")
  2. @Description("apoc.node.degree.out(node, relationshipName) - returns total number number of outgoing relationships")
  3. public long degreeOut(@Name("node") Node node, @Name(value = "types",defaultValue = "") String type) {
  4. if (type==null || type.isEmpty()) {
  5. return node.getDegree(Direction.OUTGOING);
  6. }
  7. return node.getDegree(RelationshipType.withName(type), Direction.OUTGOING);
  8. }

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void shouldDeleteRelationship() throws Exception
  3. {
  4. long n1, r;
  5. try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() )
  6. {
  7. Node node1 = graphDb.createNode();
  8. Node node2 = graphDb.createNode();
  9. n1 = node1.getId();
  10. r = node1.createRelationshipTo( node2, RelationshipType.withName( "R" ) ).getId();
  11. tx.success();
  12. }
  13. try ( Transaction tx = beginTransaction() )
  14. {
  15. assertTrue( "should delete relationship", tx.dataWrite().relationshipDelete( r ) );
  16. tx.success();
  17. }
  18. try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
  19. {
  20. assertEquals( 0, graphDb.getNodeById( n1 ).getDegree() );
  21. }
  22. }

代码示例来源:origin: neo4j/neo4j

  1. assertEquals( 1, otherNode.getDegree() );

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void shouldDeleteRelationshipAddedInTransaction() throws Exception
  3. {
  4. long n1, n2;
  5. try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() )
  6. {
  7. n1 = graphDb.createNode().getId();
  8. n2 = graphDb.createNode().getId();
  9. tx.success();
  10. }
  11. try ( Transaction tx = beginTransaction() )
  12. {
  13. int label = tx.token().relationshipTypeGetOrCreateForName( "R" );
  14. long r = tx.dataWrite().relationshipCreate( n1, label, n2 );
  15. assertTrue( tx.dataWrite().relationshipDelete( r ) );
  16. tx.success();
  17. }
  18. try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
  19. {
  20. assertEquals( 0, graphDb.getNodeById( n1 ).getDegree() );
  21. }
  22. }

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

  1. @UserFunction("apoc.node.degree")
  2. @Description("apoc.node.degree(node, rel-direction-pattern) - returns total degrees of the given relationships in the pattern, can use '>' or '<' for all outgoing or incoming relationships")
  3. public long degree(@Name("node") Node node, @Name(value = "types",defaultValue = "") String types) {
  4. if (types==null || types.isEmpty()) return node.getDegree();
  5. long degree = 0;
  6. for (Pair<RelationshipType, Direction> pair : parse(types)) {
  7. degree += getDegreeSafe(node, pair.first(), pair.other());
  8. }
  9. return degree;
  10. }

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

  1. private void sampleRels(int sampleSize, Sampler sampler, Label label, int count, Node node, RelationshipType type) {
  2. Direction direction = Direction.OUTGOING;
  3. int degree = node.getDegree(type, direction);
  4. sampler.sample(label,count,node,type,direction,degree,null);
  5. if (degree==0) return;
  6. ResourceIterator<Relationship> relIt = ((ResourceIterable<Relationship>)node.getRelationships(direction, type)).iterator();
  7. int relCount = 0;
  8. while (relIt.hasNext() && relCount++ < sampleSize) {
  9. sampler.sample(label,count,node,type,direction,degree,relIt.next());
  10. }
  11. relIt.close();
  12. }

相关文章