
| Current Path : /home/ift/52_procpy/dataninja/95__misc/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : //home/ift/52_procpy/dataninja/95__misc/gml.py |
# -*- coding: utf-8 -*-
import os
import re
import sys
import time
try:
import xmltodict
except:
import pip
pip.main(["install","xmltodict"])
import xmltodict
def sync_model_files (file,date3): # date3: the age of the drawing in the GUI
fileroot = re.sub(r"^(.*)\.(gml|graphml|py)$","\\1",file)
try:
date2 = os.path.getmtime(fileroot+".graphml")
except:
date2 = 0
try:
date1 = os.path.getmtime(fileroot+".gml")
except:
date1 = 0
try:
date0 = os.path.getmtime(fileroot+".py")
except:
date0 = 0
if date3 == max(date3,date2,date1,date0):
return(False) # no necessity for redrawing
if date2 == max(date3,date2,date1,date0):
graphtext = open(fileroot+".graphml").read()
gmltext = graphml_to_gml(graphtext)
pytext = gml_to_py(gmltext)
pytext = pytext + "\n\norig_data = '''"
pytext = pytext + graphtext + "\n'''\n"
open(fileroot+".py","w").write(pytext)
return(True) # necessity for redrawing
if date1 == max(date2,date1,date0):
gmltext = open(fileroot+".gml").read()
pytext = gml_to_py(gmltext)
pytext = pytext + "\n\norig_data = '''"
pytext = pytext + gmltext + "\n'''\n"
open(fileroot+".py","w").write(pytext)
return(True) # necessity for redrawing
if date0 == max(date2,date1,date0):
pytext = open(fileroot+".py").read()
gtext = py_to_g(pytext)
if is_graphml(gtext):
open(fileroot+".graphml","w").write(gtext)
else:
open(fileroot+".gml","w").write(gtext)
return(True) # necessity for redrawing
#*************************************************************************
def xxconvert (file,mode=""):
text = open(file).read()
fileroot = re.sub(r"^(.*)\.(.*)$","\\1",file)
if re.search(r"\.(gml|graphml)$",file) and mode == "p":
pytext = gml_to_py(text)
pytext = pytext + "\n\norig_data = '''\n"
pytext = pytext + text + "\n'''\n"
open(fileroot+".py","w").write(pytext)
if re.search(r"\.(py)$",file) and mode == "g":
gmltext = py_to_g(text)
open(fileroot+".gml","w").write(gmltext)
#*************************************************************************
def graphml_to_gml (text):
d = xmltodict.parse(text)
text = 'Creator "procpy"' + "\n"
text = text + 'Version "1.0"' + "\n"
text = text + "graph\n"
text = text + "[\n"
text = text + " hierarchic 1\n"
text = text + ' label ""' + "\n"
text = text + " directed 1\n"
nodes = {}
for node in d['graphml']['graph']['node']:
text = text + " node\n"
text = text + " [\n"
id = re.sub(r"[a-z]+","",node['@id'],9999)
text = text + " id " + id + "\n"
try:
text = text + " label \"" + node['data'][1]['y:ShapeNode']['y:NodeLabel']['#text'] + "\"\n"
except:
pass
text = text + " graphics\n"
text = text + " [\n"
w0 = node['data'][1]['y:ShapeNode']['y:Geometry']['@width']
h0 = node['data'][1]['y:ShapeNode']['y:Geometry']['@height']
x0 = str( float(node['data'][1]['y:ShapeNode']['y:Geometry']['@x']) + float(w0)/2 )
y0 = str( float(node['data'][1]['y:ShapeNode']['y:Geometry']['@y']) + float(h0)/2 )
nodes[id] = [float(x0),float(y0)]
# nodes[id] = [float(x0)+0.5*float(w0),float(y0)+0.5*float(h0)]
text = text + " x " + x0 + "\n"
text = text + " y " + y0 + "\n"
text = text + " w " + w0 + "\n"
text = text + " h " + h0 + "\n"
text = text + " type \"" + node['data'][1]['y:ShapeNode']['y:Shape']['@type'] + "\"\n"
text = text + " fill \"" + node['data'][1]['y:ShapeNode']['y:Fill']['@color'] + "\"\n"
text = text + " outline \"" + node['data'][1]['y:ShapeNode']['y:BorderStyle']['@color'] + "\"\n"
text = text + " ]\n";
text = text + " ]\n";
for edge in d['graphml']['graph']['edge']:
text = text + " edge\n"
text = text + " [\n"
sour = re.sub(r"[a-z]+","",edge['@source'],9999)
targ = re.sub(r"[a-z]+","",edge['@target'],9999)
text = text + " source " + sour + "\n"
text = text + " target " + targ + "\n"
try:
text = text + " label \"" + edge['data'][1]['y:PolyLineEdge']['y:EdgeLabel']['#text'] + "\"\n"
except:
pass
text = text + " graphics\n"
text = text + " [\n"
text = text + " fill \"" + edge['data'][1]['y:PolyLineEdge']['y:LineStyle']['@color'] + "\"\n"
text = text + " Line\n"
text = text + " [\n"
text = text + " point\n"
text = text + " [\n"
text = text + " x " + str(nodes[sour][0]) + "\n"
text = text + " y " + str(nodes[sour][1]) + "\n"
text = text + " ]\n"
additional_points = None
try:
additional_points = edge['data'][1]['y:PolyLineEdge']['y:Path']['y:Point']
except:
pass
if additional_points:
for point in additional_points:
text = text + " point\n"
text = text + " [\n"
text = text + " x " + point['@x'] + "\n"
text = text + " y " + point['@y'] + "\n"
text = text + " ]\n"
text = text + " point\n"
text = text + " [\n"
text = text + " x " + str(nodes[targ][0]) + "\n"
text = text + " y " + str(nodes[targ][1]) + "\n"
text = text + " ]\n"
text = text + " ]\n"
text = text + " ]\n";
text = text + " ]\n"
text = text + "]\n"
return text
#*************************************************************************
def is_graphml (text):
if re.search(r"<node +id=",text) and re.search(r"<edge +id=",text):
return True
return False
#*************************************************************************
def gml_to_dict (text):
if is_graphml(text):
text = graphml_to_gml(text)
text = text.replace("\r\n","\n")
text = re.sub("\t"," ",text,99999999)
text = re.sub("^(.*?graph *\n *\[)(.*)$","{ \\2",text,flags=re.DOTALL)
text = re.sub(r"(\n *)\[( *\n)","\\1{\\2",text,99999999,flags=re.DOTALL)
text = re.sub(r"(\n *)\]( *\n)","\\1},\\2",text,99999999,flags=re.DOTALL)
text = re.sub(r"(\n *)\[( *\n)","\\1{\\2",text,99999999,flags=re.DOTALL)
text = re.sub(r"(\n *)\]( *\n)","\\1},\\2",text,99999999,flags=re.DOTALL)
cells = re.split(r"\"|$",text,99999999,re.DOTALL) # make a list of all text patterns separated by "
text = re.sub(r"\"(.*?)\"","---FIELD---",text,99999999,flags=re.DOTALL) # replace all "..." cells with ---FIELD---
text = re.sub(r"(\n +)([A-Za-z0-9]+)( +)(\S+)","\\1\"\\2\":\\3\\4,",text,99999999,flags=re.DOTALL)
zaehler = 0
while 0 == 0:
zaehler = zaehler + 1
m = re.search(r"^(.*?)(\n +)(node|edge|LabelGraphics|point)( *\n)(.*)$",text,flags=re.DOTALL)
if not m:
break
text = m.group(1) + m.group(2) + '"' + m.group(3) + ("%04u" %
zaehler) + '":' + m.group(4) + m.group(5)
text = re.sub(r"(\n +)([A-Za-z0-9]+)( *\n)","\\1\"\\2\":\\3",text,99999999,flags=re.DOTALL)
text = re.sub(r"(\n +)([A-Za-z0-9]+)( *\n)","\\1\"\\2\":\\3",text,99999999,flags=re.DOTALL)
zaehler = 1
while re.search(r"---FIELD---",text):
text = re.sub(r"---FIELD---","cells["+str(zaehler)+"]",text,1)
zaehler = zaehler + 2
text = re.sub(r"\,\s*$","",text)
exec "gdata = " + text # now in gdata is all the graphic data as a dictionary
return(gdata)
#*************************************************************************
def gml_to_py (text):
pytext = '''# -*- coding: utf-8 -*-
import procpy
class PROC (object):
def __init__ (self,par):
self.SUMTIME = 0
self.SUMEFFORT = 0
self.SUMCHECKTIMEEKF = 0
self.SUMCHECKTIMEMANAGER = 0
self.SUMPATH = '.'
self.SLEEP = 0
self.BLOCK = ''
self.JUMP = 'run'
'''
gdata = gml_to_dict(text)
nodes = {}
edges = {}
for k in gdata.keys():
entry = gdata[k]
if re.search(r"^node",k):
nodes[("%06u" % int(entry['id']))] = entry
if re.search(r"^edge",k):
edges[("%06u" % int(entry['source']))+"-"+("%06u" % int(entry['target']))] = entry
nodes_keys = nodes.keys()
nodes_keys.sort()
is_target = {}
for edge in edges: # collect all blocks that occurs as targets
is_target[ int(edges[edge]['target']) ] = 1
print is_target
for k in nodes_keys: # process all the blocks
func = "t" + ("%02u" % int(nodes[k]['id']) )
if not int(nodes[k]['id']) in is_target:
func = "run"
text1 = " def " + func + " (self):\n\n"
try:
text2 = str(nodes[k]['label'])
text2 = text2.strip()
# if str(nodes[k]['id']) == text2:
# text2 = ""
except:
text2 = ""
if not text2 == "":
text2 = " " + re.sub(r"\n","\n ",text2,99999999,flags=re.DOTALL)
text2 = re.sub("\$([A-Z][A-Z0-9]*)","self.\\1",text2,99999999)
# m = re.search(r"(^.*\n|^)( \-+ *\n)(.*)$",text2,flags=re.DOTALL)
# if m:
# text2 = ( m.group(1) + "\n if self.DRYRUN == 1:\n " +
# re.sub(r"\n","\n ",m.group(3),99999999,flags=re.DOTALL) )
text2 = text2 + "\n"
if re.search(" [\:\[]([A-Z][A-Z0-9]*)[\:\]]",text2):
text2 = re.sub(" [\:\[]([A-Z][A-Z0-9]*)[\:\]]"," self.BLOCK = '\\1'\n",text2,99999999)
else:
text2 = " self.BLOCK = '" + str(nodes[k]['id']) + "'\n" + text2
for edge in edges: # now look for all edges which will be started by the node
branches = "" # the interpretation of an alternative branch
forklist = [] # the interpretation of calling (parallel) running childs
if not edges[edge]['source'] == nodes[k]['id']:
continue
forklist.append(edges[edge]['target'])
text3 = ""
try:
text3 = str(edges[edge]['label'])
except:
pass
if not text3 == "":
text3 = text3.strip()
m = re.search(r"^([0-9\.]+) *\%",text3)
if m:
func = "t" + ("%02u" % int(edges[edge]['target']))
if not int(edges[edge]['target']) in is_target:
func = "run"
text3 = "___ABC___ '" + func + "' : " + m.group(1) + " ___DEF___"
else:
text3 = re.sub("\$([A-Z][A-Z0-9]+)","self.\\1",text3,99999999)
text3 = "\n if " + text3 + ": "
text3 = text3 + "self.JUMP = 't" + ("%02u" % edges[edge]['target']) + "'\n"
else:
text3 = " self.JUMP = 't" + ("%02u" % edges[edge]['target']) + "'\n"
text2 = text2 + text3
del text3
text2 = re.sub(r"___DEF______ABC___",",",text2,99999999)
text2 = re.sub(r"___ABC___"," self.JUMP = {",text2)
text2 = re.sub(r"___DEF___","}",text2)
if not re.search(r"self\.JUMP",text2):
text2 = text2 + " self.JUMP = ''\n"
if text2 == "":
text2 = " pass\n"
text1 = text1 + text2
pytext = pytext + text1 + "\n\n"
# print pytext
return(pytext)
#*************************************************************************
def py_to_g (text):
gtext = ""
m = re.search(r"^(.*)(orig_data = \'\'\')(.*)(\n\'\'\'\n)$",text,flags=re.DOTALL)
if m:
text = m.group(1)
gtext = m.group(3)
return(gtext)