Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
tutorials:advanced:events [2016/01/28 11:53] gkazhoyatutorials:advanced:events [2016/04/04 09:07] gkazhoya
Line 85: Line 85:
   * 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]]).   * 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 while specifying ''cram-plan-occasions-events::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 unique qualifiers we can have new handlers for our 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]]).   * Then we overload our ''on-event'' method while specifying ''cram-plan-occasions-events::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 unique qualifiers we can have new handlers for our 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]]).
 +
 +
 +====== 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:load-system "cram_plan_library" :cram-plan-library)
 +;; or ,r-l-s RET cram_plan_library RET RET
 +</code>
 +
 +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:def-goal (plan-lib:achieve (cat-said ?something))
 +           (format t "cat said ~a~%" ?something))
 +(#<FUNCTION (LAMBDA #) {100EF8837B}> NIL)
 +CL-USER> (cpl:top-level (plan-lib:achieve `(cat-said hello)))
 +WARNING: Trying to prove goal (CAT-SAID HELLO) with undefined functor CAT-SAID.
 +cat said HELLO
 +NIL
 +
 +CL-USER> (prolog:def-fact-group cat-facts ()
 +           (prolog:<- (cat-said ?what-he-said)
 +             (prolog:not (prolog:equal ?what-he-said meao))
 +             (prolog:fail))
 +           (prolog:<- (cat-said meao)))
 +CL-USER> (prolog:prolog `(cat-said meao))
 +(NIL . #S(CRAM-UTILITIES::LAZY-CONS-ELEM :GENERATOR #<CLOSURE # {1005C02C6B}>))
 +CL-USER> (prolog:prolog `(cat-said hello))
 +NIL
 +CL-USER> (cpl:top-level (plan-lib:achieve `(cat-said hello)))
 +cat said HELLO
 +NIL
 +CL-USER> (cpl:top-level (plan-lib:achieve `(cat-said meao)))
 +[(ACHIEVE PLAN-LIB) INFO] 1459760764.841: Occasion `(CAT-SAID MEAO)' already achieved.
 +NIL
 +
 +</code>