Weitere ähnliche Inhalte Mehr von Skills Matter (20) Kürzlich hochgeladen (20) Neo4 J 1. Neo4J Traversers and Pattern Matchers Ian Robinson http://ianSrobinson.com @ianSrobinson ianSrobinson@gmail.com 9. VouchersOfInterestQuery public class VouchersOfInterestQuery implements Query<Voucher> { public List<Voucher> execute(final Customer customer, Object... params) { Collection<Node> nodes = customer.getNode() .traverse( Order.DEPTH_FIRST, StopEvaluator.END_OF_GRAPH, new IsVoucherFor(customer), Relationships.PROVIDES, Direction.INCOMING, Relationships.LIKES, Direction.OUTGOING, Relationships.APPLIES_TO, Direction.INCOMING) .getAllNodes(); return toArray(nodes); } private class IsVoucherFor implements ReturnableEvaluator { ... } private class IsSameCustomer implements ReturnableEvaluator { ... } private List<Voucher> toArray(Collection<Node> nodes) { ... } } 10. IsVoucherFor private class IsVoucherFor implements ReturnableEvaluator { private final Customer customer; public IsVoucherFor(Customer customer) { this.customer = customer; } @Override public booleanisReturnableNode(TraversalPositiontp) { if (!isVoucher(tp)) { return false; } if (!isSponsored(tp)) { return true; } Collection<Node> allNodes = tp.currentNode() .traverse( Order.DEPTH_FIRST, StopEvaluator.END_OF_GRAPH, new IsSameCustomer(customer), Relationships.SPONSORS, Direction.INCOMING, Relationships.CUSTOMER_OF, Direction.INCOMING) .getAllNodes(); return allNodes != null && allNodes.size() > 0; } ... } 11. isVoucher private class IsVoucherFor implements ReturnableEvaluator { private final Customer customer; public IsVoucherFor(Customer customer) { this.customer = customer; } @Override public booleanisReturnableNode(TraversalPositiontp) { if (!isVoucher(tp)) { return false; } if (!isSponsored(tp)) { return true; } Collection<Node> allNodes = tp.currentNode() .traverse( Order.DEPTH_FIRST, StopEvaluator.END_OF_GRAPH, new IsSameCustomer(customer), Relationships.SPONSORS, Direction.INCOMING, Relationships.CUSTOMER_OF, Direction.INCOMING) .getAllNodes(); return allNodes != null && allNodes.size() > 0; } ... } private booleanisVoucher(TraversalPositiontp) { return tp.notStartNode() && tp.lastRelationshipTraversed().isType(Relationships.APPLIES_TO); } 12. isSponsored private class IsVoucherFor implements ReturnableEvaluator { private final Customer customer; public IsVoucherFor(Customer customer) { this.customer = customer; } @Override public booleanisReturnableNode(TraversalPositiontp) { if (!isVoucher(tp)) { return false; } if (!isSponsored(tp)) { return true; } Collection<Node> allNodes = tp.currentNode() .traverse( Order.DEPTH_FIRST, StopEvaluator.END_OF_GRAPH, new IsSameCustomer(customer), Relationships.SPONSORS, Direction.INCOMING, Relationships.CUSTOMER_OF, Direction.INCOMING) .getAllNodes(); return allNodes != null && allNodes.size() > 0; } ... } private booleanisSponsored(TraversalPositiontp) { return tp.currentNode().hasRelationship( Relationships.SPONSORS, Direction.INCOMING); } 13. IsSameCustomer private class IsVoucherFor implements ReturnableEvaluator { private final Customer customer; public IsVoucherFor(Customer customer) { this.customer = customer; } @Override public booleanisReturnableNode(TraversalPositiontp) { if (!isVoucher(tp)) { return false; } if(!isSponsored(tp)) { return true; } Collection<Node> allNodes = tp.currentNode() .traverse( Order.DEPTH_FIRST, StopEvaluator.END_OF_GRAPH, new IsSameCustomer(customer), Relationships.SPONSORS, Direction.INCOMING, Relationships.CUSTOMER_OF, Direction.INCOMING) .getAllNodes(); return allNodes != null && allNodes.size() > 0; } ... } private class IsSameCustomer implements ReturnableEvaluator { private final Customer customer; public IsSameCustomer(Customer customer) { this.customer = customer; } @Override public booleanisReturnableNode(TraversalPositiontp) { return tp.currentNode().getId() == customer.getNode().getId(); } } 17. Recommendations Customers who bought P also bought {Pa, …, Pb} Customers who bought {Pa, …, Pb} also bought {Px, …, Py} Etc 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) 20. Using pattern matchers public Collection<Recommendation> recommend(ProductcurrentProduct, intdepthOfSearch) { HashMap<Long, Recommendation> recommendations = new HashMap<Long, Recommendation>(); Node currentProductNode = currentProduct.getNode(); for(int depth = 1; depth <= depthOfSearch; depth++) { addNodePopularities(recommendations, currentProductNode, depth); } return recommendations.values(); } private void addNodePopularities(HashMap<Long, Recommendation> recommendations, ... } 21. Building subgraph to match private void addNodePopularities(HashMap<Long, Recommendation> recommendations, Node currentProductNode, int depth) { PatternNodefirstProduct = new PatternNode(); PatternNodelastProduct = firstProduct; for(inti = 1; i <= depth; i++) { PatternNodenextProduct = new PatternNode(); PatternNode customer = new PatternNode(); customer.createRelationshipTo(lastProduct, DynamicRelationshipType.withName("Purchased") ); customer.createRelationshipTo(nextProduct, DynamicRelationshipType.withName("Purchased") ); lastProduct = nextProduct; } Iterable<PatternMatch> matches = PatternMatcher.getMatcher().match( firstProduct, currentProductNode); for ( PatternMatch match : matches ) { Node endNode = match.getNodeFor(lastProduct); incrementPopularity(recommendations, currentProductNode, endNode, depth); } } C C P Plast Pfirst