RE: JESS: [EXTERNAL] Dynamic rule matching in the LHS
"Friedman-Hill, Ernest" <[email protected]> Wed, 31 Jul 2013 17:43:12 +0000
| Newsgroups | gmane.comp.java.jess |
|---|---|
| Message-ID | <[email protected]> |
You can slightly augment my "forall" version to fire once for each good s=
et, given that bag-of-items has some kind of identifier; I'll assume a sl=
ot named "id". It doesn't matter what the contents are:
(defrule check-bag-valid
(bag-of-items (id ?id) ))
(forall
(bag-of-items (id ?id) (names $? ?name $?))
(item (name ?name)))
=3D>
(printout t "The bag is valid" crlf))
This rule could be read as "For some bag with some id, every value in the=
names slot has a matching item fact." It will fire once for every bag fo=
r which this condition holds.
-----Original Message-----
From: [email protected] [mailto:[email protected]] On=
Behalf Of Aurelien Mazurie
Sent: Wednesday, July 31, 2013 1:13 AM
To: jess-users
Subject: Re: JESS: [EXTERNAL] Dynamic rule matching in the LHS
Thank you for this tip. It is not what I am trying to achieve, however. I=
must apologize if my original email was misunderstood.
My goal is to write a rule that would fire for any 'bag-of-items' fact wh=
ose item names, as listed in a multislot, are all represented by 'item' f=
acts. The rule should not fire if one or more of these items are absent f=
rom the fact list:
(item (name A))
(item (name B))
(bag-of-items (item-names A))
(bag-of-items (item-names A B))
(bag-of-items (item-names A B C))
In this example the two first 'bag-of-items' facts would fire my hypothet=
ical rule, while the third would not, because there is no 'item' fact wit=
h name C.
The 'forall' approach suggested by M. Friedman-Hill is quite close to thi=
s, however it seems that the rule will fire only if _all_ the 'bag-of-ite=
ms' facts have all of their items represented by facts. I.e., in my examp=
le above it would not fire until I remove the third bag-of-items fact. On=
ce again, it is close but no cigar.
Best,
Aur=E9lien
On Jul 30, 2013, at 10:54 AM, "Jason Morris" <[email protected]> w=
rote:
> Another "old skool" way of doing it using predicate constraints is...
>=20
> (clear)
> (deftemplate item (slot name))
> (deftemplate bag-of-items (multislot item-names))
>=20
> (defrule fire-for-all-members-in-bag
> ; If you have a bag of item names ...
> (bag-of-items (item-names $?item-names)) ; and there is an item=20
> whose name is member of this bag ...
> ?item <-(item (name ?name&:(member$ ?name $?item-names))) =3D> ;=20
> ...then do something interesting (printout t ?name " is in the bag!"=20
> crlf))
>=20
> ;; Program
> (reset)
> (assert (item (name A)))
> (assert (item (name B)))
> (assert (item (name C)))
> (assert (bag-of-items (item-names A B C)))
> (run)
>=20
> *Jason C. Morris*
> President, Principal Consultant
> Morris Technical Solutions LLC
> President, Rules Fest Association
> Chairman, IntelliFest 2013: International Conference on Reasoning=20
> Technologies
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [email protected]'
in the BODY of a message to [email protected], NOT to the list (use yo=
ur own address!) List problems? Notify [email protected].
--------------------------------------------------------------------
--------------------------------------------------------------------
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]=
.
--------------------------------------------------------------------