Suppose I have the following schema:
(deftemplate task (declare (slot-specific TRUE)) (slot id) (slot name)
(slot probability (default 0.0)))
(deftemplate state (declare (slot-specific TRUE)) (slot id) (slot name)
(slot probability (default 0.0)))
(deftemplate precondition (slot task-id) (slot state-id)) ;state-id is a
precondition of task-id
Further suppose I want to define the probability of any given task as
the product of the probabilities of all of its preconditions. So
something like:
(defrule task-probability--using-accumulate
?t <- (task (id ?task-id))
?p <- (accumulate (bind ?product 1.0)
(bind ?product (* ?product ?prob))
?product
(and (precondition (task-id ?task-id) (state-id ?state-id))
(state (id ?state-id) (probability ?prob))))
=>
(modify ?t (probability ?p)))
Only, the above rule doesn't work. The CE in the accumulate CE can only
be a simple CE; patterns with multiple CEs using the operator 'and' are
not allowed.
To get around this limitation, I use a query to retrieve all of the
preconditions and perform the series product on the RHS:
(defquery preconditions
(declare (variables ?task-id))
(task (id ?task-id))
(precondition (task-id ?task-id) (state-id ?state-id))
(state (id ?state-id) (probability ?prob)))
(defrule task-probability
?t <- (task (id ?task-id))
(precondition (task-id ?task-id) (state-id ?state-id))
(state (id ?state-id) (probability ?state-prob))
=>
(bind ?result (run-query* preconditions ?task-id))
(bind ?prob-product 1.0)
(while (?result next)
(bind ?prob-product (* ?prob-product (?result getDouble prob))))
(modify ?t (probability ?prob-product)))
My question is, what is the preferred way to do an accumulate type
operation in Jess involving a complex pattern? My approach above is not
what queries were intended for and is probably not efficient over a
large WM space. I've also experimented with letting the rule engine do
each step of the product in a separate rule firing. This solution
requires about 5 rules and lots of temporary working memory. And it's
not clear it's any more efficient.
Thanks,
Steve Solomon
The Institute for Creative Technologies
The University of Southern California
[EMAIL PROTECTED]