From 1cc45dac08d2a81ce05f9d4dbc6ea1b9423306b3 Mon Sep 17 00:00:00 2001 From: Tony Kay Date: Tue, 15 Oct 2024 07:52:14 -0700 Subject: [PATCH] Fixed external file load for data model. --- .../data_model/working_memory_data_model.cljc | 9 ++-- .../working_memory_data_model_spec.cljc | 50 ++++++++++++++++++- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/main/com/fulcrologic/statecharts/data_model/working_memory_data_model.cljc b/src/main/com/fulcrologic/statecharts/data_model/working_memory_data_model.cljc index 043c7bf..dce4304 100644 --- a/src/main/com/fulcrologic/statecharts/data_model/working_memory_data_model.cljc +++ b/src/main/com/fulcrologic/statecharts/data_model/working_memory_data_model.cljc @@ -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))) @@ -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))) diff --git a/src/test/com/fulcrologic/statecharts/data_model/working_memory_data_model_spec.cljc b/src/test/com/fulcrologic/statecharts/data_model/working_memory_data_model_spec.cljc index 0a5c18a..d30aabb 100644 --- a/src/test/com/fulcrologic/statecharts/data_model/working_memory_data_model_spec.cljc +++ b/src/test/com/fulcrologic/statecharts/data_model/working_memory_data_model_spec.cljc @@ -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}))) @@ -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})))