Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure compiler condition comparison nil-safe #109

Merged
merged 1 commit into from
May 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/clojure/clara/rules/compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,16 @@
[left right]
(cond

;; Handle the nil cases first to ensure nil-safety.
(and (nil? left) (nil? right))
0

(nil? left)
-1

(nil? right)
1

;; Ignore functions for our comparison purposes,
;; since we won't distinguish rules by them.
(and (fn? left) (fn? right))
Expand Down
15 changes: 15 additions & 0 deletions src/test/clojure/clara/test_rules.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2485,3 +2485,18 @@

(is (= [{:?s "abc"}]
(query session find-string-substring)))))

(deftest test-condition-comparison-nil-safe
(let [q (dsl/parse-query []
;; Make two conditions that are very similar, but differ
;; where a nil will be compared to something else.
[[(accumulate :reduce-fn (fn [x y] nil)) :from [Temperature]]
[(accumulate :reduce-fn (fn [x y] 10)) :from [Temperature]]])
s (mk-session [q])]

;; Mostly just ensuring the rulebase was compiled successfully.
(is (== 3
(-> s
.rulebase
:id-to-node
count)))))