Skip to content

Instantly share code, notes, and snippets.

@fogus
Created September 10, 2012 02:28
Show Gist options
  • Save fogus/3688476 to your computer and use it in GitHub Desktop.
Save fogus/3688476 to your computer and use it in GitHub Desktop.
(use '[datomic.api :only [db q] :as d])
(def schema
[{:db/doc "A programming language"
:db/id (d/tempid :db.part/db)
:db/ident :lang
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}
{:db/doc "An influence on a programming language"
:db/id (d/tempid :db.part/db)
:db/ident :influenced
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many
:db.install/_attribute :db.part/db}])
(def tx-data
(let [[simula lisp smalltalk dart]
(repeatedly #(d/tempid :db.part/user))]
[[:db/add lisp :lang "LISP"]
[:db/add simula :lang "Simula"]
[:db/add smalltalk :lang "Smalltalk"]
[:db/add dart :lang "Dart"]
[:db/add simula :influenced smalltalk]
[:db/add lisp :influenced smalltalk]
[:db/add smalltalk :influenced dart]]))
(def influences
(let [uri "datomic:mem://influences"]
(d/delete-database uri)
(d/create-database uri)
(let [conn (d/connect uri)]
(d/transact conn schema)
(d/transact conn tx-data)
(db conn))))
(def direct-influence
'[[(direct-influence ?old ?new)
[?old :influenced ?new]]])
(def remote-influence
'[[(remote-influence ?old ?new)
(direct-influence ?old ?new)]
[(remote-influence ?old ?new)
(direct-influence ?old ?intermediate)
(remote-influence ?intermediate ?new)]])
(def rules (concat direct-influence remote-influence))
(q '[:find ?influence
:in $ %
:where
[?new :lang "Dart"]
(remote-influence ?a ?h)
[?a :lang ?influence]]
influences rules)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment