Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorials:advanced:task_trees [2015/06/09 12:42] – Added content about with-transformative-failure-handling. mpomarlantutorials:advanced:task_trees [2020/09/17 14:09] (current) – [Plan transformations: task trees, code replacement, and serialization] gkazhoya
Line 1: Line 1:
 ====== Plan transformations: task trees, code replacement, and serialization ====== ====== Plan transformations: task trees, code replacement, and serialization ======
  
-**Description:** this tutorial is aimed at CRAM developers, and its purpose is to present an in-development aspect of CRAM; expect content to change. It contains an intro to task trees, a data structure used by CRAM functions (note: this has existed in CRAM for a while and will not change). It also describes code replacement (also long existing), failure handling through code replacement (new), and serialization of task trees and functions (new).+**Disclaimer:** the most up to date overview of plan transformations you can find in README.md of ''cram_plan_transformation'' package (''CRAM_REPO/cram_3d_world/cram_plan_transformation/README.md''). The tutorial below is out of date and has not be maintained, unfortunately, but you might still find some useful explanations there. Otherwise, stick to the README. 
 + 
 +**Description:** this tutorial is aimed at CRAM developers, and its purpose is to present an in-development aspect of CRAM. It is less a proper tutorial and more documentation for work in progress. Expect content to change. It contains an intro to task trees, a data structure used by CRAM functions (note: this has existed in CRAM for a while and will not change). It also describes code replacement (also long existing), failure handling through code replacement (new), and serialization of task trees and functions (new).
  
 ===== CRAM task trees ===== ===== CRAM task trees =====
Line 277: Line 279:
  
 If you inspect the task tree for ptr-fail-handle now, you will see there is no :CALL 2 node. Unlike with-failure-handling, with-transformative-failure-handling will only store the last attempt. If you inspect the task tree for ptr-fail-handle now, you will see there is no :CALL 2 node. Unlike with-failure-handling, with-transformative-failure-handling will only store the last attempt.
 +
 +===== Using projection runs to guide plan transformation =====
 +
 +Besides reacting to runtime conditions, another possible use of plan transformation is to try out several plan structures "in simulation" (what CRAM refers to as inside a projection environment), select the best plan structure, then rerun it "for real", outside projection. To assist in writing such a plan, the with-transformative-tryouts macro is defined in and exported by the cram-projection package. Below is an example code snippet showing a very simple use of the macro, which you can run in the roslisp repl.
 +
 +<code lisp>
 +(swank:operate-on-system-for-emacs "cram-projection" (quote load-op))
 +(in-package :cpl-impl)
 +(cram-projection:define-projection-environment example-env)
 +
 +(def-top-level-cram-function tryprj () 
 +  (cram-projection:with-transformative-tryouts example-env 
 +    (progn 
 +      (format T "Got result ~A~%Now retrying for real.~%" (cram-projection:projection-ended/get-projection-outcome *projection-signal-data*)) 
 +      (setf *in-projection-environment* nil) 
 +      (retry)) 
 +    (format T "Working in environment ~A~%" (if *in-projection-environment*
 +                                                'example-env
 +                                                nil))
 +    (format T "Doing stuff.~%")))
 +    
 +(tryprj)
 +</code>
 +
 +The result from tryprj should look like this:
 +
 +<code lisp>
 +Working in environment EXAMPLE-ENV
 +Doing stuff.
 +Got result #S(CRAM-PROJECTION::PROJECTION-ENVIRONMENT-RESULT
 +              :NAME EXAMPLE-ENV
 +              :RESULT (NIL)
 +              :ENVIRONMENT ((*EPISODE-KNOWLEDGE*
 +                             . #<LIVE-EPISODE-KNOWLEDGE {1002B609F3}>)))
 +Now retrying for real.
 +Working in environment NIL
 +Doing stuff.
 +NIL
 +</code>
 +
 +We'll now look at what happens and explain the code inside tryprj.
 +
 +The parameters for cram-projection:with-transformative-tryouts are 
 +
 +  * a PROJECTION-ENVIRONMENT-NAME (same semantics of the parameter as with the with-projection-environment macro: the parameter will be converted to a symbol so you must not write 'example-env; instead, use example-env).
 +  * a TRANSFORMATION-CLAUSE in which the plan should analyze the results from projection, decide whether/how to transform BODY, decide whether to run in projection or not. Some notes on TRANSFORMATION-CLAUSE:
 +    * will typically be a block of code inside a PROGN.
 +    * will typically end in RETRY. Ending in RETURN is also possible. In that case, TRANSFORMATION-CLAUSE will not call BODY again, but instead the macro will return NIL (or whatever value is in the RETURN S-exp) to its caller.
 +    * TRANSFORMATION-CLAUSE has access to the results of projection by (cram-projection:projection-ended/get-projection-outcome cpl-impl:*projection-signal-data*). In the example above we only print the results.
 +    * the path of BODY inside the task tree is accessible to TRANSFORMATION-CLAUSE via cpl-impl:*retry-path*. 
 +    * cpl-impl:*in-projection-environment* is a flag that TRANSFORMATION-CLAUSE can set to control whether the next run of BODY will be inside the projection environment or not. At the start of the macro, cpl-impl:*in-projection-environment* is set to T (BODY will run in projection). In the example, we set it to NIL to also have a run outside the projection environment.
 +    * any special variables acting as parameters for BODY can be changed in TRANSFORMATION-CLAUSE. In the example, we use cpl-impl:*in-projection-environment* as such a parameter.
 +    * if plan transformations should be enabled for BODY, it must contain at least one CRAM-FUNCTION.
 +  * BODY, a list of S-expressions that should be run. The macro return value is the return value of BODY.
 +
 +Just like with-transformative-failure-handling, with-transformative-tryouts will store only the latest run of BODY in the task tree.
 +
 +===== Changing parameters with plan transformation =====
 +
 +Remember one of the previous laws of the task tree: 
 +
 +//When a task tree node is run, the value of PARAMETERS in either CODE or (car CODE-REPLACEMENTS) is set to the value of the parameters at the moment of the function call.//
 +
 +This means that, typically, you can't change parameters through plan transformation as previously implemented in CRAM. However, it's useful to distinguish two types of parameters for a cram-function:
 +
 +  * "run-time" parameters. These take their values from the actual runtime context that a cram-function is called in. This is the only behavior available in the main CRAM code branch.
 +  * "compile-time" parameters. These are fixed when the code is generated, either at compile time //or after plan transformation//. You can think of them as parameters to tweak how a function runs, but they can also be adjusted to reflect the runtime context. Their main use is to allow plan transformation to affect them.
 +
 +To support "compile-time" parameters, a new construct was just added to cpl, def-ptr-cram-function, and its workings are explained below. Let's first load a sample code into a new repl window:
 +
 +<code lisp>
 +(swank:operate-on-system-for-emacs "cram-language" (quote load-op))
 +
 +(defparameter par-1 1) ;; just a couple variables to give some 'runtime' context for our cram functions to work in
 +(defparameter par-2 1)
 +
 +(cpl-impl:def-ptr-cram-function example-ptr (P &rest args)
 +  (format T "Inside example-ptr.~%")
 +  (format T "Received arguments ~a~%" args)
 +  (format T "PTR parameter is (~a)~%Will now fail, so as to trigger plan transformation.~%" P)
 +  (cpl:fail 'cpl-impl:plan-failure))
 +
 +(defun compatible-ptr (P &rest args)
 +  (format T "Inside compatible-ptr.~%")
 +  (format T "Received arguments ~a~%" args)
 +  (format T "PTR parameter is (~a)~%Done!~%" P))
 +           
 +(cpl-impl:def-top-level-cram-function try-ptr ()
 +  (cpl-impl:with-transformative-failure-handling
 +    ((cpl-impl:plan-failure (f)
 +      (let ((code-path (cpl-impl::plan-failure/get-code-path f)))
 +        (format t "Will replace code now (and switch the ptr-parameter) ...~%")
 +        (cpl-impl:replace-task-code '(compatible-ptr args) #'compatible-ptr code-path :ptr-parameter "Oranges")
 +        (cpl-impl:retry))))
 +    (setf par-1 (+ par-1 par-2))
 +    (setf par-2 (- par-1 par-2))
 +    (example-ptr "Apples" par-1 par-2 0)))
 +</code>
 +
 +Of these, example-ptr is the "star" of the show. Notice that it's been defined with def-ptr-cram-function, and what that tells CRAM is that the first argument in the supplied lambda list (in this case, P) is supposed to be a "compile-time", or plan-transformation accessible, parameter.
 +
 +ptr-cram-functions are a bit peculiar. When you call them the first time they behave like regular cram functions, and take their parameters from the ones you supplied. When you call them again (meaning, when there's a node in the task tree corresponding to the plan location you're calling them from), they ignore the first parameter in the lambda list you supply them with and replace it with the ptr-parameter stored in the task tree node. ptr-cram-functions do all this automatically; you don't need to write anything different when using them. In the example above, the ptr-cram-function uses the P parameter as if it were a usual parameter.
 +
 +To actually change the compile time parameter in a node, notice the extra &key parameter in replace-task-code:
 +
 +<code lisp>
 +(cpl-impl:replace-task-code '(compatible-ptr args) #'compatible-ptr code-path :ptr-parameter "Oranges")
 +</code>
 +
 +By default the :ptr-parameter to cpl-impl:replace-task-code is set to the previously effective value of ptr-parameter that is stored in the task tree node. This either the ptr-parameter in the code slot of the node (if there are no code replacements), or the ptr-parameter in the car object in the code-replacements list of the node. When a task tree node is created and a ptr-parameter is not specified, the default is nil.
 +
 +In our example here, the compile-time parameter is a string. It can however be anything, including a struct or a list, for when you need to pass on several objects through plan transformation.
 +
 +Let's run the top-level function and see what happens (trace below shows a copy-paste from the repl window) ...
 +
 +<code lisp>
 +CL-USER> (try-ptr)
 +Inside example-ptr.
 +Received arguments (2 1 0)
 +PTR parameter is (Apples)
 +Will now fail, so as to trigger plan transformation.
 +Will replace code now (and switch the ptr-parameter) ...
 +Inside compatible-ptr.
 +Received arguments (3 2 0)
 +PTR parameter is (Oranges)
 +Done!
 +NIL
 +CL-USER> (try-ptr)
 +Inside compatible-ptr.
 +Received arguments (5 3 0)
 +PTR parameter is (Oranges)
 +Done!
 +NIL
 +CL-USER> (try-ptr)
 +Inside compatible-ptr.
 +Received arguments (8 5 0)
 +PTR parameter is (Oranges)
 +Done!
 +NIL
 +</code>
 +
 +Notice how the transformed plan persists between plan runs, including the compile-time parameter, even though the run-time parameters change as expected.
  
 ===== Task tree serialization ===== ===== Task tree serialization =====
  
 +Task trees are, as we've seen, not just a log of values but effectively a program themselves, because they track whether code replacements exists, and which functions should be used as replacements. It may be useful at times to be able to store task trees persistently, including code replacements, and we will look at de/serialization next. The assumption is you have just run the example above in your REPL, and you therefore have a task tree for the ptr-fail-handle function.
 +
 +To store the task tree, use the following:
 +
 +<code lisp>
 +(swank:operate-on-system-for-emacs "cram-execution-trace" (quote load-op))
 +(store-tree (get-top-level-task-tree 'ptr-fail-handle) "~/example.dat")
 +</code>
 +
 +It's more interesting if we restore from an empty REPL, so close your REPL and rerun it. You will need to load the cram basic packages cram_language and cram_execution_trace:
 +
 +<code lisp>
 +(swank:operate-on-system-for-emacs "cram-language" (quote load-op))
 +(swank:operate-on-system-for-emacs "cram-execution-trace" (quote load-op))
 +(in-package :cpl-impl)
 +</code>
 +
 +We'll restore the task tree in a moment, but before we do we need to reload the previous code:
 +
 +<code lisp>
 +(defvar *stop-infinity* 0)
 +(setf *stop-infinity* 0)
 +
 +(remove-top-level-task-tree 'ptr-fail-handle)
 +
 +(def-cram-function failure-causing () 
 +  (if (> 1 *stop-infinity*) 
 +      (progn
 +        (setf *stop-infinity* (+ 1 *stop-infinity*))
 +        (format T "FAILURE-CAUSING: Will attempt to send a fail signal from ~A.~%" *current-path*) 
 +        (fail 'plan-failure)))
 +      (format T "FAILURE-CAUSING: Why are we back here?~%"))
 +      
 +(def-cram-function some-function ()
 +  (failure-causing))
 +
 +(defun plan-repaired () 
 +  (format T "PLAN-REPAIRED: Hi there. This was inserted via code replacement.~%"))
 +
 +(def-top-level-cram-function ptr-fail-handle () 
 +  (with-transformative-failure-handling
 +    ((plan-failure (f)
 +       (let ((code-path (plan-failure/get-code-path f)))
 +         (format T "Code path of failure: ~A.~% Current path: ~A.~%" code-path *current-path*)
 +         (replace-task-code '(plan-repaired) #'plan-repaired code-path)
 +         (retry))))
 +    (progn 
 +      (some-function))))
 +</code>
 +
 +This is because we cannot store function executable code with cet:store-tree (for now, at least). Rather, what we can re/store are function names. Typically, you will have your plans in source packages that you load anyway; restoring the task tree is simply there to reload any code replacements that were found useful during execution. Notice that in the code we loaded the top-level function calls some-function, which in turn calls failure-causing.
 +
 +Let's then restore the task tree and rerun the top-level function.
 +
 +<code lisp>
 +(setf (gethash 'ptr-fail-handle *top-level-task-trees*) (cet:restore-tree "~/example.dat"))
 +(ptr-fail-handle)
 +</code>
 +
 +The response shows that the plan-repaired function gets called instead of failure-causing.
 +
 +The following point is very important. In order for a replacement to be restorable, it must be that the function is named. This is ok:
 +
 +<code lisp>
 +(replace-code-task '(some sexp) #'some-named-function some-code-path)
 +</code>
 +
 +This is ok as far as code replacement goes, but is not de/serializable (yet), which means it cannot be restored:
 +
 +<code lisp>
 +(replace-code-task '(some sexp) (lambda (some-args) (some-named-function some-args)) some-code-path)
 +</code>
  
 +If you use the second version, then the restored tree will just use the default function code, rather than any replacement (because the restored replacement is nil).