Created
July 7, 2012 12:57
-
-
Save cs224/3066392 to your computer and use it in GitHub Desktop.
datomic-clojure-relational-algebra-2012-07-07
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
(ns mynamespace.dt | |
(:use clojure.set) | |
(:use [datomic.api :only [q db] :as d]) | |
(:use clojure.pprint) | |
) | |
;;; http://www.lshift.net/blog/2010/08/21/some-relational-algebra-with-datatypes-in-clojure-12 | |
(defrecord Supplier [number name status city]) | |
;;;(-> Supplier .getConstructors first .getParameterTypes) | |
;;;(Supplier/getBasis) | |
(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]) | |
;;; http://stackoverflow.com/questions/2852133/clojure-vars-and-java-static-methods | |
;;; {: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))) | |
basis))) | |
relation)))) | |
;;;(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) | |
)) | |
;;;(test-datomic-query-2) | |
(defn test-datomic-query-3 [] | |
(q '[:find ?supplier-name | |
:in $suppliers | |
:where [$suppliers _ ?supplier-name _ "Paris"] | |
] | |
(map-relation Supplier suppliers) | |
)) | |
;;;(test-datomic-query-3) | |
(defn test-datomic-query-4 [] | |
(q '[:find ?supplier-name | |
:in $suppliers $shipments | |
:where | |
[$suppliers ?supplier-number ?supplier-name ?supplier-status "Paris"] | |
[$shipments ?supplier-number ?shipment-part ?shipment-quantity] | |
] | |
(map-relation Supplier suppliers) | |
(map-relation Shipment shipments) | |
)) | |
;;;(test-datomic-query-4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment