Commit 288e1cff authored by Maverick Chardet's avatar Maverick Chardet
Browse files

Added ability to know if exceptions were raised inside transitions

Some style fixes
parent 40e396fd
......@@ -7,11 +7,12 @@
"""
import sys
from typing import Dict, Tuple, List, Set
from threading import Thread, Lock, Event
from typing import Dict, Tuple, List, Set, Optional
from threading import Thread
from queue import Queue
from concerto.dependency import DepType, Dependency
from concerto.component import Component
from concerto.transition import Transition
from concerto.connection import Connection
from concerto.internal_instruction import InternalInstruction
from concerto.reconfiguration import Reconfiguration
......@@ -63,12 +64,14 @@ class Assembly(object):
self.verbosity: int = 0
self.print_time: bool = False
self.dryrun: bool = False
self.gantt: GanttRecord = None
self.name: str = None
self.gantt: Optional[GanttRecord] = None
self.name: Optional[str] = None
self.dump_program: bool = False
self.program_str: str = ""
self.error_reports: List[str] = []
def set_verbosity(self, level: int):
self.verbosity = level
for c in self._components:
......@@ -188,6 +191,7 @@ class Assembly(object):
comp.set_print_time(self.print_time)
comp.set_dryrun(self.dryrun)
comp.set_gantt_record(self.gantt)
comp.set_assembly(self)
self._components[name] = comp
self.component_connections[name] = set()
self.act_components.add(name) # _init
......@@ -344,6 +348,13 @@ class Assembly(object):
def get_components(self) -> List[Component]:
return list(self._components.values())
def thread_safe_report_error(self, component: Component, transition: Transition, error: str):
report = "Component: %s\nTransition: %s\nError:\n%s" % (component.name, transition.name, error)
self.error_reports.append(report)
def get_error_reports(self) -> List[str]:
return self.error_reports
"""
CHECK ASSEMBLY
"""
......
......@@ -123,6 +123,8 @@ class Component(object, metaclass=ABCMeta):
self.gantt: Optional[GanttRecord] = None
self.hidden_from_gantt_chart: bool = False
self._assembly = None
self.places: List[str] = []
self.switches: List[Tuple[str, Callable[[Place, str], List[int]]]] = []
self.transitions: Dict[str, Tuple] = {}
......@@ -159,6 +161,9 @@ class Component(object, metaclass=ABCMeta):
self.add_transitions(self.transitions)
self.add_dependencies(self.dependencies)
def set_assembly(self, assembly):
self._assembly = assembly
def set_verbosity(self, level: int):
self._verbosity = level
......@@ -168,7 +173,7 @@ class Component(object, metaclass=ABCMeta):
def set_dryrun(self, value: bool):
self.dryrun = value
def set_gantt_record(self, gc: GanttRecord):
def set_gantt_record(self, gc: Optional[GanttRecord]):
if not self.hidden_from_gantt_chart:
self.gantt = gc
......@@ -299,7 +304,7 @@ class Component(object, metaclass=ABCMeta):
src = None
if src_name is not None:
src = self.st_places[src_name]
self.st_transitions[name] = Transition(name, src, self.st_places[dst_name], bhv, idset, func, args)
self.st_transitions[name] = Transition(name, src, self.st_places[dst_name], bhv, idset, func, args, self)
self.trans_dependencies[name] = []
self.st_behaviors.add(bhv)
for group in self.st_groups:
......@@ -547,6 +552,9 @@ class Component(object, metaclass=ABCMeta):
# print(sys._getframe().f_back.f_code.co_name)
self.st_dependencies[name].write(val)
def thread_safe_report_error(self, transition: Transition, error: str):
self._assembly.thread_safe_report_error(self, transition, error)
"""
CHECK COMPONENT
"""
......
......@@ -24,7 +24,7 @@ class Transition:
BUILD TRANSITION
"""
def __init__(self, name, src, dst, bhv, idset, func, args):
def __init__(self, name, src, dst, bhv, idset, func, args, component):
self.name = name
self.src_place = None
if src is not None:
......@@ -34,6 +34,7 @@ class Transition:
self.dst_idset = idset
self.code = func
self.args = args
self._component = component
self.src_dock = None
if src is not None:
self.src_dock = src.create_output_dock(self)
......@@ -83,6 +84,12 @@ class Transition:
"""
return self.behavior
def execute_user_function(self, user_function, arguments):
try:
user_function(*arguments)
except Exception as e:
self._component.thread_safe_report_error(self, repr(e))
def start_thread(self, gantt_tuple, dryrun):
"""
This method creates the thread of the transition
......@@ -90,7 +97,7 @@ class Transition:
:return: the Thread of the transition
"""
if not dryrun:
self.thread = threading.Thread(target=self.code, args=self.args)
self.thread = threading.Thread(target=self.execute_user_function, args=(self.code, self.args))
if gantt_tuple is not None:
(gantt_chart, args) = gantt_tuple
gantt_chart.start_transition(*args)
......
......@@ -47,6 +47,7 @@ class Server(MadeusComponent):
def check(self):
sleep_print(self, "Checking...", 5, "Checked!")
raise Exception("Check went wrong!!!")
class Client(MadeusComponent):
......@@ -103,6 +104,7 @@ if __name__ == '__main__':
sca.set_print_time(True)
sca.set_record_gantt(True)
sca.run()
print("Errors:", sca.get_error_reports())
# Gantt chart
gc = sca.get_gantt_record().get_gantt_chart()
......
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