[an error occurred while processing this directive]
FJK Home CPE/CSC 481 News Syllabus Schedule Lecture Notes Assignments Paper Project Teams Other Links
CPE/CSC 481 Knowledge-Based Systems Winter 2010

Dave Groat has pointed out that one of Jess's experimental features, queries, are very useful in doing the first part of HW9. See the manual for the details.

A query, defined with defquery, is like a rule without a RHS. When you call it, you specify values for any variables declared as *input* arguments and it finds all matches for the conditions in the query. Thus, it is run only when you call it via the function run-query or count-query-results. If the first case, it returns a data structure which you can access from Java to find the sets of facts which satisfy the query. In the second, it just returns the number of answers.

Consider the following facts and the query Q1, which takes a major and finds students with that major:

  (defquery Q1
     (declare (variables ?major))
     (student ?s ?ssn)
     (major ?ssn ?major))
  (deffacts query-test
     (student s1 123-44-5678)    (student s2 223-44-5678)
     (student s3 323-44-5678)    (student s4 423-44-5678)
     (student s5 523-44-5678)
     (major 123-44-5678 cmsc)    (major 223-44-5678 ifsm)
     (major 323-44-5678 cmpe)    (major 423-44-5678 cmsc)
     (major 523-44-5678 cmsc))
Here are some examples of calling the function count-query-results to find the number of students in particular majors
    
  Jess> (count-query-results Q1 cmsc)
  3
  Jess> (count-query-results Q1 ifsm)
  1
  Jess> (count-query-results Q1 history)
  0
  Jess>
It should be easy to define a query like:
  (defquery possible-adjacent-pits
    "Finds the caves adjacent to a given (X,Y) which might contain a pit"
    (declare (variable ?x ?y))
    (adj ?x ?y ?x2 ?y2)
    ...)
Now, create a rule which fires for a breezy pit. Add a conditional element to the rule which uses count-query-results to call our query with the breezy pit's X and Y. test the return value to require it to be 1. The rest of this rule would then have conditional elements that pick out the adjacent cave which does have a possible pit. The rule's RHS will then change the representation to indicate that the appropriate cave does have a pit
FJK Home CPE/CSC 481 News Syllabus Schedule Lecture Notes Assignments Paper Project Teams Other Links
query.html
last modified:
Wednesday, February, 10, 2010, 14:07:41 PST
© 2000-2024
Franz J. Kurfess