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:
Create a fact of the form (unknown-person ) for each name that appears in the children multislot of a person fact but not in the name slot of any person fact (it is assumed that no two people have the same name)
For each fact of the form (unknown-person ) ask the user for the sex of the person, retract the fact and assert a new fact of the form (unknown-person ).
For each fact of the form (unknown-person ), retract the fact and create a new person fact for the person (it is assumed that the person has no children).
Your rules should do data validation to ensure that only an allowed value for is supplied by the user
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)))
)
Related
I'm a beginner to CLIPS. The goal of this program is to return the name of the company that best matches the user's demographics. I ask the user their gender and (might be storing it wrong) store it into input, ethnicity stored into input2, and age into input3. I'm running into errors when trying to do comparisons to return the company name. Eg. if input=1 (man), then compare the demographics of men in both companies and return the name of the company with more men.
(= 1 input) this line in the last rule gives me error "Function '=' expected argument #2 to be of type integer or float." which I thought I've already only allowed responses of integers. Any help would be appreciated, thank you!
(deftemplate company
(slot name)
(slot men)
(slot women)
(slot Asian)
(slot Black)
(slot Latinx)
(slot Indigenous)
(slot White)
(slot Other)
(slot <18)
(slot 18-20)
(slot 21-29)
(slot 30-39)
(slot 40+))
(deffacts demographics
(company (name Google)(men 0.669)(women 0.331)(Asian 0.428)(Black 0.088)(Latinx 0.088)(Indigenous 0.007)(White 0.445)(Other 0)(<18 0.02)(18-20 0.11)(21-29 0.59)(30-39 0.19)(40+ 0.07))
(company (name Apple)(men 0.652)(women 0.348)(Asian 0.279)(Black 0.094)(Latinx 0.148)(Indigenous 0.007)(White 0.438)(Other 0.032)(<18 0.01)(18-20 0.1)(21-29 0.57)(30-39 0.22)(40+ 0.06)))
(defrule begin => (assert (phase select-gender)))
(defrule menu (phase select-gender) =>
(printout t "Do you identify as a (1) man or (2) woman?: ") (assert (userinput (read))))
(defrule selection-okay
?phase <- (phase select-gender)
?input <- (userinput ?select&1|2)
=>
(retract ?phase)
(assert (selection ?select))
(assert (phase select-ethnicity)))
(defrule selection-nokay
?phase <- (phase select-gender)
?input <- (userinput ?select&~1&~2)
=>
(retract ?phase)
(assert (phase select-gender))
(printout t ?select " is not a valid response. Please enter 1 or 2."crlf))
(defrule menu2 (phase select-ethnicity) =>
(printout t "Do you identify as (1)Asian, (2)Black, (3)Latinx, (4)Indigenous, (5)White, or (6)Other?: ") (assert (userinput2 (read))))
(defrule selection-okay2
?phase <- (phase select-ethnicity)
?input2 <- (userinput2 ?select&1|2|3|4|5|6)
=>
(retract ?phase)
(assert (selection ?select))
(assert (phase select-age)))
(defrule selection-nokay2
?phase <- (phase select-ethnicity)
?input2 <- (userinput2 ?select&~1&~2&~3&~4&~5&~6)
=>
(retract ?phase)
(assert (phase select-ethnicity))
(printout t ?select " is not a valid response. Please enter 1 through 6."crlf))
(defrule menu3 (phase select-age) =>
(printout t "You are age: (1)<18, (2)18-20, (3)20-30, (4)30-40, (5)40+?: ") (assert (userinput3 (read))))
(defrule selection-okay3
?phase <- (phase select-age)
?input3 <- (userinput3 ?select&1|2|3|4|5)
=>
(retract ?phase)
(assert (selection ?select))
(assert (phase company-gender-men)))
(defrule selection-nokay3
?phase <- (phase select-age)
?input3 <- (userinput3 ?select&~1&~2&~3&~4&~5)
=>
(retract ?phase)
(assert (phase select-age))
(printout t ?select " is not a valid response. Please enter 1 through 5."crlf))
(defrule get-company-by-gender-men
?phase <- (company-gender-men)
(= 1 input)
(> (fact-slot-value 1 men) (fact-slot-value 2 men))
=>
(retract ?phase)
(printout t (fact-slot-value 1 name) crlf))
;(fact-slot-value 1 name)
;(> (fact-slot-value 1 men) (fact-slot-value 2 men))
I have tried removing some extra code just to see if I can access the previous user input but I'm still not getting an output.
(deftemplate company
(slot name)
(slot men)
(slot women)
(slot Asian)
(slot Black)
(slot Latinx)
(slot Indigenous)
(slot White)
(slot Other)
(slot <18)
(slot 18-20)
(slot 21-29)
(slot 30-39)
(slot 40+))
(deffacts demographics
(company (name Google)(men 0.669)(women 0.331)(Asian 0.428)(Black 0.088)(Latinx 0.088)(Indigenous 0.007)(White 0.445)(Other 0)(<18 0.02)(18-20 0.11)(21-29 0.59)(30-39 0.19)(40+ 0.07))
(company (name Apple)(men 0.652)(women 0.348)(Asian 0.279)(Black 0.094)(Latinx 0.148)(Indigenous 0.007)(White 0.438)(Other 0.032)(<18 0.01)(18-20 0.1)(21-29 0.57)(30-39 0.22)(40+ 0.06)))
(defrule begin => (assert (phase select-gender)))
(defrule menu (phase select-gender) =>
(printout t "Do you identify as a (1) man or (2) woman?: ") (assert (userinput (read)))(assert(phase select-ethnicity)))
(defrule menu2 (phase select-ethnicity) =>
(printout t "Do you identify as (1)Asian, (2)Black, (3)Latinx, (4)Indigenous, (5)White, or (6)Other?: ") (assert (userinput2 (read)))(assert(phase select-age)))
(defrule menu3 (phase select-age) =>
(printout t "You are age: (1)<18, (2)18-20, (3)20-30, (4)30-40, (5)40+?: ") (assert (userinput3 (read)))(assert(phase company-gender-men)))
(defrule get-company-by-gender-men
?phase <- (company-gender-men)
(userinput ?userinput)
=>
(retract ?phase)
(printout t ?userinput "can see user input"crlf))
(defrule get-company-by-gender-men
?phase <- (phase company-gender-men)
(userinput 1)
(company (name ?name) (men ?men1))
(company (men ?men2))
(test (> ?men1 ?men2))
=>
(retract ?phase)
(printout t ?name crlf))
I am with the following CLIPS programming problem:
New family relationship knowledge base. Please apply the following specification:
use deftemplate to define the person
a person’s attributes are: name, age, sex, children,
each attribute should be defined as a slot except children, that are multi slot
in sex slot use the (allowed-symbols female male)
add data type to each slot
use deffacts to add people from diagram
define rules:
—mother,
—father,
—sister,
—predecessor
—old — giving the difference in age between predecessor and offspring
This is the code I made:
(deftemplate person
(slot name (type SYMBOL))
(slot age (type INTEGER))
(slot sex (type SYMBOL) (allowed-symbols female male))
(multislot children (type SYMBOL))
)
(deffacts init
(person (name Fernando) (age 55) (sex male) (children Jesus Celia))
(person (name Maria) (age 50) (sex female) (children Jesus Celia))
(person (name Jesus) (age 25) (sex male))
(person (name Celia) (age 20) (sex female))
(person (name Antonio) (age 70) (sex male) (children Fernando Ana))
(person (name Ana) (age 38) (sex female))
(person (name Calra) (age 68) (sex female) (children Fernando Ana))
)
(defrule mother
(person (name ?name) (sex female) (children $?before ?child $?after))
=>
(assert (mother ?name ?child))
(printout t ?name " is mother of " $?child crlf))
(defrule father
(person (name ?name) (sex male) (children $?before ?child $?after))
=>
(assert (father ?name ?child))
(printout t ?name " is father of " $?child crlf))
(defrule sister
(children ?z ?x)
(children ?z ?y)
(female ?x)
(not (test (eq ?x ?y)))
=>
(assert sister ?x ?y))
(printout t ?x "is a sister to " ?y crlf))
(defrule predecessor
(or
(children ?x ?y)
(and (children ?x ?z)(predecessor ?z ?y))
)
=>
(assert (predecessor ?x ?y)
(printout t ?x "is a predecessor to " ?y crlf))
(defrule old
?fact_no <- (person (name ?n) (age ?a1 ?a2))
(test (<= ?a1 ?a2))
=>
(assert (result (- ?a1 ?a2)))
(printout t "There are " ?result " years" crlf))
The mother and father relationship is working but not the rest. It gives me the following ERRORS:
CLIPS> Loading Selection...
[CSTRCPSR4] Cannot redefine deftemplate person while it is in use.
ERROR:
(deftemplate MAIN::person
[CSTRCPSR1] WARNING: Redefining deffacts: init
[CSTRCPSR1] WARNING: Redefining defrule: mother +j+j
Defining defrule: father +j+j
[CSTRCPSR1] WARNING: Redefining defrule: sister
[PRNTUTIL2] Syntax Error: Check appropriate syntax for RHS patterns.
ERROR:
(defrule MAIN::sister
(children ?z ?x)
(children ?z ?y)
(female ?x)
(not (test (eq ?x ?y)))
=>
(assert sister
Defining defrule: predecessor
[EXPRNPSR3] Missing function declaration for defrule.
ERROR:
(defrule MAIN::predecessor
(or (children ?x ?y)
(and (children ?x ?z)
(predecessor ?z ?y)))
=>
(assert (predecessor ?x ?y)
(printout t ?x "is a predecessor to " ?y crlf))
(defrule
Thank you so much in advance
If you already have code loaded, issue a clear command before you try to load the same code a second time. That will remove the [CSTRCPSR4] error message.
You have missing parentheses in the sister and predecessor rules. Correcting these will allow the rules to load without errors.
(defrule sister
(children ?z ?x)
(children ?z ?y)
(female ?x)
(not (test (eq ?x ?y)))
=>
(assert (sister ?x ?y))
(printout t ?x "is a sister to " ?y crlf))
(defrule predecessor
(or
(children ?x ?y)
(and (children ?x ?z)(predecessor ?z ?y))
)
=>
(assert (predecessor ?x ?y))
(printout t ?x "is a predecessor to " ?y crlf))
These rules will still not execute with your existing code because they require children and female facts and none of your rules assert these facts.
It's not clear what you're trying to do with your old rule, but since you're trying to match two values in the age slot this rule will generate errors because that slot can only contain one value.
I am struggling with the following CLIPS programming problem:
Problem: write a set of rules to define family relations like:
(brother ?x ?y) (i.e. "x is a brother of y")
(sister ?x ?y) (i.e. "x is a sister of y")
(son ?x ?y) (i.e. "x is a son of y")
(daughter ?x ?y) (i.e. "x is a daughter of y")
The constraint in this task is that the rules can be constructed only from the following premises:
(father ?x ?y) (i.e. "x is a father of y")
(mother ?x ?y) (i.e. "x is a mother of y")
(male ?x) (i.e. "x is a male")
(female ?y) (i.e. "y is a female")
The task also assumes that there must be some initial facts provided and that to check the correctness there should be an implementation of displaying information about derived conclusions.
My attempt
I created templates and initial facts as follows:
(deftemplate father-of
(slot father)
(slot child)
)
(deftemplate mother-of
(slot mother)
(slot child)
)
(deftemplate male
(slot person)
)
(deftemplate female
(slot person)
)
(deffacts family
(mother-of(mother Anna) (child Tracy)
(mother-of(mother Anna) (child Cindy)
(female Anna)
(female Tracy)
(female Cindy)
)
My attempt in writing a rule for checking if certain person is a sister of other person was as follows:
(defrule sister-of
(and
(female (person ?x))
(female (person ?y))
(female (person ?z))
(mother-of (mother ?x) (child ?y))
(mother-of (mother ?x) (child ?z))
)
=>
(assert (sister ?y ?z))
(printout t ?y " is a sister of " ?z crlf)
)
Output error
Once I load .clp file I consistenly obtain the following error message of such form:
CLIPS (6.30 3/17/15)
CLIPS> (reset)
CLIPS> (clear)
CLIPS> (load family.clp)
Defining deftemplate: father-of
Defining deftemplate: mother-of
Defining deftemplate: male
Defining deftemplate: female
Defining deffacts: family
[PRNTUTIL2] Syntax Error: Check appropriate syntax for deftemplate pattern.
ERROR:
(deffacts MAIN::family
(mother-of (mother Anna) (child Markus))
(female Anna
FALSE
CLIPS>
My attempts
I checked CLIPS guides regarding basic programming, googled error message, but I did not make any progress.
Help will be greatly appreciated!!! For me it suffices to see how this stuff works in the case of the case of writing a rule (sister ?x ?y) with all the templates and facts provided above.
If you define a deftemplate for a fact, you must include the slot names when specifying the slot values.
(deffacts family
(mother-of(mother Anna) (child Tracy))
(mother-of(mother Anna) (child Cindy))
(female (person Anna))
(female (person Tracy))
(female (person Cindy))
)
Is there any way to get the objects in clips in order on LHS side based on a particular slot in class?
(defclass SAMPLE
"all the information about students"
(is-a BASE_SAMPLE) (role concrete) (pattern-match reactive)
(slot ID (create-accessor read-write) (access initialize-only) (propagation inherit) (visibility public) (type INTEGER))
(slot NAME (create-accessor read-write) (access initialize-only) (propagation inherit) (visibility public) (type STRING))
)
if I have 100 SAMPLE objects, and I want all of them to come in ascending order based on the slot ID on the LHS of a rule, is this poosilbe in clips?
There's two ways you can sort the objects. You can do it on the LHS by adding some additional information to either the objects or a separate fact/instance to retain information on which objects have been processed:
CLIPS> (clear)
CLIPS>
(defclass STUDENT
(is-a USER)
(slot id)
(slot full-name)
(slot processed (default no)))
CLIPS>
(definstances people
(of STUDENT (id 102) (full-name "Fred Jones"))
(of STUDENT (id 438) (full-name "Sally Smith"))
(of STUDENT (id 391) (full-name "John Farmer")))
CLIPS>
(defrule list
?i <- (object (is-a STUDENT)
(id ?id1)
(processed no))
(not (object (is-a STUDENT)
(id ?id2&:(> ?id1 ?id2))
(processed no)))
=>
(modify-instance ?i (processed yes))
(printout t ?id1 " " (send ?i get-full-name) crlf))
CLIPS> (reset)
CLIPS> (run)
102 Fred Jones
391 John Farmer
438 Sally Smith
CLIPS>
Or you can sort the values on the RHS:
CLIPS> (clear)
CLIPS>
(defclass STUDENT
(is-a USER)
(slot id)
(slot full-name))
CLIPS>
(definstances students
(of STUDENT (id 102) (full-name "Fred Jones"))
(of STUDENT (id 438) (full-name "Sally Smith"))
(of STUDENT (id 391) (full-name "John Farmer")))
CLIPS>
(deffunction id-sort (?i1 ?i2)
(> (send ?i1 get-id) (send ?i2 get-id)))
CLIPS>
(defrule list
=>
(bind ?instances (find-all-instances ((?i STUDENT)) TRUE))
(bind ?instances (sort id-sort ?instances))
(progn$ (?i ?instances)
(printout t (send ?i get-id) " " (send ?i get-full-name) crlf)))
CLIPS> (reset)
CLIPS> (run)
102 Fred Jones
391 John Farmer
438 Sally Smith
CLIPS>
I know that rules in CLIPS are usually given names using defrule. This rule is called rule1:
(deftemplate animal (slot species))
(deffacts animal
(animal (species dog)))
(defrule rule1
(animal (species ?name))
=>
(printout t ?name crlf))
(reset)
(run)
(exit)
;
But I still don't know whether it is necessary for the rules to be named. Is it possible to define a rule without giving it a name, like this?
(deftemplate animal (slot species))
(deffacts animal
(animal (species dog)))
(defrule
(animal (species ?name))
=>
(printout t ?name crlf))
(reset)
(run)
(exit)
;
From section 5 of the Basic Programming Guide:
(defrule <rule-name> [<comment>]
[<declaration>]
<conditional-element>*
=>
<action>*)
The rule name is required.
To dynamically generate a rule name, use the gensym* function which creates a unique symbol:
CLIPS>
(deffunction create-anonymous (?conditions ?actions)
(bind ?str (str-cat "(defrule " (gensym*) " "
?conditions " => " ?actions ")"))
(build ?str))
CLIPS> (deftemplate animal (slot species))
CLIPS>
(create-anonymous "(animal (species ?name))"
"(printout t ?name crlf)")
TRUE
CLIPS> (rules)
gen1
For a total of 1 defrule.
CLIPS> (ppdefrule gen1)
(defrule MAIN::gen1
(animal (species ?name))
=>
(printout t ?name crlf))
CLIPS>