Skip to content
Open
Show file tree
Hide file tree
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
47 changes: 25 additions & 22 deletions CommonGraphDiffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import copy
from xml.dom import minidom
import codecs
import cmp


def getMetaXML(metaData):
Expand Down Expand Up @@ -120,7 +121,7 @@ def booleanObjectLists(objType, selfObjs, otherObjs):
for selfObj in objsIntersection:
myDict = recursive_dict(selfObj.MetaData)
otherObj = [obj for obj in otherObjs if obj.InstanceGuid == selfObj.InstanceGuid][0]
if( cmp(recursive_dict(otherObj.MetaData), myDict) == 0): #it's the samE!
if recursive_dict(otherObj.MetaData) == myDict: # it's the same!
objsSame.append(otherObj)
else:
objsChanged.append(otherObj)
Expand Down Expand Up @@ -166,21 +167,21 @@ def add(self, objType, obj):
# print "list of all NodeGUIs", [n.InstanceGuid for n in self.Nodes]
parentNode = [n for n in self.Nodes if n.InstanceGuid == obj.ParentGuid][0]
except Exception as e:
print e
print(e)
raise ValueError("Invalid input! Apply of diff not possible")
parentNode.addPort(obj)
return True

def removeObj(self, objType, objGuid, objParentGuid=None):
if(objType == "node"):
if objType == "node":
objList = self.Nodes
if(objType == "edge"):
elif objType == "edge":
objList = self.Edges
if(objType == "port"):
elif objType == "port":
try:
parentNode = [node for node in self.Nodes if node.InstanceGuid == objParentGuid][0]
objList = parentNode.Ports
except Exception, e:
except Exception as e:
# this sometimes doesn't work because parentNode was already deleted - and that's okay!
return True
try:
Expand All @@ -189,33 +190,35 @@ def removeObj(self, objType, objGuid, objParentGuid=None):
return False
return True

def changeObj(self, objType, obj, objParentGuid = None):
if(objType == "node"):
def changeObj(self, objType, obj, objParentGuid=None):
if objType == "node":
objList = self.Nodes
if(objType == "edge"):
elif objType == "edge":
objList = self.Edges
if(objType == "port"):
elif objType == "port":
try:
parentNode = [node for node in self.Nodes if node.InstanceGuid == objParentGuid][0]
objList = parentNode.Ports
except Exception, e:
except Exception as e:
# if this doesn't work, this is bad
return False
try:
for idx, thisN in enumerate(objList):
if obj == thisN:
objList[idx].MetaData = obj.MetaData
else: return False
break
else:
return False
return True
except Exception, e:
except Exception as e:
return False

def getAllPorts(self):
return [port for node in self.Nodes for port in node.Ports]

def diff(self, other):
# If the metadata changed, include the other's metadata
if cmp(recursive_dict(self.MetaData), recursive_dict(other.MetaData)) !=0:
if recursive_dict(self.MetaData) != recursive_dict(other.MetaData):
thisDiffSet = DiffSet(other.MetaData)
else:
thisDiffSet = DiffSet()
Expand Down Expand Up @@ -266,7 +269,7 @@ def applyDiff(self, diffSet):
newCG = copy.deepcopy(self)

for thisNodeChange in [change for change in diffSet.Changes if change.__class__.__name__ == "NodeChange"]:
print thisNodeChange
print(thisNodeChange)
if(thisNodeChange.Status == "added"):
newCG.add("node", Node.addFromChange(thisNodeChange))
if(thisNodeChange.Status == "removed"):
Expand All @@ -275,7 +278,7 @@ def applyDiff(self, diffSet):
newCG.changeObj("node", Node.addFromChange(thisNodeChange))

for thisPortChange in [change for change in diffSet.Changes if change.__class__.__name__ == "PortChange"]:
print thisPortChange
print(thisPortChange)
if(thisPortChange.Status == "added"):
newCG.add("port", Port.addFromChange(thisPortChange))
if(thisPortChange.Status == "removed"):
Expand All @@ -284,7 +287,7 @@ def applyDiff(self, diffSet):
newCG.changeObj("port", Port.addFromChange(thisPortChange), objParentGuid = thisPortChange.ParentGuid)

for thisEdgeChange in [change for change in diffSet.Changes if change.__class__.__name__ == "EdgeChange"]:
print thisEdgeChange
print(thisEdgeChange)
if(thisEdgeChange.Status == "added"):
newCG.add("edge", Edge.addFromChange(thisEdgeChange))
if(thisEdgeChange.Status == "removed"):
Expand Down Expand Up @@ -339,9 +342,9 @@ def getGraphVizRep(node):
def __repr__(self):
s = '\n (#) Node (InstanceGuid: ' + self.InstanceGuid + ' Type: ' + self.Type
if self.Position is not None:
s += ' Position:' + etree.tostring(self.Position)
s += ' Position:' + etree.tostring(self.Position).decode()
if self.MetaData is not None:
s += ' MetaData: ' + etree.tostring(self.MetaData)
s += ' MetaData: ' + etree.tostring(self.MetaData).decode()
s += ' ) \n' + '\n'.join([str(p) for p in self.Ports])
return s

Expand Down Expand Up @@ -377,7 +380,7 @@ def __eq__(self, other):
def __repr__(self):
s = '\n * Port (InstanceGuid: ' + self.InstanceGuid + ' ParentGuid: ' + self.ParentGuid
if self.MetaData is not None:
s += ' MetaData: ' + etree.tostring(self.MetaData)
s += ' MetaData: ' + etree.tostring(self.MetaData).decode()
s += ' )'
return s
@classmethod
Expand Down Expand Up @@ -462,7 +465,7 @@ def CGToXML(cg, fileName):

def prettify(elem):
# Return a pretty-printed XML string for the Element.
rough_string = etree.tostring(elem, 'utf-8')
rough_string = etree.tostring(elem, 'utf-8').decode()
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent="\t")

Expand All @@ -485,6 +488,6 @@ def XMLToDS(fileName):
elif xmlChange.tag == "EdgeChange":
change = EdgeChange(status, xmlChange.get("InstanceGuid"), xmlChange.get("SrcGuid"), xmlChange.get("DstGuid"))
else:
print "!!!!!unknown tag", xmlChange.tag
print("!!!!!unknown tag {}".format(xmlChange.tag))
thisDiffSet.addChange(change)
return thisDiffSet
23 changes: 11 additions & 12 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@

def main():
CGA = cgd.CgxToObject("examples/simple_multiply_example.cgx")

CGB = cgd.CgxToObject("examples/simple_multiply_example_b.cgx")
ds = CGA.diff(CGB)
cgd.DSToXML(ds, "foo.dsx")
ds2 = cgd.XMLToDS("foo.dsx")
cgd.DSToXML(ds2, "foo2.dsx")

print "========================="
print("=========================")
CGB2 = CGA.applyDiff(ds)
print "========================="
print("=========================")
CGB3 = CGA.applyDiff(ds2)

print "=CGA========================"
print CGA
print "=CGB========================"
print CGB
print "=CGB2========================"
print CGB2
print "=CGB3========================"
print CGB3
print "========================="
print("=CGA========================")
print(CGA)
print("=CGB========================")
print(CGB)
print("=CGB2========================")
print(CGB2)
print("=CGB3========================")
print(CGB3)
print("=========================")

if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion diffgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def main():
CGA = cgd.CgxToObject(args.cg1)
CGB = cgd.CgxToObject(args.cg2)
ds = CGA.diff(CGB)
print ds
print(ds)
cgd.DSToXML(ds, args.ds)

if __name__ == "__main__":
Expand Down
57 changes: 44 additions & 13 deletions foo.dsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
<DiffSet><NodeChange InstanceGuid="98442096-8594-4bc9-b9b5-38e84dbdedac" Status="removed" /><NodeChange InstanceGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Status="changed" Type="b8963bb1-aa57-476e-a20e-ed6cf635a49c"><MetaData><Inspect>"I'm a Horse"</Inspect>
</MetaData></NodeChange><NodeChange InstanceGuid="5a4631f8-b018-469a-b175-391d5dd9a769" Status="changed" Type="57da07bd-ecab-415d-9d86-af36d7073abc"><MetaData><Inspect>"I'm a Cloud"</Inspect>
</MetaData></NodeChange><NodeChange InstanceGuid="26aacdcb-06cf-468e-82c8-996c191f5284" Status="changed" Type="57da07bd-ecab-415d-9d86-af36d7073abc"><MetaData><Inspect>"I'm a Cloud"</Inspect>
</MetaData></NodeChange><NodeChange InstanceGuid="34d122f2-91bd-406e-82d8-8654d44098ed" Status="changed" Type="3e8ca6be-fda8-4aaf-b5c0-3c54c8bb7312"><MetaData><Inspect>"I'm a Cloud"</Inspect>
</MetaData></NodeChange><NodeChange InstanceGuid="MAGICa0f78302-d79e-46b2-af23-b959da359475" Status="changed" Type="3e8ca6be-fda8-4aaf-b5c0-3c54c8bb7312"><MetaData><Inspect>"I'm a Cloud"</Inspect>
</MetaData></NodeChange><EdgeChange InstanceGuid="5a4631f8-b018-469a-b175-391d5dd9a769|7b52c449-1c50-4675-b009-ed74c6dd703a" Status="removed" /><EdgeChange InstanceGuid="34d122f2-91bd-406e-82d8-8654d44098ed|7b52c449-1c50-4675-b009-ed74c6dd703a" Status="removed" /><EdgeChange DstGuid="7b52c449-1c50-4675-b009-ed74c6dd703a" InstanceGuid="3ab31f8-b018-469a-b175-391d5dd9a769|7b52c449-1c50-4675-b009-ed74c6dd703a" SrcGuid="3ab31f8-b018-469a-b175-391d5dd9a769" Status="added" /><PortChange InstanceGuid="037acdd6-1292-4daf-80c7-f80c5db2e283" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Status="removed" /><PortChange InstanceGuid="98442096-8594-4bc9-b9b5-38e84dbdedac" ParentGuid="98442096-8594-4bc9-b9b5-38e84dbdedac" Status="removed" /><PortChange InstanceGuid="monkeymonkeymonkey37acdd6-1292-4daf-80c7-f80c5db2e283" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Status="added"><MetaData><Inspect>"I'm a Monkey"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="7b52c449-1c50-4675-b009-ed74c6dd703a" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Status="changed"><MetaData><Inspect>"I'm a Frog"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="def6138f-7fb2-4d20-89c6-7e3d55d38cbe" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Status="changed"><MetaData><Inspect>"I'm a Frog"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="5a4631f8-b018-469a-b175-391d5dd9a769" ParentGuid="5a4631f8-b018-469a-b175-391d5dd9a769" Status="changed"><MetaData><Inspect>"I'm a Frog"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="26aacdcb-06cf-468e-82c8-996c191f5284" ParentGuid="26aacdcb-06cf-468e-82c8-996c191f5284" Status="changed"><MetaData><Inspect>"I'm a Frog"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="34d122f2-91bd-406e-82d8-8654d44098ed" ParentGuid="34d122f2-91bd-406e-82d8-8654d44098ed" Status="changed"><MetaData><Inspect>"I'm a Frog"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="MAGICa0f78302-d79e-46b2-af23-b959da359475" ParentGuid="MAGICa0f78302-d79e-46b2-af23-b959da359475" Status="changed"><MetaData><Inspect>"I'm a MAGIC UNICORN"</Inspect>
</MetaData></PortChange></DiffSet>
<?xml version="1.0" ?>
<DiffSet>
<NodeChange Status="removed" InstanceGuid="98442096-8594-4bc9-b9b5-38e84dbdedac"/>
<NodeChange Status="added" InstanceGuid="FUNKYFRESHa0f78302-d79e-46b2-af23-b959da359475" Type="3e8ca6be-fda8-4aaf-b5c0-3c54c8bb7312">
<MetaData>
<Inspect>&quot;I'm a glowing crystal&quot;</Inspect>


</MetaData>
</NodeChange>
<NodeChange Status="changed" InstanceGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Type="b8963bb1-aa57-476e-a20e-ed6cf635a49c">
<MetaData>
<Inspect>&quot;I'm a Horse&quot;</Inspect>


</MetaData>
</NodeChange>
<EdgeChange Status="removed" InstanceGuid="5a4631f8-b018-469a-b175-391d5dd9a769|7b52c449-1c50-4675-b009-ed74c6dd703a"/>
<EdgeChange Status="removed" InstanceGuid="34d122f2-91bd-406e-82d8-8654d44098ed|7b52c449-1c50-4675-b009-ed74c6dd703a"/>
<EdgeChange Status="added" InstanceGuid="3ab31f8-b018-469a-b175-391d5dd9a769|7b52c449-1c50-4675-b009-ed74c6dd703a" SrcGuid="3ab31f8-b018-469a-b175-391d5dd9a769" DstGuid="7b52c449-1c50-4675-b009-ed74c6dd703a"/>
<PortChange Status="removed" InstanceGuid="037acdd6-1292-4daf-80c7-f80c5db2e283" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c"/>
<PortChange Status="removed" InstanceGuid="98442096-8594-4bc9-b9b5-38e84dbdedac" ParentGuid="98442096-8594-4bc9-b9b5-38e84dbdedac"/>
<PortChange Status="added" InstanceGuid="monkeymonkeymonkey37acdd6-1292-4daf-80c7-f80c5db2e283" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c">
<MetaData>
<Inspect>&quot;I'm a Monkey&quot;</Inspect>


</MetaData>
</PortChange>
<PortChange Status="added" InstanceGuid="FUNKYFRESHa0f78302-d79e-46b2-af23-b959da359475" ParentGuid="FUNKYFRESHa0f78302-d79e-46b2-af23-b959da359475">
<MetaData>
<Inspect>&quot;I'm a cold bottle of beer&quot;</Inspect>


</MetaData>
</PortChange>
<PortChange Status="changed" InstanceGuid="MAGICa0f78302-d79e-46b2-af23-b959da359475" ParentGuid="MAGICa0f78302-d79e-46b2-af23-b959da359475">
<MetaData>
<Inspect>&quot;I'm a MAGIC UNICORN&quot;</Inspect>


</MetaData>
</PortChange>
</DiffSet>
67 changes: 54 additions & 13 deletions foo2.dsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
<DiffSet><NodeChange InstanceGuid="98442096-8594-4bc9-b9b5-38e84dbdedac" Status="removed" /><NodeChange InstanceGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Status="changed" Type="b8963bb1-aa57-476e-a20e-ed6cf635a49c"><MetaData><Inspect>"I'm a Horse"</Inspect>
</MetaData></NodeChange><NodeChange InstanceGuid="5a4631f8-b018-469a-b175-391d5dd9a769" Status="changed" Type="57da07bd-ecab-415d-9d86-af36d7073abc"><MetaData><Inspect>"I'm a Cloud"</Inspect>
</MetaData></NodeChange><NodeChange InstanceGuid="26aacdcb-06cf-468e-82c8-996c191f5284" Status="changed" Type="57da07bd-ecab-415d-9d86-af36d7073abc"><MetaData><Inspect>"I'm a Cloud"</Inspect>
</MetaData></NodeChange><NodeChange InstanceGuid="34d122f2-91bd-406e-82d8-8654d44098ed" Status="changed" Type="3e8ca6be-fda8-4aaf-b5c0-3c54c8bb7312"><MetaData><Inspect>"I'm a Cloud"</Inspect>
</MetaData></NodeChange><NodeChange InstanceGuid="MAGICa0f78302-d79e-46b2-af23-b959da359475" Status="changed" Type="3e8ca6be-fda8-4aaf-b5c0-3c54c8bb7312"><MetaData><Inspect>"I'm a Cloud"</Inspect>
</MetaData></NodeChange><EdgeChange InstanceGuid="5a4631f8-b018-469a-b175-391d5dd9a769|7b52c449-1c50-4675-b009-ed74c6dd703a" Status="removed" /><EdgeChange InstanceGuid="34d122f2-91bd-406e-82d8-8654d44098ed|7b52c449-1c50-4675-b009-ed74c6dd703a" Status="removed" /><EdgeChange DstGuid="7b52c449-1c50-4675-b009-ed74c6dd703a" InstanceGuid="3ab31f8-b018-469a-b175-391d5dd9a769|7b52c449-1c50-4675-b009-ed74c6dd703a" SrcGuid="3ab31f8-b018-469a-b175-391d5dd9a769" Status="added" /><PortChange InstanceGuid="037acdd6-1292-4daf-80c7-f80c5db2e283" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Status="removed" /><PortChange InstanceGuid="98442096-8594-4bc9-b9b5-38e84dbdedac" ParentGuid="98442096-8594-4bc9-b9b5-38e84dbdedac" Status="removed" /><PortChange InstanceGuid="monkeymonkeymonkey37acdd6-1292-4daf-80c7-f80c5db2e283" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Status="added"><MetaData><Inspect>"I'm a Monkey"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="7b52c449-1c50-4675-b009-ed74c6dd703a" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Status="changed"><MetaData><Inspect>"I'm a Frog"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="def6138f-7fb2-4d20-89c6-7e3d55d38cbe" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Status="changed"><MetaData><Inspect>"I'm a Frog"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="5a4631f8-b018-469a-b175-391d5dd9a769" ParentGuid="5a4631f8-b018-469a-b175-391d5dd9a769" Status="changed"><MetaData><Inspect>"I'm a Frog"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="26aacdcb-06cf-468e-82c8-996c191f5284" ParentGuid="26aacdcb-06cf-468e-82c8-996c191f5284" Status="changed"><MetaData><Inspect>"I'm a Frog"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="34d122f2-91bd-406e-82d8-8654d44098ed" ParentGuid="34d122f2-91bd-406e-82d8-8654d44098ed" Status="changed"><MetaData><Inspect>"I'm a Frog"</Inspect>
</MetaData></PortChange><PortChange InstanceGuid="MAGICa0f78302-d79e-46b2-af23-b959da359475" ParentGuid="MAGICa0f78302-d79e-46b2-af23-b959da359475" Status="changed"><MetaData><Inspect>"I'm a MAGIC UNICORN"</Inspect>
</MetaData></PortChange></DiffSet>
<?xml version="1.0" ?>
<DiffSet>
<NodeChange Status="removed" InstanceGuid="98442096-8594-4bc9-b9b5-38e84dbdedac"/>
<NodeChange Status="added" InstanceGuid="FUNKYFRESHa0f78302-d79e-46b2-af23-b959da359475" Type="3e8ca6be-fda8-4aaf-b5c0-3c54c8bb7312">
<MetaData>
<Inspect>&quot;I'm a glowing crystal&quot;</Inspect>




</MetaData>
</NodeChange>
<NodeChange Status="changed" InstanceGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c" Type="b8963bb1-aa57-476e-a20e-ed6cf635a49c">
<MetaData>
<Inspect>&quot;I'm a Horse&quot;</Inspect>




</MetaData>
</NodeChange>
<EdgeChange Status="removed" InstanceGuid="5a4631f8-b018-469a-b175-391d5dd9a769|7b52c449-1c50-4675-b009-ed74c6dd703a"/>
<EdgeChange Status="removed" InstanceGuid="34d122f2-91bd-406e-82d8-8654d44098ed|7b52c449-1c50-4675-b009-ed74c6dd703a"/>
<EdgeChange Status="added" InstanceGuid="3ab31f8-b018-469a-b175-391d5dd9a769|7b52c449-1c50-4675-b009-ed74c6dd703a" SrcGuid="3ab31f8-b018-469a-b175-391d5dd9a769" DstGuid="7b52c449-1c50-4675-b009-ed74c6dd703a"/>
<PortChange Status="removed" InstanceGuid="037acdd6-1292-4daf-80c7-f80c5db2e283" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c"/>
<PortChange Status="removed" InstanceGuid="98442096-8594-4bc9-b9b5-38e84dbdedac" ParentGuid="98442096-8594-4bc9-b9b5-38e84dbdedac"/>
<PortChange Status="added" InstanceGuid="monkeymonkeymonkey37acdd6-1292-4daf-80c7-f80c5db2e283" ParentGuid="04f5c77c-d8ea-4da4-af5f-10b281e5f57c">
<MetaData>
<Inspect>&quot;I'm a Monkey&quot;</Inspect>




</MetaData>
</PortChange>
<PortChange Status="added" InstanceGuid="FUNKYFRESHa0f78302-d79e-46b2-af23-b959da359475" ParentGuid="FUNKYFRESHa0f78302-d79e-46b2-af23-b959da359475">
<MetaData>
<Inspect>&quot;I'm a cold bottle of beer&quot;</Inspect>




</MetaData>
</PortChange>
<PortChange Status="changed" InstanceGuid="MAGICa0f78302-d79e-46b2-af23-b959da359475" ParentGuid="MAGICa0f78302-d79e-46b2-af23-b959da359475">
<MetaData>
<Inspect>&quot;I'm a MAGIC UNICORN&quot;</Inspect>




</MetaData>
</PortChange>
</DiffSet>
Loading