1 #+TITLE: Concurrent Propagator System
2 #+OPTIONS: num:nil ^:nil
7 :tangle: src/propagator.clj
11 #^{:author "Eric Schulte",
13 :doc "Simple concurrent propagator system."}
15 (:use clojure.contrib.repl-utils clojure.contrib.math))
17 (defmacro cell "Define a new cell."
19 `(def ~name (agent ~state)))
23 (defmacro remember-prop [name in-cells out-cells]
27 {:in-cells (map (fn [x#] (name x#)) (quote ~in-cells))
28 :out-cells (map (fn [x#] (name x#)) (quote ~out-cells))})))
30 (defmacro propagator "Define a new propagator."
31 [name in-cells out-cells & body]
33 (remember-prop ~name ~in-cells ~out-cells)
34 (defn ~(with-meta name
36 :in-cells in-cells :out-cells out-cells))
38 (doseq [cell# ~in-cells] (add-neighbor cell# ~name))
41 (defn set-cell "Set the value of a cell" [cell value]
42 (send cell (fn [_] value)))
44 (defmacro run-propagator
45 "Run a propagator, first collect the most recent values from all
46 cells associated with the propagator, then evaluate."
48 `(let [results# (apply ~propagator (map deref (:in-cells ^#'~propagator)))]
49 (doseq [cell# (:out-cells ^#'~propagator)]
50 (when (not (= @cell# results#))
51 (send cell# (fn [_#] results#))))
54 (defmacro add-neighbor "Add a neighbor to the given cell."
58 (agent nil :validator (fn [_#] (do (future (run-propagator ~neighbor)) true)))
62 ** graphs of the propagator network
63 draw a graph in a JFrame
66 (import '(javax.swing JFrame JPanel)
67 '(java.awt Color Graphics)
68 '(java.awt.image BufferedImage))
74 (defn view "Display a graph generated by vijual" [img]
76 width (* (.getWidth img) mult)
77 height (* (.getHeight img) mult)
79 panel (doto (proxy [JPanel] []
81 (.drawImage g img offset offset nil))))]
82 (doto frame (.add panel) .pack (.setSize (+ offset width) (+ offset height)).show)))
85 pull a graph from a the propagators saved in the =prop-graph= variable
87 (defn graph-propagators []
91 (let [hsh (get prop-graph key)]
93 (map #(vec (list % (name key))) (get hsh :in-cells))
94 (map #(vec (list (name key) %)) (get hsh :out-cells)))))
95 (keys prop-graph))))))
99 ** doubling a number -- simplest
103 (propagator doubler [in-c] [out-c] (* 2 in))
104 ;; then any updates to in
106 ;; will propagate to out
110 ** square roots -- heron
117 (propagator enough [x guess] [done]
118 (if (< (abs (- (* guess guess) x)) @margin) true false))
120 (propagator heron [x done guess] [guess]
123 (/ (+ guess (/ x guess)) 2.0)))
127 ** look at mutable data stores
128 - http://clojure.org/agents
129 - http://clojure.org/atoms
130 - http://clojure.org/refs
132 most definitely will use agents, functions to set their values are
133 applied to them with send (or send-off if potentially I/O bound), they
134 support validators, and they can be set to run actions (i.e. alert
135 propagators) as part of their update cycle (with add-watcher).
137 ** design to permit distribution across multiple machines
138 it should be possible to wrap these mutable items including
139 - network connections from other machines
140 - hardware (timers, I/O devices, etc...)
142 in cells, so that the propagator system remains "pure"