-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrueBNodesCounter.java
127 lines (109 loc) · 4.27 KB
/
TrueBNodesCounter.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
/*
* Copyright 2016 Frank Pavageau
*
* Licensed 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.
*/
package com.ekino.neo4j.traversal;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.PathExpander;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.traversal.BranchState;
import org.neo4j.graphdb.traversal.Evaluation;
import org.neo4j.graphdb.traversal.Evaluator;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.graphdb.traversal.Uniqueness;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
class TrueBNodesCounter {
private static final Logger LOGGER = LoggerFactory.getLogger(TrueBNodesCounter.class);
private final GraphDatabaseService graphDb;
private final Neo4jOperations neo4jOperations;
public TrueBNodesCounter(GraphDatabaseService graphDb, Neo4jOperations neo4jOperations) {
this.graphDb = graphDb;
this.neo4jOperations = neo4jOperations;
}
public int count(boolean depthFirst) {
try (ResourceIterator<Node> roots = graphDb.findNodes(Labels.Root)) {
if (roots.hasNext()) {
return count(roots.next(), depthFirst);
}
}
return -1;
}
private int count(Node root, boolean depthFirst) {
LOGGER.info("Traversing the whole tree ({})", depthFirst ? "depth-first" : "breadth-first");
TraversalDescription td = graphDb.traversalDescription()
.uniqueness(Uniqueness.NONE)
.evaluator(new TrueBEvaluator(neo4jOperations))
.expand(new CustomPathExpander(neo4jOperations));
if (depthFirst) {
td = td.depthFirst();
} else {
td = td.breadthFirst();
}
int count = 0;
for (Path ignored : td.traverse(root)) {
count++;
}
return count;
}
private static class TrueBEvaluator implements Evaluator {
private final Neo4jOperations neo4jOperations;
public TrueBEvaluator(Neo4jOperations neo4jOperations) {
this.neo4jOperations = neo4jOperations;
}
@Override
public Evaluation evaluate(Path path) {
Node endNode = path.endNode();
return Evaluation.ofIncludes(includes(endNode));
}
private boolean includes(Node endNode) {
String propName = "value";
return neo4jOperations.hasLabel(endNode, Labels.B) &&
(Boolean) neo4jOperations.getProperty(endNode, propName);
}
@Override
public String toString() {
return "TrueBEvaluator()";
}
}
private class CustomPathExpander implements PathExpander<Object> {
private final Neo4jOperations neo4jOperations;
public CustomPathExpander(Neo4jOperations neo4jOperations) {
this.neo4jOperations = neo4jOperations;
}
@Override
public Iterable<Relationship> expand(Path path, BranchState<Object> state) {
Node endNode = path.endNode();
if (neo4jOperations.hasLabel(endNode, Labels.A)) {
return endNode.getRelationships(Direction.OUTGOING, RelationshipTypes.HAS_B);
} else if (neo4jOperations.hasLabel(endNode, Labels.B)) {
return endNode.getRelationships(Direction.OUTGOING, RelationshipTypes.HAS_A);
}
return Collections.emptyList();
}
@Override
public PathExpander<Object> reverse() {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return "CustomPathExpander()";
}
}
}