rbbt-rest 1.4.1 → 1.4.2

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