-- RobertKasper - 2011-06-02
;; Dependencies between questions are defined as separate facts (Bob K.)
;; TO DO: need to define result of a question

(deftemplate question 
   "A complete question."
   (slot id)
   (slot result)
   (slot type (default demographic))
   (slot desc (default "None"))
   (multislot quests)
   (multislot options))

(deftemplate dependency
   "A question depends on responses to another question."
   (slot qid)
   (slot dependson)
   (multislot options))

(deffacts questions
   (question (id 1) 
   (desc "Age") 
   (quests M "Your Age(M)" 
      F "Your Age(F)")
   (options 1 "Under 18" 
      2 "18-24" 
      3 "25-30" 
      4 "31-40" 
      5 "41-50" 
      6 "51-60" 
      7 "60+"))

   (question (id 8) 
   (desc "Relationship status") 
   (quests M "Current relationship status" 
      F "Current relationship status")
   (options 1 "Dating (living separately)" 
      2 "Engaged (living separately)" 
      3 "Married (living together)" 
      4 "Dating (living together)" 
      5 "Engaged (living together)" 
      6 "Married (separated)"))

   (question (id 12)
   (desc "Parent (or parent figures) in my life when I was growing up")
   (quests M "Parent (or parent figures) in my life when I was growing up"
      F "Parent (or parent figures) in my life when I was growing up")
   (options 1 "Mother and father"
      2 "Father"
      3 "Mother"))
   (dependency (qid 12)
      (dependson 8)
      (options 1 2 4 5))

   (question (id 20)
   (desc "Number of times previously married")
   (quests M "Number of times married (not counting this relationship)"
      F "Number of times married (not counting this relationship)")
   (options 1 "Under 18"
      2 "Not married before this relationship"
      3 "Once"
      4 "Twice"
      5 "Three times"
      6 "More than three times"))

   (question (id 22)
   (desc "Most recent marital relationship")
   (quests M "Your most recent marital relationship prior to this relationship"
      F "Your most recent marital relationship prior to this relationship")
   (options 1 "Widowed"
      2 "Divorced"))
   (dependency (qid 22)
      (dependson 20)
      (options 2 3 4 5))
   )

(defrule starter
   ?init <- (initial-fact)
=>
   (retract ?init)

   (printout t "              ")
   (printout t "      Marriage Demographics Demo" crlf)
   (printout t "  " crlf)
   (printout t " Hit <cr> to start" crlf)
    (printout t "  " crlf)
   (bind ?answer (readline))
   (assert (gender nil)))

(defrule getgender
   (gender nil)
=>
   (printout t "What is your Gender? (M or F)" crlf)
    (printout t "  " crlf)
   (printout t "  " crlf)
        (assert (gender (read)))
   (assert (demographics)))
   
(defrule askquestion
   (demographics)
   (gender ?g)
   ?q <- (question (id ?id) 
      (type demographic)
      (result nil) 
      (quests $?before ?g ?thequest $?after) 
      (options $?options))
   ?d <- (dependency (qid ?id)
      (dependson ?did)
      (options $?dependoptions)) 
   ;; Check for dependencies
   (or (not (dependency (qid ?id) (dependson ?) (options ?)))
       (and (dependency (qid ?id) (dependson ?did) (options $?dependoptions))
            (member$ (result ?did) $?dependoptions)
       )
   )   
=>
   (printout t ?thequest crlf)
   (bind ?len (length$ $?options))
   (bind ?i 1)
   (while (<= ?i ?len) do
      (printout t (nth$ ?i $?options) ". " (nth$ (+ ?i 1) $?options) crlf)
      (bind ?i (+ ?i  2)))
   (printout t "(" (nth$ 1 $?options) "-" (nth$ (- ?len 1) $?options) "): ")
   (bind ?answer (readline))
   (modify ?q (result ?answer))
   (printout t ?q crlf))
Topic revision: r1 - 2011-06-02 - RobertKasper
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback