Perfect. Thanks David. I have an exactly similar model with a slightly more complicated structure :). I will keep you updated with my project (totally based on neo4j and SDG) as I plan to open source it eventually.
I have one more question for you : I see the latest release changes NodeGraphRepository from interfaces to classes( RC1 to RELEASE) What necessitated this change and are there any release notes with detailed explanations about changes which I can refer to ? I am finding it hard to map out the changes 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 Set<NetworkEntity> 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: > > Iterable<EntityPath<Client, 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 (EntityPath<Client, Server> path : paths) { > StringBuilder sb = new StringBuilder(); > Iterator<NetworkEntity> iter = > path.<NetworkEntity>nodeEntities().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: > > Iterable<EntityPath<Client, 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.com>wrote: > >> 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 >> > type B, and next node is of type C. >> > Is there a way to traverse the path and keep getting the domain objects >> at >> > each stage of traversal. >> > answer mentioned on the webinar is to look at findAllPathsByTraversal in >> > NodeBacked. >> > I am not able to understand how to get an Iterator or a path with starts >> > with Type A, ends with Type C and has number of Type C in between. >> > >> > Also if I convert a domain POJO to a Node object, is there a way to know >> > what domain type it represents or is wrapped around so that it can be >> > converted back to that type. >> > Please let me know. >> > Best Regards, >> > Vipul Gupta >> >> >> >> -- >> Emil Eifrém, CEO [e...@neotechnology.com] >> Neo Technology, www.neotechnology.com >> Cell: +46 733 462 271 | US: 206 403 8808 >> http://blogs.neotechnology.com/emil >> http://twitter.com/emileifrem >> > > > > -- > David Montag <david.mon...@neotechnology.com> > Neo Technology, www.neotechnology.com > Cell: 650.556.4411 > Skype: ddmontag > _______________________________________________ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user