Commit ae66f747 authored by Savva Golubitsky's avatar Savva Golubitsky

graph factory in work

parent 20635758
......@@ -3,7 +3,7 @@ import os
from enum import Enum, auto
from functools import partial
import comsdk.aux as aux
import aux as aux
ImplicitParallelizationInfo = collections.namedtuple('ImplicitParallelizationInfo', ['array_keys_mapping', 'branches_number', 'branch_i'])
......
import re
import graph as gr
from typing import NamedTuple
from enum import Enum, auto
# Ключевые слова, описывающие граф
scope = [
"__BEGIN__",
"__END__"
]
class Parms():
class Params():
__slots__=(
'module',
'entry_func',
......@@ -16,7 +12,8 @@ class Parms():
'selector',
'function',
'morphism',
'parallelism'
'parallelism',
'comment'
)
def __init__(self):
for slot in self.__slots__:
......@@ -28,91 +25,145 @@ class Parms():
stri += s+": {}, ".format(getattr(self, s))
return stri
class GraphFactory():
__slots__ = (
'states',
'graph'
)
def __init__(self):
self.states = {}
def add_state(self, statename, params=None):
newst = True
if statename in self.states:
newst = False
if newst:
newstate = gr.State(statename)
self.states[statename] = newstate
def connect_states(self, st1, st2, morphism=None):
pass
# self.states[st1].connect_to(self.states[st2])
fact = GraphFactory()
entities = {}
topology = {}
def check_brackets(rawfile):
br = 0
qu = 0
for char in rawfile:
if char == "[":
br+=1
elif char == "{":
br+=1
elif char == "(":
br+=1
elif char == "]":
br-=1
elif char == "}":
br-=1
elif char == ")":
br-=1
elif char =="\"":
qu+=1
if br!=0 or qu%2!=0:
print("Brackets or quotes do not match! Check your file")
exit(-1)
#Props is line "[prop=smth, ...]"
def parm_from_props(props):
parm = Parms()
rs =re.split(r",\s*", props[:-1]) #.split(r", ")
def param_from_props(props):
parm = Params()
props = props.replace("]", '')
if '(' in props:
#replaces , in (smth,smth) to ;
props =props[:props.find('(')]+props[props.find('('):].replace(',',';')
rs =props.split(r",") #.split(r", ")
print(rs)
for r in rs:
r=r.split(r"=", 1)
for s in parm.__slots__:
if s == r[0]:
setattr(parm, s, r[1])
if r[0] in parm.__slots__:
setattr(parm, r[0], r[1])
else:
print("Unknown parameter: "+ r[0])
exit(-1)
return parm
def parm_from_entln(raw):
def param_from_entln(raw):
res = re.split(r"\[", raw, 1)
return res[0], parm_from_props(res[1])
def parm_from_topln(raw):
print(raw)
spl = re.split(r"\s*(=>|->|\[)\s*", raw)
return res[0], param_from_props(res[1])
def param_from_topln(raw):
spl = re.split(r"\s*(=>|->|\[|\])\s*", raw)
spl = list(filter(lambda x: x!="[" and x!="]" and x!="", spl))
left = spl[0].split(",")
right = spl[2].split(",")
if len(spl)>3:
param_from_props(spl[3])
if (len(left)!=1) and (len(right)!=1):
print("ERROR:Ambigious multiple connection in line:\n\t{}".format(raw))
exit()
elif len(left)==2:
fact.add_state(right[0])
for st in left:
fact.add_state(st)
fact.connect_states(st, right[0])
elif len(right)==2:
fact.add_state(left[0])
for st in right:
fact.add_state(st)
else:
fact.add_state(left[0])
fact.add_state(right[0])
print(spl)
# ent = [{"name":{"module":"val" , "entr_func":"val"...}}]
file = open("./testgr.adot", "r")
file = open("./test.adot", "r")
dot = file.read()
graphcheck = re.fullmatch(r"\A(digraph)\s+\w*\n{(.*\s)*}\Z", dot)
if graphcheck is None:
print("Incorrect graph, check syntax!")
exit()
file.seek(0)
dotfile = file.readlines()
check_brackets(dot)
#commts = re.findall(r"\".*\"", dot)
#
dot = re.sub(r"[ \t\r]", "", dot) #deleting all spaces
dot = re.sub(r"((digraph\w+\n?)|}|{)", "", dot)
dot = re.sub(r"\/\/.*", "", dot)
dot = re.sub(r"^\n$", "", dot)
#print("Checking graph...")
#graphcheck = re.search(r"\A(digraph)\w+\n?{((\/\/)*.*\n*)+}\Z", dot)
#if graphcheck is None:
# print("Incorrect graph, check syntax!")
# exit()
#
#print("Graph id good, processing!")
#dot = re.sub(r"//*$", "", dot)
dotlines = dot.splitlines()
dotlines = list(filter(None, dotlines))
entities = {}
topology = {}
# ent_re - regular expr for edges, states, functions properties
ent_re = re.compile(r"^\w+\[.*\]$")
# top_re - regular expr for topology properties, most time consuming one
top_re = re.compile(r"^(\w+,?)+(->|=>)(\w+,?)+(\[(\w+=(\(?\w+,?\)?)+,?)+\])?")
# (r"^\w[\w\s,]*(->|=>)\s*\w[\w\s,=\[\]()]*$")
# com_re - regular expr for comment
com_re = re.compile(r"^\s*//.*$")
for i, ln in enumerate(dotfile):
ln = re.sub(r"\s", "", ln) #deleting all spaces
if com_re.match(ln) or len(ln)==0:
continue
elif ent_re.match(ln):
name, parm = parm_from_entln(ln)
for i, ln in enumerate(dotlines):
if ent_re.match(ln):
name, parm = param_from_entln(ln)
entities[name] = parm
elif top_re.match(ln):
parm_from_topln(ln)
#for e in entities:
# print(e, entities[e])
exit()
param_from_topln(ln)
# Все что до __BEGIN__ считается объявлением
beg = re.search((scope[0]), dotfile).start()
print("states:\n")
for st in fact.states:
print(st)
for e in entities:
print(e, entities[e])
entities = {}
exit()
#Парсинг свойств переходов и состояний
rawents = re.findall(r"\w+\s*?\[.*\]", dotfile[:beg])
for raw in rawents:
res = re.split(r"\s+", raw, 1)
ent = Parms()
rs =re.split(r",\s*", res[1][1:-1]) #.split(r", ")
for r in rs:
r=r.split(r"=", 1)
for s in ent.__slots__:
if s == r[0]:
setattr(ent, s, r[1])
entities[res[0]] = ent
for s in entities:
print(s, entities[s])
#Парсинг топологии графа
rawstmts = re.findall(r"\s*([\w,\s]*(->|=>)[\w,\s]*(\[.*\])?)\n", dotfile[beg:])
print(rawstmts)
# for raw in rawstmts:
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