fact.clj

Branches > facts > 9a69ad5 > 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 attributes->entity-value-pairs (ref {}))

; These are all ephemeral right now ofc :p
(defn register-attribute! [attribute cardinality]
  (case cardinality
    ::one
    (dosync
      (commute attributes->entity-value-pairs assoc attribute (ref {}))))
  attribute)

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

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

(defn retract-fact [attribute entity]
  (dosync
    (alter (req! @attributes->entity-value-pairs attribute) dissoc entity)))

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