Skip to content

Commit

Permalink
Fixed external file load for data model.
Browse files Browse the repository at this point in the history
  • Loading branch information
awkay committed Oct 15, 2024
1 parent 8f02518 commit 1cc45da
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
(if (map? data)
(do
(log/trace "Loaded" data "into context" state-id)
(vswap! vwmem ::data-model assoc state-id data))
(vswap! vwmem update ::data-model assoc state-id data))
(log/error "Unable to use loaded data from" src "because it is not a map.")))
(catch #?(:clj Throwable :cljs :default) e
(log/error e "Unable to load data from" src)))
Expand Down Expand Up @@ -151,12 +151,11 @@
sp/DataModel
(load-data [provider {::sc/keys [vwmem] :as env} src]
#?(:clj (try
(let [data (edn/read-string (slurp src))
state-id (or (env/context-element-id env) :ROOT)]
(let [data (edn/read-string (slurp src))]
(if (map? data)
(do
(log/trace "Loaded" data "into context" state-id)
(vswap! vwmem ::data-model assoc state-id data))
(log/trace "Loaded" data "into root data of model")
(vswap! vwmem update ::data-model merge data))
(log/error "Unable to use loaded data from" src "because it is not a map.")))
(catch #?(:clj Throwable :cljs :default) e
(log/error e "Unable to load data from" src)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,35 @@
[com.fulcrologic.statecharts.chart :as chart]
[com.fulcrologic.statecharts.data-model.operations :as ops]
[com.fulcrologic.statecharts.data-model.working-memory-data-model :as wmdm]
[com.fulcrologic.statecharts.elements :refer [state]]
[com.fulcrologic.statecharts.elements :as ele :refer [state]]
[com.fulcrologic.statecharts.protocols :as sp]
[fulcro-spec.core :refer [=> assertions component specification]]))

(specification "Working memory model"
#?(:clj
(component "initial load"
(let [machine (chart/statechart {}
(ele/data-model {:expr (fn [& _]
{:a 1})})
(state {:id :A}
(state {:id :A/a})))
DM (wmdm/new-model)
vwmem (volatile! {})
mock-env {::sc/statechart machine
::sc/data-model DM
::sc/execution-model (reify sp/ExecutionModel)
::sc/event-queue (reify sp/EventQueue)
::sc/vwmem vwmem}
context (fn [c] (assoc mock-env ::sc/context-element-id c))
f (java.io.File/createTempFile "foo" "data")]

(spit f "{:a 1}")

(sp/load-data DM (context :A) (.getAbsolutePath f))

(assertions
"Places values into the correct context in working memory"
(::wmdm/data-model @vwmem) => {:A {:a 1}}))))
(let [machine (chart/statechart {}
(state {:id :A}
(state {:id :A/a})))
Expand Down Expand Up @@ -65,6 +89,30 @@
(sp/get-at DM (context :ROOT) :z) => nil))))

(specification "Flat Working memory model" :focus
#?(:clj
(component "initial load"
(let [machine (chart/statechart {}
(ele/data-model {:expr (fn [& _]
{:a 1})})
(state {:id :A}
(state {:id :A/a})))
DM (wmdm/new-flat-model)
vwmem (volatile! {})
mock-env {::sc/statechart machine
::sc/data-model DM
::sc/execution-model (reify sp/ExecutionModel)
::sc/event-queue (reify sp/EventQueue)
::sc/vwmem vwmem}
context (fn [c] (assoc mock-env ::sc/context-element-id c))
f (java.io.File/createTempFile "foo" "data")]

(spit f "{:a 1}")

(sp/load-data DM (context :A) (.getAbsolutePath f))

(assertions
"Places values into the correct context in working memory"
(::wmdm/data-model @vwmem) => {:a 1}))))
(let [machine (chart/statechart {}
(state {:id :A}
(state {:id :A/a})))
Expand Down

0 comments on commit 1cc45da

Please sign in to comment.