"no-loop" is just the easy way out. The traditional solution -- which
is more flexible -- is to write the rule so that after it's through
firing, it won't match the facts anymore. For example, here you could
write something like
(defrule updateAuthorBookList
?Author <- (Author (name ?name) (books $?books))
(Book (title ?title&:(not (member$ ?title ?books))) (authors $?
authors&:(member$ ?name ?authors)))
=>
(modify ?Author (books (create$ ?books ?title)))
(printout t ?name " is the author of the book: " ?title crlf)
)
I've added a constraint on the "title" slot so that it won't match if
the author's list of books already contains that title.
On Nov 12, 2008, at 11:54 AM, Joaquín Cañadas wrote:
The (declare (no-loop TRUE)) prevents the infinite loop. However, it
also produces the rule to fire only once per Author, finding only one
element of the list, that is, finding only one Book per Author. In my
problem, as a tipical many-to-many relationship, an Author can have
many
books. The no-loop declaration prevents the rule to search for more
than
one Book of an Author, and the result is that only one Book is added
per
Author.
I tryed retracting and asserting instead of modifying, but the
result is
the same. Maybe I should not use multislots... I don't know...
Thank you for your quick answer
Joaquín
Ernest Friedman-Hill escribió:
Check out the no-loop rule declaration; see
http://www.jessrules.com/jess/docs/71/rules.html#no-loop
On Nov 12, 2008, at 10:56 AM, Joaquín Cañadas wrote:
Hello,
I am trying to update a multislot with a rule. The problem is that
the
rule enters into an infinite loop. My example is as follows:
- I have Books and Authors,
- a Book has one or many Authors,
- an Author has one or many books.
Book facts are defined including the author list, but Author facts
are
defined without the book list. So I wrote a rule for updating the
books
of the authors. However, the “modify” causes an infinite loop.
This is the code of my example:
(deftemplate Book
(slot title)
(multislot authors)
)
(deftemplate Author
(slot name)
(multislot books)
)
(deffacts books_samples
(Book (title "Jess in Action") (authors "Ernest Friedman-Hill"))
(Book (title "The Unified Software Development Process") (authors
"Ivar
Jacobson" "Grady Booch" "James Rumbaugh"))
(Book (title "Unified Modeling Language User Guide") (authors "Grady
Booch" "James Rumbaugh" "Ivar Jacobson" ))
(Author (name "Ernest Friedman-Hill"))
(Author (name "Grady Booch"))
(Author (name "James Rumbaugh"))
(Author (name "Ivar Jacobson"))
)
(defrule updateAuthorBookList
?Author <- (Author (name ?name) (books $?books))
(Book (title ?title) (authors $?authors&:(member$ ?name $?authors)))
=>
(modify ?Author (books (create$ $?books ?title)))
(printout t ?name " is the author of the book: " ?title crlf)
)
(reset)
(run)
When I delete the “modify” line, the rule prints the results well.
Any suggestion?
Thank you in advance,
Joaquín
--------------------------------------------------------------------
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]
--------------------------------------------------------------------
---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences, Sandia National Laboratories
PO Box 969, MS 9012, Livermore, CA 94550
http://www.jessrules.com
--------------------------------------------------------------------
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]
--------------------------------------------------------------------
--------------------------------------------------------------------
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]
.
--------------------------------------------------------------------
---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences, Sandia National Laboratories
PO Box 969, MS 9012, Livermore, CA 94550
http://www.jessrules.com
--------------------------------------------------------------------
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]
--------------------------------------------------------------------