-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue446: Bean accessors don't account for IndexedPropertyDescriptors (…
- Loading branch information
1 parent
1bafa05
commit bda21fe
Showing
4 changed files
with
120 additions
and
33 deletions.
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
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
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,50 @@ | ||
(ns clara.test-java-facts | ||
(:require [clara.tools.testing-utils :as tu] | ||
[clara.rules :as rules] | ||
[clojure.test :refer [is deftest run-tests testing use-fixtures]] | ||
[clara.rules.compiler :as com]) | ||
(:import [clara.test.facts | ||
BeanTestFact])) | ||
|
||
;; A test to demonstrate that a Pojo with indexed property accessors can be used as an alpha root in a session, | ||
;; see https://github.com/cerner/clara-rules/issues/446 | ||
(tu/def-rules-test test-basic-rule | ||
{:rules [kansas-rule [[[BeanTestFact | ||
(= ?locs locations) | ||
(some #(= "Kansas" %) ?locs)]] | ||
(rules/insert! "Kansas Exists")]] | ||
:queries [string-query [[] [[?s <- String]]]] | ||
|
||
:sessions [empty-session [kansas-rule string-query] {}]} | ||
|
||
(let [locs (doto (make-array String 2) | ||
(aset 0 "Florida") | ||
(aset 1 "Kansas"))] | ||
(let [session-strings (map :?s | ||
(-> empty-session | ||
(rules/insert (BeanTestFact. locs)) | ||
(rules/fire-rules) | ||
(rules/query string-query)))] | ||
(is (= ["Kansas Exists"] session-strings))))) | ||
|
||
;; Using an indexed property accessor that doesn't have a standard accessor will throw exception as clara cannot resolve | ||
;; the usage of the accessor. See https://github.com/cerner/clara-rules/issues/446 | ||
(deftest test-indexed-property-accessor | ||
(let [rule-using-unsupported-accessor {:ns-name (ns-name *ns*) | ||
:lhs [{:type BeanTestFact :constraints [`(contains? ~'roadConditions "Slippery")]}] | ||
:rhs `(rules/insert! "doesn't matter") | ||
:name "rule-using-unsupported-accessor"}] | ||
(try | ||
(com/mk-session [[rule-using-unsupported-accessor]]) | ||
(is false "An exception should be thrown") | ||
(catch Exception e | ||
(loop [exc e] | ||
(cond | ||
(re-find #"Failed compiling alpha node" (.getMessage exc)) | ||
:success | ||
|
||
(.getCause exc) | ||
(recur (.getCause exc)) | ||
|
||
:else | ||
(is false "Exception didn't contain a message containing alpha node failure"))))))) |
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,39 @@ | ||
package clara.test.facts; | ||
|
||
/** | ||
* A Java Pojo for the express purpose of testing the behavior of compilation and execution of rules. | ||
* | ||
* This class should not be included in the released artifact, if it did make it into a released artifact it should be | ||
* ignored and not consumed as it may be moved/removed without warning to consumers. | ||
*/ | ||
public class BeanTestFact { | ||
private String[] locations; | ||
private String[] roadConditions; | ||
|
||
public BeanTestFact(String[] locations) { | ||
this.locations = locations; | ||
} | ||
|
||
// Standard and Indexed property accessors | ||
public void setLocations(String[] locations) { | ||
this.locations = locations; | ||
} | ||
public String[] getLocations() { | ||
return locations; | ||
} | ||
public void setLocations(int pos, String location) { | ||
locations[pos] = location; | ||
} | ||
public String getLocations(int pos){ | ||
return locations[pos]; | ||
} | ||
|
||
// Partial Indexed property accessor, ie. no standard accessor | ||
// See https://github.com/cerner/clara-rules/issues/446 for more details | ||
public void setRoadConditions(int pos, String condition) { | ||
roadConditions[pos] = condition; | ||
} | ||
public String getRoadConditions(int pos){ | ||
return roadConditions[pos]; | ||
} | ||
} |