Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorials:advanced:events [2015/05/11 17:59] – gkazhoya | tutorials:advanced:events [2016/01/28 11:53] – gkazhoya | ||
---|---|---|---|
Line 4: | Line 4: | ||
CRAM has an event protocol that makes the above-mentioned very simple. | CRAM has an event protocol that makes the above-mentioned very simple. | ||
- | The event callback mechanism is based on the notion of hooks of the [[doc/ | + | Code-wise, the event callback mechanism is based on the notion of hooks of the [[doc/ |
- | The protocol itself is defined in the '' | + | The protocol itself is defined in the '' |
- | Basically, there is a generic function called '' | + | In the nutshell, the protocol is pretty trivial: |
- | So, first, load the plan knowledge system and switch to its corresponding Lisp package: | + | So, let' |
<code lisp> | <code lisp> | ||
- | CL-USER> (asdf: | + | CL-USER> (ros-load: |
- | CL-USER> (in-package :plan-knowledge) | + | ;; or ,r-l-s RET cram_plan_occasions_events |
+ | CL-USER> (in-package :cram-plan-occasions-events) | ||
</ | </ | ||
- | Next, we define a custom event class that inherits from '' | + | Next, we define a custom event class that inherits from '' |
<code lisp> | <code lisp> | ||
- | PLAN-KNOWLEDGE> (defclass cat-appeared-event (event) | + | CPOE> (defclass cat-appeared-event (event) |
- | ((color-of-cat | + | ((color-of-cat |
- | :initarg :cat-color :reader cat-color | + | :initarg :cat-color :reader cat-color |
- | :initform (error | + | :initform (error |
- | | + | |
- | | + | |
#< | #< | ||
- | PLAN-KNOWLEDGE> (describe ' | + | CPOE> (describe ' |
- | CRAM-PLAN-KNOWLEDGE:: | + | CRAM-PLAN-OCCASIONS-EVENTS:: |
[symbol] | [symbol] | ||
CAT-APPEARED-EVENT names the standard-class #< | CAT-APPEARED-EVENT names the standard-class #< | ||
Line 46: | Line 47: | ||
<code lisp> | <code lisp> | ||
- | PLAN-KNOWLEDGE> (defmethod on-event ((event cat-appeared-event)) | + | CPOE> (defmethod on-event ((event cat-appeared-event)) |
- | (format t "OMG! I just saw a ~a cat!~%" | + | (format t "OMG! I just saw a ~a cat!~%" |
</ | </ | ||
- | To trigger the event we simply call the '' | + | To trigger the event we simply call the '' |
<code lisp> | <code lisp> | ||
- | PLAN-KNOWLEDGE> (on-event (make-instance ' | + | CPOE> (on-event (make-instance ' |
OMG! I just saw a black cat! | OMG! I just saw a black cat! | ||
+ | (NIL) | ||
</ | </ | ||
Now, imagine that we would like to have multiple handlers for the same event. | Now, imagine that we would like to have multiple handlers for the same event. | ||
- | E.g., in addition to screaming | + | E.g., in addition to screaming |
<code lisp> | <code lisp> | ||
- | PLAN-KNOWLEDGE> (let ((saw-cats 0)) | + | CPOE> (let ((saw-cats 0)) |
- | (defmethod on-event cat-counter ((event cat-appeared-event)) | + | (defmethod on-event cat-counter ((event cat-appeared-event)) |
- | (incf saw-cats) | + | (incf saw-cats) |
- | (format t " | + | (format t " |
+ | saw-cats)) | ||
+ | #< | ||
+ | |||
+ | CPOE> (on-event (make-instance ' | ||
+ | number of cats seen so far: 1 | ||
+ | OMG! I just saw a black cat! | ||
+ | (1 NIL) | ||
+ | |||
+ | CPOE> (on-event (make-instance ' | ||
+ | number of cats seen so far: 2 | ||
+ | OMG! I just saw a black cat! | ||
+ | (2 NIL) | ||
</ | </ | ||
+ | |||
+ | Let's break this down. | ||
+ | |||
+ | * We define a lexical variable '' | ||
+ | * Then we overload our '' |