Neo4 J

6,440 views
6,395 views

Published on

Neo4j is a graph database. It is an embedded, disk-based, fully transactional Java persistence engine that stores data structured in graphs rather than in tables. A graph (mathematical lingo for a network) is a flexible data structure that allows a more agile and rapid style of development.This graph database has got its own standalone server mode, based on interaction via REST.

Published in: Technology
1 Comment
7 Likes
Statistics
Notes
  • You can watch the video here: http://skillsmatter.com/podcast/java-jee/neo4j-traversers-and-pattern-matchers/rl-311
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
6,440
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
37
Comments
1
Likes
7
Embeds 0
No embeds

No notes for slide

Neo4 J

  1. 1. Neo4J Traversers and Pattern Matchers<br />Ian Robinson<br />http://ianSrobinson.com<br />@ianSrobinson<br />ianSrobinson@gmail.com<br />
  2. 2. Traversers<br />
  3. 3. Nodes and relationships<br />
  4. 4. What does Ian like?<br />
  5. 5. What vouchers is Ian entitled to?<br />Including this one?<br />
  6. 6. Yes<br />
  7. 7. What vouchers is Jim entitled to?<br />Including this one?<br />
  8. 8. No<br />
  9. 9. VouchersOfInterestQuery<br />public class VouchersOfInterestQuery implements Query<Voucher> {<br /> public List<Voucher> execute(final Customer customer, Object... params) {<br /> Collection<Node> nodes = customer.getNode()<br /> .traverse(<br />Order.DEPTH_FIRST, <br />StopEvaluator.END_OF_GRAPH, <br /> new IsVoucherFor(customer), <br />Relationships.PROVIDES, Direction.INCOMING, <br />Relationships.LIKES, Direction.OUTGOING, <br />Relationships.APPLIES_TO, Direction.INCOMING)<br /> .getAllNodes();<br /> return toArray(nodes);<br /> }<br /> private class IsVoucherFor implements ReturnableEvaluator {<br /> ...<br /> }<br /> private class IsSameCustomer implements ReturnableEvaluator {<br /> ...<br /> }<br /> private List<Voucher> toArray(Collection<Node> nodes) {<br /> ...<br /> } <br />}<br />
  10. 10. IsVoucherFor<br />private class IsVoucherFor implements ReturnableEvaluator {<br /> private final Customer customer;<br /> public IsVoucherFor(Customer customer) {<br />this.customer = customer;<br /> }<br /> @Override<br /> public booleanisReturnableNode(TraversalPositiontp) {<br /> if (!isVoucher(tp)) { return false; }<br /> if (!isSponsored(tp)) { return true; } <br /> Collection<Node> allNodes = tp.currentNode()<br /> .traverse(<br />Order.DEPTH_FIRST, <br />StopEvaluator.END_OF_GRAPH, <br /> new IsSameCustomer(customer), <br />Relationships.SPONSORS, Direction.INCOMING, <br />Relationships.CUSTOMER_OF, Direction.INCOMING)<br /> .getAllNodes();<br /> return allNodes != null && allNodes.size() > 0;<br /> }<br /> ...<br />}<br />
  11. 11. isVoucher<br />private class IsVoucherFor implements ReturnableEvaluator {<br /> private final Customer customer;<br /> public IsVoucherFor(Customer customer) {<br />this.customer = customer;<br /> }<br /> @Override<br /> public booleanisReturnableNode(TraversalPositiontp) {<br /> if (!isVoucher(tp)) { return false; }<br /> if (!isSponsored(tp)) { return true; } <br /> Collection<Node> allNodes = tp.currentNode()<br /> .traverse(<br />Order.DEPTH_FIRST, <br />StopEvaluator.END_OF_GRAPH, <br /> new IsSameCustomer(customer), <br />Relationships.SPONSORS, Direction.INCOMING, <br />Relationships.CUSTOMER_OF, Direction.INCOMING)<br /> .getAllNodes();<br /> return allNodes != null && allNodes.size() > 0;<br /> }<br /> ...<br />}<br />private booleanisVoucher(TraversalPositiontp) {<br /> return tp.notStartNode() <br /> && tp.lastRelationshipTraversed().isType(Relationships.APPLIES_TO);<br /> }<br />
  12. 12. isSponsored<br />private class IsVoucherFor implements ReturnableEvaluator {<br /> private final Customer customer;<br /> public IsVoucherFor(Customer customer) {<br />this.customer = customer;<br /> }<br /> @Override<br /> public booleanisReturnableNode(TraversalPositiontp) {<br /> if (!isVoucher(tp)) { return false; }<br /> if (!isSponsored(tp)) { return true; } <br /> Collection<Node> allNodes = tp.currentNode()<br /> .traverse(<br />Order.DEPTH_FIRST, <br />StopEvaluator.END_OF_GRAPH, <br /> new IsSameCustomer(customer), <br />Relationships.SPONSORS, Direction.INCOMING, <br />Relationships.CUSTOMER_OF, Direction.INCOMING)<br /> .getAllNodes();<br /> return allNodes != null && allNodes.size() > 0;<br /> }<br /> ...<br />}<br />private booleanisSponsored(TraversalPositiontp) {<br /> return tp.currentNode().hasRelationship(<br />Relationships.SPONSORS, Direction.INCOMING);<br />}<br />
  13. 13. IsSameCustomer<br />private class IsVoucherFor implements ReturnableEvaluator {<br /> private final Customer customer;<br /> public IsVoucherFor(Customer customer) {<br />this.customer = customer;<br /> }<br /> @Override<br /> public booleanisReturnableNode(TraversalPositiontp) {<br /> if (!isVoucher(tp)) { return false; }<br />if(!isSponsored(tp)) { return true; } <br /> Collection<Node> allNodes = tp.currentNode()<br /> .traverse(<br />Order.DEPTH_FIRST, <br />StopEvaluator.END_OF_GRAPH, <br /> new IsSameCustomer(customer), <br />Relationships.SPONSORS, Direction.INCOMING, <br />Relationships.CUSTOMER_OF, Direction.INCOMING)<br /> .getAllNodes();<br /> return allNodes != null && allNodes.size() > 0;<br /> }<br /> ...<br />}<br />private class IsSameCustomer implements ReturnableEvaluator {<br /> private final Customer customer;<br /> public IsSameCustomer(Customer customer) {<br />this.customer = customer;<br /> }<br /> @Override<br /> public booleanisReturnableNode(TraversalPositiontp) {<br /> return tp.currentNode().getId() == customer.getNode().getId();<br /> } <br />}<br />
  14. 14. Pattern Matchers<br />
  15. 15. Financial products<br />
  16. 16. Purchases<br />
  17. 17. Recommendations<br />Customers who bought P also bought {Pa, …, Pb}<br />Customers who bought {Pa, …, Pb} also bought {Px, …, Py}<br />Etc<br />Popularity is a function of number of purchasers and distance from original product (products close to the original product are ranked higher than those further away)<br />
  18. 18. .2<br />Customers who bought an ISA also bought…<br />
  19. 19. .2<br />Customers who bought Virgin Card and Fixed Rate Mortgage also bought…<br />
  20. 20. Using pattern matchers<br />public Collection<Recommendation> recommend(ProductcurrentProduct, <br />intdepthOfSearch) {<br />HashMap<Long, Recommendation> recommendations = <br /> new HashMap<Long, Recommendation>();<br /> Node currentProductNode = currentProduct.getNode();<br />for(int depth = 1; depth <= depthOfSearch; depth++) {<br />addNodePopularities(recommendations, currentProductNode, depth);<br /> }<br /> return recommendations.values();<br />}<br />private void addNodePopularities(HashMap<Long, Recommendation> recommendations, <br /> ...<br />}<br />
  21. 21. Building subgraph to match<br />private void addNodePopularities(HashMap<Long, Recommendation> recommendations, <br /> Node currentProductNode, int depth) {<br />PatternNodefirstProduct = new PatternNode();<br />PatternNodelastProduct = firstProduct;<br />for(inti = 1; i <= depth; i++) {<br />PatternNodenextProduct = new PatternNode();<br />PatternNode customer = new PatternNode();<br />customer.createRelationshipTo(lastProduct, <br />DynamicRelationshipType.withName("Purchased") );<br />customer.createRelationshipTo(nextProduct, <br />DynamicRelationshipType.withName("Purchased") );<br />lastProduct = nextProduct;<br /> }<br />Iterable<PatternMatch> matches = PatternMatcher.getMatcher().match(<br />firstProduct, currentProductNode);<br /> for ( PatternMatch match : matches ) {<br /> Node endNode = match.getNodeFor(lastProduct);<br />incrementPopularity(recommendations, currentProductNode, endNode, depth);<br /> }<br />}<br />C<br />C<br />P<br />Plast<br />Pfirst<br />

×