;Versione aggiornata del supermercato con brevi commenti. Mapelli I. , Cornaz M. (defmodule MAIN (export ?ALL)) ;azione da eseguire, con i valori permessi (deftemplate exec (slot action (allowed-values go turnright turnleft read inspectlegenda grasp)) (slot time)) ;é qui definito lo stato globale (?) (deftemplate status (slot time) (slot result)) ;la percezione, il tempo, la posizione, la direzione e le caselle individuate dalla percezione ;possono essere liberi, muro, il colore dello stand (se c'é lo stand), la legenda, ;sconosciuto (se abbiamo davanti un muro), l'ingresso e l'uscita (deftemplate percepts (slot time) (slot pos-r) (slot pos-c) (slot direction) (slot perc1 (allowed-values empty wall bump standred standgreen standyellow standblue standeorange standwhite standpink legenda unknown entry exit)) (slot perc2 (allowed-values empty wall bump standred standgreen standyellow standblue standeorange standwhite standpink legenda unknown entry exit)) (slot perc3 (allowed-values empty wall bump standred standgreen standyellow standblue standeorange standwhite standpink legenda unknown entry exit))) ;la percezione in fase di lettura di uno slot, abbiamo il tempo, la posizione, la direzione, ;il prodotto, la marca, il costo e la quantitá (deftemplate perc-read (slot time) (slot pos-r) (slot pos-c) (slot direction) (slot item) (slot brand) (slot cost) (slot quantity)) ;la perceziopne legata al prelievo di un prodotto, abbiamo il tempo, la posizione dello stand, ;la direzione, il prodotto, la marca, il costo, la quantitá (deftemplate perc-grasp (slot time) (slot pos-r) (slot pos-c) (slot direction) (slot item) (slot brand) (slot cost) (slot quantity)) ;la percezione legata alla legenda, abbiamo il tempo (?), la posizione della legenda, la direzione ;il colore dello stand con l'associazione al prodotto (deftemplate perc-legenda (slot time) (slot pos-r) (slot pos-c) (slot direction) (slot standcolor) (slot product)) ;fatto iniziale che ci permette di creare il supermercato (deffacts init (create)) ;regola per andare a creare il mondo (supermercato) (defrule createworld (create) => (focus ENV)) ;regola che dá il controllo all'agente (defrule go-on-agent (declare (salience 20)) ?f1 <- (status (time ?i) (result ?x&~exit)) => (focus AGENT)) ;regola che dá il controllo alla percezione (defrule go-on-env (declare (salience 20)) ?f1 <- (status (time ?i)) (exec (time ?i)) => (focus ENV)) ;regola che determina il successo dell'agente e la conseguente terminazione (defrule success (declare (salience 100)) ?f1 <- (status (result exit)) => (halt)) (defmodule ENV (import MAIN ?ALL)(export ?ALL)) ;la definizione della cella, posizione (riga,colonna) e contenuto, i valori permessi sono libero ;stand con il suo colore, muro, legenda, ingresso, uscita (deftemplate cell (slot pos-r) (slot pos-c) (slot contains (allowed-values nothing standred standgreen standyellow standblue standeorange standwhite standpink wall entry legenda exit))) ;si definisce l'accesso dello stand indicando la posizione del robot (deftemplate access (slot standpos-r) (slot standpos-c) (slot agentpos-r) (slot agentpos-c)) ;la posizione dell'agente con la direzione e il tempo (deftemplate agentpos (slot pos-r) (slot pos-c) (slot direction) (slot time)) ;definizione dello stand con i campi relativi alla posizione (riga,colonna), numero di confezioni ;prodotto, marca, costo e quantitá della confezione (deftemplate standcontent (slot pos-r) (slot pos-c) (slot amount) (slot item) (slot brand) (slot cost) (slot quantity)) ;definisce il contenuto della legenda, la posizione della legenda, ;l'associazione colore stand con prodotto (deftemplate legendacontent (slot pos-r) (slot pos-c) (slot standcolor) (slot product)) ; regola per creare il supermercato (defrule creation ?f1 <- (create) => (assert (cell (pos-r 1) (pos-c 1) (contains wall)) (cell (pos-r 1) (pos-c 2) (contains entry)) (cell (pos-r 1) (pos-c 3) (contains wall)) (cell (pos-r 1) (pos-c 4) (contains exit)) (cell (pos-r 1) (pos-c 5) (contains wall)) (cell (pos-r 1) (pos-c 6) (contains wall)) (cell (pos-r 2) (pos-c 1) (contains legenda)) (cell (pos-r 2) (pos-c 2) (contains nothing)) (cell (pos-r 2) (pos-c 3) (contains nothing)) (cell (pos-r 2) (pos-c 4) (contains nothing)) (cell (pos-r 2) (pos-c 5) (contains nothing)) (cell (pos-r 2) (pos-c 6) (contains wall)) (cell (pos-r 3) (pos-c 1) (contains wall)) (cell (pos-r 3) (pos-c 2) (contains nothing)) (cell (pos-r 3) (pos-c 3) (contains standred)) (cell (pos-r 3) (pos-c 4) (contains standyellow)) (cell (pos-r 3) (pos-c 5) (contains nothing)) (cell (pos-r 3) (pos-c 6) (contains wall)) (cell (pos-r 4) (pos-c 1) (contains wall)) (cell (pos-r 4) (pos-c 2) (contains nothing)) (cell (pos-r 4) (pos-c 3) (contains nothing)) (cell (pos-r 4) (pos-c 4) (contains nothing)) (cell (pos-r 4) (pos-c 5) (contains nothing)) (cell (pos-r 4) (pos-c 6) (contains wall)) (cell (pos-r 5) (pos-c 1) (contains wall)) (cell (pos-r 5) (pos-c 2) (contains wall)) (cell (pos-r 5) (pos-c 3) (contains wall)) (cell (pos-r 5) (pos-c 4) (contains wall)) (cell (pos-r 5) (pos-c 5) (contains wall)) (cell (pos-r 5) (pos-c 6) (contains wall))) ;in queste asserzioni si definiscono gli accessi agli stand (assert (access (standpos-r 3) (standpos-c 3) (agentpos-r 4) (agentpos-c 3)) (access (standpos-r 3) (standpos-c 4) (agentpos-r 2) (agentpos-c 4)) (access (standpos-r 2) (standpos-c 1) (agentpos-r 2) (agentpos-c 2))) ;asseriamo qui il contenuto degli stand (assert (standcontent (pos-r 3) (pos-c 3) (amount 100) (item robiola) (brand osella) (cost 100) (quantity 250)) (standcontent (pos-r 3) (pos-c 4) (amount 10) (item barolo) (brand gaja) (cost 9000) (quantity 750))) ;asseriamo il contenuto della legenda (assert (legendacontent (pos-r 2) (pos-c 1) (standcolor standred) (product food)) (legendacontent (pos-r 2) (pos-c 1) (standcolor standyellow) (product wine))) (retract ?f1) ;inizializziamo lo stato e la posizione dell'agente (assert (status (time 0) (result no)) (agentpos (pos-r 1) (pos-c 2) (time 0)(direction up))) (focus MAIN)) ;corretto movimento all'in su (defrule move-up-ok (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c)(direction up)) (cell (pos-r ?r1) (pos-c ?c) (contains nothing)) (test (= ?r1 (+ ?r 1))) => (modify ?f1 (pos-r (+ ?r 1)) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;siamo andati a sbattere contro un muro andando in su (defrule move-up-bump (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c)(direction up)) (cell (pos-r ?r1) (pos-c ?c) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink|wall|entry|legenda)) (test (= ?r1 (+ ?r 1))) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (assert (percepts (time (+ ?i 1))(pos-r ?r) (pos-c ?c) (direction up) (perc1 bump) (perc2 unknown) (perc3 unknown))) (focus MAIN)) ;siamo andati a sbattere contro un muro andando in giú (defrule move-down-bump (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c)(direction down)) (cell (pos-r ?r1) (pos-c ?c) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink|wall|entry|legenda)) (test (= ?r1 (- ?r 1))) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (assert (percepts (time (+ ?i 1))(pos-r ?r) (pos-c ?c) (direction down) (perc1 bump) (perc2 unknown) (perc3 unknown))) (focus MAIN)) ;corretto movimento all'in giú (defrule move-down-ok (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c)(direction down)) (cell (pos-r ?r1) (pos-c ?c) (contains nothing)) (test (= ?r1 (- ?r 1))) => (modify ?f1 (pos-r (- ?r 1)) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;abbiamo raggiunto l'uscita con un movimento in giú (defrule move-down-exit (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c)(direction down)) (cell (pos-r ?r1) (pos-c ?c) (contains exit)) (test (= ?r1 (- ?r 1))) => (modify ?f1 (pos-r (- ?r 1)) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)) (result exit)) (focus MAIN)) ;abbiamo raggiunto l'uscita con un movimento a sinistra (defrule move-left-ok (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c) (direction left)) (cell (pos-r ?r) (pos-c ?c1) (contains nothing)) (test (= ?c1 (- ?c 1))) => (modify ?f1 (pos-c (- ?c 1)) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;siamo andati a sbattere contro un muro andando a sinistra (defrule move-left-bump (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c) (direction left)) (cell (pos-r ?r) (pos-c ?c1) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink|wall|entry|legenda)) (test (= ?c1 (- ?c 1))) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (assert (percepts (time (+ ?i 1))(pos-r ?r) (pos-c ?c) (direction left) (perc1 bump) (perc2 unknown) (perc3 unknown))) (focus MAIN)) ;corretto movimento a destra (defrule move-right-ok (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c) (direction right)) (cell (pos-r ?r) (pos-c ?c1) (contains nothing)) (test (= ?c1 (+ ?c 1))) => (modify ?f1 (pos-c (+ ?c 1))(time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;siamo andati a sbattere contro un muro andando a destra (defrule move-right-bump (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c) (direction right)) (cell (pos-r ?r) (pos-c ?c1) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink|wall|entry|legenda)) (test (= ?c1 (+ ?c 1))) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (assert (percepts (time (+ ?i 1))(pos-r ?r) (pos-c ?c) (direction right) (perc1 bump) (perc2 unknown) (perc3 unknown))) (focus MAIN)) ;girati a sinistra (defrule turnleft1 (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action turnleft)) ?f1<- (agentpos (direction left)) => (modify ?f1 (direction down) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;girati a sinistra (defrule turnleft2 (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action turnleft)) ?f1<- (agentpos (direction down )) => (modify ?f1 (direction right) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;girati a sinistra (defrule turnleft3 (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action turnleft)) ?f1<- (agentpos (direction right)) => (modify ?f1 (direction up) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;girati a sinistra (defrule turnleft4 (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action turnleft)) ?f1<- (agentpos (direction up)) => (modify ?f1 (direction left) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;girati a destra (defrule turnright1 (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action turnright)) ?f1<- (agentpos (direction left)) => (modify ?f1 (direction up) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;girati a destra (defrule turnright2 (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action turnright)) ?f1<- (agentpos (direction up)) => (modify ?f1 (direction right) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;girati a destra (defrule turnright3 (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action turnright)) ?f1<- (agentpos (direction right)) => (modify ?f1 (direction down) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;girati a destra (defrule turnright4 (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action turnright)) ?f1<- (agentpos (direction down)) => (modify ?f1 (direction left) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) ;lettura in sú andata a buon fine (defrule read-up-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action read)) (agentpos (pos-r ?r) (pos-c ?c) (direction up)) (cell (pos-r ?r1) (pos-c ?c) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink)) (test (= ?r1 (+ ?r 1))) (access (standpos-r ?r1) (standpos-c ?c) (agentpos-r ?r) (agentpos-c ?c)) (standcontent (pos-r ?r1) (pos-c ?c) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q)) => (assert (perc-read (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction up) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q))) (assert (OK read ?i))) ;lettura in giú andata a buon fine (defrule read-down-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action read)) (agentpos (pos-r ?r) (pos-c ?c) (direction down)) (cell (pos-r ?r1) (pos-c ?c) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink)) (test (= ?r1 (- ?r 1))) (access (standpos-r ?r1) (standpos-c ?c) (agentpos-r ?r) (agentpos-c ?c)) (standcontent (pos-r ?r1) (pos-c ?c) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q)) => (assert (perc-read (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction down) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q))) (assert (OK read ?i))) ;raccolta del prodotto in sú andata a buon fine (defrule grasp-up-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action grasp)) (not (OK grasp ?i)) (agentpos (pos-r ?r) (pos-c ?c) (direction up)) (cell (pos-r ?r1) (pos-c ?c) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink)) (test (= ?r1 (+ ?r 1))) (access (standpos-r ?r1) (standpos-c ?c) (agentpos-r ?r) (agentpos-c ?c)) ?f3<- (standcontent (pos-r ?r1) (pos-c ?c) (amount ?a) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q)) (test (> ?a 0)) => (assert (perc-grasp (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction up) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q))) (modify ?f3 (amount (- ?a 1))) (assert (OK grasp ?i))) ;raccolta del prodotto in giú andata a buon fine (defrule grasp-down-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action grasp)) (not (OK grasp ?i)) (agentpos (pos-r ?r) (pos-c ?c) (direction down)) (cell (pos-r ?r1) (pos-c ?c) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink)) (test (= ?r1 (- ?r 1))) (access (standpos-r ?r1) (standpos-c ?c) (agentpos-r ?r) (agentpos-c ?c)) ?f3<- (standcontent (pos-r ?r1) (pos-c ?c) (amount ?a) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q)) (test (> ?a 0)) => (assert (perc-grasp (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction down) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q))) (modify ?f3 (amount (- ?a 1))) (assert (OK grasp ?i))) ;lettura legenda all'in sú andata a buon fine (defrule ilegenda-up-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action inspectlegenda)) (agentpos (pos-r ?r) (pos-c ?c) (direction up)) (cell (pos-r ?r1) (pos-c ?c) (contains legenda)) (test (= ?r1 (+ ?r 1))) (access (standpos-r ?r1) (standpos-c ?c) (agentpos-r ?r) (agentpos-c ?c)) (legendacontent (pos-r ?r1) (pos-c ?c) (standcolor ?col) (product ?prod)) => (assert (perc-legenda (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction up) (standcolor ?col) (product ?prod))) (assert (OK inspectlegenda ?i))) ;lettura legenda all'in giú andata a buon fine (defrule ilegenda-down-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action inspectlegenda)) (agentpos (pos-r ?r) (pos-c ?c) (direction down)) (cell (pos-r ?r1) (pos-c ?c) (contains legenda)) (test (= ?r1 (- ?r 1))) (access (standpos-r ?r1) (standpos-c ?c) (agentpos-r ?r) (agentpos-c ?c)) (legendacontent (pos-r ?r1) (pos-c ?c) (standcolor ?col) (product ?prod)) => (assert (perc-legenda (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction down) (standcolor ?col) (product ?prod))) (assert (OK inspectlegenda ?i))) ;lettura a destra andata a buon fine (defrule read-right-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action read)) (agentpos (pos-r ?r) (pos-c ?c) (direction right)) (cell (pos-c ?c1) (pos-r ?r) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink)) (test (= ?c1 (+ ?c 1))) (access (standpos-c ?c1) (standpos-r ?r) (agentpos-r ?r) (agentpos-c ?c)) (standcontent (pos-c ?c1) (pos-r ?r) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q)) => (assert (perc-read (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction right) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q))) (assert (OK read ?i))) ;lettura a sinistra andata a buon fine (defrule read-left-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action read)) (agentpos (pos-r ?r) (pos-c ?c) (direction left)) (cell (pos-c ?c1) (pos-r ?r) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink)) (test (= ?c1 (- ?c 1))) (access (standpos-c ?c1) (standpos-r ?r) (agentpos-r ?r) (agentpos-c ?c)) (standcontent (pos-c ?c1) (pos-r ?r) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q)) => (assert (perc-read (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction left) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q))) (assert (OK read ?i))) ;raccolta del prodotto da destra andata a buon fine (defrule grasp-right-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action grasp)) (not (OK grasp ?i)) (agentpos (pos-r ?r) (pos-c ?c) (direction right)) (cell (pos-c ?c1) (pos-r ?r) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink)) (test (= ?c1 (+ ?c 1))) (access (standpos-c ?c1) (standpos-r ?r) (agentpos-r ?r) (agentpos-c ?c)) ?f3<- (standcontent (pos-c ?c1) (pos-r ?r) (amount ?a) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q)) (test (> ?a 0)) => (assert (perc-grasp (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction right) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q))) (modify ?f3 (amount (- ?a 1))) (assert (OK grasp ?i))) ;raccolta del prodotto da sinistra andata a buon fine (defrule grasp-left-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action grasp)) (not (OK grasp ?i)) (agentpos (pos-r ?r) (pos-c ?c) (direction left)) (cell (pos-c ?c1) (pos-r ?r) (contains standred|standgreen|standyellow|standblue|standeorange |standwhite|standpink)) (test (= ?c1 (- ?c 1))) (access (standpos-c ?c1) (standpos-r ?r) (agentpos-r ?r) (agentpos-c ?c)) ?f3<- (standcontent (pos-c ?c1) (pos-r ?r) (amount ?a) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q)) (test (> ?a 0)) => (assert (perc-grasp (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction left) (item ?item) (brand ?brand) (cost ?cost) (quantity ?q))) (modify ?f3 (amount (- ?a 1))) (assert (OK grasp ?i))) ;lettura legenda a destra andata a buon fine (defrule ilegenda-right-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action inspectlegenda)) (agentpos (pos-r ?r) (pos-c ?c) (direction right)) (cell (pos-c ?c1) (pos-r ?r) (contains legenda)) (test (= ?c1 (+ ?c 1))) (access (standpos-r ?r) (standpos-c ?c1) (agentpos-r ?r) (agentpos-c ?c)) (legendacontent (pos-r ?r) (pos-c ?c1) (standcolor ?col) (product ?prod)) => (assert (perc-legenda (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction right) (standcolor ?col) (product ?prod))) (assert (OK inspectlegenda ?i))) ;lettura legenda a sinistra andata a buon fine (defrule ilegenda-left-OK (declare (salience 20)) (status (time ?i)) (exec (time ?i) (action inspectlegenda)) (agentpos (pos-r ?r) (pos-c ?c) (direction left)) (cell (pos-c ?c1) (pos-r ?r) (contains legenda)) (test (= ?c1 (- ?c 1))) (access (standpos-r ?r) (standpos-c ?c1) (agentpos-r ?r) (agentpos-c ?c)) (legendacontent (pos-r ?r) (pos-c ?c1) (standcolor ?col) (product ?prod)) => (assert (perc-legenda (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction left) (standcolor ?col) (product ?prod))) (assert (OK inspectlegenda ?i))) ;lettura legenda fallita (defrule ilegenda-KO (declare (salience 19)) (status (time ?i)) (exec (time ?i) (action inspectlegenda)) (agentpos (pos-r ?r) (pos-c ?c) (direction ?d) (time ?i)) (not (OK inspectlegenda ?i)) => (assert (perc-legenda (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction ?d) (standcolor fail))) (assert (KO inspectlegenda ?i))) ;lettura fallita (defrule read-KO (declare (salience 19)) (status (time ?i)) (exec (time ?i) (action read)) (agentpos (pos-r ?r) (pos-c ?c) (direction ?d) (time ?i)) (not (OK read ?i)) => (assert (perc-read (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction ?d) (item fail))) (assert (KO read ?i))) ;raccolta prodotto fallita (defrule grasp-KO (declare (salience 19)) (status (time ?i)) (exec (time ?i) (action grasp)) (agentpos (pos-r ?r) (pos-c ?c) (direction ?d) (time ?i)) (not (OK grasp ?i)) => (assert (perc-grasp (time (+ ?i 1)) (pos-r ?r) (pos-c ?c) (direction ?d) (item fail))) (assert (KO grasp ?i))) ;terminazione di un'azione conclusasi correttamente (defrule close-OK (declare (salience 18)) ?f2<- (status (time ?i)) (exec (time ?i) (action ?a&grasp|read|inspectlegenda)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c) (time ?i)) ?f3<- (OK ?a ?i) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (retract ?f3)) ;terminazione di un'azione conclusasi con un fallimento (defrule close-KO (declare (salience 18)) ?f2<- (status (time ?i)) (exec (time ?i) (action ?a&grasp|read|inspectlegenda)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c) (time ?i)) ?f3<- (KO ?a ?i) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (retract ?f3)) ;percezione che riceve l'agente sulla cella di fronte (direzione up) (defrule percept-move-up1 (declare (salience 5)) (agentpos (pos-r ?r) (pos-c ?c) (time ?t)(direction up)) (cell (pos-r ?r1) (pos-c ?c) (contains ?x&~nothing)) (test (= ?r1 (+ ?r 1))) => (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction up) (perc1 ?x) (perc2 unknown) (perc3 unknown))) (focus MAIN)) ;percezione dell'agente sulle due celle di fronte (direzione up) (defrule percept-move-up2 (declare (salience 5)) (agentpos (pos-r ?r) (pos-c ?c) (time ?t)(direction up)) (cell (pos-r ?r1) (pos-c ?c) (contains nothing)) (test (= ?r1 (+ ?r 1))) (cell (pos-r ?r2) (pos-c ?c) (contains ?y&~nothing)) (test (= ?r2 (+ ?r 2))) => (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction up) (perc1 empty) (perc2 ?y) (perc3 unknown))) (focus MAIN)) ;percezione dell'agente sulle tre celle di fronte (direzione up) (defrule percept-move-up3 (declare (salience 5)) (agentpos (pos-r ?r) (pos-c ?c) (time ?t)(direction up)) (cell (pos-r ?r1) (pos-c ?c) (contains nothing)) (test (= ?r1 (+ ?r 1))) (cell (pos-r ?r2) (pos-c ?c) (contains nothing)) (test (= ?r2 (+ ?r 2))) (cell (pos-r ?r3) (pos-c ?c) (contains ?z)) (test (= ?r3 (+ ?r 3))) => (if (eq ?z nothing) then (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction up) (perc1 empty) (perc2 empty) (perc3 empty))) else (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction up) (perc1 empty) (perc2 empty) (perc3 ?z)))) (focus MAIN)) ;percezione che riceve l'agente sulla cella di fronte (direzione down) (defrule percept-move-down1 (declare (salience 5)) (agentpos (pos-r ?r) (pos-c ?c) (time ?t)(direction down)) (cell (pos-r ?r1) (pos-c ?c) (contains ?x&~nothing)) (test (= ?r1 (- ?r 1))) => (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction down) (perc1 ?x) (perc2 unknown) (perc3 unknown))) (focus MAIN)) ;percezione che riceve l'agente sulle due celle di fronte (direzione down) (defrule percept-move-down2 (declare (salience 5)) (agentpos (pos-r ?r) (pos-c ?c) (time ?t)(direction down)) (cell (pos-r ?r1) (pos-c ?c) (contains nothing)) (test (= ?r1 (- ?r 1))) (cell (pos-r ?r2) (pos-c ?c) (contains ?y&~nothing)) (test (= ?r2 (- ?r 2))) => (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction down) (perc1 empty) (perc2 ?y) (perc3 unknown))) (focus MAIN)) ;percezione che riceve l'agente sulle tre celle di fronte (direzione down) (defrule percept-move-down3 (declare (salience 5)) (agentpos (pos-r ?r) (pos-c ?c) (time ?t)(direction down)) (cell (pos-r ?r1) (pos-c ?c) (contains nothing)) (test (= ?r1 (- ?r 1))) (cell (pos-r ?r2) (pos-c ?c) (contains nothing)) (test (= ?r2 (- ?r 2))) (cell (pos-r ?r3) (pos-c ?c) (contains ?z)) (test (= ?r3 (- ?r 3))) => (if (eq ?z nothing) then (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction down) (perc1 empty) (perc2 empty) (perc3 empty))) else (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction down) (perc1 empty) (perc2 empty) (perc3 ?z)))) (focus MAIN)) ;percezione che riceve l'agente sulla cella di fronte (direzione left) (defrule percept-move-left1 (declare (salience 5)) (agentpos (pos-c ?c) (pos-r ?r) (time ?t)(direction left)) (cell (pos-c ?c1) (pos-r ?r) (contains ?x&~nothing)) (test (= ?c1 (- ?c 1))) => (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction left) (perc1 ?x) (perc2 unknown) (perc3 unknown))) (focus MAIN)) ;percezione che riceve l'agente sulle due celle di fronte (direzione left) (defrule percept-move-left2 (declare (salience 5)) (agentpos (pos-c ?c) (pos-r ?r) (time ?t)(direction left)) (cell (pos-c ?c1) (pos-r ?r) (contains nothing)) (test (= ?c1 (- ?c 1))) (cell (pos-c ?c2) (pos-r ?r) (contains ?y&~nothing)) (test (= ?c2 (- ?c 2))) => (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction left) (perc1 empty) (perc2 ?y) (perc3 unknown))) (focus MAIN)) ;percezione che riceve l'agente sulle tre celle di fronte (direzione left) (defrule percept-move-left3 (declare (salience 5)) (agentpos (pos-r ?r) (pos-c ?c) (time ?t)(direction left)) (cell (pos-c ?c1) (pos-r ?r) (contains nothing)) (test (= ?c1 (- ?c 1))) (cell (pos-c ?c2) (pos-r ?r) (contains nothing)) (test (= ?c2 (- ?c 2))) (cell (pos-c ?c3) (pos-r ?r) (contains ?z)) (test (= ?c3 (- ?c 3))) => (if (eq ?z nothing) then (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction left) (perc1 empty) (perc2 empty) (perc3 empty))) else (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction left) (perc1 empty) (perc2 empty) (perc3 ?z)))) (focus MAIN)) ;percezione che riceve l'agente sulla cella di fronte (direzione right) (defrule percept-move-right1 (declare (salience 5)) (agentpos (pos-c ?c) (pos-r ?r) (time ?t)(direction right)) (cell (pos-c ?c1) (pos-r ?r) (contains ?x&~nothing)) (test (= ?c1 (+ ?c 1))) => (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction right) (perc1 ?x) (perc2 unknown) (perc3 unknown))) (focus MAIN)) ;percezione che riceve l'agente sulle due celle di fronte (direzione right) (defrule percept-move-right2 (declare (salience 5)) (agentpos (pos-c ?c) (pos-r ?r) (time ?t)(direction right)) (cell (pos-c ?c1) (pos-r ?r) (contains nothing)) (test (= ?c1 (+ ?c 1))) (cell (pos-c ?c2) (pos-r ?r) (contains ?y&~nothing)) (test (= ?c2 (+ ?c 2))) => (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction right) (perc1 empty) (perc2 ?y) (perc3 unknown))) (focus MAIN)) ;percezione che riceve l'agente sulle tre celle di fronte (direzione right) (defrule percept-move-right3 (declare (salience 5)) (agentpos (pos-r ?r) (pos-c ?c) (time ?t)(direction right)) (cell (pos-c ?c1) (pos-r ?r) (contains nothing)) (test (= ?c1 (+ ?c 1))) (cell (pos-c ?c2) (pos-r ?r) (contains nothing)) (test (= ?c2 (+ ?c 2))) (cell (pos-c ?c3) (pos-r ?r) (contains ?z)) (test (= ?c3 (+ ?c 3))) => (if (eq ?z nothing) then (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction right) (perc1 empty) (perc2 empty) (perc3 empty))) else (assert (percepts (time ?t)(pos-r ?r) (pos-c ?c) (direction right) (perc1 empty) (perc2 empty) (perc3 ?z)))) (focus MAIN)) ;torno al main dopo aver asserito tutte le percezioni (defrule done (status (time ?t)) => (focus MAIN)) (defmodule AGENT (import MAIN ?ALL)) (deftemplate kpos (slot time) (slot pos-r) (slot pos-c) (slot direction)) (defrule beginagent (declare (salience 10)) (status (time 0)) => (assert (kpos (time 0) (pos-r 1) (pos-c 2) (direction up))) ) (defrule act ?f <- (status (time ?t)) => (printout t crlf crlf) (printout t "action to be executed at time:" ?t) (printout t crlf crlf) (modify ?f (result no))) (defrule go (status (time ?t)) (exec (time ?t)) => (focus MAIN))