;;;************************************** ;;;Termostat ;;;Maciej Fabia, MIW 2009 ;;;************************************** ;;**************** ;;* DEFFUNCTIONS * ;;**************** (deffunction ask-question (?question $?allowed-values) (printout t ?question) (bind ?answer (read)) (if (lexemep ?answer) then (bind ?answer (lowcase ?answer))) (while (not (member ?answer ?allowed-values)) do (printout t ?question) (bind ?answer (read)) (if (lexemep ?answer) then (bind ?answer (lowcase ?answer)))) ?answer) (deffunction ask-number (?question ?lower-limit ?upper-limit) (printout t ?question) (bind ?answer (read)) (while (not (and (integerp ?answer) (>= ?answer ?lower-limit) (<= ?answer ?upper-limit))) do (printout t ?question) (bind ?answer (read))) ?answer) ;;************ ;;* RULES * ;;************ (defrule ask-month (not (month ?)) => (assert (month (ask-number "Podaj miesiac, liczba 1-12: " 1 12)))) (defrule ask-day (not (day ?)) => (assert (day (ask-question "Podaj dzien, mon/tue/wed/thu/fri/sat/san: " mon tue wed thu fri sat sun)))) (defrule ask-hour (not (hour ?)) => (assert (hour (ask-number "Podaj godzine, liczba 0-23: " 0 23)))) (defrule season-is-winter (month 1|2|12) (not (season ?)) => (assert (season winter))) (defrule season-is-spring (month 3|4|5) (not (season ?)) => (assert (season spring))) (defrule season-is-summer (month 6|7|8) (not (season ?)) => (assert (season summer))) (defrule season-is-fall (month 9|10|11) (not (season ?)) => (assert (season fall))) (defrule today-is-workday (day mon|tue|wed|thu|fri) (not (today ?)) => (assert (today workday))) (defrule today-is-weekend (day sat|sun) (not (today ?)) => (assert (today weekend))) (defrule business-hours (not (business-hours ?)) (today workday) (hour ?hour) (test (and (>= ?hour 9) (<= ?hour 17))) => (assert (business-hours yes))) (defrule not-business-hours-too-early (not (business-hours ?)) (today workday) (hour ?hour) (test (< ?hour 9)) => (assert (business-hours no))) (defrule not-business-hours-too-late (not (business-hours ?)) (today workday) (hour ?hour) (test (> ?hour 17)) => (assert (business-hours no))) (defrule not-business-hours-weekend (not (business-hours ?)) (today weekend) => (assert (business-hours no))) (defrule summer-free-time (not (setting ?)) (season summer) (business-hours no) => (assert (setting 27))) (defrule summer-business (not (setting ?)) (season summer) (business-hours yes) => (assert (setting 24))) (defrule spring-free-time (not (setting ?)) (season spring) (business-hours no) => (assert (setting 15))) (defrule spring-business (not (setting ?)) (season spring) (business-hours yes) => (assert (setting 20))) (defrule winter-free-time (not (setting ?)) (season winter) (business-hours no) => (assert (setting 14))) (defrule winter-business (not (setting ?)) (season winter) (business-hours yes) => (assert (setting 18))) (defrule fall-free-time (not (setting ?)) (season fall) (business-hours no) => (assert (setting 16))) (defrule fall-business (not (setting ?)) (season fall) (business-hours yes) => (assert (setting 20))) (defrule Answer (setting ?setting) => (printout t "Set thermostat to " ?setting " degrees" crlf))