Commit b7b100f1 authored by Savva Golubitsky's avatar Savva Golubitsky

Multiple connections resolved, selectors not

parent 1bcadd3c
import re import re
import copy
import comsdk.graph as gr import comsdk.graph as gr
from comsdk.edge import * from comsdk.edge import *
import importlib as imp import importlib as imp
from typing import NamedTuple from typing import NamedTuple
from enum import Enum, auto from enum import Enum, auto
# Ключевые слова, описывающие граф # Ключевые слова, описывающие граф
class Params(): class Params():
__slots__=( __slots__=(
...@@ -27,43 +29,81 @@ class Params(): ...@@ -27,43 +29,81 @@ class Params():
stri += s+": {}, ".format(getattr(self, s)) stri += s+": {}, ".format(getattr(self, s))
return stri return stri
class Func():
__slots__ = (
'module',
'func',
'name'
)
def __init__(self, module, name, func=None):
self.module = module
self.name = name
self.func = lambda data: data if func is None else func
def __str__(self):
return "{}.{}()".format(self.module, self.name)
class GraphFactory(): class GraphFactory():
__slots__ = ( __slots__ = (
'states', 'states',
'graph' 'graph',
'tocpp'
) )
def __init__(self): def __init__(self, tocpp=False):
self.states = {} self.states = {}
self.tocpp = tocpp
def add_state(self, statename, params=None): def add_state(self, statename, params=None):
newst = True if statename not in self.states:
if statename in self.states:
newst = False
if newst:
newstate = gr.State(statename) newstate = gr.State(statename)
self.states[statename] = newstate self.states[statename] = newstate
def _create_morphism(self, morphame): def _create_morphism(self, morphname=None):
morph = entities[morphame] if morphname is None:
return Func("", ""), Func("", "")
pred_f, func_f = Func("",""), Func("", "")
morph = entities[morphname]
for m in morph.__slots__: for m in morph.__slots__:
if getattr(morph,m) != None: if getattr(morph,m) != None:
if m!="predicate" and m!="function": if m!="predicate" and m!="function":
print("ERROR: Morphisms could not have any params exept predicate and function!\n{}".format(morphame)) print("ERROR: Morphisms could not have any params exept predicate and function!\n{}".format(morphname))
exit(0) exit(0)
if m=="predicate": if m=="predicate":
pr = getattr(morph, m) if getattr(morph,m) not in entities:
pred_f = getattr(imp.import_module(pr.module), pr.entry_func) print("\tERROR: Predicate {} is not defined!".format(getattr(morph, m)))
exit(0)
pred = entities[getattr(morph, m)]
if self.tocpp:
pred_f = Func(pred.module, pred.entry_func)
else:
pred_f = Func(pred.module, pred.entry_func, getattr(imp.import_module(pred.module), pred.entry_func))
if m=="function": if m=="function":
fu = getattr(morph, m) if getattr(morph,m) not in entities:
func_f = getattr(imp.import_module(fu.module), fu.entry_func) print("\tERROR: Function: {} is not defined!".format(getattr(morph, m)))
exit(0)
fu = entities[getattr(morph, m)]
if self.tocpp:
func_f = Func(fu.module, fu.entry_func)
else:
func_f = Func(fu.module, fu.entry_func, getattr(imp.import_module(fu.module), fu.entry_func))
return pred_f, func_f return pred_f, func_f
def connect_states(self, st1, st2, morphism=None):
pass
# self.states[st1].connect_to(self.states[st2])
fact = GraphFactory()
def add_connection(self, st1, st2, morphism=None):
pred, entr = self._create_morphism(morphism)
self.states[st1].connect_to(self.states[st2], edge=Edge(pred.func, entr.func))
print("{} --{}-{}--> {}".format(st1, pred, entr, st2))
def build(self):
self.graph = gr.Graph(self.states["__BEGIN__"], self.states["__END__"])
data = None
self.graph.init_graph(data)
#TODO: реализоывть Edge(Func, Func)
fact = GraphFactory(tocpp=True)
entities = {} entities = {}
topology = {}
def check_brackets(rawfile): def check_brackets(rawfile):
br = 0 br = 0
...@@ -87,6 +127,19 @@ def check_brackets(rawfile): ...@@ -87,6 +127,19 @@ def check_brackets(rawfile):
print("Brackets or quotes do not match! Check your file") print("Brackets or quotes do not match! Check your file")
exit(-1) exit(-1)
def split_multiple(param):
res = []
first=True
for s in param.__slots__:
attr = getattr(param,s)
if attr is not None and '|' in attr:
vals = attr.split('|')
for v in vals:
par = copy.copy(param)
setattr(par, s, v)
res.append(par)
return res
#Props is line "[proFp=smth, ...]" #Props is line "[proFp=smth, ...]"
...@@ -95,15 +148,14 @@ def param_from_props(props): ...@@ -95,15 +148,14 @@ def param_from_props(props):
props = props.replace("]", '') props = props.replace("]", '')
if '(' in props: if '(' in props:
#replaces , in (smth,smth) to ; #replaces , in (smth,smth) to ;
props =props[:props.find('(')]+props[props.find('('):].replace(',',';') props =props[:props.find('(')]+props[props.find('(')+1:props.find(')')].replace(',','|')
rs =props.split(r",") #.split(r", ") rs =props.split(r",") #.split(r", ")
print(rs)
for r in rs: for r in rs:
r=r.split(r"=", 1) r=r.split(r"=", 1)
if r[0] in parm.__slots__: if r[0] in parm.__slots__:
setattr(parm, r[0], r[1]) setattr(parm, r[0], r[1])
else: else:
print("ERROR:Unknown parameter: "+ r[0]) print("\tERROR:Unknown parameter: "+ r[0])
exit(-1) exit(-1)
return parm return parm
...@@ -112,7 +164,7 @@ def param_from_entln(raw): ...@@ -112,7 +164,7 @@ def param_from_entln(raw):
return res[0], param_from_props(res[1]) return res[0], param_from_props(res[1])
def param_from_topln(raw): def topology(raw):
spl = re.split(r"\s*(=>|->|\[|\])\s*", raw) spl = re.split(r"\s*(=>|->|\[|\])\s*", raw)
spl = list(filter(lambda x: x!="[" and x!="]" and x!="", spl)) spl = list(filter(lambda x: x!="[" and x!="]" and x!="", spl))
left = spl[0].split(",") left = spl[0].split(",")
...@@ -122,19 +174,38 @@ def param_from_topln(raw): ...@@ -122,19 +174,38 @@ def param_from_topln(raw):
if (len(left)>1) and (len(right)>1): if (len(left)>1) and (len(right)>1):
print("ERROR:Ambigious multiple connection in line:\n\t{}".format(raw)) print("ERROR:Ambigious multiple connection in line:\n\t{}".format(raw))
exit() exit()
# many to one conection
elif len(left)>1: elif len(left)>1:
p = param_from_props(spl[3])
morphs = split_multiple(p)
if len(morphs)!=len(left):
print("\tERROR:Count of edges do not match to count of states in many to one connection!\n\t\t{}".format(raw))
exit()
fact.add_state(right[0]) fact.add_state(right[0])
for st in left: for i, st in enumerate(left):
fact.add_state(st) fact.add_state(st)
fact.connect_states(st, right[0]) fact.add_connection(st, right[0], morphs[i].morphism)
# one to many connection, here could be selector
elif len(right)>1: elif len(right)>1:
p = param_from_props(spl[3])
morphs = split_multiple(p)
if len(morphs)!=len(right):
print("\tERROR:Count of edges do not match to count of states in one to many connection!\n\t\t{}".format(raw))
exit()
fact.add_state(left[0]) fact.add_state(left[0])
for st in right: for i, st in enumerate(right):
fact.add_state(st) fact.add_state(st)
fact.add_connection(left[0], st, morphs[i].morphism)
# one to one connection
else: else:
fact.add_state(left[0]) fact.add_state(left[0])
fact.add_state(right[0]) fact.add_state(right[0])
print(spl) if len(spl)==4:
pr =param_from_props(spl[3])
fact.add_connection(left[0], right[0], pr.morphism)
elif len(spl)==3:
fact.add_connection(left[0], right[0], None)
# print(spl)
file = open("./test.adot", "r") file = open("./test.adot", "r")
...@@ -171,16 +242,14 @@ for i, ln in enumerate(dotlines): ...@@ -171,16 +242,14 @@ for i, ln in enumerate(dotlines):
name, parm = param_from_entln(ln) name, parm = param_from_entln(ln)
entities[name] = parm entities[name] = parm
elif top_re.match(ln): elif top_re.match(ln):
param_from_topln(ln) topology(ln)
for e in entities:
print(e, entities[e])
print("states:\n")
for st in fact.states:
print(st)
# for e in entities:
# print(e, entities[e])
#
# print("states:\n")
# for st in fact.states:
# print(st)
#
exit() exit()
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