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 19:14] – gkazhoya | tutorials:advanced:events [2015/05/11 19:23] – gkazhoya |
---|
<code lisp> | <code lisp> |
CL-USER> (asdf:load-system :cram-plan-knowledge) ; or ,r-l-s RET cram_plan_knowledge RET RET | CL-USER> (asdf:load-system :cram-plan-knowledge) ; or ,r-l-s RET cram_plan_knowledge RET RET |
CL-USER> (in-package :plan-knowledge) | CL-USER> (in-package :cram-plan-knowledge) |
</code> | </code> |
| |
</code> | </code> |
| |
To trigger the event we simply call the ''cram-plan-knowledge:on-event'' function: | To trigger the event we simply call the ''cram-plan-knowledge:on-event'' method: |
| |
<code lisp> | <code lisp> |
Let's break this down. | Let's break this down. |
| |
* We define a lexical variable ''saw-cats'' which will be our counter (if you don't quite understand how one can define a new function within the ''let'' block read up on [[http://www.gigamonkeys.com/book/variables.html#lexical-variables-and-closures|closures]]). | * We define a lexical variable ''saw-cats'' which will be our counter (if ''saw-cats'' confuses you read up on [[http://www.gigamonkeys.com/book/variables.html#lexical-variables-and-closures|closures]]). |
* Then we overload our ''on-event'' method and specify ''cram-plan-knowledge::cat-counter'' as a method combination qualifier. The default method combination of generic function ''on-event'' is ''cram-utilities:hooks'' which simply combines the results of __all__ the suitable methods into a list. That is clearly illustrated in the result of the last ''on-event'' call in the code snippet above: ''(2 NIL)'', where ''2'' is the result of the ''cat-counter'' method and ''NIL'' results from the ''format'' statement of the other method. Combining the results of __all__ the suitable methods means that the qualifier itself (in our case ''cat-counter'') is of no importance. So, as long as we can come up with new qualifiers we can have new handlers for a particular event. | * Then we overload our ''on-event'' method while specifying ''cram-plan-knowledge::cat-counter'' as a method combination qualifier. The default method combination of generic function ''on-event'' is ''cram-utilities:hooks'' which simply combines the results of all the suitable methods into a list. That is clearly illustrated in the result of the last ''on-event'' call in the code snippet above: ''(2 NIL)'', where ''2'' is the result of the method qualified with ''cat-counter'' and ''NIL'' results from the ''format'' statement of the other method. Combining the results of all the suitable methods means that the qualifier itself (in our case ''cat-counter'') is of no importance. So, as long as we can come up with new qualifiers we can have new handlers for a particular event (if this paragraph is hard to understand look into the definition of ''cram-utilities:define-hook'' and read up more on [[http://www.lispworks.com/documentation/HyperSpec/Body/m_defi_4.htm|method combinations]]). |