rbbt-rest 1.4.3 → 1.4.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8812343a2a8345c1f58516ea3564d9f7c69a242c
4
- data.tar.gz: 0a82376a6d54a79d43e0dec28a621c042648ccdd
3
+ metadata.gz: f9221c25d61dd1032e5b8b84cf2d53757a34cc64
4
+ data.tar.gz: b1075e7931eaef5f7135e798af4055d4aab1a4a8
5
5
  SHA512:
6
- metadata.gz: ea16ac1a23c93d58ca5d8c808a7b4c650022fa250e7831403203c75b8e0e25474bf19232343102e0b07a4af397a39ee9c192924f5d1b0dc3222ef869f933ad08
7
- data.tar.gz: 6fb04bf125f9cdccefa1db06f0723133f60b65e8761314aaba1288e14ad9bbfd3f4441e5bfc65bd0968e9dc10a8510f952e44a3489f086197c83658013126e23
6
+ metadata.gz: 2703f327403947d1cb6899d59419713559ab2d25bda267655709dca55d88dee4a938fac9362398e72609153457afd4e3d8aec9101535e9375fc7e14c5373c9a0
7
+ data.tar.gz: 3f167c4d69d1ae1d8e65b2089cce94d98bc8926f834e656be721a23de7ae3d6153297f96a2ddafdb6838a4e18ce02658544aa0db5258a7e0b3cd724391e713c3
@@ -178,7 +178,7 @@ module RbbtRESTHelpers
178
178
 
179
179
  case
180
180
  when value =~ /^([<>]=?)(.*)/
181
- tsv = tsv.select(key, invert){|k| k = k.first if Array === k; k.to_f.send($1, $2.to_f)}
181
+ tsv = tsv.select(key, invert){|k| k = k.first if Array === k; (k.nil? or k.empty?) ? false : k.to_f.send($1, $2.to_f)}
182
182
  when value =~ /^\/(.+)\/.{0,2}\s*$/
183
183
  tsv = tsv.select({key => Regexp.new($1)}, invert)
184
184
  when (value =~ /^\d+$/ and tsv.type == :double or tsv.type == :flat)
@@ -5,452 +5,453 @@ require 'sinatra/base'
5
5
  require 'rbbt/knowledge_base'
6
6
  require 'rbbt/rest/knowledge_base/query'
7
7
  require 'rbbt/rest/knowledge_base/helpers'
8
+ require 'rbbt/rest/knowledge_base/locate'
9
+ require 'rbbt/rest/knowledge_base/render'
8
10
 
9
11
  module Sinatra
10
12
  module RbbtRESTKnowledgeBase
11
13
  def self.registered(base)
12
14
  base.module_eval do
13
- helpers KnowledgeBaseRESTHelpers
14
- use KnowledgeBaseRESTQuery
15
-
16
- ##{{{ Single entity
17
- #
18
- #get '/knowledge_base/:name/:database/children/:entity' do
19
- # name = consume_parameter :name
20
- # database = consume_parameter :database
21
- # entity = consume_parameter :entity
22
-
23
- # kb = get_knowledge_base name
24
- # matches = kb.children(database, entity)
25
- # case @format
26
- # when :tsv
27
- # content_type "text/tab-separated-values"
28
- # halt 200, matches.tsv.to_s
29
- # when :html
30
- # template_render('knowledge_base_partials/matches', {:matches => matches}, "Children: #{ [name, database, entity] }")
31
- # when :json
32
- # content_type :json
33
- # halt 200, matches.target.to_json
34
- # else
35
- # content_type :text
36
- # halt 200, matches.target * "\n"
37
- # end
38
- #end
39
-
40
- #get '/knowledge_base/:name/:database/parents/:entity' do
41
- # name = consume_parameter :name
42
- # database = consume_parameter :database
43
- # entity = consume_parameter :entity
44
-
45
- # kb = get_knowledge_base name
46
- # matches = kb.parents(database, entity)
47
- # case @format
48
- # when :tsv
49
- # content_type "text/tab-separated-values"
50
- # halt 200, matches.tsv.to_s
51
- # when :html
52
- # template_render('knowledge_base_partials/matches', {:matches => matches}, "Parents: #{ [name, database, entity] }")
53
- # when :json
54
- # content_type :json
55
- # halt 200, matches.source.to_json
56
- # else
57
- # content_type :text
58
- # halt 200, matches.source * "\n"
59
- # end
60
- #end
61
-
62
- #get '/knowledge_base/:name/:database/neighbours/:entity' do
63
- # name = consume_parameter :name
64
- # database = consume_parameter :database
65
- # entity = consume_parameter :entity
66
-
67
- # kb = get_knowledge_base name
68
- # neighbours = kb.neighbours(database, entity)
69
- # case @format
70
- # when :tsv
71
- # content_type "text/tab-separated-values"
72
- # halt 200, neighbours.values.collect{|m| m.tsv.to_s } * "\n\n"
73
- # when :html
74
- # template_render('knowledge_base_partials/matches', {:matches => neighbours}, "Neighbours: #{ [name, database, entity] }")
75
- # when :json
76
- # content_type :json
77
- # neighs = {}
78
- # neighs[:parents] = neighbours[:parents].source if neighbours[:parents]
79
- # neighs[:children] = neighbours[:children].target
80
- # halt 200, neighs.to_json
81
- # else
82
- # content_type :text
83
- # neighs = []
84
- # neighs.concat neighbours[:parents].source if neighbours[:parents]
85
- # neighs.concat neighbours[:children].target
86
- # halt 200, neighs * "\n"
87
- # end
88
- #end
89
-
90
- #get '/knowledge_base/:name/:database/subset' do
91
- # name = consume_parameter :name
92
- # database = consume_parameter :database
93
- # source = consume_parameter :source
94
- # target = consume_parameter :target
95
-
96
- # source = source == "all" ? :all : source.split(@array_separator) if source
97
- # target = target == "all" ? :all : target.split(@array_separator) if target
98
- # entities = { :source => source, :target => target }
99
-
100
- # kb = get_knowledge_base name
101
- # subset = kb.subset(database, entities)
102
- # case @format
103
- # when :tsv
104
- # content_type "text/tab-separated-values"
105
- # halt 200, subset.tsv.to_s
106
- # when :html
107
- # template_render('knowledge_base_partials/subset', {:subset => subset}, "Subset: #{ [name, database] }")
108
- # when :json
109
- # content_type :json
110
- # halt 200, subset.source.to_json
111
- # else
112
- # content_type :text
113
- # halt 200, subset.source * "\n"
114
- # end
115
- #end
116
-
117
- #
118
- ##{{{ Collection
119
-
120
- #post '/knowledge_base/:name/:database/collection_children' do
121
- # name = consume_parameter :name
122
- # database = consume_parameter :database
123
- # collection = consume_parameter :collection
124
- # raise ParameterException, "No collection specified" if collection.nil?
125
- # collection = JSON.parse(collection)
126
-
127
- # kb = get_knowledge_base name
128
- # matches = collection.keys.inject({}){|acc,type|
129
- # entities = collection[type]
130
- # entities.each do |entity|
131
- # _matches = kb.children(database, entity)
132
- # acc.merge!({ _matches.target_type => _matches}) if _matches and _matches.any?
133
- # end
134
- # acc
135
- # }
136
- # case @format
137
- # when :tsv
138
- # content_type "text/tab-separated-values"
139
- # matches = matches.sort_by{|k,list| list.length }.last.last
140
- # halt 200, matches.tsv.to_s
141
- # when :html
142
- # template_render('knowledge_base_partials/matches', {:matches => matches}, "Collection Children: #{ [name, database] }")
143
- # when :json
144
- # content_type :json
145
- # _matches = {}
146
- # matches.each{|type,list|
147
- # _matches[type] = list.target
148
- # }
149
- # halt 200, _matches.to_json
150
- # else
151
- # content_type :text
152
- # matches = matches.sort_by{|k,list| list.length }.last.last
153
- # halt 200, matches.target * "\n"
154
- # end
155
- #end
156
-
157
- #post '/knowledge_base/:name/:database/collection_parents' do
158
- # name = consume_parameter :name
159
- # database = consume_parameter :database
160
- # collection = consume_parameter :collection
161
- # raise ParameterException, "No collection specified" if collection.nil?
162
- # collection = JSON.parse(collection)
163
-
164
- # kb = get_knowledge_base name
165
- # matches = collection.keys.inject({}){|acc,type|
166
- # entities = collection[type]
167
- # entities.each do |entity|
168
- # _matches = kb.parents(database, entity)
169
- # acc.merge!({ _matches.target_type => _matches}) if _matches and _matches.any?
170
- # end
171
- # acc
172
- # }
173
- # case @format
174
- # when :tsv
175
- # content_type "text/tab-separated-values"
176
- # matches = matches.sort_by{|k,list| list.length }.last.last
177
- # halt 200, matches.tsv.to_s
178
- # when :html
179
- # template_render('knowledge_base_partials/matches', {:matches => matches}, "Collection Parents: #{ [name, database] }")
180
- # when :json
181
- # content_type :json
182
- # _matches = {}
183
- # matches.each{|type,list|
184
- # _matches[type] = list.target
185
- # }
186
- # halt 200, _matches.to_json
187
- # else
188
- # content_type :text
189
- # matches = matches.sort_by{|k,list| list.length }.last.last
190
- # halt 200, matches.target * "\n"
191
- # end
192
- #end
193
-
194
- #post '/knowledge_base/:name/:database/collection_neighbours' do
195
- # name = consume_parameter :name
196
- # database = consume_parameter :database
197
- # collection = consume_parameter :collection
198
- # raise ParameterException, "No collection specified" if collection.nil?
199
- # collection = JSON.parse(collection)
200
-
201
- # kb = get_knowledge_base name
202
- # matches = collection.keys.inject({}){|acc,type|
203
- # entities = collection[type]
204
- # entities.each do |entity|
205
- # _matches_h = kb.neighbours(database, entity)
206
- # _matches_h.each do |key, _matches|
207
- # target_type = case key
208
- # when :children
209
- # _matches.target_entity_type
210
- # when :parents
211
- # _matches.source_entity_type
212
- # end
213
- # _matches = acc[target_type].concat _matches if acc[target_type] and acc[target_type].any?
214
- # acc.merge!({ target_type => _matches }) if _matches and _matches.any?
215
- # end
216
- # end
217
- # acc
218
- # }
219
-
220
- # case @format
221
- # when :tsv
222
- # content_type "text/tab-separated-values"
223
- # matches = matches.sort_by{|k,list| list.length }.last.last
224
- # halt 200, matches.tsv.to_s
225
- # when :html
226
- # template_render('knowledge_base_partials/matches', {:matches => matches}, "Collection Parents: #{ [name, database] }")
227
- # when :json
228
- # content_type :json
229
- # _matches = {}
230
- # matches.each{|type,list|
231
- # _matches[type] = list.target.uniq.sort
232
- # }
233
- # halt 200, _matches.to_json
234
- # else
235
- # content_type :text
236
- # matches = matches.sort_by{|k,list| list.length }.last.last
237
- # halt 200, matches.target * "\n"
238
- # end
239
- #end
240
-
241
- #get '/knowledge_base/:name/:database/subset' do
242
- # name = consume_parameter :name
243
- # database = consume_parameter :database
244
- # source = consume_parameter :source
245
- # target = consume_parameter :target
246
-
247
- # source = source == "all" ? :all : source.split(@array_separator) if source
248
- # target = target == "all" ? :all : target.split(@array_separator) if target
249
- # entities = { :source => source, :target => target }
250
-
251
- # kb = get_knowledge_base name
252
- # subset = kb.subset(database, entities)
253
- # case @format
254
- # when :tsv
255
- # content_type "text/tab-separated-values"
256
- # halt 200, subset.tsv.to_s
257
- # when :html
258
- # template_render('knowledge_base_partials/subset', {:subset => subset}, "Subset: #{ [name, database] }")
259
- # when :json
260
- # content_type :json
261
- # halt 200, subset.source.to_json
262
- # else
263
- # content_type :text
264
- # halt 200, subset.source * "\n"
265
- # end
266
- #end
267
-
268
- ##{{{ Info
269
- #
270
- #get '/knowledge_base/info/:name/:database/:pair' do
271
- # name = consume_parameter :name
272
- # database = consume_parameter :database
273
- # pair = consume_parameter :pair
274
-
275
- # kb = get_knowledge_base name
276
- # index = kb.get_index(database)
277
-
278
- # AssociationItem.setup(pair, kb, database, false)
279
- # template_render('knowledge_base_partials/association', {:pair => pair, :kb => kb, :index => index, :database => database}, "Association: #{ pair }")
280
- #end
281
- #
282
- ##{{{ Children
283
-
284
- #get '/knowledge_base/:name/:database/entity_children/:entity' do
285
- # name = consume_parameter :name
286
- # database = consume_parameter :database
287
- # entity = consume_parameter :entity
288
-
289
- # kb = get_knowledge_base name
290
- # found = kb.identify database, entity
291
- # raise ParameterException, "Entity #{entity} was not found" unless found
292
-
293
- # list = kb.children(database, found).target_entity
294
-
295
- # case @format
296
- # when :json
297
- # content_type "application/json"
298
- # halt 200, prepare_entities_for_json(list).to_json
299
- # when :html
300
- # end
301
- #end
302
-
303
- ## List children
304
- #post '/knowledge_base/:name/:database/entity_list_children/' do
305
- # name = consume_parameter :name
306
- # database = consume_parameter :database
307
- # entities = consume_parameter :entities
308
-
309
- # raise ParameterException, "No 'entities' provided" if entities.nil?
310
-
311
- # entities = entities.split("|")
312
-
313
- # kb = get_knowledge_base name
314
-
315
- # children = {}
316
- # entities.each do |entity|
317
- # found = kb.identify database, entity
318
- # next if found.nil?
319
- # children[entity] = kb.children(database, found).target_entity
320
- # end
321
- # case @format
322
- # when :json
323
- # content_type "application/json"
324
- # halt 200, prepare_entities_for_json(children).to_json
325
- # when :html
326
- # end
327
- #end
328
-
329
- ## Collection children
330
- #post '/knowledge_base/:name/:database/entity_collection_children' do
331
- # name = consume_parameter :name
332
- # database = consume_parameter :database
333
- # entities = consume_parameter :entities
334
-
335
- # raise ParameterException, "No 'entities' provided" if entities.nil?
336
-
337
- # entities = JSON.parse(entities)
338
-
339
- # kb = get_knowledge_base name
340
-
341
- # entities.each do |type,list|
342
- # list.each do |entity|
343
- # found = kb.identify database, entity
344
- # next if found.nil?
345
- # targets = kb.children(database, found).target_entity
346
- # next if targets.nil? or targets.empty?
347
- # target_type = kb.target database
348
- # children[target_type] ||= []
349
- # children[target_type].concat targets
350
- # end
351
- # end
352
-
353
- # case @format
354
- # when :json
355
- # content_type "application/json"
356
- # halt 200, prepare_entities_for_json(children).to_json
357
- # when :html
358
- # end
359
- #end
360
-
361
- ##{{{ Neighbours
362
-
363
- #get '/knowledge_base/:name/:database/entity_neighbours/:entity' do
364
- # name = consume_parameter :name
365
- # database = consume_parameter :database
366
- # entity = consume_parameter :entity
367
-
368
- # kb = get_knowledge_base name
369
- # found = kb.identify database, entity
370
- # raise ParameterException, "Entity #{entity} was not found" unless found
371
-
372
- # list = kb.neighbours(database, found).values.select{|list| list and list.any?}.first
373
- # list = list.target_entity if list.respond_to? :target_entity
374
- # list ||= []
375
-
376
- # case @format
377
- # when :json
378
- # content_type "application/json"
379
- # halt 200, prepare_entities_for_json(list).to_json
380
- # when :html
381
- # end
382
- #end
383
-
384
- #post '/knowledge_base/:name/:database/entity_list_neighbours/' do
385
- # name = consume_parameter :name
386
- # database = consume_parameter :database
387
- # entities = consume_parameter :entities
388
-
389
- # raise ParameterException, "No 'entities' provided" if entities.nil?
390
-
391
- # entities = entities.split("|")
392
-
393
- # kb = get_knowledge_base name
394
-
395
- # children = {}
396
- # entities.each do |entity|
397
- # found = kb.identify database, entity
398
- # next if found.nil?
399
- # matches = kb.neighbours(database, found).values.select{|list| list and list.any?}.first
400
- # next if matches.nil? or matches.empty?
401
- # children[entity] = matches.target_entity
402
- # end
403
- # case @format
404
- # when :json
405
- # content_type "application/json"
406
- # halt 200, prepare_entities_for_json(children).to_json
407
- # when :html
408
- # end
409
- #end
410
-
411
- #post '/knowledge_base/:name/:database/entity_collection_neighbours' do
412
- # name = consume_parameter :name
413
- # database = consume_parameter :database
414
- # entities = consume_parameter :entities
415
-
416
- # raise ParameterException, "No 'entities' provided" if entities.nil?
417
-
418
- # entities = JSON.parse(entities)
419
-
420
- # kb = get_knowledge_base name
421
-
422
- # neighbours = {}
423
- # entities.each do |type,list|
424
- # list.each do |entity|
425
-
426
- # found = kb.identify_source database, entity
427
- # if found.nil?
428
- # reverse = true
429
- # found = kb.identify_target database, entity
430
- # else
431
- # reverse = false
432
- # end
433
- # next if found.nil?
434
-
435
- # matches = kb.neighbours(database, found)[reverse ? :parents : :children]
436
- # next if matches.nil? or matches.empty?
437
- # targets = matches.target
438
-
439
- # entity_type = reverse ? kb.source_type(database) : kb.target_type(database)
440
- # neighbours[entity_type] ||= []
441
- # neighbours[entity_type].concat targets
442
- # end
443
- # end
444
-
445
- # neighbours.each{|type, list| list.uniq!}
446
-
447
- # case @format
448
- # when :json
449
- # content_type "application/json"
450
- # halt 200, prepare_entities_for_json(neighbours).to_json
451
- # when :html
452
- # end
453
- #end
15
+ include RbbtRESTHelpers
16
+ include KnowledgeBaseRESTHelpers
17
+
18
+ get '/knowledge_base/:name/:database/children/:entity' do
19
+ name = consume_parameter :name
20
+ database = consume_parameter :database
21
+ entity = consume_parameter :entity
22
+
23
+ kb = get_knowledge_base name
24
+ matches = kb.children(database, entity)
25
+ case @format
26
+ when :tsv
27
+ content_type "text/tab-separated-values"
28
+ halt 200, matches.tsv.to_s
29
+ when :html
30
+ template_render('knowledge_base_partials/matches', {:matches => matches}, "Children: #{ [name, database, entity] }")
31
+ when :json
32
+ content_type :json
33
+ halt 200, matches.target.to_json
34
+ else
35
+ content_type :text
36
+ halt 200, matches.target * "\n"
37
+ end
38
+ end
39
+
40
+ get '/knowledge_base/:name/:database/parents/:entity' do
41
+ name = consume_parameter :name
42
+ database = consume_parameter :database
43
+ entity = consume_parameter :entity
44
+
45
+ kb = get_knowledge_base name
46
+ matches = kb.parents(database, entity)
47
+ case @format
48
+ when :tsv
49
+ content_type "text/tab-separated-values"
50
+ halt 200, matches.tsv.to_s
51
+ when :html
52
+ template_render('knowledge_base_partials/matches', {:matches => matches}, "Parents: #{ [name, database, entity] }")
53
+ when :json
54
+ content_type :json
55
+ halt 200, matches.source.to_json
56
+ else
57
+ content_type :text
58
+ halt 200, matches.source * "\n"
59
+ end
60
+ end
61
+
62
+ get '/knowledge_base/:name/:database/neighbours/:entity' do
63
+ name = consume_parameter :name
64
+ database = consume_parameter :database
65
+ entity = consume_parameter :entity
66
+
67
+ kb = get_knowledge_base name
68
+ neighbours = kb.neighbours(database, entity)
69
+ case @format
70
+ when :tsv
71
+ content_type "text/tab-separated-values"
72
+ halt 200, neighbours.values.collect{|m| m.tsv.to_s } * "\n\n"
73
+ when :html
74
+ template_render('knowledge_base_partials/matches', {:matches => neighbours}, "Neighbours: #{ [name, database, entity] }")
75
+ when :json
76
+ content_type :json
77
+ neighs = {}
78
+ neighs[:parents] = neighbours[:parents].source if neighbours[:parents]
79
+ neighs[:children] = neighbours[:children].target
80
+ halt 200, neighs.to_json
81
+ else
82
+ content_type :text
83
+ neighs = []
84
+ neighs.concat neighbours[:parents].source if neighbours[:parents]
85
+ neighs.concat neighbours[:children].target
86
+ halt 200, neighs * "\n"
87
+ end
88
+ end
89
+
90
+ get '/knowledge_base/:name/:database/subset' do
91
+ name = consume_parameter :name
92
+ database = consume_parameter :database
93
+ source = consume_parameter :source
94
+ target = consume_parameter :target
95
+
96
+ source = source == "all" ? :all : source.split(@array_separator) if source
97
+ target = target == "all" ? :all : target.split(@array_separator) if target
98
+ entities = { :source => source, :target => target }
99
+
100
+ kb = get_knowledge_base name
101
+ subset = kb.subset(database, entities)
102
+ case @format
103
+ when :tsv
104
+ content_type "text/tab-separated-values"
105
+ halt 200, subset.tsv.to_s
106
+ when :html
107
+ template_render('knowledge_base_partials/subset', {:subset => subset}, "Subset: #{ [name, database] }")
108
+ when :json
109
+ content_type :json
110
+ halt 200, subset.source.to_json
111
+ else
112
+ content_type :text
113
+ halt 200, subset.source * "\n"
114
+ end
115
+ end
116
+
117
+
118
+ #{{{ Collection
119
+
120
+ post '/knowledge_base/:name/:database/collection_children' do
121
+ name = consume_parameter :name
122
+ database = consume_parameter :database
123
+ collection = consume_parameter :collection
124
+ raise ParameterException, "No collection specified" if collection.nil?
125
+ collection = JSON.parse(collection)
126
+
127
+ kb = get_knowledge_base name
128
+ matches = collection.keys.inject({}){|acc,type|
129
+ entities = collection[type]
130
+ entities.each do |entity|
131
+ _matches = kb.children(database, entity)
132
+ acc.merge!({ _matches.target_type => _matches}) if _matches and _matches.any?
133
+ end
134
+ acc
135
+ }
136
+ case @format
137
+ when :tsv
138
+ content_type "text/tab-separated-values"
139
+ matches = matches.sort_by{|k,list| list.length }.last.last
140
+ halt 200, matches.tsv.to_s
141
+ when :html
142
+ template_render('knowledge_base_partials/matches', {:matches => matches}, "Collection Children: #{ [name, database] }")
143
+ when :json
144
+ content_type :json
145
+ _matches = {}
146
+ matches.each{|type,list|
147
+ _matches[type] = list.target
148
+ }
149
+ halt 200, _matches.to_json
150
+ else
151
+ content_type :text
152
+ matches = matches.sort_by{|k,list| list.length }.last.last
153
+ halt 200, matches.target * "\n"
154
+ end
155
+ end
156
+
157
+ post '/knowledge_base/:name/:database/collection_parents' do
158
+ name = consume_parameter :name
159
+ database = consume_parameter :database
160
+ collection = consume_parameter :collection
161
+ raise ParameterException, "No collection specified" if collection.nil?
162
+ collection = JSON.parse(collection)
163
+
164
+ kb = get_knowledge_base name
165
+ matches = collection.keys.inject({}){|acc,type|
166
+ entities = collection[type]
167
+ entities.each do |entity|
168
+ _matches = kb.parents(database, entity)
169
+ acc.merge!({ _matches.target_type => _matches}) if _matches and _matches.any?
170
+ end
171
+ acc
172
+ }
173
+ case @format
174
+ when :tsv
175
+ content_type "text/tab-separated-values"
176
+ matches = matches.sort_by{|k,list| list.length }.last.last
177
+ halt 200, matches.tsv.to_s
178
+ when :html
179
+ template_render('knowledge_base_partials/matches', {:matches => matches}, "Collection Parents: #{ [name, database] }")
180
+ when :json
181
+ content_type :json
182
+ _matches = {}
183
+ matches.each{|type,list|
184
+ _matches[type] = list.target
185
+ }
186
+ halt 200, _matches.to_json
187
+ else
188
+ content_type :text
189
+ matches = matches.sort_by{|k,list| list.length }.last.last
190
+ halt 200, matches.target * "\n"
191
+ end
192
+ end
193
+
194
+ post '/knowledge_base/:name/:database/collection_neighbours' do
195
+ name = consume_parameter :name
196
+ database = consume_parameter :database
197
+ collection = consume_parameter :collection
198
+ raise ParameterException, "No collection specified" if collection.nil?
199
+ collection = JSON.parse(collection)
200
+
201
+ kb = get_knowledge_base name
202
+ matches = collection.keys.inject({}){|acc,type|
203
+ entities = collection[type]
204
+ entities.each do |entity|
205
+ _matches_h = kb.neighbours(database, entity)
206
+ _matches_h.each do |key, _matches|
207
+ target_type = case key
208
+ when :children
209
+ _matches.target_entity_type
210
+ when :parents
211
+ _matches.source_entity_type
212
+ end
213
+ _matches = acc[target_type].concat _matches if acc[target_type] and acc[target_type].any?
214
+ acc.merge!({ target_type => _matches }) if _matches and _matches.any?
215
+ end
216
+ end
217
+ acc
218
+ }
219
+
220
+ @format ||= :json
221
+ case @format
222
+ when :tsv
223
+ content_type "text/tab-separated-values"
224
+ matches = matches.sort_by{|k,list| list.length }.last.last
225
+ halt 200, matches.tsv.to_s
226
+ when :html
227
+ template_render('knowledge_base_partials/matches', {:matches => matches}, "Collection Parents: #{ [name, database] }")
228
+ when :json
229
+ content_type :json
230
+ _matches = {}
231
+ matches.each{|type,list|
232
+ _matches[type] = list.target.uniq.sort
233
+ }
234
+ halt 200, _matches.to_json
235
+ else
236
+ content_type :text
237
+ matches = matches.sort_by{|k,list| list.length }.last.last
238
+ halt 200, matches.target * "\n"
239
+ end
240
+ end
241
+
242
+ get '/knowledge_base/:name/:database/subset' do
243
+ name = consume_parameter :name
244
+ database = consume_parameter :database
245
+ source = consume_parameter :source
246
+ target = consume_parameter :target
247
+
248
+ source = source == "all" ? :all : source.split(@array_separator) if source
249
+ target = target == "all" ? :all : target.split(@array_separator) if target
250
+ entities = { :source => source, :target => target }
251
+
252
+ kb = get_knowledge_base name
253
+ subset = kb.subset(database, entities)
254
+ case @format
255
+ when :tsv
256
+ content_type "text/tab-separated-values"
257
+ halt 200, subset.tsv.to_s
258
+ when :html
259
+ template_render('knowledge_base_partials/subset', {:subset => subset}, "Subset: #{ [name, database] }")
260
+ when :json
261
+ content_type :json
262
+ halt 200, subset.source.to_json
263
+ else
264
+ content_type :text
265
+ halt 200, subset.source * "\n"
266
+ end
267
+ end
268
+
269
+ #{{{ Info
270
+
271
+ get '/knowledge_base/info/:name/:database/:pair' do
272
+ name = consume_parameter :name
273
+ database = consume_parameter :database
274
+ pair = consume_parameter :pair
275
+
276
+ kb = get_knowledge_base name
277
+ index = kb.get_index(database)
278
+
279
+ AssociationItem.setup(pair, kb, database, false)
280
+ template_render('knowledge_base_partials/association', {:pair => pair, :kb => kb, :index => index, :database => database}, "Association: #{ pair } #{[name, database] * ":"}", :cache_type => :async)
281
+ end
282
+
283
+ #{{{ Children
284
+
285
+ get '/knowledge_base/:name/:database/entity_children/:entity' do
286
+ name = consume_parameter :name
287
+ database = consume_parameter :database
288
+ entity = consume_parameter :entity
289
+
290
+ kb = get_knowledge_base name
291
+ found = kb.identify database, entity
292
+ raise ParameterException, "Entity #{entity} was not found" unless found
293
+
294
+ list = kb.children(database, found).target_entity
295
+
296
+ case @format
297
+ when :json
298
+ content_type "application/json"
299
+ halt 200, prepare_entities_for_json(list).to_json
300
+ when :html
301
+ end
302
+ end
303
+
304
+ # List children
305
+ post '/knowledge_base/:name/:database/entity_list_children/' do
306
+ name = consume_parameter :name
307
+ database = consume_parameter :database
308
+ entities = consume_parameter :entities
309
+
310
+ raise ParameterException, "No 'entities' provided" if entities.nil?
311
+
312
+ entities = entities.split("|")
313
+
314
+ kb = get_knowledge_base name
315
+
316
+ children = {}
317
+ entities.each do |entity|
318
+ found = kb.identify database, entity
319
+ next if found.nil?
320
+ children[entity] = kb.children(database, found).target_entity
321
+ end
322
+ case @format
323
+ when :json
324
+ content_type "application/json"
325
+ halt 200, prepare_entities_for_json(children).to_json
326
+ when :html
327
+ end
328
+ end
329
+
330
+ # Collection children
331
+ post '/knowledge_base/:name/:database/entity_collection_children' do
332
+ name = consume_parameter :name
333
+ database = consume_parameter :database
334
+ entities = consume_parameter :entities
335
+
336
+ raise ParameterException, "No 'entities' provided" if entities.nil?
337
+
338
+ entities = JSON.parse(entities)
339
+
340
+ kb = get_knowledge_base name
341
+
342
+ entities.each do |type,list|
343
+ list.each do |entity|
344
+ found = kb.identify database, entity
345
+ next if found.nil?
346
+ targets = kb.children(database, found).target_entity
347
+ next if targets.nil? or targets.empty?
348
+ target_type = kb.target database
349
+ children[target_type] ||= []
350
+ children[target_type].concat targets
351
+ end
352
+ end
353
+
354
+ case @format
355
+ when :json
356
+ content_type "application/json"
357
+ halt 200, prepare_entities_for_json(children).to_json
358
+ when :html
359
+ end
360
+ end
361
+
362
+ #{{{ Neighbours
363
+
364
+ get '/knowledge_base/:name/:database/entity_neighbours/:entity' do
365
+ name = consume_parameter :name
366
+ database = consume_parameter :database
367
+ entity = consume_parameter :entity
368
+
369
+ kb = get_knowledge_base name
370
+ found = kb.identify database, entity
371
+ raise ParameterException, "Entity #{entity} was not found" unless found
372
+
373
+ list = kb.neighbours(database, found).values.select{|list| list and list.any?}.first
374
+ list = list.target_entity if list.respond_to? :target_entity
375
+ list ||= []
376
+
377
+ case @format
378
+ when :json
379
+ content_type "application/json"
380
+ halt 200, prepare_entities_for_json(list).to_json
381
+ when :html
382
+ end
383
+ end
384
+
385
+ post '/knowledge_base/:name/:database/entity_list_neighbours/' do
386
+ name = consume_parameter :name
387
+ database = consume_parameter :database
388
+ entities = consume_parameter :entities
389
+
390
+ raise ParameterException, "No 'entities' provided" if entities.nil?
391
+
392
+ entities = entities.split("|")
393
+
394
+ kb = get_knowledge_base name
395
+
396
+ children = {}
397
+ entities.each do |entity|
398
+ found = kb.identify database, entity
399
+ next if found.nil?
400
+ matches = kb.neighbours(database, found).values.select{|list| list and list.any?}.first
401
+ next if matches.nil? or matches.empty?
402
+ children[entity] = matches.target_entity
403
+ end
404
+ case @format
405
+ when :json
406
+ content_type "application/json"
407
+ halt 200, prepare_entities_for_json(children).to_json
408
+ when :html
409
+ end
410
+ end
411
+
412
+ post '/knowledge_base/:name/:database/entity_collection_neighbours' do
413
+ name = consume_parameter :name
414
+ database = consume_parameter :database
415
+ entities = consume_parameter :entities
416
+
417
+ raise ParameterException, "No 'entities' provided" if entities.nil?
418
+
419
+ entities = JSON.parse(entities)
420
+
421
+ kb = get_knowledge_base name
422
+
423
+ neighbours = {}
424
+ entities.each do |type,list|
425
+ list.each do |entity|
426
+
427
+ found = kb.identify_source database, entity
428
+ if found.nil?
429
+ reverse = true
430
+ found = kb.identify_target database, entity
431
+ else
432
+ reverse = false
433
+ end
434
+ next if found.nil?
435
+
436
+ matches = kb.neighbours(database, found)[reverse ? :parents : :children]
437
+ next if matches.nil? or matches.empty?
438
+ targets = matches.target
439
+
440
+ entity_type = reverse ? kb.source_type(database) : kb.target_type(database)
441
+ neighbours[entity_type] ||= []
442
+ neighbours[entity_type].concat targets
443
+ end
444
+ end
445
+
446
+ neighbours.each{|type, list| list.uniq!}
447
+
448
+ case @format
449
+ when :json
450
+ content_type "application/json"
451
+ halt 200, prepare_entities_for_json(neighbours).to_json
452
+ when :html
453
+ end
454
+ end
454
455
  end
455
456
  end
456
457
  end