Since the slot "children" is a multislot (you may want to consult Jess
manual section 6.4)
you cannot replace all child names at the same time. But you can do it one
by one.
(defrule l2rc
?c <- (couple (children $? ?x $? & ~: (java-objectp ?x)))
?p <- (person (name ?x))
=>
(bind ?kids (fact-slot-value ?c children))
(bind ?objs (list))
(foreach ?kid ?kids
(if (and (not (java-objectp ?kid)) (eq ?kid ?x)) then
(bind ?objs (list ?objs ?p))
else
(bind ?objs (list ?objs ?kid))
)
)
(modify ?c (children ?objs))
)
On Fri, Oct 31, 2008 at 9:29 AM, Nopphadol Chalortham
<[EMAIL PROTECTED]>wrote:
> Sorry Sorry , my deffacts and rules are error.
>
> I edit it as below:
>
> (deftemplate person (slot name)(slot age))
> (deftemplate family (slot father)(slot mother)(multislot children))
>
> (deffacts families
> (person (name John)(age 36))
> (person (name Mary)(age 32))
> (person (name Paul)(age 55))
> (person (name Suzy)(age 41))
> (person (name Tom)(age 10))
> (person (name Jerry)(age 8))
> (person (name Jennifer)(age 14))
> (person (name Clair)(age 6))
> (family (faher John)(mother Marry)(children Tom Jerry))
> (family (faher Paul)(mother Suzy)(children Jennifer Clair))
> )
> (defrule l2rf
> ?fm <- (family (father ?f & ~: (java-objectp ?f)))
> ?p <- (person (name ?f))
> =>
> (modify ?fm (father ?p))
> )
>
> (defrule l2rm
> ?fm <- (family (mother ?m & ~: (java-objectp ?m)))
> ?p <- (person (name ?m))
> =>
> (modify ?fm (mother ?p))
> )
>
>
> thank you
>
>
> On Fri, Oct 31, 2008 at 3:01 PM, Nopphadol Chalortham <
> [EMAIL PROTECTED]> wrote:
>
>> Thank you very much. My opinion, I don't dislike Ernest's way. But I like
>> your way.
>>
>> I have a problem, help me please.
>>
>>
>>
>> If I have templates and facts as below:
>>
>>
>> (deftemplate person (slot name)(slot age))
>> (deftemplate family (slot father)(slot mother)(multislot children))
>>
>> (deffacts families
>>
>> (person (name John)(age 36))
>> (person (name Mary)(age 32))
>> (person (name Paul)(age 55))
>> (person (name Suzy)(age 41))
>> (person (name Tom)(age 10))
>> (person (name Jerry)(age 8))
>> (person (name Jennifer)(age 14))
>> (person (name Clair)(age 6))
>> (family (faher John)(mother Marry)(children Tom Jerry))
>> (family (faher Paul)(mother Suzy)(children Jennifer Clair))
>> )
>>
>> I would like to have initial-facts as below:
>>
>> f-0 (MAIN::initial-fact)
>> f-1 (MAIN::person (name John) (age 36))
>> f-2 (MAIN::person (name Mary) (age 32))
>> f-3 (MAIN::person (name Paul) (age 55))
>> f-4 (MAIN::person (name Suzy) (age 41))
>> f-5 (MAIN::person (name Tom) (age 10))
>> f-6 (MAIN::person (name Jerry) (age 8))
>> f-7 (MAIN::person (name Jennifer) (age 14))
>> f-8 (MAIN::person (name Clair) (age 6))
>> f-9 (MAIN::family (faher <Fact-1>)(mother <Fact-2>)(children <Fact-5>
>> <Fact-6>))
>> f-10 (MAIN::family (faher <Fact-3>)(mother <Fact-4>)(children <Fact-7>
>> <Fact-8>))
>>
>> Rules for modifing father-slot and mother-slot are the same as
>> (defrule l2rf
>> ?f <- (family (father ?f & ~: (java-objectp ?f)))
>> ?p <- (person (name ?f))
>> =>
>> (modify ?f (father ?p))
>> )
>>
>> (defrule l2rm
>> ?f <- (couple (mother ?m & ~: (java-objectp ?m)))
>> ?p <- (person (name ?m))
>> =>
>> (modify ?f (mother ?p))
>> )
>>
>> What about rule for modifing children-slot?
>>
>>
>>
>>
>>
>>
>>
>> On Thu, Oct 30, 2008 at 4:07 PM, Wolfgang Laun <[EMAIL PROTECTED]>wrote:
>>
>>> On Wed, Oct 29, 2008 at 3:17 PM, Nopphadol Chalortham <
>>> [EMAIL PROTECTED]> wrote:
>>>
>>>> Thank you for your answer. I have tried this method already. It works.
>>>> But I would like to use only deffacts construct. Do you have any answer?
>>>>
>>>
>>> There is a somewhat roundabout way to achieve that. In the deffacts
>>> construct,
>>> you assign the couple slots husband and wife the name. (Obviously this
>>> assumes that names are unique. If not, you'd have to introduce some
>>> unique
>>> id.)
>>>
>>> You'll have to replace these name symbols with object references, which
>>> you can do with moderately simple rules, one for husband and the other
>>> one
>>> for wife: The first pattern must match such couple facts where the
>>> husband (wife)
>>> slot does not contain a Java object. The second conditional element would
>>> match a person fact by the name found in the first fact's slot. Having
>>> bound
>>> both facts to variables, it's now simple to replace the name symbol with
>>> the
>>> fact reference.
>>>
>>> Of course it would be possible to use the name as an indirect reference
>>> to a
>>> person, and write your rules accordingly. In fact, that's what frequently
>>> done
>>> when the extra effort to establish the connection between facts by a
>>> *fact* reference isn't considered worth the effort. YMMV.
>>>
>>> But why do you dislike Ernest's way?
>>>
>>> (deftemplate person (slot name)(slot age))
>>> (deftemplate couple (slot husband)(slot wife))
>>>
>>> (deffacts couples
>>> (person (name John)(age 36))
>>> (person (name Mary)(age 32))
>>> (couple (husband John)(wife Mary))
>>> (person (name Paul)(age 55))
>>> (person (name Suzy)(age 41))
>>> (couple (husband Paul)(wife Suzy))
>>> )
>>>
>>> (defrule l2rh
>>> ?c <- (couple (husband ?h & ~: (java-objectp ?h)))
>>> ?p <- (person (name ?h))
>>> =>
>>> (modify ?c (husband ?p))
>>> )
>>>
>>> (defrule l2rw
>>> ?c <- (couple (wife ?w & ~: (java-objectp ?w)))
>>> ?p <- (person (name ?w))
>>> =>
>>> (modify ?c (wife ?p))
>>> )
>>>
>>> (reset)
>>> (run)
>>> (facts)
>>>
>>>
>>>>
>>>>
>>>>
>>>>
>>>> On Wed, Oct 29, 2008 at 8:33 PM, Ernest Friedman-Hill <
>>>> [EMAIL PROTECTED]> wrote:
>>>>
>>>>> Well, you can't nest facts in this way, and neither can you usefully
>>>>> include function calls in deffacts. The right way to do this is just to
>>>>> use
>>>>> one or more assert statements; for example, you could do it all in one
>>>>> line
>>>>> like this:
>>>>>
>>>>> (assert (couple (husband (assert (person (name John)(age 38))))
>>>>> (wife (assert (person (name Marry)(age 36))))))
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On Oct 29, 2008, at 5:02 AM, Nopphadol Chalortham wrote:
>>>>>
>>>>> I would like to assert facts to the jess working memory by deffacts as
>>>>>> below;
>>>>>> This is my template's facts.
>>>>>>
>>>>>> (deftemplate person (slot name)
>>>>>> (slot age))
>>>>>> (deftemplate couple (slot husband (type object))
>>>>>> (slot wife (type object)))
>>>>>> So, I would like to insert two persons(john and marry)and a couple as
>>>>>> below:
>>>>>> (deffacts initfacts
>>>>>> (person (name John)(age 38))
>>>>>> (person (name Marry)(age 36))
>>>>>> (couple (hasband (person (name John)(age 38)) )
>>>>>> (wife (person (name Marry)(age 36)) ))
>>>>>> )
>>>>>>
>>>>>> But it has an error message: Undefined function person.
>>>>>> How can I solve this problem?
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>