Hi again, 

Thanks for the quick and clear answer on my question on combining 
backward chaining and CEs.

Now I got another problem:

Suppose there are a number of account facts in Jess' working memory:
(account (id 1) (value 2000))
(account (id 2) (value 8000))
(account (id 3) (value 4000))
...

Now I want a rule that fires for all accounts together, not for each 
one seperately:

(defrule sum-of-all-account-values
  (logical (account (id ?a&) (value ?value1)))
  (logical (account (id ?b&:(> ?b ?a)) (value ?value2)))
  (logical (account (id ?b&:(> ?c ?b)) (value ?value2)))
  ...
  (logical (not (exists (account (id ~?a&~?b&...))
  =>
  (assert (sum (+ ?value1 ?value2 ...))))

The conditions (> ?b ?a), (> ?c ?b) were added to make sure the rule 
would only fire for the combination of all accounts once.
Now, the problem isn't to get the correct sum. In a former version of 
my application I calculated it by getting all accounts with a defquery 
and then using the returned Iterator object to get the desired sum. My 
problem is to make the sum fact logically dependent of all account 
facts used to compute it. If an old account fact is modified or 
retracted, or a new one is asserted, I want the sum fact to be 
retracted automatically.

I presume there isn't a good solution to this, as this is a typical 
problem for rulebased systems, but I could be wrong.

Right now, I use a rule that first gets the number of accounts, by 
using the count-query-results function, and then uses this number to 
build a rule with the build function. The resulting rule has a salience 
of 100, so it gets executed right after is is build. It has the same 
number of (logical (account ...)) patterns as there are accounts, plus 
the (logical (not (exists (another-account)))) pattern. After it 
asserted the fact, it undefrules itself. This works, but is very 
errorprone and hard to read, not to mention the possible effects of 
other rules with salience 100 or auto-focus rules in other modules.

Regards,
Steffen.

--------------------------------------------------------------------
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