;;;====================================================== ;;; Intelligent Environment Sample Rules ;;; ;;; To execute, merely load, reset and run. ;;;====================================================== (deffacts initial-state (run) (time 7 15) (location 1 t) (location 2 f) (location 3 f) (location 4 f) (location 5 f) (location 6 f) (lights 1 off) (lights 2 off) (lights 3 off) (lights 4 off) (lights 5 off) (lights 6 off) (ac 1 off) (ac 2 off) (ac 3 off) (ac 4 off) (ac 5 off) (ac 6 off) (temperature 1 70) (temperature 2 70) (temperature 3 70) (temperature 4 70) (temperature 5 70) (temperature 6 70) (robot-location 3) (robot-sens 0 0 0) (power 0) (light-cost 1.5) (heat-cost 20) (ac-cost 25)) ;; ;; Prints out the state of the room that the inhabitant (or intruder) is in ;; (defrule print-state (location ?room t) (temperature ?room ?temp) (lights ?room ?state) (time ?hours ?minutes) => (printout t "Time: " ?hours ":" ?minutes " Location: room " ?room " Temperature: " ?temp " Light: " ?state crlf)) ;; ;; Example rule that attempts to regulate the temperature in room 1 between ;; 70 and 74 degrees ;; (defrule room-1-heat-control (temperature 1 ?temp1) => (if (> ?temp1 74) then (assert (turn-ac 1)) else (if (< ?temp1 70) then (assert (turn-heat 1)) else (assert (turn-ac-off 1))))) ;; ;; Example rule that turns the light in room 4 on at around 5:00 pm (if it is ;; not already on) ;; (defrule turn-on-room4-light-before-coming-home (time ?hours ?minutes) (lights 4 ?state) => (if (and (>= ?hours 17) (eq ?state off)) then (assert (toggle-light 4)))) ;; ;; Example rule that turns the light in room 4 if the inhabitant walked from ;; room 1 to room 3 before (and if the light is not already on) ;; (defrule turn-room4-light-after-room1-room3 (location ?room t) ?prev <- (previous-loc ?prevloc) (lights 4 ?state) => (retract ?prev) (assert (previous-loc ?room)) (if (and (and (eq ?room 3) (eq ?prevloc 1)) (eq ?state off)) then (assert (toggle-light 4)))) ;;;***************************** ;;; rules for the device control ;;;***************************** ;; ;; toggles the ligth in room x if (toggle-light x) is set ;; (defrule toggle-light ?act <- (toggle-light ?room) ?olight <- (lights ?room ?state) => (retract ?act ?olight) (if (eq ?state off) then (assert (lights ?room on)) (printout t "Light in room " ?room " turned on" crlf) else (assert (lights ?room off)) (printout t "Light in room " ?room " turned off" crlf))) ;; ;; turns on the heat in room x if (turn-heat x) is set ;; (defrule turn-heat ?act <- (turn-heat ?room) ?olight <- (ac ?room ?state) => (retract ?act ?olight) (assert (ac ?room heat)) (if (or (eq ?state off) (eq ?state cool)) then (printout t "Heat in room " ?room " turned on" crlf))) ;; ;; turns on the ac in room x if (turn-ac x) is set ;; (defrule turn-ac ?act <- (turn-ac ?room) ?olight <- (ac ?room ?state) => (retract ?act ?olight) (assert (ac ?room cool)) (if (or (eq ?state off) (eq ?state heat)) then (printout t "AC in room " ?room " turned on" crlf))) ;; ;; turns off the ac/heat in room x if (turn-ac-off x) is set ;; (defrule turn-ac-off ?act <- (turn-ac-off ?room) ?olight <- (ac ?room ?state) => (retract ?act ?olight) (assert (ac ?room off)) (if (or (eq ?state heat) (eq ?state cool)) then (printout t "AC/Heat in room " ?room " turned off" crlf))) ;; ;; moves robot by x if (move-robot x) is set ;; (defrule move-robot ?act <- (move-robot ?dist) => (retract ?act)) ;; ;; turns robot by x if (turn-robot x) is set ;; (defrule turn-robot ?act <- (turn-robot ?dist) => (retract ?act)) ;;;***************************** ;;; rules to read robot sensors ;;;***************************** ;; ;; reads the sensors of the robot ;; (defrule read-robot-sensors ?act <- (read-robot) ?ostate <- (robot-sens ?enc1 ?enc2 ?lightsens) => (retract ?act ?ostate) (assert (robot-sens 0 0 0)))