Basic BBESS Algorithm & Functions

CIS447

 

Steps of the basic algorithm:

 

  w Startup and initialization

      v load rules & prompts file

            X set global variables:  *rules*   *prompts*

 

  w Run basic interface

      v set initial variables

            X initialize *fb* to empty list

      v issue prompt to user

      v get user response (input)

            X if user response is a query . . .

                Y process the query

                     T call a central query-answering function, e.g., queryans

                Y print result of processing

            X otherwise, exit the program

            X Note: to make a better user interface, if user input is neither a query nor a

                   recognized “quit” command, print a message and give user another

                   chance to input a valid response

      v Sample code:

        (defun run ()
          (print "BBESS now running")
          (terpri)
          (do ((query (getquery)(getquery)))
                ((member query '(q quit bye exit)) 'done)
             (setf *fb* '())
             (print (queryans query))
             (terpri)
          ))
 

queryans

 

  w parameter: query

  w basic structure: cond

  w basic steps

      v call lookinfb

      v call tryprompting

      v call tryinferencing

  w returns:

      v if any of the 3 steps succeeds, returns answer generated by that step

      v otherwise, returns appropriate message about incomplete rule base

      v Note: structure of cond is well suited for this.

  w Sample code:

    (defun queryans (query)
       (cond ((lookinfb query))
             ((tryprompting query))
             ((infproc (getrules query)))
             (t "Sorry. No rules for that!")))
 

lookinfb

 

  w parameter: query (which, conceptually, is an attribute)

  w use member along with :test and appropriate testing function

      v look for a fact (attribute-value pair) whose first element is query

            X if found, return the value part of the pair

                Y Note-1: member will return a list of facts; you must pluck the first fact out

                   of that list

                Y Note-2: since first of nil is nil you can use first for plucking regardless of

                   whether or not the membership test succeeds

            X otherwise, return nil

  w Note: an appropriate testing function for you to define would be is-second, which

          would take an atom and a list as parameters and would return t if the atom is

          equal to the 2nd element of the list.

 

tryprompting

 

  w parameter: query (which, conceptually, is an attribute)

  w use let to hold result of looking in the set of prompts

      v use member along with :test and appropriate testing function to see if a prompt is

          available

      v if it is . .

            X issue a prompt

            X receive an answer

            X check the answer for validity

                Y if answer is valid, call putinfb and return the answer

                Y otherwise, return nil

      v if it is not, ask user for a valid response

      v Note: you can add the check for validity after your basic program is completed

 

putinfb

 

  w parameters: attribute, value

  w join attribute and value into a list to form a fact

  w place that fact into *fb*

      v use cons and setf

      v Note: in this program, you should use setf only for setting global variables

  w return value

      v Remember: to return something, just make it the last form evaluated

 

tryinferencing

 

  w parameter: query

  w use let to hold result of calling getrules to get a set of all appropriate rules

  w if list is not empty, return result of calling tryrules

  w otherwise, return nil

 

getrules

 

  w parameter: query

  w go through *rules* and gather up all rules whose conclusion would give an

          answer to the query, if the premises were true

      v This means all rules which have query in the attribute part of their

          conclusion

      v Remember: a conclusion is an attribute-value pair

  w return that list, even if it is empty

 

tryrules

 

  w parameter: a list of rules to try

  w use or trick and call to tryrule to generate a response

      v Note: we will see below that the response will either be an answer to the query or

          nil.

 

tryrule

 

  w parameter: one rules to try

  w call checkprems to test premises of the rule

      v If checkprems returns t call putinfb with the conclusion of the rule

            X Note: the conclusion of the rule is a fact (attribute value pair)

      v Otherwise, return nil.

 

checkprems

 

  w parameter: a list of premises (each of which is an attribute-value pair)

  w use and trick and call to checkprem to generate a response

      v Note: we will see below that the response will either be t or nil.

 

checkprem

 

  w parameter: one premise to test for truth-value

      v Remember: a premise is an attribute-value pair

  w call queryans on the attribute part of the premise

      v If the answer returned matches the value part of the premise, return t.

      v Otherwise, return nil.

 

suggested helper functions

   Note: these are not required but make your code much more readable

   Remember: readability often saves many hours of debugging time!!

  w getquery - basic user interface

  w askfor - issues prompt to user & tests for valid response

  w Basic retrieval functions

      v premises-of - get premises of a rule

      v conclusion-of - get conclusion of a rule

      v attribute-of - get attribute part of a fact

      v value-of - get value part of a fact

 

Remember: many of these functions can be tested by themselves.  Be sure to take advantage of Lisp’s flexibility to do that and thereby cut down your debugging time!

 

Sample Run:

#<OUTPUT BUFFERED FILE-STREAM CHARACTER #P"run-bbess" @1>
[2]> (load "rules.cl")
;; Loading file rules.cl ...
;; Loading of file rules.cl is finished.
T
[3]> (load "prompts.cl")
;; Loading file prompts.cl ...
;; Loading of file prompts.cl is finished.
T
[4]> (load "sho-bbess.cl")
;; Loading file sho-bbess.cl ...
;; Loading of file sho-bbess.cl is finished.
T
[5]> (run)

"BBESS now running"

"What is your query?"
stem

"Is the stem of the plant WOODY or GREEN?"
green

GREEN

"What is your query?"
stem

"Is the stem of the plant WOODY or GREEN?"
woody

WOODY

"What is your query?"
quit
DONE
[6]> (load "bbess.cl")
;; Loading file bbess.cl ...
;; Loading of file bbess.cl is finished.
T
[7]> (run)

"BBESS now running"

"What is your query?"
stem

"Is the stem of the plant WOODY or GREEN?"
green

GREEN

"What is your query?"
type

"Is the stem of the plant WOODY or GREEN?"
green

HERB

"What is your query?"
type

"Is the stem of the plant WOODY or GREEN?"
woody

"Is the position of the stem UPRIGHT or CREEPING?"
creeping

VINE

"What is your query?"
type

"Is the stem of the plant WOODY or GREEN?"
woody

"Is the position of the stem UPRIGHT or CREEPING?"
upright

"Does the plant have one main trunk:: YES or NO?"
no

SHRUB

"What is your query?"
type

"Is the stem of the plant WOODY or GREEN?"
woody

"Is the position of the stem UPRIGHT or CREEPING?"
upright

"Does the plant have one main trunk:: YES or NO?"
yes

TREE

"What is your query?"
class

"Is the stem of the plant WOODY or GREEN?"
green

"Sorry. No rules for that!"

"What is your query?"
class

"Is the stem of the plant WOODY or GREEN?"
woody

"Is the position of the stem UPRIGHT or CREEPING?"
upright

"Does the plant have one main trunk:: YES or NO?"
yes

"Is the shape of the leaves broad and flat:: YES or NO?"
yes

ANGIOSPERM

"What is your query?"
class

"Is the stem of the plant WOODY or GREEN?"
woody

"Is the position of the stem UPRIGHT or CREEPING?"
upright

"Does the plant have one main trunk:: YES or NO?"
yes

"Is the shape of the leaves broad and flat:: YES or NO?"
no

GYMNOSPERM

"What is your query?"
family

"Is the stem of the plant WOODY or GREEN?"
woody

"Is the position of the stem UPRIGHT or CREEPING?"
upright

"Does the plant have one main trunk:: YES or NO?"
yes

"Is the shape of the leaves broad and flat:: YES or NO?"
yes

"Sorry. No rules for that!"

"What is your query?"
family

"Is the stem of the plant WOODY or GREEN?"
woody

"Is the position of the stem UPRIGHT or CREEPING?"
upright

"Does the plant have one main trunk:: YES or NO?"
yes

"Is the shape of the leaves broad and flat:: YES or NO?"
no

"Is the leaf shape NEEDLELIKE or SCALELIKE?"
needlelike

"Is the pattern RANDOM or are the needles in 2EVENLINES?"
random

PINE

"What is your query?"
family

"Is the stem of the plant WOODY or GREEN?"
red


"Valid entries are ... "
(WOODY GREEN)
"Is the stem of the plant WOODY or GREEN?"
woody

"Is the position of the stem UPRIGHT or CREEPING?"
upright

"Does the plant have one main trunk:: YES or NO?"
yes

"Is the shape of the leaves broad and flat:: YES or NO?"
no

"Is the leaf shape NEEDLELIKE or SCALELIKE?"
scalelike

CYPRESS

"What is your query?"
family

"Is the stem of the plant WOODY or GREEN?"
woody

"Is the position of the stem UPRIGHT or CREEPING?"
upright

"Does the plant have one main trunk:: YES or NO?"
yes

"Is the shape of the leaves broad and flat:: YES or NO?"
no

"Is the leaf shape NEEDLELIKE or SCALELIKE?"
needlelike

"Is the pattern RANDOM or are the needles in 2EVENLINES?"
2evenlines

"Is there a silvery band under each needle:: YES or NO?"
yes

PINE

"What is your query?"
family

"Is the stem of the plant WOODY or GREEN?"
woody

"Is the position of the stem UPRIGHT or CREEPING?"
upright

"Does the plant have one main trunk:: YES or NO?"
yes

"Is the shape of the leaves broad and flat:: YES or NO?"
no

"Is the leaf shape NEEDLELIKE or SCALELIKE?"
needlelike

"Is the pattern RANDOM or are the needles in 2EVENLINES?"
2evenlines

"Is there a silvery band under each needle:: YES or NO?"
no

BALDCYPRESS

"What is your query?"
quit
DONE
[8]> (dribble)