-
Notifications
You must be signed in to change notification settings - Fork 115
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
Issue 436: Add new node sharing test namespace #439
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
55751fe
Issue 436: Add new node sharing test namespace
WilliamParker a1899a7
Correct cljs ns name
WilliamParker a9ebb00
Eliminate extra whitespace
WilliamParker f759992
Add namespace to ClojureScript test runner
WilliamParker b21783f
Merge branch 'issue436pr' of https://github.com/WilliamParker/clara-r…
WilliamParker 77de880
Correct cljs namespace header
WilliamParker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,303 @@ | ||
#?(:clj | ||
(ns clara.test-node-sharing | ||
(:require [clara.tools.testing-utils :refer [def-rules-test] :as tu] | ||
[clara.rules :refer [fire-rules | ||
insert | ||
insert-all | ||
insert-unconditional! | ||
insert! | ||
retract | ||
query]] | ||
|
||
[clara.rules.testfacts :refer [->Temperature ->Cold ->WindSpeed | ||
->ColdAndWindy]] | ||
[clojure.test :refer [is deftest run-tests testing use-fixtures]] | ||
[clara.rules.accumulators] | ||
[schema.test :as st]) | ||
(:import [clara.rules.testfacts | ||
Temperature | ||
Cold | ||
WindSpeed | ||
ColdAndWindy | ||
WindSpeed])) | ||
|
||
:cljs | ||
(ns clara.test-node-sharing | ||
(:require [clara.rules :refer [fire-rules | ||
insert | ||
insert! | ||
insert-all | ||
insert-unconditional! | ||
retract | ||
query]] | ||
[clara.rules.testfacts :refer [->Temperature Temperature | ||
->Cold Cold | ||
->WindSpeed WindSpeed | ||
->ColdAndWindy ColdAndWindy]] | ||
[clara.rules.accumulators] | ||
[cljs.test] | ||
[schema.test :as st]) | ||
(:require-macros [clara.tools.testing-utils :refer [def-rules-test]] | ||
[cljs.test :refer [is deftest run-tests testing use-fixtures]]))) | ||
|
||
(use-fixtures :once st/validate-schemas #?(:clj tu/opts-fixture) tu/side-effect-holder-fixture) | ||
|
||
;; See issue 433 for more information | ||
(def-rules-test test-or-sharing-same-condition | ||
{:rules [or-rule [[[:or | ||
[::a] | ||
[::b]] | ||
[::d]] | ||
(insert! {:fact-type ::c})] | ||
|
||
other-rule [[[::a] | ||
[::d]] | ||
(insert! {:fact-type ::e})]] | ||
:queries [c-query [[] [[?c <- ::c]]] | ||
e-query [[] [[?e <- ::e]]]] | ||
|
||
:sessions [empty-session [or-rule other-rule c-query e-query] {:fact-type-fn :fact-type}]} | ||
(let [session (-> empty-session | ||
(insert {:fact-type ::b}) | ||
(insert {:fact-type ::d}) | ||
fire-rules)] | ||
|
||
(is (= (set (query session c-query)) | ||
#{{:?c {:fact-type ::c}}})) | ||
|
||
(is (= (set (query session e-query)) | ||
#{})))) | ||
|
||
;; Replicate the same logical scenario as above in test-or-sharing-same-condition | ||
;; in a single rule with a boolean :or condition. | ||
(def-rules-test test-or-sharing-same-condition-in-same-production | ||
{:rules [single-rule [[[:or | ||
[:and [:or | ||
[::a] | ||
[::b]] | ||
[::d]] | ||
[:and | ||
[::a] | ||
[::d]]]] | ||
|
||
(insert! {:fact-type ::c})]] | ||
|
||
:queries [c-query [[] [[?c <- ::c]]]] | ||
|
||
:sessions [empty-session [single-rule c-query] {:fact-type-fn :fact-type}]} | ||
|
||
(let [session (-> empty-session | ||
(insert {:fact-type ::b}) | ||
(insert {:fact-type ::d}) | ||
fire-rules)] | ||
|
||
(is (= (query session c-query) | ||
[{:?c {:fact-type ::c}}])))) | ||
|
||
|
||
;; FIXME: Log an issue for this bug and uncomment when it is resolved. Since an :or | ||
;; condition is essentially creating two rules I'd expect this to insert twice. | ||
(comment | ||
(def-rules-test test-or-sharing-same-condition-in-same-production-duplicate-path | ||
{:rules [single-rule [[[:or | ||
[:and [:or | ||
[::a] | ||
[::b]] | ||
[::d] | ||
] | ||
[:and | ||
[::a] | ||
[::d]]]] | ||
|
||
(insert! {:fact-type ::c})]] | ||
|
||
:queries [c-query [[] [[?c <- ::c]]]] | ||
|
||
:sessions [empty-session [single-rule c-query] {:fact-type-fn :fact-type}]} | ||
|
||
(let [session (-> empty-session | ||
(insert {:fact-type ::a}) | ||
(insert {:fact-type ::d}) | ||
fire-rules)] | ||
|
||
(is (= (query session c-query) | ||
[{:?c {:fact-type ::c}} | ||
{:?c {:fact-type ::c}}]))))) | ||
|
||
(def-rules-test test-or-sharing-same-condition-in-same-production-beginning-duplicate-path | ||
{:rules [single-rule [[[:or | ||
[:and | ||
[:or | ||
[::a] | ||
[::b]] | ||
[::d] | ||
[::e]] | ||
[:and | ||
[::a] | ||
[::d] | ||
[::f]]]] | ||
|
||
(insert! {:fact-type ::c})]] | ||
|
||
:queries [c-query [[] [[?c <- ::c]]]] | ||
|
||
:sessions [empty-session [single-rule c-query] {:fact-type-fn :fact-type}]} | ||
|
||
(let [session (-> empty-session | ||
(insert {:fact-type ::a}) | ||
(insert {:fact-type ::d}) | ||
(insert {:fact-type ::e}) | ||
(insert {:fact-type ::f}) | ||
fire-rules)] | ||
|
||
(is (= (query session c-query) | ||
[{:?c {:fact-type ::c}} | ||
{:?c {:fact-type ::c}}])))) | ||
|
||
(def-rules-test test-or-sharing-same-condition-in-same-production-ending-duplicate-path | ||
{:rules [single-rule [[[:or | ||
[:and | ||
[::e] | ||
[:or | ||
[::a] | ||
[::b]] | ||
[::d]] | ||
[:and | ||
[::f] | ||
[::a] | ||
[::d]]]] | ||
|
||
(insert! {:fact-type ::c})]] | ||
|
||
:queries [c-query [[] [[?c <- ::c]]]] | ||
|
||
:sessions [empty-session [single-rule c-query] {:fact-type-fn :fact-type}]} | ||
|
||
(let [session (-> empty-session | ||
(insert {:fact-type ::a}) | ||
(insert {:fact-type ::d}) | ||
(insert {:fact-type ::e}) | ||
(insert {:fact-type ::f}) | ||
fire-rules)] | ||
|
||
(is (= (query session c-query) | ||
[{:?c {:fact-type ::c}} | ||
{:?c {:fact-type ::c}}])))) | ||
|
||
(def-rules-test test-or-sharing-same-condition-with-different-intervening-conditions | ||
|
||
{:rules [or-rule [[[:or | ||
[::a] | ||
[::b]] | ||
[::f] | ||
[::d]] | ||
(insert! {:fact-type ::c})] | ||
|
||
other-rule [[[::a] | ||
[::g] | ||
[::d]] | ||
(insert! {:fact-type ::e})]] | ||
:queries [c-query [[] [[?c <- ::c]]] | ||
e-query [[] [[?e <- ::e]]]] | ||
|
||
:sessions [empty-session [or-rule other-rule c-query e-query] {:fact-type-fn :fact-type}]} | ||
|
||
(let [session (-> empty-session | ||
(insert {:fact-type ::b}) | ||
(insert {:fact-type ::d}) | ||
(insert {:fact-type ::f}) | ||
fire-rules)] | ||
|
||
(is (= (set (query session c-query)) | ||
#{{:?c {:fact-type ::c}}})) | ||
|
||
(is (= (set (query session e-query)) | ||
#{})))) | ||
|
||
|
||
(def-rules-test test-sharing-simple-condition-followed-by-or | ||
|
||
{:rules [cold-and-windy-rule [[[WindSpeed (> windspeed 20) (do (swap! tu/side-effect-holder inc) | ||
true)] | ||
[:or | ||
[Cold] | ||
[Temperature (< temperature 0)]]] | ||
(insert! (->ColdAndWindy nil nil))]] | ||
|
||
:queries [cold-windy-query [[] [[?cw <- ColdAndWindy]]]] | ||
|
||
:sessions [empty-session [cold-and-windy-rule cold-windy-query] {}]} | ||
|
||
(reset! tu/side-effect-holder 0) | ||
(is (= (-> empty-session | ||
(insert (->WindSpeed 30 "LCY") | ||
(->Cold -20)) | ||
fire-rules | ||
(query cold-windy-query)) | ||
[{:?cw (->ColdAndWindy nil nil)}])) | ||
(is (= @tu/side-effect-holder 1) "The constraints on WindSpeed should only be evaluated once")) | ||
|
||
(def-rules-test test-shared-first-condition-multiple-rules-met | ||
|
||
{:rules [cold-fact-rule [[[WindSpeed (> windspeed 20) (do (swap! tu/side-effect-holder inc) | ||
true)] | ||
[Cold]] | ||
(insert! (->ColdAndWindy nil nil))] | ||
|
||
temp-fact-rule [[[WindSpeed (> windspeed 20) (do (swap! tu/side-effect-holder inc) | ||
true)] | ||
[Temperature (< temperature 0)]] | ||
(insert! (->ColdAndWindy nil nil))]] | ||
|
||
:queries [cold-windy-query [[] [[?cw <- ColdAndWindy]]]] | ||
|
||
:sessions [empty-session [cold-fact-rule temp-fact-rule cold-windy-query] {}]} | ||
|
||
(reset! tu/side-effect-holder 0) | ||
(is (= (-> empty-session | ||
(insert (->WindSpeed 30 "LCY") | ||
(->Cold -20)) | ||
fire-rules | ||
(query cold-windy-query)) | ||
[{:?cw (->ColdAndWindy nil nil)}])) | ||
(is (= @tu/side-effect-holder 1) "The constraints on WindSpeed should only be evaluated once")) | ||
|
||
(def-rules-test test-shared-second-condition-multiple-rules-unmet | ||
|
||
{:rules [cold-fact-rule [[[::absent-type-1] | ||
[WindSpeed (> windspeed 20) (do (swap! tu/side-effect-holder inc) | ||
true)] | ||
[Cold]] | ||
(insert! (->ColdAndWindy nil nil))] | ||
|
||
temp-fact-rule [[[::absent-type-2] | ||
[WindSpeed (> windspeed 20) (do (swap! tu/side-effect-holder inc) | ||
true)] | ||
[Temperature (< temperature 0)]] | ||
(insert! (->ColdAndWindy nil nil))]] | ||
|
||
:queries [cold-windy-query [[] [[?cw <- ColdAndWindy]]]] | ||
|
||
:sessions [empty-session [cold-fact-rule temp-fact-rule cold-windy-query] {}]} | ||
|
||
(reset! tu/side-effect-holder 0) | ||
(is (empty? (-> empty-session | ||
(insert (->WindSpeed 30 "LCY") | ||
(->Cold -20)) | ||
fire-rules | ||
(query cold-windy-query)))) | ||
;; Note that even though the parent condition isn't met, the WindSpeed will still be evaluated | ||
;; to determine if it meets the constraints since it isn't dependent on the parent condition. | ||
;; It is possible that this would be change in the future if we decided to use lazy evaluation, | ||
;; but in any case it should not be more than 1. | ||
(is (= @tu/side-effect-holder 1) "The constraints on WindSpeed should be evaluated exactly once")) | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Todo: eliminate this whitespace. |
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fails both before and after #434 so I don't think it is a regression.