[rules-users] insertLogical and modifyRetract
Gras, Patrick
Patrick.Gras at generali.ch
Tue Oct 7 11:57:14 EDT 2008
Hello,
If you can change the code for CreatedFact and let it have a reference to UserFact and change the rule to:
rule "fact = 1"
when
$f : UserFact(fact1 == 1);
then
insertLogical(new CreatedFact($f));
end
you will also have to overide equals and hashcode for CreatedFact so that several CreatedFacts referencing the same UserFact are considered equal...
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...
-Patrick
-----Message d'origine-----
De : rules-users-bounces at lists.jboss.org [mailto:rules-users-bounces at lists.jboss.org]De la part de tim tim
Envoyé : mardi, 7. octobre 2008 16:53
À : rules-users at lists.jboss.org
Objet : [rules-users] insertLogical and modifyRetract
Hello,
I am a bit confused about how insertLogical() supposed to work in drools 5.
when i have a rule such as:
rule "fact = 1"
when
$f : UserFact(fact1 == 1);
then
insertLogical(new CreatedFact($f.getFact2()));
end
now i change $f in such a way, that the rule will fire again.
via
// build first version
UserFact f = new UserFact();
f.setFact1(1);
f.setFact2(1);
memory.insert(f);
memory.fireAllRules(); // <- Rule fires once
// now i change the memory and fire the rules again
memory.modifyRetract(f) ;
f.setFact2(100); // <- changing $f, but leaving fact1 as it is.
memory.modifyInsert(f);
memory.fireAllRules(); // <- Rule fires again
now the rule should fire again, which it does.
but i end up with two CreatedFact instances in the workingMemory..
one with the old OtherFact value 1, and one with the new value, 100
but i want only the second instance. the one created first is not valid any more.
i could write an extra rule for retracting the first CreatedFact-fact, but then i would have
a very tight coupling of the two rules.
is there a better way?
it seems odd to me, that a consequence of a rule stays in memory, when there is
a more current version of the rule evocation with the _same_ facts in the precondition
and a different consequence.
thanks in advance, tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20081007/f8c006a9/attachment.html
More information about the rules-users
mailing list