Basically, I'm trying to implement rules engine in neo4j and I think that
pattern matching would be the right way to find the rule. But I really don't
know how to do that in Cypher or even in PatternMatching. 

This is my graph

(customers)-------------------[:applies]----------------------(rules)
     |                                                                          
 
|
     | [:customer]                                                          
| [:rule]
     |                                                                          
 
|
(customer)                                                                
(rule)
                                                                               
/     \
                                                              [:less_than] /    
  
\ [:greater_than]
                                                                             
/         \
                                                                       
(child)      (adult)

And on [:less_than] and [:greater_than] relationships I have another
property called "threshold" in my example the threshold for less_than would
be 12 and 25 for greater_than. 

The tricky bit is the child nodes of (rule) node can be anything depends on
the age of the customer. So, I'm trying to find a way to include condition
statement in pattern matching as well. I guess that would be the "where" in
Cypher. For example, if the customer is 13 years old which is morn than 12
that the returned node would be adult. 

This is what I've done but I only get nothing back. 

    public void findByPatternMatching() throws Exception {
        final int value = 10;

        PatternNode subReferenceNode = new PatternNode();

        PatternNode rulesNode = new PatternNode();

        PatternNode ruleNode = new PatternNode();
        rulesNode.createRelationshipTo(ruleNode, MyRelationship.RULE,
Direction.OUTGOING);

        subReferenceNode.createRelationshipTo(rulesNode,
MyRelationship.APPLIES, Direction.OUTGOING);

        final PatternNode possibleAnswerNode = new PatternNode();

        PatternRelationship ruleToAnswerRelationship =
ruleNode.createRelationshipTo(possibleAnswerNode, MyRelationship.LESS_THAN);
        RelationshipType relationshipType =
ruleToAnswerRelationship.getType();

        if (relationshipType.name().equals("LESS_THAN")) {
            ruleToAnswerRelationship.addPropertyConstraint("Threshold", new
ValueMatcher() {
                @Override
                public boolean matches(Object o) {
                    int threshold = (Integer) o;
                    if (o instanceof Integer) {
                        if (value > 0 && value < threshold) {
                            return true;
                        }
                    }
                    return false;
                }
            });
        }

        PatternMatcher matcher = PatternMatcher.getMatcher();
        Iterable<PatternMatch> matches = matcher.match(rulesNode,
DatabaseHelper.getInstance().getRuleNode());

        HashSet<String> possibleNodes = new HashSet<String>();

        PatternUtil.printGraph(rulesNode, System.out);

        for (PatternMatch pm : matches) {
            possibleNodes.add((String)
pm.getNodeFor(possibleAnswerNode).getProperty("Name"));
        }

        assertThat( possibleNodes, containsOnlyNodeNames("Child"));

    }

And this is the graph that I use PatternUtil.printGraph (which I really
don't quite understand, especially the false values)

: 
        RULE:false: 
                : 
                        RULE:false: 
                        LESS_THAN:false: 
                                : 
                                        LESS_THAN:false: 
        APPLIES:false: 
                : 
                        APPLIES:false: 

Thank you very much. 

--
View this message in context: 
http://neo4j-user-list.438527.n3.nabble.com/I-m-really-stuck-in-PatternMatching-tp3072170p3072170.html
Sent from the Neo4J User List mailing list archive at Nabble.com.
_______________________________________________
Neo4j mailing list
[email protected]
https://lists.neo4j.org/mailman/listinfo/user

Reply via email to