cram-code/cram_highlevel — pull request #7 by fairlight1337 (2014-01-11 12:21:22+00:00)
Title: Refining and documenting policy usage

Added documentation strings to the policy-related functions in `with-policy.lisp`. Fixed a few bugs that wouldn't allow execution of policies, made the whole execution of policies more lightweight (and readable), and added a `named` equivalent of `with-policy`, i.e. `with-named-policy`. This way, policies can now be used like this:

```lisp
(with-named-policy 'policy-name (param-value-1 param-value-2)
(body))
```

Also, exported symbols for policy usage in the package.lisp file.

A complete example of how to use policies is now:

```lisp
(define-policy my-policy (max-num match-num)
(:check (format t "Checking if random number from 0 to ~a equals ~a~%" max-num match-num)
(let ((rnd (random max-num)))
(format t "Got number ~a~%" rnd)
(cond ((eql rnd match-num)
(format t "Match~%")
t)
(t (sleep 1)))))
(:recover (format t "Running recovery mechanisms~%"))
(:clean-up (format t "Running clean-up~%")))
```

```lisp
(top-level
(with-named-policy 'my-policy (10 5)
(loop do (format t "Main loop cycle.~%")
(sleep 2))))
```

The policy checks random numbers of a certain range (`0` to `max-num`) to match a given value (`match-num`). The two values are dynamic parameterizations for the otherwise static policy. When the numbers match, the recovery code of the policy is executed, the body code is stopped, and afterwards, the clean-up code is run.

The important bit here is that the main body code (independent from the policy) runs concurrently besides the policy itself, thus allowing parallel monitoring of an executed task.