Last active
August 18, 2023 18:01
-
-
Save aamedina/69e3cf586bfe8eec2449dee3fe9825f5 to your computer and use it in GitHub Desktop.
testing rdfox examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; https://github.com/aamedina/rdf | |
;; If you want to store your databases locally use ~/.datomic/local.edn | |
;; to configure a path :storage-dir pointing to the directory to keep the Datomic Local databases | |
;; or update the dev/system.edn to use :storage-dir :mem | |
;; clojure -A:dev | |
;; user=> (reset) | |
;; dev=> | |
(def boot-db (net.wikipunk.datomic.boot/test-bootstrap (:db system))) | |
(defn make-test-db | |
[db all-tx-data] | |
(reduce (fn [with-db tx-data] | |
(try | |
(:db-after (d/with with-db {:tx-data tx-data})) | |
(catch Throwable ex | |
(throw (ex-info (.getMessage ex) {:tx-data tx-data}))))) | |
(if (instance? datomic.dev_local.with_db.WithDb db) | |
db | |
(d/with-db db)) | |
all-tx-data)) | |
(def agg-test-tx-data | |
[[{:db/ident :bob | |
:rdf/type :schema/Person} | |
{:db/ident :mary | |
:rdf/type :schema/Person} | |
{:db/ident :jen | |
:rdf/type :schema/Person} | |
{:db/ident :accounting | |
:rdf/type :schema/Organization} | |
{:db/ident :hr | |
:rdf/type :schema/Organization}] | |
[{:db/ident :acme | |
:rdf/type :schema/Organization | |
:schema/department [:accounting :hr]}] | |
[[:db/add :bob :schema/worksFor :accounting] | |
[:db/add :bob :schema/baseSalary 50000M] | |
[:db/add :mary :schema/worksFor :hr] | |
[:db/add :mary :schema/baseSalary 47000M] | |
[:db/add :jen :schema/worksFor :accounting] | |
[:db/add :jen :schema/baseSalary 60000M]]]) | |
(defn dept-avg-salary | |
[db] | |
(d/q '[:find ?did (avg ?s) | |
:in $ % | |
:where | |
(dept-salaries ?d ?s) | |
[?d :db/ident ?did]] | |
db '[[(dept-salaries ?d ?s) | |
[?org :schema/department ?d] | |
[?d :rdf/type :schema/Organization] | |
[?x :schema/worksFor ?d] | |
[?x :schema/baseSalary ?s]]])) | |
(dept-avg-salary (make-test-db boot-db agg-test-tx-data)) | |
; => [[:accounting 55000.0] [:hr 47000.0]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment