Re: [rules-users] Negation semantics in Drools
is correct. >>>>> > >>>>> > Please explain further if I am missing anything. >>>>> > >>>>> > Edson >>>>> > >>>>> > >>>>> > 2009/4/17 Paul Fodor >>>>> >> >>>>> >> Hi Edson, Greg, >>>>> >> I don't think the rule is written wrong. This is how the win-nowin >>>>> program is written in logic programming: X wins if there is a move from X >>>>> to >>>>> some Y and Y doesn't win: >>>>> >> >>>>> >> win(X):- move(X,Y), not(win(Y)). >>>>> >> >>>>> >> rule "direct" % Drools >>>>> >> >>>>> >> when >>>>> >> m : Move(x : first, y : second) >>>>> >> not Win(first == y) >>>>> >> then >>>>> >> insert(new Win(m.getFirst())); >>>>> >> end >>>>> >> >>>>> >> I think that it's interesting that, in Jess (another production rule >>>>> system), the stratified model is always computed right, no matter what was >>>>> the order of the facts in the database. If you want to take a look, please >>>>> see the equivalent program in Jess for win-nowin that I attached. Just run >>>>> it with: >>>>> >> jess test.clp >>>>> >> >>>>> >> win_upper1_jess.jess >>>>> >> >>>>> >> (move (cur 1) (next 2)) >>>>> >> (move (cur 1) (next 3)) >>>>> >> (move (cur 2) (next 4)) >>>>> >> (move (cur 2) (next 5)) >>>>> >> ... >>>>> >> >>>>> >> win_upper2_jess.jess >>>>> >> >>>>> >> (move (cur 2) (next 4)) >>>>> >> (move (cur 2) (next 5)) >>>>> >> (move (cur 1) (next 2)) >>>>> >> (move (cur 1) (next 3)) >>>>> >> ... >>>>> >> >>>>> >> test.clp: >>>>> >> >>>>> >> (deftemplate move (slot cur) (slot next)) >>>>> >> (deftemplate win (slot val)) >>>>> >> >>>>> >> (defrule find_win >>>>> >> (move (cur ?cur) (next ?next)) >>>>> >> (not (win (val ?next))) >>>>> >> => >>>>> >> (assert (win (val ?cur))) >>>>> >> ) >>>>> >> >>>>> >> (defquery query-win >>>>> >> (win (val ?val)) >>>>> >> ) >>>>> >> (open "win_result.txt" output a) >>>>> >> (printout output ./win_upper1_jess.jess crlf) >>>>> >> (reset) >>>>> >> (load-facts "./win_upper1_jess.jess") >>>>> >> (bind ?tmx (call java.lang.management.ManagementFactory >>>>> getThreadMXBean)) >>>>> >> (deffunction cputime () (return (* (?tmx getCurrentThreadCpuTime) >>>>> 1E-9))) >>>>> >> (bind ?starttime_wall (time)) >>>>> >> (bind ?starttime_cpu (cputime)) >>>>> >> (run) >>>>> >> (bind ?query_result (run-query* query-win)) >>>>> >> (bind ?count 0) >>>>> >> (while (?query_result next) >>>>> >> (++ ?count) >>>>> >> ) >>>>> >> (printout output "solutions: " ?count crlf) >>>>> >> (bind ?endtime_cpu (cputime)) >>>>> >> (bind ?endtime_wall (time)) >>>>> >> (bind ?walltime (- ?endtime_wall ?starttime_wall)) >>>>> >> (bind ?cputime (- ?endtime_cpu ?starttime_cpu)) >>>>> >> (printout output "computing cputime: " ?cputime crlf) >>>>> >> (printout output "computing walltime: " ?walltime crlf) >>>>> >> (close output) >>>>> >> >>>>> >> Regards, >>>>> >> Paul Fodor. >>>>> >> >>>>> >> 2009/4/16 Edson Tirelli >>>>> >>> >>>>> >>>Ha, thanks a lot Greg. I need new glasses... he is actually >>>>> comparing with the parameter "second", but when creating the win fact, >>>>> using >>>>> the parameter "first". >>>>> >>> >>>>> >>> not Win(first == m.second) >>>>> >>> insert(new Win(m.first)); >>>>> >>> >>>>> >>>Yes, in this case the engine is working exactly as it should. >>>>> >>> >>>>> >>>Anyway, I added the (fixed) test case to the codebase, just in >>>>> case. :) >>>>> >>> >>>>> >>>Thanks, >>>>> >>>Edson >>>>> >>> >>>>> >>> 2009/4/16 Greg Barton >>>>> >>>> >>>>> >>>> You don't have to worry. The engine is acting as it should. >>>>> >>>> >>>>> >>>> The rule Paul had was this, a bit simplified for clarity: >>>>> >>>> >>>>> >>>> rule "direct" >>>>> >>>> when >>>>> >>>>m : Move() >>>>> >>>>not Win(first == m.second) >>>>> >>>> then >>>>> >>>>insert(new Win(m.first)); >>>>> >>>> end >>>>> >>>> >>>>> >>>> If the insertion order is [Move(1,2), Move(2,3)] then the rule >>>>> matches first on Move(2,3) and Win(2) is inserted. No other rule fires >>>>> because now Move(1,2) and Win(2) match up, removing the instantiation with >>>>> Move(1,2) from the agenda. >>>>> >>>> >>>>> >>>> If the insertion order is [Move(2,3), Move(1,2)] then the order is >>>>> this: >>>>> >>>> >>>>> >>>> matched Move(1,2) insert Win(1) >>>>> >>>> matched Move(2,3) insert Win(2) >>>>> >>>> >>>>> >>>> The insertion of Win(1) in the first firing does NOT prevent the >>>>> instantiation with Move(2,3) from then firing. >>>>> >>>> >>>>> >>>> So it's all good. :) Sample code and output attached. >>>>> >>>> >>>>> >>>> --- On Thu, 4/16/09, Greg Barton wrote: >>>>> >>>> >>>>> >>>> > From: Greg Barton >>>>> >>>> > Subject: Re: [rules-users] Negation semantics in Drools >>>>> >>>> > To: "Rules Users List" >>>>> >>>> > Date: Thursday, April 16, 2009, 8:50 PM >>>>> >>>> > It is on the latest snapshot release, >>>>> >>>> > 5.0.0.20090417.005612-483 >>>>> >>>> > >>>>> >>>> > --- On Thu, 4/16/09, Edson Tirelli >>>>> >>>> > wrote: >>>>> >>>> > >>>>> >>>> > > We need to investigate if that is still happening >>>>> >>>> > in >>>>> >>>> > > latest trunk. >>>>> >>>> > >>>>> >>>> > >>>>> >>>> > >>>>> >>>> > ___ >>>>> >>>> > rules-users mailing list >>>>> >>>> > rules-users@lists.jboss.org >>>>> >>>> > https://lists.jboss.org/mailman/listinfo/rules-users >>>>> >>>> >>>>> >>>> >>>>> >>>> >>>>> >>>> ___ >>>>> >>>> rules-users mailing list >>>>> >>>> rules-users@lists.jboss.org >>>>> >>>> https://lists.jboss.org/mailman/listinfo/rules-users >>>>> >>>> >>>>> >>> >>>>> >>> >>>>> >>> >>>>> >>> -- >>>>> >>> Edson Tirelli >>>>> >>> JBoss Drools Core Development >>>>> >>> JBoss, a division of Red Hat @ www.jboss.com >>>>> >>> >>>>> >>> ___ >>>>> >>> rules-users mailing list >>>>> >>> rules-users@lists.jboss.org >>>>> >>> https://lists.jboss.org/mailman/listinfo/rules-users >>>>> >>> >>>>> >> >>>>> >> >>>>> >> ___ >>>>> >> rules-users mailing list >>>>> >> rules-users@lists.jboss.org >>>>> >> https://lists.jboss.org/mailman/listinfo/rules-users >>>>> >> >>>>> > >>>>> > >>>>> > >>>>> > -- >>>>> > Edson Tirelli >>>>> > JBoss Drools Core Development >>>>> > JBoss, a division of Red Hat @ www.jboss.com >>>>> > >>>>> > ___ >>>>> > rules-users mailing list >>>>> > rules-users@lists.jboss.org >>>>> > https://lists.jboss.org/mailman/listinfo/rules-users >>>>> > >>>>> >>>>> >>>>> >>>>> ___ >>>>> rules-users mailing list >>>>> rules-users@lists.jboss.org >>>>> https://lists.jboss.org/mailman/listinfo/rules-users >>>>> >>>>> >>>> >>>> >>>> -- >>>> Edson Tirelli >>>> JBoss Drools Core Development >>>> JBoss, a division of Red Hat @ www.jboss.com >>>> >>>> ___ >>>> rules-users mailing list >>>> rules-users@lists.jboss.org >>>> https://lists.jboss.org/mailman/listinfo/rules-users >>>> >>>> >>> >>> ___ >>> rules-users mailing list >>> rules-users@lists.jboss.org >>> https://lists.jboss.org/mailman/listinfo/rules-users >>> >>> >> >> >> -- >> Edson Tirelli >> JBoss Drools Core Development >> JBoss, a division of Red Hat @ www.jboss.com >> >> ___ >> rules-users mailing list >> rules-users@lists.jboss.org >> https://lists.jboss.org/mailman/listinfo/rules-users >> >> > > ___ > rules-users mailing list > rules-users@lists.jboss.org > https://lists.jboss.org/mailman/listinfo/rules-users > > -- Edson Tirelli JBoss Drools Core Development JBoss, a division of Red Hat @ www.jboss.com ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Negation semantics in Drools
t;>> >> jess test.clp >>>> >> >>>> >> win_upper1_jess.jess >>>> >> >>>> >> (move (cur 1) (next 2)) >>>> >> (move (cur 1) (next 3)) >>>> >> (move (cur 2) (next 4)) >>>> >> (move (cur 2) (next 5)) >>>> >> ... >>>> >> >>>> >> win_upper2_jess.jess >>>> >> >>>> >> (move (cur 2) (next 4)) >>>> >> (move (cur 2) (next 5)) >>>> >> (move (cur 1) (next 2)) >>>> >> (move (cur 1) (next 3)) >>>> >> ... >>>> >> >>>> >> test.clp: >>>> >> >>>> >> (deftemplate move (slot cur) (slot next)) >>>> >> (deftemplate win (slot val)) >>>> >> >>>> >> (defrule find_win >>>> >> (move (cur ?cur) (next ?next)) >>>> >> (not (win (val ?next))) >>>> >> => >>>> >> (assert (win (val ?cur))) >>>> >> ) >>>> >> >>>> >> (defquery query-win >>>> >> (win (val ?val)) >>>> >> ) >>>> >> (open "win_result.txt" output a) >>>> >> (printout output ./win_upper1_jess.jess crlf) >>>> >> (reset) >>>> >> (load-facts "./win_upper1_jess.jess") >>>> >> (bind ?tmx (call java.lang.management.ManagementFactory >>>> getThreadMXBean)) >>>> >> (deffunction cputime () (return (* (?tmx getCurrentThreadCpuTime) >>>> 1E-9))) >>>> >> (bind ?starttime_wall (time)) >>>> >> (bind ?starttime_cpu (cputime)) >>>> >> (run) >>>> >> (bind ?query_result (run-query* query-win)) >>>> >> (bind ?count 0) >>>> >> (while (?query_result next) >>>> >> (++ ?count) >>>> >> ) >>>> >> (printout output "solutions: " ?count crlf) >>>> >> (bind ?endtime_cpu (cputime)) >>>> >> (bind ?endtime_wall (time)) >>>> >> (bind ?walltime (- ?endtime_wall ?starttime_wall)) >>>> >> (bind ?cputime (- ?endtime_cpu ?starttime_cpu)) >>>> >> (printout output "computing cputime: " ?cputime crlf) >>>> >> (printout output "computing walltime: " ?walltime crlf) >>>> >> (close output) >>>> >> >>>> >> Regards, >>>> >> Paul Fodor. >>>> >> >>>> >> 2009/4/16 Edson Tirelli >>>> >>> >>>> >>>Ha, thanks a lot Greg. I need new glasses... he is actually >>>> comparing with the parameter "second", but when creating the win fact, >>>> using >>>> the parameter "first". >>>> >>> >>>> >>> not Win(first == m.second) >>>> >>> insert(new Win(m.first)); >>>> >>> >>>> >>>Yes, in this case the engine is working exactly as it should. >>>> >>> >>>> >>>Anyway, I added the (fixed) test case to the codebase, just in >>>> case. :) >>>> >>> >>>> >>>Thanks, >>>> >>>Edson >>>> >>> >>>> >>> 2009/4/16 Greg Barton >>>> >>>> >>>> >>>> You don't have to worry. The engine is acting as it should. >>>> >>>> >>>> >>>> The rule Paul had was this, a bit simplified for clarity: >>>> >>>> >>>> >>>> rule "direct" >>>> >>>> when >>>> >>>>m : Move() >>>> >>>>not Win(first == m.second) >>>> >>>> then >>>> >>>>insert(new Win(m.first)); >>>> >>>> end >>>> >>>> >>>> >>>> If the insertion order is [Move(1,2), Move(2,3)] then the rule >>>> matches first on Move(2,3) and Win(2) is inserted. No other rule fires >>>> because now Move(1,2) and Win(2) match up, removing the instantiation with >>>> Move(1,2) from the agenda. >>>> >>>> >>>> >>>> If the insertion order is [Move(2,3), Move(1,2)] then the order is >>>> this: >>>> >>>> >>>> >>>> matched Move(1,2) insert Win(1) >>>> >>>> matched Move(2,3) insert Win(2) >>>> >>>> >>>> >>>> The insertion of Win(1) in the first firing does NOT prevent the >>>> instantiation with Move(2,3) from then firing. >>>> >>>> >>>> >>>> So it's all good. :) Sample code and output attached. >>>> >>>> >>>> >>>> --- On Thu, 4/16/09, Greg Barton wrote: >>>> >>>> >>>> >>>> > From: Greg Barton >>>> >>>> > Subject: Re: [rules-users] Negation semantics in Drools >>>> >>>> > To: "Rules Users List" >>>> >>>> > Date: Thursday, April 16, 2009, 8:50 PM >>>> >>>> > It is on the latest snapshot release, >>>> >>>> > 5.0.0.20090417.005612-483 >>>> >>>> > >>>> >>>> > --- On Thu, 4/16/09, Edson Tirelli >>>> >>>> > wrote: >>>> >>>> > >>>> >>>> > > We need to investigate if that is still happening >>>> >>>> > in >>>> >>>> > > latest trunk. >>>> >>>> > >>>> >>>> > >>>> >>>> > >>>> >>>> > ___ >>>> >>>> > rules-users mailing list >>>> >>>> > rules-users@lists.jboss.org >>>> >>>> > https://lists.jboss.org/mailman/listinfo/rules-users >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> ___ >>>> >>>> rules-users mailing list >>>> >>>> rules-users@lists.jboss.org >>>> >>>> https://lists.jboss.org/mailman/listinfo/rules-users >>>> >>>> >>>> >>> >>>> >>> >>>> >>> >>>> >>> -- >>>> >>> Edson Tirelli >>>> >>> JBoss Drools Core Development >>>> >>> JBoss, a division of Red Hat @ www.jboss.com >>>> >>> >>>> >>> ___ >>>> >>> rules-users mailing list >>>> >>> rules-users@lists.jboss.org >>>> >>> https://lists.jboss.org/mailman/listinfo/rules-users >>>> >>> >>>> >> >>>> >> >>>> >> ___ >>>> >> rules-users mailing list >>>> >> rules-users@lists.jboss.org >>>> >> https://lists.jboss.org/mailman/listinfo/rules-users >>>> >> >>>> > >>>> > >>>> > >>>> > -- >>>> > Edson Tirelli >>>> > JBoss Drools Core Development >>>> > JBoss, a division of Red Hat @ www.jboss.com >>>> > >>>> > ___ >>>> > rules-users mailing list >>>> > rules-users@lists.jboss.org >>>> > https://lists.jboss.org/mailman/listinfo/rules-users >>>> > >>>> >>>> >>>> >>>> ___ >>>> rules-users mailing list >>>> rules-users@lists.jboss.org >>>> https://lists.jboss.org/mailman/listinfo/rules-users >>>> >>>> >>> >>> >>> -- >>> Edson Tirelli >>> JBoss Drools Core Development >>> JBoss, a division of Red Hat @ www.jboss.com >>> >>> ___ >>> rules-users mailing list >>> rules-users@lists.jboss.org >>> https://lists.jboss.org/mailman/listinfo/rules-users >>> >>> >> >> ___ >> rules-users mailing list >> rules-users@lists.jboss.org >> https://lists.jboss.org/mailman/listinfo/rules-users >> >> > > > -- > Edson Tirelli > JBoss Drools Core Development > JBoss, a division of Red Hat @ www.jboss.com > > ___ > rules-users mailing list > rules-users@lists.jboss.org > https://lists.jboss.org/mailman/listinfo/rules-users > > ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Negation semantics in Drools
nal answer. In your example, with Move(1,2) and >>> Move(2,3), the final answer must be Win(2), right? And that is what Drools >>> will answer, does not matter the order in which the data is entered into the >>> engine. >>> > >>> >BUT, *very important*: the following construct in backward chaining: >>> > >>> > win(X):- move(X,Y), not(win(Y)). >>> > >>> > Is better represented in forward chaining using *logicalInsert* >>> instead of a regular *insert*: >>> > >>> > rule "direct" % Drools >>> > >>> > when >>> > m : Move(x : first, y : second) >>> > not Win(first == y) >>> > then >>> > logicalInsert(new Win(m.getFirst())); >>> > end >>> > >>> > Since in your backward chaining rule, only one win() predicate >>> instantiation will remain true. >>> > >>> > So, even with differences in the reasoning algorithm, the answer is >>> correct. >>> > >>> > Please explain further if I am missing anything. >>> > >>> > Edson >>> > >>> > >>> > 2009/4/17 Paul Fodor >>> >> >>> >> Hi Edson, Greg, >>> >> I don't think the rule is written wrong. This is how the win-nowin >>> program is written in logic programming: X wins if there is a move from X to >>> some Y and Y doesn't win: >>> >> >>> >> win(X):- move(X,Y), not(win(Y)). >>> >> >>> >> rule "direct" % Drools >>> >> >>> >> when >>> >> m : Move(x : first, y : second) >>> >> not Win(first == y) >>> >> then >>> >> insert(new Win(m.getFirst())); >>> >> end >>> >> >>> >> I think that it's interesting that, in Jess (another production rule >>> system), the stratified model is always computed right, no matter what was >>> the order of the facts in the database. If you want to take a look, please >>> see the equivalent program in Jess for win-nowin that I attached. Just run >>> it with: >>> >> jess test.clp >>> >> >>> >> win_upper1_jess.jess >>> >> >>> >> (move (cur 1) (next 2)) >>> >> (move (cur 1) (next 3)) >>> >> (move (cur 2) (next 4)) >>> >> (move (cur 2) (next 5)) >>> >> ... >>> >> >>> >> win_upper2_jess.jess >>> >> >>> >> (move (cur 2) (next 4)) >>> >> (move (cur 2) (next 5)) >>> >> (move (cur 1) (next 2)) >>> >> (move (cur 1) (next 3)) >>> >> ... >>> >> >>> >> test.clp: >>> >> >>> >> (deftemplate move (slot cur) (slot next)) >>> >> (deftemplate win (slot val)) >>> >> >>> >> (defrule find_win >>> >> (move (cur ?cur) (next ?next)) >>> >> (not (win (val ?next))) >>> >> => >>> >> (assert (win (val ?cur))) >>> >> ) >>> >> >>> >> (defquery query-win >>> >> (win (val ?val)) >>> >> ) >>> >> (open "win_result.txt" output a) >>> >> (printout output ./win_upper1_jess.jess crlf) >>> >> (reset) >>> >> (load-facts "./win_upper1_jess.jess") >>> >> (bind ?tmx (call java.lang.management.ManagementFactory >>> getThreadMXBean)) >>> >> (deffunction cputime () (return (* (?tmx getCurrentThreadCpuTime) >>> 1E-9))) >>> >> (bind ?starttime_wall (time)) >>> >> (bind ?starttime_cpu (cputime)) >>> >> (run) >>> >> (bind ?query_result (run-query* query-win)) >>> >> (bind ?count 0) >>> >> (while (?query_result next) >>> >> (++ ?count) >>> >> ) >>> >> (printout output "solutions: " ?count crlf) >>> >> (bind ?endtime_cpu (cputime)) >>> >> (bind ?endtime_wall (time)) >>> >> (bind ?walltime (- ?endtime_wall ?starttime_wall)) >>> >> (bind ?cputime (- ?endtime_cpu ?starttime_cpu)) >>> >> (printout output "computing cputime: " ?cputime crlf) &
Re: [rules-users] Negation semantics in Drools
gt;> > >> > 2009/4/17 Paul Fodor >> >> >> >> Hi Edson, Greg, >> >> I don't think the rule is written wrong. This is how the win-nowin >> program is written in logic programming: X wins if there is a move from X to >> some Y and Y doesn't win: >> >> >> >> win(X):- move(X,Y), not(win(Y)). >> >> >> >> rule "direct" % Drools >> >> >> >> when >> >> m : Move(x : first, y : second) >> >> not Win(first == y) >> >> then >> >> insert(new Win(m.getFirst())); >> >> end >> >> >> >> I think that it's interesting that, in Jess (another production rule >> system), the stratified model is always computed right, no matter what was >> the order of the facts in the database. If you want to take a look, please >> see the equivalent program in Jess for win-nowin that I attached. Just run >> it with: >> >> jess test.clp >> >> >> >> win_upper1_jess.jess >> >> >> >> (move (cur 1) (next 2)) >> >> (move (cur 1) (next 3)) >> >> (move (cur 2) (next 4)) >> >> (move (cur 2) (next 5)) >> >> ... >> >> >> >> win_upper2_jess.jess >> >> >> >> (move (cur 2) (next 4)) >> >> (move (cur 2) (next 5)) >> >> (move (cur 1) (next 2)) >> >> (move (cur 1) (next 3)) >> >> ... >> >> >> >> test.clp: >> >> >> >> (deftemplate move (slot cur) (slot next)) >> >> (deftemplate win (slot val)) >> >> >> >> (defrule find_win >> >> (move (cur ?cur) (next ?next)) >> >> (not (win (val ?next))) >> >> => >> >> (assert (win (val ?cur))) >> >> ) >> >> >> >> (defquery query-win >> >> (win (val ?val)) >> >> ) >> >> (open "win_result.txt" output a) >> >> (printout output ./win_upper1_jess.jess crlf) >> >> (reset) >> >> (load-facts "./win_upper1_jess.jess") >> >> (bind ?tmx (call java.lang.management.ManagementFactory >> getThreadMXBean)) >> >> (deffunction cputime () (return (* (?tmx getCurrentThreadCpuTime) >> 1E-9))) >> >> (bind ?starttime_wall (time)) >> >> (bind ?starttime_cpu (cputime)) >> >> (run) >> >> (bind ?query_result (run-query* query-win)) >> >> (bind ?count 0) >> >> (while (?query_result next) >> >> (++ ?count) >> >> ) >> >> (printout output "solutions: " ?count crlf) >> >> (bind ?endtime_cpu (cputime)) >> >> (bind ?endtime_wall (time)) >> >> (bind ?walltime (- ?endtime_wall ?starttime_wall)) >> >> (bind ?cputime (- ?endtime_cpu ?starttime_cpu)) >> >> (printout output "computing cputime: " ?cputime crlf) >> >> (printout output "computing walltime: " ?walltime crlf) >> >> (close output) >> >> >> >> Regards, >> >> Paul Fodor. >> >> >> >> 2009/4/16 Edson Tirelli >> >>> >> >>>Ha, thanks a lot Greg. I need new glasses... he is actually >> comparing with the parameter "second", but when creating the win fact, using >> the parameter "first". >> >>> >> >>> not Win(first == m.second) >> >>> insert(new Win(m.first)); >> >>> >> >>>Yes, in this case the engine is working exactly as it should. >> >>> >> >>>Anyway, I added the (fixed) test case to the codebase, just in >> case. :) >> >>> >> >>>Thanks, >> >>>Edson >> >>> >> >>> 2009/4/16 Greg Barton >> >>>> >> >>>> You don't have to worry. The engine is acting as it should. >> >>>> >> >>>> The rule Paul had was this, a bit simplified for clarity: >> >>>> >> >>>> rule "direct" >> >>>> when >> >>>>m : Move() >> >>>>not Win(first == m.second) >> >>>> then >> >>>>insert(new Win(m.first)); >> >>>> end >> >>>> >> >>>> If the insertion order is [Move(1,2), Move(2,3)] then the rule >> ma
Re: [rules-users] Negation semantics in Drools
; >> (move (cur 2) (next 5)) > >> (move (cur 1) (next 2)) > >> (move (cur 1) (next 3)) > >> ... > >> > >> test.clp: > >> > >> (deftemplate move (slot cur) (slot next)) > >> (deftemplate win (slot val)) > >> > >> (defrule find_win > >> (move (cur ?cur) (next ?next)) > >> (not (win (val ?next))) > >> => > >> (assert (win (val ?cur))) > >> ) > >> > >> (defquery query-win > >> (win (val ?val)) > >> ) > >> (open "win_result.txt" output a) > >> (printout output ./win_upper1_jess.jess crlf) > >> (reset) > >> (load-facts "./win_upper1_jess.jess") > >> (bind ?tmx (call java.lang.management.ManagementFactory > getThreadMXBean)) > >> (deffunction cputime () (return (* (?tmx getCurrentThreadCpuTime) > 1E-9))) > >> (bind ?starttime_wall (time)) > >> (bind ?starttime_cpu (cputime)) > >> (run) > >> (bind ?query_result (run-query* query-win)) > >> (bind ?count 0) > >> (while (?query_result next) > >> (++ ?count) > >> ) > >> (printout output "solutions: " ?count crlf) > >> (bind ?endtime_cpu (cputime)) > >> (bind ?endtime_wall (time)) > >> (bind ?walltime (- ?endtime_wall ?starttime_wall)) > >> (bind ?cputime (- ?endtime_cpu ?starttime_cpu)) > >> (printout output "computing cputime: " ?cputime crlf) > >> (printout output "computing walltime: " ?walltime crlf) > >> (close output) > >> > >> Regards, > >> Paul Fodor. > >> > >> 2009/4/16 Edson Tirelli > >>> > >>>Ha, thanks a lot Greg. I need new glasses... he is actually > comparing with the parameter "second", but when creating the win fact, using > the parameter "first". > >>> > >>> not Win(first == m.second) > >>> insert(new Win(m.first)); > >>> > >>>Yes, in this case the engine is working exactly as it should. > >>> > >>>Anyway, I added the (fixed) test case to the codebase, just in case. > :) > >>> > >>>Thanks, > >>>Edson > >>> > >>> 2009/4/16 Greg Barton > >>>> > >>>> You don't have to worry. The engine is acting as it should. > >>>> > >>>> The rule Paul had was this, a bit simplified for clarity: > >>>> > >>>> rule "direct" > >>>> when > >>>>m : Move() > >>>>not Win(first == m.second) > >>>> then > >>>>insert(new Win(m.first)); > >>>> end > >>>> > >>>> If the insertion order is [Move(1,2), Move(2,3)] then the rule matches > first on Move(2,3) and Win(2) is inserted. No other rule fires because now > Move(1,2) and Win(2) match up, removing the instantiation with Move(1,2) > from the agenda. > >>>> > >>>> If the insertion order is [Move(2,3), Move(1,2)] then the order is > this: > >>>> > >>>> matched Move(1,2) insert Win(1) > >>>> matched Move(2,3) insert Win(2) > >>>> > >>>> The insertion of Win(1) in the first firing does NOT prevent the > instantiation with Move(2,3) from then firing. > >>>> > >>>> So it's all good. :) Sample code and output attached. > >>>> > >>>> --- On Thu, 4/16/09, Greg Barton wrote: > >>>> > >>>> > From: Greg Barton > >>>> > Subject: Re: [rules-users] Negation semantics in Drools > >>>> > To: "Rules Users List" > >>>> > Date: Thursday, April 16, 2009, 8:50 PM > >>>> > It is on the latest snapshot release, > >>>> > 5.0.0.20090417.005612-483 > >>>> > > >>>> > --- On Thu, 4/16/09, Edson Tirelli > >>>> > wrote: > >>>> > > >>>> > > We need to investigate if that is still happening > >>>> > in > >>>> > > latest trunk. > >>>> > > >>>> > > >>>> > > >>>> > ___ > >>>> > rules-users mailing list > >>>> > rules-users@lists.jboss.org > >>>> > https://lists.jboss.org/mailman/listinfo/rules-users > >>>> > >>>> > >>>> > >>>> ___ > >>>> rules-users mailing list > >>>> rules-users@lists.jboss.org > >>>> https://lists.jboss.org/mailman/listinfo/rules-users > >>>> > >>> > >>> > >>> > >>> -- > >>> Edson Tirelli > >>> JBoss Drools Core Development > >>> JBoss, a division of Red Hat @ www.jboss.com > >>> > >>> ___ > >>> rules-users mailing list > >>> rules-users@lists.jboss.org > >>> https://lists.jboss.org/mailman/listinfo/rules-users > >>> > >> > >> > >> ___ > >> rules-users mailing list > >> rules-users@lists.jboss.org > >> https://lists.jboss.org/mailman/listinfo/rules-users > >> > > > > > > > > -- > > Edson Tirelli > > JBoss Drools Core Development > > JBoss, a division of Red Hat @ www.jboss.com > > > > ___ > > rules-users mailing list > > rules-users@lists.jboss.org > > https://lists.jboss.org/mailman/listinfo/rules-users > > > > > > ___ > rules-users mailing list > rules-users@lists.jboss.org > https://lists.jboss.org/mailman/listinfo/rules-users > > -- Edson Tirelli JBoss Drools Core Development JBoss, a division of Red Hat @ www.jboss.com ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Negation semantics in Drools
n > >> (move (cur ?cur) (next ?next)) > >> (not (win (val ?next))) > >> => > >> (assert (win (val ?cur))) > >> ) > >> > >> (defquery query-win > >> (win (val ?val)) > >> ) > >> (open "win_result.txt" output a) > >> (printout output ./win_upper1_jess.jess crlf) > >> (reset) > >> (load-facts "./win_upper1_jess.jess") > >> (bind ?tmx (call java.lang.management.ManagementFactory > getThreadMXBean)) > >> (deffunction cputime () (return (* (?tmx getCurrentThreadCpuTime) > 1E-9))) > >> (bind ?starttime_wall (time)) > >> (bind ?starttime_cpu (cputime)) > >> (run) > >> (bind ?query_result (run-query* query-win)) > >> (bind ?count 0) > >> (while (?query_result next) > >> (++ ?count) > >> ) > >> (printout output "solutions: " ?count crlf) > >> (bind ?endtime_cpu (cputime)) > >> (bind ?endtime_wall (time)) > >> (bind ?walltime (- ?endtime_wall ?starttime_wall)) > >> (bind ?cputime (- ?endtime_cpu ?starttime_cpu)) > >> (printout output "computing cputime: " ?cputime crlf) > >> (printout output "computing walltime: " ?walltime crlf) > >> (close output) > >> > >> Regards, > >> Paul Fodor. > >> > >> 2009/4/16 Edson Tirelli > >>> > >>>Ha, thanks a lot Greg. I need new glasses... he is actually > comparing with the parameter "second", but when creating the win fact, using > the parameter "first". > >>> > >>> not Win(first == m.second) > >>> insert(new Win(m.first)); > >>> > >>>Yes, in this case the engine is working exactly as it should. > >>> > >>>Anyway, I added the (fixed) test case to the codebase, just in case. > :) > >>> > >>>Thanks, > >>>Edson > >>> > >>> 2009/4/16 Greg Barton > >>>> > >>>> You don't have to worry. The engine is acting as it should. > >>>> > >>>> The rule Paul had was this, a bit simplified for clarity: > >>>> > >>>> rule "direct" > >>>> when > >>>>m : Move() > >>>>not Win(first == m.second) > >>>> then > >>>>insert(new Win(m.first)); > >>>> end > >>>> > >>>> If the insertion order is [Move(1,2), Move(2,3)] then the rule matches > first on Move(2,3) and Win(2) is inserted. No other rule fires because now > Move(1,2) and Win(2) match up, removing the instantiation with Move(1,2) > from the agenda. > >>>> > >>>> If the insertion order is [Move(2,3), Move(1,2)] then the order is > this: > >>>> > >>>> matched Move(1,2) insert Win(1) > >>>> matched Move(2,3) insert Win(2) > >>>> > >>>> The insertion of Win(1) in the first firing does NOT prevent the > instantiation with Move(2,3) from then firing. > >>>> > >>>> So it's all good. :) Sample code and output attached. > >>>> > >>>> --- On Thu, 4/16/09, Greg Barton wrote: > >>>> > >>>> > From: Greg Barton > >>>> > Subject: Re: [rules-users] Negation semantics in Drools > >>>> > To: "Rules Users List" > >>>> > Date: Thursday, April 16, 2009, 8:50 PM > >>>> > It is on the latest snapshot release, > >>>> > 5.0.0.20090417.005612-483 > >>>> > > >>>> > --- On Thu, 4/16/09, Edson Tirelli > >>>> > wrote: > >>>> > > >>>> > > We need to investigate if that is still happening > >>>> > in > >>>> > > latest trunk. > >>>> > > >>>> > > >>>> > > >>>> > ___ > >>>> > rules-users mailing list > >>>> > rules-users@lists.jboss.org > >>>> > https://lists.jboss.org/mailman/listinfo/rules-users > >>>> > >>>> > >>>> > >>>> ___ > >>>> rules-users mailing list > >>>> rules-users@lists.jboss.org > >>>> https://lists.jboss.org/mailman/listinfo/rules-users > >>>> > >>> > >>> > >>> > >>> -- > >>> Edson Tirelli > >>> JBoss Drools Core Development > >>> JBoss, a division of Red Hat @ www.jboss.com > >>> > >>> ___ > >>> rules-users mailing list > >>> rules-users@lists.jboss.org > >>> https://lists.jboss.org/mailman/listinfo/rules-users > >>> > >> > >> > >> ___ > >> rules-users mailing list > >> rules-users@lists.jboss.org > >> https://lists.jboss.org/mailman/listinfo/rules-users > >> > > > > > > > > -- > > Edson Tirelli > > JBoss Drools Core Development > > JBoss, a division of Red Hat @ www.jboss.com > > > > ___ > > rules-users mailing list > > rules-users@lists.jboss.org > > https://lists.jboss.org/mailman/listinfo/rules-users > > > > > ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Negation semantics in Drools
time crlf) >> (close output) >> >> Regards, >> Paul Fodor. >> >> 2009/4/16 Edson Tirelli >>> >>>Ha, thanks a lot Greg. I need new glasses... he is actually comparing with the parameter "second", but when creating the win fact, using the parameter "first". >>> >>> not Win(first == m.second) >>> insert(new Win(m.first)); >>> >>>Yes, in this case the engine is working exactly as it should. >>> >>>Anyway, I added the (fixed) test case to the codebase, just in case. :) >>> >>>Thanks, >>>Edson >>> >>> 2009/4/16 Greg Barton >>>> >>>> You don't have to worry. The engine is acting as it should. >>>> >>>> The rule Paul had was this, a bit simplified for clarity: >>>> >>>> rule "direct" >>>> when >>>>m : Move() >>>>not Win(first == m.second) >>>> then >>>>insert(new Win(m.first)); >>>> end >>>> >>>> If the insertion order is [Move(1,2), Move(2,3)] then the rule matches first on Move(2,3) and Win(2) is inserted. No other rule fires because now Move(1,2) and Win(2) match up, removing the instantiation with Move(1,2) from the agenda. >>>> >>>> If the insertion order is [Move(2,3), Move(1,2)] then the order is this: >>>> >>>> matched Move(1,2) insert Win(1) >>>> matched Move(2,3) insert Win(2) >>>> >>>> The insertion of Win(1) in the first firing does NOT prevent the instantiation with Move(2,3) from then firing. >>>> >>>> So it's all good. :) Sample code and output attached. >>>> >>>> --- On Thu, 4/16/09, Greg Barton wrote: >>>> >>>> > From: Greg Barton >>>> > Subject: Re: [rules-users] Negation semantics in Drools >>>> > To: "Rules Users List" >>>> > Date: Thursday, April 16, 2009, 8:50 PM >>>> > It is on the latest snapshot release, >>>> > 5.0.0.20090417.005612-483 >>>> > >>>> > --- On Thu, 4/16/09, Edson Tirelli >>>> > wrote: >>>> > >>>> > > We need to investigate if that is still happening >>>> > in >>>> > > latest trunk. >>>> > >>>> > >>>> > >>>> > ___ >>>> > rules-users mailing list >>>> > rules-users@lists.jboss.org >>>> > https://lists.jboss.org/mailman/listinfo/rules-users >>>> >>>> >>>> >>>> ___ >>>> rules-users mailing list >>>> rules-users@lists.jboss.org >>>> https://lists.jboss.org/mailman/listinfo/rules-users >>>> >>> >>> >>> >>> -- >>> Edson Tirelli >>> JBoss Drools Core Development >>> JBoss, a division of Red Hat @ www.jboss.com >>> >>> ___ >>> rules-users mailing list >>> rules-users@lists.jboss.org >>> https://lists.jboss.org/mailman/listinfo/rules-users >>> >> >> >> ___ >> rules-users mailing list >> rules-users@lists.jboss.org >> https://lists.jboss.org/mailman/listinfo/rules-users >> > > > > -- > Edson Tirelli > JBoss Drools Core Development > JBoss, a division of Red Hat @ www.jboss.com > > ___ > rules-users mailing list > rules-users@lists.jboss.org > https://lists.jboss.org/mailman/listinfo/rules-users > ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Negation semantics in Drools
I see. Thank you, Paul. 2009/4/17 Edson Tirelli > >I did not had time to analyze what jess is doing, but note that what is > important is the final answer. In your example, with Move(1,2) and > Move(2,3), the final answer must be Win(2), right? And that is what Drools > will answer, does not matter the order in which the data is entered into the > engine. > >BUT, *very important*: the following construct in backward chaining: > > win(X):- move(X,Y), not(win(Y)). > > Is better represented in forward chaining using *logicalInsert* instead > of a regular *insert*: > > rule "direct" % Drools > when > m : Move(x : first, y : second) > not Win(first == y) > then > logicalInsert(new Win(m.getFirst())); > end > > Since in your backward chaining rule, only one win() predicate > instantiation will remain true. > > So, even with differences in the reasoning algorithm, the answer is > correct. > > Please explain further if I am missing anything. > > Edson > > > 2009/4/17 Paul Fodor > > Hi Edson, Greg, >> I don't think the rule is written wrong. This is how the win-nowin program >> is written in logic programming: X wins if there is a move from X to some Y >> and Y doesn't win: >> win(X):- move(X,Y), not(win(Y)). >> >> rule "direct" % Drools >> >> when >> m : Move(x : first, y : second) >> not Win(first == y) >> then >> insert(new Win(m.getFirst())); >> end >> >> I think that it's interesting that, in Jess (another production rule >> system), the stratified model is always computed right, no matter what was >> the order of the facts in the database. If you want to take a look, please >> see the equivalent program in Jess for win-nowin that I attached. Just run >> it with: >> jess test.clp >> >> win_upper1_jess.jess >> >> (move (cur 1) (next 2)) >> (move (cur 1) (next 3)) >> (move (cur 2) (next 4)) >> (move (cur 2) (next 5)) >> ... >> >> win_upper2_jess.jess >> >> (move (cur 2) (next 4)) >> (move (cur 2) (next 5)) >> (move (cur 1) (next 2)) >> (move (cur 1) (next 3)) >> ... >> >> test.clp: >> >> (deftemplate move (slot cur) (slot next)) >> (deftemplate win (slot val)) >> >> (defrule find_win >> (move (cur ?cur) (next ?next)) >> (not (win (val ?next))) >> => >> (assert (win (val ?cur))) >> ) >> >> (defquery query-win >> (win (val ?val)) >> ) >> (open "win_result.txt" output a) >> (printout output ./win_upper1_jess.jess crlf) >> (reset) >> (load-facts "./win_upper1_jess.jess") >> (bind ?tmx (call java.lang.management.ManagementFactory getThreadMXBean)) >> (deffunction cputime () (return (* (?tmx getCurrentThreadCpuTime) 1E-9))) >> (bind ?starttime_wall (time)) >> (bind ?starttime_cpu (cputime)) >> (run) >> (bind ?query_result (run-query* query-win)) >> (bind ?count 0) >> (while (?query_result next) >> (++ ?count) >> ) >> (printout output "solutions: " ?count crlf) >> (bind ?endtime_cpu (cputime)) >> (bind ?endtime_wall (time)) >> (bind ?walltime (- ?endtime_wall ?starttime_wall)) >> (bind ?cputime (- ?endtime_cpu ?starttime_cpu)) >> (printout output "computing cputime: " ?cputime crlf) >> (printout output "computing walltime: " ?walltime crlf) >> (close output) >> >> Regards, >> Paul Fodor. >> 2009/4/16 Edson Tirelli >> >> >>>Ha, thanks a lot Greg. I need new glasses... he is actually comparing >>> with the parameter "second", but when creating the win fact, using the >>> parameter "first". >>> >>> not Win(first == m.second) >>> insert(new Win(m.first)); >>> >>>Yes, in this case the engine is working exactly as it should. >>> >>>Anyway, I added the (fixed) test case to the codebase, just in case. >>> :) >>> >>>Thanks, >>>Edson >>> >>> 2009/4/16 Greg Barton >>> >>> You don't have to worry. The engine is acting as it should. >>>> >>>> The rule Paul had was this, a bit simplified for clarity: >>>> >>>> rule "direct" >>>> when >>>>m : Move() >>>>not Win(first == m.second) >>>> then >>>>insert(new W
Re: [rules-users] Negation semantics in Drools
I did not had time to analyze what jess is doing, but note that what is important is the final answer. In your example, with Move(1,2) and Move(2,3), the final answer must be Win(2), right? And that is what Drools will answer, does not matter the order in which the data is entered into the engine. BUT, *very important*: the following construct in backward chaining: win(X):- move(X,Y), not(win(Y)). Is better represented in forward chaining using *logicalInsert* instead of a regular *insert*: rule "direct" % Drools when m : Move(x : first, y : second) not Win(first == y) then logicalInsert(new Win(m.getFirst())); end Since in your backward chaining rule, only one win() predicate instantiation will remain true. So, even with differences in the reasoning algorithm, the answer is correct. Please explain further if I am missing anything. Edson 2009/4/17 Paul Fodor > Hi Edson, Greg, > I don't think the rule is written wrong. This is how the win-nowin program > is written in logic programming: X wins if there is a move from X to some Y > and Y doesn't win: > win(X):- move(X,Y), not(win(Y)). > > rule "direct" % Drools > > when > m : Move(x : first, y : second) > not Win(first == y) > then > insert(new Win(m.getFirst())); > end > > I think that it's interesting that, in Jess (another production rule > system), the stratified model is always computed right, no matter what was > the order of the facts in the database. If you want to take a look, please > see the equivalent program in Jess for win-nowin that I attached. Just run > it with: > jess test.clp > > win_upper1_jess.jess > > (move (cur 1) (next 2)) > (move (cur 1) (next 3)) > (move (cur 2) (next 4)) > (move (cur 2) (next 5)) > ... > > win_upper2_jess.jess > > (move (cur 2) (next 4)) > (move (cur 2) (next 5)) > (move (cur 1) (next 2)) > (move (cur 1) (next 3)) > ... > > test.clp: > > (deftemplate move (slot cur) (slot next)) > (deftemplate win (slot val)) > > (defrule find_win > (move (cur ?cur) (next ?next)) > (not (win (val ?next))) > => > (assert (win (val ?cur))) > ) > > (defquery query-win > (win (val ?val)) > ) > (open "win_result.txt" output a) > (printout output ./win_upper1_jess.jess crlf) > (reset) > (load-facts "./win_upper1_jess.jess") > (bind ?tmx (call java.lang.management.ManagementFactory getThreadMXBean)) > (deffunction cputime () (return (* (?tmx getCurrentThreadCpuTime) 1E-9))) > (bind ?starttime_wall (time)) > (bind ?starttime_cpu (cputime)) > (run) > (bind ?query_result (run-query* query-win)) > (bind ?count 0) > (while (?query_result next) > (++ ?count) > ) > (printout output "solutions: " ?count crlf) > (bind ?endtime_cpu (cputime)) > (bind ?endtime_wall (time)) > (bind ?walltime (- ?endtime_wall ?starttime_wall)) > (bind ?cputime (- ?endtime_cpu ?starttime_cpu)) > (printout output "computing cputime: " ?cputime crlf) > (printout output "computing walltime: " ?walltime crlf) > (close output) > > Regards, > Paul Fodor. > 2009/4/16 Edson Tirelli > > >>Ha, thanks a lot Greg. I need new glasses... he is actually comparing >> with the parameter "second", but when creating the win fact, using the >> parameter "first". >> >> not Win(first == m.second) >> insert(new Win(m.first)); >> >>Yes, in this case the engine is working exactly as it should. >> >>Anyway, I added the (fixed) test case to the codebase, just in case. :) >> >>Thanks, >>Edson >> >> 2009/4/16 Greg Barton >> >> You don't have to worry. The engine is acting as it should. >>> >>> The rule Paul had was this, a bit simplified for clarity: >>> >>> rule "direct" >>> when >>>m : Move() >>>not Win(first == m.second) >>> then >>>insert(new Win(m.first)); >>> end >>> >>> If the insertion order is [Move(1,2), Move(2,3)] then the rule matches >>> first on Move(2,3) and Win(2) is inserted. No other rule fires because now >>> Move(1,2) and Win(2) match up, removing the instantiation with Move(1,2) >>> from the agenda. >>> >>> If the insertion order is [Move(2,3), Move(1,2)] then the order is this: >>> >>> matched Move(1,2) insert Win(1) >>> matched Move(2,3) insert Win(2) >>> >>> The insertion of Win(1) in the first firing does NOT prevent the >>> instanti
Re: [rules-users] Negation semantics in Drools
Hi Edson, Greg, I don't think the rule is written wrong. This is how the win-nowin program is written in logic programming: X wins if there is a move from X to some Y and Y doesn't win: win(X):- move(X,Y), not(win(Y)). rule "direct" % Drools when m : Move(x : first, y : second) not Win(first == y) then insert(new Win(m.getFirst())); end I think that it's interesting that, in Jess (another production rule system), the stratified model is always computed right, no matter what was the order of the facts in the database. If you want to take a look, please see the equivalent program in Jess for win-nowin that I attached. Just run it with: jess test.clp win_upper1_jess.jess (move (cur 1) (next 2)) (move (cur 1) (next 3)) (move (cur 2) (next 4)) (move (cur 2) (next 5)) ... win_upper2_jess.jess (move (cur 2) (next 4)) (move (cur 2) (next 5)) (move (cur 1) (next 2)) (move (cur 1) (next 3)) ... test.clp: (deftemplate move (slot cur) (slot next)) (deftemplate win (slot val)) (defrule find_win (move (cur ?cur) (next ?next)) (not (win (val ?next))) => (assert (win (val ?cur))) ) (defquery query-win (win (val ?val)) ) (open "win_result.txt" output a) (printout output ./win_upper1_jess.jess crlf) (reset) (load-facts "./win_upper1_jess.jess") (bind ?tmx (call java.lang.management.ManagementFactory getThreadMXBean)) (deffunction cputime () (return (* (?tmx getCurrentThreadCpuTime) 1E-9))) (bind ?starttime_wall (time)) (bind ?starttime_cpu (cputime)) (run) (bind ?query_result (run-query* query-win)) (bind ?count 0) (while (?query_result next) (++ ?count) ) (printout output "solutions: " ?count crlf) (bind ?endtime_cpu (cputime)) (bind ?endtime_wall (time)) (bind ?walltime (- ?endtime_wall ?starttime_wall)) (bind ?cputime (- ?endtime_cpu ?starttime_cpu)) (printout output "computing cputime: " ?cputime crlf) (printout output "computing walltime: " ?walltime crlf) (close output) Regards, Paul Fodor. 2009/4/16 Edson Tirelli > >Ha, thanks a lot Greg. I need new glasses... he is actually comparing > with the parameter "second", but when creating the win fact, using the > parameter "first". > > not Win(first == m.second) > insert(new Win(m.first)); > >Yes, in this case the engine is working exactly as it should. > >Anyway, I added the (fixed) test case to the codebase, just in case. :) > >Thanks, >Edson > > 2009/4/16 Greg Barton > > You don't have to worry. The engine is acting as it should. >> >> The rule Paul had was this, a bit simplified for clarity: >> >> rule "direct" >> when >>m : Move() >>not Win(first == m.second) >> then >>insert(new Win(m.first)); >> end >> >> If the insertion order is [Move(1,2), Move(2,3)] then the rule matches >> first on Move(2,3) and Win(2) is inserted. No other rule fires because now >> Move(1,2) and Win(2) match up, removing the instantiation with Move(1,2) >> from the agenda. >> >> If the insertion order is [Move(2,3), Move(1,2)] then the order is this: >> >> matched Move(1,2) insert Win(1) >> matched Move(2,3) insert Win(2) >> >> The insertion of Win(1) in the first firing does NOT prevent the >> instantiation with Move(2,3) from then firing. >> >> So it's all good. :) Sample code and output attached. >> >> --- On Thu, 4/16/09, Greg Barton wrote: >> >> > From: Greg Barton >> > Subject: Re: [rules-users] Negation semantics in Drools >> > To: "Rules Users List" >> > Date: Thursday, April 16, 2009, 8:50 PM >> > It is on the latest snapshot release, >> > 5.0.0.20090417.005612-483 >> > >> > --- On Thu, 4/16/09, Edson Tirelli >> > wrote: >> > >> > > We need to investigate if that is still happening >> > in >> > > latest trunk. >> > >> > >> > >> > ___ >> > rules-users mailing list >> > rules-users@lists.jboss.org >> > https://lists.jboss.org/mailman/listinfo/rules-users >> >> >> >> >> ___ >> rules-users mailing list >> rules-users@lists.jboss.org >> https://lists.jboss.org/mailman/listinfo/rules-users >> >> > > > -- > Edson Tirelli > JBoss Drools Core Development > JBoss, a division of Red Hat @ www.jboss.com > > ___ > rules-users mailing list > rules-users@lists.jboss.org > https://lists.jboss.org/mailman/listinfo/rules-users > > ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Negation semantics in Drools
Ha, thanks a lot Greg. I need new glasses... he is actually comparing with the parameter "second", but when creating the win fact, using the parameter "first". not Win(first == m.second) insert(new Win(m.first)); Yes, in this case the engine is working exactly as it should. Anyway, I added the (fixed) test case to the codebase, just in case. :) Thanks, Edson 2009/4/16 Greg Barton > You don't have to worry. The engine is acting as it should. > > The rule Paul had was this, a bit simplified for clarity: > > rule "direct" > when >m : Move() >not Win(first == m.second) > then >insert(new Win(m.first)); > end > > If the insertion order is [Move(1,2), Move(2,3)] then the rule matches > first on Move(2,3) and Win(2) is inserted. No other rule fires because now > Move(1,2) and Win(2) match up, removing the instantiation with Move(1,2) > from the agenda. > > If the insertion order is [Move(2,3), Move(1,2)] then the order is this: > > matched Move(1,2) insert Win(1) > matched Move(2,3) insert Win(2) > > The insertion of Win(1) in the first firing does NOT prevent the > instantiation with Move(2,3) from then firing. > > So it's all good. :) Sample code and output attached. > > --- On Thu, 4/16/09, Greg Barton wrote: > > > From: Greg Barton > > Subject: Re: [rules-users] Negation semantics in Drools > > To: "Rules Users List" > > Date: Thursday, April 16, 2009, 8:50 PM > > It is on the latest snapshot release, > > 5.0.0.20090417.005612-483 > > > > --- On Thu, 4/16/09, Edson Tirelli > > wrote: > > > > > We need to investigate if that is still happening > > in > > > latest trunk. > > > > > > > > ___ > > rules-users mailing list > > rules-users@lists.jboss.org > > https://lists.jboss.org/mailman/listinfo/rules-users > > > > > ___ > rules-users mailing list > rules-users@lists.jboss.org > https://lists.jboss.org/mailman/listinfo/rules-users > > -- Edson Tirelli JBoss Drools Core Development JBoss, a division of Red Hat @ www.jboss.com ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Negation semantics in Drools
You don't have to worry. The engine is acting as it should. The rule Paul had was this, a bit simplified for clarity: rule "direct" when m : Move() not Win(first == m.second) then insert(new Win(m.first)); end If the insertion order is [Move(1,2), Move(2,3)] then the rule matches first on Move(2,3) and Win(2) is inserted. No other rule fires because now Move(1,2) and Win(2) match up, removing the instantiation with Move(1,2) from the agenda. If the insertion order is [Move(2,3), Move(1,2)] then the order is this: matched Move(1,2) insert Win(1) matched Move(2,3) insert Win(2) The insertion of Win(1) in the first firing does NOT prevent the instantiation with Move(2,3) from then firing. So it's all good. :) Sample code and output attached. --- On Thu, 4/16/09, Greg Barton wrote: > From: Greg Barton > Subject: Re: [rules-users] Negation semantics in Drools > To: "Rules Users List" > Date: Thursday, April 16, 2009, 8:50 PM > It is on the latest snapshot release, > 5.0.0.20090417.005612-483 > > --- On Thu, 4/16/09, Edson Tirelli > wrote: > > > We need to investigate if that is still happening > in > > latest trunk. > > > > ___ > rules-users mailing list > rules-users@lists.jboss.org > https://lists.jboss.org/mailman/listinfo/rules-users sample.drl Description: Binary data package com.sample; import java.io.InputStreamReader; import java.io.Reader; import java.util.Iterator; import org.drools.FactHandle; import org.drools.RuleBase; import org.drools.RuleBaseFactory; import org.drools.WorkingMemory; import org.drools.compiler.PackageBuilder; import org.drools.rule.Package; /** * This is a sample file to launch a rule package from a rule source file. */ public class DroolsTest { public static final void main(String[] args) throws Exception { boolean reversed = false; if(args.length > 0) { reversed = Boolean.parseBoolean(args[0]); } System.out.println("*"); System.out.println(reversed ? "Reversed" : "Regular"); System.out.println(); //load up the rulebase RuleBase ruleBase = readRule(); WorkingMemory workingMemory = ruleBase.newStatefulSession(); //go ! Move[] moves = new Move[] { new Move(1,2), new Move(2,3) }; if(reversed) { workingMemory.insert( moves[1] ); workingMemory.insert( moves[0] ); } else { workingMemory.insert( moves[0] ); workingMemory.insert( moves[1] ); } System.out.println("Before"); for(Iterator i = workingMemory.iterateFactHandles(); i.hasNext(); ) { System.out.println(workingMemory.getObject((FactHandle)i.next())); } System.out.println(); workingMemory.fireAllRules(); System.out.println(); System.out.println("After"); for(Iterator i = workingMemory.iterateFactHandles(); i.hasNext(); ) { System.out.println(workingMemory.getObject((FactHandle)i.next())); } System.out.println("*"); } /** * Please note that this is the "low level" rule assembly API. */ private static RuleBase readRule() throws Exception { //read in the source Reader source = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/sample.drl" ) ); //optionally read in the DSL (if you are using it). //Reader dsl = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/mylang.dsl" ) ); //Use package builder to build up a rule package. //An alternative lower level class called "DrlParser" can also be used... PackageBuilder builder = new PackageBuilder(); //this will parse and compile in one step //NOTE: There are 2 methods here, the one argument one is for normal DRL. builder.addPackageFromDrl( source ); //Use the following instead of above if you are using a DSL: //builder.addPackageFromDrl( source, dsl ); //get the compiled package (which is serializable) Package pkg = builder.getPackage(); //add the package to a rulebase (deploy the rule package). RuleBase ruleBase = RuleBaseFactory.newRuleBase(); ruleBase.addPackage( pkg ); return ruleBase; } public static class Move { public final int first; public final int second; public Move(int first, int second) { this.first = first; this.second = second; } public int getFirst() { return this.first; } public int getSecond() { return this.second; } public String toString() { return "
Re: [rules-users] Negation semantics in Drools
It is on the latest snapshot release, 5.0.0.20090417.005612-483 --- On Thu, 4/16/09, Edson Tirelli wrote: > We need to investigate if that is still happening in > latest trunk. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Negation semantics in Drools
In this case, I think Paul's initial analysis is pertinent. Unless I am missing something obvious, the order of the facts may change the order in which the activations are executed, but not the number of executions. The end result should be the same with the same number of Win facts in the working memory. We need to investigate if that is still happening in latest trunk. []s Edson 2009/4/16 Paul Fodor > On Thu, Apr 16, 2009 at 1:18 PM, Greg Barton wrote: > >> >> This is not a problem with negation. I don't think you understand rule >> conflict resolution. >> >> Here's your rule: >> >> rule "direct" >> when >> m : Move(x : first, y : second) >> not Win(first == y) >> then >> insert(new Win(m.getFirst())); >> end >> >> This rule, regardles of whether it uses negation, will be affected by fact >> insertion order. This is because the first condition will match on any Move >> in working memory. When all of the potential firings of this rule are put >> on the agenda, which will initially be one for each Move in working memory, >> one must be selected to fire. The order of fact insertion (recency) is one >> of the ways Drools resolves this by default. > > > Thank you. I see. > > >> If you don't want that behavior you can change it by using a different >> conflict resolver instance. See classes in the package org.drools.conflict: >> >> >> https://hudson.jboss.org/hudson/job/drools/lastSuccessfulBuild/artifact/trunk/target/javadocs/unstable/drools-core/org/drools/conflict/package-summary.html > > Or define your own. > > > That is very dificult task. The win-nowin test was just an example of use > of the default negation. My requirement covers stratified or non-stratified > logic programs, and not one example in particular. > I don't really think that it is possible to implement a general "rule > conflict resolution" resolver for default negation. This is hard > in production rule systems. > > Paul. > > >> You can install a new one by calling either of these methods on your >> RuleBaseConfiguration: >> >> setProperty("drools.conflictResolver", ) >> >> or >> >> setConflictResolver() >> >> --- On Thu, 4/16/09, Paul Fodor wrote: >> >> > From: Paul Fodor >> > Subject: [rules-users] Negation semantics in Drools >> > To: "Rules Users List" >> > Date: Thursday, April 16, 2009, 10:43 AM >> > Dear Sir, >> > >> > I found a problem with negation in Drools when changing the >> > order of the >> > facts in the database also changes the final model. >> > Consider the classic win-nowin problem encoded in Drools as >> > follows: >> > >> > win.drl: >> > >> > package tests; >> > import tests.Test.Win; >> > import tests.Test.Move; >> > rule "direct" >> > when >> > m : Move(x : first, y : second) >> > not Win(first == y) >> > then >> > insert(new Win(m.getFirst())); >> > end >> > >> > With two moves in the database: move(1,2) and move(2,3), >> > for one order of the facts in the input file we get one >> > result: win(2), >> > while for the other order (i.e., move(2,3) and move(1,2)) >> > we get 2 results: >> > win(1) and win(2). >> > >> > For win_upper1_drools.drools: >> > > java tests.Test win_upper1_drools.drools win.drl >> > result >> > reading rulefile: win.drl ... >> > reading datafile: win_upper1_drools.drools ... >> > computing cputime: 0.0 >> > computing walltime: 0.0030 >> > Derived facts in memory:move(1, 2). >> > win(2). >> > move(2, 3). >> > 3 >> > >> > For win_upper2_drools.drools: >> > > java tests.Test win_upper2_drools.drools win.drl >> > result >> > reading rulefile: win.drl ... >> > reading datafile: win_upper2_drools.drools ... >> > computing cputime: 0.0 >> > computing walltime: 0.0040 >> > Derived facts in memory:win(1). >> > win(2). >> > move(1, 2). >> > move(2, 3). >> > 4 >> > >> > I attached all the sources used in these tests in the >> > email. This example is >> > locally stratified. I am using the latest released version >> > of Drools. >> > >> > Regards, >> > Paul Fodor >> > >> > win.drl: >> > >> > package tests; >> > import tests.Test.Win; >> > import tests.Test.Move; >> > rule "direct" >> > when >> > m : Move(x : first, y : second) >> > not Win(first == y) >> > then >> > insert(new Win(m.getFirst())); >> > end >> > >> > >> > win_upper1_drools.drools: >> > >> > move >> > 1 >> > 2 >> > move >> > 2 >> > 3 >> > >> > win_upper2_drools.drools: >> > >> > move >> > 2 >> > 3 >> > move >> > 1 >> > 2 >> > >> > Test.java: >> > >> > package tests; >> > import java.io.*; >> > import java.io.InputStreamReader; >> > import org.drools.RuleBase; >> > import org.drools.RuleBaseFactory; >> > import org.drools.StatefulSession; >> > import org.drools.compiler.PackageBuilder; >> > import org.drools.FactHandle; >> > import java.util.Iterator; >> > import java.lang.management.ManagementFactory; >> > import java.lang.management.ThreadMXBean; >> > public class Test { >> > public static void main (String args[]) { >> > if (args.length < 3) { >> > u
Re: [rules-users] Negation semantics in Drools
On Thu, Apr 16, 2009 at 1:18 PM, Greg Barton wrote: > > This is not a problem with negation. I don't think you understand rule > conflict resolution. > > Here's your rule: > > rule "direct" > when > m : Move(x : first, y : second) > not Win(first == y) > then > insert(new Win(m.getFirst())); > end > > This rule, regardles of whether it uses negation, will be affected by fact > insertion order. This is because the first condition will match on any Move > in working memory. When all of the potential firings of this rule are put > on the agenda, which will initially be one for each Move in working memory, > one must be selected to fire. The order of fact insertion (recency) is one > of the ways Drools resolves this by default. Thank you. I see. > If you don't want that behavior you can change it by using a different > conflict resolver instance. See classes in the package org.drools.conflict: > > > https://hudson.jboss.org/hudson/job/drools/lastSuccessfulBuild/artifact/trunk/target/javadocs/unstable/drools-core/org/drools/conflict/package-summary.html Or define your own. That is very dificult task. The win-nowin test was just an example of use of the default negation. My requirement covers stratified or non-stratified logic programs, and not one example in particular. I don't really think that it is possible to implement a general "rule conflict resolution" resolver for default negation. This is hard in production rule systems. Paul. > You can install a new one by calling either of these methods on your > RuleBaseConfiguration: > > setProperty("drools.conflictResolver", ) > > or > > setConflictResolver() > > --- On Thu, 4/16/09, Paul Fodor wrote: > > > From: Paul Fodor > > Subject: [rules-users] Negation semantics in Drools > > To: "Rules Users List" > > Date: Thursday, April 16, 2009, 10:43 AM > > Dear Sir, > > > > I found a problem with negation in Drools when changing the > > order of the > > facts in the database also changes the final model. > > Consider the classic win-nowin problem encoded in Drools as > > follows: > > > > win.drl: > > > > package tests; > > import tests.Test.Win; > > import tests.Test.Move; > > rule "direct" > > when > > m : Move(x : first, y : second) > > not Win(first == y) > > then > > insert(new Win(m.getFirst())); > > end > > > > With two moves in the database: move(1,2) and move(2,3), > > for one order of the facts in the input file we get one > > result: win(2), > > while for the other order (i.e., move(2,3) and move(1,2)) > > we get 2 results: > > win(1) and win(2). > > > > For win_upper1_drools.drools: > > > java tests.Test win_upper1_drools.drools win.drl > > result > > reading rulefile: win.drl ... > > reading datafile: win_upper1_drools.drools ... > > computing cputime: 0.0 > > computing walltime: 0.0030 > > Derived facts in memory:move(1, 2). > > win(2). > > move(2, 3). > > 3 > > > > For win_upper2_drools.drools: > > > java tests.Test win_upper2_drools.drools win.drl > > result > > reading rulefile: win.drl ... > > reading datafile: win_upper2_drools.drools ... > > computing cputime: 0.0 > > computing walltime: 0.0040 > > Derived facts in memory:win(1). > > win(2). > > move(1, 2). > > move(2, 3). > > 4 > > > > I attached all the sources used in these tests in the > > email. This example is > > locally stratified. I am using the latest released version > > of Drools. > > > > Regards, > > Paul Fodor > > > > win.drl: > > > > package tests; > > import tests.Test.Win; > > import tests.Test.Move; > > rule "direct" > > when > > m : Move(x : first, y : second) > > not Win(first == y) > > then > > insert(new Win(m.getFirst())); > > end > > > > > > win_upper1_drools.drools: > > > > move > > 1 > > 2 > > move > > 2 > > 3 > > > > win_upper2_drools.drools: > > > > move > > 2 > > 3 > > move > > 1 > > 2 > > > > Test.java: > > > > package tests; > > import java.io.*; > > import java.io.InputStreamReader; > > import org.drools.RuleBase; > > import org.drools.RuleBaseFactory; > > import org.drools.StatefulSession; > > import org.drools.compiler.PackageBuilder; > > import org.drools.FactHandle; > > import java.util.Iterator; > > import java.lang.management.ManagementFactory; > > import java.lang.management.ThreadMXBean; > > public class Test { > > public static void main (String args[]) { > > if (args.length < 3) { > > usage(); > > } > > long starttime_cpu, endtime_cpu, starttime_sys, > > endtime_sys; > > double cputime, walltime; > > ThreadMXBean tb_cpu = ManagementFactory.getThreadMXBean(); > > // create rulebase > > try { > > FileWriter output = new FileWriter(args[2]); > > BufferedWriter bufWrite = new BufferedWriter(output); > > bufWrite.write(args[0] + "\n"); > > bufWrite.flush(); > > System.out.println("reading rulefile: " + > > args[1] + " ..."); > > Reader source = new > > InputStreamReader(Test.class.getResourceAsStream(args[1])); > > final PackageBuilder builder =
Re: [rules-users] Negation semantics in Drools
This is not a problem with negation. I don't think you understand rule conflict resolution. Here's your rule: rule "direct" when m : Move(x : first, y : second) not Win(first == y) then insert(new Win(m.getFirst())); end This rule, regardles of whether it uses negation, will be affected by fact insertion order. This is because the first condition will match on any Move in working memory. When all of the potential firings of this rule are put on the agenda, which will initially be one for each Move in working memory, one must be selected to fire. The order of fact insertion (recency) is one of the ways Drools resolves this by default. If you don't want that behavior you can change it by using a different conflict resolver instance. See classes in the package org.drools.conflict: https://hudson.jboss.org/hudson/job/drools/lastSuccessfulBuild/artifact/trunk/target/javadocs/unstable/drools-core/org/drools/conflict/package-summary.html Or define your own. You can install a new one by calling either of these methods on your RuleBaseConfiguration: setProperty("drools.conflictResolver", ) or setConflictResolver() --- On Thu, 4/16/09, Paul Fodor wrote: > From: Paul Fodor > Subject: [rules-users] Negation semantics in Drools > To: "Rules Users List" > Date: Thursday, April 16, 2009, 10:43 AM > Dear Sir, > > I found a problem with negation in Drools when changing the > order of the > facts in the database also changes the final model. > Consider the classic win-nowin problem encoded in Drools as > follows: > > win.drl: > > package tests; > import tests.Test.Win; > import tests.Test.Move; > rule "direct" > when > m : Move(x : first, y : second) > not Win(first == y) > then > insert(new Win(m.getFirst())); > end > > With two moves in the database: move(1,2) and move(2,3), > for one order of the facts in the input file we get one > result: win(2), > while for the other order (i.e., move(2,3) and move(1,2)) > we get 2 results: > win(1) and win(2). > > For win_upper1_drools.drools: > > java tests.Test win_upper1_drools.drools win.drl > result > reading rulefile: win.drl ... > reading datafile: win_upper1_drools.drools ... > computing cputime: 0.0 > computing walltime: 0.0030 > Derived facts in memory:move(1, 2). > win(2). > move(2, 3). > 3 > > For win_upper2_drools.drools: > > java tests.Test win_upper2_drools.drools win.drl > result > reading rulefile: win.drl ... > reading datafile: win_upper2_drools.drools ... > computing cputime: 0.0 > computing walltime: 0.0040 > Derived facts in memory:win(1). > win(2). > move(1, 2). > move(2, 3). > 4 > > I attached all the sources used in these tests in the > email. This example is > locally stratified. I am using the latest released version > of Drools. > > Regards, > Paul Fodor > > win.drl: > > package tests; > import tests.Test.Win; > import tests.Test.Move; > rule "direct" > when > m : Move(x : first, y : second) > not Win(first == y) > then > insert(new Win(m.getFirst())); > end > > > win_upper1_drools.drools: > > move > 1 > 2 > move > 2 > 3 > > win_upper2_drools.drools: > > move > 2 > 3 > move > 1 > 2 > > Test.java: > > package tests; > import java.io.*; > import java.io.InputStreamReader; > import org.drools.RuleBase; > import org.drools.RuleBaseFactory; > import org.drools.StatefulSession; > import org.drools.compiler.PackageBuilder; > import org.drools.FactHandle; > import java.util.Iterator; > import java.lang.management.ManagementFactory; > import java.lang.management.ThreadMXBean; > public class Test { > public static void main (String args[]) { > if (args.length < 3) { > usage(); > } > long starttime_cpu, endtime_cpu, starttime_sys, > endtime_sys; > double cputime, walltime; > ThreadMXBean tb_cpu = ManagementFactory.getThreadMXBean(); > // create rulebase > try { > FileWriter output = new FileWriter(args[2]); > BufferedWriter bufWrite = new BufferedWriter(output); > bufWrite.write(args[0] + "\n"); > bufWrite.flush(); > System.out.println("reading rulefile: " + > args[1] + " ..."); > Reader source = new > InputStreamReader(Test.class.getResourceAsStream(args[1])); > final PackageBuilder builder = new PackageBuilder(); > builder.addPackageFromDrl(source); > if (builder.hasErrors()) { > System.out.println(builder.getErrors().toString()); > System.exit(0); > } > final RuleBase ruleBase = > RuleBaseFactory.newRuleBase(); > ruleBase.addPackage(builder.getPackage()); > final StatefulSession session = > ruleBase.newStatefulSession(); > // loading datafile > System.out.println("reading datafile: " + > args[0] + " ..."); > FileReader input = new FileReader(args[0]); > starttime_sys = System.nanoTime(); > starttime_cpu = tb_cpu.getCurrentThreadCpuTime(); > BufferedReader bufRead = new BufferedReader(input); > String first, second, line = bufRead.readLine(); > w