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

Func reworked, Selector added

parent d2596858
...@@ -34,7 +34,8 @@ class Edge(object): ...@@ -34,7 +34,8 @@ class Edge(object):
'_morphism', '_morphism',
'_io_mapping', '_io_mapping',
'preprocess', 'preprocess',
'postprocess' 'postprocess',
'order'
] ]
def __init__(self, predicate, morphism, def __init__(self, predicate, morphism,
io_mapping=InOutMapping(), io_mapping=InOutMapping(),
......
...@@ -2,11 +2,41 @@ import collections ...@@ -2,11 +2,41 @@ import collections
import os import os
from enum import Enum, auto from enum import Enum, auto
from functools import partial from functools import partial
import importlib as imp
import comsdk.aux as aux import comsdk.aux as aux
ImplicitParallelizationInfo = collections.namedtuple('ImplicitParallelizationInfo', ['array_keys_mapping', 'branches_number', 'branch_i']) 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: class Transfer:
def __init__(self, edge, output_state, index=None): def __init__(self, edge, output_state, index=None):
self.edge = edge self.edge = edge
...@@ -23,39 +53,6 @@ class IdleRunType(Enum): ...@@ -23,39 +53,6 @@ class IdleRunType(Enum):
INIT = auto() INIT = auto()
CLEANUP = 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: class PluralState:
def __init__(self, states): def __init__(self, states):
self.states = states self.states = states
......
import re import re
import copy import copy
import comsdk.graph as gr
from comsdk.edge import *
import importlib as imp 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(): class Params():
__slots__=( __slots__=(
'module', 'module',
...@@ -17,7 +14,8 @@ class Params(): ...@@ -17,7 +14,8 @@ class Params():
'function', 'function',
'morphism', 'morphism',
'parallelism', 'parallelism',
'comment' 'comment',
'order'
) )
def __init__(self): def __init__(self):
for slot in self.__slots__: for slot in self.__slots__:
...@@ -29,25 +27,14 @@ class Params(): ...@@ -29,25 +27,14 @@ class Params():
stri += s+": {}, ".format(getattr(self, s)) stri += s+": {}, ".format(getattr(self, s))
return stri return stri
class Func(): entities = {}
__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' 'tocpp',
'entities'
) )
def __init__(self, tocpp=False): def __init__(self, tocpp=False):
self.states = {} self.states = {}
...@@ -57,17 +44,19 @@ class GraphFactory(): ...@@ -57,17 +44,19 @@ class GraphFactory():
if selectorname is not None: if selectorname is not None:
sp = entities[selectorname] sp = entities[selectorname]
if self.tocpp: if self.tocpp:
sel_f = Func(sp.module, sp.entry_func) sel_f = Func(sp.module, sp.entry_func, dummy=True)
else: else:
sel_f = Func(sp.module, sp.entry_func, getattr(imp.import_module(sp.module), sp.entry_func)) 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: if statename not in self.states:
newstate = gr.State(statename, selector=sel_f) newstate = State(statename, selector=sel_f)
self.states[statename] = newstate self.states[statename] = newstate
def _create_morphism(self, morphname=None): def _create_morphism(self, morphname=None):
if morphname is None: if morphname is None:
return Func("", ""), Func("", "") return Func(), Func()
pred_f, func_f = Func("",""), Func("", "") pred_f, func_f = Func(), Func()
morph = entities[morphname] morph = entities[morphname]
for m in morph.__slots__: for m in morph.__slots__:
if getattr(morph,m) != None: if getattr(morph,m) != None:
...@@ -80,34 +69,42 @@ class GraphFactory(): ...@@ -80,34 +69,42 @@ class GraphFactory():
exit(0) exit(0)
pred = entities[getattr(morph, m)] pred = entities[getattr(morph, m)]
if self.tocpp: if self.tocpp:
pred_f = Func(pred.module, pred.entry_func) pred_f = Func(pred.module, pred.entry_func, dummy=True)
else: 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 m=="function":
if getattr(morph,m) not in entities: if getattr(morph,m) not in entities:
print("\tERROR: Function: {} is not defined!".format(getattr(morph, m))) print("\tERROR: Function: {} is not defined!".format(getattr(morph, m)))
exit(0) exit(0)
fu = entities[getattr(morph, m)] fu = entities[getattr(morph, m)]
if self.tocpp: if self.tocpp:
func_f = Func(fu.module, fu.entry_func) func_f = Func(fu.module, fu.entry_func, dummy=True)
else: 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 return pred_f, func_f
def add_connection(self, st1, st2, morphism=None): def add_connection(self, st1, st2, morphism=None):
pred, entr = self._create_morphism(morphism) pred, entr = self._create_morphism(morphism)
self.states[st1].connect_to(self.states[st2], edge=Edge(pred, entr)) 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): 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() self.graph.init_graph()
return self.graph
fact = GraphFactory(tocpp=True) class Parser():
entities = {} __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 br = 0
qu = 0 qu = 0
for char in rawfile: for char in rawfile:
...@@ -129,7 +126,7 @@ def check_brackets(rawfile): ...@@ -129,7 +126,7 @@ 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): def _split_multiple(self,param):
res = [] res = []
first=True first=True
for s in param.__slots__: for s in param.__slots__:
...@@ -142,10 +139,8 @@ def split_multiple(param): ...@@ -142,10 +139,8 @@ def split_multiple(param):
res.append(par) res.append(par)
return res return res
#Props is line "[proFp=smth, ...]"
def _param_from_props(self,props):
#Props is line "[proFp=smth, ...]"
def param_from_props(props):
parm = Params() parm = Params()
props = props.replace("]", '') props = props.replace("]", '')
if '(' in props: if '(' in props:
...@@ -161,97 +156,85 @@ def param_from_props(props): ...@@ -161,97 +156,85 @@ def param_from_props(props):
exit(-1) exit(-1)
return parm return parm
def param_from_entln(raw): def _param_from_entln(self, raw):
res = re.split(r"\[", raw, 1) 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 = 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(",")
right = spl[2].split(",") right = spl[2].split(",")
if len(spl)>3: if len(spl)>3:
param_from_props(spl[3]) self._param_from_props(spl[3])
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 # many to one conection
elif len(left)>1: elif len(left)>1:
p = param_from_props(spl[3]) p = self._param_from_props(spl[3])
morphs = split_multiple(p) morphs = self._split_multiple(p)
if len(morphs)!=len(left): 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)) print("\tERROR:Count of edges do not match to count of states in many to one connection!\n\t\t{}".format(raw))
exit() exit()
fact.add_state(right[0]) self.fact.add_state(right[0])
for i, st in enumerate(left): for i, st in enumerate(left):
fact.add_state(st) self.fact.add_state(st)
fact.add_connection(st, right[0], morphs[i].morphism) self.fact.add_connection(st, right[0], morphs[i].morphism)
# one to many connection, here could be selector # one to many connection, here could be selector
elif len(right)>1: elif len(right)>1:
p = param_from_props(spl[3]) p = self._param_from_props(spl[3])
fact.add_state(left[0],selector=p.selector) self.fact.add_state(left[0],selectorname=p.selector)
morphs = split_multiple(p) morphs = self._split_multiple(p)
if len(morphs)!=len(right): 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)) print("\tERROR:Count of edges do not match to count of states in one to many connection!\n\t\t{}".format(raw))
exit() exit()
for i, st in enumerate(right): for i, st in enumerate(right):
fact.add_state(st) self.fact.add_state(st)
fact.add_connection(left[0], st, morphs[i].morphism) self.fact.add_connection(left[0], st, morphs[i].morphism)
# one to one connection # one to one connection
else: else:
fact.add_state(left[0]) self.fact.add_state(left[0])
fact.add_state(right[0]) self.fact.add_state(right[0])
if len(spl)==4: if len(spl)==4:
pr =param_from_props(spl[3]) pr =self._param_from_props(spl[3])
fact.add_connection(left[0], right[0], pr.morphism) self.fact.add_connection(left[0], right[0], pr.morphism)
elif len(spl)==3: elif len(spl)==3:
fact.add_connection(left[0], right[0], None) self.fact.add_connection(left[0], right[0], None)
# print(spl)
def parse_file(self, filename):
file = open(filename, "r")
file = open("./test.adot", "r") dot = file.read()
dot = file.read() self._check_brackets(dot)
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"[ \t\r]", "", dot) #deleting all spaces dot = re.sub(r"\/\/.*", "", dot)
dot = re.sub(r"((digraph\w+\n?)|}|{)", "", dot) dot = re.sub(r"^\n$", "", dot)
dot = re.sub(r"\/\/.*", "", dot) #print("Checking graph...")
dot = re.sub(r"^\n$", "", dot) #graphcheck = re.search(r"\A(digraph)\w+\n?{((\/\/)*.*\n*)+}\Z", dot)
#print("Checking graph...") #if graphcheck is None:
#graphcheck = re.search(r"\A(digraph)\w+\n?{((\/\/)*.*\n*)+}\Z", dot) # print("Incorrect graph, check syntax!")
#if graphcheck is None: # exit()
# print("Incorrect graph, check syntax!") #
# exit() #print("Graph id good, processing!")
# #dot = re.sub(r"//*$", "", dot)
#print("Graph id good, processing!") dotlines = dot.splitlines()
dotlines = list(filter(None, dotlines))
# ent_re - regular expr for edges, states, functions properties
#dot = re.sub(r"//*$", "", dot) ent_re = re.compile(r"^\w+\[.*\]$")
dotlines = dot.splitlines() # top_re - regular expr for topology properties, most time consuming one
dotlines = list(filter(None, dotlines)) top_re = re.compile(r"^(\w+,?)+(->|=>)(\w+,?)+(\[(\w+=(\(?\w+,?\)?)+,?)+\])?")
# (r"^\w[\w\s,]*(->|=>)\s*\w[\w\s,=\[\]()]*$")
for i, ln in enumerate(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): if ent_re.match(ln):
name, parm = param_from_entln(ln) name, parm = self._param_from_entln(ln)
entities[name] = parm entities[name] = parm
elif top_re.match(ln): elif top_re.match(ln):
topology(ln) self._topology(ln)
self.entities = entities
return self.fact.build()
# for e in entities: # pars = Parser()
# print(e, entities[e]) # graph = pars.parse_file("./test.adot")
#
# print("states:\n")
# for st in fact.states:
# print(st)
#
exit()
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 ...@@ -38,5 +38,5 @@ digraph CODEOBJECT_GENERATOR
// В зависимости от результата вычисления функции-SELECTOR осуществляется переход по // В зависимости от результата вычисления функции-SELECTOR осуществляется переход по
//первому или второму ребру //первому или второму ребру
RESULT_SAVED, DUMP_CREATED -> __END__ [morphism=(EDGE_8, EDGE_9),order=(20,30)] 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