Skip to content

Instantly share code, notes, and snippets.

@skuro
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save skuro/10755103 to your computer and use it in GitHub Desktop.

Select an option

Save skuro/10755103 to your computer and use it in GitHub Desktop.
Testing clara rules and queries over POJOs
package tk.skuro;
public class Bean {
private final String _value;
public Bean(String value) {
_value = value;
}
public String getValue() {
return _value;
}
}
(import tk.skuro.Bean)
(deftest a-test
(testing "Clara rule and query over POJOs"
(let [;; to be used as a starting fact
bean1 (Bean. "Initial")
;; to be inserted into the working memory by a matching rule
bean2 (Bean. "Derived")
rule ;; matches a Bean and inserts a new one
(dsl/parse-rule [[Bean [b] (= "Initial" (.getValue b))
(insert! (Bean. "Derived")))
result ;; should find the Bean inserted by the rule
(dsl/parse-query []
[[Bean [c] (= "Derived" (.getValue b))]])
session ;; creates a fresh session, inserts the initial Bean then fires the rules
(-> (mk-session [rule result])
(insert bean1)
(fire-rules))]
(is (= #{bean2} (set (query session result))))))) ;; (not (= #{#<Bean tk.skuro.Bean@1b0e0936>} #{{}}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment