Sort a set of facts CLIPS - clips

I am trying to order a collection of facts in CLIPS according to a comparator based on two fields ...
Unfortunately I cannot understand why the comparator (apparently right) prints 2 if two facts are passed in which the first field is the same.
MY COMPARATOR:
(deffunction MAIN::rating-sort (?f1 ?f2)
(printout t ?f1 crlf)
(printout t ?f2 crlf)
(printout t "f1-SC " (fact-slot-value ?f1 sum-certainties) crlf)
(printout t "f2-SC " (fact-slot-value ?f2 sum-certainties) crlf)
(printout t "f1-TP " (fact-slot-value ?f1 total-price) crlf)
(printout t "f2-TP " (fact-slot-value ?f2 total-price) crlf)
(if (< (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) then (printout t "1" crlf) return TRUE
else (if (> (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) then (printout t "2" crlf) return FALSE
else (if (> (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) then (printout t "3" crlf) return TRUE
else (if (< (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) then (printout t "4" crlf) return FALSE
else (printout t "5" crlf) return FALSE)))))
THE FACTS IN WM:
f-64 (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 1 0 0 0) (total-price 75.0) (certainty 14.0 -0.001 -0.001 -0.001) (sum-certainties 13.997) (flag TRUE))
f-66 (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 1 0 0) (total-price 100.0) (certainty -0.001 14.0 -0.001 -0.001) (sum-certainties 13.997) (flag TRUE))
f-68 (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 0 1 0) (total-price 75.0) (certainty -0.001 -0.001 14.0 -0.001) (sum-certainties 13.997) (flag TRUE))
f-70 (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 0 0 1) (total-price 100.0) (certainty -0.001 -0.001 -0.001 14.0) (sum-certainties 13.997) (flag TRUE))
MY COMPARATOR OUTPUT:
<Fact-64>
<Fact-66>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 100.0
4
<Fact-68>
<Fact-70>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 100.0
4
<Fact-64>
<Fact-68>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 75.0
2
<Fact-66>
<Fact-68>
f1-SC 13.997
f2-SC 13.997
f1-TP 100.0
f2-TP 75.0
2
I don't understand how 13.997 can be greater than 13.997.
Thank you all in advance.

You need to be using (return TRUE) and (return FALSE) in your deffunction rather than return TRUE and return FALSE, but the code works the same even with this change. The comparator never prints 2.
CLIPS (6.31 6/12/19)
CLIPS>
(deffunction rating-sort (?f1 ?f2)
(printout t ?f1 crlf)
(printout t ?f2 crlf)
(printout t "f1-SC " (fact-slot-value ?f1 sum-certainties) crlf)
(printout t "f2-SC " (fact-slot-value ?f2 sum-certainties) crlf)
(printout t "f1-TP " (fact-slot-value ?f1 total-price) crlf)
(printout t "f2-TP " (fact-slot-value ?f2 total-price) crlf)
(if (< (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties))
then
(printout t "1" crlf) return TRUE
else
(if (> (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties))
then (printout t "2" crlf) return FALSE
else
(if (> (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price))
then (printout t "3" crlf) return TRUE
else
(if (< (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price))
then (printout t "4" crlf) return FALSE
else (printout t "5" crlf) return FALSE)))))
CLIPS>
(deftemplate alternative
(multislot hotels)
(multislot times)
(slot total-price)
(multislot certainty)
(slot sum-certainties)
(slot flag))
CLIPS>
(deffacts alternatives
(alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 1 0 0 0)
(total-price 75.0) (certainty 14.0 -0.001 -0.001 -0.001)
(sum-certainties 13.997) (flag TRUE))
(alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 1 0 0)
(total-price 100.0) (certainty -0.001 14.0 -0.001 -0.001)
(sum-certainties 13.997) (flag TRUE))
(alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 0 1 0)
(total-price 75.0) (certainty -0.001 -0.001 14.0 -0.001)
(sum-certainties 13.997) (flag TRUE))
(alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 0 0 1)
(total-price 100.0) (certainty -0.001 -0.001 -0.001 14.0)
(sum-certainties 13.997) (flag TRUE)))
CLIPS> (reset)
CLIPS> (sort rating-sort (find-all-facts ((?f alternative)) TRUE))
<Fact-1>
<Fact-2>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 100.0
4
<Fact-3>
<Fact-4>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 100.0
4
<Fact-1>
<Fact-3>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 75.0
5
<Fact-2>
<Fact-3>
f1-SC 13.997
f2-SC 13.997
f1-TP 100.0
f2-TP 75.0
3
<Fact-2>
<Fact-4>
f1-SC 13.997
f2-SC 13.997
f1-TP 100.0
f2-TP 100.0
5
(<Fact-1> <Fact-3> <Fact-2> <Fact-4>)
CLIPS>

Related

CLIPS beginner: How CLIPS rules interpret the facts order?

I would like to know in each day which activities I do, so I constructed the following code:
(deftemplate schedule
(slot activity)
(slot starthour)
(slot endhour)
)
(defrule r1
(schedule (activity ?a) (starthour ?start) (endhour ?end))
(not (busy ?start ?a))
=>
(assert (busy ?start ?a))
)
(defrule r2
(busy ?d ?a)
(schedule (activity ?a) (starthour ?start) (endhour ?end))
(test (< ?d ?end))
=>
(assert (busy ( + ?d 1) ?a))
)
CLIPS> (assert (schedule (activity reading) (starthour 3) (endhour 5)))
<Fact-1>
CLIPS> (assert (schedule (activity music) (starthour 4) (endhour 7)))
<Fact-2>
For the facts that I inserted I obtained a result that does not order the days.
How CLIPS interpret the order of the facts?
Is there a way to me to combine in a day more than 1 activity?
Thanks very much!
By default CLIPS uses a depth first strategy for determining which rule to execute next, so generally the next rule executed will have been activated by the last fact asserted or retracted. Section 5.3, Conflict Resolution Strategies, in the Basic Programming Guide describes this process in greater detail.
As long as the correct facts are being generated by the rules, you shouldn't be particularly concerned with the order in which they're place on the fact-list because 1) It's not practical to use the fact-list to display the output of a program and 2) forcing a particular order of placement can be difficult and overly complex.
Instead, to order facts in your program output, collect all of the relevant facts using one of the fact query functions and then use the sort function with a custom comparator to order the facts before printing them:
CLIPS (6.4 2/9/21)
CLIPS>
(deftemplate schedule
(slot activity)
(slot starthour)
(slot endhour))
CLIPS>
(deftemplate busy
(slot activity)
(slot hour))
CLIPS>
(defrule r1
(schedule (activity ?a) (starthour ?start) (endhour ?end))
(not (busy (activity ?a) (hour ?start)))
=>
(assert (busy (activity ?a) (hour ?start))))
CLIPS>
(defrule r2
(busy (activity ?a) (hour ?d))
(schedule (activity ?a) (starthour ?start) (endhour ?end))
(test (< ?d ?end))
=>
(assert (busy (activity ?a) (hour (+ ?d 1)))))
CLIPS>
(deffacts schedules
(schedule (activity reading) (starthour 3) (endhour 5))
(schedule (activity music) (starthour 4) (endhour 7)))
CLIPS>
(deffunction busy-compare (?f1 ?f2)
;; Sort by hour
(if (> (fact-slot-value ?f2 hour) (fact-slot-value ?f1 hour))
then (return FALSE))
(if (< (fact-slot-value ?f2 hour) (fact-slot-value ?f1 hour))
then (return TRUE))
;; And then sort by activity
(if (> (str-compare (fact-slot-value ?f2 activity)
(fact-slot-value ?f1 activity)) 0)
then (return FALSE)
else (return TRUE)))
CLIPS>
(defrule print
(declare (salience -10))
=>
(bind ?schedule (find-all-facts ((?f busy)) TRUE))
(bind ?schedule (sort busy-compare ?schedule))
(foreach ?s ?schedule
(format t "%2d %s%n" (fact-slot-value ?s hour) (fact-slot-value ?s activity))))
CLIPS> (reset)
CLIPS> (run)
3 reading
4 music
4 reading
5 music
5 reading
6 music
7 music
CLIPS> (facts)
f-1 (schedule (activity reading) (starthour 3) (endhour 5))
f-2 (schedule (activity music) (starthour 4) (endhour 7))
f-3 (busy (activity music) (hour 4))
f-4 (busy (activity music) (hour 5))
f-5 (busy (activity music) (hour 6))
f-6 (busy (activity music) (hour 7))
f-7 (busy (activity reading) (hour 3))
f-8 (busy (activity reading) (hour 4))
f-9 (busy (activity reading) (hour 5))
For a total of 9 facts.
CLIPS>
Here's another way to do it storing multiple activities in each busy fact:
CLIPS> (clear)
CLIPS>
(deftemplate schedule
(slot activity)
(slot starthour)
(slot endhour))
CLIPS>
(deftemplate busy
(multislot activity)
(slot hour))
CLIPS>
(defrule r1
(schedule (activity ?a) (starthour ?start) (endhour ?end))
=>
(loop-for-count (?hour ?start ?end)
(assert (busy (activity ?a) (hour ?hour)))))
CLIPS>
(defrule combine
?b1 <- (busy (activity $?a) (hour ?d))
?b2 <- (busy (activity ?n&:(not (member$ ?n ?a))) (hour ?d))
=>
(modify ?b1 (activity ?a ?n))
(retract ?b2))
CLIPS>
(deffacts schedules
(schedule (activity reading) (starthour 3) (endhour 5))
(schedule (activity music) (starthour 4) (endhour 7)))
CLIPS>
(deffunction busy-compare (?f1 ?f2)
(if (> (fact-slot-value ?f2 hour) (fact-slot-value ?f1 hour))
then (return FALSE))
(if (< (fact-slot-value ?f2 hour) (fact-slot-value ?f1 hour))
then (return TRUE))
(return FALSE))
CLIPS>
(defrule print
(declare (salience -10))
=>
(bind ?schedule (find-all-facts ((?f busy)) TRUE))
(bind ?schedule (sort busy-compare ?schedule))
(foreach ?s ?schedule
(format t "%2d %s%n" (fact-slot-value ?s hour) (implode$ (fact-slot-value ?s activity)))))
CLIPS> (reset)
CLIPS> (run)
3 reading
4 reading music
5 reading music
6 music
7 music
CLIPS> (facts)
f-1 (schedule (activity reading) (starthour 3) (endhour 5))
f-2 (schedule (activity music) (starthour 4) (endhour 7))
f-5 (busy (activity music) (hour 6))
f-6 (busy (activity music) (hour 7))
f-7 (busy (activity reading) (hour 3))
f-8 (busy (activity reading music) (hour 4))
f-9 (busy (activity reading music) (hour 5))
For a total of 7 facts.
CLIPS>

Two-field comparator CLIPS

I need to write a comparator to order facts in CLIPS. This comparator must sort the facts according to a first field (sum-certanties) and if the first field were not enough to find a sort, I would like it to order them according to the second field (total-price).
This is what I wrote, but it doesn't work ...
(deffunction MAIN::rating-sort (?f1 ?f2)
(if (< (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) then return TRUE
else (if (> (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) then return FALSE
else (if (> (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) then return TRUE
else (if (< (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) then return FALSE
else then return FALSE)))))
Descending order for sum-certainties and ascending order for total-price.
Your function is fine. You just need to pass its name and a list of facts into the sort function.
CLIPS (6.31 6/12/19)
CLIPS>
(deffunction MAIN::rating-sort (?f1 ?f2)
(if (< (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) then return TRUE
else (if (> (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) then return FALSE
else (if (> (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) then return TRUE
else (if (< (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) then return FALSE
else then return FALSE)))))
CLIPS>
(deftemplate thing
(slot sum-certainties)
(slot total-price))
CLIPS>
(deffacts things
(thing (sum-certainties 90) (total-price 200))
(thing (sum-certainties 30) (total-price 100))
(thing (sum-certainties 30) (total-price 300))
(thing (sum-certainties 90) (total-price 150))
(thing (sum-certainties 50) (total-price 150))
(thing (sum-certainties 70) (total-price 200)))
CLIPS> (reset)
CLIPS>
(foreach ?f (sort rating-sort (find-all-facts ((?f thing)) TRUE))
(printout t (fact-slot-value ?f sum-certainties) " " (fact-slot-value ?f total-price) crlf))
90 150
90 200
70 200
50 150
30 100
30 300
CLIPS>

CLIPS compare 2 dates

I'm trying to compare 2 dates to see if one person is older than 25 years old. I tried to bind a variable to the subtraction of the current date and his birthday, and then compare the variable to 25:
(deftemplate data
(multislot current_date (type INTEGER))
)
(deffacts today
(data (current_date 12 4 2018))
)
(deftemplate driver
(multislot name)
(multislot dateBorn)
)
(deffacts drivers
(driver (name Daniel Silva)(dateBorn 3 4 1985))
(driver (name Carlos Santos)(dateBorn 3 4 2000))
)
(defrule cantDrive
(driver(dateBorn $? ?age))
(data (current_date $? ?date))
(bind ?data (- ?age ?date))
(test(< ?data 25))
=>
(printout t "He is younger than 25" crlf)
)
The above code doesn’t run, and I don’t understand why. Is there any operation that is incorrect? Is there any way to compare two dates? For example, I was born in 26/06/1997 and if I need to be at least 25 to rent a car, how do I confirm that?
I can set the current date.
CLIPS (6.31 2/3/18)
CLIPS>
(deffunction current-date ()
(bind ?lt (local-time))
(format nil "%04d-%02d-%02d" (nth$ 1 ?lt) (nth$ 2 ?lt) (nth$ 3 ?lt)))
CLIPS>
(deffunction is-leap-year (?year)
(if (= (mod ?year 400) 0) then (return TRUE))
(if (= (mod ?year 100) 0) then (return FALSE))
(if (= (mod ?year 4) 0) then (return TRUE))
(return FALSE))
CLIPS>
(defglobal ?*days-before-month* = (create$ 0 31 59 90 120 151 181 212 243 273 304 334))
CLIPS> (defglobal ?*days-before-month-leap-year* = (create$ 0 31 60 91 121 152 182 213 244 274 305 335))
CLIPS>
(deffunction days-from-year-begin (?date)
(bind ?year (string-to-field (sub-string 1 4 ?date)))
(bind ?month (string-to-field (sub-string 6 7 ?date)))
(bind ?day (string-to-field (sub-string 9 10 ?date)))
(if (is-leap-year ?year)
then
(return (+ (nth$ ?month ?*days-before-month-leap-year*) ?day))
else
(return (+ (nth$ ?month ?*days-before-month*) ?day))))
CLIPS>
(deffunction days-until-year-end (?date)
(bind ?year (string-to-field (sub-string 1 4 ?date)))
(bind ?month (string-to-field (sub-string 6 7 ?date)))
(bind ?day (string-to-field (sub-string 9 10 ?date)))
(if (is-leap-year ?year)
then
(return (- 366 (+ (nth$ ?month ?*days-before-month-leap-year*) ?day)))
else
(return (- 365 (+ (nth$ ?month ?*days-before-month*) ?day)))))
CLIPS>
(deffunction date-days-diff (?date1 ?date2)
(bind ?year1 (string-to-field (sub-string 1 4 ?date1)))
(bind ?year2 (string-to-field (sub-string 1 4 ?date2)))
(if (= ?year1 ?year2)
then
(return (- (days-from-year-begin ?date1) (days-from-year-begin ?date2))))
(if (> ?year1 ?year2)
then
(bind ?negate FALSE)
else
(bind ?negate TRUE)
(bind ?temp ?date1)
(bind ?date1 ?date2)
(bind ?date2 ?temp)
(bind ?temp ?year1)
(bind ?year1 ?year2)
(bind ?year2 ?temp))
(bind ?day-count (+ (days-until-year-end ?date2) (days-from-year-begin ?date1)))
(loop-for-count (?year (+ ?year2 1) (- ?year1 1)) do
(if (is-leap-year ?year)
then (bind ?day-count (+ ?day-count 366))
else (bind ?day-count (+ ?day-count 365))))
(if ?negate
then
(return (- 0 ?day-count))
else
(return ?day-count)))
CLIPS>
(deffunction date-years-diff (?date1 ?date2)
(bind ?year1 (string-to-field (sub-string 1 4 ?date1)))
(bind ?year2 (string-to-field (sub-string 1 4 ?date2)))
(if (= ?year1 ?year2)
then
(return 0))
(if (> ?year1 ?year2)
then
(bind ?negate FALSE)
else
(bind ?negate TRUE)
(bind ?temp ?date1)
(bind ?date1 ?date2)
(bind ?date2 ?temp))
(bind ?year1 (string-to-field (sub-string 1 4 ?date1)))
(bind ?year2 (string-to-field (sub-string 1 4 ?date2)))
(bind ?month1 (string-to-field (sub-string 6 7 ?date1)))
(bind ?month2 (string-to-field (sub-string 6 7 ?date2)))
(bind ?day1 (string-to-field (sub-string 9 10 ?date1)))
(bind ?day2 (string-to-field (sub-string 9 10 ?date2)))
(bind ?years (- ?year1 ?year2))
(if (= ?month1 ?month2)
then
(if (< ?day1 ?day2)
then
(bind ?years (- ?years 1)))
else
(if (< ?month1 ?month2)
then
(bind ?years (- ?years 1))))
(if ?negate
then (return (- 0 ?years))
else (return ?years)))
CLIPS>
(deftemplate driver
(slot name)
(slot dateBorn))
CLIPS>
(deffacts drivers
(driver (name "Daniel Silva") (dateBorn "1985-03-04"))
(driver (name "Carlos Santos") (dateBorn "2000-03-04")))
CLIPS>
(defrule cantDrive
(driver (name ?name) (dateBorn ?born))
(test (< (date-years-diff (current-date) ?born) 25))
=>
(printout t ?name " is younger than 25" crlf))
CLIPS> (reset)
CLIPS> (run)
Carlos Santos is younger than 25
CLIPS>

check multiple fields in if() CLIPS

I'm trying to do a comparation in a rule on CLIPS thah check if one of three conditions it's true to assert a new fact. The code is:
(defrule empresa_cae_mucho
(Empresa (nombre ?n)(var_anio ?anio)(var_sem ?sem)(var_tri ?tri))
=>
(or (or (test(> ?anio 30))(test (> ?sem 30))(test (> ?tri 30))))
(assert valor_infravalorado
(nombre ?n))
(assert (Explicacion
(nombre ?n)
(motivo "la empresa ha caido bastante aunque no en el ultimo mes
pero su PER es bajo")))
)
But it doesn't work and I can't find the right form of do this in internet. Any help?
CLIPS> (clear)
CLIPS>
(deftemplate Empresa
(slot nombre)
(slot var_anio)
(slot var_sem)
(slot var_tri))
CLIPS>
(deftemplate valor_infravalorado
(slot nombre))
CLIPS>
(deftemplate Explicacion
(slot nombre)
(slot motivo))
CLIPS>
(deffacts start
(Empresa (nombre 1) (var_anio 40) (var_sem 10) (var_tri 25))
(Empresa (nombre 2) (var_anio 0) (var_sem 35) (var_tri 10))
(Empresa (nombre 3) (var_anio 30) (var_sem 20) (var_tri 55))
(Empresa (nombre 4) (var_anio 30) (var_sem 30) (var_tri 30)))
CLIPS>
(defrule empresa_cae_mucho
(Empresa (nombre ?n)
(var_anio ?anio)
(var_sem ?sem)
(var_tri ?tri))
(test (or (> ?anio 30)
(> ?sem 30)
(> ?tri 30)))
=>
(assert (valor_infravalorado (nombre ?n)))
(assert (Explicacion
(nombre ?n)
(motivo "la empresa ..."))))
CLIPS> (reset)
CLIPS> (watch rules)
CLIPS> (watch facts)
CLIPS> (run)
FIRE 1 empresa_cae_mucho: f-3
==> f-5 (valor_infravalorado (nombre 3))
==> f-6 (Explicacion (nombre 3) (motivo "la empresa ..."))
FIRE 2 empresa_cae_mucho: f-2
==> f-7 (valor_infravalorado (nombre 2))
==> f-8 (Explicacion (nombre 2) (motivo "la empresa ..."))
FIRE 3 empresa_cae_mucho: f-1
==> f-9 (valor_infravalorado (nombre 1))
==> f-10 (Explicacion (nombre 1) (motivo "la empresa ..."))
CLIPS> (facts)
f-0 (initial-fact)
f-1 (Empresa (nombre 1) (var_anio 40) (var_sem 10) (var_tri 25))
f-2 (Empresa (nombre 2) (var_anio 0) (var_sem 35) (var_tri 10))
f-3 (Empresa (nombre 3) (var_anio 30) (var_sem 20) (var_tri 55))
f-4 (Empresa (nombre 4) (var_anio 30) (var_sem 30) (var_tri 30))
f-5 (valor_infravalorado (nombre 3))
f-6 (Explicacion (nombre 3) (motivo "la empresa ..."))
f-7 (valor_infravalorado (nombre 2))
f-8 (Explicacion (nombre 2) (motivo "la empresa ..."))
f-9 (valor_infravalorado (nombre 1))
f-10 (Explicacion (nombre 1) (motivo "la empresa ..."))
For a total of 11 facts.
CLIPS>

How do you remove one facts in CLIPS?

How do you remove one facts in CLIPS? The fact would be entered by a person and compared with the base was present, it deletes.
I tried so:
(defrule Deleting::ruleDeleteOneSynSoftgoal "This rule delete one synsoftagoal found in the basis of fact."
(declare (salience 42))
(printout t "Enter below the two softgoals field that want to be deleting:" crlf crlf
"the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line." crlf crlf)
(bind ?dsyntype (readline))
(bind ?dsyntopic (readline))
?fact3 <- (synSoftgoal
(ttId ?ttId3)
(syntopic ?syntopic3)
(syntype ?syntype3)
)
(test (and (eq ?dsyntopic ?syntopic3) (eq ?dsyntype ?syntype3)))
=>
(retract ?fact3
)
But, it is show this erro:
[PRNTUTIL2] Syntax Error: Check appropriate syntax for defrule.
ERROR:
(defrule Deleting::ruleDeleteOneSynSoftgoal "This rule delete one synsoftagoal found in the basis of fact."
(declare (salience 42))
(printout t "Enter below the two softgoals field that want to be deleting:" crlf crlf "the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line." crlf crlf)
(bind ?dsyntype (
Can you help me?
The condition of a rule should be used for matching facts/instances and the action portion of the rule is where you would perform actions such as printing output and receiving input.
You can either use a separate rule to query the user and another to delete the fact, or use the fact set query functions to search and delete the fact from the query rule.
CLIPS>
(deftemplate synSoftgoal
(slot ttId)
(slot syntopic)
(slot syntype))
CLIPS>
(deffacts initial
(synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
(synSoftgoal (ttId 2) (syntopic "A") (syntype "2"))
(synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
(synSoftgoal (ttId 4) (syntopic "B") (syntype "2")))
CLIPS>
(defrule QueryRule
=>
(printout t "Enter below the two softgoals field that want to be deleting:" crlf crlf
"the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line." crlf crlf)
(assert (dsyntype (readline)))
(assert (dsyntopic (readline))))
CLIPS>
(defrule DeleteRule
?fact1 <- (dsyntype ?dsyntype)
?fact2 <- (dsyntopic ?dsyntopic)
?fact3 <- (synSoftgoal
(ttId ?ttId3)
(syntopic ?dsyntopic)
(syntype ?dsyntype))
=>
(retract ?fact1 ?fact2 ?fact3))
CLIPS> (reset)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
f-2 (synSoftgoal (ttId 2) (syntopic "A") (syntype "2"))
f-3 (synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
f-4 (synSoftgoal (ttId 4) (syntopic "B") (syntype "2"))
For a total of 5 facts.
CLIPS> (run)
Enter below the two softgoals field that want to be deleting:
the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line.
2
A
CLIPS> (facts)
f-0 (initial-fact)
f-1 (synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
f-3 (synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
f-4 (synSoftgoal (ttId 4) (syntopic "B") (syntype "2"))
For a total of 4 facts.
CLIPS> (clear)
CLIPS>
(deftemplate synSoftgoal
(slot ttId)
(slot syntopic)
(slot syntype))
CLIPS>
(deffacts initial
(synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
(synSoftgoal (ttId 2) (syntopic "A") (syntype "2"))
(synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
(synSoftgoal (ttId 4) (syntopic "B") (syntype "2")))
CLIPS>
(defrule QueryAndDeleteRule
=>
(printout t "Enter below the two softgoals field that want to be deleting:" crlf crlf
"the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line." crlf crlf)
(bind ?dsyntype (readline))
(bind ?dsyntopic (readline))
(do-for-all-facts ((?f synSoftgoal)) (and (eq ?f:syntopic ?dsyntopic) (eq ?f:syntype ?dsyntype))
(retract ?f)))
CLIPS> (reset)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
f-2 (synSoftgoal (ttId 2) (syntopic "A") (syntype "2"))
f-3 (synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
f-4 (synSoftgoal (ttId 4) (syntopic "B") (syntype "2"))
For a total of 5 facts.
CLIPS> (run)
Enter below the two softgoals field that want to be deleting:
the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line.
2
A
CLIPS> (facts)
f-0 (initial-fact)
f-1 (synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
f-3 (synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
f-4 (synSoftgoal (ttId 4) (syntopic "B") (syntype "2"))
For a total of 4 facts.
CLIPS>

Resources