I think Mark Egloff wrote:
> Hello there 
> 
> I am looking for a way to increment a specific value from a fact within
> a rule. I have only found the "modify" command for unordered facts.
> Exists there also a way to modify ordered facts?
> 

The general rule is that if you feel the urge to modify an ordered
fact, then there's a problem in your design, and you should be using
an unordered fact.


> Could you also provide me an example how I could increment a value
> "amount" for an unordered fact like this:
> 
> (event (type A) (amount 2)) 
> 

This is an unordered fact, so you may be getting your terms mixed up.

> My first idea, is like this, but this seems to end in an endless loop.
> Could someone tell me why?  
> So I think there must exists better ways for a simple incrementation. ;
> )
> Please show me or just give me some hints.


If you modify a fact on the right-hand-side of a rule, and the
modified fact still matches, then the rule will fire again with this
new match. This is a classic problem. We've actually been talking a
lot on this list about ways to deal with this that will be included in
Jess 7.

For now, the trick is to change the rule so that either the modified
fact doesn't match anymore, or so that some -other- rule will also
match, will fire first, and thus prevent the original rule from firing
again immediately.

> 
> (defrule incrementtest (event (type A) (amount ?x )) ?id <- (event (type
> A) (amount ?x))  => (modify ?id (amount (+ ?x 1)))) 

Note that this rule can only match the same exact fact twice, since ?x
will be "unified" across the two rules. If the goal is to somehow
accumulate a sum based on a number of events, you could fix the
endless loop by adding an "already-counted" slot to the event fact,
and setting it to "TRUE" once you've considered the event.


---------------------------------------------------------
Ernest Friedman-Hill  
Distributed Systems Research        Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 [EMAIL PROTECTED]
Livermore, CA 94550         http://herzberg.ca.sandia.gov

--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------

Reply via email to