xmltopdf.py
Go to the documentation of this file.
1 # script to transform a PSM-xml model into a graph (saved as pdf) with objects as nodes and parent-child relationships as vertices using graphviz dot.
2 #the central function here is xmltopdf(). it takes a directory (with trailing "//" or "\") and a name (without file ending).
3 #it opens the directory + <name>.xml model and creates pdfs containing the trees described in the models as graphs for each scene and reference object in the model
4 #using graphviz (for usage example, see "test()").
5 
6 import subprocess
7 from copy import deepcopy
8 
9 graphviz_command = "dot"
10 
11 class TreeNode:
12  name = ""
13  parent = []
14  reference = False
15 
16 def read(directory, name):
17  filename = directory + name + ".xml"
18  with open(filename, 'r') as i:
19  contents = i.read()
20  return contents
21 
22 def xmltodot(directory, name):
23  contents = read(directory, name)
24  scenes = contents.split("<scene ")
25  dots = []
26  bitcomp = ""
27  bitcomp += directory + name + "; "
28 
29  nodelist = []
30  rootlist = []
31  for scene in scenes:
32  if not scene.find("ocm") == -1:
33  scenename = scene.split("name=\"")[1].split("\"")[0]
34  nodes = scene.split("<")
35  del nodelist[:]
36  del rootlist[:]
37  for node in nodes:
38  values = node.split(" ")
39  if values[0] == "object":
40  if len(nodelist) > 0:
41  rootlist.append([deepcopy(nodelist), root.name])
42  del nodelist[:]
43  top = TreeNode()
44  top.name = "NULL"
45  root = TreeNode()
46  root.name = values[1].split("\"")[1]
47  root.parent = [top]
48  currentroot = root
49  nodelist.append(root)
50  if values[0] == "child":
51  child = TreeNode()
52  child.name = values[1].split("\"")[1]
53  child.parent = [currentroot]
54  child.reference = (len(values) > 2)
55  currentroot = child
56  nodelist.append(child)
57  if values[0] == "/child>":
58  currentroot = currentroot.parent[0]
59  rootlist.append([deepcopy(nodelist), root.name]) #last root
60 
61  for root in rootlist:
62  nodelist = root[0]
63  rootname = root[1]
64 
65  dot = "digraph " + name + "_" + rootname + " {\n"
66 
67  #create graph nodes:
68  for o in rootlist:
69  dot += o[1] + "[label=" + o[1] + "]\n"
70  dot += "\n"
71 
72  #create edges:
73  for o in nodelist:
74  if not o.name == "NULL" and not o.parent[0].name == "NULL":
75  dot += o.parent[0].name + " -> " + o.name
76  if o.reference:
77  dot += "[style=dashed]"
78  dot += "\n"
79 
80  dot += "}\n\n"
81 
82  print(dot)
83 
84  dotname = scenename + "_" + rootname
85  outname = directory + name + "_" + dotname + ".dot"
86  with open(outname, 'w') as f:
87  f.write(dot)
88 
89  dots.append(scenename + "_" + rootname)
90 
91  return dots
92 
93 def dottopdf(directory, name, graphs):
94  for graph in graphs:
95  subprocess.call([graphviz_command, "-Tpdf", directory + name + "_" + graph + ".dot", "-o", directory + name + "_" + graph + ".pdf"])
96 
97 def xmltopdf(directory, name):
98  graphs = xmltodot(directory, name)
99  dottopdf(directory, name, graphs)
100 
101 def test():
102  directory = "../data/"
103  name = "advertisement"
104  xmltopdf(directory, name)
def xmltodot(directory, name)
Definition: xmltopdf.py:22
def xmltopdf(directory, name)
Definition: xmltopdf.py:97
def read(directory, name)
Definition: xmltopdf.py:16
def test()
Definition: xmltopdf.py:101
def dottopdf(directory, name, graphs)
Definition: xmltopdf.py:93


asr_psm
Author(s): Braun Kai, Gehrung Joachim, Heizmann Heinrich, Meißner Pascal
autogenerated on Fri Nov 15 2019 04:00:08