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.
138 (use 'ring.adapter.jetty)
139 (use 'clojure.contrib.seq-utils)
140 (import 'java.util.Date 'java.text.SimpleDateFormat)
146 ;; propagator -- adds value of input to names
147 (propagator adder [input names] [names]
148 (when (> (count (seq input)) 0) (set (cons input names))))
153 (binding [*out* out] (println (:uri req)))
155 ;; page to accept input
156 (when (= "/" (:uri req))
158 :headers {"Content-Type" "text/html"}
160 "<form name=\"\" action=\"add\" method=\"get\">"
161 "Word: <input type=\"type\" name=\"word\" />"
162 "<input type=\"submit\" value=\"Submit\" />"
164 ;; dump value into "input" cell
165 (when (re-matches #"/add" (:uri req))
166 (set-cell input (second (re-matches #".+=(.+)" (:query-string req))))
167 {:status 303 :headers {"Location" "../list"}})
168 ;; render the value of the "list" cell
169 (when-let [matched (re-matches #"/list" (:uri req))]
171 :headers {"Content-Type" "text/html"}
172 :body (apply str (flatten (list "<ul>"
173 (map #(str "<li>"%"</li>") @names)
175 "<p><a href=\"/\">another word</a></p>")))}))))
177 (run-jetty app {:port 3000})
181 ** look at mutable data stores
182 - http://clojure.org/agents
183 - http://clojure.org/atoms
184 - http://clojure.org/refs
186 most definitely will use agents, functions to set their values are
187 applied to them with send (or send-off if potentially I/O bound), they
188 support validators, and they can be set to run actions (i.e. alert
189 propagators) as part of their update cycle (with add-watcher).
191 ** design to permit distribution across multiple machines
192 it should be possible to wrap these mutable items including
193 - network connections from other machines
194 - hardware (timers, I/O devices, etc...)
196 in cells, so that the propagator system remains "pure"