-
Notifications
You must be signed in to change notification settings - Fork 2
/
mbrainz.clj
304 lines (255 loc) · 7.19 KB
/
mbrainz.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
(require '[datomic.api :as d])
(require '[datomic-q-explain.core :as explain])
(def uri "datomic:free://localhost:4334/mbrainz")
(def conn (d/connect uri))
(def db (d/db conn))
;; Queries taken from
;; https://github.com/Datomic/mbrainz-sample/wiki/Queries
(d/q
'[:find ?id ?type ?gender
:in $ ?name
:where
[?e :artist/name ?name]
[?e :artist/gid ?id]
[?e :artist/type ?type]
[?e :artist/gender ?gender]]
db "Lupe Fiasco")
(explain/q-explain
'[:find ?id ?type ?gender
:in $ ?name
:where
[?e :artist/name ?name]
[?e :artist/gid ?id]
[?e :artist/type ?type]
[?e :artist/gender ?gender]]
db
"Lupe Fiasco")
(d/q
'[:find (count ?title)
:in $ ?artist-name
:where
[?a :artist/name ?artist-name]
[?t :track/artists ?a]
[?t :track/name ?title]]
db "John Lennon")
(explain/q-explain
'[:find (count ?title)
:in $ ?artist-name
:where
[?a :artist/name ?artist-name]
[?t :track/artists ?a]
[?t :track/name ?title]]
db "John Lennon")
(count
(d/q
'[:find ?title ?album ?year
:in $ ?artist-name
:where
[?a :artist/name ?artist-name]
[?t :track/artists ?a]
[?t :track/name ?title]
[?m :medium/tracks ?t]
[?r :release/media ?m]
[?r :release/name ?album]
[?r :release/year ?year]]
db "John Lennon"))
(explain/q-explain
'[:find ?title ?album ?year
:in $ ?artist-name
:where
[?a :artist/name ?artist-name]
[?t :track/artists ?a]
[?t :track/name ?title]
[?m :medium/tracks ?t]
[?r :release/media ?m]
[?r :release/name ?album]
[?r :release/year ?year]]
db "John Lennon")
(count
(d/q
'[:find ?title ?album ?year
:in $ ?artist-name
:where
[?a :artist/name ?artist-name]
[?t :track/artists ?a]
[?t :track/name ?title]
[?m :medium/tracks ?t]
[?r :release/media ?m]
[?r :release/name ?album]
[?r :release/year ?year]
[(<= ?year 1980)]]
db "John Lennon"))
(explain/q-explain
'[:find ?title ?album ?year
:in $ ?artist-name
:where
[?a :artist/name ?artist-name]
[?t :track/artists ?a]
[?t :track/name ?title]
[?m :medium/tracks ?t]
[?r :release/media ?m]
[?r :release/name ?album]
[?r :release/year ?year]
[(<= ?year 1980)]]
db "John Lennon")
(def simple-rules
'[;; Given ?t bound to track entity-ids, binds ?r to the corresponding
;; set of album release entity-ids
[(track-release ?t ?r)
[?m :medium/tracks ?t]
[?r :release/media ?m]]
;; Supply track entity-ids as ?t, and the other parameters will be
;; bound to the corresponding information about the tracks
[(track-info ?t ?track-name ?artist-name ?album ?year)
[?t :track/name ?track-name]
[?t :track/artists ?a]
[?a :artist/name ?artist-name]
(track-release ?t ?r)
[?r :release/name ?album]
[?r :release/year ?year]]])
(count
(d/q
'[:find ?title ?album ?year
:in $ % ?artist-name
:where
[?a :artist/name ?artist-name]
[?t :track/artists ?a]
[?t :track/name ?title]
(track-release ?t ?r)
[?r :release/name ?album]
[?r :release/year ?year]]
db simple-rules "John Lennon"))
(explain/q-explain
'[:find ?title ?album ?year
:in $ % ?artist-name
:where
[?a :artist/name ?artist-name]
[?t :track/artists ?a]
[?t :track/name ?title]
(track-release ?t ?r)
[?r :release/name ?album]
[?r :release/year ?year]]
db simple-rules "John Lennon")
(count
(d/q
'[:find ?artist ?rname ?type
:in $ ?aname
:where
[?a :artist/name ?aname]
[?ar :abstractRelease/artists ?a]
[?ar :abstractRelease/name ?rname]
[?ar :abstractRelease/artistCredit ?artist]
[?ar :abstractRelease/type ?type-e]
[?type-e :db/ident ?type]]
db "The Beatles"))
(explain/q-explain
'[:find ?artist ?rname ?type
:in $ ?aname
:where
[?a :artist/name ?aname]
[?ar :abstractRelease/artists ?a]
[?ar :abstractRelease/name ?rname]
[?ar :abstractRelease/artistCredit ?artist]
[?ar :abstractRelease/type ?type-e]
[?type-e :db/ident ?type]]
db "The Beatles")
(def collab-rules
'[;; Generic transitive network walking, used by collaboration network
;; rule below
;; Supply:
;; ?e1 -- an entity-id
;; ?attr -- an attribute ident
;; and ?e2 will be bound to entity-ids such that ?e1 and ?e2 are both
;; values of the given attribute for some entity (?x)
[(transitive-net-1 ?attr ?e1 ?e2)
[?x ?attr ?e1]
[?x ?attr ?e2]
[(not= ?e1 ?e2)]]
;; Same as transitive-net-1, but search one more level of depth. We
;; define this rule twice, once for each case, and the rule
;; represents the union of the two cases:
;; - The entities are directly related via the attribute
;; - The entities are related to the given depth (in this case 2) via the attribute
[(transitive-net-2 ?attr ?e1 ?e2)
(transitive-net-1 ?attr ?e1 ?e2)]
[(transitive-net-2 ?attr ?e1 ?e2)
(transitive-net-1 ?attr ?e1 ?x)
(transitive-net-1 ?attr ?x ?e2)
[(not= ?e1 ?e2)]]
;; Artist collaboration graph-walking rules, based on generic
;; graph-walk rule above
;; Supply an artist name as ?artist-name-1, an ?artist-name-2 will be
;; bound to the names of artists who directly collaborated with the
;; artist(s) having that name
[(collab ?artist-name-1 ?artist-name-2)
[?a1 :artist/name ?artist-name-1]
(transitive-net-1 :track/artists ?a1 ?a2)
[?a2 :artist/name ?artist-name-2]]
;; Alias for collab
[(collab-net-1 ?artist-name-1 ?artist-name-2)
(collab ?artist-name-1 ?artist-name-2)]
;; Collaboration network walk to depth 2
[(collab-net-2 ?artist-name-1 ?artist-name-2)
[?a1 :artist/name ?artist-name-1]
(transitive-net-2 :track/artists ?a1 ?a2)
[?a2 :artist/name ?artist-name-2]]])
(count
(d/q
'[:find ?aname2
:in $ % ?aname
:where (collab-net-2 ?aname ?aname2)]
db collab-rules "Paul McCartney"))
(explain/q-explain
'[:find ?aname2
:in $ % ?aname
:where (collab-net-2 ?aname ?aname2)]
db collab-rules "Paul McCartney")
(d/q
'[:find ?aname (count ?e)
:with ?a
:in $ ?criterion [?aname ...]
:where
[?a :artist/name ?aname]
[?e ?criterion ?a]]
db :abstractRelease/artists ["Jay-Z" "Beyoncé Knowles"])
(explain/q-explain
'[:find ?aname (count ?e)
:with ?a
:in $ ?criterion [?aname ...]
:where
[?a :artist/name ?aname]
[?e ?criterion ?a]]
db :abstractRelease/artists ["Jay-Z" "Beyoncé Knowles"])
(count
(d/q
'[:find ?aname ?tname
:in $ ?artist-name
:where
[?a :artist/name ?artist-name]
[?t :track/artists ?a]
[?t :track/name ?tname]
[(not= "Outro" ?tname)]
[(not= "[outro]" ?tname)]
[(not= "Intro" ?tname)]
[(not= "[intro]" ?tname)]
[?t2 :track/name ?tname]
[?t2 :track/artists ?a2]
[(not= ?a2 ?a)]
[?a2 :artist/name ?aname]]
db "The Who"))
(explain/q-explain
'[:find ?aname ?tname
:in $ ?artist-name
:where
[?a :artist/name ?artist-name]
[?t :track/artists ?a]
[?t :track/name ?tname]
[(not= "Outro" ?tname)]
[(not= "[outro]" ?tname)]
[(not= "Intro" ?tname)]
[(not= "[intro]" ?tname)]
[?t2 :track/name ?tname]
[?t2 :track/artists ?a2]
[(not= ?a2 ?a)]
[?a2 :artist/name ?aname]]
db "The Who")