(clear) (watch rules) ; Global Variables (bind ?AmountToSpend 0) (bind ?MoneyBeforeRoundOfPurchases 0) ;****************************************************************************** ; Rule for gaming applications ; ; MED CPU speed ; MAX video card speed ;****************************************************************************** (defrule gaming-rule (application gaming) ?factnum <- (application gaming) => (retract ?factnum) (assert (HWNeed CPU med) ) (printout JavaApp "You need a MED cpu for games" crlf) (assert (HWNeed VideoCard max) ) (printout JavaApp "You need a MAX power video card" crlf) ) ;****************************************************************************** ; Rule for internet surfing ; ; STD CPU speed ; STD video card speed ; MED Network ; MED HD ;****************************************************************************** (defrule internet-rule (application internet) ?factnum <- (application internet) => (retract ?factnum) (assert (HWNeed CPU std) ) (printout JavaApp "You need a STD cpu for internet surfing" crlf) (assert (HWNeed VideoCard std) ) (printout JavaApp "You need a STD power video card for internet surfing" crlf) (assert (HWNeed Network med) ) (printout JavaApp "You need a MED network connection device for internet surfing" crlf) (assert (HWNeed HD med) ) (printout JavaApp "You need a MED size hard drive for internet surfing" crlf) ) ;****************************************************************************** ; Rules for video editing and conversion ;****************************************************************************** (defrule videoediting-rule (application videoediting) ?factnum <- (application videoediting) => (retract ?factnum) (assert (HWNeed CPU max) ) (printout JavaApp "You need a MAX cpu for video-editing" crlf) (assert (HWNeed HD max) ) (printout JavaApp "You need a MAX hard drive capacity" crlf) (assert (HWNeed videocard-capture capture) ) (printout JavaApp "You need a CAPTURE video card" crlf) ) ;****************************************************************************** ; Simplification rules ; ; I have no idea why the line where the fact number is found has to be a ; preposition for the rule. It will not work if moved into the conclusion. ;****************************************************************************** (defrule eliminate-med-rule (HWNeed ?hardware max) (HWNeed ?hardware med) ?factnum <- (HWNeed ?hardware med) => (retract ?factnum) ) (defrule eliminate-std-rule (HWNeed ?hardware max) (HWNeed ?hardware std) ?factnum <- (HWNeed ?hardware std) => (retract ?factnum) ) ;****************************************************************************** ; Assert starting condition ;****************************************************************************** (assert (start selection) ) ;****************************************************************************** ; When the batch command is used to open the file, this is the first thing ; the user will see prompting them for inputs. ;****************************************************************************** (defrule startup-rule (start selection) ?factnum <- (start selection) => (retract ?factnum) ; Ask the user if they play games with their system (printout JavaApp "Do you use your computer for gaming? (y or n)" crlf) (printout JavaApp " Note: If the only games you play are 5 years old or older, answer no" crlf) (printout JavaApp "JESS-PROMPT_USER" crlf) (bind ?userselection (read JavaApp) ) (if (eq ?userselection y) then (assert (application gaming) ) ) ; Ask the user if they do video editing with their system (printout JavaApp "Do you use your computer for video-editing? (y or n)" crlf) (printout JavaApp "JESS-PROMPT_USER" crlf) (bind ?userselection (read JavaApp) ) (if (eq ?userselection y) then (assert (application videoediting) ) ) ; Ask the user if they surf the internet (printout JavaApp "Do you surf the internet with your computer (y or n)" crlf) (printout JavaApp "JESS-PROMPT_USER" crlf) (bind ?userselection (read JavaApp) ) (if (eq ?userselection y) then (assert (application internet) ) ) ; Assert the rule that asks the user to calculate the budget (assert (ready to ask for budget)) ) ;****************************************************************************** ; This rule is seperate from the other rules because it needs to run last. It ; can only run after the other rules, because this rule will fire off the rules ; that actually build the system. ;****************************************************************************** (defrule ask-for-budget-rule (declare (salience -100)) (ready to ask for budget) ?factnum <- (ready to ask for budget) => (retract ?factnum) ; Ask the user how much they have to spend on the new system (printout JavaApp "How much do you expect to spend on the new system? (integers only)" crlf) (printout JavaApp "JESS-PROMPT_USER" crlf) (bind ?AmountToSpend (read JavaApp) ) (printout JavaApp "Computer System Budget: " ?AmountToSpend crlf) (assert (purchase standard hardware)) ) ;****************************************************************************** ; This rule is fired only once. It purchases the standard hardware. This ; hardware is the very lowest needed, and the minimum is purchased. ;****************************************************************************** (defrule purchasing-standard-hardware (declare (salience -100)) (purchase standard hardware) ?factnum <- (purchase standard hardware) => (retract ?factnum) ; Buy one of everything! ; Buy a CPU (printout JavaApp "JESS-HWCDB:CPU:" ?AmountToSpend crlf) (bind ?AmountToSpend (read JavaApp)) ; Buy a Video Card (printout JavaApp "JESS-HWCDB:VideoCard:" ?AmountToSpend crlf) (bind ?AmountToSpend (read JavaApp)) (assert (purchase max hardware one)) ) ;****************************************************************************** ; This is the state that purchases the max hardware components. State 1 in ; the purchasing loop. ;****************************************************************************** (defrule Purchase-Max-Hardware-First-State (declare (salience -100)) (purchase max hardware one) ?factnum <- (purchase max hardware one) => (retract ?factnum) ; Remember how much money was here when the hardware purchasing started. (bind ?MoneyBeforeRoundOfPurchases ?AmountToSpend) (assert (purchase CPU max)) (assert (purchase VideoCard max)) (assert (purchase HW max)) (assert (purchase med hardware)) ) ;****************************************************************************** ; This is the state that purchases the med hardware components. State 2 in ; the purchasing loop. ;****************************************************************************** (defrule Purchase-Med-Hardware (declare (salience -100)) (purchase med hardware) ?factnum <- (purchase med hardware) => (retract ?factnum) ; Remember how much money was here when the hardware purchasing started. (bind ?MoneyBeforeRoundOfPurchases ?AmountToSpend) (assert (purchase CPU med)) (assert (purchase VideoCard med)) (assert (purchase HW med)) (assert (purchase max hardware two)) ) ;****************************************************************************** ; This is the state that purchases the max hardware components. State 3 in ; the purchasing loop. ;****************************************************************************** (defrule Purchase-Max-Hardware-Second-State (declare (salience -100)) (purchase max hardware two) ?factnum <- (purchase max hardware two) => (retract ?factnum) (assert (purchase CPU max)) (assert (purchase VideoCard max)) (assert (purchase HW max)) (assert (decide purchase more hardware)) ) ;****************************************************************************** ; This is the rule that actually talks to ComputerAdvice Java application to ; purchase the hardware from the cost database. ;****************************************************************************** (defrule purchase-hardware (declare (salience 100)) (purchase ?ComponentName ?Level) (HWNeed ?ComponentName ?Level) ?factnum <- (purchase ?ComponentName ?B) => (retract ?factnum) (printout JavaApp "JESS-HWCDB:" ?ComponentName ":" ?AmountToSpend crlf) (bind ?AmountToSpend (read JavaApp)) ) ;****************************************************************************** ; Purchase hardware ; ;****************************************************************************** (defrule start-to-purchase-hardware (declare (salience -100)) (start hardware purchasing) ?factnum <- (start hardware purchasing) => (retract ?factnum) (bind ?MoneyBeforeRoundOfPurchases ?AmountToSpend) ; But the CPU (assert (purchase CPU)) (assert (purchase VideoCard)) (assert (decide purchase more hardware)) ) ;****************************************************************************** ; Decide if more hardware should be purchased ;****************************************************************************** (defrule more-purchases (declare (salience -100)) (decide purchase more hardware) ?factnum <- (decide purchase more hardware) => (retract ?factnum) (if (eq ?AmountToSpend ?MoneyBeforeRoundOfPurchases) then (assert (print system info)) else (assert(purchase max hardware one)) ) ) ;****************************************************************************** ; Print out the system info and close down the Java front end. ;****************************************************************************** (defrule print-system-info (print system info) ?factnum <- (print system info) => (retract ?factnum) (printout JavaApp "JESS-PRINT_SYSTEM_INFO" crlf) (printout JavaApp "JESS-FINISHED" crlf) ) ;****************************************************************************** ; Buy a component; ;****************************************************************************** (defrule buy-component (declare (salience -50)) (buy ?componentname) ?factnum <- (buy ?componentname) => (retract ?factnum) (printout JavaApp "JESS-HWCDB:" ?componentname ":" ?AmountToSpend crlf) (bind ?AmountToSpend (read JavaApp) ) ) ; Create a socket connection to Computer Advice front end (socket 127.0.0.1 8034 JavaApp) (run)