Commit 21b3c639 authored by Vlad Golubev's avatar Vlad Golubev

Добавил презентацию графовой модели в виде словаря в GraphFactory

Данный функционал позволяет отправлять на клиент структурное описание графовой модели для последующей визуализации и интерактивной работы. Граф представляется в виде словаря со следующей структурой: graph: { 'init_state_name': { 'subgraph': None/{subgraph}, 'connect_to': [{'next': next_state_name, 'morph_f': morphism, 'pred_f': predicate}, ...] // здесь отобразаются связи вершины с другими вершинами }, 'state_name2': {...}, ... 'term_state_name': {...} } Данный словарь заполняется во время парсинга adot-файла. Не зависит от режима парсинга: c генарацией cpp или без.
parent b86dcefc
This diff is collapsed.
......@@ -6,7 +6,7 @@ from pycomsdk.comsdk.edge import Edge
from pycomsdk.comsdk.graph import Graph, Func, State, Selector
class Params():
class Params:
__slots__ = (
'module',
'entry_func',
......@@ -33,28 +33,42 @@ class Params():
# entities = {}
class GraphFactory():
class GraphFactory:
__slots__ = (
'name',
'states',
'graph',
'issub',
'tocpp',
'entities'
'entities',
'graph_structure'
)
def __init__(self, tocpp=False):
"""
Функция инициализации. Существует два режима конфигурации:
1) Без генерации cpp-кода
2) Генерация cpp-кода
:param tocpp: переключатель режима работы фабрики
"""
self.states = {}
self.entities = {}
self.tocpp = tocpp
self.name = None
self.issub = False
self.graph_structure = {} # dict-representation of graph
def add_state(self, statename):
if statename not in self.states:
self.states[statename] = State(statename)
if statename in self.entities:
self.states[statename].comment = self.entities[statename].comment
self.graph_structure.update({statename: {
'subgraph': None,
'connect_to': [],
'graph_name': self.name
}})
def _create_morphism(self, morphname=None):
comment = ""
......@@ -91,6 +105,11 @@ class GraphFactory():
def add_connection(self, st1, st2, morphism=None, ordr=0):
pred, entr, comm = self._create_morphism(morphism)
self.states[st1].connect_to(self.states[st2], edge=Edge(pred, entr, order=ordr, comment=comm))
self.graph_structure[st1]['connect_to'].append({
'next': st2,
'pred_f': str(pred),
'morph_f': str(entr),
})
print("{} -> {}".format(st1, st2))
def build(self, nsub):
......@@ -123,15 +142,22 @@ class GraphFactory():
else:
self.states[s].selector = Selector(len(self.states[s].transfers))
if s in self.entities and self.entities[s].subgraph is not None:
# print(s + " is subgraph")
print("Replacing state {} with subgraph {}".format(s, self.entities[s].subgraph))
parsr = Parser(subgraph=True, tocpp=self.tocpp)
subgr = parsr.parse_file(self.entities[s].subgraph)
self.states[s].replace_with_graph(subgr)
self.graph_structure[s]['subgraph'] = {
'graph': parsr.fact.graph_structure,
'init_state': subgr.init_state.name,
'term_state': subgr.term_state.name,
'subgraph_name': parsr.fact.name
}
self.graph = Graph(self.graph.init_state, self.graph.term_state)
return self.graph
class Parser():
class Parser:
__slots__ = (
'fact',
'issub'
......@@ -462,7 +488,6 @@ class _Bush():
res = ""
for tr in self.branches[i]:
edge = tr.edge
print(tr.output_state.name)
if edge.comment != "":
res += "\t//{}\n".format(edge.comment)
if edge.pred_f.name != "":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment