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:advanced:events [2016/04/04 09:07] – gkazhoya | tutorials:advanced:events [2018/04/04 09:19] – created gkazhoya | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Events in CRAM ====== | + | This page migrated |
- | + | ||
- | This is a short tutorial to showcase how to trigger and handle custom-defined events. | + | |
- | CRAM has an event protocol that makes the above-mentioned very simple. | + | |
- | + | ||
- | Code-wise, the event callback mechanism is based on the notion of hooks of the [[doc/ | + | |
- | The protocol itself is defined in the '' | + | |
- | + | ||
- | In the nutshell, the protocol is pretty trivial: there is a generic function called '' | + | |
- | + | ||
- | So, let's first load the plan knowledge system and switch to its corresponding Lisp package: | + | |
- | + | ||
- | <code lisp> | + | |
- | CL-USER> (ros-load: | + | |
- | ;; or ,r-l-s RET cram_plan_occasions_events RET RET | + | |
- | CL-USER> (in-package : | + | |
- | </ | + | |
- | + | ||
- | Next, we define a custom event class that inherits from '' | + | |
- | + | ||
- | <code lisp> | + | |
- | CPOE> (defclass cat-appeared-event (event) | + | |
- | ((color-of-cat | + | |
- | :initarg :cat-color :reader cat-color | + | |
- | :initform (error | + | |
- | ' | + | |
- | : | + | |
- | #< | + | |
- | + | ||
- | CPOE> (describe ' | + | |
- | CRAM-PLAN-OCCASIONS-EVENTS:: | + | |
- | [symbol] | + | |
- | CAT-APPEARED-EVENT names the standard-class #< | + | |
- | CAT-APPEARED-EVENT>: | + | |
- | Direct superclasses: | + | |
- | No subclasses. | + | |
- | Not yet finalized. | + | |
- | Direct slots: | + | |
- | COLOR-OF-CAT | + | |
- | Initargs: : | + | |
- | Initform: (ERROR ' | + | |
- | " | + | |
- | Readers: CAT-COLOR | + | |
- | </ | + | |
- | + | ||
- | Now that we have our custom event type we define an event handler for it: | + | |
- | + | ||
- | <code lisp> | + | |
- | CPOE> (defmethod on-event ((event cat-appeared-event)) | + | |
- | (format t "OMG! I just saw a ~a cat!~%" | + | |
- | </ | + | |
- | + | ||
- | To trigger the event we simply call the '' | + | |
- | + | ||
- | <code lisp> | + | |
- | CPOE> (on-event (make-instance ' | + | |
- | OMG! I just saw a black cat! | + | |
- | (NIL) | + | |
- | </ | + | |
- | + | ||
- | Now, imagine that we would like to have multiple handlers for the same event. | + | |
- | E.g., in addition to screaming excitedly each time we see a cat, we would also like to count the number of cats seen so far: | + | |
- | + | ||
- | <code lisp> | + | |
- | CPOE> (let ((saw-cats 0)) | + | |
- | (defmethod on-event cat-counter ((event cat-appeared-event)) | + | |
- | (incf saw-cats) | + | |
- | (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 '' | + | |
- | + | ||
- | + | ||
- | ====== Occasions and goals in CRAM ====== | + | |
- | + | ||
- | Here is a short demo of how to define your own goals. | + | |
- | First, we need to load the CRAM plan library into our REPL: | + | |
- | + | ||
- | <code lisp> | + | |
- | CL-USER> (ros-load: | + | |
- | ;; or ,r-l-s RET cram_plan_library RET RET | + | |
- | </ | + | |
- | + | ||
- | Next we need to define how we achieve our custom occasion, and what the occasion mean, i.e. when does it hold: | + | |
- | <code lisp> | + | |
- | CL-USER> (cpl: | + | |
- | | + | |
- | (#< | + | |
- | CL-USER> (cpl: | + | |
- | WARNING: Trying to prove goal (CAT-SAID HELLO) with undefined functor CAT-SAID. | + | |
- | cat said HELLO | + | |
- | NIL | + | |
- | + | ||
- | CL-USER> (prolog: | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | CL-USER> (prolog: | + | |
- | (NIL . # | + | |
- | CL-USER> (prolog: | + | |
- | NIL | + | |
- | CL-USER> (cpl: | + | |
- | cat said HELLO | + | |
- | NIL | + | |
- | CL-USER> (cpl: | + | |
- | [(ACHIEVE PLAN-LIB) INFO] 1459760764.841: | + | |
- | NIL | + | |
- | + | ||
- | </ | + |