-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAPI.cls
658 lines (568 loc) · 18.7 KB
/
API.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/// <example language="swagger">
/// swagger: '2.0'
///
/// info:
/// version: "1.0.0"
/// title: iKnow REST APIs
/// description: |
/// This is the [OpenAPI Specification](https://github.com/OAI/OpenAPI-Specification)
/// of the iKnow REST API for the Entity Browser module.
/// Use [swagger-ui](https://github.com/swagger-api/swagger-ui/blob/master/README.md)
/// or a similar tool to conveniently browse and test these APIs.
/// For more information on iKnow, visit [intersystems.com](http://www.intersystems.com)
///
/// contact:
/// name: InterSystems
/// url: http://wrc.intersystems.com
/// email: [email protected]
///
/// consumes:
/// - application/json
/// produces:
/// - application/json
///
///
/// parameters:
/// domainParam:
/// name: domain
/// type: integer
/// in: path
/// description: the ID of the domain to query
/// required: true
///
/// definitions:
/// RequestObject:
/// type: object
/// properties:
/// filter:
/// description: filter object restricting the sources to be considered in this query
/// $ref: '#/definitions/Filter'
/// filterMode:
/// description: filter mode to use for optionally recalculating certain metrics. Defaults to recalculating all and re-sorting the results (15).
/// type: integer
/// default: 15
/// example: 15
/// enum: [1, 3, 5, 7, 11, 13, 15]
/// blacklists:
/// description: array of blacklist IDs or names to use for limiting query results, OR a valid SELECT clause retrieving entity values outright
/// type: array
/// default: []
/// items:
/// type: integer
///
/// Filter:
/// # TODO: move into proper hierarchy once swagger-ui supports it
/// type: object
/// example:
/// { "operator": "OR",
/// "filters": [ { "ids": [ 123 ] },
/// {"field": "Year", "operator": ">", "value": 2016 } ] }
/// properties:
/// ids:
/// description: for use with source-ID-based filters
/// type: array
/// items:
/// type: integer
/// extIds:
/// description: for use with external-ID-based filters
/// type: array
/// items:
/// type: string
/// operator:
/// description: for use with group filters ("AND"|"OR") or metadata-based filters (any other value)
/// type: string
/// enum: ["AND", "OR", "=", "!=", "<", ">", ">=", "<=", "BETWEEN", "IS NULL", "IS NOT NULL"]
/// field:
/// description: for use with metadata-based filters
/// type: string
/// value:
/// description: for use with metadata-based filters
/// type: string
/// negate:
/// description: for use with group filters
/// type: boolean
/// default: false
/// filters:
/// description: for use with group filters
/// type: array
/// items:
/// $ref: '#/definitions/Filter'
/// className:
/// description: use for filters not covered by other shorthands
/// type: string
/// example: '%iKnow.Filters.ContainsEntityFilter'
/// arguments:
/// description: for use with 'className' property, any arguments to be passed to the filter's %New() method (except domain ID)
/// type: array
/// items:
/// type: string
/// </example>
Class EntityBrowser.API Extends (%iKnow.REST.Base, %iKnow.REST.Utils)
{
Parameter PAGESIZE = 100;
XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
<Routes>
<Route Url="/swagger" Method="GET" Call="GetSwagger" />
<Route Url="/domain/:domain/similar/:string" Method="GET" Call="GetSimilarGET" Cors="true"/>
<Route Url="/domain/:domain/similar" Method="POST" Call="GetSimilar" Cors="true"/>
<Route Url="/domain/:domain/related/:entity" Method="GET" Call="GetRelatedGET" Cors="true"/>
<Route Url="/domain/:domain/related" Method="POST" Call="GetRelated" Cors="true"/>
</Routes>
}
/// Extend the timeout for the long queries up to 2 hours
ClassMethod OnPreDispatch(pUrl As %String, pMethod As %String, ByRef pContinue As %Boolean) As %Status
{
set %response.Timeout = 60 * 60 * 2 // 2 hours at max
return ##class(%iKnow.REST.Base).OnPreDispatch(pUrl, pMethod, pContinue)
}
/// <example language="swagger">
/// summary: |
/// Retrieves the graph representation of all entities similar to a seed string
/// parameters:
/// - $ref: '#/parameters/domainParam'
/// - name: string
/// in: path
/// description: seed string
/// type: string
/// required: true
/// example: bananas
/// responses:
/// 200:
/// description: Successful response
/// </example>
ClassMethod GetSimilarGET(pDomainId As %Integer, pString As %String = "") As %Status [ Internal ]
{
quit ..GetSimilar(pDomainId, pString)
}
/// <example language="swagger">
/// summary: |
/// Retrieves the graph representation of all entities similar to a seed string
/// parameters:
/// - $ref: '#/parameters/domainParam'
/// - name: requestObject
/// description: JSON object with a list of query-specific arguments
/// in: body
/// schema:
/// allOf:
/// - $ref: '#/definitions/RequestObject'
/// - type: object
/// properties:
/// string:
/// description: seed string to start from
/// type: string
/// example: "banana"
/// responses:
/// 200:
/// description: Successful response
/// </example>
ClassMethod GetSimilar(pDomainId As %Integer, pString As %String = "") As %Status
{
set tSC = $$$OK
$$$IKRESTINITDEBUG
try {
do ..ResolveDomainId(.pDomainId)
set:%request.Content.%IsDefined("string") pString = %request.Content.string
set tSC = ..ParseArgs(pDomainId,,, .tFilter, .tFilterMode, .tBlacklists)
quit:$$$ISERR(tSC)
set tSC = ##class(%iKnow.Queries.EntityAPI).GetSimilar(.tEntities, pDomainId, pString, 1, ..#PAGESIZE, tFilter, tFilterMode,, tBlacklists)
quit:$$$ISERR(tSC)
do ..TokenizeArray(.tEntityTokens, .tEntities, pString, .tSeedIndex, $lb("id","value","frequency","spread"), pDomainId, tFilter)
if 'tSeedIndex {
do ..TokenizeString(.tSeedTokens, pString)
merge tEntityTokens(0) = tSeedTokens
set tSeedIndex = 0
} else {
set tGraph(0,"ch",tSeedIndex)=""
}
set tAncestry(tSeedIndex)="",
tGraph = 0
set tSC = ..BuildGraph(.tGraph, .tEntityTokens, tSeedIndex, 0, 1, .tAncestry)
quit:$$$ISERR(tSC)
//k ^bdb
//s ^bdb($i(^bdb)) = pString
//m ^bdb("g") = tGraph
//m ^bdb("t") = tEntities
set tObjects = { "nodes": [], "edges": [] }
set tSC = ..AppendChildObjects(.tObjects, .tGraph, .tEntityTokens, 0, 1)
quit:$$$ISERR(tSC)
set tSC = ..UpdateRelated(pDomainId, .tObjects, pString)
set tResult = { "graph": (tObjects) }
} catch (ex) {
set tSC = ex.AsStatus()
}
quit ..%ProcessResult(tSC, $g(tResult))
}
/// <example language="swagger">
/// summary: |
/// Retrieves the graph representation of all entities related to a seed entity
/// parameters:
/// - $ref: '#/parameters/domainParam'
/// - name: entity
/// in: path
/// description: seed enitity
/// type: string
/// required: true
/// example: bananas
/// responses:
/// 200:
/// description: Successful response
/// </example>
ClassMethod GetRelatedGET(pDomainId As %Integer, pEntity As %String = "") As %Status [ Internal ]
{
quit ..GetRelated(pDomainId, pEntity)
}
/// <example language="swagger">
/// summary: |
/// Retrieves the graph representation of all entities related to a seed entity
/// parameters:
/// - $ref: '#/parameters/domainParam'
/// - name: requestObject
/// description: JSON object with a list of query-specific arguments
/// in: body
/// schema:
/// allOf:
/// - $ref: '#/definitions/RequestObject'
/// - type: object
/// properties:
/// entity:
/// description: seed entity to start from
/// type: string
/// example: 'bananas'
/// mode:
/// type: string
/// enum: ['proximity','direct']
/// default: 'proximity'
/// responses:
/// 200:
/// description: Successful response
/// </example>
ClassMethod GetRelated(pDomainId As %Integer, pEntity As %String = "") As %Status
{
set tSC = $$$OK
$$$IKRESTINITDEBUG
try {
do ..ResolveDomainId(.pDomainId)
set:%request.Content.%IsDefined("entity") pEntity = %request.Content.entity
set:'%request.Content.%IsDefined("mode") %request.Content.mode = "proximity"
set tSC = ..ParseArgs(pDomainId,,, .tFilter, .tFilterMode, .tBlacklists)
quit:$$$ISERR(tSC)
if (%request.Content.mode = "proximity") {
set tSC = ##class(%iKnow.Semantics.ProximityAPI).GetProfile(.tEntities, pDomainId, pEntity, 1, ..#PAGESIZE, tFilter, tBlacklists)
quit:$$$ISERR(tSC)
} else {
set tSC = ##class(%iKnow.Queries.EntityAPI).GetRelated(.tEntities, pDomainId, pEntity, 1, ..#PAGESIZE, tFilter, tFilterMode,,,,, tBlacklists)
quit:$$$ISERR(tSC)
}
do ..TokenizeArray(.tEntityTokens, .tEntities,,, $lb("id","value","score"), pDomainId, tFilter)
set tGraph = 0
set tSC = ..BuildGraph(.tGraph, .tEntityTokens, -1, 0, 1, .tAncestry)
quit:$$$ISERR(tSC)
//k ^bdb
//s ^bdb($i(^bdb)) = pEntity
//m ^bdb("g") = tGraph
//m ^bdb("t") = tEntities
set tObjects = { "nodes": [], "edges": [] }
set tSC = ..AppendChildObjects(.tObjects, .tGraph, .tEntityTokens, 0, 1)
quit:$$$ISERR(tSC)
set tSC = ..UpdateRelated(pDomainId, .tObjects, pEntity)
set tResult = { "graph": (tObjects) }
} catch (ex) {
set tSC = ex.AsStatus()
}
quit ..%ProcessResult(tSC, $g(tResult))
}
ClassMethod UpdateRelated(pDomainId As %Integer, ByRef pObjects As %DynamicObject, pEntity) As %Status
{
k %stem
#dim edges As %DynamicArray = pObjects.edges
#dim nodes As %DynamicArray = pObjects.nodes
do ..UpdateNodes(pDomainId,.pObjects)
set size = edges.%Size()-2
set i=0
for
{
quit:i>size
set edge = edges.%Get(i)
set nextEdge = edges.%Get(i+1)
set sourceEdge = edge.source
set targetEdge = edge.target
set sourceNode = ..GetObjIdBySourceID(nodes, sourceEdge)
set targetNode = ..GetObjIdBySourceID(nodes, targetEdge)
set sourceNode = nodes.%Get(sourceNode)
set targetNode = nodes.%Get(targetNode)
set lableSource = $select(sourceEdge=0:pEntity, 1:sourceNode.label)
set lableTarget = targetNode.label
set stemSource = ##class(%iKnow.Queries.EntityAPI).GetStem(pDomainId, lableSource, "ru")
set stemTarget = ##class(%iKnow.Queries.EntityAPI).GetStem(pDomainId, lableTarget, "ru")
if ($d(%stem(stemTarget)) = 1)
{
set %stem(stemTarget) = 2
}
elseif (targetEdge '= nextEdge.source)
{
set %stem(stemTarget)=1
}
if stemSource = stemTarget
{
if (nextEdge.source '= targetEdge) && (i'=0)
{
do edges.%Remove(i)
do nodes.%Remove(targetNode)
set size = size - 1
set i = i - 1
}
elseif (targetEdge '= nextEdge.source) && (sourceEdge = 0)
{
do edges.%Remove(i)
do nodes.%Remove(targetNode)
set size = size - 1
set i = i - 1
}
elseif (targetEdge = nextEdge.source)
{
set edge.target = nextEdge.target
do edges.%Remove(i+1)
do nodes.%Remove(targetNode)
set size = size - 1
set i = i - 1
do ..ChangeEdges(.edges, targetEdge, sourceEdge, i, size)
}
}
set i = i + 1
}
do ..UpdateRelatedN(pDomainId, .pObjects, pEntity)
quit $$$OK
}
ClassMethod UpdateRelatedN(pDomainId As %Integer, ByRef pObjects As %DynamicObject, pEntity) As %Status
{
#dim edges As %DynamicArray = pObjects.edges
#dim nodes As %DynamicArray = pObjects.nodes
set size = edges.%Size()-2
set i=0
for
{
quit:i>size
set edge = edges.%Get(i)
set nextEdge = edges.%Get(i+1)
set targetEdge = edge.target
set targetNode = ..GetObjIdBySourceID(nodes, targetEdge)
set targetNode = nodes.%Get(targetNode)
set lableTarget = targetNode.label
set stemTarget = ##class(%iKnow.Queries.EntityAPI).GetStem(pDomainId, lableTarget, "ru")
if ($d(%stem(stemTarget)) = 1) && (%stem(stemTarget) = 2) && (targetEdge '= nextEdge.source)
{
if ($d(%stem(stemTarget_"H")) = 1)
{
do edges.%Remove(i)
set size = size - 1
continue
}
set %stem(stemTarget_"H") = 1
}
set i = i + 1
}
quit $$$OK
}
ClassMethod UpdateNodes(pDomainId As %Integer, ByRef pObjects As %DynamicObject) As %Status
{
#dim nodes As %DynamicArray = pObjects.nodes
for i=0:1:nodes.%Size()-1
{
set node = nodes.%Get(i)
if ($l(node.label, " ") = 1)
{
set stem = ##class(%iKnow.Queries.EntityAPI).GetStem(pDomainId, node.label, "ru")
set node.label = stem
set node.entities.%Get(0).value = stem
}
}
quit $$$OK
}
ClassMethod ChangeEdges(ByRef Edges As %DynamicArray, target As %Integer, source As %Integer, i As %Integer, size As %Integer) As %Status
{
while (i>size)
{
set edge = Edges.%Get(i)
set sourceEdge = edge.source
set targetEdge = edge.target
if (sourceEdge = target)
{
set obj = {
"source": (source),
"target": (targetEdge),
"type": "similar"
}
do Edges.%Remove(i)
do Edges.%Push(obj)
set size = size - 1
}
set i = i + 1
}
quit $$$OK
}
ClassMethod GetObjIdBySourceID(Nodes As %DynamicArray, source) As %Integer
{
set iter = Nodes.%GetIterator()
while iter.%GetNext(.key , .value )
{
return:value.id=source key
}
return 0
}
ClassMethod AppendChildObjects(ByRef pObjects, ByRef pGraph, ByRef pEntityTokens, pParentNodeID As %Integer = 0, pLevel As %Integer = 1) As %Status
{
set tSC = $$$OK
try {
set tNodeID = ""
for {
set tNodeID = $order(pGraph(pParentNodeID, "ch", tNodeID), 1, tEdgeType)
quit:tNodeID=""
set tEntIDx = pGraph(tNodeID)
// skip if we already cover this further down in the tree
continue:($g(pGraph("l",tEntIDx)) > pLevel)
//s ^bdb($i(^bdb)) = $justify(" ",pLevel*2)_$e(tEdgeType)_" > "_pEntityTokens(tEntIDx)
set tEntObj = {}
set tProp = pEntityTokens(tEntIDx,0)
for {
set tProp = $order(pEntityTokens(tEntIDx,tProp),1,tPropValue)
quit:tProp=""
do tEntObj.%Set(tProp, tPropValue)
}
//set lable = ##class(%iKnow.Queries.EntityAPI).GetStem(1, pEntityTokens(tEntIDx), "ru")
set tNodeObj = {"id": (tNodeID), "label": (pEntityTokens(tEntIDx)), "type": "entity", "entities": [ (tEntObj) ]}
do pObjects.nodes.%Push(tNodeObj)
set tEdgeObj = {"source": (pParentNodeID), "target": (tNodeID), "type": (tEdgeType)}
do pObjects.edges.%Push(tEdgeObj)
set tSC = ..AppendChildObjects(.pObjects, .pGraph, .pEntityTokens, tNodeID, pLevel+1)
quit:$$$ISERR(tSC)
}
} catch (ex) {
set tSC = ex.AsStatus()
}
quit tSC
}
/// builds the following graph structure:
/// graph = $i
/// graph(node_id) = ent_idx
/// graph("l", ent_idx) = highest level for this ent_idx
/// graph(node_id,"ch",child_node_id) = ""
ClassMethod BuildGraph(ByRef pGraph, ByRef pEntityTokens, pSeedIndex As %Integer = 0, pSeedID As %Integer = 0, pLevel As %Integer = 1, ByRef pAncestry) As %Status
{
set tSC = $$$OK
try {
set i = ""
for {
set i = $order(pEntityTokens(i))
quit:i=""
continue:i=pSeedIndex
continue:$d(pAncestry(i))
if pSeedIndex<0 {
set tEdge = "related"
} elseif $$IsSimilar(pSeedIndex, i) {
set tEdge = "similar"
} else {
// not similar, not root-related
continue
}
if ($g(pGraph("l",i)) < pLevel) {
set pGraph("l",i) = pLevel
} else {
// there's already a more deeply nested instance of this
// entity, so we can skip
continue
}
// add as child node
set pGraph = pGraph+1
set pGraph(pGraph) = i
set pGraph(pSeedID,"ch",pGraph) = tEdge
// look for children further down
set pAncestry(i)=""
set tSC = ..BuildGraph(.pGraph, .pEntityTokens, i, pGraph, pLevel+1, .pAncestry)
quit:$$$ISERR(tSC)
kill pAncestry(i)
}
quit:$$$ISERR(tSC)
} catch (ex) {
set tSC = ex.AsStatus()
}
quit tSC
IsSimilar(tOriginIndex, tDestIndex, mode="")
if (mode="") {
// if the origin has more words, it'll never match
quit:pEntityTokens(tOriginIndex,0)>pEntityTokens(tDestIndex,0) 0
// if the origin is not shorter, it'll never match
// note that this also excludes identical strings
quit:$l(pEntityTokens(tOriginIndex))>=$l(pEntityTokens(tDestIndex)) 0
set similar = 1
for posO = 1:1:pEntityTokens(tOriginIndex,0) {
set similar = 0, length = $l(pEntityTokens(tOriginIndex,posO))
for posD = 1:1:pEntityTokens(tDestIndex,0) {
// for single-character tokens, require starting position
if length=1 {
set similar = ($e(pEntityTokens(tDestIndex,posD),1,length)=pEntityTokens(tOriginIndex,posO))
} else {
set similar = ''$find(pEntityTokens(tDestIndex,posD),pEntityTokens(tOriginIndex,posO))
}
quit:similar
}
quit:'similar
}
quit similar
} else {
quit 0
}
}
ClassMethod TokenizeArray(Output pTokens, ByRef pEntities, pSeedString As %String = "", Output pSeedIndex As %Integer, pDescriptor As %List = {$lb("id","value","frequency","spread")}, pDomainId As %Integer = 0, pFilter As %iKnow.Filters.Filter = "") [ Internal, Private ]
{
kill pTokens
set pSeedIndex = 0
set tDescriptor = $ll(pDescriptor)
for i = 1:1:tDescriptor {
if $ld(pDescriptor,i,tLabel) && (tLabel'="") {
set:tLabel="value" tValueIndex = i
set tDescriptor(i) = tLabel
}
}
set i = ""
for {
set i = $order(pEntities(i), 1, tEntity)
quit:i=""
set ptr=0, j=0
while $listnext(tEntity,ptr,tValue) {
set j=j+1
continue:'$d(tDescriptor(j),tLabel)
set pTokens(i,tLabel) = tValue
}
if pDomainId && $g(pTokens(i,"id")) {
if '$d(pTokens(i,"frequency")) {
set pTokens(i,"frequency") = ##class(%iKnow.Queries.EntityAPI).GetFrequency(pDomainId, pTokens(i,"id"),,, pFilter)
}
if '$d(pTokens(i,"spread")) {
set pTokens(i,"spread") = ##class(%iKnow.Queries.EntityAPI).GetSpread(pDomainId, pTokens(i,"id"),, pFilter)
}
}
set pTokens(i) = $lg(tEntity,tValueIndex),
pTokens(i,0) = $l(pTokens(i)," ")
for j = 1:1:pTokens(i,0) {
set pTokens(i,j) = $p(pTokens(i)," ",j)
}
set:pTokens(i)=pSeedString pSeedIndex = i
}
}
ClassMethod TokenizeString(Output pTokens, pString As %String)
{
kill pTokens
set pTokens = pString,
pTokens(0) = $l(pString," ")
for j = 1:1:pTokens(0) {
set pTokens(j) = $p(pString," ",j)
}
}
/// Enforce utf-8
ClassMethod %ProcessResult(pSC As %Status, pResult As %DynamicObject = "") As %Status [ Internal ]
{
set %response.CharSet = "utf-8"
quit ##super(pSC, pResult)
}
}