Skip to content

Commit

Permalink
Aggregate rules from graphs and dbs when reasoning
Browse files Browse the repository at this point in the history
  • Loading branch information
JaceRockman committed Jun 18, 2024
1 parent 61a21eb commit afd46fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/clj/fluree/db/api/query.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
;; reason graph that we find.
(<? (reasoner/reason time-travel-db
reasoners
(or (first reasoner-rules-db)
(first reasoner-rules))
reasoner-rules
reasoner-rules-db
opts))
time-travel-db))]
(assoc-in reasoned-db [:policy :cache] (atom {})))))
Expand Down
52 changes: 29 additions & 23 deletions src/clj/fluree/db/reasoner.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -146,28 +146,34 @@
(log/debug "Reasoner - source OWL rules: " graph)
(owl-datalog/owl->datalog inserts graph)))

(defn rules-from-dbs
[methods inserts dbs]
(for [method methods]
(mapcat (fn [db]
(as-> db $
(clojure.core.async/<!! (resolve/rules-from-db $ method))
(rules-from-graph method inserts $)))
dbs)))

(defn all-rules
"Gets all relevant rules for the specified methods from the
supplied rules graph or from the db if no graph is supplied."
[methods db inserts graph-or-db]
[methods db inserts rules-graphs rules-dbs]
(go-try
(let [rules-db (cond
(nil? graph-or-db) db
(db? graph-or-db) graph-or-db)
supplied-rules* (when-not rules-db
(try*
(parse-rules-graph graph-or-db)
(catch* e
(log/error "Error parsing supplied rules graph:" e)
(throw e))))]
(loop [[method & r] methods
rules []]
(if method
(let [rules-graph* (or supplied-rules*
(<? (resolve/rules-from-db rules-db method)))
rules* (rules-from-graph method inserts rules-graph*)]
(recur r (into rules rules*)))
rules)))))
(let [parsed-rules-graphs (try*
(map parse-rules-graph rules-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))
[db]
(conj rules-dbs db))
all-rules-from-dbs (apply concat (rules-from-dbs methods inserts all-rules-dbs))
all-rules (concat all-rules-from-graphs all-rules-from-dbs)]
all-rules)))

(defn triples->map
"Turns triples from same subject (@id) originating from
Expand Down Expand Up @@ -219,16 +225,16 @@
db*)))))

(defn reason
[db methods graph-or-db {:keys [max-fuel reasoner-max]
:or {reasoner-max 10} :as _opts}]
[db methods rules-graphs rules-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 graph-or-db))
raw-rules (<? (all-rules methods* db* inserts rules-graphs rules-dbs))
_ (log/debug "Reasoner - extracted rules: " raw-rules)
reasoning-rules (->> raw-rules
(resolve/rules->graph db*)
Expand Down

0 comments on commit afd46fc

Please sign in to comment.