multiple improvements based on Ken Wesson's comments
[propagator.git] / src / graphs.clj
blob2a1d723d39ab98a1364e1e7095ed4907dbdb4886
2 (in-ns 'propagator)
3 (use 'vijual)
4 (import '(javax.swing JFrame JPanel)
5         '(java.awt Color Graphics)
6         '(java.awt.image BufferedImage))
8 ;; saving graph information
9 ;; record keeping for graphing propagators
10 (def prop-graph {})
11 (defmacro remember-prop [name in-cells out-cells]
12   `(def prop-graph
13         (assoc prop-graph
14           (quote ~name)
15           {:in-cells (map (fn [x#] (name x#)) (quote ~in-cells))
16            :out-cells (map (fn [x#] (name x#)) (quote ~out-cells))})))
18 (defmacro propagator "Define a new propagator."
19   [name in-cells out-cells & body]
20   `(do
21      (remember-prop ~name ~in-cells ~out-cells)
22      (defn ~(with-meta name
23               (assoc (meta name)
24                 :in-cells in-cells :out-cells out-cells))
25        ~in-cells ~@body)
26      (doseq [cell# ~in-cells] (add-neighbor cell# ~name))
27      ~name))
29 ;; stuff for graphing
30 (def dim [300 300])
32 (def frame (JFrame.))
34 (defn view "Display a graph generated by vijual" [img]
35   (let [mult 1.5
36         width (* (.getWidth img) mult)
37         height (* (.getHeight img) mult)
38         offset 50
39         panel (doto (proxy [JPanel] []
40                       (paint [g]
41                              (.drawImage g img offset offset nil))))]
42     (doto frame (.add panel) .pack (.setSize (+ offset width) (+ offset height)).show)))
44 (defn graph-propagators []
45   (vec (set
46         (apply concat
47                (map (fn [key]
48                       (let [hsh (get prop-graph key)]
49                         (concat
50                          (map #(vec (list % (name key))) (get hsh :in-cells))
51                          (map #(vec (list (name key) %)) (get hsh :out-cells)))))
52                     (keys prop-graph))))))
54 (defn view-network []
55   (view (draw-directed-graph-image (graph-propagators))))
57 (defn clear-network []
58   (def prop-graph {})
59   (def frame (JFrame.)))