133 lines
4.1 KiB
Python
Executable File
133 lines
4.1 KiB
Python
Executable File
#!/usr/bin/python
|
|
# -*- coding: UTF-8 -*-
|
|
import matplotlib
|
|
import matplotlib.pyplot as plt
|
|
from xml import sax
|
|
import networkx as nx
|
|
import dfs_to_routes
|
|
#import pymysql
|
|
|
|
Edges = []
|
|
Value = {}
|
|
Labels = {}
|
|
|
|
|
|
class UmlHandler(sax.ContentHandler):
|
|
# XML Content Handler 重写类
|
|
def __init__(self):
|
|
self.CurrentData = ""
|
|
|
|
# 元素开始事件处理
|
|
def startElement(self, tag, attributes):
|
|
self.CurrentData = tag
|
|
if tag == "mxCell":
|
|
if 'value' in attributes:
|
|
if attributes['value'] != '':
|
|
if "开始" == attributes['value']:
|
|
g.add_node('开始')
|
|
Value[attributes['id']] = '开始'
|
|
if "结束" == attributes['value']:
|
|
g.add_node('结束')
|
|
Value[attributes['id']] = '结束'
|
|
if "vertex" in attributes:
|
|
if "style" in attributes:
|
|
if "swimlane" in attributes['style']:
|
|
pass
|
|
else:
|
|
if attributes['value'] != '':
|
|
if 'edgeLabel' not in attributes['style']:
|
|
g.add_node(attributes['value'])
|
|
Value[attributes['id']] = attributes['value']
|
|
else:
|
|
Labels[attributes['parent']] = attributes['value']
|
|
if "edge" in attributes:
|
|
if "target" not in attributes or "source" not in attributes:
|
|
print(attributes['value'])
|
|
else:
|
|
source = attributes['source']
|
|
target = attributes['target']
|
|
if 'value' in attributes:
|
|
if attributes['value'] != '':
|
|
Labels[attributes['id']] = attributes['value']
|
|
if source != target:
|
|
Edges.append((source, target, {'id': attributes['id']}))
|
|
|
|
|
|
def ProcessXML(fp):
|
|
# 创建一个 XMLReader
|
|
parser = sax.make_parser()
|
|
parser.setFeature(sax.handler.feature_namespaces, 0)
|
|
# 重写 ContextHandler
|
|
Handler = UmlHandler()
|
|
parser.setContentHandler(Handler)
|
|
parser.parse(fp)
|
|
|
|
|
|
def ProcessEdges(g):
|
|
# 处理边
|
|
for i in Edges:
|
|
g.add_edge(Value[i[0]], Value[i[1]], id=i[2]['id'])
|
|
# 处理边的标签
|
|
for i in Labels.keys():
|
|
for edge in g.edges(data=True):
|
|
if edge[2]['id'] == i:
|
|
edge[2]['value'] = Labels[i]
|
|
|
|
|
|
def DrawGraph(g):
|
|
nx.draw_circular(g, with_labels=True)
|
|
|
|
# 圆形画出该有向图
|
|
plt.show()
|
|
|
|
|
|
def ProcessDatabase(routes_lst):
|
|
# 指定数据库
|
|
con = pymysql.connect(host='localhost', user='root', passwd='root', port=3306, db='routes')
|
|
cursor = con.cursor()
|
|
# Insert 数据
|
|
for route in routes_lst:
|
|
sql = "INSERT INTO routes_input (Route) VALUE" + "(\'" + route + "\')"
|
|
cursor.execute(sql)
|
|
con.commit()
|
|
|
|
|
|
def CheckOutDegrees(g):
|
|
for i in g.out_degree:
|
|
if i[1] == 0:
|
|
if i[0] != "结束":
|
|
raise Exception("图存在格式问题,请检查,结点“"+i[0]+"”出度为0。")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# 防止matplot的中文乱码 Mac 平台下
|
|
matplotlib.rcParams['font.sans-serif'] = ['Arial Unicode MS']
|
|
# Windows 平台下
|
|
# matplotlib.rcParams['font.sans-serif'] = ['Microsoft Yahei']
|
|
|
|
# 创建新的有向图
|
|
g = nx.DiGraph()
|
|
g.clear()
|
|
|
|
# !!!!!!!!!!
|
|
# 在此处更换 XML 文件路径
|
|
# !!!!!!!!!!
|
|
filepath = "bpmn.xml"
|
|
# 处理 XML 数据
|
|
ProcessXML(filepath)
|
|
|
|
# 处理边的标签
|
|
ProcessEdges(g)
|
|
|
|
# 检查出度
|
|
CheckOutDegrees(g)
|
|
|
|
# 画图(虽然不能看几乎)
|
|
DrawGraph(g)
|
|
|
|
# DFS 搜索路径
|
|
routes = dfs_to_routes.dfs(g, "开始", g.adj, g.nodes.__sizeof__())
|
|
|
|
# 插入数据库
|
|
# ProcessDatabase(routes)
|