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 Alright, this will use Clojure's [[http://github.com/mmcgrana/ring][Ring]] web server middle-ware.
129 So, I guess the best demo here would be some reading/writing through a
132 The =app= will dispatch the incoming data to input cell by the route
133 at the end of the url, then there can be a couple of output cells
134 which will render different views of the related data.
137 (use 'ring.adapter.jetty)
138 (use 'clojure.contrib.list-utils)
139 (import 'java.util.Date 'java.text.SimpleDateFormat)
145 ;; propagator -- adds value of input to names
146 (propagator adder [input names] [names]
147 (when (count (seq input)) (set (cons input names))))
152 (binding [*out* out] (println (:uri req)))
154 ;; dump value into "input" cell
155 (when-let [matched (re-matches #"/add/(.+)" (:uri req))]
156 (set-cell input (second matched))
157 {:status 303 :headers {"Location" "../list"}})
158 ;; render the value of the "list" cell
159 (when-let [matched (re-matches #"/list" (:uri req))]
161 :headers {"Content-Type" "text/html"}
162 :body (apply str (flatten (list "<ul>" (map #(str "<li>"%"</li>") @names) "</ul>")))}))))
164 (run-jetty app {:port 3000})
168 ** look at mutable data stores
169 - http://clojure.org/agents
170 - http://clojure.org/atoms
171 - http://clojure.org/refs
173 most definitely will use agents, functions to set their values are
174 applied to them with send (or send-off if potentially I/O bound), they
175 support validators, and they can be set to run actions (i.e. alert
176 propagators) as part of their update cycle (with add-watcher).
178 ** design to permit distribution across multiple machines
179 it should be possible to wrap these mutable items including
180 - network connections from other machines
181 - hardware (timers, I/O devices, etc...)
183 in cells, so that the propagator system remains "pure"