Question

I'm doing an exercise and where I need to create a simple dating agency matching system. Currently each person have one hobby, hobbies are categories by active and inactive and if the two hobbies match and the two persons have opposite sex, the dating agency thinks these to persons are a match.

So now I need the system to be able to allow for multiple hobbies but I cant seem to get my head around this.

Here's what I have:

(deftemplate MAIN::person
    (slot name)
    (slot age)
    (multislot hobby)
    (slot town)
    (slot sex))

(person (name "Jane")
        (age 25)
        (hobby fashion gardening)
        (town montrose)
        (sex female))

(deffacts hobby_types "Hobby categories"
    (hobby_type gardening active)
    (hobby_type swinging active)
    (hobby_type reading inactive)
    (hobby_type fashion inactive)
)

(defrule compatible_hobbies
    (opposite_sex ?name1 ?name2)
    (person (name ?name1) (hobby ?hobby1))
    (person (name ?name2) (hobby ?hobby2))
    (hobby_type ?hobby1 ?type)
    (hobby_type ?hobby2 ?type)
    =>
    (assert (hobbies_match ?name1 ?name2))
)

So first up, do I declare the multislot field correctly? And how do I adjust my compatible_hobbies rule to match against either of the hobbies that a person might have?

Was it helpful?

Solution

The multislot hobby in person is declared correctly. But your rule won't match the hobbies correctly and would match the same fact to the same fact showing that Jane is compatible with Jane. Actually if the rule matched facts it would generate the Cartesian product of the person facts (or a large number of non-desired matches).

I modified the rule to:

(defrule compatible_hobbies (person (name ?n1) (hobby $? ?h1 $?)) (person (name ?n2) (hobby $? ?h1 $?)) (hobby_type ?h1 ?x) (test (neq ?n1 ?n2)) => (assert (hobbies_compatible ?n1 ?n2)) )

So if you assert two person facts like

(person (name "Jane") (hobby blah1 gardening blah2)) and (person (name "Jim") (hobby blah3 gardening blah4)) then the rule will fire and the fact (hobbies_compatible Jim Jane) will be asserted (as well as the fact (hobbies_compatible "Jim" "Jane")).

The wildcard $? variable matches a set of symbols preceding and following the desired matching hobby. Your opposite sex fact should probably also be a test instead of a fact.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top