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 +4 -4
- data/lib/rbbt/rest/common/table.rb +1 -1
- data/lib/rbbt/rest/knowledge_base.rb +442 -441
- data/lib/rbbt/rest/knowledge_base/helpers.rb +16 -0
- data/lib/rbbt/rest/knowledge_base/locate.rb +41 -0
- data/lib/rbbt/rest/knowledge_base/query.rb +441 -441
- data/lib/rbbt/rest/knowledge_base/render.rb +15 -0
- data/lib/rbbt/rest/workflow.rb +2 -0
- data/lib/rbbt/rest/workflow/jobs.rb +1 -3
- data/share/views/association/Default.haml +9 -0
- data/share/views/compass/layout.sass +15 -0
- data/share/views/compass/rbbt/table.sass +3 -1
- data/share/views/entity_partials/list_container.haml +18 -6
- data/share/views/knowledge_base_partials/association.haml +22 -19
- data/share/views/knowledge_base_partials/association/info.haml +38 -0
- data/share/views/knowledge_base_partials/association/value.haml +13 -0
- data/share/views/public/js/_ajax_replace.js +3 -1
- data/share/views/public/js/rbbt/modal.js +61 -58
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9221c25d61dd1032e5b8b84cf2d53757a34cc64
|
4
|
+
data.tar.gz: b1075e7931eaef5f7135e798af4055d4aab1a4a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
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
|