Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tutorials:advanced:events [2015/05/11 17:31] – created gkazhoya | tutorials:advanced:events [2018/04/04 09:20] (current) – gkazhoya | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Events in CRAM ====== | ====== Events in CRAM ====== | ||
- | This is a short tutorial to showcase how to trigger and handle custom-defined events. | + | This page migrated |
- | 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/ | + | |
- | The protocol itself is defined in the ``cram_plan_knowledge`` package. That is also the package where new types of events and their handlers should be defined. That's why we will work in that package. | + | |
- | + | ||
- | First, load the plan knowledge system and switch to the corresponding Lisp package: | + | |
- | + | ||
- | <code lisp> | + | |
- | CL-USER> (asdf: | + | |
- | CL-USER> (in-package : | + | |
- | </ | + | |
- | + | ||
- | Next, we define a custom event class that inherits from '' | + | |
- | + | ||
- | <code lisp> | + | |
- | PLAN-KNOWLEDGE> | + | |
- | ((color-of-cat | + | |
- | :initarg :cat-color :reader cat-color | + | |
- | :initform (error | + | |
- | ' | + | |
- | : | + | |
- | #< | + | |
- | + | ||
- | PLAN-KNOWLEDGE> | + | |
- | CRAM-PLAN-KNOWLEDGE:: | + | |
- | [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> | + | |
- | PLAN-KNOWLEDGE> | + | |
- | (format t "OMG! I just saw a ~a cat!~%" | + | |
- | </ | + | |
- | + | ||
- | To trigger the event we simply call the '' | + | |
- | + | ||
- | <code lisp> | + | |
- | PLAN-KNOWLEDGE> | + | |
- | OMG! I just saw a black cat! | + | |
- | </ | + |