Skip to content

Instantly share code, notes, and snippets.

@WilliamParker
Created May 5, 2016 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WilliamParker/3858c7965138c808533cc2ec6b4e9b85 to your computer and use it in GitHub Desktop.
Save WilliamParker/3858c7965138c808533cc2ec6b4e9b85 to your computer and use it in GitHub Desktop.
(defn constant-accum [v]
(accumulate
:initial-value v
:reduce-fn (constantly v)
:combine-fn (constantly v)
:retract-fn (constantly v)
:convert-return-fn identity))
(deftest nil-accumulate-node-test
(let [r1 (dsl/parse-rule [[?r <- (constant-accum nil) :from [Cold]]]
(insert! (->TemperatureHistory ?r)))
r2 (dsl/parse-rule [[Hot (= ?hot-temp temperature)]
[?r <- (constant-accum nil) :from [Cold (< temperature ?hot-temp)]]]
(insert! (->TemperatureHistory ?r)))
q (dsl/parse-query [] [[TemperatureHistory (= ?temps temperatures)]])
empty-session (mk-session [r1 q] :cache false)]
(doseq [[empty-session node-type] [[(mk-session [r1 q] :cache false)
"AccumulateNode"]
[(mk-session [r2 q] :cache false)
"AccumulateWithJoinFilterNode"]]]
(is (= (-> empty-session
fire-rules
(query q))
[])
(str "A nil initial value with no matching elements should not propagate from a " node-type))
(is (= (-> empty-session
(insert (Cold. 10) (->Hot 100))
fire-rules
(query q))
[{:?temps nil}])
(str "Adding an element to an " node-type " that has a nil initial value"
\newline
"and a nil value after adding the fact should still propagate"))
(is (= (-> empty-session
(insert (Cold. 10) (->Hot 100))
fire-rules
(retract (Cold. 10))
(query q))
[])
(str "Adding then retracting the same element from an " node-type " that retracts "
\newline
"to nil should cause retraction of downstream facts"))
(is (= (-> empty-session
(insert (Cold. 10) (Cold. 20) (->Hot 100))
fire-rules
(retract (Cold. 10))
(query q))
[])
(str "Adding then retracting the same element from an " node-type " that retracts "
\newline
"to nil should cause retraction of downstream facts, even if another fact was not retracted."))
(is (= (-> empty-session
(insert (Cold. 10) (Cold. 20) (->Hot 100))
fire-rules
(retract (Cold. 10))
fire-rules
(insert (Cold. 10))
(fire-rules)
(query q))
[{:?temps nil}])
(str "Adding two facts, retracting one, then adding it again should make a nil result propagate through an " node-type)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment