-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.py
More file actions
421 lines (357 loc) · 12.5 KB
/
Graph.py
File metadata and controls
421 lines (357 loc) · 12.5 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
from Element import *
import copy
from clockdeco import clock
class Edge:
""" Edge labels are assumed to be function-free and ground, and there can be no edge otherwise"""
__slots__ = 'source', 'sink', 'label'
def __init__(self, source, sink, label):
self.source= source
self.sink = sink
self.label = label
def isConsistent(self, other):
if self.source.isConsistent(other.source) and self.sink.isConsistent(other.sink) and self.label == other.label:
return True
return False
def isEquivalent(self, other):
if self.source.isEquivalent(other.source) and self.sink.isEquivalent(other.sink) and self.label == other.label:
return True
return False
def __eq__(self, other):
if other is None:
return False
if self.source.ID == other.source.ID and self.sink.ID == other.sink.ID and self.label == other.label:
return True
return False
def __ne__(self, other):
return (not self.__eq__(other))
def __hash__(self):
return hash(self.source.ID) ^ hash(self.sink.ID) ^ hash(self.label)
def assign(self, endpoint, new_val):
new_val.ID = self.endpoint.ID
self.endpoint = new_val
def merge(self, other):
"""Merges source and sink"""
if not self.isConsistent(other):
return None
self.source.merge(other.source)
self.sink.merge(other.sink)
return self
def swapSink(self,sink):
self.sink = sink
return self
def __repr__(self):
return 'Edge {} --{}--> {}'.format(self.source, self.label, self.sink)
class Graph(Element):
"""A graph is an element with elements, edges, and restrictions"""
def __init__(self, ID, typ, name=None, Elements=None, Edges=None, Restrictions=None):
if Elements == None:
Elements = set()
if Edges == None:
Edges = set()
if Restrictions == None:
Restrictions = set()
super(Graph, self).__init__(ID, typ, name)
self.elements = Elements
self.edges = Edges
self.subgraphs = Restrictions
def __len__(self):
return len(self.elements)
def __iter__(self):
elms = iter(self.elements)
yield next(elms)
def getElementById(self, ID):
for element in self.elements:
if element.ID == ID:
return element
return None
def getElmByRID(self, ID):
for element in self.elements:
if element.replaced_ID == ID:
return element
for edge in self.edges:
if edge.source.replaced_ID == ID:
return edge.source
if edge.sink.replaced_ID == ID:
return edge.sink
return None
def replaceWith(self, oldsnk, newsnk):
''' removes oldsnk from self.elements, replaces all edges with snk = oldsnk with newsnk'''
if oldsnk == newsnk:
return
if self.getElementById(newsnk.ID) is None:
raise NameError('newsnk replacer is not found in self')
if oldsnk in self.elements:
self.elements.remove(oldsnk)
for incoming in (edge for edge in self.edges if edge.sink == oldsnk):
incoming.sink = newsnk
#update constraint edges which might reference specific elements being replaced
for r in self.subgraphs:
for r_edge in r.edges:
if r_edge.source == oldsnk:
if r_edge.source in r.elements:
r.elements.add(newsnk)
r.replaceWith(r_edge.source, newsnk)
if r_edge.sink == oldsnk:
if r_edge.sink in r.elements:
r.elements.add(newsnk)
r.replaceWith(r_edge.sink, newsnk)
return self
def assign(self, old_elm_in_edge, new_elm, remove_old=True):
if new_elm not in self.elements:
self.elements.add(new_elm)
if remove_old:
self.elements.remove(old_elm_in_edge)
edges = iter(self.edges)
for edge in edges:
if edge.source == old_elm_in_edge:
self.edges.add(Edge(new_elm, edge.sink, edge.label))
self.edges.remove(edge)
if edge.sink == old_elm_in_edge:
self.edges.add(Edge(edge.source, new_elm, edge.label))
self.edges.remove(edge)
for r in self.subgraphs:
if r.name == 'Restriction':
r.assign(old_elm_in_edge, new_elm)
def getEdgesByLabel(self, label):
return {edge for edge in self.edges if edge.label == label}
def getEdgesByIdsAndLabel(self, source_id, sink_id, label):
return {edge for edge in self.edges if edge.source.ID == source_id and edge.sink.ID == sink_id and edge.label == label}
def getIncidentEdges(self, element):
return {edge for edge in self.edges if edge.source == element}
def getNeighbors(self, element):
return {edge.sink for edge in self.edges if edge.source.ID == element.ID}
def getEstablishingParent(self, element):
return next(iter(edge.source for edge in self.edges if edge.sink == element and edge.label == 'effect-of'))
def getParents(self, element):
return set(edge.source for edge in self.edges if edge.sink == element)
def getNeighborsByLabel(self, element, label):
return {edge.sink for edge in self.edges if edge.source.ID == element.ID and edge.label == label}
def getIncidentEdgesByLabel(self, element, label):
return {edge for edge in self.edges if edge.source.ID == element.ID and edge.label == label}
def getParentsByLabel(self, element, label):
return set(edge.source for edge in self.edges if edge.sink is element and edge.label is label)
def getIncomingEdges(self, element):
return {edge for edge in self.edges if edge.sink == element}
def getIncomingEdgesByType(self, element, typ):
return {edge for edge in self.edges if edge.sink == element and edge.source.typ == typ}
def getIncomingEdgesByTypeAndLabel(self, element, typ, label):
return {edge for edge in self.edges if edge.sink == element and edge.source.typ == typ and edge.label == label}
###### rGet ####################
def rGetDescendants(self, element, Descendants=None):
if Descendants == None:
Descendants = set()
Descendants.add(element)
#Base Case
incidentEdges = self.getIncidentEdges(element)
if len(incidentEdges) == 0:
return Descendants
#Induction
for edge in incidentEdges:
#Descendants.add(edge.sink)
Descendants = self.rGetDescendants(edge.sink, Descendants)
return Descendants
def rGetDescendantEdges(self, element, Descendant_Edges=None):
if Descendant_Edges == None:
Descendant_Edges = set()
#Base Case
incident_Edges = self.getIncidentEdges(element)
if len(incident_Edges) == 0:
return Descendant_Edges
#Induction
Descendant_Edges= Descendant_Edges.union(incident_Edges)
for edge in incident_Edges:
Descendant_Edges = self.rGetDescendantEdges(edge.sink, Descendant_Edges)
return Descendant_Edges
def isConsistentSubgraph(self, cndt_subgraph, return_map=False):
"""
@param other: a graph which may be a consistent subgraph of self
@param return_map
@return: if for each other.edge, there is a consistent self.edge, following the shared-endpoints rule of edge sets
"""
possible_map = isConsistentEdgeSet(Rem=copy.deepcopy(cndt_subgraph.edges), Avail=copy.deepcopy(self.edges),
return_map=return_map)
if not possible_map is False:
#returns True when return_map is False
#return_map = possible_map
return possible_map
return False
def findConsistentSubgraph(self, cndt_subgraph):
return findConsistentEdgeMap(Rem = copy.deepcopy(cndt_subgraph.edges), Avail = copy.deepcopy(self.edges))
def isInternallyConsistent(self):
return not self.equivalentWithRestrictions()
def equivalentWithRestrictions(self):
if not hasattr(self, 'subplans') or len(self.subplans) == 0:
return False
for restriction in self.subgraphs:
if restriction.type_graph != 'Restriction':
continue
if restriction.isIsomorphicSubgraphOf(self):
return True
return False
def __repr__(self):
edges = str([edge for edge in self.edges])
elms = str([elm for elm in self.elements])
return '\n' + edges + '\n\n_____\n\n ' + elms + '\n'
################################################################
# consistent edge sets following shared endpoints clause ####
################################################################
def isConsistentEdgeSet(Rem, Avail, map_=None, return_map=False):
if map_ == None:
map_ = {}
#Base Case - all Remaining edges
if len(Rem) == 0:
if return_map:
return map_
return True
edge_match = Rem.pop()
cndt_edges = {edge for edge in Avail if edge.isConsistent(edge_match)}
if edge_match.source in map_:
cndt_edges -= {edge for edge in cndt_edges if not edge.source == map_[edge_match.source]}
if edge_match.sink in map_:
cndt_edges -= {edge for edge in cndt_edges if not edge.sink == map_[edge_match.sink]}
if len(cndt_edges) == 0:
return False
for cndt in cndt_edges:
Map_ = copy.deepcopy(map_)
if not cndt.source in map_:
Map_[edge_match.source] = cndt.source
if not cndt.sink in map_:
Map_[edge_match.sink] = cndt.sink
_Map = isConsistentEdgeSet(copy.deepcopy(Rem), Avail-{cndt}, Map_, return_map)
if not _Map is False:
if return_map:
return _Map
#if isConsistentEdgeSet(copy.deepcopy(Rem), Avail-{cndt}, Map_):
# if return_map:
# return Map_
# return True
return False
def findConsistentEdgeMap(Rem, Avail, map_ = None, Super_Maps = None):
if map_ is None:
map_ = {}
if Super_Maps is None:
Super_Maps = []
#Base Case - all Remaining edges
if len(Rem) == 0:
Super_Maps.append(map_)
return Super_Maps
edge_match = Rem.pop()
cndt_edges = {edge for edge in Avail if edge.isConsistent(edge_match)}
if edge_match.source in map_:
cndt_edges -= {edge for edge in cndt_edges if not edge.source == map_[edge_match.source]}
if edge_match.sink in map_:
cndt_edges -= {edge for edge in cndt_edges if not edge.sink == map_[edge_match.sink]}
if len(cndt_edges) == 0:
return Super_Maps
for cndt in cndt_edges:
Map_ = copy.deepcopy(map_)
if not cndt.source in map_:
Map_[edge_match.source] = cndt.source
if not cndt.sink in map_:
Map_[edge_match.sink] = cndt.sink
findConsistentEdgeMap(copy.deepcopy(Rem), Avail, Map_, Super_Maps)
return Super_Maps
#A method - unify - which given two graphs, will merge. currently performed by mergeGraph
# def UnifyActions(_Map = None, R = None, A = None):
# """
#
# @param _Map: dictonary
# @param R: edges to account for
# @param A: edges which account as
# @return: dictionary _Map
# """
#
# if _Map ==None:
# _Map = {} ;#_Map is a 1:1 mapping (r : a) for r in "R" for a in "A" s.t. every edge in "R" has one partner in "A"
# #Mapping is a dictionary.
# if R == None:
# R = [] ;#"R" is the set of edges all of whose edges must be accounted for
# if A == None:
# A = [] ;#"A" is the set of edges which account for edges in "R".
#
# if len(R) == 0:
# return _Map
#
# rem = R.pop()
# cndts = {edge for edge in A if edge.isConsistent(rem)}
#
# if rem.source in _Map:
# cndts -= {edge for edge in cndts if not edge.source == _Map[rem.source]}
# if rem.sink in _Map:
# cndts -= {edge for edge in cndts if not edge.sink == _Map[rem.sink]}
#
# if len(cndts) == 0:
# return []
#
# Mbins = []
# for cndt in cndts:
# Map_ = copy.deepcopy(_Map)
# if not cndt.source in _Map:
# Map_[rem.source] = cndt.source
# if not cndt.sink in _Map:
# Map_[rem.sink] = cndt.sink
#
# #if this 'cndt' was to account for 'rem', recursively solve for rest of R and append all possible worlds in []
# M_ = isConsistentEdgeSet(Map_ = _Map, R = copy.deepcopy(R), A = A-{cndt})
# Mbins = consistentMaps(prior_maps=Map_,cndt_maps = M_, Mbins = Mbins)
#
# if len(Mbins) == 0:
# return []
#
# return _Map.extend(Mbins)
import unittest
class TestGraph(unittest.TestCase):
pass
# def test_consistent_edge_set(self):
# """
# Full Graph
# 1 --> 2 --> 3 --> 5
# 2 --> 4 --> 5
#
# Requirements
# [2] --> [3]
# [2] --> [4]
#
#
# """
# G = ['buffer',
# Element(ID=1,name=1, typ='1'),
# Element(ID=2,name=2, typ='2'),
# Element(ID=3,name=3, typ='3'),
# Element(ID=4,name=4, typ='4'),
# Element(ID=5,name=5, typ='5')]
# O = [Element(ID=20, typ='2'),
# Element(ID=30, typ='3'),
# Element(ID=40, typ='4')]
#
# Avail = {Edge(G[1],G[2],'a'),
# Edge(G[2],G[3], 'b'),
# Edge(G[2],G[4], 'c'),
# Edge(G[3],G[5], 'd'),
# Edge(G[4],G[5], 'e')}
# Rem = {
# Edge(O[0],O[1], 'b'),
# Edge(O[0],O[2], 'c')}
#
#
# isit = isConsistentEdgeSet(Rem, Avail)
# assert(isit)
# assert(not isConsistentEdgeSet(Avail, Rem))
# print(isit)
#
# #With LARGER example to look through
# G = ['buffer']
# G+= [Element(ID=i, name=i, typ=str(i)) for i in range(1,900)]
# Avail = {Edge(G[i],G[i+1],'m') for i in range(1,700)}
# Avail.update({Edge(G[1],G[2],'a'),
# Edge(G[2],G[3], 'b'),
# Edge(G[2],G[4], 'c'),
# Edge(G[3],G[5], 'd'),
# Edge(G[4],G[5], 'e')})
#
# isit = isConsistentEdgeSet(Rem, Avail)
# assert (isit)
# print(isit)
if __name__ == '__main__':
pass
#unittest.main()