-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyceliumNode.py
152 lines (118 loc) · 5.31 KB
/
MyceliumNode.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
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
import uuid
class MyceliumNode:
identifiers = {}
properties = {}
parent_node_id = None
child_node_ids = []
relationships = None
id = None
def __init__(self):
self.id = uuid.uuid4()
self.identifiers = {}
self.properties = {}
self.parent_node_id = None
self.relationships = relationsShipByType()
def getid(self):
return self.id
def getIdentifiers(self):
return self.identifiers
def addChildId(self, child_id):
if child_id not in self.child_node_ids:
self.child_node_ids.append(child_id)
def getParentNodeId(self):
return self.parent_node_id
def setParentNodeId(self, parent_node_id):
self.parent_node_id = parent_node_id
def addIdentifier(self, identifier_type, identifier_value):
self.identifiers[identifier_type] = identifier_value
def getIdentifier(self, identifier_type):
if identifier_type in self.identifiers.keys():
return self.identifiers[identifier_type]
else:
return None
def addProperty(self, property_value, property_type):
self.properties[property_type] = property_value
def getProperty(self, property_type):
if property_type in self.properties.keys():
return self.properties[property_type]
else:
return None
def addRelated(self,realtionship_type, related_id):
self.relationships.addRelated(realtionship_type, related_id)
def print(self):
print("UUID: %s Identifier: %s" %(format(self.id), format(self.identifiers)))
self.relationships.print()
class relationsShipByType:
relationshipdict = {}
def __init__(self):
self.relationshipdict = {}
def getRelatedByList(self, realtionship_type):
if realtionship_type in self.relationshipdict:
return self.relationshipdict[realtionship_type]
return None
def addRelated(self, realtionship_type, related_id):
if realtionship_type in self.relationshipdict:
related_ids = self.relationshipdict[realtionship_type]
if related_id not in related_ids:
related_ids.append(related_id)
self.relationshipdict[realtionship_type] = related_ids
else:
related_ids = []
related_ids.append(related_id)
self.relationshipdict[realtionship_type] = related_ids
def print(self):
for k in self.relationshipdict.keys():
print("%s, %s" %(k, format(self.relationshipdict[k])))
class MyceliumNodeList:
nodeList = None
def __init__(self):
self.nodeList = {}
def addNode(self, identifier_type, identifier_value):
#print("add %s,%s" %(identifier_type, identifier_value))
node_id = self.findNode(identifier_type, identifier_value)
if node_id is None:
node = MyceliumNode()
node.addIdentifier(identifier_type, identifier_value)
self.nodeList[node.getid()] = node
else:
node = self.nodeList[node_id]
node.addIdentifier(identifier_type, identifier_value)
self.nodeList[node.getid()] = node
return node.getid()
def updateNode(self, identifier_type, identifier_value, some_other_identifier_type, some_other_identifier_value):
#print("add other Identifier %s,%s ____ %s,%s" %(identifier_type, identifier_value, some_other_identifier_type, some_other_identifier_value))
node_id = self.findNode(identifier_type, identifier_value)
same_node_id = self.findNode(some_other_identifier_type, some_other_identifier_value)
#print(node_id, same_node_id)
if node_id is None and same_node_id is None:
node = MyceliumNode()
node.addIdentifier(identifier_type, identifier_value)
node.addIdentifier(some_other_identifier_type, some_other_identifier_value)
self.nodeList[node.getid()] = node
return node.getid()
elif same_node_id is None:
node = self.nodeList.get(node_id)
node.addIdentifier(some_other_identifier_type, some_other_identifier_value)
self.nodeList[node.getid()] = node
return node.getid()
elif node_id is None:
node = self.nodeList.get(same_node_id)
node.addIdentifier(identifier_type, identifier_value)
self.nodeList[node.getid()] = node
return node.getid()
def addRelatedNode(self, identifier_type, identifier_value, relationship_type, child_identifier_type, child_identifier_value):
#print("add Related Node %s,%s __[%s]__ %s,%s" %(identifier_type, identifier_value, relationship_type, child_identifier_type, child_identifier_value))
node_id = self.findNode(identifier_type, identifier_value)
child_node_id = self.findNode(child_identifier_type, child_identifier_value)
node = self.nodeList[node_id]
node.addRelated(relationship_type, child_node_id)
self.nodeList[node_id] = node
def findNode(self, identifier_type, identifier_value):
if len(self.nodeList) == 0:
return None
else:
for n in self.nodeList.keys():
node = self.nodeList.get(n)
if node.getIdentifier(identifier_type) == identifier_value:
return n
return None