User:Swatyraul

FAMILIE PERSOANA :

(deftemplate Persoana (multislot Nume)(multislot Prenume)(multislot mama)(multislot tata) (multislot casatorit)) (deffacts Familie   ;bunicii, the OGs    (Persoana(Nume Popescu)(Prenume Ion)(mama nil)(tata nil)(casatorit Maria Popescu))    (Persoana(Nume Popescu)(Prenume Maria)(mama nil)(tata nil)(casatorit Ion Popescu))    ;copii bunicilor, the second generation    ;cristian o are pe elena       (Persoana(Nume Popescu)(Prenume Cristian)(mama Maria Popescu)(tata Ion Popescu)(casatorit Elena Popescu))       (Persoana(Nume Popescu)(Prenume Elena)(mama nil)(tata nil)(casatorit Cristian Popescu))     ; Adrian saracu nu are pe nimeni?       (Persoana(Nume Popescu)(Prenume Adrian)(mama Maria Popescu)(tata Ion Popescu)(casatorit Cristina Popescu))        (Persoana(Nume Popescu)(Prenume Cristina)(mama nil)(tata nil)(casatorit Adrian Popescu))       (Persoana(Nume Popescu)(Prenume Ciprian)(mama Maria Popescu)(tata Ion Popescu)(casatorit nil))    ;maria ii copil da are sot       (Persoana(Nume Andreescu)(Prenume Maria)(mama nil)(tata nil)(casatorit Petre Andreescu)) (Persoana(Nume Andreescu)(Prenume Petre)(mama nil)(tata nil)(casatorit Maria Anreescu)) ;un nepot pierdut in padure, nu se stie nimic de el          (Persoana(Nume Andreescu)(Prenume Ionel)(mama nil)(tata nil)(casatorit Maria Popescu)) ;o nepoata care nu s-o pierdut in padure (Persoana(Nume Avram)(Prenume Ionel)(mama Maria Andreescu)(tata nil)(casatorit Maria Popescu)) ; niste matusi nebune (Persoana(Nume Popescu)(Prenume Adriana)(mama nil)(tata nil)(casatorit nil)) ;ceva verisor de a lu Avram (Persoana(Nume Popescu)(Prenume Tudor)(mama Cristina Popescu)(tata Adrian Popescu)(casatorit nil)))  /*     (defrule grandp (Persoana (Nume $?x)(Mama $?y)(Tata $?z)) (Persoana (Nume $?y)(Mama $?y1)(Tata $?y2)) (Persoana (Nume $?z)(Mama $?z1)(Tata $?z2)) =>        (printout t "Nepot: " $?x "-> Bunici: " $?y1"," $?y2"," $?z1","$?z2 crlf) )(run)   */ DAMA: (deftemplate Dama (slot x) (slot y) (slot used(allowed-values TRUE FALSE))) (foreach ?x (create$ 1 2 3 4) (foreach ?y (create$ 1 2 3 4)       (assert (Dama (x ?x)(y ?y) (used FALSE))))) (defrule rezolvare ?id1<- (Dama (x ?x1)(y ?y1) (used FALSE)) ?id2<- (Dama (x ?x2 & ~?x1)(y ?y2 & ~?y1) (used FALSE)) ?id3<- (Dama (x ?x3 & ~?x1 & ~?x2)(y ?y3 & ~?y1 & ~?y2) (used FALSE)) ?id4<- (Dama (x ?x4 & ~?x1 & ~?x2 & ~?x3)(y ?y4 & ~?y1 & ~?y2 & ~?y3 ) (used FALSE)) (test (neq (abs (- ?x1 ?x2)) (abs (- ?y1 ?y2)))) (test (neq (abs (- ?x1 ?x3)) (abs (- ?y1 ?y3)))) (test (neq (abs (- ?x1 ?x4)) (abs (- ?y1 ?y4)))) (test (neq (abs (- ?x2 ?x3)) (abs (- ?y2 ?y3)))) (test (neq (abs (- ?x2 ?x4)) (abs (- ?y2 ?y4)))) (test (neq (abs (- ?x3 ?x4)) (abs (- ?y3 ?y4)))) =>   (printout t "Dama1: x1[" ?x1 "],y1[" ?y1"]" crlf) (printout t "Dama2: x2[" ?x2 "],y2[" ?y2"]" crlf) (printout t "Dama3: x3[" ?x3 "],y3[" ?y3"]" crlf) (printout t "Dama4: x4[" ?x4 "],y4[" ?y4"]" crlf) (modify ?id1 (used TRUE)) (modify ?id2 (used TRUE)) (modify ?id3 (used TRUE)) (modify ?id4 (used TRUE)) )(run)

ARCA NOE:

deftemplate arca (slot animal) (slot gen) (slot inaltime))

(foreach ?animal (create$ soarece veverita pisica caine vaca elefant)   (foreach ?gen (create$ masculin feminin) (printout t "Inaltimea animalului " ?animal " de genul " ?gen " este: ") (bind ?inaltime(read)) (assert (arca (animal ?animal)(gen ?gen)(inaltime ?inaltime))) ) ) (facts)

/*(deftemplate Arca (slot anim) (slot sex)(slot inaltime))

(assert (Arca (anim soarece)(sex M) (inaltime 0.1))) (assert (Arca (anim soarece)(sex F)(inaltime 0.1))) (assert (Arca (anim veverita)(sex M)(inaltime 0.3))) (assert (Arca (anim veverita)(sex F)(inaltime 0.3))) (assert (Arca (anim pisica)(sex M)(inaltime 0.8))) (assert (Arca (anim pisica)(sex F)(inaltime 0.8))) (assert (Arca (anim caine)(sex M)(inaltime 1.4))) (assert (Arca (anim caine)(sex F)(inaltime 1.4))) (assert (Arca (anim vaca)(sex M)(inaltime 2.4))) (assert (Arca (anim vaca)(sex F)(inaltime 2.4))) (assert (Arca (anim elefant)(sex M)(inaltime 5.9))) (assert (Arca (anim elefant)(sex F)(inaltime 5.9))) (facts)*/

SUMA ELEMENT :

(deftemplate element (slot valoare)(slot nume)) (assert (element(valoare 2)(nume A))) (assert (element(valoare 3)(nume B))) (assert(element(valoare 7)(nume C))) (assert(element(valoare 1001)(nume D)))

(deftemplate suma (slot valoare)) (assert (suma(valoare 0)))

(defrule CalulSuma	?idve <- (element (valoare ?ve))   ?idvs <- (suma(valoare ?vs))    =>    (modify ?idvs (valoare (+ ?ve ?vs)))    (retract ?idve)    )

/*modify modifica o anumita val retract extrage din baza de cunostinte

(defrule AfisareSuma   (suma(valoare ?vs))    =>    (printout t " Suma = "?vs crlf)	)

(run) (facts) PROGONOZA METEO: (deftemplate Conditii   (slot judet)    (slot culoareCer)    (slot ceata)    (slot vantDin)    (slot vantSpre)    (slot perioada)    (slot tipulNorilor) )
 * (watch all)

(deffacts PrognozaMeteo   (Conditii(judet AR)(culoareCer rosu)(perioada seara)(tipulNorilor senin))    (Conditii(judet TM)(ceata da)(perioada dimineata)(tipulNorilor senin))    (Conditii(judet B)(ceata da)(perioada seara)(tipulNorilor senin))    (Conditii(judet SB)(vantDin nordest)(vantSpre sud)(tipulNorilor cumulus))    (Conditii(judet BH)(vantDin nordest)(vantSpre vest)(tipulNorilor cumulus))    (Conditii(judet TG)(vantDin nordest)(vantSpre sud)(tipulNorilor nimbostratus))    (Conditii(judet SJ)(vantDin sudvest)(vantSpre nord)(tipulNorilor nimbostratus))    (Conditii(judet MM)(vantDin nordest)(vantSpre sud)(tipulNorilor cirrocumulus))    (Conditii(judet CJ)(vantDin nordest)(vantSpre sud)(tipulNorilor stratocumulus))    (Conditii(judet AB)(vantDin nordest)(vantSpre sud)(tipulNorilor cumulonimbus)(perioada seara))    (Conditii(judet CT)(vantDin nordest)(vantSpre sud)(tipulNorilor cirrostratus)) (Conditii(judet SM)(vantDin nordest)(vantSpre sud)(tipulNorilor altostratus)) (Conditii(judet IS)(vantDin nordest)(vantSpre sud)(tipulNorilor altocumulus)) ) (reset) (run)

(defrule PrognozaPloaie   ?var <- (Conditii(judet ?l)(vantDin ?vd &:(= ?vd nordest))(vantSpre ?vs &:(= ?vs sud)) (tipulNorilor ?t &:(or(= ?t nimbostratus )(= ?t cirrocumulus )(= ?t cirrostratus)(= ?t altocumulus))))   =>    (printout t "Se prognozeaza ploaie in: " ?l crlf) ) (run)

(defrule PrognozaGer   ?var <- (Conditii(judet ?l)(culoareCer ?cer & :(= ?cer rosu))(perioada ?p & :(= ?p seara)))    =>    (printout t "Se prognozeaza ger in: " ?l crlf) ) (run)

(defrule PrognozaCald   ?var <- (Conditii(judet ?l)(ceata ?c & :(= ?c da))(perioada ?p &:(or(= ?p dimineata)(= ?p seara))))    =>    (printout t "Se prognozeaza cald in: " ?l crlf) ) (run)

(defrule PrognozaNinsoare   ?var <- (Conditii(judet ?l)(perioada ?p &:(or(= ?p seara)(= ?p ziua)))(tipulNorilor ?t &:(= ?t cumulonimbus)))    =>    (printout t "Se prognozeaza ninsoare in: " ?l crlf) ) (run)

(defrule PrognozaPloaieScurta   ?var <- (Conditii(judet ?l)(vantDin ?vd &:(= ?vd sudvest))(vantSpre ?vs &:(= ?vs nord)) (tipulNorilor ?t &:(= ?t nimbostratus )))   =>    (printout t "Se prognozeaza o ploaie scurta in: " ?l crlf) ) (run)

Mediu :

(defglobal ?*TASK_PRIORITY_1* = 500) (defglobal ?*TASK_PRIORITY_2* = 200)

(deftemplate animal(slot denumire)	(slot mancare)	(slot modViata)	(slot mediuViata)	(slot modalitateReproducere)	(slot zona)	(slot medeViata)	(slot putere)) ;0 la ierbivore 1,2 la celelalte

(assert(animal(denumire leu)(mancare carnivor)(putere 2))) (assert(animal(denumire hiena)(mancare carnivor)(putere 1))) (assert(animal(denumire caprioara)(mancare vegetarian))) (assert(animal(denumire broasca)(mancare vegetarian)(modViata diurn)(mediuViata semiacvatic)(modalitateReproducere oua))) (assert(animal(denumire ariciMare)(mancare vegetarian)(modViata diurn)(mediuViata semiacvatici)(zona mediteraneana))) (assert(animal(denumire foca)(zona polara))) (assert(animal(denumire ursPolar)(zona polara)))

(defrule antipattern   ?p <- (animal {zona == polara} (denumire ?name))  =>    (printout t " ***Animale Polare " ?name crlf)    )

(defrule mediuSemiacvatic   (declare (salience ?*TASK_PRIORITY_1*))    ?p <- (animal {mediuViata == semiacvatic} (denumire ?name))    =>    (printout t " *** Animalul care are mediul de viata semiacvatic este: " ?name crlf))

(defrule VegetarianMediteranean   (declare (salience ?*TASK_PRIORITY_2*))    ?p <- (animal {mancare == vegetarian && zona == mediteraneana} (denumire ?name))    =>     (printout t " *** Animalul care e vegetarian si traieste in zona mediteraneana este " ?name crlf))

(defrule predator-prada   ?animal1 <- (animal)    ?animal2 <- (animal {mancare == animal1.mancare && mancare == carnivor && (putere < animal1.putere)})    ?animal3 <- (animal {mancare == carnivor})    ?animal4 <- (animal {mancare == vegetarian})    =>    (printout t " *** Praduitorul este " ?animal1.denumire " si prada este " ?animal2.denumire crlf)    (printout t "*** Pradatorul este " ?animal3.denumire " si prada este " ?animal4.denumire crlf)) (run)

(deftemplate persoana(multislot nume)(slot ochi)(slot par)(slot nationalitate))

(assert (persoana (nume Ion Ionescu)(ochi verzi)(par saten)(nationalitate franceza))) (assert (persoana (nume Bogdan Popescu)(ochi caprui)(par blond)(nationalitate germana))) (assert (persoana (nume Ionel Romanu)(ochi albastri)(par blond)(nationalitate germana))) (assert (persoana (nume Cristi Ardelean)(ochi verzi)(par blond)(nationalitate germana))) (assert (persoana (nume Mihai Popescu)(ochi purpurii)(par castaniu)(nationalitate romana)))

(defrule rule1    (persoana (nume $? ?prenume ?nume)(ochi verzi)(par saten)(nationalitate franceza))    =>    (printout t "Regula 1 -> Nume: " ?nume " ,Prenume: " ?prenume crlf)    ) (run) (defrule rule2   (persoana (nume $? ?pn ?n)(ochi ?o & ~albastri)(par ?p & ~negru)(nationalitate ?nt))    (not(persoana (nume ?pnn & ~?pn ?nn & ~?n)(ochi ?o)))	(not(persoana (nume ?pnnn & ~?pn ?nnn & ~?n)(par ?p)))    =>    (printout t "Regula 2 -> Nume: " ?pn " ,Prenume " ?n crlf)    ) (run) (defrule rule3    (persoana(nume $? ?prenume1 ?nume1)(ochi ?ochi1)(par ?par1 & ~blond)(nationalitate ?nt))    (test (or (eq ?par1 saten) (eq ?ochi1 albastri)))    (persoana (nume $? ?prenume2 ?nume2)(ochi ?ochi2 & verzi)(par ?par2 & ?par1 )(nationalitate ?nt1))    (test(or (eq ?par1 saten)(not (eq ?ochi2 caprui))))    =>    (printout t "Regula 3 ->" crlf)    (printout t "Persoana 1: " ?nume1 " " ?prenume1 " ochi " ?ochi1 " par " ?par1 " nationalitate " ?nt crlf)    (printout t "Persoana 2: " ?nume2 " prenume " ?prenume2 " ochi " ?ochi2 " par " ?par2 " nationalitate "?nt1 crlf)    )(run)

AKO IS-A

(defglobal ?*TASK_PRIORITY1* =2) (defglobal ?*TASK_PRIORITY2* =1)

(deftemplate relat(slot mode (type STRING)) (slot son (type STRING))(slot parent(type STRING)))

(assert(relat(mode "ako")     (son "ballon")        (parent "aircraft"))) (assert(relat(mode "ako")     (son "pr.driven")     (parent "aircraft"))) (assert(relat(mode "ako")     (son "jet")           (parent "aircraft"))) (assert(relat(mode "ako")     (son "blimp")         (parent "ballon"))) (assert(relat(mode "ako")     (son "blimp")         (parent "pr.driven"))) (assert(relat(mode "ako")     (son "special")       (parent "pr.driven"))) (assert(relat(mode "ako")     (son "dc-3")          (parent "pr.driven"))) (assert(relat(mode "ako")     (son "dc-9")          (parent "jet"))) (assert(relat(mode "ako")     (son "concorde")      (parent "jet"))) (assert(relat(mode "has-shape")(son "ballon")       (parent "round"))) (assert(relat(mode "has-shape")(son "blimp")        (parent "ellips"))) (assert(relat(mode "is-a")    (son "good year")     (parent "blimp"))) (assert(relat(mode "is-a")    (son "spirit of stl") (parent "special"))) (assert(relat(mode "is-a")    (son "airforce1")     (parent "dc-9")))

(defrule cauta-solutie-is-a   (declare (salience ?*TASK_PRIORITY1*))    (relat (son ?a))    ?m <- (relat (mode "is-a") (son ?a) (parent ?q))    ?n <- (relat (mode "ako") (son ?q) (parent ?p))    =>    (assert (relat(parent ?p)))    (retract ?m ?n)    )(run)

(defrule cauta-solutie   (declare(salience ?*TASK_PRIORITY2*))    (relat (son ?a))    ?v <- (relat (mode "ako") (son ?a) (parent ?q))    =>    (retract ?v)    (assert (relat (parent ?q)))    )(run)

(defrule parinte-de-parinte   (declare(salience ?*TASK_PRIORITY2*))    (relat(parent ?p))    ?v <- (relat(mode "ako") (son ?p)(parent ?q))    =>    (retract ?v)    (assert (relat(parent ?p)))    )(run)

(defrule printeaza-solutie   (declare(salience -1))    ?n <- (relat (parent ?p))    =>    (printout t ?p crlf)    (retract ?n)    )(run)

INTREBARI:

(deftemplate Intreb (slot intrebare)(slot raspuns))

(assert (Intreb (intrebare nil)(raspuns nil)))

(defrule r1   (Intreb (intrebare nil) (raspuns nil)) =>    (printout t "It is very big?" crlf)   (bind ?r (read))    (assert (Intreb(intrebare"It is very big?")(raspuns ?r)))    )(run)

(defrule r2.1   (Intreb(intrebare "It is very big? ")(raspuns yes))    =>    (printout t "Does it have a long neck?" crlf)   (bind ?r (read))    (assert (Intreb(intrebare"Does it have a long neck?")(raspuns ?r)))    )(run) (defrule r2.2   (Intreb(intrebare "It is very big?")(raspuns no))    =>    (printout t "Does it squeak?" crlf)   (bind ?r (read))    (assert (Intreb(intrebare"Does it squeak?")(raspuns ?r)))    )(run)

(defrule r3.1   (Intreb (intrebare "Does it have a long neck?")(raspuns yes))    =>    (printout t "ESTE O GIRAFA!" crlf)   )(run)

(defrule r4.1   (Intreb (intrebare"Does it have a long neck?")(raspuns no))    =>    (printout t "Does it have a tunk?" crlf)   (bind ?r (read))    (assert(Intreb(intrebare"Does it have a tunk?")(raspuns ?r)))    )(run)

(defrule r5.1   (Intreb (intrebare "Does it have a tunk?")(raspuns yes))    =>	(printout t "Este un elefant!" crlf)   )(run)

(defrule r5.2   (Intreb (intrebare "Does it have a tunk?")(raspuns no))    =>    (printout t "Does it like to be in water?" crlf)   (bind ?r (read))    (assert(Intreb(intrebare "Does it like to be in water?")(raspuns ?r)))    )(run)

(defrule r6.1   (Intreb(intrebare "Does it have a tunk?")(raspuns yes))    =>    (printout t "HIPPO!" crlf)   )(run) (defrule r6.2   (Intreb(intrebare "Does it have a tunk?")(raspuns no))    =>    (printout t "RHINNO!" crlf)   )(run)

(defrule r7.1	(Intreb(intrebare"Does it squeack?")(raspuns yes))   =>    (printout t "its a mouse!" crlf)   )(run)

(defrule r7.2   (Intreb(intrebare "Does it squeack?")(raspuns no))    =>    (printout t "squirrel" crlf)    )(run)

CALCUL MAXIM


 * Exercitiul 1

(printout t "Elementul maxim din sir este " (max 4 8 71 2 3 5)crlf)

(deffunction calculMaxim ($?lista)   (bind ?maxVal(nth$ 1 ?lista))    (foreach ?n ?lista if(> ?n ?maxVal) then (bind ?maxval ?n))    (return ?maxVal) )

COMBINATII

(assert(number 0)(number 1)(number 2)(number 3)(number 4)(number 5)(number 6)(number 7)(number 8)(number 9)(letter S)   (letter E)(letter N)(letter D)(letter M)(letter O)(letter R)(letter Y))

(deftemplate combination    (slot letter(type SYMBOL))    (slot number(type INTEGER)) )

(defrule generate-combinations   (number ?x)    (letter ?a)    =>    (assert(combination (letter ?a)(number ?x))))

(defrule find-solution   (combination(letter M)(number ?m&:(= ?m 1)))    (combination(letter O)(number ?o&~?m))    (combination(letter S)(number ?s&~?o&~?m))    (combination(letter D)(number ?d&~?s&~?o&~?m))    (combination(letter E)(number ?e&~?d&~?s&~?o&~?m))    (combination(letter Y)(number ?y&~?e&~?d&~?s&~?o&~?m))    (combination(letter R)(number ?r&~?y&~?e&~?d&~?s&~?o&~?m))    (combination(letter N)(number ?n&~?r&~?y&~?e&~?d&~?s&~?o&~?m))    (test(=(+(+(* 1000 ?s)(* 100 ?e)(* 10 ?n)?d) (+(* 1000 ?m)(* 100 ?o)(* 10 ?r)?e))   (+(* 10000 ?m)(* 1000 ?o)(* 100 ?n)(* 10 ?e)?y)))    =>    (printout t "o solutie : "crlf)    (printout t "S= " ?s)    (printout t "E= " ?e)    (printout t "N= " ?n)    (printout t "D= " ?d)    (printout t "M= " ?m)    (printout t "O= " ?o)    (printout t "R= " ?r)    (printout t "Y= " ?y)    (printout t crlf)    (printout t " " ?s ?e ?n ?d crlf)    (printout t "+" ?m ?o ?r ?e crlf)    (printout t "--"crlf)    (printout t ?m ?o ?n ?e ?y crlf) ) (run) (reset)

assert(number 0)(number 1)(number 2)(number 3)(number 4)(number 5)(number 6)(number 7)(number 8)(number 9)(letter S)   (letter E)(letter N)(letter D)(letter M)(letter O)(letter R)(letter Y))

(deftemplate combination    (slot letter(type SYMBOL))    (slot number(type INTEGER)) )

(defrule generate-combinations   (number ?x)    (letter ?a)    =>    (assert(combination (letter ?a)(number ?x))))

(defrule find-solution   (combination(letter M)(number ?m&:(= 1 ?m)))    (combination(letter O)(number ?o&:(= 0 ?o)))    (combination(letter S)(number ?s&:(= 9 ?s)))    (combination(letter D)(number ?d&~?s&~?o&~?m))    (combination(letter E)(number ?e&~?d&~?s&~?o&~?m))    (combination(letter Y)(number ?y&~?e&~?d&~?s&~?o&~?m&:(=(mod(+ ?d ?e)10)?y)))    (combination(letter N)(number ?n&~?y&~?e&~?d&~?s&~?o&~?m&:(=(+ ?e 1)?n)))    (combination(letter R)(number ?r&~?n&~?y&~?e&~?d&~?s&~?o&~?m&: (=(mod(+ ?n ?r(/ (+ ?d ?e)10))10)?e)))    =>    (printout t "o solutie : "crlf)    (printout t "S= " ?s)    (printout t "E= " ?e)    (printout t "N= " ?n)    (printout t "D= " ?d)    (printout t "M= " ?m)    (printout t "O= " ?o)    (printout t "R= " ?r)    (printout t "Y= " ?y)    (printout t crlf)    (printout t " " ?s ?e ?n ?d crlf)    (printout t "+" ?m ?o ?r ?e crlf)    (printout t "--"crlf)    (printout t ?m ?o ?n ?e ?y crlf)

) (run) (reset)

VIN: deftemplate Vin (multislot nume) (slot culoare) (slot tip) (slot gust) (slot tarie) )

(deffacts Vinuri   (Vin (nume Avincis Cuvee )(culoare alb)(tip linistit)(gust dulce)(tarie puternic))    (Vin (nume Corcovva Dessert)(culoare alb)(tip linistit)(gust sec)(tarie puternic))    (Vin (nume Puterea Inimii)(culoare rosu)(tip linistit)(gust demidulce)(tarie slab))    (Vin (nume Ducesa Ancestrala)(culoare rosu)(tip linistit)(gust sec)(tarie puternic))    (Vin (nume Avincis CuveeSpumant)(culoare alb)(tip spumant)(gust dulce)(tarie slab))    (Vin (nume Corcovva DessertSpumant)(culoare alb)(tip spumant)(gust sec)(tarie puternic))    (Vin (nume Puterea InimiiSpumant)(culoare rosu)(tip spumant)(gust dulce)(tarie puternic))    (Vin (nume Ducesa Ancestrala) (culoare rosu) (tip spumant) (gust sec)(tarie puternic))    )(reset) (run)

(defrule carnePasare    ?var <- (Vin (nume $?n)(culoare ?c & :(= ?c alb))(tip ?t &: (= ?t linistit)) (gust ?g & :(= ?g sec) ) )	=>  (printout t "Pentru carne de pasare se recomanda vinul " $?n " de culoare " ?c" cu gust " ?g crlf)       ) (run)

(defrule salata   ?var <- (Vin (nume $?n)(culoare ?c & :(= ?c rosu)) (gust ?g & :(= ?g demidulce))) =>    (printout t "Pentru salata se recomanda vinul " ?n " de culoare "?c" cu gust "?g crlf)    ) (run)

(defrule desertCiocolata   ?var <- (Vin (nume $?n)(tarie ?t & : (= ?t slab)) (gust ?g | :(= ?g dulce) | :(= ?g demidulce) ) ) =>	(printout t "Pentru deserturi cu ciocolata se recomanda vinul " ?n "cu gust "?g crlf) ) (run)

(defrule branzeturi    ?var <- (Vin (nume $?n) (gust ?g & :(= ?g sec)) (tip ?t & :(= ?t spumant)) )         =>     (printout t "Pentru branzeturi se recomanda " $?n " cu gust " ?g" si tipul " ?t crlf) ) (run)

CENTI IN CONT :

deffacts facts(stare (value 0)))

(defrule start    ?a <- (stare(value ?v&0)) => 	(printout t "Introduceti 5 sau 25" crlf) 	(bind ?x (read)) 	(if (eq ?x 5) then (printout t "Aveti " (+ ?v 5) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 5)))) else (printout t "Aveti " (+ ?v 25) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 25))))))

(defrule five 	?a <- (stare(value ?v&5)) => 	(printout t "Introduceti 5 sau 25" crlf) 	(bind ?x (read)) 	(if (eq ?x 5) then (printout t "Aveti " (+ ?v 5) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 5)))) else (printout t "Aveti " (+ ?v 25) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 25))))))

(defrule ten 	?a <- (stare(value ?v&10)) => 	(printout t "Introduceti 5 sau 25" crlf) 	(bind ?x (read)) 	(if (eq ?x 5) then (printout t "Aveti " (+ ?v 5) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 5)))) else (printout t "Aveti " (+ ?v 25) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 25))))))

(defrule fifteen   ?a <- (stare(value ?v&15)) => 	(printout t "Introduceti 5 sau 25" crlf) 	(bind ?x (read)) 	(if (eq ?x 5) then (printout t "Aveti " (+ ?v 5) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 5)))) else (printout t "Aveti " (+ ?v 25) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 25))))))

(defrule twenty 	?a <- (stare(value ?v&20)) => 	(printout t "Introduceti 5 sau 25" crlf) 	(bind ?x (read)) 	(if (eq ?x 5) then (printout t "Aveti " (+ ?v 5) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 5)))) else (printout t "Aveti " (+ ?v 25) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 25))))))

(defrule twentyfive ?a <- (stare(value ?v&25)) => 	(printout t "Introduceti 5 sau 25" crlf) 	(bind ?x (read)) 	(if (eq ?x 5) then (printout t "Aveti " (+ ?v 5) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 5)))) else (printout t "Aveti " (+ ?v 25) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 25))))))

(defrule thirty 	?a <- (stare(value ?v&30)) => 	(printout t "Introduceti 5 sau 25" crlf) 	(bind ?x (read)) 	(if (eq ?x 5) then (printout t "Aveti " (+ ?v 5) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 5)))) else (printout t "Aveti " (+ ?v 25) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 25))))))

(defrule thirtyfive 	?a <- (stare(value ?v&35)) => 	(printout t "Introduceti 5 sau 25" crlf) 	(bind ?x (read)) 	(if (eq ?x 5) then (printout t "Aveti " (+ ?v 5) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 5)))) else (printout t "Aveti " (+ ?v 25) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 25))))))

(defrule fourty 	?a <- (stare(value ?v&40)) => 	(printout t "Introduceti 5 sau 25" crlf) 	(bind ?x (read)) 	(if (eq ?x 5) then (printout t "Aveti " (+ ?v 5) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 5)))) else (printout t "Aveti " (+ ?v 25) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 25))))))

(defrule fourtyfive 	?a <- (stare(value ?v&45)) => 	(printout t "Introduceti 5 sau 25" crlf) 	(bind ?x (read)) 	(if (eq ?x 5) then (printout t "Aveti " (+ ?v 5) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 5)))) else (printout t "Aveti " (+ ?v 25) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 25))))))

(defrule fifty 	?a <- (stare(value ?v&50)) => 	(printout t "Introduceti 5 sau 25" crlf) 	(bind ?x (read)) 	(if (eq ?x 5) then (printout t "Aveti " (+ ?v 5) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 5)))) else (printout t "Aveti " (+ ?v 25) " centi in cont" crlf) (retract ?a) (assert (stare (value (+ ?v 25))))))

(defrule succes 	?a <- (stare(value ?v)) 	(test (>= ?v 55)) => 	(printout t "Drink your coffee and fill the pit of your soul, aici e restul: " (- ?v 55) crlf) ) (reset) (run)

STRAMOSI :

(deftemplate Persoana 	(slot nume)    (slot mama)    (slot tata)    (slot casatorit))

(deffacts arbore   (Persoana (nume Elena)(mama nil)(tata nil)(casatorit Mircea))    (Persoana (nume Mircea)(mama nil)(tata nil)(casatorit Elena))    (Persoana (nume Maria)(mama Elena)(tata Mircea)(casatorit Toma))    (Persoana (nume Ana)(mama nil)(tata nil) (casatorit Vasile))    (Persoana (nume Vasile)(mama nil)(tata nil)(casatorit Ana))    (Persoana (nume Toma)(mama Ana)(tata Vasile)(casatorit Maria))    (Persoana (nume Ion)(mama Maria)(tata Toma) (casatorit nil)))

(defrule stramosiiIon   ?var <- (FALSE) 	?ion <- (Persoana(nume ?n &:(= ?n Ion)))	?mama <- (Persoana(nume ?nm &:(= ?nm Maria)))    ?tata <- (Persoana(nume ?nt &:(= ?nt Toma)))    =>    (while(<> ?var TRUE) ?bunica<- (Persoana(nume ?nb &:(= ?nm.mama ?nb))) (printout t ?bunica.nume crlf) (printout t ?bunica.casatorit crlf ) ?bunicu <- (Persoana (nume ?nbunic &:(=?nt.nume ?nbunic))) (printout t ?bunicu.nume crlf) (printout t ?bunicu.casatorit crlf) ?mama <- ?bunica ?tata <- ?bunicu (if (= ?bunica.mama nil)           then             ?var <- (TRUE)) ) ) (run)

COLOR: (deftemplate country(slot name)(slot border)) (deftemplate culori(multislot nume)) (deftemplate solution(multislot perechi)) (deffacts facts (country (name Romania) (border Ungaria)) (country (name Romania) (border Ucraina)) (country (name Romania) (border Moldova)) (country (name Ungaria) (border Romania)) (country (name Ungaria) (border Ucraina)) (country (name Ungaria) (border Slovacia)) (country (name Ucraina) (border Ungaria)) (country (name Ucraina) (border Romania)) (country (name Ucraina) (border Slovacia)) (country (name Ucraina) (border Moldova)) (country (name Slovacia) (border Ucraina)) (country (name Slovacia) (border Ungaria)) (country (name Moldova) (border Romania)) (country (name Moldova) (border Ucraina)) (solution (perechi Romania white Ungaria white Ucraina white Slovacia white Moldova white)) (culori (nume albastru verde rosu galben)) ) (defrule solutii (solution (perechi $?b ?x white $?e)) (culori (nume $? ?c1 $?)) => (assert (solution (perechi $?b ?x ?c1 $?e)) ) ) (defrule curatenie (declare (salience -1)) ?s <- (solution (perechi $?b ?x white $?e)) => (retract ?s) ) (defrule curatenieSolutiiGresite (declare (salience -2)) (country (name ?x) (border ?y)) ?s <- (solution (perechi $?b ?x ?cx $?mijloc ?y ?cy $?e)) (test (eq ?cx ?cy)) => (retract ?s) ) (defrule afisare (declare (salience -3)) (solution (perechi $?b)) => (printout t $?b crlf) ) (reset) (run)

ULTIMU LAB ::

(deftemplate goal(slot move)(slot on-top-of)) (deffacts initial-data (queue A B C D) (queue E F G) (push-value S V T R )) (reset) (defrule push-value ?push-value <-(push-value ?value $?restul) ?queue <-(queue $?rest) => (retract ?queue) (retract ?push-value) (assert (queue ?rest ?value)) (assert (push-value ?restul)) (printout t "pushing value " ?value crlf) (facts) ) (run) (defrule pop-value-valid ?queue <-(queue ?value $?rest) =>(retract ?queue) (assert (queue ?rest)) (printout t "Popped value " ?value " " $?rest crlf) ) (run) (defrule pop-value-invalid ?pop-value <- (pop-value) (queue) => (retract ?pop-value) (printout t "popping from empty queue" crlf) ) (run) (facts)
 * (goal (move C)(on-top-of G ))
 * Operatii de baza
 * ?pop-value <-(pop-value)

(deffunction det-distance (?x1 ?x2 ?y1 ?y2 ) (bind ?d1(sqrt (+(**(- ?x2 ?x1)2) (** (- ?y2 ?y1) 2)))) (printout t "Distanata dintre 2 puncte este " ?d1 crlf ) (return ?d1) ) (deffunction det_paralel(?x1 ?x2 ?x3 ?x4 ?y1 ?y2 ?y3 ?y4) (printout t ?x2 "- " ?x1 crlf) (bind ?xd1(- ?x2 ?x1)) (printout t ?x3 "- " ?x4 crlf) (bind ?xd2(- ?x3 ?x4)) (printout t ?y2 "- " ?y1 crlf) (bind ?yd1(- ?y2 ?y1)) Problema 4

Problema 5

(printout t ?y3 "- " ?y4 crlf) (bind ?yd2(- ?y3 ?y4)) (printout t ?xd1 " " ?xd2 " " ?yd1 " " ?yd2 crlf) (if(= (/ ?yd1 ?xd1) (/ ?yd2 ?xd2))then (bind ?b paralele) (printout t paralele crlf) else (bind ?b neparalele) (printout t neparalele crlf) ) (return ?b) ) (deffunction det_perpendicular(?x1 ?x2 ?x3 ?x4 ?y1 ?y2 ?y3 ?y4) (bind ?xd1(- ?x2 ?x1)) (bind ?xd2(- ?x3 ?x2)) (bind ?yd1(- ?y2 ?y1)) (bind ?yd2(- ?y3 ?y2)) (if(=(* (/ ?yd1 ?xd1) (/ ?yd2 ?xd2)) -1)then (bind ?b perpendiculare ) (printout t perpendiculare crlf) else (bind ?b neperpendiculare ) (printout t neperpendiculare crlf) ) (return ?b) ) (deftemplate figuri(slot tip)(multislot coordonate)) (assert (figuri (tip patrulater)(coordonate 0 4 10 6 6 10 4 0))) ;dreptunghi (assert (figuri (tip patrulater)(coordonate 2 4 6 4 8 10 8 6)));patrat (assert (figuri (tip patrulater)(coordonate -1 0 1 0 5 7 5 3)));romb (assert (figuri (tip patrulater)(coordonate 0 0 3 3 5 3 5 0)));oarcare (assert (figuri (tip patrulater)(coordonate -3 -2 4 3 1 3 3 1)));paralelogram (deffunction determinare_tip_figura ($?lista) (bind ?x1(nth$ 1 ?lista)) (bind ?x2(nth$ 2 ?lista)) (bind ?x3(nth$ 3 ?lista)) (bind ?x4(nth$ 4 ?lista)) (bind ?y1(nth$ 5 ?lista)) (bind ?y2(nth$ 6 ?lista)) (bind ?y3(nth$ 7 ?lista)) (bind ?y4(nth$ 8 ?lista)) (if (and (= (det_paralel ?x1 ?x2 ?x3 ?x4 ?y1 ?y2 ?y3 ?y4) paralele) (= (det_paralel ?x2 ?x3 ?x4 ?x1 ?y2 ?y3 ?y4 ?y1) paralele)) then (if (and (= (det_perpendicular ?x1 ?x2 ?x3 ?x4 ?y1 ?y2 ?y3 ?y4 ) perpendiculare ) (= (det_perpendicular ?x2 ?x3 ?x4 ?x1 ?y2 ?y3 ?y4 ?y1) perpendiculare)) then ( if( and (= (det-distance ?x1 ?x2 ?y1 ?y2) (det-distance ?x2 ?x3 ?y2 ?y3) )(=(det-distance ?x3 ?x4 ?y3 ?y4) (det-distance ?x1 ?x4 ?y1 ?y4))

(=(det-distance ?x2 ?x3 ?y2 ?y3) (det-distance ?x3 ?x4 ?y3 ?y4))(=(det- distance ?x1 ?x2 ?y1 ?y2) (det-distance ?x1 ?x4 ?y1 ?y4)))then

(printout t "Figura este un " patrat crlf) (bind ?b patrat) else (printout t " Figura este un "dreptunghi crlf) (bind ?b dreptunghi)

) else (bind ?xmijloc( +(/ (- ?x3 ?x1) 2) ?x1) ) (bind ?ymijloc( +(/ (- ?y3 ?y1) 2) ?y1) ) (printout t "XMIJL: " ?xmijloc " YMIJL:" ?ymijloc crlf) (bind ?d1(det-distance ?x1 ?x2 ?y1 ?y2)) (bind ?d2(det-distance ?xmijloc ?x2 ?ymijloc ?y2)) (bind ?d3(det-distance ?x1 ?xmijloc ?y1 ?ymijloc)) (if (or (=( integer (* ?d1 ?d1) ) ( integer (+ (* ?d2 ?d2)(* ? d3 ?d3)))) (=(integer(* ?d2 ?d2))(integer (+ (* ?d1 ?d1)(* ?d3 ? d3)))) (=(integer (* ?d3 ?d3) ) (integer (+ (* ?d2 ?d2)(* ?d1 ? d1)))) )then (printout t "Figura este un "romb crlf) (bind ?b romb) else (printout t "Figura este un "paralelogram crlf) (bind ?b paralelogram) ) ) else (printout t "Figura este un " oarecare crlf) (bind ?b oarecare) ) (return ?b) ) (defrule determinare_tip_figura ?id <- (figuri (tip ?m)(coordonate $?lista)) => (printout t crlf) (bind ?t (determinare_tip_figura $?lista)) (modify ?id (tip ?t)) (retract ?id) ) (run) (facts)