Commit ce7ac6cc authored by ivan minutillo's avatar ivan minutillo
Browse files

basic queries

parent 3db018f1
No preview for this file type
......@@ -14,70 +14,79 @@
:resourceInventoriedAs {:type Int}
:resourceConformsTo {:type Int}}}}
:objects
{:Process
{:description "An activity that changes inputs into outputs. It could transform or transport economic resource(s)."
:fields
{:id {:type Int}
:name {:description "An informal or formal textual identifier for an item. Does not imply uniqueness."
:type (non-null String)}
:note {:type String
:description "A textual description or comment."}
:before {:type String
:description "The economic event occurred prior to this date/time."}
:finished {:type Boolean
:description "The process is complete or not. This is irrespective of if the original goal has been met, and indicates that no more will be done."}}}
:Action {:description "An informal or formal textual identifier for an object. Does not imply uniqueness."
:fields {:id {:type (non-null Int)}
:name {:type (non-null String)}}}
:Unit {:description "Defines a unit of measurement, along with its display symbol."
:fields {:id {:type (non-null Int)}
:name {:type String}}}
:Agent {:description "A person or a group"
:fields {:id {:type (non-null Int)}
:name {:type String}
:note {:type String}
:type {:type String}}}
:ResourceSpecification {:description "An informal or formal textual identifier for an object. Does not imply uniqueness."
:fields {:id {:type (non-null Int)}
:name {:type String}
:note {:type String}}}
:EconomicResource {:description "References one or more concepts in a common taxonomy or other classification scheme for purposes of categorization or grouping."
:fields {:id {:type (non-null Int)}
:name {:type String}
:valueflows {:type (list IncomingValueflows)}
:accountingQuantityNumericValue {:type Float}
:accountingQuantityUnit {:type Unit}
:onhandQuantityUnit {:type Unit}
:onhandQuantityNumericValue {:type Float}
:unitOfEffort {:type Unit}
:note {:type String}
:conformsTo {:type ResourceSpecification}}}
:EconomicEvent {:description "Relates an economic event to a verb, such as consume, produce, work, improve, etc."
:unions {:trackResult
{:members [:Process :EconomicEvent :EconomicResource]}}
:objects {:Process {:description "An activity that changes inputs into outputs. It could transform or transport economic resource(s)."
:fields
{:id {:type Int}
:name {:description "An informal or formal textual identifier for an item. Does not imply uniqueness."
:type (non-null String)}
:note {:type String
:description "A textual description or comment."}
:before {:type String
:description "The economic event occurred prior to this date/time."}
:finished {:type Boolean
:description "The process is complete or not. This is irrespective of if the original goal has been met, and indicates that no more will be done."}
; Inverse relationships and queries
:inputs {:type (list :EconomicEvent)}
:outputs {:type (list :EconomicEvent)}
:unplannedEconomicEvents {:type (list :EconomicEvent)}
:committedInputs {:type (list :EconomicEvent)}
:committedOutputs {:type (list :EconomicEvent)}
}}
:Action {:description "An informal or formal textual identifier for an object. Does not imply uniqueness."
:fields {:id {:type (non-null Int)}
:name {:type (non-null String)}}}
:Unit {:description "Defines a unit of measurement, along with its display symbol."
:fields {:id {:type (non-null Int)}
:note {:type String}
:hasPointInTime {:type String}
:provider {:type Agent}
:receiver {:type Agent}
:action {:type Action}
:inputOf {:type Process}
:outputOf {:type Process}
:resourceQuantityNumericValue {:type Float}
:resourcequantityunit {:type Unit}
:effortQuantityNumericValue {:type Float}
:effortQuantityUnit {:type Unit}
:resourceInventoriedAs {:type EconomicResource}
:resourceConformsTo {:type ResourceSpecification}}}
:IncomingValueflows {:description "Relates the whole valueflows starting from a resource"
:fields {:text {:type String}
:type {:type String}}}}
:name {:type String}}}
:Agent {:description "A person or a group"
:fields {:id {:type (non-null Int)}
:name {:type String}
:note {:type String}
:type {:type String}}}
:ResourceSpecification {:description "An informal or formal textual identifier for an object. Does not imply uniqueness."
:fields {:id {:type (non-null Int)}
:name {:type String}
:note {:type String}}}
:EconomicResource {:description "References one or more concepts in a common taxonomy or other classification scheme for purposes of categorization or grouping."
:fields {:id {:type (non-null Int)}
:name {:type String}
:track {:type (list :trackResult)}
:accountingQuantityNumericValue {:type Float}
:accountingQuantityUnit {:type Unit}
:onhandQuantityUnit {:type Unit}
:onhandQuantityNumericValue {:type Float}
:unitOfEffort {:type Unit}
:note {:type String}
:conformsTo {:type ResourceSpecification}}}
:EconomicEvent {:description "Relates an economic event to a verb, such as consume, produce, work, improve, etc."
:fields {:id {:type (non-null Int)}
:note {:type String}
:hasPointInTime {:type String}
:provider {:type Agent}
:receiver {:type Agent}
:action {:type Action}
:inputOf {:type Process}
:outputOf {:type Process}
:resourceQuantityNumericValue {:type Float}
:resourcequantityunit {:type Unit}
:effortQuantityNumericValue {:type Float}
:effortQuantityUnit {:type Unit}
:resourceInventoriedAs {:type EconomicResource}
:resourceConformsTo {:type ResourceSpecification}}}
:IncomingValueflows {:description "Relates the whole valueflows starting from a resource"
:fields {:text {:type String}
:type {:type String}}}}
:queries {; Agent module
:agent {:type :Agent
......@@ -89,7 +98,7 @@
:args {}
:resolve :query/allAgents}
; Observation layer
; Observation layer
:economicEvent {:type :EconomicEvent
:description "Retrieve the economic event by its ID, if it exists."
:args {:id {:type ID}}
......
......@@ -11,38 +11,3 @@
(:gen-class))
(defn find-economicEvent-by-id [id]
(let [event (queryEconomicEvents id)
effortQuantityUnit (queryUnits (:unit event))
action (queryActions (:action event))
provider (queryAgents (:provider event))
receiver (queryAgents (:receiver event))
resourceInventoriedAs (queryEconomicResources (:resourceInventoriedAs event))
resourceConformsTo (queryResourceSpecifications (:resourceConformsTo event))
inputOf (queryProcesses (:inputOf event))
outputOf (queryProcesses (:outputOf event))]
(-> event
(merge {:inputOf inputOf})
(merge {:outputOf outputOf})
(merge {:effortQuantityUnit effortQuantityUnit})
(merge {:action action})
(merge {:provider provider})
(merge {:receiver receiver})
(merge {:resourceConformsTo resourceConformsTo})
(merge {:resourceInventoriedAs resourceInventoriedAs}))))
(defn find-economicResource-by-id [id]
(let [vf (incoming-vf-dfs {:type (str "economicResource_" id)})
resource (queryEconomicResources id)]
(merge resource
{:valueflows vf}
)))
......@@ -30,10 +30,19 @@
; Query Process by id or returns all the processes
(defn queryProcesses
([id]
(jdbc/execute-one! db
["select * from Process where id = ?" id]
{:builder-fn opt/as-unqualified-maps}
))
(let [process (jdbc/execute-one! db
["select * from Process where id = ?" id]
{:builder-fn opt/as-unqualified-maps})
inputs (jdbc/execute! db
["select * from EconomicEvent where inputOf = ?" id]
{:builder-fn opt/as-unqualified-maps})
outputs (jdbc/execute! db
["select * from EconomicEvent where outputOf = ?" id]
{:builder-fn opt/as-unqualified-maps})]
(-> process
(merge {:inputs inputs})
(merge {:outputs outputs}))))
([]
(jdbc/execute! db
["select * from Process"]
......@@ -53,17 +62,7 @@
["select * from Agent"]
{:builder-fn opt/as-unqualified-maps})))
(defn queryEconomicEvents
([id]
(jdbc/execute-one! db
["select * from EconomicEvent where id = ?" id]
{:builder-fn opt/as-unqualified-maps}
))
([]
(jdbc/execute! db
["select * from EconomicEvent"]
{:builder-fn opt/as-unqualified-maps}
)))
(defn queryResourceSpecifications
([id]
......@@ -102,6 +101,35 @@
)))
(defn queryEconomicEvents
([id]
(let [event (jdbc/execute-one! db
["select * from EconomicEvent where id = ?" id]
{:builder-fn opt/as-unqualified-maps})
effortQuantityUnit (queryUnits (:unit event))
action (queryActions (:action event))
provider (queryAgents (:provider event))
receiver (queryAgents (:receiver event))
resourceInventoriedAs (queryEconomicResources (:resourceInventoriedAs event))
resourceConformsTo (queryResourceSpecifications (:resourceConformsTo event))
inputOf (queryProcesses (:inputOf event))
outputOf (queryProcesses (:outputOf event))]
(-> event
(merge {:inputOf inputOf})
(merge {:outputOf outputOf})
(merge {:effortQuantityUnit effortQuantityUnit})
(merge {:action action})
(merge {:provider provider})
(merge {:receiver receiver})
(merge {:resourceConformsTo resourceConformsTo})
(merge {:resourceInventoriedAs resourceInventoriedAs}))))
([]
(jdbc/execute! db
["select * from EconomicEvent"]
{:builder-fn opt/as-unqualified-maps})))
(defn createEconomicEvent
[event]
(sql/insert! db
......
(ns vfprocess.db.traversal
(:require [next.jdbc :as jdbc]
[camel-snake-kebab.core :as csk]
[next.jdbc.optional :as opt]
[com.walmartlabs.lacinia.schema :refer [tag-with-type]]
[vfprocess.db.queries :refer [db queryEconomicResources queryProcesses]])
)
......@@ -14,62 +14,95 @@
(defn first-neighbors
[v]
(let [node (queryEconomicResources v)]
{:type (str "outputOf_" (:id node))}))
(tag-with-type (-> node
(merge {:type (str "outputOf_" (:id node))})) :EconomicEvent)
))
(defn visited? [v coll]
(some #(= % v) coll))
(defn find-id
[type]
(.substring type (+ (.indexOf type "_") 1)))
(defn find-neighbors
"Returns the sequence of neighbors for the given node"
[v coll]
(get coll v))
(defn find-tracks
"Returns the sequence of neighbors for the given node"
[v]
(let [id (find-id (:type v))]
(println id)
(cond
; If the node is a process, retrieve the input economic event
(.contains (:type v) "process")
(let [node (jdbc/execute-one! db
["select * from EconomicEvent where inputOf = ? " id]
{:builder-fn opt/as-unqualified-maps})]
["select * from EconomicEvent where inputOf = ? " id]
{:builder-fn opt/as-unqualified-maps})]
(if (= nil node)
nil
{:type (str "inputOf_" (:id node))
:text (:name node)}))
(tag-with-type (-> node
(merge {:type (str "inputOf_" (:id node))})) :EconomicEvent)))
; If the node is an economic event input of a process, retrieve the incoming resource
(.contains (:type v) "inputOf")
(let [node (queryEconomicResources id)]
(if (= nil node)
nil
{:type (str "economicResource_" (:id node))
:text (str (:accountingQuantityNumericValue node) " " (:name node))}))
(tag-with-type (-> node
(merge {:type (str "economicResource_" (:id node))})) :EconomicResource)))
; If the node is an economic event output of a process, retrieve the incoming process
(.contains (:type v) "outputOf")
(let [node (queryProcesses id)]
(let [node (queryProcesses (:outputOf v))]
(if (= nil node)
nil
{:type (str "process_" (:id node))
:text (:name node)}))
(tag-with-type (-> node
(merge {:type (str "process_" (:id node))})) :Process)))
; If the node is a resource, retrieve the incoming event (outputOf)
(.contains (:type v) "economicResource")
(let [node(jdbc/execute-one! db
["select * from EconomicEvent where resourceInventoriedAs = ?" id]
{:builder-fn opt/as-unqualified-maps})]
(println node)
(let [node (jdbc/execute-one! db
["select * from EconomicEvent where resourceInventoriedAs = ?" id]
{:builder-fn opt/as-unqualified-maps})]
(if (= nil node)
nil
{:type (str "outputOf_" (:id node))
:text (str (:name (:action node)) " " (:resourceQuantityNumericValue node) " " (:name (:resourceQuantityUnit node)) " of " (:name (:resourceInventoriedAs node)))}))
(do
(tag-with-type (-> node
(merge {:type (str "outputOf_" (:id node))})) :EconomicEvent))))
:else nil)))
(defn incoming-vf-dfs
"Traverses a graph in Depth First Search (DFS)"
[v]
(println v)
(loop [stack (vector v) ;; Use a stack to store nodes we need to explore
visited []] ;; A vector to store the sequence of visited nodes
(if (empty? stack) ;; Base case - return visited nodes if the stack is empty
visited
(let [v (peek stack)
neighbors (find-neighbors v)
neighbors (find-tracks v)
new-stack (if (= nil neighbors) [] (vector neighbors))]
(if (= nil neighbors)
(recur new-stack (conj visited neighbors))
(recur new-stack (conj visited neighbors)))))))
\ No newline at end of file
(recur new-stack visited)
(recur new-stack (conj visited neighbors)))))))
; (defn graph-dfs
; "Traverses a graph in Depth First Search (DFS)"
; [v]
; (loop [stack (vector v) ;; Use a stack to store nodes we need to explore
; visited []] ;; A vector to store the sequence of visited nodes
; (if (empty? stack) ;; Base case - return visited nodes if the stack is empty
; visited
; (let [v (peek stack)
; neighbors (find-tracks v)
; not-visited (filter (complement #(visited? % visited)) neighbors)
; new-stack (into (pop stack) not-visited)]
; (if (visited? v visited)
; (recur new-stack visited)
; (recur new-stack (conj visited v)))))))
(ns vfprocess.records)
(defrecord ProcessRecord [id name note before finished])
(defrecord EconomicResourceRecord [id name track accountingQuantityNumericValue accountingQuantityUnit onhandQuantityUnit onhandQuantityNumericValue unitOfEffort note conformsTo])
(defrecord EconomicEventRecord [id
note
hasPointInTime
provider
receiver
action
inputOf
outputOf
resourceQuantityNumericValue
resourcequantityunit
effortQuantityNumericValue
effortQuantityUnit
resourceInventoriedAs
resourceConformsTo])
......@@ -5,7 +5,8 @@
[next.jdbc.sql :as sql]
[com.walmartlabs.lacinia.schema :as schema]
[com.walmartlabs.lacinia.resolve :refer [resolve-as]]
[com.walmartlabs.lacinia.schema :refer [tag-with-type]]
[com.stuartsierra.component :as component]
[vfprocess.db.traversal :refer [incoming-vf-dfs
first-neighbors]]
......@@ -18,125 +19,118 @@
queryUnits
queryActions
createEconomicEvent]]
[vfprocess.core :refer [find-economicEvent-by-id]]
[clojure.edn :as edn]))
(defn resolve-process-by-id
[args]
(let [{:keys [id]} args]
(queryProcesses id)))
(defn resolve-processes
[]
(queryProcesses))
(defn resolve-agent-by-id
[args]
(let [{:keys [id]} args]
(queryAgents id)))
(defn resolve-agents
[]
(queryAgents))
(defn resolve-economicEvent-by-id
[args]
(let [{:keys [id]} args]
(find-economicEvent-by-id id)))
(defn resolve-economicEvents
[]
(queryEconomicEvents))
(defn resolve-economicResource-by-id
[args]
(let [{:keys [id]} args]
(queryEconomicResources id)))
(defn resolve-economicResources
[]
(queryEconomicResources))
(defn resolve-resourceSpecification-by-id
[args]
(let [{:keys [id]} args]
(queryResourceSpecifications id)))
(defn resolve-resourceSpecifications
[]
(queryResourceSpecifications))
(defn resolve-action-by-id
[args]
(let [{:keys [id]} args]
(queryActions id)))
(defn resolve-actions
[]
(queryActions))
(defn resolve-unit-by-id
[args]
(let [{:keys [id]} args]
(queryUnits id)))
(defn resolve-units
[]
(queryUnits))
(defn incoming-valueflows
[args]
(let [{:keys [id]} args
node (first-neighbors id)
resource (queryEconomicResources id)
first-node {:type (str "economicResource_" id)
:text (str (:accountingQuantityNumericValue resource) " " (:name resource))}]
(incoming-vf-dfs first-node)))
(defn find-economicResource-by-id [id]
(let [economicResource (queryEconomicResources id)
first-node {:type (str "economicResource_" id)}
incoming-valueflows (incoming-vf-dfs first-node)]
(do
(-> economicResource
(merge {:track incoming-valueflows})))))
(defn mutationNewEconomicEvent
[args]
(let [{:keys [event]} args
economicResource (queryEconomicResources (:resourceInventoriedAs event))
action (queryActions (:action event))
]
(createEconomicEvent event)
(println event)
(if (some? (:resourceInventoriedAs event))
(if (= (:resourceEffect action) "+")
(do
(println "ciao")
(sql/update! db :EconomicResource
{:accountingQuantityNumericValue (+
(:accountingQuantityNumericValue economicResource)
(:resourceQuantityNumericValue event))}
{:onhandQuantitynumericValue (+
(let [{:keys [event]} args
economicResource (queryEconomicResources (:resourceInventoriedAs event))
toEconomicResource (queryEconomicResources (:toResourceInventoriedAs event))
action (queryActions (:action event))
economicEvent (createEconomicEvent event)
]
(cond
(some? (:resourceInventoriedAs event))
(cond
(= (:resourceEffect action) "+")
(if (= (:createResource event) true)
(do (sql/insert! db :EconomicResource economicResource))
(do
(sql/update! db :EconomicResource
{:accountingQuantityNumericValue (+
(:accountingQuantityNumericValue economicResource)
(:resourceQuantityNumericValue event))
:onhandQuantitynumericValue (+
(:accountingQuantityNumericValue economicResource)
(:resourceQuantityNumericValue event))}
{:id (:id economicResource)})
event))
(= (:resourceEffect action) "-")
(do
(sql/update! db :EconomicResource
{:accountingQuantityNumericValue (-
(:accountingQuantityNumericValue economicResource)
(:resourceQuantityNumericValue event))}))
(:resourceQuantityNumericValue (:resourceInventoriedAs event))))))
(defn resolver-map
[]
{:query/process (fn [context args value] (resolve-process-by-id args))
:query/allProcesses (fn [context args value] (resolve-processes))
:query/agent (fn [context args value] (resolve-agent-by-id args))
:query/allAgents (fn [context args value] (resolve-agents))
:query/economicEvent (fn [context args value] (resolve-economicEvent-by-id args))
:query/allEconomicEvents (fn [context args value] (resolve-economicEvents))
:query/economicResource (fn [context args value] (resolve-economicResource-by-id args))
:query/allEconomicResources (fn [context args value] (resolve-economicResources))
:query/resourceSpecification (fn [context args value] (resolve-resourceSpecification-by-id args))
:query/allResourceSpecification (fn [context args value] (resolve-resourceSpecifications))
:query/action (fn [context args value] (resolve-action-by-id args))
:query/allActions (fn [context args value] (resolve-actions))
:query/unit (fn [context args value] (resolve-unit-by-id args))
:query/allUnits (fn [context args value] (resolve-units))
:query/incomingValueflows (fn [context args value] (incoming-valueflows args))
:mutation/createEconomicEvent (fn [context args value] (mutationNewEconomicEvent args))
})
(:resourceQuantityNumericValue event))
:onhandQuantitynumericValue (-
(:accountingQuantityNumericValue economicResource)