forked from gtfierro/BAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.py
311 lines (259 loc) · 8.81 KB
/
node.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
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
import uuid
import itertools
import networkx as nx
import zope.interface
import gis
import redis
geocache = redis.StrictRedis(host='localhost', port=6379, db=0)
# clean out old keys
for k in geocache.keys():
geocache.delete(k)
def uniquify(l):
c = itertools.count(start=1)
if isinstance(item, Node):
return [item.set_name(item.node_name+" "+str(c.next())) for item in l]
else:
raise Exception("we got here?!")
return [item+" "+str(c.next()) for item in l]
class Node(object):
"""
Inherited class for nodes and objects (below)
Uses uuid as a unique identifier, has optional name
Supports methods add_child, add_parent
"""
class NodeList(list):
def add_child(self, child):
for item in self:
item.add_child(child)
def add_parent(self, parent):
for item in self:
item.add_parent(parent)
@property
def areas(self):
return gis.Area.objects.filter(nodes__in=[x.link for x in self])
def __init__(self, name, external_parents=[], external_childs=[], metadata={}, uid=None):
"""
obj_type: string that conforms to the list of recognized object types
container: Obj or Relational of which this object is a part
name: string name of this object
"""
self.node_name = name
self.external_parents = external_parents
self.external_childs = external_childs
self.uid = uuid.uuid4() if not uid else uid
if isinstance(self.uid, str):
self.uid = uuid.UUID(self.uid)
self.metadata = metadata
self.tags = []
self.link, _ = gis.NodeLink.objects.get_or_create(uuid=self.uid)
#self.container.add_nodes(self)
def get_name(self):
return self.node_name
def __str__(self):
return self.node_name
def __cmp__(self, other):
# use self.uuid to compare to other objects
if isinstance(other, Node):
if isinstance(self.uid, uuid.UUID):
return self.uid.__cmp__(other.uid)
return -1
else:
return -1
def __hash__(self):
#hack to get graph copy working
if hasattr(self,'uid'):
return hash(self.uid)
return object.__hash__(self)
def set_name(self, name):
self.name = name
return self
def validate(self):
pass
def _apply_to_multiple(fxn):
"""
DECORATOR
if we are applying add_child or add_parent to a list of points, then we apply the fxn to each
of those points in turn
"""
def apply_multiple(self, *args):
if isinstance(args[0],list):
for rel in args[0]:
return fxn(self, rel)
else: return fxn(self, *args)
return apply_multiple
@_apply_to_multiple
def add_child(self, child):
"""
Give this node a child w/n the context of it's container graph
child: Device or Obj (which ever this object's type is)
if the target child is part of an external container, then this
node makes note of that
"""
children = [child] if not isinstance(child,list) else child
for child in children:
if child.container != self.container:
self.external_childs.append(child.container)
child.external_parents.append(self.container)
self.container.external_childs.append(child.container)
child.container.external_parents.append(self.container)
self.container.add_node_child(self, child)
@_apply_to_multiple
def add_parent(self, parent):
"""
Give this node a parent w/n the context of it's container graph
parent: Device or Obj (which ever this object's type is)
if the target parent is part of an external container, then this
node makes note of that
"""
parents = list(parent) if not isinstance(parent,list) else parent
for parent in parents:
if parent.container != self.container:
self.external_parents.append(parent.container)
parent.external_childs.append(self.container)
self.container.external_parents.append(parent.container)
parent.container.external_childs.append(self.container)
self.container.add_node_parent(self, parent)
@classmethod
def type(cls):
for interface in zope.interface.implementedBy(cls):
if interface.getName().startswith('D') or interface.getName().startswith('I'):
return interface.getName()[1:]
return cls.__name__
@property
def areas(self):
return self.container.areas if hasattr(self.container,'areas') and not self.link.areas.all() else self.link.areas
def __emittable__(self):
"""Returns a dictionary representation of the object (passed to Web API)"""
import node_types
#TODO: do caching here?
return {
'name': self.node_name,
'type': self.type(),
'uuid': str(self.uid),
'methods': node_types.get_methods(self),
}
class Container(nx.DiGraph):
"""
Inheritable class for handling basic graph operations beyond what networkx provides
"""
def __init__(self, contents):
nx.DiGraph.__init__(self)
self.parents = []
self.children = []
# TODO: can we get rid of the obj stuff?
# if contents: for c in contents, self.add_node(c)
if contents:
for obj in contents:
obj.container = self
self.add_node(obj)
def search(self, fn, retfn=lambda x: x):
"""
searches dfs preorder for nodes for which the function [fn] evaluates to true
It appends all True values to a results list, and applies [retfn] to them
"""
if not self.nodes():
return []
results = []
for nd in nx.dfs_preorder_nodes(self):
if fn(nd):
results.append(retfn(nd))
#if the node is itself a container, we search it too!
if isinstance(nd, Container):
results.extend(nd.search(fn,retfn))
return results
def add_node_child(self, node, child):
if child not in self:
self.add_node(child)
if not self.__contains__(node):
self.add_edge(node,child)
elif not self.has_edge(node,child):
self.add_edge(node,child)
def add_node_parent(self, node, parent):
if parent not in self:
self.add_node(parent)
if not self.__contains__(node):
self.add_edge(parent,node)
elif not self.has_edge(parent, node):
self.add_edge(parent,node)
class Device(Node):
"""
Internal components of a larger object
"""
required_setpoints = []
required_points = []
def __init__(self, name, uid=None):
self.attributes = {}
Node.__init__(self,name,uid=uid)
self.validate()
#print "Device",self.name, self.uid
def validate(self):
req = set(self.required_points)
for k in self.attributes.keys():
k = k.split(' ')[0]
req.discard(k)
if req:
raise NotImplementedError("Required points %s are not provided for %s" %
(str(list(req)), self.name))
def set_attribute(self, att, value):
self.attributes[att] = value
def get_attribute(self, att):
return self.attributes[att]
def del_attribute(self, att):
del self.attributes[att]
def __getitem__(self, att):
return self.get_attribute(att)
def __setitem__(self, att, value):
self.set_attribute(att, value)
def __delitem__(self, att):
self.del_attribute(att)
class Obj(Node, Container):
required_setpoints = []
required_devices = []
def __init__(self, container, name, devices=None, uid=None):
self.container = container
if devices is None:
self.devices = {}
else:
self.devices = dict(itertools.chain(*[zip(uniquify([k] * len(v)), uniquify(v)) if isinstance(v, list) else ((k, v),) for k, v in devices.items()]))
for d in self.devices:
self.devices[d].tags = d.replace(' ','_').split('_')
Node.__init__(self, name,uid=uid)
Container.__init__(self, self.devices.values())
self.container.add_node(self)
self.tags.append(self.type())
self.validate()
#print ">>>Object",self.name, self.uid
def search(self, fn, retfn=lambda x: x):
if not self.nodes():
return []
res = []
for dev in self.nodes():
if fn(dev):
res.append(retfn(dev))
return res
def validate(self):
req = set(self.required_devices)
for k in self.devices.keys():
k = k.split(' ')[0]
req.discard(k)
if req:
raise NotImplementedError("Required devices %s are not provided for %s" %
(str(list(req)), self.name))
def add_area(self, area):
geocache.set(str(self.uid), area)
self.areas.add(area)
def __getitem__(self, key):
if key in self.devices:
return Node.NodeList(self.devices[key]) if isinstance(self.devices[key],list) else self.devices[key]
else:
return Node.NodeList(filter(lambda x: x, [self.devices[k] if key in k else None for k in self.devices]))
class Relational(Container):
def __init__(self, name, objects=[]):
self.domain_name = name
self.uid = uuid.uuid4()
Container.__init__(self, objects)
def get_name(self):
return self.domain_name
#place holder...?
class Domain(Relational):
pass