It would be nice if there were a form which allowed binding of function results to pattern variables.
For example:
(defrule myrule ()
(x (a ?a))
(y (b ?b) (c ?c))
(BIND (?d ?e) (compute-2-vals ?a ?b ?c))
(z (a ?a) (b ?b) (d ?d) (e ?d))
=>
(action))
One could hack this by using 2 rules and a dummy data structure to shuttle the results
from one to the other:
(defrule r1 ()
(x (a ?a))
(y (b ?b))
=>
(multiple-value-bind (?c ?d) (compute-2-vals ?a ?b)
(assert `(bind-result (rule r1) (num 0)
(value-0 ,?a)
(value-1 ,?b)
(value-2 ,?c)
(value-3 ,?d)))))
(defrule r2 ()
(bind-result (rule r1) (num 0) (value-0 ?a) (value-1 ?b) (value-2 ?c) (value-3 ?d))
(z (a ?a) (b ?b) (c ?c) (d ?d))
=>
(action))
I've quickly coded up a no-frills version of this - see the attached a file. BIND patterns are only recognized in the main (AND) block of the rule - not, for example, in OR, LOGICAL or other compound pattern blocks.
A better approach would be to add this directly to the rule parser, but I am unable to make heads or tails of what's going on in there.
Hope this is useful - even if only as an idea.
Cheers,
Aneil
Implements a BIND pattern keyword for binding function results in rule pattern
Logged In: YES
user_id=870521
Originator: YES
Sry - the first lisp form in my comment should have been:
(defrule myrule ()
(x (a ?a))
(y (b ?b))
(BIND (?c ?d) (compute-2-vals ?a ?b))
(z (a ?a) (b ?b) (c ?c) (d ?d))
=>
(action))
Logged In: YES
user_id=21258
Originator: NO
That's a good idea. If memory serves, CLIPS (and Jess?) support this conditional element. I think this would make a good next feature.