tiny indentation change
[propagator.git] / propagator.org
blob8787e25041372c5e20ebb84dcff90763721a55dc
1 #+TITLE: Concurrent Propagator System
2 #+OPTIONS: num:nil ^:nil
3 #+LaTeX_CLASS: normal
5 * code
6   :PROPERTIES:
7   :tangle:   src/propagator.clj
8   :END:
9 #+begin_src clojure
10   (ns
11       #^{:author "Eric Schulte",
12          :license "GPLV3",
13          :doc "Simple concurrent propagator system."}
14     propagator
15     (:use clojure.contrib.repl-utils clojure.contrib.math))
16   
17   (defmacro cell "Define a new cell."
18     [name state]
19     `(def ~name (agent ~state)))
20   
21   ;; record keeping for graphing propagators
22   (def prop-graph {})
23   (defmacro remember-prop [name in-cells out-cells]
24     `(def prop-graph
25           (assoc prop-graph
26             (quote ~name)
27             {:in-cells (map (fn [x#] (name x#)) (quote ~in-cells))
28              :out-cells (map (fn [x#] (name x#)) (quote ~out-cells))})))
29   
30   (defmacro propagator "Define a new propagator."
31     [name in-cells out-cells & body]
32     `(do
33        (remember-prop ~name ~in-cells ~out-cells)
34        (defn ~(with-meta name
35                 (assoc (meta name)
36                   :in-cells in-cells :out-cells out-cells))
37          ~in-cells ~@body)
38        (doseq [cell# ~in-cells] (add-neighbor cell# ~name))
39        ~name))
40   
41   (defn set-cell "Set the value of a cell" [cell value]
42     (send cell (fn [_] value)))
43   
44   (defmacro run-propagator
45     "Run a propagator, first collect the most recent values from all
46   cells associated with the propagator, then evaluate."
47     [propagator]
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#))))
52        results#))
53   
54   (defmacro add-neighbor "Add a neighbor to the given cell."
55     [cell neighbor]
56     `(add-watcher
57       ~cell :send
58       (agent nil :validator (fn [_#] (do (future (run-propagator ~neighbor)) true)))
59       (fn [_# _#])))
60 #+end_src
61 * extensions
62 ** graphs of the propagator network
63 draw a graph in a JFrame
64 #+begin_src clojure :tangle src/graphs.clj
65   (in-ns 'propagator)
66   (use 'vijual)
67   (import '(javax.swing JFrame JPanel)
68           '(java.awt Color Graphics)
69           '(java.awt.image BufferedImage))
70   
71   (def dim [300 300])
72   
73   (def frame (JFrame.))
74   
75   (defn view "Display a graph generated by vijual" [img]
76     (let [mult 1.5
77           width (* (.getWidth img) mult)
78           height (* (.getHeight img) mult)
79           offset 50
80           panel (doto (proxy [JPanel] []
81                         (paint [g]
82                                (.drawImage g img offset offset nil))))]
83       (doto frame (.add panel) .pack (.setSize (+ offset width) (+ offset height)).show)))
84   
85   (defn graph-propagators []
86     (vec (set
87           (apply concat
88                  (map (fn [key]
89                         (let [hsh (get prop-graph key)]
90                           (concat
91                            (map #(vec (list % (name key))) (get hsh :in-cells))
92                            (map #(vec (list (name key) %)) (get hsh :out-cells)))))
93                       (keys prop-graph))))))
94   
95   (defn view-network []
96     (view (draw-directed-graph-image (graph-propagators))))
97   
98   (defn clear-network []
99     (def prop-graph {})
100     (def frame (JFrame.)))
101 #+end_src
103 * usage
104 ** doubling a number -- simplest
105 #+begin_src clojure
106   (cell in-c 0)
107   (cell out-c 0)
108   (propagator doubler [in-c] [out-c] (* 2 in))
109   ;; then any updates to in
110   (set-cell in-c 1)
111   ;; will propagate to out
112   @out-c
113 #+end_src
115 ** square roots -- heron
116 #+begin_src clojure
117   (cell guess 1)
118   (cell x 9)
119   (cell done false)
120   (cell margin 0.1)
121   
122   (propagator enough [x guess] [done]
123     (Thread/sleep 1000)
124     (if (< (abs (- (* guess guess) x)) @margin) true false))
125   
126   (propagator heron [x done guess] [guess]
127     (Thread/sleep 1000)
128     (if done
129       guess
130       (/ (+ guess (/ x guess)) 2.0)))
131 #+end_src
133 ** web server
134 Alright, this will use Clojure's [[http://github.com/mmcgrana/ring][Ring]] web server middle-ware.
136 So, I guess the best demo here would be some reading/writing through a
137 MVC setup.
139 The =app= will dispatch the incoming data to input cell by the route
140 at the end of the url, then there can be a couple of output cells
141 which will render different views of the related data.
143 #+begin_src clojure :tangle src/web.clj
144   (load-file "/home/eschulte/src/propagator/src/propagator.clj")
145   (in-ns 'propagator)
146   (use 'ring.adapter.jetty)
147   (use 'clojure.contrib.seq-utils)
148   (import 'java.util.Date 'java.text.SimpleDateFormat)
149   
150   ;; cells
151   (cell names '())
152   (cell input "")
153   
154   (propagator adder [input names] [names]
155               (when (> (count (seq input)) 0)
156                 (set (cons (str input " :1") names))))
157   
158   (defn app [req]
159     (or
160      ;; page to accept input
161      (when (= "/" (:uri req))
162        {:status 200
163         :headers {"Content-Type" "text/html"
164                   "Title" "3000"}
165         :body (apply str
166                      "<form name=\"\" action=\"add\" method=\"get\">"
167                      "Word: <input type=\"type\" name=\"word\" />"
168                      "<input type=\"submit\" value=\"Submit\" />"
169                      "</form>")})
170      ;; dump value into "input" cell
171      (when (re-matches #"/add" (:uri req))
172        (set-cell input (second (re-matches #".+=(.+)" (:query-string req))))
173        {:status 303 :headers {"Location" "../list"}})
174      ;; render the value of the "list" cell
175      (when-let [matched (re-matches #"/list" (:uri req))]
176        {:status  200
177         :headers {"Content-Type" "text/html"}
178         :body    (apply str (flatten (list "<ul>"
179                                            (map #(str "<li>"%"</li>") @names)
180                                            "</ul>"
181                                            "<p><a href=\"/\">another word</a></p>")))})))
182   
183   (run-jetty #'app {:port 3000})
184 #+end_src
186 * notes
187 ** look at mutable data stores
188 - http://clojure.org/agents
189 - http://clojure.org/atoms
190 - http://clojure.org/refs
192 most definitely will use agents, functions to set their values are
193 applied to them with send (or send-off if potentially I/O bound), they
194 support validators, and they can be set to run actions (i.e. alert
195 propagators) as part of their update cycle (with add-watcher).
197 ** design to permit distribution across multiple machines
198 it should be possible to wrap these mutable items including
199 - network connections from other machines
200 - hardware (timers, I/O devices, etc...)
202 in cells, so that the propagator system remains "pure"