Skip to content

Commit

Permalink
Make remove-rule update queues
Browse files Browse the repository at this point in the history
  • Loading branch information
oakes committed Sep 15, 2023
1 parent 0925c3a commit adc9aae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/odoyle/rules.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,16 @@ This is no longer necessary, because it is accessible via `match` directly."}
(recur session (:parent-id node))))
session))
(update :rule-name->node-id dissoc rule-name)
(update :node-id->rule-name dissoc node-id))
(update :node-id->rule-name dissoc node-id)
(update :then-queue (fn [then-queue]
(reduce
(fn [s [id _ :as tuple]]
(if (= id node-id)
(disj s tuple)
s))
then-queue
then-queue)))
(update :then-finally-queue disj node-id))
(throw (ex-info (str rule-name " does not exist in session") {}))))

#?(:clj
Expand Down
23 changes: 20 additions & 3 deletions test/odoyle/rules_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,8 @@

(deftest removing-a-rule
(let [*rule1-count (atom 0)
*rule2-count (atom 0)]
*rule2-count (atom 0)
*rule3-count (atom 0)]
(-> (reduce o/add-rule (o/->session)
(o/ruleset
{::rule1
Expand All @@ -1059,20 +1060,36 @@
[:what
[id ::height height]
:then
(swap! *rule2-count inc)]}))
(swap! *rule2-count inc)]
::rule3
[:what
[id ::color color]
:then-finally
(swap! *rule3-count inc)]}))
(o/insert ::alice ::color "red")
(o/insert ::bob ::height 72)
o/fire-rules
((fn [session]
(is (= 1 @*rule1-count))
(is (= 1 @*rule2-count))
(is (= 1 @*rule3-count))
session))
(o/remove-rule ::rule2)
(o/insert ::alice ::color "red")
(o/insert ::bob ::height 72)
(o/remove-rule ::rule2)
o/fire-rules
((fn [session]
(is (= 2 @*rule1-count))
(is (= 1 @*rule2-count))
(is (= 2 @*rule3-count))
session))
(o/insert ::alice ::color "red")
(o/insert ::bob ::height 72)
(o/remove-rule ::rule3)
o/fire-rules
((fn [session]
(is (= 3 @*rule1-count))
(is (= 1 @*rule2-count))
(is (= 2 @*rule3-count))
session)))))

0 comments on commit adc9aae

Please sign in to comment.