Compare commits

..

No commits in common. "02d2a569312a14ad33c9eff95b983715b0a5af38" and "35e86859e80b3cdd6bb1b9fe706f15d3329ec2a7" have entirely different histories.

3 changed files with 57 additions and 92 deletions

View file

@ -5,14 +5,10 @@
(= content nothing))
(defprotocol ICell
(neighbors [this]
"returns all propagators the cell serves as input to")
(content [this]
"returns current content")
(add-content! [this increment]
"adds content to cell if none is present")
(new-neighbor! [this new-neighbor]
"adds a new propagator as a neighbor to the cell"))
(neighbors [this])
(content [this])
(add-content! [this increment])
(new-neighbor! [this new-neighbor]))
(defrecord Cell
[neighbors
@ -38,6 +34,7 @@
(let [old-neighbors (deref (:neighbors this))]
(do (swap! (:neighbors this) conj new-neighbor)
;; add input
;;(alert-propagator new-neighbor)
:ok))))
;; returns an initialized cell

View file

@ -3,6 +3,7 @@
[prop-net.cell :as c]))
;; currently not implementing abort semantics
(defn- alerted-propagators* [] #{})
(defn- cells* [] {})
(defn- propagators-ever-alerted* [] #{})
@ -11,11 +12,11 @@
(defprotocol IPropNet
"A Naive Scalar Propagation Network. Add Cells and Propagators, wire them
together and run. Cells should update based on wiring"
;; -- introspect methods
(get-alerted-propagators [this]
"returns the keys of all propagators to be alerted on next scheduler cycle")
;; introspect methods
(alerted-propagators [this]
"")
(propagators-ever-alerted [this]
"returns the keys of all propagators alerted since last scheduler initialization")
"")
(get-cells [this]
"returns hash-map of all cells in network")
(propagators [this]
@ -29,16 +30,15 @@
;; mutation methods
(add-cell! [this cell-key]
"create a cell associated with a given key")
"")
(add-content-to-cell! [this cell-key content]
"add content to a cell at the given key, alerting the cell's neighboring
propagators, if any")
(add-propagator! [this prop-key func]
"create a propagator from the given function and associate it with the given key")
"")
(add-propagator! [this func]
"")
(add-neighbor-to-cell! [this cell-key prop-key]
"give the cell of the given cellkey a new neighbor of the given propagator key")
"")
(add-output-to-propagator! [this prop-key cell-key]
"wire the output of a given propagator to the set-content! function of a given cell")
"")
;; run methods
(initialize-scheduler! [this]
@ -52,6 +52,8 @@
(defn- deref-get [rec key] (deref (key rec)))
;; TODO re-implementing with reference to PropNet
;; children should not make calls on each other, only return things to do?
(defrecord PropNet
[alerted-propagators
last-value-of-run
@ -59,9 +61,8 @@
all-propagators
propagators-ever-alerted]
IPropNet
;; introspect methods INFO - complete
(get-alerted-propagators [net] (deref-get net :alerted-propagators))
(alerted-propagators [net] (deref-get net :alerted-propagators))
(propagators-ever-alerted [net] (deref-get net :propagators-ever-alerted))
(get-cells [net] (deref-get net :cells))
(propagators [net] (deref-get net :all-propagators))
@ -69,8 +70,8 @@
(cell-key (get-cells net)))
(cell-content [net cell-key] (deref-get (get-cell net cell-key)
:content))
(propagator [net prop-key] (prop-key (propagators net)))
(propagator [net prop-key] (deref-get (propagators net)
prop-key))
;; mutation methods
(add-cell! [net cell-key]
(swap! (:cells net) assoc cell-key (c/make-cell))
@ -89,23 +90,9 @@
{:cell cur-cell})))
(catch Exception e
(prn e)))))
(add-propagator! [this prop-key func]
(swap! (:all-propagators this) assoc prop-key (p/function->propagator func)))
(add-neighbor-to-cell! [this cell-key prop-key]
;; cell only needs to have its neighbors
;; c/new-neighbor! prop-key
(c/new-neighbor! (get-cell this cell-key) prop-key)
;; propagator needs to have the c/content wrapped in a lambda and have that added to its inputs
(p/add-input! (propagator this prop-key) (fn [] (c/content (get-cell this cell-key))))
:ok
)
(add-output-to-propagator! [this prop-key cell-key]
(let [output-setter
;; need to add content from the net in order to alert propagators
;; further down the network
(fn [new-content] (add-content-to-cell! this cell-key new-content))]
(p/add-output! (propagator this prop-key)
output-setter)))
;; (add-propagator! [this prop-key func])
;; (add-neighbor-to-cell! [this cell-key propagator])
;; (add-output-to-propagator! [this prop-key cell])
;; run methods
(last-value-of-run [net] (deref-get net :last-value-of-run))
@ -114,14 +101,9 @@
(reset! (:last-value-of-run this) done)
(reset! (:propagators-ever-alerted this) propagators-ever-alerted*)
:ok)
(simulate! [net]
(run! (fn [prop-key]
(do (swap! (:alerted-propagators net) disj prop-key)
(p/apply! (propagator net prop-key))))
(vec (get-alerted-propagators net))))
;; (simulate! [net])
;; (halt! [net])
)
;; returns a propnet
(defn make-prop-net []
(map->PropNet
@ -142,13 +124,7 @@
(add-cell! this-net :5)
(add-cell! this-net :c*9)
(add-cell! this-net :9)
(add-cell! this-net :32+5)
(add-propagator! this-net :plusser +)
(add-neighbor-to-cell! this-net :32 :plusser)
(add-content-to-cell! this-net :32 32)
(add-neighbor-to-cell! this-net :5 :plusser)
(add-content-to-cell! this-net :5 5)
(add-output-to-propagator! this-net :plusser :32+5)
this-net))
(defn -main []

View file

@ -1,53 +1,45 @@
(ns prop-net.propagator)
(def ^:private no-output [])
(defn- no-output? [output]
(= output no-output))
(ns prop-net.propagator
(:require [prop-net.cell :as c]))
(defprotocol IPropagator
(inputs
[this]
"returns the functions to be called to retrieve the propagators inputs")
(output
[this]
"returns the function to be called to set the result content of output cell")
(add-input!
[this input-lookup]
"add an input function to retrieve the content of a cell")
(add-output!
[this output-set!]
"add an output function to set the content of a cell")
(apply!
[this]
"applies the propagator function to results of all input-lookup
calls and calls the output function with the result"))
(inputs [this])
(output [this])
(add-input! [this input-cell])
(add-output! [this output-cell])
(apply! [this]))
(defrecord Propagator
[input-getters output-setter function]
[inputs output function]
IPropagator
(inputs [this] (deref (:input-getters this)))
(output [this] (deref (:output-setter this)))
(add-input! [this input-func]
(do (swap! (:input-getters this) conj input-func)
:ok))
(add-output! [this output-set!]
(if (no-output? (output this))
(do (reset! (:output-setter this) output-set!)
(inputs [this] (deref (:inputs this)))
(output [this] (deref (:output this)))
(add-input! [this input-cell]
;; this is some java shenanigans prop_net.cell.Cell
(when (not (instance? prop_net.cell.Cell input-cell))
(throw (ex-info "Input must be a cell" {:input input-cell})))
(let [old-inputs (deref (:inputs this))]
(do (swap! (:inputs this) conj input-cell)
:ok)))
(add-output! [this output-cell]
;; TODO for some reason instance? is not evaluating to true here, but types should really be checked
;; from the net itself
;; (when (not (instance? prop_net.cell.Cell output-cell))
;; (throw (ex-info "Output must be a cell" {:output output-cell
;; :type (type output-cell)})))
(if (c/nothing? (deref (:output this)))
(do (reset! (:output this) output-cell)
:ok)
(throw (ex-info "Output cell already present" {:propagator this :conflict output}))))
;; apply will call output with the result of calling :function with all inputs
(throw (ex-info "Output already set" {:propagator this :conflict output}))))
(apply! [this]
(let [input-getters (vec (inputs this))
output-set! (output this)]
(output-set! (apply (:function this)
;; there might be a cleaner way to apply each lookup?
(map (fn [lookup] (lookup))
input-getters))))))
(let [input-cells (deref (:inputs this))
output-cell (deref (:outputs this))]
(c/add-content! output-cell (apply (:function this)
(map c/content input-cells))))))
(defn function->propagator
[func]
(map->Propagator
{:input-getters (atom #{})
:output-setter (atom no-output)
{:inputs (atom #{})
:output (atom c/nothing)
:function func}))