diff options
Diffstat (limited to 'src/chameleon/aai_processor.clj')
-rw-r--r-- | src/chameleon/aai_processor.clj | 68 |
1 files changed, 45 insertions, 23 deletions
diff --git a/src/chameleon/aai_processor.clj b/src/chameleon/aai_processor.clj index 7025c31..d1a7d25 100644 --- a/src/chameleon/aai_processor.clj +++ b/src/chameleon/aai_processor.clj @@ -1,8 +1,25 @@ (ns chameleon.aai-processor - (:require [chameleon.txform :refer :all] - [chameleon.route :refer :all] - [cheshire.core :refer :all] - [clojure.set :refer :all])) + (:require + [chameleon.route :refer :all] + [cheshire.core :refer :all] + [integrant.core :as ig] + [clojure.set :refer :all])) + +(defonce ^:private p-attr (atom nil)) +(defonce ^:private t-attr (atom nil)) + +(defmethod ig/init-key :chameleon/aai-processor [_ {:keys [provenance-attr truth-attr]}] + (reset! p-attr provenance-attr) + (reset! t-attr truth-attr)) + +(defmethod ig/halt-key! :chameleon/aai-processor [_ _] + (reset! p-attr nil) + (reset! t-attr nil)) + +(defn- map-keywords + "Maps all string based keys to keywords" + [kmap] + (into {} (for [[key value] kmap] [(keyword key) value]))) (defn- gen-trim-relationship "Generates a trimmed down version of the relationship containing only the id, type, url, and target" @@ -25,19 +42,23 @@ (let [resource-type (get-in body ["properties" "_type"]) id (body "_id") type (get-in body ["properties" "type"]) - properties (body "properties") - entity-response { - "id" id - "type" type - "properties" (dissoc properties "_type" "type") - }] + properties (body "properties")] (if (= resource-type "entity") ; Transform into an entity type (let [relationships (body "relationships")] - (assoc entity-response - "in" (into [] (map gen-trim-relationship (filter #(= (get-in % ["target" "id"]) id) relationships))) - "out" (into [] (map gen-trim-relationship (filter #(= (get-in % ["source" "id"]) id) relationships))))) - entity-response))) + { + "id" id + "type" type + "properties" (dissoc properties "_type" "type") + "in" (into [] (map gen-trim-relationship (filter #(= (get-in % ["target" "id"]) id) relationships))) + "out" (into [] (map gen-trim-relationship (filter #(= (get-in % ["source" "id"]) id) relationships))) + }) + ; Transform into a relationship type + { + "id" id + "type" type + "properties" (dissoc properties "_type" "type") + }))) (defn from-spike "Transforms Spike-based event payloads to a format accepted by Gallifrey for vertices and relationships" @@ -53,12 +74,13 @@ entity (map-keywords (parse-type txpayload)) key (:key entity) properties (assoc (:properties entity) :type (:type entity)) - truth-time (:truth-time entity) - entity-assertion {:meta {:key key - :operation operation - :time {:t-t truth-time}}}] - (assert-gallifrey gallifrey-host "aai" (name entity-type) (if (= entity-type :entity) - (assoc entity-assertion :body (generate-string {:properties properties})) - (assoc entity-assertion :body (generate-string (conj {:properties properties} - {:source (rename-keys (:source entity) {"key" "id"})} - {:target (rename-keys (:target entity) {"key" "id"})}))))))) + truth-time (if (not (nil? (get properties @t-attr))) {:t-t (get properties @t-attr)}) + assertion {:meta {:key key + :operation operation + :time truth-time}} + provenance (get properties @p-attr "aai")] + (assert-gallifrey gallifrey-host provenance (name entity-type) (if (= entity-type :entity) + (assoc assertion :body (generate-string {:properties properties})) + (assoc assertion :body (generate-string (conj {:properties properties} + {:source (rename-keys (:source entity) {"key" "id"})} + {:target (rename-keys (:target entity) {"key" "id"})}))))))) |