The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

mirkata (11) [Avatar] Offline
#1
Hi,
I have the folowing problem

(defrule Rule1
?p<-(Smth
(active true)(trueStatements $?he2&smilieneq (length$ $?he2) 0))
)
?c<-(Smth2 (name (nth$ 1 ?he2))(hasEvent ?y) )

=> ...

I have a rule that matches the truStaatemnts, whe nthey are >(not equal 0) . If this is the case I would like to macth another fact which name attribute is the name of the first true statement. I tried several ways but always got error that fter name nno (s are allowed. So how could I do this?

In the book I havent found an annswer either.

A second questioin is it a better way to define that the multislot should have at least one element?

Wish a nice day to all of you,
Miroslav
ejfried (326) [Avatar] Offline
#2
Re: map list elemnt to fact
First, to match a multislot containing at least one element, you can use

?p<-(Smth (trueStatements ?first $?))

There must be at least one element to match the variable ?first; the blank multifield matches zero or more additional elements. This is much more efficient.

This would let you write the next pattern as

?c<-(Smth2 (name ?first) (hasEvent ?y))

Now, as far as the book not telling you how to do it the way you're trying: sure it does. It talks about predicate constraints on page 105, and return-value constraints on page 106, either of which you could have used.
mirkata (11) [Avatar] Offline
#3
Re: map list elemnt to fact
Hi,
thank you for the smart suggestion, it works brilliantly. And yo were right about the book, I should have missed it. I would be glad if in the next edition there is a bigger part with the CLIPS functions for newbies like me.

I am still interested if we havent matched the element in ?c but only the list, what is the way to take, for instance the 15th element as a name? Could we bind it before and then use it in name?

And last question, is it possible to say, fire for all elemnts in the list, so they should be matched to name and fire for aech and when the last one is reached modify active to false so that the rule doesnt fire anymore? If this is not very much rule-based coformative shoud I define a function here and call it?

Thanks,
Miroslav
ejfried (326) [Avatar] Offline
#4
Re: map list elemnt to fact
As I said, you just have to use the predicate or return-value constraint syntax:

?c<-(Smth2 (name =(nth$ 1 ?he2))(hasEvent ?y) )

Notice the "=" sign -- that's what makes it a return-value constraint.

As far as your second question: yes, just writing an explicit loop over the list elements is probably the best thing to do.