Skip to content

Instantly share code, notes, and snippets.

Created July 7, 2012 12:57
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
(ns mynamespace.dt
(:use clojure.set)
(:use [datomic.api :only [q db] :as d])
(:use clojure.pprint)
(defrecord Supplier [number name status city])
;;;(-> Supplier .getConstructors first .getParameterTypes)
(defrecord Part [number name colour weight city])
(defrecord Shipment [supplier part quantity])
(def suppliers
#{(Supplier. "S1" "Smith" 20 "London")
(Supplier. "S2" "Jones" 10 "Paris")
(Supplier. "S3" "Blake" 30 "Paris")})
(def parts
#{(Part. "P1" "Nut" "Red" 12.0 "London")
(Part. "P2" "Bolt" "Green" 17.0 "Paris")
(Part. "P3" "Screw" "Blue" 17.0 "Oslo")})
(def shipments
#{(Shipment. "S1" "P1" 300)
(Shipment. "S2" "P2" 200)
(Shipment. "S2" "P3" 400)})
;;;(rename parts {:number :id :city :location})
;;;(select (fn [imap] (= (:name imap) "Smith")) suppliers)
;;;(project suppliers [:city])
;;;(join parts shipments {:number :part})
;;;(project (join (select #(= (:city %) "Paris") suppliers) shipments {:number :supplier}) [:name])
;;; {:number "S1", :name "Smith", :status 20, :city "London"} -> ["S1" "Smith" 20 "London"]
(defn map-relation [record-id relation]
(let [basis (. (. record-id getMethod "getBasis" (make-array Class 0)) invoke nil (make-array Object 0))]
(into []
(map (fn [entry]
(into []
(map (fn [sym]
(get entry (keyword sym)))
;;;(map-relation Supplier suppliers)
(defn test-datomic-query-2 []
(q '[:find ?supplier-name
:in [[?supplier-number ?supplier-name ?supplier-status ?supplier-city]]
:where [(= ?supplier-city "Paris")]
(map-relation Supplier suppliers)
(defn test-datomic-query-3 []
(q '[:find ?supplier-name
:in $suppliers
:where [$suppliers _ ?supplier-name _ "Paris"]
(map-relation Supplier suppliers)
(defn test-datomic-query-4 []
(q '[:find ?supplier-name
:in $suppliers $shipments
[$suppliers ?supplier-number ?supplier-name ?supplier-status "Paris"]
[$shipments ?supplier-number ?shipment-part ?shipment-quantity]
(map-relation Supplier suppliers)
(map-relation Shipment shipments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment