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

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

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

Node.getSingleRelationship介绍

[英]Returns the only relationship of a given type and direction that is attached to this node, or null. This is a convenience method that is used in the commonly occurring situation where a node has exactly zero or one relationships of a given type and direction to another node. Typically this invariant is maintained by the rest of the code: if at any time more than one such relationships exist, it is a fatal error that should generate an unchecked exception. This method reflects that semantics and returns either:

  1. null if there are zero relationships of the given type and direction,
  2. the relationship if there's exactly one, or
  3. throws an unchecked exception in all other cases.

This method should be used only in situations with an invariant as described above. In those situations, a "state-checking" method (e.g. hasSingleRelationship(...)) is not required, because this method behaves correctly "out of the box."
[中]返回附加到此节点的唯一给定类型和方向的关系,或[$0$]。这是一种方便的方法,用于一个节点与另一个节点的给定类型和方向的关系为零或一的常见情况。通常,此不变量由代码的其余部分维护:如果在任何时候存在多个这样的关系,则这是一个致命错误,应生成未检查的异常。此方法反映了该语义,并返回:
1.null如果给定类型和方向的关系为零,
1.关系(如果只有一个),或
1.在所有其他情况下引发未经检查的异常。
此方法仅适用于具有上述不变量的情况。在这些情况下,不需要“状态检查”方法(例如:hasSingleRelationship(...)),因为该方法“开箱即用”的行为是正确的

代码示例

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

@Test
public void getSingleRelationshipOnNodeWithOneLoopOnly()
{
  Node node = getGraphDb().createNode();
  Relationship singleRelationship = node.createRelationshipTo( node, TEST );
  assertEquals( singleRelationship, node.getSingleRelationship( TEST, Direction.OUTGOING ) );
  assertEquals( singleRelationship, node.getSingleRelationship( TEST, Direction.INCOMING ) );
  assertEquals( singleRelationship, node.getSingleRelationship( TEST, Direction.BOTH ) );
  commit();
  newTransaction();
  assertEquals( singleRelationship, node.getSingleRelationship( TEST, Direction.OUTGOING ) );
  assertEquals( singleRelationship, node.getSingleRelationship( TEST, Direction.INCOMING ) );
  assertEquals( singleRelationship, node.getSingleRelationship( TEST, Direction.BOTH ) );
  finish();
}

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

@Test
@Graph( { "a TO b", "b TO c", "c TO a" } )
public void canCreateGraphFromMultipleStrings()
{
  Map<String,Node> graph = data.get();
  Set<Node> unique = new HashSet<>();
  Node n = graph.get( "a" );
  while ( unique.add( n ) )
  {
    try ( Transaction ignored = graphdb.beginTx() )
    {
      n = n.getSingleRelationship( RelationshipType.withName( "TO" ), Direction.OUTGOING ).getEndNode();
    }
  }
  assertEquals( graph.size(), unique.size() );
}

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

@Test
public void testGetDirectedRelationship()
{
  Node node1 = getGraphDb().getNodeById( node1Id );
  Relationship rel = node1.getSingleRelationship( MyRelTypes.TEST,
    Direction.OUTGOING );
  assertEquals( int1, rel.getProperty( key1 ) );
}

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

@After
public void deleteTestingGraph()
{
  Node node1 = getGraphDb().getNodeById( node1Id );
  Node node2 = getGraphDb().getNodeById( node2Id );
  node1.getSingleRelationship( MyRelTypes.TEST, Direction.BOTH ).delete();
  node1.delete();
  node2.delete();
}

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

@Test
@Graph( "I know you" )
public void get_Relationship_by_ID() throws JsonParseException
{
  Node node = data.get().get( "I" );
  Relationship relationship;
  try ( Transaction transaction = node.getGraphDatabase().beginTx() )
  {
    relationship = node.getSingleRelationship(
        RelationshipType.withName( "know" ),
        Direction.OUTGOING );
  }
  String response = gen().expectedStatus(
      com.sun.jersey.api.client.ClientResponse.Status.OK.getStatusCode() ).get(
      getRelationshipUri( relationship ) ).entity();
  assertTrue( JsonHelper.jsonToMap( response ).containsKey( "start" ) );
}

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

@Test
public void getNewlyCreatedLoopRelationshipFromCache()
{
  Node node = getGraphDb().createNode();
  node.createRelationshipTo( getGraphDb().createNode(), TEST );
  newTransaction();
  Relationship relationship = node.createRelationshipTo( node, TEST );
  newTransaction();
  assertEquals( relationship, node.getSingleRelationship( TEST, Direction.INCOMING ) );
}

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

@Test
public void getOrCreateRelationshipWithUniqueFactory()
{
  final String key = "name";
  final String value = "Mattias";
  final Node root = graphDb.createNode();
  final Index<Relationship> index = relationshipIndex( LuceneIndexImplementation.EXACT_CONFIG );
  final RelationshipType type = withName( "SINGLE" );
  UniqueFactory<Relationship> factory = new UniqueFactory.UniqueRelationshipFactory( index )
  {
    @Override
    protected Relationship create( Map<String, Object> properties )
    {
      assertEquals( value, properties.get( key ) );
      assertEquals( 1, properties.size() );
      return root.createRelationshipTo( graphDatabase().createNode(), type );
    }
  };
  Relationship unique = factory.getOrCreate( key, value );
  assertEquals( unique, root.getSingleRelationship( type, Direction.BOTH ) );
  assertNotNull( unique );
  assertEquals( unique, index.get( key, value ).getSingle() );
  assertEquals( unique, factory.getOrCreate( key, value ) );
  assertEquals( unique, root.getSingleRelationship( type, Direction.BOTH ) );
  assertEquals( unique, index.get( key, value ).getSingle() );
  finishTx( false );
}

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

@Test
public void testNodeChangeProperty()
{
  Node node1 = getGraphDb().getNodeById( node1Id );
  Node node2 = getGraphDb().getNodeById( node2Id );
  Relationship rel = node1.getSingleRelationship( MyRelTypes.TEST,
    Direction.BOTH );
  // test change property
  node1.setProperty( key1, int2 );
  node2.setProperty( key1, int1 );
  rel.setProperty( key1, int2 );
  int[] newIntArray = new int[] { 3, 2, 1 };
  node1.setProperty( arrayKey, newIntArray );
  node2.setProperty( arrayKey, newIntArray );
  rel.setProperty( arrayKey, newIntArray );
}

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

@Test
public void testDirectedRelationship1()
{
  Node node1 = getGraphDb().getNodeById( node1Id );
  Relationship rel = node1.getSingleRelationship( MyRelTypes.TEST, Direction.BOTH );
  Node[] nodes = rel.getNodes();
  assertEquals( 2, nodes.length );
  Node node2 = getGraphDb().getNodeById( node2Id );
  assertTrue( nodes[0].equals( node1 ) && nodes[1].equals( node2 ) );
  assertEquals( node1, rel.getStartNode() );
  assertEquals( node2, rel.getEndNode() );
  Relationship[] relArray = getRelationshipArray( node1.getRelationships( MyRelTypes.TEST, Direction.OUTGOING ) );
  assertEquals( 1, relArray.length );
  assertEquals( rel, relArray[0] );
  relArray = getRelationshipArray( node2.getRelationships(
    MyRelTypes.TEST, Direction.INCOMING ) );
  assertEquals( 1, relArray.length );
  assertEquals( rel, relArray[0] );
}

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

@Test
public void testNodeRemoveProperty()
{
  Node node1 = getGraphDb().getNodeById( node1Id );
  Node node2 = getGraphDb().getNodeById( node2Id );
  Relationship rel = node1.getSingleRelationship( MyRelTypes.TEST,
    Direction.BOTH );
  // test remove property
  assertEquals( 1, node1.removeProperty( key1 ) );
  assertEquals( 2, node2.removeProperty( key1 ) );
  assertEquals( 1, rel.removeProperty( key1 ) );
  assertEquals( string1, node1.removeProperty( key2 ) );
  assertEquals( string2, node2.removeProperty( key2 ) );
  assertEquals( string1, rel.removeProperty( key2 ) );
  assertNotNull( node1.removeProperty( arrayKey ) );
  assertNotNull( node2.removeProperty( arrayKey ) );
  assertNotNull( rel.removeProperty( arrayKey ) );
}

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

assertEquals( highMark, relAboveTheLine.getId() );
assertEquals( relBelowTheLine,
    db.getNodeById( idBelow ).getSingleRelationship( this, Direction.OUTGOING ) );
assertEquals( relAboveTheLine,
    db.getNodeById( idBelow ).getSingleRelationship( this, Direction.INCOMING ) );
assertEquals( idBelow, relBelowTheLine.getId() );
assertEquals( highMark, relAboveTheLine.getId() );

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

@Test
public void ensureCorrectPathEntitiesInShortPath()
{
  /*
   * (a)-->(b)
   */
  createGraph( "a TO b" );
  Node a = getNodeWithName( "a" );
  Node b = getNodeWithName( "b" );
  Relationship r = a.getSingleRelationship( to, OUTGOING );
  Path path = Iterables.single( getGraphDb().bidirectionalTraversalDescription()
    .mirroredSides( getGraphDb().traversalDescription().relationships( to, OUTGOING ).uniqueness( NODE_PATH ) )
    .collisionEvaluator( Evaluators.atDepth( 1 ) )
    .sideSelector( SideSelectorPolicies.LEVEL, 1 )
    .traverse( a, b ) );
  assertContainsInOrder( path.nodes(), a, b );
  assertContainsInOrder( path.reverseNodes(), b, a );
  assertContainsInOrder( path.relationships(), r );
  assertContainsInOrder( path.reverseRelationships(), r );
  assertContainsInOrder( path, a, r, b );
  assertEquals( a, path.startNode() );
  assertEquals( b, path.endNode() );
  assertEquals( r, path.lastRelationship() );
}

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

assertProperties( properties, node );
assertProperties( properties, rel );
Node highNode = node.getSingleRelationship( OTHER_TYPE, Direction.OUTGOING ).getEndNode();
assertProperties( properties, highNode );
verified++;

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

@Test
public void testAddProperty()
{
  String key3 = "key3";
  Node node1 = getGraphDb().getNodeById( node1Id );
  Node node2 = getGraphDb().getNodeById( node2Id );
  Relationship rel = node1.getSingleRelationship( MyRelTypes.TEST,
    Direction.BOTH );
  // add new property
  node2.setProperty( key3, int1 );
  rel.setProperty( key3, int2 );
  assertTrue( node1.hasProperty( key1 ) );
  assertTrue( node2.hasProperty( key1 ) );
  assertTrue( node1.hasProperty( key2 ) );
  assertTrue( node2.hasProperty( key2 ) );
  assertTrue( node1.hasProperty( arrayKey ) );
  assertTrue( node2.hasProperty( arrayKey ) );
  assertTrue( rel.hasProperty( arrayKey ) );
  assertTrue( !node1.hasProperty( key3 ) );
  assertTrue( node2.hasProperty( key3 ) );
  assertEquals( int1, node1.getProperty( key1 ) );
  assertEquals( int2, node2.getProperty( key1 ) );
  assertEquals( string1, node1.getProperty( key2 ) );
  assertEquals( string2, node2.getProperty( key2 ) );
  assertEquals( int1, rel.getProperty( key1 ) );
  assertEquals( string1, rel.getProperty( key2 ) );
  assertEquals( int2, rel.getProperty( key3 ) );
}

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

Relationship realRelationship = db.getRelationshipById( relationship );
assertEquals( realSelfRelationship,
    realStartNode.getSingleRelationship( RelTypes.REL_TYPE1, Direction.INCOMING ) );
assertEquals( asSet( realSelfRelationship, realRelationship ),
    Iterables.asSet( realStartNode.getRelationships( Direction.OUTGOING ) ) );

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

assertEquals( node1.getProperty( "test" ), 1 );
assertEquals( rel.getProperty( "test" ), 11 );
assertEquals( rel, node1.getSingleRelationship( MyRelTypes.TEST,
    Direction.OUTGOING ) );
node1.delete();

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

Relationship loadedRel = node1.getSingleRelationship( withName( "TEST" ), Direction.OUTGOING );
assertEquals( rel, loadedRel );
assertThat(loadedRel, inTx(db, hasProperty( "key1" ).withValue( "value1" )));

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

@Test
public void testExtractNode() throws Exception {
  Long id = db.execute("CREATE (f:Foo)-[rel:FOOBAR {a:1}]->(b:Bar) RETURN id(rel) as id").<Long>columnAs("id").next();
  testCall(db, "CALL apoc.refactor.extractNode({ids},['FooBar'],'FOO','BAR')", map("ids", singletonList(id)),
      (r) -> {
        assertEquals(id, r.get("input"));
        Node node = (Node) r.get("output");
        assertEquals(true, node.hasLabel(Label.label("FooBar")));
        assertEquals(1L, node.getProperty("a"));
        assertNotNull(node.getSingleRelationship(RelationshipType.withName("FOO"), Direction.OUTGOING));
        assertNotNull(node.getSingleRelationship(RelationshipType.withName("BAR"), Direction.INCOMING));
      });
}
@Test

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

@Test
public void testMergeNodesWithSelfRelationships() throws Exception {
  Map<String, Object> result = Iterators.single(db.execute("CREATE \n" +
      "(alice:Person {name:'Alice'}),\n" +
      "(bob:Person {name:'Bob'}),\n" +
      "(bob)-[:likes]->(bob) RETURN id(alice) AS aliceId, id(bob) AS bobId"));
  // Merge (bob) into (alice).
  // The updated node should have one self relationship.
  // NB: the "LIMIT 1" here is important otherwise Cypher tries to check if another MATCH is found, causing a failing read attempt to deleted node
  testCall(db,
      "MATCH (alice:Person {name:'Alice'}), (bob:Person {name:'Bob'}) WITH * LIMIT 1 CALL apoc.refactor.mergeNodes([alice, bob]) yield node return node",
      (r)-> {
        Node node = (Node) r.get("node");
        assertEquals(result.get("aliceId"), node.getId());
        assertEquals("Bob", node.getProperty("name"));
        assertEquals(1, node.getDegree(Direction.INCOMING));
        assertEquals(1, node.getDegree(Direction.OUTGOING));
        assertTrue(node.getSingleRelationship(RelationshipType.withName("likes"), Direction.OUTGOING).getEndNode().equals(node));
      });
}

代码示例来源:origin: org.neo4j/neo4j-graph-collections

/**
 * Deletes this sorted tree.
 */
public void delete()
{
  acquireLock();
  Relationship rel = treeRoot.getUnderlyingNode().getSingleRelationship(
    RelTypes.TREE_ROOT, Direction.INCOMING );
  treeRoot.delete();
  rel.delete();
}

相关文章