DROOLS local variable assignment with nested members -
i having trouble assigning local variable nested members/objects in drools 6.2 , optaplanner 2 java classes. trying determine when 2 facts/instances nested members have same values. test case below simple, in reality i'm trying compare multiple nested members in rule.
public class { private b x; //public b getx(), public void setx(b b) follow ... } public class b { private int y; //public int gety(), public void sety(int y) follow ... } rule "testnestedmembers" when a(x.y : y, $x : x) a(x2.y == y, $x : x2) scoreholder.addhardconstraintmatch(kcontext,-1000); message [id=1, level=error, path=org/somebody/project/planner/solver/planscorerules.drl, line=16, column=0 text=[err 102] line 16:49 mismatched input ':' in rule "testnestedmembers"] message [id=2, level=error, path=org/somebody/project/planner/solver/planscorerules.drl, line=0, column=0 text=parser returned null package] --- warning messages: --- info messages: @ org.optaplanner.core.config.score.director.scoredirectorfactoryconfig.buildkiebase(scoredirectorfactoryconfig.java:387) i have reviewed answers, such as: drools rule expression : access nested class data member
and geoffrey de smet's answer illustrates conditional, not local assignment. i've tried different variations, no luck. thank advice.
edit: should have said creating binding instead of assigning local variable.
first i'll point out causes compiler errors.
rule "testnestedmembers" when a(x.y : y, $x : x) // (1) a(x2.y == y, $x : x2) // (2) (3) (1) binding has form <variable> : <field>, x.y isn't valid variable name. (2) same x2.y. also, x2 isn't field in a. (3) nothing keeps rule engine matching same fact both patterns class a. means rule fire each , every fact of class a, since (as intend) a.x.y equal itself.
correct is
rule "testnestedmembers" when $a1: a( $x1: x ) a( != $a1, $x2: x, $x1.gety() == $x2.gety() ) ... however! rule fires twice, once 1 fact bound $a1, , once other (matching) 1 bound $a1. 1 possibility test existence of such pair (or cluster!):
rule "testnestedmembers" when $a1: a( $x1: x ) exists a( != $a1, $x2: x, $x1.gety() == $x2.gety() ) ... another option ensure ordering testing attribute:
rule "testnestedmembers" when $a1: a( $x1: x, $id: id ) exists a( id > $id, $x2: x, $x1.gety() == $x2.gety() ) now fires each pair of a's second has greater (unique!) id.
Comments
Post a Comment