(def atom1 (atom 0)) (def agent1 (agent 0)) (def ref1 (ref 0))
(swap! atom1 inc) (prn @atom1) ;->1 (send agent1 inc) (prn @agent1) ;->1 (dosync (alter ref1 inc)) (prn @ref1) ;->1
(reset! atom1 0) (send agent1 (fn [_] 0)) (dosync (ref-set ref1 0))
(defn atom1-change [] (prn "atom1-change") (swap! atom1 inc) (prn @atom1)) (defn agent1-change [] (prn "agent1-change") (send agent1 inc) (prn @agent1)) (defn ref1-change [] (prn "ref1-change") (dosync (alter ref1 inc)) (prn @ref1))
(atom1-change) ;"atom1-change" ;1 (prn @atom1) ;1 (agent1-change) ;"agent1-change" ;0 (prn @agent1) ;1 (ref1-change) ;"ref1-change" ;1 (prn @ref1) ;1
(defn sleep [ms] (. System.Threading.Thread (Sleep ms)))
(defn agent1-thread[x] (sleep 1000) (send *agent* agent1-thread) (prn x) (inc x)) (send agent1 agent1-thread)
(defn agent1-thread[x] (send *agent* agent1-thread) (sleep 1000) (prn x) (inc x)) (send agent1 agent1-thread)
(reset! atom1 atom1) (dosync (ref-set ref1 ref1)) (send agent1 (fn [_] agent1))
(def timer (agent nil) ) (defn on-timer [_] (sleep 1000) (send *agent* on-timer) (prn "tic-tac")) (send timer on-timer)
(def timer (agent nil) ) (defn on-timer [_] (sleep 1000) (prn "tic-tac") (send *agent* on-timer)) (send timer on-timer)
(def agent1 (agent nil)) (def agent2 (agent nil)) (def timer (agent nil)) (def atom1 (atom 0)) (def atom2 (atom 0)) (defn atoms-change-thread1 [_] (reset! atom1 1) (sleep (rand-int 90)) (reset! atom2 2) (sleep (rand-int 90)) (send *agent* atoms-change-thread1) nil) (defn atoms-change-thread2 [_] (reset! atom1 3) (sleep (rand-int 90)) (reset! atom2 4) (sleep (rand-int 90)) (send *agent* atoms-change-thread2) nil) (defn on-timer [_] (prn @atom1 @atom2) (sleep 1000) (send *agent* on-timer) nil) (send agent1 atoms-change-thread1) (send agent2 atoms-change-thread2) (send timer on-timer)
(def agent1 (agent nil)) (def agent2 (agent nil)) (def timer (agent nil)) (def ref1 (ref 0)) (def ref2 (ref 0)) (defn refs-change-thread1 [_] (dosync (ref-set ref1 1) (sleep (rand-int 90)) (ref-set ref2 2) (sleep (rand-int 90))) (send *agent* refs-change-thread1) nil) (defn refs-change-thread2 [_] (dosync (ref-set ref1 3) (sleep (rand-int 90)) (ref-set ref2 4) (sleep (rand-int 90))) (send *agent* refs-change-thread2) nil) (defn on-timer [_] (prn @ref1 @ref2) (sleep 1000) (send *agent* on-timer) nil) (send agent1 refs-change-thread1) (send agent2 refs-change-thread2) (send timer on-timer)
(def agent1 (agent nil)) (def agent2 (agent nil)) (def timer (agent nil)) (def agent3 (agent 0)) (def agent4 (agent 0)) (defn agents-change-thread1 [_] (send agent3 (fn [_] 1)) (sleep (rand-int 90)) (send agent4 (fn [_] 2)) (sleep (rand-int 90)) (send *agent* agents-change-thread1) nil) (defn agents-change-thread2 [_] (send agent3 (fn [_] 3)) (sleep (rand-int 90)) (send agent4 (fn [_] 4)) (sleep (rand-int 90)) (send *agent* agents-change-thread2) nil) (defn on-timer [_] (prn @agent3 @agent4) (sleep 1000) (send *agent* on-timer) nil) (send agent1 agents-change-thread1) (send agent2 agents-change-thread2) (send timer on-timer)
(def v [1 1 0 0 0]) (def world (vec (map (fn [x] (ref x)) v))) (defn place [x] (world x)) (def agent1 (agent 0)) (def agent2 (agent 1)) (def agent-show-world (agent nil)) (defn agent-change [x] (let [old (place x) new-coord (rand-int (count world)) new (place new-coord)] (sleep (rand-int 50)) (if (= @old 1) (do (send *agent* agent-change) (dosync (if (= @new 0) (do (ref-set old 0) (ref-set new 1) new-coord) x))) (prn "agent " *agent* "is out")))) (defn show-world [_] (sleep 1000) (send *agent* show-world) (prn (map (fn [x] (deref x)) world))) (send agent-show-world show-world) (send agent1 agent-change) (send agent2 agent-change)
(dosync (if (= @new 0) (do (ref-set old 0) (ref-set new 1) new-coord) x)))
(defn agent-change [x] (let [old (place x) new-coord (rand-int (count v)) new (place new-coord)] (sleep (rand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 1 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 2 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 2 2 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 5 5 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 4 5 1 0 0 0 0 0 0 0 0 1 1 1 0 1 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 5 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 5 4 1 1 0 0 0 0 0 0 0 0 1 0 1 0 1 5 5 5 1 5 1 0 1 1 0 0 0 0 0 0 0 0 0 1 5 5 5 5 5 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 5 4 5 5 5 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 5 4 4 5 5 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 5 5 5 5 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0))
(defn render-bitmap [bitmap bit color] (let [bit-pos (positions #{bit} bitmap) rendered-bitmap (Bitmap. bitmaps-dim bitmaps-dim)] (doseq [b bit-pos] (let [dy (quot b bitmaps-dim) dx (rem b bitmaps-dim)] (.SetPixel rendered-bitmap dx dy color))) rendered-bitmap))
(def current-wins (atom nil)) (def win-app (agent nil)) (def winforms-app-inited? (atom false)) (def chart (atom nil)) (def series (atom nil)) (defn get-series [series-name] (first (filter (fn [x] (if (= (. x Name) series-name) true false)) @series))) (defn add-xy [series-name xy] (let [series (get-series series-name)] (when series (.AddXY (.Points series) xy) (when (> (.Count (.Points series)) 500) (.RemoveAt (.Points series) 0))))) (defn create-series [chart] (let [series1 (. chart Series)] (.Add series1 "herb") (.Add series1 "sugar") (.Add series1 "anthill-sugar") (.Add series1 "aphises") (doseq [s series1] (doto s (.set_ChartType SeriesChartType/Spline) (.set_IsVisibleInLegend true) )) (reset! series series1) (doto (get-series "herb") (.set_Color Color/Green) (.set_LegendText "Herb") ) (doto (get-series "sugar") (.set_Color Color/White) (.set_LegendText "Free sugar") ) (doto (get-series "anthill-sugar") (.set_Color (Color/FromArgb 255 115 61 0)) (.set_LegendText "Anthill sugar") ) (doto (get-series "aphises") (.set_Color (ControlPaint/Light Color/Green)) (.set_LegendText "Aphises") ))) (defn chart-update[chart] (add-xy "anthill-sugar" @world-time (anthill-sugar-calc)) (add-xy "herb" @world-time (herb-calc)) (add-xy "sugar" @world-time (free-sugar-calc)) (add-xy "aphises" @world-time (count @aphises)) (let [chart-areas (. chart ChartAreas) chart-area (first chart-areas) axis-x (. chart-area AxisX)] (doto axis-x (.set_Minimum (if (> @world-time 500) (- @world-time 500) 0)) (.set_Maximum (if (> @world-time 500) @world-time 500))))) (defn create-form [] (let [form (Form.) panel (Panel.) animation-timer (Timer.) world-timer (Timer.) chart1 (Chart.) series1 (. chart1 Series)] (doto chart1 (.set_Name "chart1") (.set_Location (new Point size 0)) (.set_Size (Size. size size)) (.set_BackColor (ControlPaint/Light bgcolor))) (.Add (. chart1 ChartAreas) "MainChartArea") (.Add (. chart1 Legends) "Legend") (doto (first (. chart1 ChartAreas)) (.set_BackColor bgcolor)) (doto (first (. chart1 Legends)) (.set_BackColor bgcolor)) (create-series chart1) (reset! chart chart1) (chart-update chart1) (let [chart-areas (. chart1 ChartAreas) chart-area (first chart-areas) axis-x (. chart-area AxisX) axis-y (. chart-area AxisY)] (doto axis-x (.set_IsStartedFromZero true)) (doto axis-y (.set_IsStartedFromZero true))) (doto panel (.set_Location (new Point 0 0)) (.set_Name "panel1") (.set_Size (Size. size size)) (.add_Click (gen-delegate EventHandler [sender args] (when (= (.Button args) MouseButtons/Right) (swap! show-lens? (fn [x] (not x)))) (when (= (.Button args) MouseButtons/Left) (let [mouse-x (@mouse-pos 0) mouse-y (@mouse-pos 1) x (/ mouse-x scale) y (/ mouse-y scale) p (place [xy])] (prn [xy] @p) (.Focus panel))))) (.add_MouseMove (gen-delegate MouseEventHandler [sender args] (reset! mouse-pos [ (* (quot (.X args) scale) scale) (* (quot (.Y args) scale) scale)]))) (.add_MouseWheel (gen-delegate MouseEventHandler [sender args] (let [f (fn [x] (let [new-sleep (+ x (* 50 (/ (.Delta args) 120)))] (if (> new-sleep 0) new-sleep 0)))] (swap! ant-sleep-ms f) (swap! ladybug-sleep-ms f) (swap! aphis-sleep-ms f) (prn @ant-sleep-ms))))) (doto animation-timer (.set_Interval animation-sleep-ms) (.set_Enabled true) (.add_Tick (gen-delegate EventHandler [sender args] (do (when @buf-graph (.Render (@buf-graph 0) (@buf-graph 1))) (reset! rectangles-in-cells []) (reset! rendered-bitmaps []) (let [v (vec (for [x (range dim) y (range dim)] @(place [xy])))] (dorun (for [x (range dim) y (range dim)] (render-place (v (+ (* x dim) y)) xy))) (reset! buf-graph (render panel)) (when @show-lens? (reset! buf-graph (render-lens)))))))) (doto world-timer (.set_Interval 5000) (.set_Enabled true) (.add_Tick (gen-delegate EventHandler [sender args] (swap! world-time inc) (chart-update chart1)))) (doto (.Controls form) (.Add panel) (.Add chart1)) (doto form (.set_ClientSize (Size. (* size 2) size)) (.set_Text "Super Ants")) form)) (defn init-winforms-app [] (when-not @winforms-app-inited? (Application/EnableVisualStyles) (Application/SetCompatibleTextRenderingDefault false) (reset! winforms-app-inited? true))) (defn start-gui [x] (init-winforms-app) (reset! current-wins (create-form)) (Application/Run @current-wins))
Source: https://habr.com/ru/post/256017/
All Articles