-
Notifications
You must be signed in to change notification settings - Fork 115
/
java.clj
52 lines (42 loc) · 1.62 KB
/
java.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(ns clara.rules.java
"This namespace is for internal use and may move in the future.
Java support. Users should use the Java API, or the clara.rules namespace from Clojure."
(:require [clara.rules :as clara]
[clara.rules.engine :as eng]
[clara.rules.compiler :as com]
[clara.rules.memory :as mem])
(:refer-clojure :exclude [==])
(:import [clara.rules.engine LocalTransport]
[clara.rules WorkingMemory QueryResult]))
(deftype JavaQueryResult [result]
QueryResult
(getResult [_ fieldName]
(get result (keyword fieldName)))
Object
(toString [_]
(.toString result)))
(defn- run-query [session name args]
(let [query-var (or (resolve (symbol name))
(throw (IllegalArgumentException.
(str "Unable to resolve symbol to query: " name))))
;; Keywordize string keys from Java.
keyword-args (into {}
(for [[k v] args]
[(keyword k) v]))
results (eng/query session (deref query-var) keyword-args)]
(map #(JavaQueryResult. %) results)))
(deftype JavaWorkingMemory [session]
WorkingMemory
(insert [this facts]
(JavaWorkingMemory. (apply clara/insert session facts)))
(retract [this facts]
(JavaWorkingMemory. (apply clara/retract session facts)))
(fireRules [this]
(JavaWorkingMemory. (clara/fire-rules session)))
(query [this name args]
(run-query session name args))
(query [this name]
(run-query session name {})))
(defn mk-java-session [rulesets]
(JavaWorkingMemory.
(com/mk-session (map symbol rulesets))))