rbbt-rest 1.4.3 → 1.4.4

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