1818import org .neo4j .graphdb .Node ;
1919import org .neo4j .graphdb .Path ;
2020import org .neo4j .graphdb .PathExpanders ;
21+ import org .neo4j .graphdb .ResourceIterable ;
2122import org .neo4j .graphdb .traversal .Evaluation ;
22- import org .neo4j .graphdb .traversal .TraversalDescription ;
2323import org .neo4j .helpers .collection .IteratorUtil ;
2424
2525import java .util .ArrayList ;
@@ -63,7 +63,9 @@ public AllClustersQuery(int minRank, int maxRank, int threshold,
6363 this .is = is ;
6464 }
6565
66- private TraversalDescription untilMaxRank (GraphDatabaseService service ) {
66+ private ResourceIterable <Node > untilMaxRank (GraphDatabaseService service ) {
67+ Iterable <Node > start = IteratorUtil .loop (service .findNodes (NodeLabels .NODE ,
68+ SequenceProperties .RANK .name (), minRank ));
6769 return service .traversalDescription ()
6870 .breadthFirst ()
6971 .evaluator (path -> {
@@ -74,23 +76,28 @@ private TraversalDescription untilMaxRank(GraphDatabaseService service) {
7476 return Evaluation .EXCLUDE_AND_PRUNE ;
7577 }
7678 })
77- .relationships (RelTypes .NEXT , Direction .OUTGOING );
79+ .relationships (RelTypes .NEXT , Direction .OUTGOING )
80+ .traverse (start ).nodes ();
7881 }
7982
8083 @ Override
8184 public Map <Integer , List <Cluster >> execute (GraphDatabaseService service ) {
85+ Map <Integer , List <Cluster >> individualNodes = new HashMap <>();
8286 Set <Long > bubbleSourcesNested = new HashSet <>();
8387 Set <Long > bubbleSourcesToCluster = new HashSet <>();
8488 Set <Long > bubbleSourcesToKeepIntact = new HashSet <>();
85- Iterable <Node > start = IteratorUtil .loop (service .findNodes (NodeLabels .NODE ,
86- SequenceProperties .RANK .name (), minRank ));
87- for (Node n : untilMaxRank (service ).traverse (start ).nodes ()) {
89+ for (Node n : untilMaxRank (service )) {
8890 if (n .hasLabel (NodeLabels .BUBBLE_SOURCE )) {
8991 bubbleSourcesToCluster .add (n .getId ());
90- if (((long []) n .getProperty (BubbleProperties .BUBBLE_SOURCE_IDS .name ()))
91- .length > 0 ) {
92+ if (getBubbleIDs (n ).length > 0 ) {
9293 bubbleSourcesNested .add (n .getId ());
9394 }
95+ } else {
96+ if (getBubbleIDs (n ).length == 0 ) {
97+ Cluster individualNode = createSingletonCluster (service , n );
98+ individualNodes .put (individualNode .getStartRank (),
99+ Collections .singletonList (individualNode ));
100+ }
94101 }
95102 int interestingness = is .compute (new Neo4jScoreContainer (n ));
96103 n .setProperty (SequenceProperties .INTERESTINGNESS .name (), interestingness );
@@ -103,7 +110,12 @@ public Map<Integer, List<Cluster>> execute(GraphDatabaseService service) {
103110 }
104111 }
105112 bubbleSourcesToCluster .removeAll (bubbleSourcesNested );
106- return cluster (service , bubbleSourcesToCluster , bubbleSourcesToKeepIntact );
113+ return mergeMaps (Stream .of (individualNodes ,
114+ cluster (service , bubbleSourcesToCluster , bubbleSourcesToKeepIntact )));
115+ }
116+
117+ private long [] getBubbleIDs (Node n ) {
118+ return (long []) n .getProperty (BubbleProperties .BUBBLE_SOURCE_IDS .name ());
107119 }
108120
109121 private Map <Integer , List <Cluster >> cluster (GraphDatabaseService service ,
0 commit comments