Menu

#2 Bind function results to pattern vars

open
nobody
None
5
2007-09-15
2007-09-15
No

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

Discussion

  • Aneil Mallavarapu

    Implements a BIND pattern keyword for binding function results in rule pattern

     
  • Aneil Mallavarapu

    • summary: Bind function eval to pattern vars --> Bind function results to pattern vars
     
  • Aneil Mallavarapu

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

     
  • David E. Young

    David E. Young - 2007-09-17

    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.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.