From 9dd4850dee829e5fd62a9a4b15ac3f4051017e2d Mon Sep 17 00:00:00 2001 From: Jose Gomez Date: Sat, 10 Oct 2026 09:09:10 -0500 Subject: [PATCH 1/2] feat: store accum filter memory for accurate deserialization --- src/main/clojure/clara/rules/durability.clj | 21 +++- src/main/clojure/clara/rules/engine.clj | 37 ++++++- src/main/clojure/clara/rules/memory.clj | 69 +++++++++++- src/test/clojure/clara/test_durability.clj | 111 +++++++++++++++++++- 4 files changed, 230 insertions(+), 8 deletions(-) diff --git a/src/main/clojure/clara/rules/durability.clj b/src/main/clojure/clara/rules/durability.clj index f0ae475..69327c5 100644 --- a/src/main/clojure/clara/rules/durability.clj +++ b/src/main/clojure/clara/rules/durability.clj @@ -319,6 +319,24 @@ (transient {})) persistent!))) +(defn index-accum-filtered-memory [seen accum-filtered-mem] + (let [index-matches (fn [matches] + (mapv (fn [[fact node-id]] + [(find-index-or-add! seen fact) node-id]) + matches)) + index-facts (fn [facts] + (mapv #(find-index-or-add! seen %) facts))] + (update-vals accum-filtered-mem + (fn [left-matches->filtered] + (persistent! + (reduce-kv (fn [m left-matches accum-bindings->facts] + (assoc! m + (index-matches left-matches) + (-> (index-update-bindings-keys #(index-bindings seen %) accum-bindings->facts) + (update-vals index-facts)))) + (transient {}) + left-matches->filtered)))))) + (defn index-beta-memory [internal-seen seen bmem] (let [index-update-tokens (fn [tokens] (mapv #(index-token internal-seen seen %) @@ -458,7 +476,8 @@ (update :alpha-memory #(index-alpha-memory internal-seen seen %)) (update :beta-memory #(index-beta-memory internal-seen seen %)) (update :production-memory #(index-production-memory internal-seen seen %)) - (update :activation-map #(index-activation-map internal-seen seen %)))] + (update :activation-map #(index-activation-map internal-seen seen %)) + (update :accum-filtered-memory #(index-accum-filtered-memory seen %)))] {:memory indexed :indexed-facts (vec-indexed-facts seen idx-fact-arr-pair-fn) diff --git a/src/main/clojure/clara/rules/engine.clj b/src/main/clojure/clara/rules/engine.clj index 555c2c2..806f7b1 100644 --- a/src/main/clojure/clara/rules/engine.clj +++ b/src/main/clojure/clara/rules/engine.clj @@ -1434,6 +1434,7 @@ :when (some? converted-result)] + (mem/add-accum-filtered! memory node (:matches token) fact-bindings (vec filtered-facts)) (send-accumulated node accum-condition accumulator result-binding token converted-result fact-bindings transport memory listener)) @@ -1455,6 +1456,7 @@ (when (some? converted-result) ;; Send the created accumulated item to the children. (doseq [token tokens] + (mem/add-accum-filtered! memory node (:matches token) join-bindings []) (send-accumulated node accum-condition accumulator result-binding token converted-result join-bindings transport memory listener)))) @@ -1471,6 +1473,9 @@ convert-return-fn (:convert-return-fn accumulator) grouped-candidate-facts (mem/get-accum-reduced-all memory node join-bindings)] + (doseq [token tokens] + (mem/remove-accum-filtered! memory node (:matches token))) + (cond (seq grouped-candidate-facts) @@ -1591,6 +1596,12 @@ new-converted (when (some? accum-result) (convert-return-fn accum-result))]] + ;; Store the facts even when the result does not change, e.g. acc/min with a new larger fact. + (if (some? new-converted) + (mem/add-accum-filtered! memory node (:matches token) bindings + (into (vec previous-filtered-facts) new-filtered-facts)) + (mem/remove-accum-filtered! memory node (:matches token) bindings)) + (cond ;; When both the new and previous result were nil do nothing. @@ -1687,6 +1698,10 @@ new-converted (when (some? new-result) (convert-return-fn new-result))]] + (if (some? new-converted) + (mem/add-accum-filtered! memory node (:matches token) bindings (vec new-facts)) + (mem/remove-accum-filtered! memory node (:matches token) bindings)) + (cond ;; When both the previous and new results are nil do nothing. @@ -1722,21 +1737,39 @@ ;; the join filter function. Since the :matches are not used in the join filter function and the bindings in the ;; token will contain all bindings used in the "ancestor token" to join with these same facts, we can just pass the token ;; as-is to the join filter. + ;; + ;; A read-only session has no compiled join-filter-fn, so it cannot filter the facts again. + ;; The activation methods above store the filtered facts with mem/add-accum-filtered! each time they run the join filter. + ;; This lookup uses them first. The key is the :matches of the token that entered this node. + ;; That token's :matches is the prefix before this node's entry in the descendant token. + ;; Sessions serialized before the filtered facts were stored have none, so the join filter runs again. IAccumInspect (token->matching-elements [this memory token] (let [join-bindings (-> token :bindings (select-keys (get-join-keys this))) fact-bindings (-> token :bindings (select-keys new-bindings)) - accum-reduced (mem/get-accum-reduced memory this join-bindings (merge join-bindings fact-bindings))] + accum-bindings (merge join-bindings fact-bindings) + accum-reduced (mem/get-accum-reduced memory this join-bindings accum-bindings) + matches (:matches token) + node-match-idx (first (keep-indexed (fn [i [_ node-id]] (when (= id node-id) i)) matches)) + filtered (if node-match-idx + (mem/get-accum-filtered memory this (subvec matches 0 node-match-idx) accum-bindings) + ::mem/no-accum-filtered)] ;; The functionality to throw conditions with meaningful information assumes that all bindings in the token ;; are meaningful to the join, which is not the case here since the token passed is from a descendant of this node, not ;; this node. The generated error message also wouldn't make much sense in the context of session inspection. ;; We could create specialized error handling here, but in reality most cases that cause errors here would also cause ;; errors at rule firing time so the benefit would be limited. Nevertheless there would be some benefit and it is ;; possible that we will do it in the future.. - (if (not= accum-reduced ::mem/no-accum-reduced) + (cond + (not= filtered ::mem/no-accum-filtered) + filtered + + (not= accum-reduced ::mem/no-accum-reduced) (for [fact accum-reduced :when (join-filter-fn token fact fact-bindings {})] fact) + + :else [])))) ;; This lives here as it is both close to the node that it represents, and is accessible to both clj and cljs diff --git a/src/main/clojure/clara/rules/memory.clj b/src/main/clojure/clara/rules/memory.clj index eab6682..24cce60 100644 --- a/src/main/clojure/clara/rules/memory.clj +++ b/src/main/clojure/clara/rules/memory.clj @@ -48,6 +48,12 @@ ;; accumulated items on this node. (get-accum-reduced-complete [memory node]) + ;; Returns the facts that passed the join filter of an accumulator node with a join filter. + ;; left-matches are the :matches of the token that entered the node. + ;; accum-bindings are the join bindings merged with the fact bindings. + ;; Returns ::no-accum-filtered when no facts are stored. These facts are used only for inspection. + (get-accum-filtered [memory node left-matches accum-bindings]) + ;; Returns insertions that occurred at the given node for the given token. ;; Returns a sequence of the form ;; [facts-inserted-for-one-rule-activation facts-inserted-for-another-rule-activation] @@ -80,6 +86,14 @@ ;; Removes the result of a reduced accumulator execution to the given memory and node. (remove-accum-reduced! [memory node join-bindings fact-bindings]) + ;; Stores the facts that passed the join filter of an accumulator node with a join filter. + ;; See get-accum-filtered. + (add-accum-filtered! [memory node left-matches accum-bindings facts]) + + ;; Removes the filtered facts stored for the token with the given left-matches at the given node. + ;; Without accum-bindings, removes them for all accum-bindings of that token. + (remove-accum-filtered! [memory node left-matches] [memory node left-matches accum-bindings]) + ;; Add a record that a given fact twas inserted at a given node with ;; the given support. Used for truth maintenance. ;; This should be called at most once per rule activation. @@ -457,7 +471,8 @@ ^Map beta-memory ^Map accum-memory ^Map production-memory - ^NavigableMap activation-map] + ^NavigableMap activation-map + ^Map accum-filtered-memory] IMemoryReader (get-rulebase [memory] rulebase) @@ -502,6 +517,9 @@ :fact-bindings fact-binding :result reduced})) + (get-accum-filtered [memory node left-matches accum-bindings] + (get-in accum-filtered-memory [(:id node) left-matches accum-bindings] ::no-accum-filtered)) + (get-insertions [memory node token] (get (get production-memory (:id node) {}) @@ -629,6 +647,39 @@ (not-empty (dissoc fact-binding-accum-map fact-bindings)))) (not-empty join-binding-accum-map))))) + (add-accum-filtered! [memory node left-matches accum-bindings facts] + (hm/compute! accum-filtered-memory (:id node) + (fn add-lmfm + [_ left-matches-filtered-map] + (if left-matches-filtered-map + (let [left-matches-filtered-map (->mutable-map left-matches-filtered-map)] + (hm/compute! left-matches-filtered-map left-matches + (fn add-abfm + [_ accum-bindings-filtered-map] + (assoc accum-bindings-filtered-map accum-bindings facts))) + left-matches-filtered-map) + {left-matches {accum-bindings facts}})))) + + (remove-accum-filtered! [memory node left-matches] + (hm/compute-if-present! accum-filtered-memory (:id node) + (fn remove-lmfm + [_ left-matches-filtered-map] + (let [left-matches-filtered-map (->mutable-map left-matches-filtered-map)] + (hm/compute-if-present! left-matches-filtered-map left-matches + (fn remove-abfm [_ _] nil)) + (not-empty left-matches-filtered-map))))) + + (remove-accum-filtered! [memory node left-matches accum-bindings] + (hm/compute-if-present! accum-filtered-memory (:id node) + (fn remove-lmfm + [_ left-matches-filtered-map] + (let [left-matches-filtered-map (->mutable-map left-matches-filtered-map)] + (hm/compute-if-present! left-matches-filtered-map left-matches + (fn remove-abfm + [_ accum-bindings-filtered-map] + (not-empty (dissoc accum-bindings-filtered-map accum-bindings)))) + (not-empty left-matches-filtered-map))))) + ;; The value under each token in the map should be a sequence ;; of sequences of facts, with each inner sequence coming from a single ;; rule activation. @@ -803,8 +854,12 @@ (persistent-maps persistent-vals production-memory) (into {} (map (juxt key (comp ->persistent-coll val))) - activation-map))))) + activation-map) + (persistent-maps persistent-vals accum-filtered-memory))))) +;;; accum-filtered-memory holds the facts that passed the join filter of each accumulator node with a join filter. +;;; Only inspection reads it. The node replaces these facts each time it runs its join filter. +;;; It is nil in memory restored from sessions serialized before this field existed. (defrecord PersistentLocalMemory [rulebase activation-group-sort-fn activation-group-fn @@ -812,7 +867,8 @@ beta-memory accum-memory production-memory - activation-map] + activation-map + accum-filtered-memory] IMemoryReader (get-rulebase [memory] rulebase) @@ -858,6 +914,9 @@ :fact-bindings fact-binding :result reduced})) + (get-accum-filtered [memory node left-matches accum-bindings] + (get-in accum-filtered-memory [(:id node) left-matches accum-bindings] ::no-accum-filtered)) + (get-insertions [memory node token] (get (get production-memory (:id node) {}) @@ -888,7 +947,8 @@ (.put treemap activation-group (->activation-priority-queue activations false))) - treemap)))) + treemap) + (->mutable-map (or accum-filtered-memory {}))))) (defn local-memory "Creates an persistent local memory for the given rule base." @@ -901,4 +961,5 @@ (hf/hash-map) (hf/hash-map) (hf/hash-map) + (hf/hash-map) (hf/hash-map))) diff --git a/src/test/clojure/clara/test_durability.clj b/src/test/clojure/clara/test_durability.clj index c60d6c7..8731227 100644 --- a/src/test/clojure/clara/test_durability.clj +++ b/src/test/clojure/clara/test_durability.clj @@ -11,8 +11,9 @@ [clojure.java.io :as jio] [clojure.test :refer :all] [clara.rules.compiler :as com] + [clara.tools.inspect :as inspect] [clara.tools.testing-utils :as tu]) - (:import [clara.rules.testfacts Temperature] + (:import [clara.rules.testfacts Temperature WindSpeed] [clara.rules.engine TestNode] [clara.rules.engine ReadOnlyLocalSession] [java.io ByteArrayInputStream ByteArrayOutputStream])) @@ -404,6 +405,114 @@ restored-qresults1 restored-qresults2))))) +(defn session-serde + [s deserialize-opts] + (let [holder (atom []) + mem-serializer (->LocalMemorySerializer holder)] + (with-open [baos (ByteArrayOutputStream.)] + (d/serialize-session-state s (df/create-session-serializer baos) mem-serializer {:with-rulebase? true}) + (with-open [bais (ByteArrayInputStream. (.toByteArray baos))] + (d/deserialize-session-state (df/create-session-serializer bais) mem-serializer deserialize-opts))))) + +(defn accumulated-facts + "Returns the facts accumulated by the accumulator conditions of the rule, one set per condition match." + [session rule-name] + (->> (inspect/inspect session) + :rule-matches + (some (fn [[rule explanations]] + (when (= rule-name (:name rule)) + explanations))) + (mapcat :matches) + (keep :facts-accumulated) + (mapv set))) + +(defn temps-under-threshold-accumulated + [session] + (accumulated-facts session "clara.durability-rules/find-temps-under-threshold")) + +;; Unlike find-temps-under-threshold, this accumulator also has an equality join on ?loc. +;; The join bindings are then part of the accum memory key. +(defrule find-temps-under-wind-speed + [WindSpeed (= ?loc location) (= ?ws windspeed)] + [?ts <- (acc/all) :from [Temperature (= location ?loc) (< temperature ?ws)]] + => + (insert! (->TemperatureHistory (mapv :temperature ?ts)))) + +;; A new matching fact above the minimum leaves the result unchanged but adds to the accumulated facts. +(defrule find-min-temp-under-wind-speed + [WindSpeed (= ?loc location) (= ?ws windspeed)] + [?min <- (acc/min :temperature) :from [Temperature (= location ?loc) (< temperature ?ws)]] + => + (insert! (->Cold ?min))) + +(deftest test-durability-read-only-accumulator-inspection + (let [fired (:fired-session (session-test (mk-session 'clara.durability-rules))) + expected [#{(->Temperature 40 "LAX") (->Temperature 30 "SAN") (->Temperature 20 "CHI")}] + restored (session-serde fired {}) + ro-restored (session-serde fired {:read-only? true})] + + (testing "inspection shows only the facts that passed the join filter" + (is (= expected + (temps-under-threshold-accumulated fired) + (temps-under-threshold-accumulated restored) + (temps-under-threshold-accumulated ro-restored))) + (is (= (set (inspect/get-root-facts fired)) + (set (inspect/get-root-facts ro-restored))))) + + (testing "inspection shows only the facts that passed the join filter when the accumulator has join bindings" + (let [rule-name "clara.test-durability/find-temps-under-wind-speed" + fired (-> (mk-session 'clara.test-durability) + (insert (->Temperature 10 "MCI") + (->Temperature 30 "MCI") + (->Temperature 5 "LAX") + (->WindSpeed 20 "MCI")) + fire-rules)] + (is (= [#{(->Temperature 10 "MCI")}] + (accumulated-facts fired rule-name) + (accumulated-facts (session-serde fired {}) rule-name) + (accumulated-facts (session-serde fired {:read-only? true}) rule-name))))) + + (testing "a restored session does not use stale filtered facts after its candidate facts change" + (let [insert-temps #(-> % + (insert (->Temperature 45 "DEN") (->Temperature 60 "PHX")) + fire-rules) + retract-temp #(-> % + (retract (->Temperature 40 "LAX")) + fire-rules)] + (is (= [#{(->Temperature 45 "DEN") (->Temperature 40 "LAX") (->Temperature 30 "SAN") (->Temperature 20 "CHI")}] + (temps-under-threshold-accumulated (insert-temps fired)) + (temps-under-threshold-accumulated (insert-temps restored)))) + (is (= [#{(->Temperature 30 "SAN") (->Temperature 20 "CHI")}] + (temps-under-threshold-accumulated (retract-temp fired)) + (temps-under-threshold-accumulated (retract-temp restored)))))) + + (testing "the accumulated facts change when the accumulator result does not" + (let [rule-name "clara.test-durability/find-min-temp-under-wind-speed" + fired (-> (mk-session 'clara.test-durability) + (insert (->Temperature 10 "MCI") (->WindSpeed 20 "MCI")) + fire-rules + (insert (->Temperature 15 "MCI") (->Temperature 25 "MCI")) + fire-rules)] + (is (= [#{(->Temperature 10 "MCI") (->Temperature 15 "MCI")}] + (accumulated-facts fired rule-name) + (accumulated-facts (session-serde fired {:read-only? true}) rule-name))))) + + (testing "a read-only session serialized again keeps the filtered facts" + (is (= expected + (temps-under-threshold-accumulated (session-serde ro-restored {:read-only? true}))))) + + (testing "a session serialized without filtered facts still deserializes in read-only mode" + ;; This is the shape of sessions serialized before the filtered facts were stored. + ;; The join filter cannot run in read-only mode, so inspection shows all candidate facts. + (let [old-ro-restored (with-redefs-fn {#'d/index-accum-filtered-memory (constantly nil)} + #(session-serde fired {:read-only? true}))] + (is (= [{:?tut (dr/->TempsUnderThreshold [(->Temperature 40 "LAX") + (->Temperature 30 "SAN") + (->Temperature 20 "CHI")])}] + (query old-ro-restored dr/temps-under-thresh))) + (is (= [(conj (first expected) (->Temperature 50 "MCI"))] + (temps-under-threshold-accumulated old-ro-restored))))))) + (defn get-test-nodes [session] (->> session From daa129394e0ab347475bed8717ff047fd94cf421 Mon Sep 17 00:00:00 2001 From: Jose Gomez Date: Mon, 12 Oct 2026 14:53:36 -0500 Subject: [PATCH 2/2] chore: tiny performance enhancement using filter accum memory --- src/main/clojure/clara/rules/engine.clj | 35 +++++++++----- src/main/clojure/clara/rules/memory.clj | 4 +- .../clara/performance/test_rule_execution.clj | 46 +++++++++++++++++++ src/test/clojure/clara/test_durability.clj | 20 +++++++- 4 files changed, 90 insertions(+), 15 deletions(-) diff --git a/src/main/clojure/clara/rules/engine.clj b/src/main/clojure/clara/rules/engine.clj index 806f7b1..4a56808 100644 --- a/src/main/clojure/clara/rules/engine.clj +++ b/src/main/clojure/clara/rules/engine.clj @@ -1395,6 +1395,16 @@ :when (join-node-matches node token fact bindings join-filter-fn (:env accum-condition))] fact)) +(defn- stored-or-filtered + "Returns the facts in candidate-facts that pass the join filter for the given token. + A stored entry is always exact, so it is used when present. The node stores entries only for tokens it propagated. + Without an entry, which is also the case for sessions from older versions, the join filter runs." + [node memory token candidate-facts bindings join-filter-fn accum-condition] + (let [stored (mem/get-accum-filtered memory node (:matches token) bindings)] + (if (not= stored ::mem/no-accum-filtered) + stored + (vec (filter-accum-facts node token candidate-facts bindings join-filter-fn accum-condition))))) + ;; A specialization of the AccumulateNode that supports additional tests ;; that have to occur on the beta side of the network. The key difference between this and the simple ;; accumulate node is the join-filter-fn, which accepts a token and a fact and filters out facts that @@ -1473,16 +1483,13 @@ convert-return-fn (:convert-return-fn accumulator) grouped-candidate-facts (mem/get-accum-reduced-all memory node join-bindings)] - (doseq [token tokens] - (mem/remove-accum-filtered! memory node (:matches token))) - (cond (seq grouped-candidate-facts) (doseq [token tokens [fact-bindings candidate-facts] grouped-candidate-facts - :let [filtered-facts (filter-accum-facts node token candidate-facts fact-bindings join-filter-fn accum-condition)] + :let [filtered-facts (stored-or-filtered node memory token candidate-facts fact-bindings join-filter-fn accum-condition)] :when (or (seq filtered-facts) ;; Even if there no filtered facts, if there are no new bindings an initial value @@ -1512,7 +1519,11 @@ (retract-accumulated node accum-condition accumulator result-binding token converted-result join-bindings transport memory listener)))) - :else nil))) + :else nil) + + ;; Removed after the retraction above, which reads these filtered facts. + (doseq [token tokens] + (mem/remove-accum-filtered! memory node (:matches token))))) (get-join-keys [node] binding-keys) @@ -1557,7 +1568,7 @@ ;; since the final result is guaranteed to be the same. :when (seq new-filtered-facts) - :let [previous-filtered-facts (filter-accum-facts node token previous-candidates bindings join-filter-fn accum-condition) + :let [previous-filtered-facts (stored-or-filtered node memory token previous-candidates bindings join-filter-fn accum-condition) previous-accum-result-init (cond (seq previous-filtered-facts) @@ -1659,9 +1670,11 @@ (doseq [;; Get all of the previously matched tokens so we can retract and re-send them. token matched-tokens - :let [previous-facts (filter-accum-facts node token previous-candidates bindings join-filter-fn accum-condition) + :let [previous-facts (stored-or-filtered node memory token previous-candidates bindings join-filter-fn accum-condition) - new-facts (filter-accum-facts node token new-candidates bindings join-filter-fn accum-condition)] + ;; Equal facts give the same join filter result, so removing the retracted facts from the filtered facts + ;; gives the same facts, in the same order, as filtering new-candidates. + new-facts (second (mem/remove-first-of-each facts previous-facts))] ;; The previous matching elements are a superset of the matching elements after retraction. ;; Therefore, if the counts before and after are equal nothing retracted actually matched @@ -1674,9 +1687,7 @@ ;; elements before and after a right-retraction cannot be different. previous-result (do-accumulate accumulator previous-facts) - ;; TODO: Can we use the retract-fn here if present to improve performance? We'd also potentially - ;; avoid needing to filter facts twice above, since elements present both before and after retraction - ;; will given to the join-filter-fn twice (once when creating previous-facts and once when creating new-facts). + ;; TODO: Can we use the retract-fn here if present to improve performance? ;; Note that any future optimizations here must ensure that the result propagated here is equal to the result ;; that will be recreated as the previous result in right activate, and that this can be dependent on the order ;; of candidates in the memory, since, for example (acc/all) can return both [A B] and [B A] but these are not equal. @@ -1739,7 +1750,7 @@ ;; as-is to the join filter. ;; ;; A read-only session has no compiled join-filter-fn, so it cannot filter the facts again. - ;; The activation methods above store the filtered facts with mem/add-accum-filtered! each time they run the join filter. + ;; The activation methods above keep the filtered facts current with mem/add-accum-filtered!, and read them through stored-or-filtered. ;; This lookup uses them first. The key is the :matches of the token that entered this node. ;; That token's :matches is the prefix before this node's entry in the descendant token. ;; Sessions serialized before the filtered facts were stored have none, so the join filter runs again. diff --git a/src/main/clojure/clara/rules/memory.clj b/src/main/clojure/clara/rules/memory.clj index 24cce60..b6fea3d 100644 --- a/src/main/clojure/clara/rules/memory.clj +++ b/src/main/clojure/clara/rules/memory.clj @@ -51,7 +51,7 @@ ;; Returns the facts that passed the join filter of an accumulator node with a join filter. ;; left-matches are the :matches of the token that entered the node. ;; accum-bindings are the join bindings merged with the fact bindings. - ;; Returns ::no-accum-filtered when no facts are stored. These facts are used only for inspection. + ;; Returns ::no-accum-filtered when no facts are stored. The node and inspection read these facts. (get-accum-filtered [memory node left-matches accum-bindings]) ;; Returns insertions that occurred at the given node for the given token. @@ -858,7 +858,7 @@ (persistent-maps persistent-vals accum-filtered-memory))))) ;;; accum-filtered-memory holds the facts that passed the join filter of each accumulator node with a join filter. -;;; Only inspection reads it. The node replaces these facts each time it runs its join filter. +;;; The node and inspection read it. The node replaces these facts each time they change. ;;; It is nil in memory restored from sessions serialized before this field existed. (defrecord PersistentLocalMemory [rulebase activation-group-sort-fn diff --git a/src/test/clojure/clara/performance/test_rule_execution.clj b/src/test/clojure/clara/performance/test_rule_execution.clj index ed2c35d..443bf41 100644 --- a/src/test/clojure/clara/performance/test_rule_execution.clj +++ b/src/test/clojure/clara/performance/test_rule_execution.clj @@ -35,3 +35,49 @@ r/fire-rules) :iterations 5 :mean-assertion (partial > 10000)}))) + +(defrecord Threshold [v]) +(defrecord Temperature [temperature]) + +;; Each Threshold is a token in the AccumulateWithJoinFilterNode, and all Temperature facts share one binding group. +;; Inserting or retracting in small batches makes the node update the result of every token for each batch. +(def threshold-facts (mapv #(->Threshold (* 100 (inc %))) (range 10))) +(def temperature-batches (partition-all 10 (mapv ->Temperature (range 1000)))) + +(defn- run-accum-join-filter-perf + [session accumulator-name] + (let [thresholds-fired (-> session + (r/insert-all threshold-facts) + r/fire-rules) + all-fired (reduce (fn [s batch] + (r/fire-rules (r/insert-all s batch))) + thresholds-fired + temperature-batches)] + (run-performance-test {:description (str "AccumulateWithJoinFilterNode " accumulator-name " incremental insert") + :func #(reduce (fn [s batch] + (r/fire-rules (r/insert-all s batch))) + thresholds-fired + temperature-batches) + :iterations 5 + :verbose true + :mean-assertion (partial > 500)}) + (run-performance-test {:description (str "AccumulateWithJoinFilterNode " accumulator-name " incremental retract") + :func #(reduce (fn [s batch] + (r/fire-rules (apply r/retract s batch))) + all-fired + temperature-batches) + :iterations 5 + :verbose true + :mean-assertion (partial > 500)}))) + +(def-rules-test test-accum-join-filter-perf + {:rules [all-rule [[[Threshold (= ?v v)] + [?ts <- (acc/all) :from [Temperature (< temperature ?v)]]] + '(identity ?ts)] + count-rule [[[Threshold (= ?v v)] + [?n <- (acc/count) :from [Temperature (< temperature ?v)]]] + '(identity ?n)]] + :sessions [all-session [all-rule] {} + count-session [count-rule] {}]} + (run-accum-join-filter-perf all-session "acc/all") + (run-accum-join-filter-perf count-session "acc/count")) diff --git a/src/test/clojure/clara/test_durability.clj b/src/test/clojure/clara/test_durability.clj index 8731227..4070cfc 100644 --- a/src/test/clojure/clara/test_durability.clj +++ b/src/test/clojure/clara/test_durability.clj @@ -511,7 +511,25 @@ (->Temperature 20 "CHI")])}] (query old-ro-restored dr/temps-under-thresh))) (is (= [(conj (first expected) (->Temperature 50 "MCI"))] - (temps-under-threshold-accumulated old-ro-restored))))))) + (temps-under-threshold-accumulated old-ro-restored))))) + + (testing "a full session serialized without filtered facts gives the same results after changes" + ;; The node finds no stored filtered facts, so it runs the join filter. + (let [old-restored (with-redefs-fn {#'d/index-accum-filtered-memory (constantly nil)} + #(session-serde fired {})) + change (fn [s] + (-> s + (insert (->Temperature 45 "DEN") (->Temperature 60 "PHX")) + fire-rules + (retract (->Temperature 40 "LAX")) + fire-rules)) + changed (change fired) + old-changed (change old-restored)] + (is (= (frequencies (query changed dr/temps-under-thresh)) + (frequencies (query old-changed dr/temps-under-thresh)))) + (is (= [#{(->Temperature 45 "DEN") (->Temperature 30 "SAN") (->Temperature 20 "CHI")}] + (temps-under-threshold-accumulated changed) + (temps-under-threshold-accumulated old-changed))))))) (defn get-test-nodes [session]