fact.clj

Branches > facts > latest > files / src / net / bowmap / stormbow / fact.clj

(ns net.bowmap.stormbow.fact
  (:require
   [clojure.spec.alpha :as s]))

(s/def ::fact (s/keys :opt [::attribute ::entity ::value]))

(defonce aev (ref {}))

; These are all ephemeral right now ofc :p
; Registers an ephemeral attribute
; For ephemeral stuff, I don't think this is a good way to go about this :p
; Makes sense for persistent information, but this is just terrible concurrency.
(defn register-attribute! [attribute cardinality]
  (case cardinality
    ::one
    (dosync
      (commute aev assoc attribute (ref {}))))
  attribute)

(register-attribute! ::playing ::one)

(defn assert-fact [attribute entity value]
  (dosync
    ; Can only have one value
    (alter (@aev attribute) assoc entity value)))

(defn retract-fact [attribute entity]
  (dosync
    (alter (req! @aev attribute) dissoc entity)))

(defn fact
  "Returns the current transactional value of the fact given its attribute"
  [attribute]
  (some-> (@aev attribute) deref))