Skip to content

Commit

Permalink
Clarify that the counting is of activation group changes, not individ…
Browse files Browse the repository at this point in the history
…ual rule activations
  • Loading branch information
WilliamParker committed Jul 5, 2019
1 parent d5ade2b commit 13ef0df
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/main/clojure/clara/rules/engine.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1730,9 +1730,9 @@

(loop [next-group (mem/next-activation-group transient-memory)
last-group nil
flushed-updates-count 0]
group-change-count 0]

(when (> flushed-updates-count max-cycles)
(when (> group-change-count max-cycles)
(throw (ex-info
(str "It appears that the rules are in an infinite loop."
" Incorrect use of truth maintenance is a frequent source of such loops; see the website."
Expand All @@ -1757,7 +1757,7 @@
;; group before continuing.
(do
(flush-updates *current-session*)
(recur (mem/next-activation-group transient-memory) next-group (inc flushed-updates-count)))
(recur (mem/next-activation-group transient-memory) next-group (inc group-change-count)))

(do

Expand Down Expand Up @@ -1835,13 +1835,20 @@
(when (some-> node :production :props :no-loop)
(flush-updates *current-session*)))))

(recur (mem/next-activation-group transient-memory) next-group flushed-updates-count)))
;; If the activation group changed, then the updates will be flushed after the loop statement
;; and we will recur again, this time with the group-change-count incremented. It is expected
;; that changes between activation groups will be less numerous than each single activation
;; of a rule, and the former can remain constant even for large numbers of facts. That is,
;; it is more dependent on the logical structure of fact flow than the simple volume.
;; Therefore counting only upon activation groups changes will reduce the likelihood of a false
;; positive in infinite cycle detection.
(recur (mem/next-activation-group transient-memory) next-group group-change-count)))

;; There were no items to be activated, so flush any pending
;; updates and recur with a potential new activation group
;; since a flushed item may have triggered one.
(when (flush-updates *current-session*)
(recur (mem/next-activation-group transient-memory) next-group (inc flushed-updates-count)))))))
(recur (mem/next-activation-group transient-memory) next-group (inc group-change-count)))))))

(deftype LocalSession [rulebase memory transport listener get-alphas-fn pending-operations]
ISession
Expand Down

0 comments on commit 13ef0df

Please sign in to comment.