Irwin
Irwin

Reputation: 12829

Create rules in CLIPS to move from unknown to known person facts

Assume that you are given a set of 'person' facts that are defined according to the following construct:

(deftemplate person (slot name) (slot sex) (allowed-values male female) (multislot children))

Write rules to do the following:

Your rules should do data validation to ensure that only an allowed value for is supplied by the user

Upvotes: 0

Views: 224

Answers (1)

Irwin
Irwin

Reputation: 12829

Define the template in CLIPS:

(deftemplate person 
    (slot name) 
    (slot sex) 
    (slot gender (allowed-values male female)) 
    (multislot children))

Start with the unknown-person creation (caveat: this may not be completely correct as it still creates a person without checking to see if they exist).

(defrule childrencataloguer "First layer of unknown person resolution"
    (person (children $?ch))
    =>
    (progn$ (?term ?ch)
        (assert (unknown-person ?term))
    ))

Deal with the caveat above

(defrule removeunknownswithpersonsalready
    (person (name ?n))
    ?up <-(unknown-person ?n)
    =>
    (retract ?up))

Now, get the gender:

(defrule getgender 
    ?up-nogen <-(unknown-person ?n)
    =>
    (retract ?up-nogen)
    (printout t crlf "Please enter male or female to indicate " ?n "'s gender" crlf )
    (assert (unknown-person ?n (read)))
)

There are other ways you can do the gender confirmation, I would have liked to use the deftemplate itself, so that the allowed-values would have fed into the validation. But I don't know how yet.

(assert (gender male))
(assert (gender female))

Now, do validation:

(defrule checkgender
    ?p <- (unknown-person ?n ?s)
    (not (gender ?s))
    =>
    (retract ?p)
    (assert (unknown-person ?n))
)

Finally, graduate from unknown

(defrule graduatefromunknown
    (declare (salience -10))
    ?up <- (unknown-person ?n ?s)
    =>
    (retract ?up)
    (assert (person (name ?n) (sex ?s)))
)

Upvotes: 0

Related Questions