can i retrieve fact's value after searching for it? Clips - clips

I know how to search to check whether fact slot exists, I need to know how to obtain the value of other slots in that fact
(deftemplate details
(slot name(type STRING))(slot occ (type STRING))......)
and
(deffacts details
(details (occ "student")(name "mike"))
(details (occ "professor")(name "john"))
.......
)
(defrule search
(userinfo(occ?occ))
(details(occ?occ) )
=>
?????????????????????????
)

Just bind them in the conditions of the rule:
(defrule search
(userinfo (occ ?occ))
(details (occ ?occ) (name ?name))
=>
(printout t "Name is " ?name crlf))

Related

hey i have a problem in this clips code can any one please fix it?

this is a clips expert system code to make a diagnosis for a disease so i am trying to find out whats the problem in that code and i really dont know what to do
[CSTRCPSR1]
[CSTRCPSR2]
error
defrule diagnosis
(symptom ?s)
(disease (symptoms ?s) (name ?d))
=>
(printout t "The patient may have " ?d "." crlf))
(defrule ask-symptoms
(not (symptom ?))
=>
(bind ?symptom (read))
(assert (symptom ?symptom)))
(defrule ask-symptoms
(not (symptom ?))
=>
(printout t "What are the patient's symptoms?" crlf)
(bind ?symptom (read))
(assert (symptom ?symptom)))
(defrule ask-symptoms-2
(symptom ?s)
(not (symptom ?))
=>
(printout t "Any other symptom? " crlf)
(bind ?symptom (read))
(assert (symptom ?symptom)))
(defclass disease
(is-a USER)
(role concrete)
(multislot symptoms)
(slot name))
(definstances diseases
(disease (symptoms fever headache) (name "influenza"))
(disease (symptoms fever sorethroat) (name "strep throat"))
(disease (symptoms cough shortness-of-breath) (name "pneumonia"))
(disease (symptoms stomachache nausea) (name "food poisoning")))
i tried everything to fix it but i really dont know where is the problem
The diagnosis defrule is missing an opening parenthesis.
The diagnosis defrule references the disease defclass before the
class is defined.
Classes automatically have a name slot defined.
You have two ask-symptoms rules.
The conditions of the ask-symptoms-2 rule will never be satisfied.
Any fact matching the first pattern will cause the following pattern
to fail.
If you want to read multiple symptoms, use the readline function
along with the explode$ function.
In the diagnosis rule, you need to use the object keyword to match
an instance of a defclass.
In the diagnosis rule, the disease pattern can only be matched by
diseases with exactly one symptom.
For example:
CLIPS (6.4 2/9/21)
CLIPS>
(defclass disease
(is-a USER)
(role concrete)
(multislot symptoms)
(slot text))
CLIPS>
(defrule diagnosis
(object (is-a disease) (name ?name) (text ?d))
(exists
(symptoms $? ?s $?)
(object (is-a disease) (name ?name) (symptoms $? ?s $?)))
=>
(printout t "The patient may have " ?d "." crlf))
CLIPS>
(defrule ask-symptoms
=>
(printout t "What are the patient's symptoms? ")
(bind ?symptoms (readline))
(assert (symptoms (explode$ ?symptoms))))
CLIPS>
(definstances diseases
(influenza of disease (symptoms fever headache) (text "influenza"))
(strep-throat of disease (symptoms fever sorethroat) (text "strep throat"))
(pneumonia of disease (symptoms cough shortness-of-breath) (text "pneumonia"))
(food-poisoning of disease (symptoms stomachache nausea) (text "food poisoning")))
CLIPS> (reset)
CLIPS> (run)
What are the patient's symptoms? cough nausea
The patient may have pneumonia.
The patient may have food poisoning.
CLIPS>

Syntax Error Check appropriate syntax for deftemplate in Clips ide

So I'm trying to build a program that takes user's input by displaying a message on the screen “Enter your subject”.
In the second rule, I'm to mention subject names “artificial intelligence”, “operating system” and “compiler construction” and check that entered subject from the first rule matches with the mentioned subjects by displaying this message on the screen.
“Your entered subject is true”.
Here is the code:
(deftemplate subject
(slot subject)
(slot artificial intelligence)
(slot operating system)
(slot compiler construction))
(defrule reading-input
(printout t "Enter your subject:")
(assert(var(read))))
(defrule checking-input
(var? artificial intelligence)
(var? operating system)
(var? compiler construction)
(com(artificial intelligence ? artificial intelligence))
(test(=?artificial intelligence ? artificial intelligence))
(test(=?operating system ? operating system))
(test(=? compiler construction ? compiler construction))
(=>
printout t "Your entered subject is true:"))
CLIPS (6.31 6/12/19)
CLIPS>
(defrule reading-input
=>
(printout t "Enter your subject: ")
(assert (var (readline))))
CLIPS>
(defrule checking-input
(var "artificial intelligence" |
"operating system" |
"compiler construction")
=>
(printout t "Your entered subject is true." crlf))
CLIPS> (reset)
CLIPS> (run)
Enter your subject: operating system
Your entered subject is true.
CLIPS> (reset)
CLIPS> (run)
Enter your subject: algorithms
CLIPS>

Test if a fact with a field value of a multifield exists in Clips

I have the following fact templates for a station and a train:
(deftemplate station
(slot name (type SYMBOL))
(slot status (type SYMBOL) (default open)))
(deftemplate train
(slot departs-from (type SYMBOL))
(multislot stops-at (type SYMBOL)))
Both slots departs-from and stops-at reference names of stations.
(deffacts trains-and-stations
(station (name station-a))
(station (name station-b))
(station (name station-c))
(train (departs-from station-a) (stops-at unknown-station))
(train (departs-from station-a) (stops-at station-b station-c))
(train (departs-from station-b) (stops-at station-c station-a)))
I want to make a rule that tests if a station fact used in each train exists:
(defrule check-valid-stations
?train <- (train (departs-from ?from) (stops-at $?stops))
(not (exists (station (name ?from))))
=>
(printout t "Route of " $train " contains an unknown station!" crlf))
The rule above only checks if a departure station is valid, how do I check if both departure station and every stop station exist?
The simplest thing to do is have separate rules that check the departs-from and stops-at stations and fire each time an unknown station is found. If you want a single message to be printed regardless of the number of unknown stations, then add an id slot to each train fact so that you can write more complex patterns that will match just once for each train.
CLIPS (6.31 6/12/19)
CLIPS>
(deftemplate station
(slot name (type SYMBOL))
(slot status (type SYMBOL) (default open)))
CLIPS>
(deftemplate train
(slot id)
(slot departs-from (type SYMBOL))
(multislot stops-at (type SYMBOL)))
CLIPS>
(deffacts trains-and-stations
(station (name station-a))
(station (name station-b))
(station (name station-c))
(train (id 1) (departs-from station-a) (stops-at unknown-station))
(train (id 2) (departs-from station-a) (stops-at station-b station-c))
(train (id 3) (departs-from station-b) (stops-at station-c station-a))
(train (id 4) (departs-from some-station) (stops-at station-c station-a bad-location)))
CLIPS>
(defrule check-valid-stations-departs
?train <- (train (departs-from ?from))
(not (station (name ?from)))
=>
(printout t "Route of " ?train " contains an unknown station: " ?from crlf))
CLIPS>
(defrule check-valid-stations-stops
?train <- (train (stops-at $? ?to $?))
(not (station (name ?to)))
=>
(printout t "Route of " ?train " contains an unknown station: " ?to crlf))
CLIPS>
(defrule check-valid-stations
?train <- (train (id ?id) (departs-from ?from))
(exists (or (not (station (name ?from)))
(and (train (id ?id) (stops-at $? ?to $?))
(not (station (name ?to))))))
=>
(printout t "Route of " ?train " contains an unknown station!" crlf))
CLIPS> (reset)
CLIPS> (run)
Route of <Fact-7> contains an unknown station: some-station
Route of <Fact-7> contains an unknown station!
Route of <Fact-7> contains an unknown station: bad-location
Route of <Fact-4> contains an unknown station!
Route of <Fact-4> contains an unknown station: unknown-station
CLIPS>

Compare one value to another to test if file exists in CLIPS

I'm trying to get user to input a book name, then test if the book exists in the library. If not the program should ask him to enter the book details. But the program sees all input as a new book. Is my comparing the two values wrong or my readline?
Code so far:
(deftemplate book (slot name) (slot author) (slot code))
(deffacts current-lib
(book (name "Alice in Wonderland") (author Lewis-Carroll) (code CAR))
(book (name "The Bourne Supremacy") (author Robert-Ludlum) (code LUD)))
(defrule readnew "inputs potential new book details"
=>
(printout t "Enter the name of the book:")
(bind ?b_name (readline))
(assert (potential ?b_name)))
(defrule add-book "determine if book already exists otherwise add"
?out <- (potential ?newname)
(and (potential ?newname)
(not (book (name ?b_name&?newname) (author $?))))
=>
(printout t "Book is new, please enter the author's name:" crlf)
(bind ?auth (readline))
(printout t "Please enter a three letter code for the book:" crlf)
(bind ?coode (read))
(assert (book (name ?newname) (author ?auth) (code ?coode)))
(retract ?out))
You provided code, but not the steps you took to run it so I'll have to guess at the cause of your problem. The simplest explanation would be that you did not issue a reset command to assert the facts in your current-lib deffacts.
I made a few changes to your code. In your current-lib deffacts, the author names should be strings since you're using readline in your add-book rule to get the names. There is also unnecessary code in the conditions of your add-book rule.
CLIPS (6.31 2/3/18)
CLIPS>
(deftemplate book
(slot name)
(slot author) (slot code))
CLIPS>
(deffacts current-lib
(book (name "Alice in Wonderland") (author "Lewis Carroll") (code CAR))
(book (name "The Bourne Supremacy") (author "Robert Ludlum") (code LUD)))
CLIPS>
(defrule readnew
=>
(printout t "Enter the name of the book:" crlf)
(bind ?b_name (readline))
(assert (potential ?b_name)))
CLIPS>
(defrule add-book
?out <- (potential ?newname)
(not (book (name ?newname)))
=>
(printout t "Book is new, please enter the author's name:" crlf)
(bind ?auth (readline))
(printout t "Please enter a three letter code for the book:" crlf)
(bind ?coode (read))
(assert (book (name ?newname) (author ?auth) (code ?coode)))
(retract ?out))
CLIPS>
Now, if you add a book that doesn't exist you'll be ask for the additional information.
CLIPS> (reset)
CLIPS> (run)
Enter the name of the book:
Ringworld
Book is new, please enter the author's name:
Larry Niven
Please enter a three letter code for the book:
RNG
CLIPS>
If you try to add a book that doesn't exist, the add-book rule won't execute.
CLIPS> (reset)
CLIPS> (run)
Enter the name of the book:
Alice in Wonderland
CLIPS>

Rules activates but not firing in JESS

I am new to JESS and I know a little of its functionality. I have a program that asks for a students id-number and the program goes and gets the student's subjects.
The thing is by the end of procedure the program asks the user if he/she wants to get another student's subjects.
If the user wants to input another student, It will go back to the first rule that fired but when I tried that, the rule only activated and did not fire.
What could be the problem? I made sure there was (run) of course.
Here's my code.
(defrule check-subject
?a <- (phase check-back)
?stud <- (lookupID (id ?id))
(student-information (id-number ?id)
(course ?course)
(subjects-taken $?taken)
(year ?year)
(semester ?sem))
(prospectus-information (curriculum-name ?course)
(1st-year-1st-sem-subjects $?subjects1))
=>
(printout t "Student took: " (intersection$ $?taken $?subjects1) crlf)
(printout t "Student flunked: " (complement$ $?taken $?subjects1) crlf)
(assert (back-subject (complement$ $?taken $?subjects1)))
(retract ?a ?stud)
(ask))
And I have the function ask
(deffunction ask ()
(printout t "Consult another? (y/n) ")
(if (eq (read) y)
then (assert (phase choose-student))))
And my defrule when phase choose-student is asserted
(defrule student-select
(phase choose-student)
=>
(printout t "Input Student ID: ")
(assert (lookupID (id (read)))))
The rule student-select, activates but never fires. My program stops there.
It's better to write the control part of an application separate from the rules.
(deffunction ask ()
(printout t "Consult another? (y/n) ")
(if (eq (read) y) then
(printout t "Input Student ID: ")
(assert (lookupID (id (read))))
(return TRUE)
else
(return FALSE)))
Use this in a while function:
(while (ask) do (run))
And you won't need the (phase choose-student) any more.

Resources