Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gevorg95 update graph #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 192 additions & 10 deletions src/cls/EntityBrowser/API.cls
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <example language="swagger">
/// swagger: '2.0'
///
///
/// info:
/// version: "1.0.0"
/// title: iKnow REST APIs
Expand All @@ -10,26 +10,26 @@
/// 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
Expand All @@ -49,7 +49,7 @@
/// default: []
/// items:
/// type: integer
///
///
/// Filter:
/// # TODO: move into proper hierarchy once swagger-ui supports it
/// type: object
Expand Down Expand Up @@ -100,7 +100,7 @@
Class EntityBrowser.API Extends (%iKnow.REST.Base, %iKnow.REST.Utils)
{

Parameter PAGESIZE = 0;
Parameter PAGESIZE = 100;

XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
Expand Down Expand Up @@ -197,7 +197,9 @@ ClassMethod GetSimilar(pDomainId As %Integer, pString As %String = "") As %Statu
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) {
Expand Down Expand Up @@ -285,7 +287,9 @@ ClassMethod GetRelated(pDomainId As %Integer, pEntity As %String = "") As %Statu
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) {
Expand All @@ -294,6 +298,183 @@ 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
{
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
Expand All @@ -317,6 +498,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)}
Expand Down