-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_library.py
38 lines (33 loc) · 1.26 KB
/
graph_library.py
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
from klighd_types.klighd.SKGraphSchema import *
from rendering_library import addText
def createGraph(uri):
graph = SKGraph(id=uri,revision=1)
return graph
def createNode(parent):
if (parent.children == None):
parent.children = []
nodeId = f"{parent.id}$$N{len(parent.children)}"
if parent.type == "graph":
nodeId = "$root"
node = SKNode(id=nodeId)
parent.children.append(node)
return node
def createLabel(parent, text):
if (parent.children == None):
parent.children = []
labelId = f"{parent.id}$$L{len(parent.children)}"
label = SKLabel(id=labelId, text=text)
parent.children.append(label)
addText(label, text)
return label
# Create an edge from source to target inside the parent.
def createEdge(parent, source, target):
# TODO: the semantics of the ID might require it to be the index in relation to all edges, not also the nodes. Same in node position if the node contains edges.
edgeId = f"{source.id}$$E{len(parent.children)}"
edge = SKEdge(id=edgeId, sourceId=source.id, targetId=target.id)
parent.children.append(edge)
return edge
def addProperty(element, key, value):
if (element.properties == None):
element.properties = {}
element.properties[key] = value