Skip to content

Commit

Permalink
feat: implement hierarchy loaders, fact loaders, update deps (#2)
Browse files Browse the repository at this point in the history
* wip: hierarchy work in progress

* feat: more progress implementing def'd hierarchies and facts

* fix: fix rule loading tests and fact loading handling of functions

* chore: update deps

* feat: linter updates and add defdata macro
  • Loading branch information
k13gomez authored Mar 10, 2024
1 parent bee29ff commit b92f791
Show file tree
Hide file tree
Showing 10 changed files with 397 additions and 75 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
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
* Enhance memory add-activations implementation by replacing get/set with compute!
Expand Down
2 changes: 2 additions & 0 deletions clj-kondo/clj-kondo.exports/clara/rules/config.edn
Original file line number Diff line number Diff line change
@@ -1,4 +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
34 changes: 15 additions & 19 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@
:deps/prep-lib {:alias :build
:fn compile-main-java
:ensure "target/main/classes"}
:deps {org.clojure/clojure {:mvn/version "1.10.3"}
org.clojure/core.cache {:mvn/version "1.0.225"}
:deps {org.clojure/clojure {:mvn/version "1.11.2"}
org.clojure/core.cache {:mvn/version "1.1.234"}
org.clj-commons/digest {:mvn/version "1.4.100"}
com.github.k13labs/futurama {:mvn/version "1.0.2"}
com.cnuernber/ham-fisted {:mvn/version "2.016"}
com.github.k13labs/futurama {:mvn/version "1.0.3"}
com.cnuernber/ham-fisted {:mvn/version "2.017"}
prismatic/schema {:mvn/version "1.4.1"}
org.clojure/data.fressian {:mvn/version "1.0.0"}}
org.clojure/data.fressian {:mvn/version "1.1.0"}}

;for more examples of aliases see: https://github.com/seancorfield/dot-clojure
:aliases
{:build {:paths ["tool"]
:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"}}
:deps {io.github.clojure/tools.build {:mvn/version "0.10.0"}}
:ns-default build}
:dev {:extra-paths ["dev"]
:extra-deps {reloaded.repl/reloaded.repl {:mvn/version "0.2.4"}
org.clojure/math.combinatorics {:mvn/version "0.1.3"}
org.clojure/math.combinatorics {:mvn/version "0.3.0"}
criterium/criterium {:mvn/version "0.4.6"}}}

:clj-kondo {:extra-deps {clj-kondo/clj-kondo {:mvn/version "2023.12.15"}}
:clj-kondo {:extra-deps {clj-kondo/clj-kondo {:mvn/version "2024.03.05"}}
:main-opts ["-m" "clj-kondo.main"]}

:test {:extra-paths ["src/test/clojure" "target/test/classes"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.80.1274"}
:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"}
lambdaisland/kaocha-junit-xml {:mvn/version "1.17.101"}
lambdaisland/kaocha-cloverage {:mvn/version "1.1.89"}
org.clojure/test.check {:mvn/version "1.1.1"}
pjstadig/humane-test-output {:mvn/version "0.10.0"}}}
pjstadig/humane-test-output {:mvn/version "0.11.0"}}}

:runner {:main-opts ["-e" "(println \"warn-on-reflection =\" (set! *warn-on-reflection* true))"
"-m" "kaocha.runner"]
:exec-fn kaocha.runner/exec-fn}


:repl {:extra-deps {nrepl/nrepl {:mvn/version "1.1.0"}
cider/cider-nrepl {:mvn/version "0.44.0"}}
:repl {:extra-deps {nrepl/nrepl {:mvn/version "1.1.1"}
cider/cider-nrepl {:mvn/version "0.46.0"}}
:main-opts ["-e" "(println \"warn-on-reflection =\" (set! *warn-on-reflection* true))"
"-m" "nrepl.cmdline" "--interactive"
"--middleware" "[\"cider.nrepl/cider-middleware\"]"]}
Expand All @@ -47,20 +47,16 @@
:format-check {:extra-deps {cljfmt/cljfmt {:mvn/version "0.9.2"}}
:main-opts ["-m" "cljfmt.main" "check" "src" "dev"]}

:outdated {:extra-deps {olical/depot {:mvn/version "2.3.0"}
rewrite-clj/rewrite-clj {:mvn/version "0.6.1"}}
:main-opts ["-m" "depot.outdated.main"]}

:jar {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.0.216"}}
:jar {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.303"}}
:exec-fn hf.depstar/jar
:exec-args {:jar "build/clara-rules.jar"}}

:install-maven {:extra-deps {slipset/deps-deploy {:mvn/version "0.1.5"}}
:install-maven {:extra-deps {slipset/deps-deploy {:mvn/version "0.2.2"}}
:exec-fn deps-deploy.deps-deploy/deploy
:exec-args {:installer :local
:artifact "build/clara-rules.jar"}}

:deploy-maven {:replace-deps {slipset/deps-deploy {:mvn/version "0.1.5"}}
:deploy-maven {:replace-deps {slipset/deps-deploy {:mvn/version "0.2.2"}}
:exec-fn deps-deploy.deps-deploy/deploy
:exec-args {:installer :remote
:artifact "build/clara-rules.jar"}}}
Expand Down
42 changes: 39 additions & 3 deletions dev/user.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
(ns user
(:refer-clojure :exclude [derive underive])
(:require [criterium.core :refer [report-result
quick-benchmark] :as crit]
[clara.rules.platform :refer [compute-for]]
[clojure.core.async :refer [go timeout <!]]
[clara.rules :refer [defrule mk-session clear-ns-productions!]]
[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]
[clojure.core.cache.wrapped :as cache]
[schema.core :as sc]
Expand All @@ -12,10 +16,42 @@
(:import [java.util.concurrent CompletableFuture]))

(comment
(clear-ns-vars!)
(add-tap #'println)
(remove-tap #'println)
(tap> "foobar"))

(defhierarchy foobar
(derive! :thing/foo :thing/that)
(doseq [x (range 20)]
(derive! [:thing/foo (- 20 x)] [:thing/that (- 20 x)]))
(derive! :thing/bar :thing/that))

(defrule return-a-thing
[:thing/that [{:keys [value]}] (= value ?value)]
=>
(insert! {:type :thing/result
:value ?value}))

(defquery query-a-thing
[]
[?output <- :thing/result])

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

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

(time
(-> (mk-session 'user :fact-type-fn :type)
(fire-rules)
(query query-a-thing)))

(def session-cache
(cache/lru-cache-factory {}))

Expand Down Expand Up @@ -69,7 +105,7 @@
~@fact-rules))))

(comment
(clear-ns-productions!)
(clear-ns-vars!)
(mk-types 2500)
(def rules
(mk-rules 2500))
Expand All @@ -81,7 +117,7 @@

(time
(mk-session 'user [(conj rules {:ns-name (ns-name *ns*)
:lhs [{:type :foobar16
:lhs [{:type :foobar1
:constraints []}]
:rhs `(println ~(str :foobar))})]
:cache session-cache
Expand Down
34 changes: 17 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,37 @@
<dependency>
<groupId>org.clojure</groupId>
<artifactId>clojure</artifactId>
<version>1.10.3</version>
<version>1.11.2</version>
</dependency>
<dependency>
<groupId>com.cnuernber</groupId>
<artifactId>ham-fisted</artifactId>
<version>2.017</version>
</dependency>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>core.cache</artifactId>
<version>1.0.225</version>
<artifactId>data.fressian</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.clj-commons</groupId>
<artifactId>digest</artifactId>
<version>1.4.100</version>
<groupId>prismatic</groupId>
<artifactId>schema</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>com.github.k13labs</groupId>
<artifactId>futurama</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.cnuernber</groupId>
<artifactId>ham-fisted</artifactId>
<version>2.016</version>
</dependency>
<dependency>
<groupId>prismatic</groupId>
<artifactId>schema</artifactId>
<version>1.4.1</version>
<groupId>org.clj-commons</groupId>
<artifactId>digest</artifactId>
<version>1.4.100</version>
</dependency>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>data.fressian</artifactId>
<version>1.0.0</version>
<artifactId>core.cache</artifactId>
<version>1.1.234</version>
</dependency>
</dependencies>
<build>
Expand Down
Loading

0 comments on commit b92f791

Please sign in to comment.