Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
tutorials:beginner:designators [2016/01/22 15:02] – [Using action designators] gkazhoya | tutorials:beginner:designators [2016/03/04 14:09] – gkazhoya | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Creating action designators for the turtlesim | + | ====== Creating action designators for the TurtleSim |
**Description: | **Description: | ||
Line 22: | Line 22: | ||
<code lisp> | <code lisp> | ||
- | (defparameter spy-location (make-designator | + | (defparameter spy-location (make-designator |
</ | </ | ||
(Do not add this code to your tutorial files, it is meant simply for illustration here.) | (Do not add this code to your tutorial files, it is meant simply for illustration here.) | ||
- | This line of code creates a location designator ('' | + | This line of code creates a location designator ('' |
<code lisp> | <code lisp> | ||
Line 49: | Line 49: | ||
</ | </ | ||
- | In your '' | + | In your '' |
<code lisp> | <code lisp> | ||
Line 64: | Line 64: | ||
</ | </ | ||
- | Finally, we add '' | + | Finally, we add '' |
<code lisp> | <code lisp> | ||
+ | (defpackage : | ||
+ | (:nicknames :tut) | ||
(:use :cpl :roslisp : | (:use :cpl :roslisp : | ||
+ | (: | ||
</ | </ | ||
Line 74: | Line 77: | ||
==== Creating an action designator ==== | ==== Creating an action designator ==== | ||
- | Let's try to create an action designator in the repl command line: | + | Let's try to create an action designator in the REPL command line: |
<code lisp> | <code lisp> | ||
- | TUT> (defparameter | + | TUT> (defparameter |
- | MYDESIG | + | MY-DESIG |
- | TUT> (desig-prop-value | + | TUT> (desig-prop-value |
3 | 3 | ||
</ | </ | ||
- | As we can see, the make-designator function simply returns a designator object with the properties we specify. We can also read those already specified property values with the desig-prop-value function. So far, so good, but the interesting thing about designators is their resolution. Let's try that in the repl command line: | + | As we can see, the '' |
<code lisp> | <code lisp> | ||
- | TUT> (reference | + | TUT> (reference |
WARNING: | WARNING: | ||
| | ||
Line 93: | Line 96: | ||
| | ||
?ACT) with undefined functor ACTION-DESIG. | ?ACT) with undefined functor ACTION-DESIG. | ||
- | ; Evaluation aborted on #<CRAM-DESIGNATORS: | + | ; Evaluation aborted on #< |
</ | </ | ||
- | This rather obscure error message is roslisp_repl's way of telling you there are no rules in place to resolve this designator, so let's try to provide a few such rules. | + | This rather obscure error message is CRAM's way of telling you there are no rules in place to resolve this designator, so let's try to provide a few such rules. |
+ | In case you were wondering why we use keywords in designator properties, that is because we would like to be able to specify rules to resolve designators in one package and create designators that are resolved through these rules in a different package. To avoid naming clashes between packages and huge import lists, we simply define all designator properties in the '' | ||
==== Defining inference rules for designators ==== | ==== Defining inference rules for designators ==== | ||
- | Append the following to your action-designators.lisp file: | + | Append the following to your '' |
<code lisp> | <code lisp> | ||
Line 109: | Line 113: | ||
radius | radius | ||
edges) | edges) | ||
- | | + | |
- | (cram-reasoning: | + | (def-fact-group shape-actions (action-desig) |
- | ;; for each kind of shape, call make-turtle-shape with the right number of edges | + | ;; for each kind of shape, call MAKE-TURTLE-SHAPE with the right number of edges |
;; triangle | ;; triangle | ||
- | (cram-reasoning: | + | (<- (action-desig ?desig (draw-shape ?action)) |
- | (desig-prop ?desig (type shape)) | + | (desig-prop ?desig (:type :shape)) |
- | (desig-prop ?desig (shape triangle)) | + | (desig-prop ?desig (:shape :triangle)) |
- | (desig-prop ?desig (radius ?radius)) | + | (desig-prop ?desig (:radius ?radius)) |
- | (cram-reasoning: | + | (lisp-fun make-turtle-shape :radius ?radius :edges 3 ?action)) |
;; square | ;; square | ||
- | (cram-reasoning: | + | (<- (action-desig ?desig (draw-shape ?action)) |
- | (desig-prop ?desig (type shape)) | + | (desig-prop ?desig (:type :shape)) |
- | (desig-prop ?desig (shape square)) | + | (desig-prop ?desig (:shape :square)) |
- | (desig-prop ?desig (radius ?radius)) | + | (desig-prop ?desig (:radius ?radius)) |
- | (cram-reasoning: | + | (lisp-fun make-turtle-shape :radius ?radius :edges 4 ?action)) |
;; pentagon | ;; pentagon | ||
- | (cram-reasoning: | + | (<- (action-desig ?desig (draw-shape ?action)) |
- | (desig-prop ?desig (type shape)) | + | (desig-prop ?desig (:type :shape)) |
- | (desig-prop ?desig (shape pentagon)) | + | (desig-prop ?desig (:shape :pentagon)) |
- | (desig-prop ?desig (radius ?radius)) | + | (desig-prop ?desig (:radius ?radius)) |
- | (cram-reasoning: | + | (lisp-fun make-turtle-shape :radius ?radius :edges 5 ?action)) |
;; hexagon | ;; hexagon | ||
- | (cram-reasoning: | + | (<- (action-desig ?desig (draw-shape ?action)) |
- | (desig-prop ?desig (type shape)) | + | (desig-prop ?desig (:type :shape)) |
- | (desig-prop ?desig (shape hexagon)) | + | (desig-prop ?desig (:shape :hexagon)) |
- | (desig-prop ?desig (radius ?radius)) | + | (desig-prop ?desig (:radius ?radius)) |
- | (cram-reasoning: | + | (lisp-fun make-turtle-shape :radius ?radius :edges 6 ?action))) |
</ | </ | ||
- | Let's see what this code does. The defstruct declares a struct | + | Let's see what this code does. The '' |
- | As for the inference rules themselves, these are Prolog code embedded in Lisp, for it is Prolog that powers the inference behind designator resolution. To learn more about CRAM Prolog look at the [[cram_prolog|Using Prolog for reasoning]] tutorial. The def-fact-group is a collection of several rules, each of similar structure, so it helps to look at one of them in more detail: | + | As for the inference rules themselves, these are Prolog code embedded in Lisp, for it is Prolog that powers the inference behind designator resolution. To learn more about CRAM Prolog look at the [[cram_prolog|Using Prolog for reasoning]] tutorial. The '' |
<code lisp> | <code lisp> | ||
- | (cram-reasoning: | + | (<- (action-desig ?desig (draw-shape ?action)) |
- | (desig-prop ?desig (type shape)) | + | (desig-prop ?desig (:type :shape)) |
- | (desig-prop ?desig (shape pentagon)) | + | (desig-prop ?desig (:shape :pentagon)) |
- | (desig-prop ?desig (radius ?radius)) | + | (desig-prop ?desig (:radius ?radius)) |
- | (cram-reasoning: | + | (lisp-fun make-turtle-shape :radius ?radius :edges 5 ?action)) |
</ | </ | ||
- | The first parameter to cram-reasoning: | + | The first parameter to '' |
<code lisp> | <code lisp> | ||
- | (action-desig ?desig (shape ?action)) | + | (action-desig ?desig (draw-shape ?action)) |
</ | </ | ||
Line 163: | Line 167: | ||
<code lisp> | <code lisp> | ||
- | (desig-prop ?desig (type shape)) | + | (desig-prop ?desig (:type :shape)) |
- | (desig-prop ?desig (shape pentagon)) | + | (desig-prop ?desig (:shape :pentagon)) |
- | (desig-prop ?desig (radius ?radius)) | + | (desig-prop ?desig (:radius ?radius)) |
- | (cram-reasoning: | + | (lisp-fun make-turtle-shape :radius ?radius :edges 5 ?action) |
</ | </ | ||
- | PROLOG's inference semantics is, in a nutshell, 'IF there is some assignment to variables such that all elements of the body are true, THEN use that assignment of variables to execute | + | Prolog's inference semantics is, in a nutshell, 'IF there is some assignment to variables such that all elements of the body are true, THEN use that assignment of variables to evaluate |
+ | |||
+ | For the example rule we've selected, Prolog looks at the designator and asks "does it contain a key-value pair that is '' | ||
- | For this example rule we've selected, PROLOG looks at the designator and asks "does it contain a key-value pair that is (type shape)?" (so no variables here except | + | If all of the above body rules are true (or can be executed sucessfully) then the head is itself |
- | If all of the above body rules are true (or can be executed sucessfully) then the head is itself run, and what it does is glue the newly created ?action, the object containing the concrete values, to the designator ?desig. Or in more concrete terms, it defines what the value of the action-desig predicate is if applied to ? | + | If in any step of the inference Prolog stumbles upon '' |
- | For this tutorial, the inference is very simple: in fact, it is just a conditional switch based on the shape parameter which translates a name like ' | + | For this tutorial, the inference is very simple: in fact, it is just a conditional switch based on the shape parameter which translates a name like '' |
- | Reload the tutorial package in roslisp_repl. This will also load the newly defined inference rules. | + | Reload the tutorial package in '' |
- | Let's try to create, then reference a designator in the repl command line again: | + | Let's try to create, then reference a designator in the REPL command line again: |
<code lisp> | <code lisp> | ||
- | TUT> (defparameter | + | TUT> (defparameter |
- | MYDESIG | + | MY-DESIG-2 |
- | TUT> (reference | + | TUT> (reference |
- | (SHAPE # | + | (DRAW-SHAPE # |
</ | </ | ||