-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TraversalExample.java
134 lines (125 loc) · 4 KB
/
TraversalExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Licensed to Neo Technology under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Neo Technology licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
// START SNIPPET: sampleDocumentation
// START SNIPPET: _sampleDocumentation
package org.neo4j.examples;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.traversal.Evaluators;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.kernel.Traversal;
import org.neo4j.kernel.Uniqueness;
public class TraversalExample
{
public String knowsLikesTraverser( Node node )
{
String output = "";
// START SNIPPET: knowslikestraverser
for ( Path position : Traversal.description()
.depthFirst()
.relationships( Rels.KNOWS )
.relationships( Rels.LIKES, Direction.INCOMING )
.evaluator( Evaluators.toDepth( 5 ) )
.traverse( node ) )
{
output += position + "\n";
}
// END SNIPPET: knowslikestraverser
return output;
}
// START SNIPPET: basetraverser
final TraversalDescription FRIENDS_TRAVERSAL = Traversal.description()
.depthFirst()
.relationships( Rels.KNOWS )
.uniqueness( Uniqueness.RELATIONSHIP_GLOBAL );
// END SNIPPET: basetraverser
public String traverseBaseTraverser( Node node )
{
String output = "";
// START SNIPPET: traversebasetraverser
for ( Path path : FRIENDS_TRAVERSAL.traverse( node ) )
{
output += path + "\n";
}
// END SNIPPET: traversebasetraverser
return output;
}
public String depth3( Node node )
{
String output = "";
// START SNIPPET: depth3
for ( Path path : FRIENDS_TRAVERSAL
.evaluator( Evaluators.toDepth( 3 ) )
.traverse( node ) )
{
output += path + "\n";
}
// END SNIPPET: depth3
return output;
}
public String depth4( Node node )
{
String output = "";
// START SNIPPET: depth4
for ( Path path : FRIENDS_TRAVERSAL
.evaluator( Evaluators.fromDepth( 2 ) )
.evaluator( Evaluators.toDepth( 4 ) )
.traverse( node ) )
{
output += path + "\n";
}
// END SNIPPET: depth4
return output;
}
public String nodes( Node node )
{
String output = "";
// START SNIPPET: nodes
for ( Node currentNode : FRIENDS_TRAVERSAL
.traverse( node )
.nodes() )
{
output += currentNode.getProperty( "name" ) + "\n";
}
// END SNIPPET: nodes
return output;
}
public String relationships( Node node )
{
String output = "";
// START SNIPPET: relationships
for ( Relationship relationship : FRIENDS_TRAVERSAL
.traverse( node )
.relationships() )
{
output += relationship.getType() + "\n";
}
// END SNIPPET: relationships
return output;
}
// START SNIPPET: sourceRels
private enum Rels implements RelationshipType
{
LIKES, KNOWS
}
// END SNIPPET: sourceRels
}