Navigation Menu

Skip to content

Commit

Permalink
grapher now uses node names
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaeyoung committed Sep 27, 2015
1 parent 107b079 commit ae598fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
23 changes: 15 additions & 8 deletions CommonGraphDiffer.py
Expand Up @@ -47,16 +47,19 @@ def __repr__(self):
return '\n'.join([str(c) for c in self.Changes])

class NodeChange(object):
def __init__(self, Status, InstanceGuid, Position=None, Type=None, MetaData=None):
def __init__(self, Status, InstanceGuid, Position=None, Type=None, MetaData=None, Name=None):
self.Status = Status
self.InstanceGuid = InstanceGuid
self.Position = Position
self.Type = Type
self.MetaData = MetaData
self.Name = Name
def toXML(self):
attributes = {}
attributes["Status"] = self.Status
attributes["InstanceGuid"] = self.InstanceGuid
if self.Name is not None:
attributes["Name"] = self.Name
if self.Status != "removed":
attributes["Type"] = self.Type
e = buildXML("NodeChange", attributes, self.MetaData)
Expand Down Expand Up @@ -221,7 +224,7 @@ def diff(self, other):
for thisNode in nodesRemoved:
thisDiffSet.addChange(NodeChange("removed", thisNode.InstanceGuid, thisNode.Position))
for thisNode in nodesAdded:
thisDiffSet.addChange(NodeChange("added", thisNode.InstanceGuid, thisNode.Position, thisNode.Type, thisNode.MetaData))
thisDiffSet.addChange(NodeChange("added", thisNode.InstanceGuid, thisNode.Position, thisNode.Type, thisNode.MetaData, thisNode.Name))
#TODO: somehow adding nodes doesn't add position metadata (this is a GHXtoCG thing)
for thisNode in nodesChanged:
thisDiffSet.addChange(NodeChange("changed", thisNode.InstanceGuid, thisNode.Position, thisNode.Type, thisNode.MetaData))
Expand Down Expand Up @@ -302,11 +305,12 @@ def toXML(self):
return e

class Node(object):
def __init__(self, Type, InstanceGuid, Position, MetaData):
def __init__(self, Type, InstanceGuid, Position, MetaData, Name=None):
self.Type = Type
self.InstanceGuid = InstanceGuid
self.Position = Position
self.MetaData = MetaData
self.Name = Name
self.Ports = []
def __eq__(self, other):
if self.InstanceGuid == other.InstanceGuid:
Expand All @@ -318,7 +322,10 @@ def addFromChange(cls, nodeChange):
return cls(nodeChange.Type, nodeChange.InstanceGuid, nodeChange.Position, nodeChange.MetaData)
@staticmethod
def getLabel(obj):
return obj.InstanceGuid
if obj.Name is not None:
return obj.Name
else:
return obj.InstanceGuid

@staticmethod
def getXY(obj):
Expand All @@ -345,8 +352,8 @@ def toXML(self):
attribs = {}
attribs["Type"] = self.Type
attribs["InstanceGuid"] = self.InstanceGuid
#if self.Name is not None:
# attribs["Name"] = self.Name
if self.Name is not None:
attribs["Name"] = self.Name
e = E("Node", attribs)
p = E("Ports")
p.extend([k.toXML() for k in self.Ports])
Expand Down Expand Up @@ -437,7 +444,7 @@ def CgxToObject(xmlfile):
thisCG = CommonGraph(root.find("MetaData"))
for xmlNode in root.findall(".//Node"):
xmlNodeAsDict = recursive_dict(xmlNode)[1]
thisNode = Node(xmlNode.get('Type'), xmlNode.get('InstanceGuid'), xmlNode.find('Position'), xmlNode.find('MetaData'))
thisNode = Node(xmlNode.get('Type'), xmlNode.get('InstanceGuid'), xmlNode.find('Position'), xmlNode.find('MetaData'), Name=xmlNode.get('Name'))
for xmlPort in xmlNode.findall(".//Port"):
xmlPortAsDict = recursive_dict(xmlPort)[1]
thisNode.addPort(Port(xmlPort.get('InstanceGuid'), thisNode.InstanceGuid, xmlPort.find('MetaData')))
Expand Down Expand Up @@ -472,7 +479,7 @@ def XMLToDS(fileName):
for xmlChange in xmlChanges:
status = xmlChange.get("Status")
if xmlChange.tag == "NodeChange":
change = NodeChange(status,xmlChange.get("InstanceGuid"), xmlChange.find("Position"), xmlChange.get("Type"), xmlChange.find("MetaData"))
change = NodeChange(status,xmlChange.get("InstanceGuid"), xmlChange.find("Position"), xmlChange.get("Type"), xmlChange.find("MetaData"), xmlChange.get("Name"))
elif xmlChange.tag == "PortChange":
change = PortChange(status, xmlChange.get("InstanceGuid"), xmlChange.get("ParentGuid"), xmlChange.find("MetaData"))
elif xmlChange.tag == "EdgeChange":
Expand Down
2 changes: 1 addition & 1 deletion examples/versionMultipleEdges-A-B.ds
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<DiffSet>
<NodeChange InstanceGuid="80453fe7-0b2b-487e-88ec-ebb7fb9dd1dd" Status="added" Type="59e0b89a-e487-49f8-bab8-b5bab16be14c">
<NodeChange InstanceGuid="80453fe7-0b2b-487e-88ec-ebb7fb9dd1dd" Name="Panel" Status="added" Type="59e0b89a-e487-49f8-bab8-b5bab16be14c">
<MetaData>
<Inspect>&lt;chunk name=&quot;Object&quot; index=&quot;5&quot;&gt;&lt;items count=&quot;2&quot;&gt;&lt;item name=&quot;GUID&quot; type_name=&quot;gh_guid&quot; type_code=&quot;9&quot;&gt;59e0b89a-e487-49f8-bab8-b5bab16be14c&lt;/item&gt;&lt;item name=&quot;Name&quot; type_name=&quot;gh_string&quot; type_code=&quot;10&quot;&gt;Panel&lt;/item&gt;&lt;/items&gt;&lt;chunks count=&quot;1&quot;&gt;&lt;chunk name=&quot;Container&quot;&gt;&lt;items count=&quot;9&quot;&gt;&lt;item name=&quot;Description&quot; type_name=&quot;gh_string&quot; type_code=&quot;10&quot;&gt;A panel for custom notes and text values&lt;/item&gt;&lt;item name=&quot;InstanceGuid&quot; type_name=&quot;gh_guid&quot; type_code=&quot;9&quot;&gt;80453fe7-0b2b-487e-88ec-ebb7fb9dd1dd&lt;/item&gt;&lt;item name=&quot;Name&quot; type_name=&quot;gh_string&quot; type_code=&quot;10&quot;&gt;Panel&lt;/item&gt;&lt;item name=&quot;NickName&quot; type_name=&quot;gh_string&quot; type_code=&quot;10&quot;&gt;Panel&lt;/item&gt;&lt;item name=&quot;Optional&quot; type_name=&quot;gh_bool&quot; type_code=&quot;1&quot;&gt;false&lt;/item&gt;&lt;item name=&quot;ScrollRatio&quot; type_name=&quot;gh_double&quot; type_code=&quot;6&quot;&gt;0&lt;/item&gt;&lt;item name=&quot;Source&quot; index=&quot;0&quot; type_name=&quot;gh_guid&quot; type_code=&quot;9&quot;&gt;ddce4aa2-bc22-4e97-9e43-7beb86f62899&lt;/item&gt;&lt;item name=&quot;SourceCount&quot; type_name=&quot;gh_int32&quot; type_code=&quot;3&quot;&gt;1&lt;/item&gt;&lt;item name=&quot;UserText&quot; type_name=&quot;gh_string&quot; type_code=&quot;10&quot;&gt;Double click to edit panel content…&lt;/item&gt;&lt;/items&gt;&lt;chunks count=&quot;2&quot;&gt;&lt;chunk name=&quot;Attributes&quot;&gt;&lt;items count=&quot;5&quot;&gt;&lt;item name=&quot;Bounds&quot; type_name=&quot;gh_drawing_rectanglef&quot; type_code=&quot;35&quot;&gt;&lt;X&gt;960&lt;/X&gt;&lt;Y&gt;303&lt;/Y&gt;&lt;W&gt;160&lt;/W&gt;&lt;H&gt;100&lt;/H&gt;&lt;/item&gt;&lt;item name=&quot;MarginLeft&quot; type_name=&quot;gh_int32&quot; type_code=&quot;3&quot;&gt;0&lt;/item&gt;&lt;item name=&quot;MarginRight&quot; type_name=&quot;gh_int32&quot; type_code=&quot;3&quot;&gt;0&lt;/item&gt;&lt;item name=&quot;MarginTop&quot; type_name=&quot;gh_int32&quot; type_code=&quot;3&quot;&gt;0&lt;/item&gt;&lt;item name=&quot;Selected&quot; type_name=&quot;gh_bool&quot; type_code=&quot;1&quot;&gt;true&lt;/item&gt;&lt;/items&gt;&lt;/chunk&gt;&lt;chunk name=&quot;PanelProperties&quot;&gt;&lt;items count=&quot;6&quot;&gt;&lt;item name=&quot;Colour&quot; type_name=&quot;gh_drawing_color&quot; type_code=&quot;36&quot;&gt;&lt;ARGB&gt;255;255;250;90&lt;/ARGB&gt;&lt;/item&gt;&lt;item name=&quot;DrawIndices&quot; type_name=&quot;gh_bool&quot; type_code=&quot;1&quot;&gt;true&lt;/item&gt;&lt;item name=&quot;DrawPaths&quot; type_name=&quot;gh_bool&quot; type_code=&quot;1&quot;&gt;true&lt;/item&gt;&lt;item name=&quot;Multiline&quot; type_name=&quot;gh_bool&quot; type_code=&quot;1&quot;&gt;true&lt;/item&gt;&lt;item name=&quot;Stream&quot; type_name=&quot;gh_bool&quot; type_code=&quot;1&quot;&gt;false&lt;/item&gt;&lt;item name=&quot;Wrap&quot; type_name=&quot;gh_bool&quot; type_code=&quot;1&quot;&gt;true&lt;/item&gt;&lt;/items&gt;&lt;/chunk&gt;&lt;/chunks&gt;&lt;/chunk&gt;&lt;/chunks&gt;&lt;/chunk&gt;</Inspect>

Expand Down
12 changes: 7 additions & 5 deletions grapher.py
Expand Up @@ -17,10 +17,10 @@ def graphAddCGX(G, CGX, col='black'):
G.edge_attr['color'] = col
G.node_attr['color'] = col
for (nodeid, name, position) in CGX.getNodesForGraphviz():
print (nodeid, name, position)
# print (nodeid, name, position)
G.add_node(nodeid, color=col, label=name, pin="true", pos=position + "!")
for (src, dst) in CGX.getEdgePairs():
print (src, dst)
# print (src, dst)
G.add_edge(src,dst)
return G

Expand Down Expand Up @@ -55,18 +55,20 @@ def main():
ds = cgd.XMLToDS(args.ds)

G = pgv.AGraph('digraph foo {}')
print "is G strict", G.is_strict()
print "is G directed", G.is_directed()

G.node_attr['fontsize'] = 2.0
G.node_attr['fontsize'] = 10.0
G.node_attr['shape'] = 'circle'

print "## generating graph from", args.cg1, "and", args.ds

G = graphAddCGX(G, CGA, 'grey')

G = graphApplyDS(G, CGA, ds, addedColor='#319E8E', removedColor='#D13A82', changedColor='#FFDF4E')

G.layout(prog='neato') # layout with default (neato)
G.draw(args.png)

print "## wrote", args.png

if __name__ == "__main__":
main()

0 comments on commit ae598fd

Please sign in to comment.