Commit 5c53382f authored by Savva Golubitsky's avatar Savva Golubitsky

Func reworked, Selector added

parent d2596858
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
File added
......@@ -34,7 +34,8 @@ class Edge(object):
'_morphism',
'_io_mapping',
'preprocess',
'postprocess'
'postprocess',
'order'
]
def __init__(self, predicate, morphism,
io_mapping=InOutMapping(),
......
......@@ -2,11 +2,41 @@ import collections
import os
from enum import Enum, auto
from functools import partial
import importlib as imp
import comsdk.aux as aux
ImplicitParallelizationInfo = collections.namedtuple('ImplicitParallelizationInfo', ['array_keys_mapping', 'branches_number', 'branch_i'])
class Func():
__slots__ = (
'module',
'func',
'name'
)
def __init__(self, module="", name="", dummy=True,func=None):
self.module = module
self.name = name
if func is not None:
self.func = func
elif dummy:
self.func = lambda data: data
else:
self.func = getattr(imp.import_module(module), name)
def __str__(self):
return "{}.{}()".format(self.module, self.name)
class Selector(Func):
__slots__=(
'ntransf'
)
def __init__(self, ntransf, module="", name="", dummy=True):
self.ntransf = ntransf
super().__init__(module, name, func=(lambda x: [True for i in range(ntransf)]) if dummy else None, dummy=False)
class Transfer:
def __init__(self, edge, output_state, index=None):
self.edge = edge
......@@ -23,39 +53,6 @@ class IdleRunType(Enum):
INIT = auto()
CLEANUP = auto()
# class GraphFactory:
# def __init__(self):
# pass
#
# def create_state():
# pass
#
# def create_edge():
# Here we should somehow pass the argument for "special" edges
# Essentially, we change only io_mapping
# pass
#
# def make_graph():
# pass
'''
class PluralGraphFactory:
def __init__(self, plural_keys_mappings, parallel_graphs_number):
self.plural_keys_mappings = plural_keys_mappings
self.parallel_graphs_number = parallel_graphs_number
self.init_state = None
def create_state(state):
if self.init_state == None:
self.init_state = state
def create_edge():
# Here we should somehow pass the argument for "special" edges
# Essentially, we change only io_mapping
pass
def make_graph():
pass
'''
class PluralState:
def __init__(self, states):
self.states = states
......
import re
import copy
import comsdk.graph as gr
from comsdk.edge import *
import importlib as imp
from typing import NamedTuple
from enum import Enum, auto
from comsdk.graph import Graph, Func, State
from comsdk.edge import Edge
# Ключевые слова, описывающие граф
class Params():
__slots__=(
'module',
......@@ -17,7 +14,8 @@ class Params():
'function',
'morphism',
'parallelism',
'comment'
'comment',
'order'
)
def __init__(self):
for slot in self.__slots__:
......@@ -29,25 +27,14 @@ class Params():
stri += s+": {}, ".format(getattr(self, s))
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)
entities = {}
class GraphFactory():
__slots__ = (
'states',
'graph',
'tocpp'
'tocpp',
'entities'
)
def __init__(self, tocpp=False):
self.states = {}
......@@ -57,17 +44,19 @@ class GraphFactory():
if selectorname is not None:
sp = entities[selectorname]
if self.tocpp:
sel_f = Func(sp.module, sp.entry_func)
sel_f = Func(sp.module, sp.entry_func, dummy=True)
else:
sel_f = Func(sp.module, sp.entry_func, getattr(imp.import_module(sp.module), sp.entry_func))
else:
sel_f = Func()
if statename not in self.states:
newstate = gr.State(statename, selector=sel_f)
newstate = State(statename, selector=sel_f)
self.states[statename] = newstate
def _create_morphism(self, morphname=None):
if morphname is None:
return Func("", ""), Func("", "")
pred_f, func_f = Func("",""), Func("", "")
return Func(), Func()
pred_f, func_f = Func(), Func()
morph = entities[morphname]
for m in morph.__slots__:
if getattr(morph,m) != None:
......@@ -80,34 +69,42 @@ class GraphFactory():
exit(0)
pred = entities[getattr(morph, m)]
if self.tocpp:
pred_f = Func(pred.module, pred.entry_func)
pred_f = Func(pred.module, pred.entry_func, dummy=True)
else:
pred_f = Func(pred.module, pred.entry_func, getattr(imp.import_module(pred.module), pred.entry_func))
pred_f = Func(pred.module, pred.entry_func)
if m=="function":
if getattr(morph,m) not in entities:
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)
func_f = Func(fu.module, fu.entry_func, dummy=True)
else:
func_f = Func(fu.module, fu.entry_func, getattr(imp.import_module(fu.module), fu.entry_func))
func_f = Func(fu.module, fu.entry_func)
return pred_f, func_f
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, entr))
print("{} --{}-{}--> {}".format(st1, pred, entr, st2))
# print("{} --{}-{}--> {}".format(st1, pred, entr, st2))
def build(self):
self.graph = gr.Graph(self.states["__BEGIN__"], self.states["__END__"])
self.graph = Graph(self.states["__BEGIN__"], self.states["__END__"])
self.graph.init_graph()
return self.graph
fact = GraphFactory(tocpp=True)
entities = {}
class Parser():
__slots__ = (
'entities',
'fact'
)
def __init__(self, tocpp=True):
self.entities = {}
self.fact = GraphFactory(tocpp=True)
def check_brackets(rawfile):
def _check_brackets(self, rawfile):
br = 0
qu = 0
for char in rawfile:
......@@ -129,7 +126,7 @@ def check_brackets(rawfile):
print("Brackets or quotes do not match! Check your file")
exit(-1)
def split_multiple(param):
def _split_multiple(self,param):
res = []
first=True
for s in param.__slots__:
......@@ -142,10 +139,8 @@ def split_multiple(param):
res.append(par)
return res
#Props is line "[proFp=smth, ...]"
def param_from_props(props):
#Props is line "[proFp=smth, ...]"
def _param_from_props(self,props):
parm = Params()
props = props.replace("]", '')
if '(' in props:
......@@ -161,97 +156,85 @@ def param_from_props(props):
exit(-1)
return parm
def param_from_entln(raw):
def _param_from_entln(self, raw):
res = re.split(r"\[", raw, 1)
return res[0], param_from_props(res[1])
return res[0], self._param_from_props(res[1])
def topology(raw):
def _topology(self,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])
self._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()
# many to one conection
elif len(left)>1:
p = param_from_props(spl[3])
morphs = split_multiple(p)
p = self._param_from_props(spl[3])
morphs = self._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])
self.fact.add_state(right[0])
for i, st in enumerate(left):
fact.add_state(st)
fact.add_connection(st, right[0], morphs[i].morphism)
self.fact.add_state(st)
self.fact.add_connection(st, right[0], morphs[i].morphism)
# one to many connection, here could be selector
elif len(right)>1:
p = param_from_props(spl[3])
fact.add_state(left[0],selector=p.selector)
morphs = split_multiple(p)
p = self._param_from_props(spl[3])
self.fact.add_state(left[0],selectorname=p.selector)
morphs = self._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()
for i, st in enumerate(right):
fact.add_state(st)
fact.add_connection(left[0], st, morphs[i].morphism)
self.fact.add_state(st)
self.fact.add_connection(left[0], st, morphs[i].morphism)
# one to one connection
else:
fact.add_state(left[0])
fact.add_state(right[0])
self.fact.add_state(left[0])
self.fact.add_state(right[0])
if len(spl)==4:
pr =param_from_props(spl[3])
fact.add_connection(left[0], right[0], pr.morphism)
pr =self._param_from_props(spl[3])
self.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")
dot = file.read()
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))
# 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,=\[\]()]*$")
for i, ln in enumerate(dotlines):
self.fact.add_connection(left[0], right[0], None)
def parse_file(self, filename):
file = open(filename, "r")
dot = file.read()
self._check_brackets(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))
# 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,=\[\]()]*$")
for i, ln in enumerate(dotlines):
if ent_re.match(ln):
name, parm = param_from_entln(ln)
name, parm = self._param_from_entln(ln)
entities[name] = parm
elif top_re.match(ln):
topology(ln)
self._topology(ln)
self.entities = entities
return self.fact.build()
# for e in entities:
# print(e, entities[e])
#
# print("states:\n")
# for st in fact.states:
# print(st)
#
exit()
# pars = Parser()
# graph = pars.parse_file("./test.adot")
import comsdk.parser as prs
from comsdk.graph import Selector, Func
p= 5
res = lambda : [True for i in range(p)]
print(res())
# ff = Func(func=lambda x: [True for i in range(p)])
data = {}
sel1 = Selector(3)
sel2 = Selector(3, "selectors", "sel1")
sel3 = Selector(3, "selector", "sel2")
print(sel1)
print(sel2)
print(sel3)
\ No newline at end of file
__BEGIN__ --.()-.()--> INPUT_READY
INPUT_READY --predicate_funcs.predicate_x()-case_gen_funcs.function_1()--> TEPMLATE_COPIED
TEPMLATE_COPIED --predicate_funcs.predicate_y()-case_gen_funcs.function_2()--> NAMES_SUBSTITUTED
NAMES_SUBSTITUTED --predicate_funcs.predicate_x()-case_gen_funcs.function_3()--> CONTENT_SUBSTITUTED
CONTENT_SUBSTITUTED --.()-case_gen_funcs.create_dump()--> DUMP_CREATED
CONTENT_SUBSTITUTED --predicate_funcs.predicate_y()-case_gen_funcs.save_to_db()--> RESULT_SAVED
CONTENT_SUBSTITUTED --predicate_funcs.predicate_x()-case_gen_funcs.save_to_file()--> RESULT_SAVED
RESULT_SAVED --predicate_funcs.predicate_y()-case_gen_funcs.repeat()--> INPUT_READY
RESULT_SAVED --predicate_funcs.predicate_x()-case_gen_funcs.exit()--> __END__
RESULT_SAVED --.()-case_gen_funcs.exit()--> __END__
DUMP_CREATED --case_gen_funcs.check_dump()-case_gen_funcs.exit()--> __END__
......@@ -38,5 +38,5 @@ digraph CODEOBJECT_GENERATOR
// В зависимости от результата вычисления функции-SELECTOR осуществляется переход по
//первому или второму ребру
RESULT_SAVED, DUMP_CREATED -> __END__ [morphism=(EDGE_8, EDGE_9),order=(20,30)]
RESULT_SAVED -> INPUT_READY,__END__ [selector=SELECTOR, morphism=(EDGE_6, EDGE_7)]
RESULT_SAVED -> INPUT_READY,FAKE,__END__ [selector=SELECTOR, morphism=(EDGE_6,EDGE_1,EDGE_7)]
}
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