Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/query api overhaul #820

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
61b04f3
Include reasoning logic when restricting db
JaceRockman Jun 11, 2024
b34628b
Update the policy wrapping function
JaceRockman Jun 12, 2024
b26b1d7
Aggregate rules from graphs and dbs when reasoning
JaceRockman Jun 18, 2024
ad4ce82
Allow reasoning with ledger names as rule sources
JaceRockman Jun 19, 2024
6438282
Make parse-rule-dbs and rules-from-dbs asynchronous
JaceRockman Jun 19, 2024
ae9ab30
Fix tests and api usage of reasoner/reason
JaceRockman Jun 19, 2024
35f464c
Add datalog tests for multiple rule sources
JaceRockman Jun 20, 2024
2f6eba4
Add negative test cases for multiple rule sources
JaceRockman Jun 20, 2024
be074b1
Handle duplicate rule ids from multiple sources
JaceRockman Jun 21, 2024
e401d34
Update sample data and tests for owl reasoning tests
JaceRockman Jun 25, 2024
e39eb51
Add owl reasoning tests for multiple sources
JaceRockman Jun 25, 2024
e1bf7d0
Extract all of the restrict logic into functions
JaceRockman Jul 1, 2024
bac78e3
Move condense sparql-query and fql-query into query
JaceRockman Jul 2, 2024
c06c3f1
Implement the stateful query api fluree/q
JaceRockman Jul 3, 2024
99e5530
Fix some merge issues
JaceRockman Jul 30, 2024
80f992a
Fix credential-query to exclude user provided :did
JaceRockman Jul 31, 2024
c2354d9
Fix db->time-travel-db function
JaceRockman Jul 31, 2024
695f11e
Merge branch 'feature/multiple-reasoner-sources2' into feature/query-…
JaceRockman Jul 31, 2024
91d764e
Merge branch 'feature/multiple-reasoner-sources2' into feature/query-…
JaceRockman Aug 7, 2024
3a6bba2
Merge branch 'feature/multiple-reasoner-sources2' into feature/query-…
JaceRockman Aug 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/clj/fluree/db/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[clojure.core.async :as async :refer [go <!]]
[fluree.db.query.api :as query-api]
[fluree.db.api.transact :as transact-api]
[fluree.db.flake.flake-db :refer [db?]]
[fluree.db.util.core :as util]
[fluree.db.util.async :refer [go-try <?]]
[fluree.db.ledger.json-ld :as jld-ledger]
Expand Down Expand Up @@ -296,9 +297,9 @@
(let [{query :subject, identity :did} (if (= :sparql format)
(cred/verify-jws cred-query)
(<? (cred/verify cred-query)))]
(log/debug "Credential query with identity: " identity " and query: " query)
(let [policy-db (<? (policy/wrap-identity-policy ds identity default-allow? values-map))]
(<? (query-api/query policy-db query opts))))))))
(log/debug "Credential query with identity: " identity " and query: " query)
(let [policy-db (<? (policy/wrap-identity-policy ds identity default-allow? values-map))]
(<? (query-api/query policy-db query (dissoc opts :did)))))))))

(defn query-connection
"Queries the latest db in the ledger specified by the 'from' parameter in the
Expand Down Expand Up @@ -412,10 +413,10 @@
or if no rules graph is supplied, the rules will be looked for within
the db itself."
([db methods] (reason db methods nil nil))
([db methods rules-graph] (reason db methods rules-graph nil))
([db methods rules-graph opts]
([db methods rule-sources] (reason db methods rule-sources nil))
([db methods rule-sources opts]
(promise-wrap
(reasoner/reason db methods rules-graph opts))))
(reasoner/reason db methods rule-sources opts))))

(defn reasoned-count
"Returns a count of reasoned facts in the provided db."
Expand Down
4 changes: 2 additions & 2 deletions src/clj/fluree/db/flake/flake_db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@
(policy/root-db db))

reasoner/Reasoner
(-reason [db methods rules-graph fuel-tracker reasoner-max]
(flake.reasoner/reason db methods rules-graph fuel-tracker reasoner-max))
(-reason [db methods rule-sources fuel-tracker reasoner-max]
(flake.reasoner/reason db methods rule-sources fuel-tracker reasoner-max))
(-reasoned-facts [db]
(flake.reasoner/reasoned-facts db)))

Expand Down
96 changes: 71 additions & 25 deletions src/clj/fluree/db/flake/reasoner.cljc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
(ns fluree.db.flake.reasoner
(:require [clojure.string :as str]
(:require [clojure.core.async :as async]
[clojure.string :as str]
[fluree.db.flake :as flake]
[fluree.db.json-ld.iri :as iri]
[fluree.db.util.core :as util :refer [try* catch*]]
[fluree.db.reasoner.util :refer [parse-rules-graph]]
[fluree.db.util.log :as log]
[fluree.db.query.exec :refer [queryable?]]
[fluree.db.query.exec :as exec]
[fluree.db.flake.transact :as flake.transact]
[fluree.db.util.async :refer [go-try <?]]
[fluree.db.reasoner.resolve :as resolve]
Expand Down Expand Up @@ -163,28 +164,50 @@
(log/debug "Reasoner - source OWL rules: " graph)
(owl-datalog/owl->datalog inserts graph)))

(defn extract-rules-from-dbs
[method inserts dbs]
(go-try
(loop [[db & remaining-dbs] dbs
rules []]
(if db
(let [updated-rules (into rules
(as-> db $
(<? (resolve/rules-from-db $ method))
(rules-from-graph method inserts $)))]
(recur remaining-dbs updated-rules))
rules))))

(defn rules-from-dbs
[methods inserts dbs]
(go-try
(loop [[method & remaining-methods] methods
rules []]
(if method
(recur remaining-methods (into rules (<? (extract-rules-from-dbs method inserts dbs))))
(remove empty? rules)))))

(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 rule-sources]
(go-try
(let [rules-db (cond
(nil? graph-or-db) db
(queryable? 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 [rule-graphs (filter #(and (map? %) (not (exec/queryable? %))) rule-sources)
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 (mapcat (fn [method]
(mapcat (fn [parsed-rules-graph]
(rules-from-graph method inserts parsed-rules-graph))
parsed-rule-graphs))
methods)
rule-dbs (filter #(or (string? %) (exec/queryable? %)) rule-sources)
all-rule-dbs (if (or (nil? rule-dbs) (empty? rule-dbs))
[db]
(conj rule-dbs db))
all-rules-from-dbs (<? (rules-from-dbs methods inserts all-rule-dbs))]
(concat all-rules-from-graphs all-rules-from-dbs))))

(defn triples->map
"Turns triples from same subject (@id) originating from
Expand Down Expand Up @@ -235,14 +258,37 @@
(recur r db**))
db*)))))

(defn find-duplicate-ids
[raw-rules]
(let [rule-ids (map first raw-rules)]
(->> rule-ids
frequencies
(filter #(< 1 (val %))))))

(defn deduplicate-raw-rules
"Given a list of reasoning rules, identifies rules with duplicate ids and renames them using
indexes."
[raw-rules]
(let [duplicate-id-frequencies (find-duplicate-ids raw-rules)]
(reduce (fn [rules [duplicate-id occurances]]
(let [grouped-rules (group-by #(= duplicate-id (first %)) rules)]
(loop [suffix occurances
rules-to-update (get grouped-rules true)
updated-rules-list (get grouped-rules false)]
(if (empty? rules-to-update)
updated-rules-list
(let [updated-rule [(str duplicate-id suffix) (last (first rules-to-update))]]
(recur (dec suffix) (rest rules-to-update) (conj updated-rules-list updated-rule)))))))
raw-rules duplicate-id-frequencies)))

(defn reason
[db methods rules-graph fuel-tracker reasoner-max]
[db methods rule-sources fuel-tracker reasoner-max]
(go-try
(let [db* (update db :reasoner #(into methods %))
tx-state (flake.transact/->tx-state :db db*)
inserts (atom nil)
(let [db* (update db :reasoner #(into methods %))
tx-state (flake.transact/->tx-state :db db*)
inserts (atom nil)
;; TODO - rules can be processed in parallel
raw-rules (<? (all-rules methods db* inserts rules-graph))
raw-rules (<? (all-rules methods db* inserts rule-sources))
_ (log/debug "Reasoner - extracted rules: " raw-rules)
reasoning-rules (-> raw-rules
resolve/rules->graph
Expand Down
8 changes: 4 additions & 4 deletions src/clj/fluree/db/json_ld/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
[fluree.db.json-ld.iri :as iri]
[fluree.db.platform :as platform]
[clojure.core.async :as async :refer [go <!]]
[fluree.db.query.api :as query-api]
[fluree.db.api.transact :as transact-api]
[fluree.db.util.core :as util]
[fluree.db.util.async :refer [go-try <?]]
[fluree.db.ledger.json-ld :as jld-ledger]
[fluree.db.ledger :as ledger]
[fluree.db.util.log :as log]
[fluree.db.query.api :as query-api]
[fluree.db.query.range :as query-range]
[fluree.db.nameservice.core :as nameservice]
[fluree.db.connection :refer [notify-ledger]]
Expand Down Expand Up @@ -573,11 +573,11 @@
or if no rules graph is supplied, the rules will be looked for within
the db itself."
([db methods] (reason db methods nil nil))
([db methods rules-graph] (reason db methods rules-graph nil))
([db methods rules-graph opts]
([db methods rule-sources] (reason db methods rule-sources nil))
([db methods rule-sources opts]
(log/warn "DEPRECATED function `reason` superseded by `fluree.db.api/reason`")
(promise-wrap
(reasoner/reason db methods rules-graph opts))))
(reasoner/reason db methods rule-sources opts))))

(defn ^{:deprecated "3.0"
:superseded-by "fluree.db/reasoned-count"}
Expand Down
Loading