Skip to content

Instantly share code, notes, and snippets.

@WilliamParker
Created October 14, 2016 09:09
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/d3dbe9059ebe6ba094afe97c74391f9f to your computer and use it in GitHub Desktop.
Save WilliamParker/d3dbe9059ebe6ba094afe97c74391f9f to your computer and use it in GitHub Desktop.
(deftest test-rhs-retraction-with-two-interacting-fire-once-rules
(let [r1 (dsl/parse-rule [[Third]
[First]]
(do
(insert-unconditional! (->First))
(retract! (->Second))
(retract! (->Third))))
r2 (dsl/parse-rule [[:not [Second]]
[?fourth <- Fourth]
[?fs <- (acc/all) :from [First]]]
(do (retract! ?fourth)
(doseq [f ?fs]
(retract! f))))
first-query (dsl/parse-query [] [[First]])
empty-session (mk-session [r1 r2 first-query] :cache false)]
;; The expected order of events here is as follows:
;; 1. r2 is initially blocked by the presence of Second.
;; 2. r1 fires and inserts a First. It is effectively a fire-once rule since
;; it retracts Third.
;; 3. r2 fires and retracts all First facts present. The First inserted in Step 2
;; should be accumulated one and ready for retraction. If this First is not present
;; in the accumulated First facts before r2 fires it will not be retracted, since r2
;; is, like r1, a fire-once rule since it retracts Fourth.
;; Verifying that the insertion of this First into the rule network happens before
;; the execution of the RHS of r2 occurs is the primary goal of this test.
;; When a RHS retraction immediately activated the rule network
;; but an unconditional insertion was added to the buffer the ordering of RHS retractions and unconditional
;; insertions relative to other Rete operations could be different. Since these events come from the same
;; rule activation they should have the same ordering relative to other Rete operations in the session.
(is (empty? (-> empty-session
(insert (->Second) (->Fourth))
(insert (->Third))
(insert (->First))
fire-rules
(query first-query))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment