<div dir="ltr">ups, i created a nice endless loop with my proposed solution. :-)<br><br>the correct (still ugly) solution is:<br><br>rule
"fact1 should be smaller then 1"<br>
when<br>
$f : UserFact($fact1 : fact1 >= 1)<br> not IntConstraintOccurence(<br>
ruleId == "fact1 should be smaller then 1",<br>
constraintType == ConstraintType.NEGATIVE_SOFT,<br>
causes contains $f,<br>
weight != $fact1) // the weight has to be different, else the rule should not fire<br><br>
then
<br>
// use the value of fact1 to determine the error<br>
insertLogical(new IntConstraintOccurence("fact1 should be smaller then 1", $fact1, $f ));
<br>
end<br><br>
rule
"fact1 should be smaller then 1, old constraint exists"<br>
when<br>
$f : UserFact($fact1 : fact1 >= 1);<br><br> // see if there is already a fact in the <br>
$oldConstraint : IntConstraintOccurence(<br> ruleId == "fact1 should be smaller then 1",<br> constraintType == ConstraintType.NEGATIVE_SOFT,<br>
causes contains $f,<br> weight != $fact1) // the weight has to be different, else the rule should not fire <br>then
<br> retract($oldConstraint); // manually retract the obsolete fact<br>
// use the value of fact1 to determine the error<br>
insertLogical(new IntConstraintOccurence("fact1 should be smaller then 1",ConstraintType.NEGATIVE_<div dir="ltr">SOFT, $fact1, $f ));
<br>
end</div><br><br><br><br><br><div class="gmail_quote">On Tue, Oct 7, 2008 at 6:59 PM, tim tim <span dir="ltr"><<a href="mailto:timbaermannextra@googlemail.com">timbaermannextra@googlemail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div dir="ltr">thanks, you are right, that is kind of a solution. but not really what i am<br>
looking for.<br>i am using drools-solver, and the "CreatedFact" is an IntConstraintOccurrence, which i do not want to change.<br>
<br>maybe i should make the example more specific for my problem.<br><br>i have a rule, used for the solver:<br><br>rule
"fact1 should be smaller then 1"<br> when<br>
$f : UserFact(fact1 >= 1);<br> then
<br> // use the value of fact1 to determine the error<br> insertLogical(new IntConstraintOccurence("fact1 should be smaller then 1", $f.getFact1(), $f ));
<br>end<br><br><br>i have a Move that decrements fact1, so that after the move the error should be 1 smaller.<br>but the move is almost never triggered, because the old constraintOccurence is not retracted from memory and the new one added anyway, <br>
hence the error is nearly double after the move, as now two constraintOccurences represent the same error.<br><br><br>what i could think of is something like this:<br><br>one rule for the first test:<br><br>
rule
"fact1 should be smaller then 1"<br>
when<br>
$f : UserFact(fact1 >= 1)<br> not IntConstraintOccurence(<br>
ruleId == "fact1 should be smaller then 1",<br>
constraintType == ConstraintType.NEGATIVE_SOFT,<br>
causes contains $f)<br>
then
<br>
// use the value of fact1 to determine the error<br>
insertLogical(new IntConstraintOccurence("fact1 should be smaller then 1", $f.getFact1(), $f ));
<br>
end<br><br>and a second rule, when the first rule has already fired before:<br><br>
rule
"fact1 should be smaller then 1, old constraint exists"<br>
when<br>
$f : UserFact(fact1 >= 1);<br><br> // see if there is already a fact in the <br>
$oldConstraint : IntConstraintOccurence(<br> ruleId == "fact1 should be smaller then 1",<br> constraintType == ConstraintType.NEGATIVE_SOFT,<br>
causes contains $f) <br>then
<br> retract($oldConstraint); // manually retract the obsolete fact<br>
// use the value of fact1 to determine the error<br>
insertLogical(new IntConstraintOccurence("fact1 should be smaller then 1",ConstraintType.NEGATIVE_SOFT, $f.getFact1(), $f ));
<br>
end<br> <br>this solution seems very verbose for such a common task. and again, i do not understand, why an obsolete fact is not<br>retracted automatically?<br><br>best, tim<br><br><div class="gmail_quote">
On Tue, Oct 7, 2008 at 5:57 PM, Gras, Patrick <span dir="ltr"><Patrick.Gras@generali.ch></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>
<div><span><font size="2" color="#0000ff" face="Arial">Hello,</font></span></div>
<div><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div><span><font size="2" color="#0000ff" face="Arial">If you
can change the code for CreatedFact and let it have a reference to UserFact and
change the rule to:</font></span></div>
<div><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div><span><font size="2" color="#0000ff" face="Arial"><font size="3" color="#000000" face="Times New Roman"><div>rule "fact =
1"<br> when<br> $f :
UserFact(fact1 == 1);<br> then
<br></div> insertLogical(new
CreatedFact($f));
<br>end</font><br></font></span></div>
<div><span><font size="2" color="#0000ff" face="Arial">you
will also have to overide equals and hashcode for CreatedFact so that several
CreatedFacts referencing the same UserFact are considered
equal...</font></span></div>
<div><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div><span><font size="2" color="#0000ff" face="Arial">But
with this solution CreatedFact will always be up to date with the value of
UserFact even without firing the rules and maybe it's not what you
want...</font></span></div>
<div><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div><span><font size="2" color="#0000ff" face="Arial">-Patrick</font></span></div>
<blockquote dir="ltr" style="margin-right: 0px;">
<div dir="ltr" align="left"><font size="2" face="Tahoma">-----Message d'origine-----<br><b>De :</b>
<a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a>
[mailto:<a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a>]<b>De la part de</b> tim
tim<br><b>Envoyé :</b> mardi, 7. octobre 2008 16:53<br><b>À :</b>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br><b>Objet :</b> [rules-users] insertLogical
and modifyRetract<br><br></font></div><div><div></div><div>
<div dir="ltr">Hello,<br><br>I am a bit confused about how insertLogical()
supposed to work in drools 5.<br><br>when i have a rule such as:<br><br>rule
"fact = 1"<br> when<br>
$f : UserFact(fact1 == 1);<br> then
<br> insertLogical(new
CreatedFact($f.getFact2()));
<br>end<br><br>now i change $f in such a way, that the rule will fire
again.<br>via <br><br>// build first version<br>UserFact f = new
UserFact();<br>f.setFact1(1);<br>f.setFact2(1);<br><br>memory.insert(f);<br>memory.fireAllRules();
// <- Rule fires once<br><br>// now i change the memory and fire the rules
again<br><br>memory.modifyRetract(f) ;<br>f.setFact2(100); // <- changing
$f, but leaving fact1 as it
is.<br>memory.modifyInsert(f);<br><br>memory.fireAllRules(); // <-
Rule fires again<br><br>now the rule should fire again, which it does.<br>but
i end up with two CreatedFact instances in the workingMemory..<br>one with the
old OtherFact value 1, and one with the new value, 100<br><br>but i want only
the second instance. the one created first is not valid any more.<br>i could
write an extra rule for retracting the first CreatedFact-fact, but then i
would have<br>a very tight coupling of the two rules.<br> <br>is there a
better way?<br>it seems odd to me, that a consequence of a rule stays in
memory, when there is<br>a more current version of the rule evocation with the
_same_ facts in the precondition<br>and a different consequence.<br><br>thanks
in advance, tim<br><br></div></div></div></blockquote></div>
<br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>