(defmodule MAIN (export ?ALL)) (deftemplate exec (slot action (allowed-values go turnright turnleft fire grasp climb)) (slot time)) (deftemplate status (slot time) (slot result) (slot gold)) (deftemplate percepts (slot time) (slot pos-r) (slot pos-c) (slot perception)) (deffacts init (create) (dimension 5)) (defrule createworld (create) => (focus ENV)) (defrule go-on-agent (declare (salience 20)) ?f1 <- (status (time ?i) (result ?x&~death)) => (focus AGENT)) (defrule go-on-env (declare (salience 20)) ?f1 <- (status (time ?i)) (exec (time ?i)) => (focus ENV)) (defrule failure (declare (salience 100)) ?f1 <- (status (result death)) => (halt)) (defrule success (declare (salience 100)) ?f1 <- (status (result yes) (gold ?g)) => (halt)) (defmodule ENV (import MAIN ?ALL)(export ?ALL)) (deftemplate cell (slot pos-r) (slot pos-c) (slot contains (allowed-values nothing wumpus pit gold))) (deftemplate agentpos (slot pos-r) (slot pos-c) (slot direction) (slot time) (slot gold) (slot arrow)) (defrule creation ?f1 <- (create) => (assert (cell (pos-r 1) (pos-c 1) (contains nothing)) (cell (pos-r 1) (pos-c 2) (contains nothing)) (cell (pos-r 1) (pos-c 3) (contains nothing)) (cell (pos-r 1) (pos-c 4) (contains wumpus)) (cell (pos-r 1) (pos-c 5) (contains nothing)) (cell (pos-r 2) (pos-c 1) (contains nothing)) (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 3) (pos-c 1) (contains nothing)) (cell (pos-r 3) (pos-c 2) (contains pit)) (cell (pos-r 3) (pos-c 3) (contains nothing)) (cell (pos-r 3) (pos-c 4) (contains nothing)) (cell (pos-r 3) (pos-c 5) (contains nothing)) (cell (pos-r 4) (pos-c 1) (contains nothing)) (cell (pos-r 4) (pos-c 2) (contains nothing)) (cell (pos-r 4) (pos-c 3) (contains pit)) (cell (pos-r 4) (pos-c 4) (contains gold)) (cell (pos-r 4) (pos-c 5) (contains nothing)) (cell (pos-r 5) (pos-c 1) (contains nothing)) (cell (pos-r 5) (pos-c 2) (contains nothing)) (cell (pos-r 5) (pos-c 3) (contains nothing)) (cell (pos-r 5) (pos-c 4) (contains nothing)) (cell (pos-r 5) (pos-c 5) (contains nothing)) ) (retract ?f1) (assert (status (time 0) (result no) (gold 0)) (agentpos (pos-r 1) (pos-c 1) (time 0)(direction right)(gold 0)(arrow 1))) (focus MAIN)) (defrule move-up-ok (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) (dimension ?d) ?f1<- (agentpos (pos-r ?r&~?d) (pos-c ?c)(direction up)) => (modify ?f1 (pos-r (+ ?r 1)) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) (defrule move-up-bump (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) (dimension ?d) ?f1<- (agentpos (pos-r ?d) (pos-c ?c)(direction up)) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (assert (percepts (time (+ ?i 1))(pos-r ?d) (pos-c ?c) (perception bump)))) (defrule move-down-bump (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r 1) (pos-c ?c)(direction down)) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (assert (percepts (time (+ ?i 1))(pos-r 1) (pos-c ?c) (perception bump)))) (defrule move-down-ok (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r&~1) (pos-c ?c)(direction down)) => (modify ?f1 (pos-r (- ?r 1)) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) (defrule move-left-ok (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c&~1) (direction left)) => (modify ?f1 (pos-c (- ?c 1)) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) (defrule move-left-bump (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) ?f1<- (agentpos (pos-r ?r) (pos-c 1) (direction left)) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (assert (percepts (time (+ ?i 1))(pos-r ?r) (pos-c 1) (perception bump)))) (defrule move-right-ok (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) (dimension ?d) ?f1<- (agentpos (pos-r ?r) (pos-c ?c&~?d) (direction right)) => (modify ?f1 (pos-c (+ ?c 1))(time (+ ?i 1))) (modify ?f2 (time (+ ?i 1)))) (defrule move-right-bump (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action go)) (dimension ?d) ?f1<- (agentpos (pos-r ?r) (pos-c ?d) (direction right)) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (assert (percepts (time (+ ?i 1))(pos-r ?r) (pos-c ?d) (perception bump)))) (defrule climb-ok (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action climb)) ?f1<- (agentpos (pos-r 1) (pos-c 1) (gold ?g)) => (modify ?f2 (time (+ ?i 1)) (result yes) (gold ?g))) (defrule climb-bump (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action climb)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c)) (or (test (neq ?r 1)) (test (neq ?c 1))) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (assert (percepts (time (+ ?i 1))(pos-r ?r) (pos-c ?c) (perception bump)))) (defrule getgold-ok (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action grasp)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c) (gold ?g)) ?f3<- (cell (pos-r ?r) (pos-c ?c) (contains gold)) => (modify ?f1 (gold (+ ?g 1)) (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (modify ?f3 (contains nothing)) (assert (percepts (time (+ ?i 1))(pos-r ?r) (pos-c ?c) (perception gotgold)))) (defrule getgold-ko (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action grasp)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c)) (cell (pos-r ?r) (pos-c ?c) (contains ?x&~gold)) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) ) (defrule fire-ok (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action fire)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c) (direction ?d) (arrow ?a&~0)) ?f3<- (cell (pos-r ?x) (pos-c ?y) (contains wumpus)) => (modify ?f1 (time (+ ?i 1)) (arrow (- ?a 1))) (modify ?f2 (time (+ ?i 1))) (if (or (and (= ?r ?x) (eq ?d right) (> ?y ?c)) (and (= ?r ?x) (eq ?d left) (< ?y ?c)) (and (= ?c ?y) (eq ?d up) (> ?x ?r)) (and (= ?c ?y) (eq ?d down) (< ?x ?r))) then (modify ?f3 (contains nothing)) (assert (percepts (time (+ ?i 1))(pos-r ?r) (pos-c ?c) (perception cry))))) (defrule fire-ko (declare (salience 20)) ?f2<- (status (time ?i)) (exec (time ?i) (action fire)) ?f1<- (agentpos (pos-r ?r) (pos-c ?c) (arrow 0)) => (modify ?f1 (time (+ ?i 1))) (modify ?f2 (time (+ ?i 1))) (assert (percepts (time (+ ?i 1))(pos-r ?r) (pos-c ?c) (perception click)))) (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)))) (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)))) (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)))) (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)))) (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)))) (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)))) (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)))) (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)))) (defrule checkpos (declare (salience 10)) ?f2 <- (status (time ?i)) ?f1 <- (agentpos (pos-r ?r) (pos-c ?c) (time ?i)) ?f3 <- (cell (pos-r ?r) (pos-c ?c) (contains wumpus|pit)) => (modify ?f2 (time (+ ?i 1)) (result death)) (focus MAIN)) (defrule producepercept1 (declare (salience 5)) (agentpos (pos-r ?r) (pos-c ?c) (time ?t)) (cell (pos-r ?x) (pos-c ?y) (contains wumpus)) (or (and (test (= ?y ?c)) (test (= ?x (- ?r 1)))) (and (test (= ?y ?c)) (test (= ?x (+ ?r 1)))) (and (test (= ?x ?r)) (test (= ?y (- ?c 1)))) (and (test (= ?x ?r)) (test (= ?y (+ ?c 1))))) => (assert (percepts (time ?t) (pos-r ?r) (pos-c ?c) (perception smell))) ) (defrule producepercept2 (declare (salience 5)) (agentpos (pos-r ?r) (pos-c ?c) (time ?t)) (cell (pos-r ?x) (pos-c ?y) (contains pit)) (or (and (test (= ?y ?c)) (test (= ?x (- ?r 1)))) (and (test (= ?y ?c)) (test (= ?x (+ ?r 1)))) (and (test (= ?x ?r)) (test (= ?y (- ?c 1)))) (and (test (= ?x ?r)) (test (= ?y (+ ?c 1))))) => (assert (percepts (time ?t) (pos-r ?r) (pos-c ?c) (perception breeze))) ) (defrule producepercept3 (declare (salience 5)) (agentpos (pos-r ?r) (pos-c ?c) (time ?t)) (cell (pos-r ?r) (pos-c ?c) (contains gold)) => (assert (percepts (time ?t) (pos-r ?r) (pos-c ?c) (perception glitter))) ) (defrule done (status (time ?t)) => (focus MAIN)) (defmodule AGENT (import MAIN ?ALL)) (deftemplate kpos (slot time) (slot pos-r) (slot pos-c) (slot gold) (slot arrow)(slot direction)) (defrule beginagent (declare (salience 10)) (status (time 0)) => (assert (kpos (time 0) (pos-r 1) (pos-c 1) (direction right) (arrow 1) (gold 0))) ) (defrule act (status (time ?t)) => (assert (ask ?t))) (defrule go (status (time ?t)) (exec (time ?t)) => (focus MAIN))