Test Templates against each other in CLIPS RULES - clips

How do I test if all variables in template equal variables in different templates or if they don't. Need to have different outcomes from each output.
Attaching my code below:
``;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Sablony pre vstupy ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deftemplate mobily
(slot znacka (type SYMBOL))
(slot model (type STRING) )
(slot rozlisenie (type SYMBOL) (allowed-values HD FULL_HD Q_HD))
(slot SD_Karta (type SYMBOL) (allowed-values ano nie))
(slot interna_pamat (type INTEGER) (range 32 512))
(slot operacny_system (type SYMBOL) (allowed-values iOS android ine))
(slot uhlopriecka (type FLOAT) (range 4.0 7.0))
(slot rozlisenie_fotoaparatu (type INTEGER) (range 4 128))
(slot kapacita_baterie (type INTEGER) (range 5 25)))
(deftemplate zakaznik
(slot znacka (type SYMBOL))
(slot model (type STRING))
(slot rozlisenie (type SYMBOL) (allowed-values HD FULL_HD Q_HD))
(slot SD_Karta (type SYMBOL) (allowed-values ano nie))
(slot interna_pamat (type INTEGER) (range 32 512))
(slot operacny_system (type SYMBOL) (allowed-values iOS android ine))
(slot uhlopriecka (type FLOAT) (range 4.0 7.0))
(slot rozlisenie_fotoaparatu (type INTEGER) (range 4 128))
(slot kapacita_baterie (type INTEGER) (range 5 25)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Vstupy ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deffacts mobily_na_sklade
(mobily(znacka Motorola)(model "Defy")(rozlisenie HD)(SD_Karta ano)(interna_pamat 64)(operacny_system android)(uhlopriecka 6.5)(rozlisenie_fotoaparatu 60)(kapacita_baterie 20)) (mobily(znacka Samsung)(model "Galaxy")(rozlisenie FULL_HD)(SD_Karta ano)(interna_pamat 128)(operacny_system android)(uhlopriecka 6.5)(rozlisenie_fotoaparatu 32)(kapacita_baterie 25))
(mobily(znacka Iphone)(model "14")(rozlisenie FULL_HD)(SD_Karta nie)(interna_pamat 128)(operacny_system iOS)(uhlopriecka 6.1)(rozlisenie_fotoaparatu 12)(kapacita_baterie 25))
(mobily(znacka Iphone)(model "13")(rozlisenie Q_HD)(SD_Karta nie)(interna_pamat 128)(operacny_system iOS)(uhlopriecka 6.1)(rozlisenie_fotoaparatu 12)(kapacita_baterie 20))
(mobily(znacka Asus)(model "Zenfone")(rozlisenie HD)(SD_Karta ano)(interna_pamat 32)(operacny_system android)(uhlopriecka 5.9)(rozlisenie_fotoaparatu 64)(kapacita_baterie 14))
(mobily(znacka Samsung)(model "Galaxy")(rozlisenie Q_HD)(SD_Karta nie)(interna_pamat 128)(operacny_system android)(uhlopriecka 6.1)(rozlisenie_fotoaparatu 12)(kapacita_baterie 25))
(mobily(znacka Motorola)(model "Moto")(rozlisenie FULL_HD)(SD_Karta ano)(interna_pamat 32)(operacny_system android)(uhlopriecka 6.3)(rozlisenie_fotoaparatu 13)(kapacita_baterie 15))
(mobily(znacka Xiaomi)(model "Redmi")(rozlisenie HD)(SD_Karta ano)(interna_pamat 64)(operacny_system android)(uhlopriecka 6.5)(rozlisenie_fotoaparatu 108)(kapacita_baterie 16))
)
(defrule vypis_uvod
=>
(printout t "Vitajte v programe na vyber telefonu" crlf)
(printout t "Prosim vyberte vase specifikacie: " crlf))
(defrule osobne_hodnoty
=>
(printout t "Zadajte znacku" crlf)
(bind ?znacka(read))
(printout t "Zadajte model" crlf)
(bind ?model(read))
(printout t "Zadajte ci vas vybrany telefon podporuje SD Karty (ano/nie)" crlf)
(bind ?SD_Karta(read))
(printout t "Zadajte rozlisenie displeja (HD, FULL_HD, Q_HD)" crlf)
(bind ?rozlisenie(read))
(printout t "Zadajte velkost internej pamete (32 - 512)" crlf)
(bind ?interna_pamat(read))
(printout t "Zadajte uhlopriecku (4.0 - 7.0)" crlf)
(bind ?uhlopriecka(read))
(printout t "Zadajte operacny system(iOS, android, ine)" crlf)
(bind ?operacny_system(read))
(printout t "Zadajte rozlisenie_fotoaparatu (4 - 128)" crlf)
(bind ?rozlisenie_fotoaparatu(read))
(printout t "Zadajte kapacitu baterie v hodinach (5 - 25" crlf)
(bind ?kapacita_baterie(read))
(assert (zakaznik (znacka ?znacka)(model ?model)(rozlisenie ?rozlisenie)(SD_Karta ?SD_Karta)(interna_pamat ?interna_pamat)(operacny_system ?operacny_system) (uhlopriecka ?uhlopriecka) (rozlisenie_fotoaparatu ?rozlisenie_fotoaparatu)(kapacita_baterie ?kapacita_baterie))))
(defrule telefon_je_na_sklade
(and
(zakaznik (znacka ?znacka)(model ?model)(rozlisenie ?rozlisenie)(SD_Karta ?SD_Karta)(interna_pamat ?interna_pamat)(operacny_system ?operacny_system) (uhlopriecka ?uhlopriecka) (rozlisenie_fotoaparatu ?rozlisenie_fotoaparatu)(kapacita_baterie ?kapacita_baterie))
(mobily (znacka ?znacka)(model ?model)(rozlisenie ?rozlisenie)(SD_Karta ?SD_Karta)(interna_pamat ?interna_pamat)(operacny_system ?operacny_system) (uhlopriecka ?uhlopriecka) (rozlisenie_fotoaparatu ?rozlisenie_fotoaparatu)(kapacita_baterie ?kapacita_baterie)))
=>
(printout t "Telefon je na sklade:" ?znacka ?model crlf))
(defrule telefon_nie_je_na_sklade
(and
(zakaznik (znacka ?znacka)(model ?model)(rozlisenie ?rozlisenie)(SD_Karta ?SD_Karta)(interna_pamat ?interna_pamat)(operacny_system ?operacny_system) (uhlopriecka ?uhlopriecka) (rozlisenie_fotoaparatu ?rozlisenie_fotoaparatu)(kapacita_baterie ?kapacita_baterie))
(not(mobily (znacka ?znacka)(model ?model)(rozlisenie ?rozlisenie)(SD_Karta ?SD_Karta)(interna_pamat ?interna_pamat)(operacny_system ?operacny_system) (uhlopriecka ?uhlopriecka) (rozlisenie_fotoaparatu ?rozlisenie_fotoaparatu)(kapacita_baterie ?kapacita_baterie))))
=>
(printout t "Bohuzial zadany telefon nemame na sklade" crlf))
`
`
I tried using AND in the rules but when I did this only the last rule was implemented even if the variables were actually the same.

Both of your rules work. You just need to use the exact values specified in your mobily facts including the same case for symbols/strings and the enclosing quotation marks if you are matching a string.
CLIPS> (reset)
CLIPS> (run)
Vitajte v programe na vyber telefonu
Prosim vyberte vase specifikacie:
Zadajte znacku
Xiaomi
Zadajte model
"Redmi"
Zadajte ci vas vybrany telefon podporuje SD Karty (ano/nie)
ano
Zadajte rozlisenie displeja (HD, FULL_HD, Q_HD)
HD
Zadajte velkost internej pamete (32 - 512)
64
Zadajte uhlopriecku (4.0 - 7.0)
6.5
Zadajte operacny system(iOS, android, ine)
android
Zadajte rozlisenie_fotoaparatu (4 - 128)
108
Zadajte kapacitu baterie v hodinach (5 - 25
16
Telefon je na sklade:XiaomiRedmi
CLIPS> (reset)
CLIPS> (run)
Vitajte v programe na vyber telefonu
Prosim vyberte vase specifikacie:
Zadajte znacku
Xiaomi
Zadajte model
"Redmi"
Zadajte ci vas vybrany telefon podporuje SD Karty (ano/nie)
ano
Zadajte rozlisenie displeja (HD, FULL_HD, Q_HD)
HD
Zadajte velkost internej pamete (32 - 512)
64
Zadajte uhlopriecku (4.0 - 7.0)
6.5
Zadajte operacny system(iOS, android, ine)
android
Zadajte rozlisenie_fotoaparatu (4 - 128)
108
Zadajte kapacitu baterie v hodinach (5 - 25
20
Bohuzial zadany telefon nemame na sklade
CLIPS>

Related

Expected argument #1 to be of type integer or float on CLIPS code

I have a problem with my code that is to recommend a herbicide and an application rate for it as appropriate in a given field situation.
(deftemplate plant
(multislot weed)
(multislot crop))
(deftemplate Herbicide
(slot orgmatter)
(slot sencor)
(slot lasso)
(slot bicep))
(deffacts p
(plant (weed B) (crop C S))
(plant (weed B G) (crop C S))
(plant (weed B G) (crop C)))
(deffacts H
(Herbicide (orgmatter 1) (sencor 0.0) (lasso 2.0) (bicep 1.5))
(Herbicide (orgmatter 2) (sencor 0.75) (lasso 1.0) (bicep 2.5))
(Herbicide (orgmatter 3) (sencor 0.75) (lasso 0.5) (bicep 3.0)))
(defrule read-input
=>
(printout t "what is type of crop? (C:Corn , S:Soyabeans): ")
(assert (crop(read)))
(printout t "what is type of weed? (B:broadleaf , G:gress): ")
(assert (weed(read)))
(printout t "what is the organic matter? (1:<2% ,2: 2-4%, 3: >4%: ")
(assert (orgmatter(read))))
(defrule check-input
(crop ?crop)
(weed ?weed)
(orgmatter ? orgmatter)
(plant (weed $?weed1) (crop $?crop1))
(Herbicide (orgmatter ?orgmatter1) (sencor ?sencor1) (lasso ?lasso1)(bicep ?bicep1))
(test (member$ ?crop ?crop1))
(test (member$ ?weed ?weed1))
(test (= orgmatter ?orgmatter1))
=>
(printout t "you can use" ?sencor1 " pt/ac of sencor" crlf)
(printout t "you can use" ?lasso1 " pt/ac of lasso" crlf)
(printout t "you can use" ?bicep1 " pt/ac of bicep" crlf)))
The error is the following: Function = expected argument#1 to be of type integer or float
Your code has an extra ) at the end.
In defrule check-input, you have a test:
(test (= orgmatter ?orgmatter1))
Which is comparing a SYMBOL orgmatter with the variable ?orgmatter1. The = test works only with numerals. If you want to compare SYMBOLs or STRINGs, you need to use the eq function.
(test (eq orgmatter ?orgmatter1))
Nevertheless if you are not using ?orgmatter1 anywhere else, it is more effective to do a literal match rather than a test.
(Herbicide (orgmatter orgmatter)
(sencor ?sencor1)
(lasso ?lasso1)
(bicep ?bicep1))

CLIPS does not recognize deftemplate name

I am trying to retract a deftemplate fact but when I do this CLIPS keeps saying I have to first declare the deffunction yet it is the appropriate deftemplate.What seems to be the problem?
I have attached the related code:
I get this error:
[EXPRNPSR3] Missing function declaration for Agriculture.
What seems to be the problem?
(deftemplate Agriculture
(slot weed
(type SYMBOL)
(allowed-symbols B G))
(slot crop
(type SYMBOL)
(allowed-symbols C S))
(slot organic-matter
(type INTEGER)
(allowed-values 1 2 3)))
(defrule Sencor-1
(and (Agriculture(weed B))
(Agriculture(crop C|S))
(Agriculture(organic-matter 1)))
=>
(printout t "Do not use Sencor!!"crlf))
(defrule Sencor-2
(and (Agriculture(weed B))
(Agriculture(crop C|S))
(Agriculture(organic-matter 2|3)))
=>
(printout t " " crlf "Use 3/4 pt/ac of Sencor" crlf ))
(defrule Lasso-1
(and (Agriculture(weed B|G))
(Agriculture(crop C|S))
(Agriculture(organic-matter 1)))
=>
(printout t crlf"Use 2 pt/ac of Lasso" crlf))
(defrule Lasso-2
(and (Agriculture(weed B|G))
(Agriculture(crop C|S))
(Agriculture(organic-matter 2)))
=>
(printout t crlf "Use 1 pt/ac of Lasso" crlf))
(defrule Lasso-3
(and (Agriculture(weed B|G))
(Agriculture(crop C|S))
(Agriculture(organic-matter 3)))
=>
(printout t crlf "Use 0.5 pt/ac of Lasso" crlf))
(defrule Bicep-1
(and (Agriculture(weed B|G))
(Agriculture(crop C))
(Agriculture(organic-matter 1)))
=>
(printout t crlf "Use 1.5 pt/ac of Bicep" crlf))
(defrule Bicep-2
(and (Agriculture(weed B|G))
(Agriculture(crop C))
(Agriculture(organic-matter 2)))
=>
(printout t crlf"Use 2.5 pt/ac of Bicep" crlf))
(defrule Bicep-3
(and (Agriculture(weed B|G))
(Agriculture(crop C))
(Agriculture(organic-matter 3)))
=>
(printout t crlf "Use 3 pt/ac of Bicep" crlf))
(defrule input
(initial-fact)
=>
(printout t crlf "What is the crop? (C:corn,S:soybean)")
(bind ?a (read))
(assert(Agriculture(crop ?a))) ;gets input from user
(printout t crlf "What is the weed problem? (B:broadleaf, G:grass)")
(bind ?b (read))
(assert(Agriculture(weed ?b)))
(printout t crlf "What is the % of organic matter content? (1:<2%,2:2-4%,3:>4%)")
(bind ?c (read))
(assert(Agriculture(organic-matter ?c)))
?d <- (Agriculture(crop ?a) (weed ?b) (organic-matter ?c))
(printout t ""crlf crlf "RECOMMENDATIONS:"crlf)
(retract ?d))
In the RHS of the input rule you state:
?d <- (Agriculture(crop ?a) (weed ?b) (organic-matter ?c))
This is interpreted as "Run function Agriculture and bind its results into ?d".
What you probably are trying to do is:
(bind ?d (assert (Agriculture (crop ?a) (weed ?b) (organic-matter ?c))))

CLIPS Printout Number Of Times Rule Fired

I have the following CLIPS construct below and I'm interested in tallying up how many times a particular rule fired so the output will be a series of the rules fired followed by their count (see picture for example).
So it would be all those rules followed by:
temperature: (times fired) seconds
pressure: (times fired) seconds
(deftemplate oil-measure
(slot utc-time (type STRING))
(slot temperature (type INTEGER))
(slot pressure (type INTEGER)))
(defrule oil-is-hot
(oil-measure (temperature ?temp) (utc-time ?time))
(test (> ?temp 32))
=>
(printout t ?time tab "temperature:" tab ?temp crlf))
(defrule pressure-is-high
(oil-measure (pressure ?press&:(> ?press 0)) (utc-time ?time))
=>
(printout t ?time tab "pressure:" tab ?press crlf))
You can store counters in global variables and increase them accordingly.
(defglobal ?*hot-oil-times* = 0)
(defrule oil-is-hot
(oil-measure (temperature ?temp) (utc-time ?time))
(test (> ?temp 32))
=>
(bind ?*hot-oil-times* (+ ?*hot-oil-times* 1))
(printout t ?time tab "temperature:" tab ?*hot-oil-times* tab ?temp crlf))
Code incorporating noxdafox's solution:
(defglobal ?*hot-oil-times* = 0)
(defglobal ?*high-pressure-times* = 0)
(deftemplate oil-measure
(slot utc-time (type STRING))
(slot temperature (type INTEGER))
(slot pressure (type INTEGER)))
(defrule oil-is-hot
(oil-measure (temperature ?temp) (utc-time ?time))
(test (> ?temp 32))
=>
(bind ?*hot-oil-times* (+ ?*hot-oil-times* 1))
(printout t ?time tab "temperature:" tab ?*hot-oil-times* tab ?temp crlf))
(defrule pressure-is-high
(oil-measure (pressure ?press&:(> ?press 0)) (utc-time ?time))
=>
(bind ?*high-pressure-times* (+ ?*high-pressure-times* 1))
(printout t ?time tab "pressure:" tab ?*high-pressure-times* tab ?press crlf))

rule that does not run

excuse my English, I'm Spanish.
I have a problem with my expert system computer configurations made in CLIPS.
My rule "preciogamingoc" does not run, I tried a thousand ways but do not work.
I leave the code here.
I dont know how to put it better, sorry for that.
;;****TEMPLATES*****
(deftemplate pc
(slot tipo)
(slot oc)
(multislot procesador)
(multislot ram)
(multislot placabase)
(multislot discoduro)
(multislot grafica)
(multislot fuente)
(slot precio))
;;****FUNCTIONS*****
(deffunction pregunta (?pregunta $?respuestas-posibles)
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta)))
(while (not (member ?respuesta ?respuestas-posibles)) do
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta))))
?respuesta)
(deffunction sino (?pregunta)
(bind ?respuesta (pregunta ?pregunta si no s n))
(if (or (eq ?respuesta si) (eq ?respuesta s))
then TRUE
else FALSE))
(deffunction tipo (?pregunta)
(bind ?respuesta (pregunta ?pregunta g d o))
(switch ?respuesta
(case g then (return g))
(case d then (return d))
(case o then (return o))
))
;;******FACTS*******
(deffacts inicia
(pc))
;;******RULES*******
(defrule tipopc
?A <- (pc (procesador)(ram)(placabase)(discoduro)(grafica)(fuente))
=>
(switch (tipo "Tipo (juegos:g/diseño:d/otros:o)? ")
(case g then ( modify ?A (tipo g)(oc nose)(procesador "i5 4690_Intel")
(ram "2x4GB 2133 cl9")
(placabase "Gigabyte_H97M-HD3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 960")
(fuente "700W+ silver")
(precio 550)))
(case d then ( modify ?A (tipo d)(oc nose)(procesador "i7 4790_Intel")
(ram "2x8GB 2133 cl9")
(placabase "Gigabyte_H97M-HD3 ejemplo")
(discoduro "1Tb HDD + 120gb SSD")
(grafica "kuadro k620")
(fuente "700W+ silver")
(precio 700)))
(case o then ( modify ?A (tipo o)(oc nose)(procesador "i3 6100_Intel")
(ram "2x4GB 2400 cl12 DDR4")
(placabase "Msi H110M PRO-VH")
(discoduro "1Tb")
(grafica "no incluida")
(fuente "450W+ bronze")
(precio 300)))))
(defrule oc
?A <- (pc(tipo g)(oc nose))
=>
(if (sino "Hará overclock al procesador (si/no)?: ")
then ( modify ?A (oc si)))
)
(defrule preciogamingoc
?A <- (pc (tipo g) (oc si) (procesador) (ram) (placabase) (discoduro) (grafica) (fuente))
=>
(printout t "Veo que su sistema será destinado a juegos y que además se le podrá hacer overclock." crlf
"Ahora debe introducir el presupuesto medio para que podamos darle una configuración mas concreta." crlf
"Introduzca su presupuesto de referencia(>= 750): ")
(bind ?precio (read))
(if (and(>= ?precio 750)(< ?precio 850))
;;then ((printout t "bien"crlf))
then (modify ?A (procesador "i5 4690K_Intel")
(ram "2x8GB 2133 cl9")
(placabase "Gigabyte_Z97X gaming 3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 970")
(fuente "700W+ silver")
(precio 750)))
;;(else (if (>= ?precio 850)
;;then (modify ?C (procesador "i7 4790K_Intel")
;;(ram "2x8GB 2133 cl9")
;;(placabase "Gigabyte_Z97X gaming 5 ejemplo")
;;(discoduro "1Tb HDD")
;;(grafica "GTX 970")
;;(fuente "700W+ silver")
;(precio 850))))
)
;;*******MAIN*******
(defrule inicio
(declare (salience 1))
=>
(printout t crlf)
(printout t "Sistema Experto de Configuracion de Ordenadores")
(printout t crlf crlf))
(defrule presupuesto
(declare (salience -1))
(pc (procesador ?A)(ram ?F)(placabase ?B)(discoduro ?M)(grafica ?G)(fuente ?P)(precio ?O))
=>
(printout t crlf crlf "-.PRESUPUESTO.-" crlf crlf)
(printout t "PROCESADOR: " ?A crlf)
(printout t "RAM: " ?F crlf)
(printout t "PLACA BASE: " ?B crlf)
(printout t "DISCO DURO:" ?M crlf)
(printout t "GRAFICA: " ?G crlf)
(printout t "FUENTE: " ?P crlf crlf)
(printout t "PRECIO: " ?O crlf)
)
The preciogamingoc rule matches a pc fact, so you can run the program, check to see which rules execute, and examine the pc fact to see which values have been assigned to it:
CLIPS> (reset)
CLIPS> (watch rules)
CLIPS> (run)
FIRE 1 inicio: *
Sistema Experto de Configuracion de Ordenadores
FIRE 2 tipopc: f-1
Tipo (juegos:g/diseño:d/otros:o)? g
FIRE 3 oc: f-2
Hará overclock al procesador (si/no)?: si
FIRE 4 presupuesto: f-3
-.PRESUPUESTO.-
PROCESADOR: i5 4690_Intel
RAM: 2x4GB 2133 cl9
PLACA BASE: Gigabyte_H97M-HD3 ejemplo
DISCO DURO:1Tb
GRAFICA: GTX 960
FUENTE: 700W+ silver
PRECIO: 550
CLIPS> (facts)
f-0 (initial-fact)
f-3 (pc (tipo g) (oc si) (procesador "i5 4690_Intel") (ram "2x4GB 2133 cl9") (placabase "Gigabyte_H97M-HD3 ejemplo") (discoduro "1Tb") (grafica "GTX 960") (fuente "700W+ silver") (precio 550))
For a total of 2 facts.
CLIPS> (ppfact 3)
(pc
(tipo g)
(oc si)
(procesador "i5 4690_Intel")
(ram "2x4GB 2133 cl9")
(placabase "Gigabyte_H97M-HD3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 960")
(fuente "700W+ silver")
(precio 550))
CLIPS>
The pattern from the preciogamingoc rule is:
?A <- (pc (tipo g)
(oc si)
(procesador)
(ram)
(placabase)
(discoduro)
(grafica)
(fuente))
The fact values for the tipo and oc slots match the pattern, but the values for the processor, ram, placabase, discoduro, grafica, and fuente slots do not. These slots can have zero or more values. The fact has a single value for each, but your pattern specifies that each of these slots be empty so the pattern is not matched.
You need to either remove these slots from the pattern or check for specific values. You also need to prevent the rule from executing again once the pc fact has been modified in the actions of the rule (perhaps by changing the value of oc to si-actualizado to indicate the rule has been applied):
(defrule preciogamingoc
?A <- (pc (tipo g) (oc si))
=>
(printout t "Veo que su sistema será destinado a juegos y que además se le podrá hacer overclock." crlf
"Ahora debe introducir el presupuesto medio para que podamos darle una configuración mas concreta." crlf
"Introduzca su presupuesto de referencia(>= 750): ")
(bind ?precio (read))
(if (and (>= ?precio 750)(< ?precio 850))
then
(modify ?A (oc si-actualizado)
(procesador "i5 4690K_Intel")
(ram "2x8GB 2133 cl9")
(placabase "Gigabyte_Z97X gaming 3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 970")
(fuente "700W+ silver")
(precio 750))))

CLIPS counting facts or template instances that match the pattern

First I declare:
(deftemplate worker
(slot id
(type STRING)
(default ?DERIVE))
(slot salary
(type FLOAT)
(default ?DERIVE)))
then I add:
(assert(worker(id "a")(salary 30.0)))
(assert(worker(id "b")(salary 40.0)))
(assert(worker(id "c")(salary 60.0)))
(assert(worker(id "d")(salary 70.0)))
(assert(worker(id "e")(salary 10.0)))
How can I count how many 'workers' I have?
How can I count for example how many workers have salary over 30?
Use the fact-set query functions:
CLIPS>
(deftemplate worker
(slot id (type STRING) (default ?DERIVE))
(slot salary (type FLOAT) (default ?DERIVE)))
CLIPS> (assert (worker (id "a") (salary 30.0)))
<Fact-1>
CLIPS> (assert (worker (id "b") (salary 40.0)))
<Fact-2>
CLIPS> (assert (worker (id "c") (salary 60.0)))
<Fact-3>
CLIPS> (assert (worker (id "d") (salary 70.0)))
<Fact-4>
CLIPS> (assert (worker (id "e") (salary 10.0)))
<Fact-5>
CLIPS> (find-all-facts ((?f worker)) (> ?f:salary 30.0))
(<Fact-2> <Fact-3> <Fact-4>)
CLIPS> (length$ (find-all-facts ((?f worker)) (> ?f:salary 30.0)))
3
CLIPS> (do-for-all-facts ((?f worker)) (> ?f:salary 30.0) (printout t ?f:id crlf))
b
c
d
CLIPS>

Resources