Commit 8a3af356 authored by Savva Golubitsky's avatar Savva Golubitsky

subgraphs seems to be working

parent 1a1b931c
...@@ -107,6 +107,27 @@ class Graph: ...@@ -107,6 +107,27 @@ class Graph:
return False return False
return True return True
def generate_cpp(self):
data = {}
self.init_graph(data)
cur_state = self.init_state
implicit_parallelization_info = None
while cur_state is not None:
# print('1) In main loop', implicit_parallelization_info)
# morph = _run_state(cur_state, data, implicit_parallelization_info)
transfer_f, implicit_parallelization_info = _run_state(cur_state, data, implicit_parallelization_info)
# print('2) In main loop', implicit_parallelization_info)
if '__EXCEPTION__' in data:
return False
# cur_state, implicit_parallelization_info = morph(data)
print(cur_state.name, transfer_f)
cur_state = transfer_f(data)
# print(morph)
if '__EXCEPTION__' in data:
return False
return True
def init_graph(self, data={}): def init_graph(self, data={}):
if not self._initialized: if not self._initialized:
self.init_state.idle_run(IdleRunType.INIT, [self.init_state.name]) self.init_state.idle_run(IdleRunType.INIT, [self.init_state.name])
...@@ -152,9 +173,9 @@ class State: ...@@ -152,9 +173,9 @@ class State:
def __sort_by_order(tr): def __sort_by_order(tr):
return tr.edge.order return tr.edge.order
self.transfers.sort(key = __sort_by_order) self.transfers.sort(key = __sort_by_order)
print(self.name) # print(self.name)
for t in self.transfers: # for t in self.transfers:
print("\t", t.edge.order, t.edge.pred_name, t.edge.morph_name) # print("\t", t.edge.order, t.edge.pred_name, t.edge.morph_name)
if self._proxy_state is not None: if self._proxy_state is not None:
return self._proxy_state.idle_run(idle_run_type, branching_states_history) return self._proxy_state.idle_run(idle_run_type, branching_states_history)
if idle_run_type == IdleRunType.INIT: if idle_run_type == IdleRunType.INIT:
...@@ -193,7 +214,9 @@ class State: ...@@ -193,7 +214,9 @@ class State:
def replace_with_graph(self, graph): def replace_with_graph(self, graph):
self._proxy_state = graph.init_state self._proxy_state = graph.init_state
graph.term_state.transfers = self.transfers graph.term_state.transfers = self.transfers
graph.term_state.selector = self.selector
def run(self, data, implicit_parallelization_info=None): def run(self, data, implicit_parallelization_info=None):
print('STATE {}\n\tjust entered, implicit_parallelization_info: {}'.format(self.name, implicit_parallelization_info)) print('STATE {}\n\tjust entered, implicit_parallelization_info: {}'.format(self.name, implicit_parallelization_info))
if self._proxy_state is not None: if self._proxy_state is not None:
...@@ -336,26 +359,6 @@ class SerialParallelizationPolicy: ...@@ -336,26 +359,6 @@ class SerialParallelizationPolicy:
return _morph return _morph
class OnlyOneSelectionPolicy:
def __init__(self):
pass
def select(self, predicate_values):
trues_indices = _get_trues(predicate_values)
if len(trues_indices) != 1:
return None
return trues_indices
class AllSelectionPolicy:
def __init__(self):
pass
def select(self, predicate_values):
trues_indices = _get_trues(predicate_values)
if len(trues_indices) != len(predicate_values):
return None
return trues_indices
class BadGraphStructure(Exception): class BadGraphStructure(Exception):
pass pass
......
...@@ -86,6 +86,14 @@ class GraphFactory(): ...@@ -86,6 +86,14 @@ class GraphFactory():
# print("{} --{}-{}--> {}".format(st1, pred, entr, st2)) # print("{} --{}-{}--> {}".format(st1, pred, entr, st2))
def build(self): def build(self):
print("BUILDING {}\nStates:".format(self.name))
for s in self.states:
print("\t"+ s)
if self.issub:
self.graph = Graph(self.states[self.name+"_"+"__BEGIN__"], self.states[self.name+"_"+"__END__"])
else:
self.graph = Graph(self.states["__BEGIN__"], self.states["__END__"])
self.graph.init_graph()
for s in self.states: for s in self.states:
if s in self.entities and self.entities[s].selector is not None: if s in self.entities and self.entities[s].selector is not None:
if self.tocpp: if self.tocpp:
...@@ -95,15 +103,10 @@ class GraphFactory(): ...@@ -95,15 +103,10 @@ class GraphFactory():
else: else:
self.states[s].selector = Selector(len(self.states[s].transfers)) self.states[s].selector = Selector(len(self.states[s].transfers))
if s in self.entities and self.entities[s].subgraph is not None: if s in self.entities and self.entities[s].subgraph is not None:
subgr = Parser(subgraph=True).parse_file(self.entities[s].subgraph) print("Replacing state {} with subgraph {}".format(s,self.entities[s].subgraph))
# print(self.states[s].transfers) parsr = Parser(subgraph=True)
subgr = parsr.parse_file(self.entities[s].subgraph)
self.states[s].replace_with_graph(subgr) self.states[s].replace_with_graph(subgr)
print(subgr.term_state.transfers)
if self.issub:
self.graph = Graph(self.states[self.name+"_"+"__BEGIN__"], self.states[self.name+"_"+"__END__"])
else:
self.graph = Graph(self.states["__BEGIN__"], self.states["__END__"])
self.graph.init_graph()
return self.graph return self.graph
class Parser(): class Parser():
......
import comsdk.parser as prs from comsdk.graph import *
from comsdk.graph import Selector, Func from comsdk.edge import *
from comsdk.communication import *
from test_funcs.simplest import *
data = {}
sl = Selector(1) data= {"a":1}
print(sl.func(data))
pars = prs.Parser(tocpp=True)
graph = pars.parse_file("./test.adot")
pred = Func(func=dummy_predicate)
morph = Func(func=increment_a_edge)
s_1 = State('s_1')
s_2 = State('s_2')
s_3 = State('s_3')
s_1.connect_to(s_2, edge=Edge(pred, morph))
s_2.connect_to(s_3, edge=Edge(pred, morph))
sub_s_1 = State('sub_s_1')
sub_s_2 = State('sub_s_2')
sub_s_1.connect_to(sub_s_2, edge=Edge(pred, morph))
s_2.replace_with_graph(Graph(sub_s_1, sub_s_2))
gr = Graph(s_1, s_3)
gr.init_graph()
gr.run(data)
\ No newline at end of file
...@@ -2,6 +2,7 @@ def dummy_edge(data): ...@@ -2,6 +2,7 @@ def dummy_edge(data):
pass pass
def increment_a_edge(data): def increment_a_edge(data):
data['b'] = 5
data['a'] += 1 data['a'] += 1
def increment_a_array_edge(data): def increment_a_array_edge(data):
...@@ -21,4 +22,7 @@ def positiveness_predicate(data): ...@@ -21,4 +22,7 @@ def positiveness_predicate(data):
return data['a'] > 0 return data['a'] > 0
def nonpositiveness_predicate(data): def nonpositiveness_predicate(data):
return data['a'] <= 0 return data['a'] <= 0
\ No newline at end of file
def copy_to_c(data):
data['c'] = data['b']
\ No newline at end of file
...@@ -2,5 +2,6 @@ digraph ADD { ...@@ -2,5 +2,6 @@ digraph ADD {
FUNC [module=test_funcs.simplest, entry_func=increment_a_edge] FUNC [module=test_funcs.simplest, entry_func=increment_a_edge]
PRED [module=test_funcs.simplest, entry_func=positiveness_predicate] PRED [module=test_funcs.simplest, entry_func=positiveness_predicate]
MORPH [predicate=PRED, function=FUNC] MORPH [predicate=PRED, function=FUNC]
__BEGIN__ -> __END__ [morphism = MORPH] __BEGIN__ -> ST [morphism = MORPH]
ST -> __END__
} }
\ No newline at end of file
...@@ -2,8 +2,13 @@ digraph SIMPLEST { ...@@ -2,8 +2,13 @@ digraph SIMPLEST {
FUNC [module=test_funcs.simplest, entry_func=increment_a_edge] FUNC [module=test_funcs.simplest, entry_func=increment_a_edge]
PRED [module=test_funcs.simplest, entry_func=positiveness_predicate] PRED [module=test_funcs.simplest, entry_func=positiveness_predicate]
MORPH [predicate=PRED, function=FUNC] MORPH [predicate=PRED, function=FUNC]
ST1 [subgraph=./tests/parser_test/add.adot]
FUNC2 [module=test_funcs.simplest, entry_func=copy_to_c]
MORPH2 [predicate=PRED, function=FUNC2]
ST2 [subgraph=./tests/parser_test/add.adot]
__BEGIN__ -> ST1 __BEGIN__ -> ST1
ST1 -> ST2 [morphism=MORPH] ST1 -> ST2 [morphism=MORPH]
ST2 -> __END__ [morphism=MORPH] ST2 -> ST3 [morphism=MORPH2]
ST3 -> __END__ [morphism=MORPH]
} }
\ No newline at end of file
...@@ -433,15 +433,17 @@ class GraphGoodCheck(unittest.TestCase): ...@@ -433,15 +433,17 @@ class GraphGoodCheck(unittest.TestCase):
p_12 = p_23 := dummy p_12 = p_23 := dummy
f_12 := a + 1 f_12 := a + 1
''' '''
pred = Func(func=dummy_predicate)
morph = Func(func=increment_a_edge)
s_1 = State('s_1') s_1 = State('s_1')
s_2 = State('s_2') s_2 = State('s_2')
s_3 = State('s_3') s_3 = State('s_3')
s_1.connect_to(s_2, edge=Edge(dummy_predicate, increment_a_edge)) s_1.connect_to(s_2, edge=Edge(pred, morph))
s_2.connect_to(s_3, edge=Edge(dummy_predicate, increment_a_edge)) s_2.connect_to(s_3, edge=Edge(pred, morph))
sub_s_1 = State('sub_s_1') sub_s_1 = State('sub_s_1')
sub_s_2 = State('sub_s_2') sub_s_2 = State('sub_s_2')
sub_s_1.connect_to(sub_s_2, edge=Edge(dummy_predicate, increment_a_edge)) sub_s_1.connect_to(sub_s_2, edge=Edge(pred, morph))
s_2.replace_with_graph(Graph(sub_s_1, sub_s_2)) s_2.replace_with_graph(Graph(sub_s_1, sub_s_2))
correct_outputs = [] correct_outputs = []
for ic in initial_conditions: for ic in initial_conditions:
......
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