persistent jframe
[propagator.git] / propagator.org
blobde220928520963c97307b2d6b35eb1ebe4d21ad4
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   (defmacro propagator "Define a new propagator."
22     [name in-cells out-cells & body]
23     `(do
24        (defn ~(with-meta name
25                 (assoc (meta name)
26                   :in-cells in-cells :out-cells out-cells))
27          ~in-cells ~@body)
28        (doseq [cell# ~in-cells] (add-neighbor cell# ~name))
29        ~name))
30   
31   (defn set-cell "Set the value of a cell" [cell value]
32     (send cell (fn [_] value)))
33   
34   (defmacro run-propagator
35     "Run a propagator, first collect the most recent values from all
36   cells associated with the propagator, then evaluate."
37     [propagator]
38     `(let [results# (apply ~propagator (map deref (:in-cells ^#'~propagator)))]
39        (doseq [cell# (:out-cells ^#'~propagator)]
40          (when (not (= @cell# results#))
41            (send cell# (fn [_#] results#))))
42        results#))
43   
44   (defmacro add-neighbor "Add a neighbor to the given cell."
45     [cell neighbor]
46     `(add-watcher
47       ~cell :send
48       (agent nil :validator (fn [_#] (do (future (run-propagator ~neighbor)) true)))
49       (fn [_# _#])))
50 #+end_src
51 * extensions
52 ** graphs of the propagator network
53 #+begin_src clojure
54   (use 'vijual)
55   (import '(javax.swing JFrame JPanel)
56           '(java.awt Color Graphics)
57           '(java.awt.image BufferedImage))
58   
59   (def dim [300 300])
60   
61   (def frame (JFrame.))
62   
63   (defn view "Display a graph generated by vijual" [img]
64     (let [mult 1.5
65           width (* (.getWidth img) mult)
66           height (* (.getHeight img) mult)
67           offset 50
68           panel (doto (proxy [JPanel] []
69                         (paint [g]
70                                (.drawImage g img offset offset nil))))]
71       (doto frame (.add panel) .pack (.setSize (+ offset width) (+ offset height)).show)))
72 #+end_src
74 * usage
75 ** doubling a number -- simplest
76 #+begin_src clojure
77   (cell in 0)
78   (cell out 0)
79   (propagator doubler [in] [out] (* 2 in))
80   ;; then any updates to in
81   (set-cell in 1)
82   ;; will propagate to out
83   @out
84 #+end_src
86 ** square roots -- heron
87 results in errors...
89 #+begin_src clojure
90   (cell guess 1)
91   (cell x 9)
92   (cell done false)
93   (cell margin 0.1)
94   
95   (propagator enough [x guess] [done]
96     (if (< (abs (- (* guess guess) x)) @margin) true false))
97   
98   (propagator heron [x guess] [guess]
99     (if (enough x guess)
100       guess
101       (/ (+ guess (/ x guess)) 2.0)))
102 #+end_src
104 * notes
105 ** look at mutable data stores
106 - http://clojure.org/agents
107 - http://clojure.org/atoms
108 - http://clojure.org/refs
110 most definitely will use agents, functions to set their values are
111 applied to them with send (or send-off if potentially I/O bound), they
112 support validators, and they can be set to run actions (i.e. alert
113 propagators) as part of their update cycle (with add-watcher).
115 ** design to permit distribution across multiple machines
116 it should be possible to wrap these mutable items including
117 - network connections from other machines
118 - hardware (timers, I/O devices, etc...)
120 in cells, so that the propagator system remains "pure"