Skip to content

Commit

Permalink
feat: linter updates and add defdata macro
Browse files Browse the repository at this point in the history
  • Loading branch information
k13gomez committed Mar 10, 2024
1 parent 06420d6 commit 6c87786
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
This is a history of changes to k13labs/clara-rules.

# 1.4.0-SNAPSHOT
* include linter fixes for `defrule` and `defquery` which were not processing docstrings correctly.
* `defrule` now defines rules as functions with two arities, no args returns the rule map, and 2 args is the compiled RHS.
* `clojure.lang.Fn` now implements `clara.rules.compiler/IRuleSource`, and returns a single rule by invoking the like `(a-rule)`.
* add built-in support to serialize `clojure.lang.Var` so that a rule handler var can be serialized correctly.
* add function `clara.rules.compiler/load-rules-from-source` to simplify loading rules
* add `defhierarchy` macro to define hierarchies of facts allowing to easily establish parent/child relationships.
* add `defdata` macro to define facts and collections of facts in namespaces allowing to easily embed and insert them during mk-session.
* rename `clear-ns-productions!` to `clear-ns-vars!` since now there are ns-installed vars that are not productions.

# 1.3.2
Expand Down
1 change: 1 addition & 0 deletions clj-kondo/clj-kondo.exports/clara/rules/config.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{:lint-as {clara.rules/defsession clojure.core/def
clara.rules/defhierarchy clojure.core/def
clara.rules/defdata clojure.core/def
clara.rules.platform/eager-for clojure.core/for
clara.rules.platform/compute-for clojure.core/for}
:hooks {:analyze-call {clara.rules/defquery hooks.clara-rules/analyze-defquery-macro
Expand Down
14 changes: 8 additions & 6 deletions dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
quick-benchmark] :as crit]
[clara.rules.platform :refer [compute-for]]
[clojure.core.async :refer [go timeout <!]]
[clara.rules :refer [defrule defquery defhierarchy insert!
insert insert-all fire-rules query
[clara.rules :refer [defrule defquery defdata defhierarchy
insert! insert insert-all fire-rules query
mk-session clear-ns-vars!
derive! underive!]]
[clara.rules.compiler :as com]
Expand Down Expand Up @@ -37,13 +37,15 @@
[]
[?output <- :thing/result])

(def ^:fact foo
(defdata foo
{:type :thing/foo
:value 1})

(def ^:fact bar
{:type :thing/bar
:value 2})
(defdata bar
[{:type :thing/bar
:value 2}
{:type :thing/bar
:value 3}])

(time
(-> (mk-session 'user :fact-type-fn :type)
Expand Down
11 changes: 11 additions & 0 deletions src/main/clojure/clara/rules.clj
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,17 @@
(binding [hierarchy/*hierarchy* (atom (hierarchy/make-hierarchy))]
~@body))))

(defmacro defdata
"Defines a data fact which is stored in the given var. For instance, the following fact is simply a
map which is then inserted into the session when the namespace is loaded.
(defdata default-temperature
(Cold. 32))"
[name & body]
(let [doc (if (string? (first body)) (first body) nil)]
`(def ~(vary-meta name assoc :fact true :doc doc)
~@body)))

(defmacro clear-ns-vars!
"Ensures that any rule/query definitions which have been cached will be cleared from the associated namespace.
Rule and query definitions can be cached such that if their definitions are not explicitly overwritten with the same
Expand Down

0 comments on commit 6c87786

Please sign in to comment.