Skip to content

Commit

Permalink
Allow reasoning with ledger names as rule sources
Browse files Browse the repository at this point in the history
Only works with query-connection since you need a conn to get a db
from a ledger name.
  • Loading branch information
JaceRockman committed Jun 19, 2024
1 parent afd46fc commit f49322c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
28 changes: 16 additions & 12 deletions src/clj/fluree/db/api/query.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(:require [clojure.core.async :as async]
[clojure.string :as str]
[fluree.json-ld :as json-ld]
[fluree.db.db.json-ld :as db]
[fluree.db.fuel :as fuel]
[fluree.db.ledger.json-ld :as jld-ledger]
[fluree.db.ledger :as ledger]
Expand Down Expand Up @@ -45,14 +46,11 @@
time-travel-db (-> (if t
(<? (time-travel/as-of policy-db t))
policy-db))
reasoned-db (let [{:keys [reasoners reasoner-rules reasoner-rules-db]} opts]
(if reasoners
;; Currently we only support one rule source, so we take the first db or first
;; reason graph that we find.
reasoned-db (let [{:keys [reasoner-methods rule-graphs rule-dbs] :as reasoning} opts]
(if reasoner-methods
(<? (reasoner/reason time-travel-db
reasoners
reasoner-rules
reasoner-rules-db
reasoner-methods
reasoning
opts))
time-travel-db))]
(assoc-in reasoned-db [:policy :cache] (atom {})))))
Expand Down Expand Up @@ -161,6 +159,15 @@
parse-t-val)]
[alias nil])))

(defn parse-rule-dbs
[conn dbs-or-aliases]
(map (fn [db-or-alias]
(cond
(db/db? db-or-alias) db-or-alias
(string? db-or-alias) (ledger/-db (clojure.core.async/<!! (jld-ledger/load conn db-or-alias)))
:else (throw "Invalid rule db provided. Must be a db object or a string of the ledger name.")))
dbs-or-aliases))

(defn load-alias
[conn alias t context opts]
(go-try
Expand All @@ -170,11 +177,8 @@
ledger (<? (jld-ledger/load conn address))
db (ledger/-db ledger)
t* (or explicit-t t)
rules-db (let [dbs-or-aliases (:reasoner-rules-db opts)]
(if (string? (first dbs-or-aliases))
[(ledger/-db (<? (jld-ledger/load conn (first dbs-or-aliases))))]
dbs-or-aliases))
opts* (assoc opts :reasoner-rules-db rules-db)]
rule-dbs (parse-rule-dbs conn (:rule-dbs opts))
opts* (assoc opts :rule-dbs rule-dbs)]
(<? (restrict-db db t* context opts*)))
(catch* e
(throw (contextualize-ledger-400-error
Expand Down
22 changes: 12 additions & 10 deletions src/clj/fluree/db/reasoner.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,22 @@
dbs)))

(defn all-rules
[methods db inserts rules-graphs rules-dbs]
[methods db inserts rule-graphs rule-dbs]
(go-try
(let [parsed-rules-graphs (try*
(map parse-rules-graph rules-graphs)
(let [parsed-rule-graphs (try*
(map parse-rules-graph rule-graphs)
(catch* e
(log/error "Error parsing supplied rules graph:" e)
(throw e)))
all-rules-from-graphs (apply concat
(for [method methods]
(mapcat (fn [parsed-rules-graph]
(rules-from-graph method inserts parsed-rules-graph))
parsed-rules-graphs)))
all-rules-dbs (if (or (nil? rules-dbs) (empty? rules-dbs))
parsed-rule-graphs)))
all-rule-dbs (if (or (nil? rule-dbs) (empty? rule-dbs))
[db]
(conj rules-dbs db))
all-rules-from-dbs (apply concat (rules-from-dbs methods inserts all-rules-dbs))
(conj rule-dbs db))
all-rules-from-dbs (apply concat (rules-from-dbs methods inserts all-rule-dbs))
all-rules (concat all-rules-from-graphs all-rules-from-dbs)]
all-rules)))

Expand Down Expand Up @@ -225,16 +225,18 @@
db*)))))

(defn reason
[db methods rules-graphs rules-dbs {:keys [max-fuel reasoner-max]
:or {reasoner-max 10} :as _opts}]
[db methods
{:keys [rule-graphs rule-dbs]}
{:keys [max-fuel reasoner-max]
:or {reasoner-max 10} :as _opts}]
(go-try
(let [methods* (set (util/sequential methods))
fuel-tracker (fuel/tracker max-fuel)
db* (update db :reasoner #(into methods* %))
tx-state (db/->tx-state :db db*)
inserts (atom nil)
;; TODO - rules can be processed in parallel
raw-rules (<? (all-rules methods* db* inserts rules-graphs rules-dbs))
raw-rules (<? (all-rules methods* db* inserts rule-graphs rule-dbs))
_ (log/debug "Reasoner - extracted rules: " raw-rules)
reasoning-rules (->> raw-rules
(resolve/rules->graph db*)
Expand Down

0 comments on commit f49322c

Please sign in to comment.