December 2015 |
Artificial Intelligence: Expert Systems with CLIPS and Free Software |
LIU/Brooklyn Artificial Intelligence |
(deftemplate node (slot name ) (slot type ) (slot question ) (slot yes-node) (slot no-node ) (slot answer ) ) This is similar to what we would see as a structure in procedural code. |
(deffacts tree (node (name root)(type decision) (question "Is the animal warm blooded?" ) (yes-node node1) (no-node node2)) (node (name node1)(type decision) (question "Does it purr?") (yes-node node3) (no-node node4)) (node (name node2) (type answer) (answer snake)) (node (name node3) (type answer) (answer cat)) (node (name node4) (type answer) (answer dog)) (current-node root) ) These create nodes on reset that either push you down the tree (decisions) or are leafs (answer). Now we just need some rules to stitch it together. |
(defrule ask-decision-node-question ?node <- (current-node ?name) (node (name ?name) (type decision) (question ?question)) (not (answer ?)) => (printout t ?question "(yes or no) ") (assert (answer (read))) ) ;******* ;****** ;***** (defrule bad-answer ?answer <- (answer ~yes&~no) => (retract ?answer) ) (defrule proceed-to-yes-branch ?node <- (current-node ?name) (node (name ?name) (type decision) (yes-node ?yes-branch)) ?answer <- (answer yes) => (retract ?node ?answer) (assert (current-node ?yes-branch)) ) ;******* ;****** ;***** (defrule proceed-to-no-branch ?node <- (current-node ?name) (node (name ?name) (type decision) (no-node ?yes-branch)) ?answer <- (ansert no) => (retract ?node ?answer) (assert (current-node ?no-branch)) ) One set of rules for yes decisions, another for no decisions and a third bad answers. The last rule is for the leaf |
(defrule correct ?node <- (current-node ?name) (node (name ?name) (tyoe answer)(answer ?value)) (not (answer ?)) => (prinout t "I guess it is a " ?value crlf) )At this point I note three things here:
|