Re: [Neo4j] Question from Webinar - traversing a path with nodes of different types

2011-04-22 Thread David Montag
Hi Vipul,

Out of curiosity, what does process in this context mean?

As Rick alludes to, you'd have some component performing the simulation
using the domain objects and possibly a graph traversal.

An example of an algorithm for this would be to walk the graph from 1, and
whenever you find a branch, you split the walk. When you finish walking a
branch (a point where more than one branch joins) you use some kind of
synchronization to join the walks.

Does this make sense?

David

On Wed, Apr 20, 2011 at 11:16 PM, Vipul Gupta vipulgupta...@gmail.comwrote:

 Hi David,

 Inputs are 1 and 6 + Graph is acyclic.

 domain.Client@1 - domain.Router@2 - domain.Router@3 - domain.Router@5- 
 domain.Server@6
   - domain.Router@7 - domain.Router@8 -

 I want a way to start from 1,

 process the 2 path till it reaches 5 (say in a thread)
 process the 7 path till it reaches 5 (in another thread)

  then process 5 and eventually 6.
 the above step of processing intermediate path and waiting on the blocking
 point can happen over and over again in a more complex graph (that is there
 could be a number of loops in between even) and the traversal stops only we
 reach 6

 I hope this makes it a bit clear. I was working out something for this, but
 it is turning out to be too complex a solution for this sort of traversal of
 a graph, so I am hoping if you can suggest something.

 Best Regards,
 Vipul


 On Thu, Apr 21, 2011 at 11:36 AM, David Montag 
 david.mon...@neotechnology.com wrote:

 Hi Vipul,

 Zooming out a little bit, what are the inputs to your algorithm, and what
 do you want it to do?

 For example, given 1 and 6, do you want to find any points in the chain
 between them that are join points of two (or more) subchains (5 in this
 case)?

 David


 On Wed, Apr 20, 2011 at 10:56 PM, Vipul Gupta vipulgupta...@gmail.comwrote:

 my mistake - I meant 5 depends on both 3 and 8 and acts as a blocking
 point till 3 and 8 finishes


 On Thu, Apr 21, 2011 at 11:19 AM, Vipul Gupta 
 vipulgupta...@gmail.comwrote:

 David/Michael,

 Let me modify the example a bit.
 What if my graph structure is like this

 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Server@6
   - domain.Router@7 - domain.Router@8 -


 Imagine a manufacturing line.
 6 depends on both 3 and 8 and acts as a blocking point till 3 and 8
 finishes.

 Is there a way to get a cleaner traversal for such kind of
 relationship. I want to get a complete intermediate traversal from
 Client to Server.

 Thank a lot for helping out on this.

 Best Regards,
 Vipul




 On Thu, Apr 21, 2011 at 12:09 AM, David Montag 
 david.mon...@neotechnology.com wrote:

 Hi Vipul,

 Thanks for listening!

 It's a very good question, and the short answer is: yes! I'm cc'ing our
 mailing list so that everyone can take part in the answer.

 Here's the long answer, illustrated by an example:

 Let's assume you're modeling a network. You'll have some domain classes
 that are all networked entities with peers:

 @NodeEntity
 public class NetworkEntity {
 @RelatedTo(type = PEER, direction = Direction.BOTH, elementClass
 = NetworkEntity.class)
 private SetNetworkEntity peers;

 public void addPeer(NetworkEntity peer) {
 peers.add(peer);
 }
 }

 public class Server extends NetworkEntity {}
 public class Router extends NetworkEntity {}
 public class Client extends NetworkEntity {}

 Then we can build a small network:

 Client c = new Client().persist();
 Router r1 = new Router().persist();
 Router r21 = new Router().persist();
 Router r22 = new Router().persist();
 Router r3 = new Router().persist();
 Server s = new Server().persist();

 c.addPeer(r1);
 r1.addPeer(r21);
 r1.addPeer(r22);
 r21.addPeer(r3);
 r22.addPeer(r3);
 r3.addPeer(s);

 c.persist();

 Note that after linking the entities, I only call persist() on the
 client. You can read more about this in the reference documentation, but
 essentially it will cascade in the direction of the relationships created,
 and will in this case cascade all the way to the server entity.

 You can now query this:

 IterableEntityPathClient, Server paths =
 c.findAllPathsByTraversal(Traversal.description());

 The above code will get you an EntityPath per node visited during the
 traversal from c. The example does however not use a very interesting
 traversal description, but you can still print the results:

 for (EntityPathClient, Server path : paths) {
 StringBuilder sb = new StringBuilder();
 IteratorNetworkEntity iter =
 path.NetworkEntitynodeEntities().iterator();
 while (iter.hasNext()) {
 sb.append(iter.next());
 if (iter.hasNext()) sb.append( - );
 }
 System.out.println(sb);
 }

 This will print each path, with all entities in the path. This is what
 it looks like:

 domain.Client@1
 domain.Client@1 - domain.Router@2
 domain.Client@1 - domain.Router@2 - domain.Router@3
 domain.Client@1 - 

Re: [Neo4j] Question from Webinar - traversing a path with nodes of different types

2011-04-21 Thread David Montag
Hi Vipul,

Zooming out a little bit, what are the inputs to your algorithm, and what do
you want it to do?

For example, given 1 and 6, do you want to find any points in the chain
between them that are join points of two (or more) subchains (5 in this
case)?

David

On Wed, Apr 20, 2011 at 10:56 PM, Vipul Gupta vipulgupta...@gmail.comwrote:

 my mistake - I meant 5 depends on both 3 and 8 and acts as a blocking
 point till 3 and 8 finishes


 On Thu, Apr 21, 2011 at 11:19 AM, Vipul Gupta vipulgupta...@gmail.comwrote:

 David/Michael,

 Let me modify the example a bit.
 What if my graph structure is like this

 domain.Client@1 - domain.Router@2 - domain.Router@3 - domain.Router@5- 
 domain.Server@6
   - domain.Router@7 - domain.Router@8 -


 Imagine a manufacturing line.
 6 depends on both 3 and 8 and acts as a blocking point till 3 and 8
 finishes.

 Is there a way to get a cleaner traversal for such kind of relationship. I
 want to get a complete intermediate traversal from Client to Server.

 Thank a lot for helping out on this.

 Best Regards,
 Vipul




 On Thu, Apr 21, 2011 at 12:09 AM, David Montag 
 david.mon...@neotechnology.com wrote:

 Hi Vipul,

 Thanks for listening!

 It's a very good question, and the short answer is: yes! I'm cc'ing our
 mailing list so that everyone can take part in the answer.

 Here's the long answer, illustrated by an example:

 Let's assume you're modeling a network. You'll have some domain classes
 that are all networked entities with peers:

 @NodeEntity
 public class NetworkEntity {
 @RelatedTo(type = PEER, direction = Direction.BOTH, elementClass =
 NetworkEntity.class)
 private SetNetworkEntity peers;

 public void addPeer(NetworkEntity peer) {
 peers.add(peer);
 }
 }

 public class Server extends NetworkEntity {}
 public class Router extends NetworkEntity {}
 public class Client extends NetworkEntity {}

 Then we can build a small network:

 Client c = new Client().persist();
 Router r1 = new Router().persist();
 Router r21 = new Router().persist();
 Router r22 = new Router().persist();
 Router r3 = new Router().persist();
 Server s = new Server().persist();

 c.addPeer(r1);
 r1.addPeer(r21);
 r1.addPeer(r22);
 r21.addPeer(r3);
 r22.addPeer(r3);
 r3.addPeer(s);

 c.persist();

 Note that after linking the entities, I only call persist() on the
 client. You can read more about this in the reference documentation, but
 essentially it will cascade in the direction of the relationships created,
 and will in this case cascade all the way to the server entity.

 You can now query this:

 IterableEntityPathClient, Server paths =
 c.findAllPathsByTraversal(Traversal.description());

 The above code will get you an EntityPath per node visited during the
 traversal from c. The example does however not use a very interesting
 traversal description, but you can still print the results:

 for (EntityPathClient, Server path : paths) {
 StringBuilder sb = new StringBuilder();
 IteratorNetworkEntity iter =
 path.NetworkEntitynodeEntities().iterator();
 while (iter.hasNext()) {
 sb.append(iter.next());
 if (iter.hasNext()) sb.append( - );
 }
 System.out.println(sb);
 }

 This will print each path, with all entities in the path. This is what it
 looks like:

 domain.Client@1
 domain.Client@1 - domain.Router@2
 domain.Client@1 - domain.Router@2 - domain.Router@3
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Server@6
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Router@4

 Let us know if this is what you looked for. If you want to only find
 paths that end with a server, you'd use this query instead:

 IterableEntityPathClient, Server paths =
 c.findAllPathsByTraversal(Traversal.description().evaluator(new Evaluator()
 {
 @Override
 public Evaluation evaluate(Path path) {
 if (new ConvertingEntityPath(graphDatabaseContext,
 path).endEntity() instanceof Server) {
 return Evaluation.INCLUDE_AND_PRUNE;
 }
 return Evaluation.EXCLUDE_AND_CONTINUE;
 }
 }));

 In the above code example, graphDatabaseContext is a bean of type
 GraphDatabaseContext created by Spring Data Graph. This syntax will
 dramatically improve in future releases. It will print:

 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Server@6

 Regarding your second question about types: If you want to convert a node
 into an entity, you would use the TypeRepresentationStrategy configured
 internally in Spring Data Graph. See the reference documentation for more
 information on this. If you want to convert Neo4j paths to entity paths, you
 can use the ConvertingEntityPath class as seen above. As an implementation
 detail, the class name is stored on the node as a property.

 Hope this helped!

 

Re: [Neo4j] Question from Webinar - traversing a path with nodes of different types

2011-04-21 Thread Vipul Gupta
Hi David,

Inputs are 1 and 6 + Graph is acyclic.

domain.Client@1 - domain.Router@2 - domain.Router@3 - domain.Router@5 -
domain.Server@6
  - domain.Router@7 - domain.Router@8 -

I want a way to start from 1,

process the 2 path till it reaches 5 (say in a thread)
process the 7 path till it reaches 5 (in another thread)

then process 5 and eventually 6.
the above step of processing intermediate path and waiting on the blocking
point can happen over and over again in a more complex graph (that is there
could be a number of loops in between even) and the traversal stops only we
reach 6

I hope this makes it a bit clear. I was working out something for this, but
it is turning out to be too complex a solution for this sort of traversal of
a graph, so I am hoping if you can suggest something.

Best Regards,
Vipul


On Thu, Apr 21, 2011 at 11:36 AM, David Montag 
david.mon...@neotechnology.com wrote:

 Hi Vipul,

 Zooming out a little bit, what are the inputs to your algorithm, and what
 do you want it to do?

 For example, given 1 and 6, do you want to find any points in the chain
 between them that are join points of two (or more) subchains (5 in this
 case)?

 David


 On Wed, Apr 20, 2011 at 10:56 PM, Vipul Gupta vipulgupta...@gmail.comwrote:

 my mistake - I meant 5 depends on both 3 and 8 and acts as a blocking
 point till 3 and 8 finishes


 On Thu, Apr 21, 2011 at 11:19 AM, Vipul Gupta vipulgupta...@gmail.comwrote:

 David/Michael,

 Let me modify the example a bit.
 What if my graph structure is like this

 domain.Client@1 - domain.Router@2 - domain.Router@3 - domain.Router@5- 
 domain.Server@6
   - domain.Router@7 - domain.Router@8 -


 Imagine a manufacturing line.
 6 depends on both 3 and 8 and acts as a blocking point till 3 and 8
 finishes.

 Is there a way to get a cleaner traversal for such kind of relationship. I
 want to get a complete intermediate traversal from Client to Server.

 Thank a lot for helping out on this.

 Best Regards,
 Vipul




 On Thu, Apr 21, 2011 at 12:09 AM, David Montag 
 david.mon...@neotechnology.com wrote:

 Hi Vipul,

 Thanks for listening!

 It's a very good question, and the short answer is: yes! I'm cc'ing our
 mailing list so that everyone can take part in the answer.

 Here's the long answer, illustrated by an example:

 Let's assume you're modeling a network. You'll have some domain classes
 that are all networked entities with peers:

 @NodeEntity
 public class NetworkEntity {
 @RelatedTo(type = PEER, direction = Direction.BOTH, elementClass =
 NetworkEntity.class)
 private SetNetworkEntity peers;

 public void addPeer(NetworkEntity peer) {
 peers.add(peer);
 }
 }

 public class Server extends NetworkEntity {}
 public class Router extends NetworkEntity {}
 public class Client extends NetworkEntity {}

 Then we can build a small network:

 Client c = new Client().persist();
 Router r1 = new Router().persist();
 Router r21 = new Router().persist();
 Router r22 = new Router().persist();
 Router r3 = new Router().persist();
 Server s = new Server().persist();

 c.addPeer(r1);
 r1.addPeer(r21);
 r1.addPeer(r22);
 r21.addPeer(r3);
 r22.addPeer(r3);
 r3.addPeer(s);

 c.persist();

 Note that after linking the entities, I only call persist() on the
 client. You can read more about this in the reference documentation, but
 essentially it will cascade in the direction of the relationships created,
 and will in this case cascade all the way to the server entity.

 You can now query this:

 IterableEntityPathClient, Server paths =
 c.findAllPathsByTraversal(Traversal.description());

 The above code will get you an EntityPath per node visited during the
 traversal from c. The example does however not use a very interesting
 traversal description, but you can still print the results:

 for (EntityPathClient, Server path : paths) {
 StringBuilder sb = new StringBuilder();
 IteratorNetworkEntity iter =
 path.NetworkEntitynodeEntities().iterator();
 while (iter.hasNext()) {
 sb.append(iter.next());
 if (iter.hasNext()) sb.append( - );
 }
 System.out.println(sb);
 }

 This will print each path, with all entities in the path. This is what
 it looks like:

 domain.Client@1
 domain.Client@1 - domain.Router@2
 domain.Client@1 - domain.Router@2 - domain.Router@3
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Server@6
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Router@4

 Let us know if this is what you looked for. If you want to only find
 paths that end with a server, you'd use this query instead:

 IterableEntityPathClient, Server paths =
 c.findAllPathsByTraversal(Traversal.description().evaluator(new Evaluator()
 {
 @Override
 public Evaluation evaluate(Path path) {
 if (new 

Re: [Neo4j] Question from Webinar - traversing a path with nodes of different types

2011-04-21 Thread Rick Bullotta
Sounds like a simulation/operations research application.  The graph database 
will be suitable for modeling the entities and their characteristics (transfer 
times = properties on relationships, setup and services times = properties on 
nodes, queue sizes, etc) but I think you'll need a layer on top of the 
traversal framework for managing the overall simulation logic.



- Reply message -
From: Vipul Gupta vipulgupta...@gmail.com
Date: Thu, Apr 21, 2011 2:16 am
Subject: [Neo4j] Question from Webinar - traversing a path with nodes of 
different types
To: David Montag david.mon...@neotechnology.com
Cc: UserList user@lists.neo4j.org, michael.hun...@neotechnology.com 
michael.hun...@neotechnology.com

Hi David,

Inputs are 1 and 6 + Graph is acyclic.

domain.Client@1 - domain.Router@2 - domain.Router@3 - domain.Router@5 -
domain.Server@6
  - domain.Router@7 - domain.Router@8 -

I want a way to start from 1,

process the 2 path till it reaches 5 (say in a thread)
process the 7 path till it reaches 5 (in another thread)

then process 5 and eventually 6.
the above step of processing intermediate path and waiting on the blocking
point can happen over and over again in a more complex graph (that is there
could be a number of loops in between even) and the traversal stops only we
reach 6

I hope this makes it a bit clear. I was working out something for this, but
it is turning out to be too complex a solution for this sort of traversal of
a graph, so I am hoping if you can suggest something.

Best Regards,
Vipul


On Thu, Apr 21, 2011 at 11:36 AM, David Montag 
david.mon...@neotechnology.com wrote:

 Hi Vipul,

 Zooming out a little bit, what are the inputs to your algorithm, and what
 do you want it to do?

 For example, given 1 and 6, do you want to find any points in the chain
 between them that are join points of two (or more) subchains (5 in this
 case)?

 David


 On Wed, Apr 20, 2011 at 10:56 PM, Vipul Gupta vipulgupta...@gmail.comwrote:

 my mistake - I meant 5 depends on both 3 and 8 and acts as a blocking
 point till 3 and 8 finishes


 On Thu, Apr 21, 2011 at 11:19 AM, Vipul Gupta vipulgupta...@gmail.comwrote:

 David/Michael,

 Let me modify the example a bit.
 What if my graph structure is like this

 domain.Client@1 - domain.Router@2 - domain.Router@3 - domain.Router@5- 
 domain.Server@6
   - domain.Router@7 - domain.Router@8 -


 Imagine a manufacturing line.
 6 depends on both 3 and 8 and acts as a blocking point till 3 and 8
 finishes.

 Is there a way to get a cleaner traversal for such kind of relationship. I
 want to get a complete intermediate traversal from Client to Server.

 Thank a lot for helping out on this.

 Best Regards,
 Vipul




 On Thu, Apr 21, 2011 at 12:09 AM, David Montag 
 david.mon...@neotechnology.com wrote:

 Hi Vipul,

 Thanks for listening!

 It's a very good question, and the short answer is: yes! I'm cc'ing our
 mailing list so that everyone can take part in the answer.

 Here's the long answer, illustrated by an example:

 Let's assume you're modeling a network. You'll have some domain classes
 that are all networked entities with peers:

 @NodeEntity
 public class NetworkEntity {
 @RelatedTo(type = PEER, direction = Direction.BOTH, elementClass =
 NetworkEntity.class)
 private SetNetworkEntity peers;

 public void addPeer(NetworkEntity peer) {
 peers.add(peer);
 }
 }

 public class Server extends NetworkEntity {}
 public class Router extends NetworkEntity {}
 public class Client extends NetworkEntity {}

 Then we can build a small network:

 Client c = new Client().persist();
 Router r1 = new Router().persist();
 Router r21 = new Router().persist();
 Router r22 = new Router().persist();
 Router r3 = new Router().persist();
 Server s = new Server().persist();

 c.addPeer(r1);
 r1.addPeer(r21);
 r1.addPeer(r22);
 r21.addPeer(r3);
 r22.addPeer(r3);
 r3.addPeer(s);

 c.persist();

 Note that after linking the entities, I only call persist() on the
 client. You can read more about this in the reference documentation, but
 essentially it will cascade in the direction of the relationships created,
 and will in this case cascade all the way to the server entity.

 You can now query this:

 IterableEntityPathClient, Server paths =
 c.findAllPathsByTraversal(Traversal.description());

 The above code will get you an EntityPath per node visited during the
 traversal from c. The example does however not use a very interesting
 traversal description, but you can still print the results:

 for (EntityPathClient, Server path : paths) {
 StringBuilder sb = new StringBuilder();
 IteratorNetworkEntity iter =
 path.NetworkEntitynodeEntities().iterator();
 while (iter.hasNext()) {
 sb.append(iter.next());
 if (iter.hasNext()) sb.append( - );
 }
 System.out.println(sb);
 }

 This will print each path, with all entities in the path. This is what
 it looks like:

 

Re: [Neo4j] Question from Webinar - traversing a path with nodes of different types

2011-04-20 Thread Vipul Gupta
David/Michael,

Let me modify the example a bit.
What if my graph structure is like this

domain.Client@1 - domain.Router@2 - domain.Router@3 - domain.Router@5 -
domain.Server@6
  - domain.Router@7 - domain.Router@8 -


Imagine a manufacturing line.
6 depends on both 3 and 8 and acts as a blocking point till 3 and 8
finishes.

Is there a way to get a cleaner traversal for such kind of relationship. I
want to get a complete intermediate traversal from Client to Server.

Thank a lot for helping out on this.

Best Regards,
Vipul




On Thu, Apr 21, 2011 at 12:09 AM, David Montag 
david.mon...@neotechnology.com wrote:

 Hi Vipul,

 Thanks for listening!

 It's a very good question, and the short answer is: yes! I'm cc'ing our
 mailing list so that everyone can take part in the answer.

 Here's the long answer, illustrated by an example:

 Let's assume you're modeling a network. You'll have some domain classes
 that are all networked entities with peers:

 @NodeEntity
 public class NetworkEntity {
 @RelatedTo(type = PEER, direction = Direction.BOTH, elementClass =
 NetworkEntity.class)
 private SetNetworkEntity peers;

 public void addPeer(NetworkEntity peer) {
 peers.add(peer);
 }
 }

 public class Server extends NetworkEntity {}
 public class Router extends NetworkEntity {}
 public class Client extends NetworkEntity {}

 Then we can build a small network:

 Client c = new Client().persist();
 Router r1 = new Router().persist();
 Router r21 = new Router().persist();
 Router r22 = new Router().persist();
 Router r3 = new Router().persist();
 Server s = new Server().persist();

 c.addPeer(r1);
 r1.addPeer(r21);
 r1.addPeer(r22);
 r21.addPeer(r3);
 r22.addPeer(r3);
 r3.addPeer(s);

 c.persist();

 Note that after linking the entities, I only call persist() on the client.
 You can read more about this in the reference documentation, but essentially
 it will cascade in the direction of the relationships created, and will in
 this case cascade all the way to the server entity.

 You can now query this:

 IterableEntityPathClient, Server paths =
 c.findAllPathsByTraversal(Traversal.description());

 The above code will get you an EntityPath per node visited during the
 traversal from c. The example does however not use a very interesting
 traversal description, but you can still print the results:

 for (EntityPathClient, Server path : paths) {
 StringBuilder sb = new StringBuilder();
 IteratorNetworkEntity iter =
 path.NetworkEntitynodeEntities().iterator();
 while (iter.hasNext()) {
 sb.append(iter.next());
 if (iter.hasNext()) sb.append( - );
 }
 System.out.println(sb);
 }

 This will print each path, with all entities in the path. This is what it
 looks like:

 domain.Client@1
 domain.Client@1 - domain.Router@2
 domain.Client@1 - domain.Router@2 - domain.Router@3
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Server@6
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Router@4

 Let us know if this is what you looked for. If you want to only find paths
 that end with a server, you'd use this query instead:

 IterableEntityPathClient, Server paths =
 c.findAllPathsByTraversal(Traversal.description().evaluator(new Evaluator()
 {
 @Override
 public Evaluation evaluate(Path path) {
 if (new ConvertingEntityPath(graphDatabaseContext,
 path).endEntity() instanceof Server) {
 return Evaluation.INCLUDE_AND_PRUNE;
 }
 return Evaluation.EXCLUDE_AND_CONTINUE;
 }
 }));

 In the above code example, graphDatabaseContext is a bean of type
 GraphDatabaseContext created by Spring Data Graph. This syntax will
 dramatically improve in future releases. It will print:

 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Server@6

 Regarding your second question about types: If you want to convert a node
 into an entity, you would use the TypeRepresentationStrategy configured
 internally in Spring Data Graph. See the reference documentation for more
 information on this. If you want to convert Neo4j paths to entity paths, you
 can use the ConvertingEntityPath class as seen above. As an implementation
 detail, the class name is stored on the node as a property.

 Hope this helped!

 David

 On Wed, Apr 20, 2011 at 9:20 AM, Emil Eifrem e...@neotechnology.comwrote:

 David / Michael, do you guys want to dig in and help out Vipul below?

 -EE

 On Wed, Apr 20, 2011 at 09:17, Vipul Gupta vipulgupta...@gmail.com
 wrote:
  Hi Emil,
  I would like to start by thanking you for the webinar. It was very
 useful .
  This is the question I asked on the webinar as well.
  How can we traverse a graph which consists of nodes of different types
 using
  SDG?
  Say first Node in relationship is of type A, next 3 nodes in path are of

Re: [Neo4j] Question from Webinar - traversing a path with nodes of different types

2011-04-20 Thread Vipul Gupta
my mistake - I meant 5 depends on both 3 and 8 and acts as a blocking
point till 3 and 8 finishes

On Thu, Apr 21, 2011 at 11:19 AM, Vipul Gupta vipulgupta...@gmail.comwrote:

 David/Michael,

 Let me modify the example a bit.
 What if my graph structure is like this

 domain.Client@1 - domain.Router@2 - domain.Router@3 - domain.Router@5- 
 domain.Server@6
   - domain.Router@7 - domain.Router@8 -


 Imagine a manufacturing line.
 6 depends on both 3 and 8 and acts as a blocking point till 3 and 8
 finishes.

 Is there a way to get a cleaner traversal for such kind of relationship. I
 want to get a complete intermediate traversal from Client to Server.

 Thank a lot for helping out on this.

 Best Regards,
 Vipul




 On Thu, Apr 21, 2011 at 12:09 AM, David Montag 
 david.mon...@neotechnology.com wrote:

 Hi Vipul,

 Thanks for listening!

 It's a very good question, and the short answer is: yes! I'm cc'ing our
 mailing list so that everyone can take part in the answer.

 Here's the long answer, illustrated by an example:

 Let's assume you're modeling a network. You'll have some domain classes
 that are all networked entities with peers:

 @NodeEntity
 public class NetworkEntity {
 @RelatedTo(type = PEER, direction = Direction.BOTH, elementClass =
 NetworkEntity.class)
 private SetNetworkEntity peers;

 public void addPeer(NetworkEntity peer) {
 peers.add(peer);
 }
 }

 public class Server extends NetworkEntity {}
 public class Router extends NetworkEntity {}
 public class Client extends NetworkEntity {}

 Then we can build a small network:

 Client c = new Client().persist();
 Router r1 = new Router().persist();
 Router r21 = new Router().persist();
 Router r22 = new Router().persist();
 Router r3 = new Router().persist();
 Server s = new Server().persist();

 c.addPeer(r1);
 r1.addPeer(r21);
 r1.addPeer(r22);
 r21.addPeer(r3);
 r22.addPeer(r3);
 r3.addPeer(s);

 c.persist();

 Note that after linking the entities, I only call persist() on the client.
 You can read more about this in the reference documentation, but essentially
 it will cascade in the direction of the relationships created, and will in
 this case cascade all the way to the server entity.

 You can now query this:

 IterableEntityPathClient, Server paths =
 c.findAllPathsByTraversal(Traversal.description());

 The above code will get you an EntityPath per node visited during the
 traversal from c. The example does however not use a very interesting
 traversal description, but you can still print the results:

 for (EntityPathClient, Server path : paths) {
 StringBuilder sb = new StringBuilder();
 IteratorNetworkEntity iter =
 path.NetworkEntitynodeEntities().iterator();
 while (iter.hasNext()) {
 sb.append(iter.next());
 if (iter.hasNext()) sb.append( - );
 }
 System.out.println(sb);
 }

 This will print each path, with all entities in the path. This is what it
 looks like:

 domain.Client@1
 domain.Client@1 - domain.Router@2
 domain.Client@1 - domain.Router@2 - domain.Router@3
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Server@6
 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Router@4

 Let us know if this is what you looked for. If you want to only find paths
 that end with a server, you'd use this query instead:

 IterableEntityPathClient, Server paths =
 c.findAllPathsByTraversal(Traversal.description().evaluator(new Evaluator()
 {
 @Override
 public Evaluation evaluate(Path path) {
 if (new ConvertingEntityPath(graphDatabaseContext,
 path).endEntity() instanceof Server) {
 return Evaluation.INCLUDE_AND_PRUNE;
 }
 return Evaluation.EXCLUDE_AND_CONTINUE;
 }
 }));

 In the above code example, graphDatabaseContext is a bean of type
 GraphDatabaseContext created by Spring Data Graph. This syntax will
 dramatically improve in future releases. It will print:

 domain.Client@1 - domain.Router@2 - domain.Router@3 -
 domain.Router@5 - domain.Server@6

 Regarding your second question about types: If you want to convert a node
 into an entity, you would use the TypeRepresentationStrategy configured
 internally in Spring Data Graph. See the reference documentation for more
 information on this. If you want to convert Neo4j paths to entity paths, you
 can use the ConvertingEntityPath class as seen above. As an implementation
 detail, the class name is stored on the node as a property.

 Hope this helped!

 David

 On Wed, Apr 20, 2011 at 9:20 AM, Emil Eifrem e...@neotechnology.comwrote:

 David / Michael, do you guys want to dig in and help out Vipul below?

 -EE

 On Wed, Apr 20, 2011 at 09:17, Vipul Gupta vipulgupta...@gmail.com
 wrote:
  Hi Emil,
  I would like to start by thanking you for the webinar. It was very
 useful .
  This is the question I