Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions components/schema_migrate/src/schema_migrate/core.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(ns schema-migrate.core
(:require
[clojure.walk :as walk]
[clojure.spec.alpha :as spec]
[clojure.string :as s]
[clojure.walk :as walk]
[datascript.core :refer [squuid]]
[datomic.api :as d]
[datomic-store.main :as ds]
[clojure.spec.alpha :as spec]
[datomic.api :as d]
[nano-id.core :refer [nano-id]]))

(def
Expand Down Expand Up @@ -185,6 +185,11 @@
[conn t]
(:db/id (t-key->entity conn t)))

(defn t-key->bp-uuid
"Get the db/id using translation-key"
[conn t]
(:bp/uuid (t-key->entity conn t)))

(defn t-key-action-name->eid
"Given a translation-key of a group-variable an action's name return the action's entity id"
[conn gv-t-key action-name]
Expand Down Expand Up @@ -344,7 +349,7 @@

(defn ->conditional
"Payload for a Conditional."
[_conn {:keys [ttype operator values group-variable-uuid] :as params}]
[conn {:keys [ttype operator values group-variable-uuid sub-conditional-operator sub-conditionals] :as params}]
(let [payload (cond-> {}
(nil? (:bp/uuid params)) (assoc :bp/uuid (rand-uuid))
(nil? (:bp/nid params)) (assoc :bp/nid (nano-id))
Expand All @@ -354,14 +359,16 @@
group-variable-uuid (assoc :conditional/group-variable-uuid group-variable-uuid)
ttype (assoc :conditional/type ttype)
operator (assoc :conditional/operator operator)
values (assoc :conditional/values values))]
values (assoc :conditional/values values)
sub-conditional-operator (assoc :conditional/sub-conditional-operator sub-conditional-operator)
(seq sub-conditionals) (assoc :conditional/sub-conditionals (map #(->conditional conn %) sub-conditionals)))]
(if (spec/valid? :behave/conditional payload)
payload
(spec/explain :behave/conditional payload))))

(defn ->action
"Payload for an Action."
[conn {:keys [nname ttype target-value conditionals] :as params}]
[conn {:keys [nname ttype target-value conditionals conditionals-operator] :as params}]
(let [payload (cond-> {}
(nil? (:bp/uuid params)) (assoc :bp/uuid (rand-uuid))
(not (:bp/nid params)) (assoc :bp/nid (nano-id))
Expand All @@ -371,14 +378,15 @@
nname (assoc :action/name nname)
ttype (assoc :action/type ttype)
target-value (assoc :action/target-value target-value)
conditionals-operator (assoc :action/conditionals-operator conditionals-operator)
conditionals (assoc :action/conditionals (map #(->conditional conn %) conditionals)))]
(if (spec/valid? :behave/action payload)
payload
(spec/explain :behave/action payload))))

(defn ->variable
[_conn {:keys [nname domain-uuid list-eid translation-key help-key kind bp6-label bp6-code map-units-convertible?
dimension-uuid native-unit-uuid metric-unit-uuid english-unit-uuid] :as params}]
dimension-uuid native-unit-uuid metric-unit-uuid english-unit-uuid] :as params}]
(let [payload (cond-> {}
(nil? (:bp/uuid params)) (assoc :bp/uuid (rand-uuid))
(not (:bp/nid params)) (assoc :bp/nid (nano-id))
Expand Down Expand Up @@ -406,7 +414,7 @@
"Payload for a new Group Variable."
[conn {:keys
[parent-group-eid order variable-eid cpp-namespace cpp-class cpp-function cpp-parameter translation-key conditionally-set? actions
hide-result-conditionals hide-result? disable-multi-valued-input-conditionals disable-multi-valued-input-conditional-operator] :as params}]
hide-result-conditionals hide-result? disable-multi-valued-input-conditionals disable-multi-valued-input-conditional-operator] :as params}]
(let [payload (if (spec/valid? :behave/group-variable params)
params
(cond-> {}
Expand Down
20 changes: 12 additions & 8 deletions components/schema_migrate/src/schema_migrate/interface.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@
:doc "Get the :db/id using translation-key"}
t-key->eid c/t-key->eid)

(def ^{:arglists '([conn t])
:doc "Get the :bp/uuid using translation-key"}
t-key->bp-uuid c/t-key->bp-uuid)

(def ^{:arglists '([conn t])
:doc "Given a translation-key of a group-variable an action's name return the action's entity id"}
t-key-action-name->eid c/t-key-action-name->eid)

(def ^{:arglists '([conn bp6-code])
:doc "Given a Behave6 Variable code (e.g. `vSurfaceFuelCode`), returns the matching variable entity ID."}
:doc "Given a Behave6 Variable code (e.g. `vSurfaceFuelCode`), returns the matching variable entity ID."}
bp6-code->variable-eid c/bp6-code->variable-eid)

(def ^{:arglists '([conn attr])
Expand Down Expand Up @@ -95,23 +99,23 @@
remove-nested-i18ns-tx c/remove-nested-i18ns-tx)

(def ^{:arglists '([data])
:doc "Payload for a new Entity, which adds a :bp/nid and :bp/uuid."}
:doc "Payload for a new Entity, which adds a :bp/nid and :bp/uuid."}
->entity c/->entity)

(def ^{:arglists '([uuid operator value])
:doc "Payload for a Group Variable Conditional."}
:doc "Payload for a Group Variable Conditional."}
->gv-conditional c/->gv-conditional)

(def ^{:arglists '([operator values])
:doc "Payload for a Module Conditional."}
:doc "Payload for a Module Conditional."}
->module-conditional c/->module-conditional)

(def ^{:arglists '([conn params])
:doc "Payload for a Conditional."}
->conditional c/->conditional)

(def ^{:arglists '([conn params])
:doc "Payload for a new Group."}
:doc "Payload for a new Group."}
->group c/->group)

(def ^{:arglists '([conn params])
Expand All @@ -123,14 +127,14 @@
->variable c/->variable)

(def ^{:arglists '([conn params])
:doc "Payload for a new Group Variable."}
:doc "Payload for a new Group Variable."}
->group-variable c/->group-variable)

(def ^{:arglists '([source-eid destination-eid])
:doc "Payload for a new Link."}
:doc "Payload for a new Link."}
->link c/->link)

(def ^{:arglists '([migration-name])
(def ^{:arglists '([migration-name])
:->actiondoc "Payload for a new migration."}
->migration c/->migration)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
(ns migrations.2026-03-24-add-conditionals-to-default-heading-if-missing
(:require [behave-cms.server :as cms]
[behave-cms.store :refer [default-conn]]
[datomic.api :as d]
[schema-migrate.interface :as sm]))

;; ===========================================================================================================
;; Overview
;; ===========================================================================================================

;; Adds conditionals for when a user forgets to select a direction mode

;; ===========================================================================================================
;; Initialize
;; ===========================================================================================================

(cms/init-db!)

#_{:clj-kondo/ignore [:missing-docstring]}
(def conn (default-conn))

;; ===========================================================================================================
;; Payload
;; ===========================================================================================================

(defn t-key->conditional [conn t-key]
{:ttype :group-variable
:operator :equal
:values #{"true"}
:group-variable-uuid (sm/t-key->bp-uuid conn t-key)})

#_{:clj-kondo/ignore [:missing-docstring]}
(def payload
[{:db/id (sm/t-key->eid conn "behaveplus:surface:output:fire_behavior:surface_fire:direction_mode:heading")
:group-variable/actions [(sm/->action
conn
{:nname "Enable if no direction mode is selected and at least one of these outputs are selected"
:ttype :select
:conditionals-operator :and
:conditionals [{:ttype :group-variable
:operator :equal
:values #{"false"}
:group-variable-uuid (sm/t-key->bp-uuid conn "behaveplus:surface:output:fire_behavior:surface_fire:direction_mode:direction_of_interest")}

{:ttype :group-variable
:operator :equal
:values #{"false"}
:group-variable-uuid (sm/t-key->bp-uuid conn "behaveplus:surface:output:fire_behavior:surface_fire:direction_mode:heading_backing_flanking")}

{:ttype :group-variable
:operator :equal
:values #{"false"}
:group-variable-uuid (sm/t-key->bp-uuid conn "behaveplus:surface:output:fire_behavior:surface_fire:direction_mode:heading")
:sub-conditional-operator :or
:sub-conditionals [(t-key->conditional conn "behaveplus:surface:output:fire_behavior:surface_fire:rate_of_spread")
(t-key->conditional conn "behaveplus:surface:output:fire_behavior:surface_fire:fireline_intensity")
(t-key->conditional conn "behaveplus:surface:output:fire_behavior:surface_fire:flame_length")
(t-key->conditional conn "behaveplus:surface:output:size:surface___fire_size:fire_perimeter")
(t-key->conditional conn "behaveplus:surface:output:size:surface___fire_size:fire_area")
(t-key->conditional conn "behaveplus:surface:output:size:surface___fire_size:length-to-width-ratio")
(t-key->conditional conn "behaveplus:surface:output:size:surface___fire_size:spread-distance")]}]})]}])

;; ===========================================================================================================
;; Transact Payload
;; ===========================================================================================================

(comment
#_{:clj-kondo/ignore [:missing-docstring]}
(try (def tx-data @(d/transact conn payload))
(catch Exception e (str "caught exception: " (.getMessage e)))))

;; ===========================================================================================================
;; In case we need to rollback.
;; ===========================================================================================================

(comment
(sm/rollback-tx! conn tx-data))
12 changes: 10 additions & 2 deletions projects/behave_cms/src/cljc/behave_cms/queries.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns behave-cms.queries
(:require [clojure.string :as str]
(:require [clojure.string :as str]
#?(:cljs [datascript.core :as d]
:clj [datahike.api :as d])))
:clj [datahike.api :as d])))

(def rules
'[[(module ?a ?m) [?e :application/modules ?m]]
Expand Down Expand Up @@ -51,4 +51,12 @@

[(app-root ?a ?s)
[?m :module/submodules ?s]
[?a :application/modules ?m]]

;; Find app root from an action entity
[(app-root ?a ?action)
[?gv :group-variable/actions ?action]
[?g :group/group-variables ?gv]
[?sm :submodule/groups ?g]
[?m :module/submodules ?sm]
[?a :application/modules ?m]]])
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns behave-cms.components.conditionals.subs
(:require [re-frame.core :refer [reg-sub subscribe]]
[datascript.core :as d]
[behave-cms.store :refer [conn]]))
(:require [re-frame.core :refer [reg-sub subscribe]]
[datascript.core :as d]
[behave-cms.store :refer [conn]]))

(reg-sub
:conditionals/all-conditionals
Expand All @@ -21,7 +21,7 @@
module-conditionals (d/q '[:find ?c
:in $ ?g ?conditional-attr
:where
[?g ?conditionals-attr ?c]
[?g ?conditional-attr ?c]
[?c :conditional/type :module]]
@@conn
eid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
;;; Helpers

(defn- clear-editor []
(rf/dispatch [:state/set-state :editors {}]))
(rf/dispatch [:state/update [:editors :conditional] (fn [] nil)]))

(defn- clear-show-sub-conditional-editor []
(rf/dispatch [:state/set-state :show-sub-conditional-editor {}]))
Expand Down Expand Up @@ -138,8 +138,11 @@
(set-field (conj cond-path :conditional/values) nil)
(set-field (conj cond-path :conditional/group-variable-uuid)
(u/input-value %)))
:options (map (fn [{value :bp/uuid label :variable/name}]
{:value value :label label}) @variables)}]
:options (map (fn [{value :bp/uuid label :variable/name direction :group-variable/direction}]
{:value value
:label (if direction
(str label " (" (name direction) ")")
label)}) @variables)}]

[dropdown
{:label "Operator:"
Expand Down Expand Up @@ -298,9 +301,13 @@
gv-id @(rf/subscribe [:bp/lookup gv-uuid])
[module-id] @(rf/subscribe [:group-variable/module-submodule-group gv-id])
module-name @(rf/subscribe [:entity-attr module-id :module/name])
direction @(rf/subscribe [:entity-attr gv-id :group-variable/direction])
v-name (if (= conditional-type :module)
"Module"
@(rf/subscribe [:gv-uuid->variable-name gv-uuid]))]
(let [n @(rf/subscribe [:gv-uuid->variable-name gv-uuid])]
(if direction
(str n " (" (name direction) ")")
n)))]
[:div.conditionals-graph__node
(when (seq sub-conditionals)
[:div.conditionals-graph__operator
Expand Down Expand Up @@ -351,6 +358,6 @@
[conditionals-graph
parent-eid
conditional-eid
cond-attr
:conditional/sub-conditionals
:conditional/sub-conditional-operator]])]]))
(sort-by :variable/name conditionals)))]]))))
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns behave-cms.components.entity-form
(:require [behave-cms.components.common :refer [dropdown btn-sm]]
[behave-cms.components.conditionals.views :refer [conditionals-graph manage-conditionals]]
[behave-cms.components.group-variable-selector :refer [group-variable-selector]]
[behave-cms.components.translations :refer [all-translations]]
[behave-cms.utils :as u]
Expand Down Expand Up @@ -371,6 +372,18 @@
[:div.my-3
[all-translations @state]]))

(defmethod field-input :conditionals
[{:keys [label field-key cond-op-attr original]}]
(let [entity-id (:db/id original)]
(when entity-id
[:div.mb-3
(when label [:label.form-label label])
[:div.row
[:div.col-9
[conditionals-graph entity-id entity-id field-key cond-op-attr]]
[:div.col-3
[manage-conditionals entity-id field-key]]]])))

;;; Public Fns

(defn entity-form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@
maps. When an existing entity is selected, renders an [all-translations] table for
each attr that has a non-nil value on the entity.

`form-below?` - (optional) when true, renders the entity-form below the table instead
of to the right. Defaults to false.

"
[{:keys [title entity entities table-header-attrs entity-form-fields parent-id parent-field order-attr form-state-path on-select translation-config translation-attrs modify?]
[{:keys [title entity entities table-header-attrs entity-form-fields parent-id parent-field order-attr form-state-path on-select translation-config translation-attrs modify? form-below?]
:or {modify? true}}]
(r/with-let [entity-id-atom (r/atom nil)
show-entity-form? (r/atom false)]
[:div {:style {:display "flex"
:height "100%"
:padding "30px"}}
[:div {:style {:padding-right "10px"
[:div {:style {:display "flex"
:flex-direction (if form-below? "column" "row")
:height "100%"
:padding "30px"}}
[:div {:style {:padding-right (when-not form-below? "10px")
:width "100%"}}
[simple-table
(if (seq table-header-attrs)
Expand Down
Loading