Thanks Peter. Really appreciate your help.

Yes, in somecases the path is exactly 4.
But in others, it may not be. I am wondering what would be the prune
criterion if the path is not exactly 4.
Say,

   REL1        REL2       REL3
A ---------> P ---------> Q -----------> T

And I would like to get A->P->Q->T returned as a path along the with
previous 2 paths.


On Mon, Feb 14, 2011 at 6:17 PM, Peter Neubauer <
peter.neuba...@neotechnology.com> wrote:

> Hi John,
> see the following example on something that should roughly work as you
> want. Notice that I only check for the existence of "relProp" on REL3,
> you can of course check for a value, but I didn't have a good example.
>
> This gives you the paths:
>
> (1)--[REL1,3]-->(2)--[REL2,4]-->(3)--[REL3,5]-->(4)--[REL4,6]-->(5)
> (1)--[REL1,7]-->(6)--[REL3,8]-->(7)--[REL4,10]-->(8)--[REL2,12]-->(9)
>
> Which is what you want - paths of length exactly 4, with unordered
> REL1, REL2, REL3, REL4 and a constraint on REL3?
>
> /peter
>
>
> package org.neo4j.examples.orderedpath;
>
> import static org.neo4j.graphdb.DynamicRelationshipType.withName;
>
> import java.util.ArrayList;
>
> import org.junit.BeforeClass;
> import org.junit.Test;
> 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.Transaction;
> import org.neo4j.graphdb.traversal.Evaluation;
> import org.neo4j.graphdb.traversal.Evaluator;
> import org.neo4j.graphdb.traversal.TraversalDescription;
> import org.neo4j.graphdb.traversal.Traverser;
> import org.neo4j.kernel.EmbeddedGraphDatabase;
> import org.neo4j.kernel.Traversal;
>
> public class NonOrderedPathTest
> {
>    private static EmbeddedGraphDatabase db;
>    private static RelationshipType REL1 = withName( "REL1" ),
>            REL2 = withName( "REL2" ), REL3 = withName( "REL3" ),
>            REL4 = withName( "REL4" ), REL8 = withName( "REL8" ),
>            REL9 = withName( "REL9" ), REL10 = withName( "REL10" );
>
>    @BeforeClass
>    public static void createTheGraph()
>    {
>        db = new EmbeddedGraphDatabase( "target/db" );
>        Transaction tx = db.beginTx();
>        // START SNIPPET: createGraph
>        Node A = db.createNode();
>        Node B = db.createNode();
>        Node C = db.createNode();
>        Node D = db.createNode();
>        Node E = db.createNode();
>        Node P = db.createNode();
>        Node Q = db.createNode();
>        Node R = db.createNode();
>        Node S = db.createNode();
>        Node T = db.createNode();
>        Node U = db.createNode();
>        Node X = db.createNode();
>        Node Y = db.createNode();
>        Node Z = db.createNode();
>        A.createRelationshipTo( X, REL1 );
>        X.createRelationshipTo( Y, REL2 );
>        Y.createRelationshipTo( Z, REL8 );
>        A.createRelationshipTo( B, REL1 );
>        B.createRelationshipTo( C, REL2 );
>        Relationship rel3 = C.createRelationshipTo( D, REL3 );
>        rel3.setProperty( "relProp", 123 );
>        D.createRelationshipTo( E, REL4 );
>        A.createRelationshipTo( P, REL1 );
>        Relationship rel33 = P.createRelationshipTo( Q, REL3 );
>        rel33.setProperty( "relProp", 123 );
>        Q.createRelationshipTo( R, REL9 );
>        Q.createRelationshipTo( R, REL4 );
>        R.createRelationshipTo( E, REL10 );
>        R.createRelationshipTo( S, REL2 );
>        S.createRelationshipTo( T, REL9 );
>        T.createRelationshipTo( U, REL8 );
>
>        tx.success();
>        tx.finish();
>    }
>
>    @Test
>    public void testPath()
>    {
>        TraversalDescription td = Traversal.description().expand(
>                Traversal.expanderForTypes( REL1, Direction.OUTGOING, REL2,
>                        Direction.OUTGOING, REL3, Direction.OUTGOING, REL4,
>                        Direction.OUTGOING ) ).evaluator( new Evaluator()
>        {
>
>            public Evaluation evaluate( Path path )
>            {
>                if ( path.length() == 0 )
>                {
>                    return Evaluation.EXCLUDE_AND_CONTINUE;
>                }
>                ArrayList<String> relTypes = new ArrayList<String>();
>                for(Relationship rel : path.relationships()) {
>                    String relname = rel.getType().name();
>                    if(relname.equals( REL3.name() )) {
>                        if(rel.hasProperty( "relProp" )) {
>                        } else {
>                            return Evaluation.EXCLUDE_AND_PRUNE;
>                        }
>                    }
>                    if(!relTypes.contains( relname )) {
>                        relTypes.add( relname );
>                    } else {
>                        return Evaluation.EXCLUDE_AND_PRUNE;
>                    }
>                }
>                return
>
> path.length()==4?Evaluation.INCLUDE_AND_CONTINUE:Evaluation.EXCLUDE_AND_CONTINUE;
>            }
>        } );
>        Traverser t = td.traverse( db.getNodeById( 1 ) );
>        for ( Path path : t )
>        {
>            System.out.println( path );
>        }
>
>    }
> }
>
>
> Cheers,
>
> /peter neubauer
>
> GTalk:      neubauer.peter
> Skype       peter.neubauer
> Phone       +46 704 106975
> LinkedIn   http://www.linkedin.com/in/neubauer
> Twitter      http://twitter.com/peterneubauer
>
> http://www.neo4j.org               - Your high performance graph database.
> http://www.thoughtmade.com - Scandinavia's coolest Bring-a-Thing party.
>
>
>
>  On Mon, Feb 14, 2011 at 6:34 PM, John Howard <johnyho...@gmail.com>
> wrote:
> > Thanks Peter & Craig.
> >
> > There are no such constraints, that is, relationship types can reoccur if
> a
> > path satisfies the input criteria.
> > For ex: If there was another path in our graph example, say,
> >       REL1        REL3         REL4       REL2      REL9       REL8
> >  A ---------> P ---------> Q -----------> R ---------> S
> > ---------->T----------->U
> >
> > we would like to see 2 paths, viz,  A ->B->C->D-> E and  A ->P->Q->R-> S
> >
> > I tried with the map suggestion, the prune logic doesn't seem to fit with
> > the existing evaluators.
> > Again, my understanding of the prune evaluators may be wrong.
> > Appreciate your help.
> >
> > Thank you.
> >
> >
> > On Mon, Feb 14, 2011 at 5:22 AM, Peter Neubauer <
> > peter.neuba...@neotechnology.com> wrote:
> >
> >> John,
> >> In your example, are there any constraints on that of the four
> >> relationship types, every one need to be exactly one time in the path,
> >> or is it just any of these?
> >>
> >> Instead of using a List like in the previous example, you can the use
> >> a Map to tick the visited relationship types and check the property
> >> when you get to REL3? I can do it for you in neo4j-examples again if
> >> you give me some more details :)
> >>
> >> Cheers,
> >>
> >> /peter neubauer
> >>
> >> GTalk:      neubauer.peter
> >> Skype       peter.neubauer
> >> Phone       +46 704 106975 <+46704106975>
>  >> LinkedIn   http://www.linkedin.com/in/neubauer
> >> Twitter      http://twitter.com/peterneubauer
> >>
> >> http://www.neo4j.org               - Your high performance graph
> database.
> >> http://www.thoughtmade.com - Scandinavia's coolest Bring-a-Thing party.
> >>
> >>
> >>
> >>  On Sun, Feb 13, 2011 at 10:16 AM, John Howard <johnyho...@gmail.com>
> >> wrote:
> >> > Thanks Peter. That was very useful.
> >> >
> >> > I am trying to get the path A ->B->C->D-> E in the below graph, with
> the
> >> > following inputs
> >> > Start Node: A,
> >> > Relationships: REL1, REL4, REL2 (that is, order is not known)
> >> > Relationship Property: relProp= "abc" in REL3
> >> >
> >> > I was not able to define an evaluator which takes the combination of
> >> > relationship types(without regard to order) and relationship
> properties
> >> to
> >> > return the desired path.
> >> > Can it be achievable in the current traversal framework?
> >> >
> >> >      REL1          REL2        REL8
> >> >  A ---------> X ---------> Y -----------> Z
> >> >
> >> >       REL1        REL2         REL3        REL4
> >> >  A ---------> B ---------> C -----------> D ---------> E
> >> >
> >> >       REL1        REL3         REL9       REL10
> >> >  A ---------> P ---------> Q -----------> R ---------> E
> >> >
> >> >
> >> > As always, appreciate your guidance.
> >> >
> >> >
> >> >
> >> > =======================================================
> >> > John,
> >> > if the order of the relationships doesn't matter, you could do
> >> > something like this with the Traversal API (holding an ordered Path to
> >> > test against in your Evaluator):
> >> >
> >> >    public void testPath()
> >> >    {
> >> >        final ArrayList<RelationshipType> orderedPath = new
> >> > ArrayList<RelationshipType>();
> >> >        orderedPath.add( withName( "REL1" ) );
> >> >        orderedPath.add( withName( "REL2" ) );
> >> >        orderedPath.add( withName( "REL3" ) );
> >> >        orderedPath.add( withName( "REL4" ) );
> >> >
> >> >        TraversalDescription td = Traversal.description().evaluator(
> >> >                new Evaluator()
> >> >                {
> >> >
> >> >                    public Evaluation evaluate( Path path )
> >> >                    {
> >> >                        if ( path.length() == 0 )
> >> >                        {
> >> >                            return Evaluation.EXCLUDE_AND_CONTINUE;
> >> >                        }
> >> >                        String  currentName=
> >> > path.lastRelationship().getType().name();
> >> >                        String relationshipType = orderedPath.get(
> >> > path.length() - 1 ).name();
> >> >                        if ( path.length() == orderedPath.size() )
> >> >                        {
> >> >                            if ( currentName.equals(
> >> >                                    relationshipType ) )
> >> >                            {
> >> >                                return Evaluation.INCLUDE_AND_PRUNE;
> >> >                            }
> >> >                            else
> >> >                            {
> >> >                                return Evaluation.EXCLUDE_AND_PRUNE;
> >> >                            }
> >> >                        }
> >> >                        else
> >> >                        {
> >> >                            if ( currentName.equals(
> >> >                                    relationshipType ) )
> >> >                            {
> >> >                                return Evaluation.EXCLUDE_AND_CONTINUE;
> >> >                            }
> >> >                            else
> >> >                            {
> >> >                                return Evaluation.EXCLUDE_AND_PRUNE;
> >> >                            }
> >> >                        }
> >> >                    }
> >> >                } );
> >> >        Traverser t = td.traverse( getNodeWithName( "A" ) );
> >> >        for ( Path path : t )
> >> >        {
> >> >            System.out.println(path);
> >> >        }
> >> >
> >> >    }
> >> >
> >> > I am attaching the classes for your reference, so you have full code.
> >> >
> >> > Also, you could try JRuby, Gremlin or other along the same lines. But
> >> > this is a basic Java example in creating an ordered Path information
> >> > and checking against it during the traversal. You can add in more
> >> > stuff of course ...
> >> >
> >> > Cheers,
> >> >
> >> > /peter neubauer
> >> >
> >> > GTalk:      neubauer.peter
> >> > Skype       peter.neubauer
> >> > Phone       +46 704 106975 <+46704106975>
>  >> > LinkedIn   http://www.linkedin.com/in/neubauer
> >> > Twitter      http://twitter.com/peterneubauer
> >> >
> >> > http://www.neo4j.org               - Your high performance graph
> >> database.
> >> > http://www.thoughtmade.com - Scandinavia's coolest Bring-a-Thing
> party.
> >> >
> >> >
> >> >
> >> > On Tue, Jan 25, 2011 at 10:59 PM, John Howard <johnyhowdy at
> gmail.com>
> >> > wrote:
> >> >> Hello,
> >> >>
> >> >> We are trying to use neo4j graph database in one of our applications.
> >> >> I think we hit a roadblock with traversal framework. It could be due
> to
> >> > our
> >> >> limited knowledge of neo4j framework.
> >> >>
> >> >> Here is what we are trying to accomplish:
> >> >> We need to get a path(from the graph below) from the nodes A to E
> >> through
> >> >> relations REL1, REL2, REL3 & REL4.
> >> >> All we know before the traversal is: Node A, REL1, REL2, REL3,
> >> >> REL4..(sometime we even know the end node E)
> >> >> So how can we get the path A to E? I can't seem to achieve it using
> >> >> evaluator and/or relationships in TraversalDescription. It always
> >> returns
> >> > me
> >> >> nodes X, P,Q since they have one my relationships(REL1, REL3).
> >> >>
> >> >>
> >> >>      REL1        REL2        REL8
> >> >>  A ---------> X ---------> Y -----------> Z
> >> >>
> >> >>       REL1      REL2        REL3        REL4
> >> >>  A ---------> B ---------> C -----------> D ---------> E
> >> >>
> >> >>       REL1       REL3         REL9       REL10
> >> >>  A ---------> P ---------> Q -----------> R ---------> E
> >> >>
> >> >>
> >> >> Nodes: A, B, C, D,E,X,Y,Z,P,Q,R,
> >> >> Relations: REL1....REL10
> >> >>
> >> >>
> >> >> Thank you for your help.
> >> >>
> >> >> John
> >> >> _______________________________________________
> >> >> Neo4j mailing list
> >> >> User at lists.neo4j.org
> >> >> https://lists.neo4j.org/mailman/listinfo/user
> >> >>
> >> > -------------- next part --------------
> >> > A non-text attachment was scrubbed...
> >> > Name: gist.zip
> >> > Type: application/zip
> >> > Size: 5718 bytes
> >> > Desc: not available
> >> > Url :
> >> >
> >>
> http://lists.neo4j.org/pipermail/user/attachments/20110126/f2e3cac5/attachment-0001.zip
> >> > _______________________________________________
> >> > Neo4j mailing list
> >> > User@lists.neo4j.org
> >> > https://lists.neo4j.org/mailman/listinfo/user
> >> >
> >> _______________________________________________
> >> Neo4j mailing list
> >> User@lists.neo4j.org
> >>  https://lists.neo4j.org/mailman/listinfo/user
> >>
> > _______________________________________________
> > Neo4j mailing list
> > User@lists.neo4j.org
> > https://lists.neo4j.org/mailman/listinfo/user
> >
> _______________________________________________
> Neo4j mailing list
> User@lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
>
_______________________________________________
Neo4j mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user

Reply via email to