1   /**
2    * Licensed to Neo Technology under one or more contributor
3    * license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright
5    * ownership. Neo Technology licenses this file to you under
6    * the Apache License, Version 2.0 (the "License"); you may
7    * not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.neo4j.examples.orderedpath;
20  
21  import static org.neo4j.graphdb.DynamicRelationshipType.withName;
22  
23  import java.util.ArrayList;
24  
25  import org.junit.BeforeClass;
26  import org.junit.Test;
27  import org.neo4j.graphdb.Node;
28  import org.neo4j.graphdb.Path;
29  import org.neo4j.graphdb.RelationshipType;
30  import org.neo4j.graphdb.Transaction;
31  import org.neo4j.graphdb.traversal.Evaluation;
32  import org.neo4j.graphdb.traversal.Evaluator;
33  import org.neo4j.graphdb.traversal.TraversalDescription;
34  import org.neo4j.graphdb.traversal.Traverser;
35  import org.neo4j.kernel.EmbeddedGraphDatabase;
36  import org.neo4j.kernel.Traversal;
37  
38  public class OrderedPathTest
39  {
40      private static EmbeddedGraphDatabase db;
41      private static RelationshipType REL1 = withName( "REL1" ),
42              REL2 = withName( "REL2" ), REL3 = withName( "REL3" );
43  
44      @BeforeClass
45      public static void createTheGraph()
46      {
47          db = new EmbeddedGraphDatabase( "target/db" );
48          Transaction tx = db.beginTx();
49          // START SNIPPET: createGraph
50          Node A = db.createNode();
51          Node B = db.createNode();
52          Node C = db.createNode();
53          Node D = db.createNode();
54          A.createRelationshipTo( B, REL1 );
55          B.createRelationshipTo( C, REL2 );
56          C.createRelationshipTo( D, REL3 );
57          A.createRelationshipTo( C, REL2 );
58  
59          // END SNIPPET: createGraph
60          tx.success();
61          tx.finish();
62      }
63  
64      @Test
65      public void testPath()
66      {
67          // START SNIPPET: walkOrderedPath
68          final ArrayList<RelationshipType> orderedPathContext = new ArrayList<RelationshipType>();
69          orderedPathContext.add( REL1 );
70          orderedPathContext.add( withName( "REL2" ) );
71          orderedPathContext.add( withName( "REL3" ) );
72          TraversalDescription td = Traversal.description().evaluator(
73                  new Evaluator()
74                  {
75  
76                      public Evaluation evaluate( Path path )
77                      {
78                          if ( path.length() == 0 )
79                          {
80                              return Evaluation.EXCLUDE_AND_CONTINUE;
81                          }
82                          String currentName = path.lastRelationship().getType().name();
83                          String relationshipType = orderedPathContext.get(
84                                  path.length() - 1 ).name();
85                          if ( path.length() == orderedPathContext.size() )
86                          {
87                              if ( currentName.equals( relationshipType ) )
88                              {
89                                  return Evaluation.INCLUDE_AND_PRUNE;
90                              }
91                              else
92                              {
93                                  return Evaluation.EXCLUDE_AND_PRUNE;
94                              }
95                          }
96                          else
97                          {
98                              if ( currentName.equals( relationshipType ) )
99                              {
100                                 return Evaluation.EXCLUDE_AND_CONTINUE;
101                             }
102                             else
103                             {
104                                 return Evaluation.EXCLUDE_AND_PRUNE;
105                             }
106                         }
107                     }
108                 } );
109         Traverser t = td.traverse( db.getNodeById( 1 ) );
110         for ( Path path : t )
111         {
112             System.out.println( path );
113         }
114         // END SNIPPET: walkOrderedPath
115     }
116 }