From 7419904bfe390546ec45bb8645005a343a66f32d Mon Sep 17 00:00:00 2001 From: Gevorg Arutiunian Date: Mon, 27 Aug 2018 17:15:09 +0300 Subject: [PATCH 1/3] Update similar and related graphs --- src/cls/EntityBrowser/API.cls | 155 ++++++++++++++++++++++++++++++---- 1 file changed, 137 insertions(+), 18 deletions(-) diff --git a/src/cls/EntityBrowser/API.cls b/src/cls/EntityBrowser/API.cls index 2b85a07..b374e46 100644 --- a/src/cls/EntityBrowser/API.cls +++ b/src/cls/EntityBrowser/API.cls @@ -1,6 +1,6 @@ /// /// swagger: '2.0' -/// +/// /// info: /// version: "1.0.0" /// title: iKnow REST APIs @@ -10,18 +10,18 @@ /// 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: support@intersystems.com -/// +/// /// consumes: /// - application/json /// produces: /// - application/json -/// -/// +/// +/// /// parameters: /// domainParam: /// name: domain @@ -29,7 +29,7 @@ /// in: path /// description: the ID of the domain to query /// required: true -/// +/// /// definitions: /// RequestObject: /// type: object @@ -49,7 +49,7 @@ /// default: [] /// items: /// type: integer -/// +/// /// Filter: /// # TODO: move into proper hierarchy once swagger-ui supports it /// type: object @@ -100,7 +100,7 @@ Class EntityBrowser.API Extends (%iKnow.REST.Base, %iKnow.REST.Utils) { -Parameter PAGESIZE = 0; +Parameter PAGESIZE = 50; XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ] { @@ -189,15 +189,17 @@ ClassMethod GetSimilar(pDomainId As %Integer, pString As %String = "") As %Statu 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 +#; 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) { @@ -277,15 +279,17 @@ ClassMethod GetRelated(pDomainId As %Integer, pEntity As %String = "") As %Statu 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 + 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) { @@ -294,6 +298,120 @@ ClassMethod GetRelated(pDomainId As %Integer, pEntity As %String = "") As %Statu quit ..%ProcessResult(tSC, $g(tResult)) } +ClassMethod UpdateRelated(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 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") + //w sourceEdge_":"_targetEdge,! + 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) + { + //w " "_sourceEdge_":"_targetEdge_" -> "_sourceEdge_":"_nextEdge.target,! + set obj = { + "source": (sourceEdge), + "target": (nextEdge.target), + "type": "similar" + } + + do edges.%Remove(i) + do nodes.%Remove(targetNode) + //do edges.%Remove(i+1) + do edges.%Push(obj) + set size = size - 1 + set i = i - 1 + + do ..ChengEdges(.edges, targetEdge, sourceEdge, i, size) + } + + } + set i = i + 1 + } + + do pObjects.%Set("edges",edges) + + q 1 +} + +ClassMethod ChengEdges(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" + } + //w " "_sourceEdge_":"_targetEdge_" -> "_source_":"_targetEdge,! + 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 @@ -317,6 +435,7 @@ ClassMethod AppendChildObjects(ByRef pObjects, ByRef pGraph, ByRef pEntityTokens 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)} From b91eaa719376fc0bf95e61092068425d75977ca1 Mon Sep 17 00:00:00 2001 From: Gevorg Arutiunian Date: Tue, 28 Aug 2018 15:25:00 +0300 Subject: [PATCH 2/3] Update class --- src/cls/EntityBrowser/API.cls | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/cls/EntityBrowser/API.cls b/src/cls/EntityBrowser/API.cls index b374e46..468fdb0 100644 --- a/src/cls/EntityBrowser/API.cls +++ b/src/cls/EntityBrowser/API.cls @@ -189,10 +189,10 @@ ClassMethod GetSimilar(pDomainId As %Integer, pString As %String = "") As %Statu 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 + //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) @@ -279,16 +279,16 @@ ClassMethod GetRelated(pDomainId As %Integer, pEntity As %String = "") As %Statu 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 + //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 tSC = ..UpdateRelated(pDomainId, .tObjects, pEntity) set tResult = { "graph": (tObjects) } @@ -328,7 +328,7 @@ ClassMethod UpdateRelated(pDomainId As %Integer, ByRef pObjects As %DynamicObjec set stemSource = ##class(%iKnow.Queries.EntityAPI).GetStem(pDomainId, lableSource, "ru") set stemTarget = ##class(%iKnow.Queries.EntityAPI).GetStem(pDomainId, lableTarget, "ru") - //w sourceEdge_":"_targetEdge,! + if stemSource = stemTarget { if (nextEdge.source '= targetEdge) && (i'=0) @@ -347,7 +347,6 @@ ClassMethod UpdateRelated(pDomainId As %Integer, ByRef pObjects As %DynamicObjec } elseif (targetEdge = nextEdge.source) { - //w " "_sourceEdge_":"_targetEdge_" -> "_sourceEdge_":"_nextEdge.target,! set obj = { "source": (sourceEdge), "target": (nextEdge.target), @@ -356,24 +355,22 @@ ClassMethod UpdateRelated(pDomainId As %Integer, ByRef pObjects As %DynamicObjec do edges.%Remove(i) do nodes.%Remove(targetNode) - //do edges.%Remove(i+1) do edges.%Push(obj) set size = size - 1 set i = i - 1 - do ..ChengEdges(.edges, targetEdge, sourceEdge, i, size) + do ..ChangeEdges(.edges, targetEdge, sourceEdge, i, size) } - } set i = i + 1 } - do pObjects.%Set("edges",edges) + do pObjects.%Set("nodes",nodes) - q 1 + quit $$$OK } -ClassMethod ChengEdges(ByRef Edges As %DynamicArray, target As %Integer, source As %Integer, i As %Integer, size As %Integer) As %Status +ClassMethod ChangeEdges(ByRef Edges As %DynamicArray, target As %Integer, source As %Integer, i As %Integer, size As %Integer) As %Status { while (i>size) { @@ -389,12 +386,11 @@ ClassMethod ChengEdges(ByRef Edges As %DynamicArray, target As %Integer, source "target": (targetEdge), "type": "similar" } - //w " "_sourceEdge_":"_targetEdge_" -> "_source_":"_targetEdge,! + do Edges.%Remove(i) do Edges.%Push(obj) set size = size - 1 } - set i = i + 1 } quit $$$OK From 80d714e040a04cfbf0372990b362de6ff710ac62 Mon Sep 17 00:00:00 2001 From: Gevorg Arutiunian Date: Wed, 29 Aug 2018 21:54:18 +0300 Subject: [PATCH 3/3] Bugs fixed, method UpdateRelated was updated --- src/cls/EntityBrowser/API.cls | 89 ++++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 11 deletions(-) diff --git a/src/cls/EntityBrowser/API.cls b/src/cls/EntityBrowser/API.cls index 468fdb0..16e0149 100644 --- a/src/cls/EntityBrowser/API.cls +++ b/src/cls/EntityBrowser/API.cls @@ -100,7 +100,7 @@ Class EntityBrowser.API Extends (%iKnow.REST.Base, %iKnow.REST.Utils) { -Parameter PAGESIZE = 50; +Parameter PAGESIZE = 100; XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ] { @@ -300,9 +300,12 @@ ClassMethod GetRelated(pDomainId As %Integer, pEntity As %String = "") As %Statu 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 @@ -328,7 +331,17 @@ ClassMethod UpdateRelated(pDomainId As %Integer, ByRef pObjects As %DynamicObjec 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) @@ -347,15 +360,10 @@ ClassMethod UpdateRelated(pDomainId As %Integer, ByRef pObjects As %DynamicObjec } elseif (targetEdge = nextEdge.source) { - set obj = { - "source": (sourceEdge), - "target": (nextEdge.target), - "type": "similar" - } + set edge.target = nextEdge.target - do edges.%Remove(i) + do edges.%Remove(i+1) do nodes.%Remove(targetNode) - do edges.%Push(obj) set size = size - 1 set i = i - 1 @@ -364,9 +372,68 @@ ClassMethod UpdateRelated(pDomainId As %Integer, ByRef pObjects As %DynamicObjec } set i = i + 1 } - do pObjects.%Set("edges",edges) - do pObjects.%Set("nodes",nodes) + 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 }