Commit b39d0871 authored by Maverick Chardet's avatar Maverick Chardet
Browse files

Added test for switch, fixed switch bugs

parent 775a4e1f
......@@ -130,6 +130,7 @@ class Component (object, metaclass=ABCMeta):
self.st_places : Dict[str, Place] = {}
self.st_transitions : Dict[str,Transition] = {}
self.st_switches : Set[str] = set()
self.st_dependencies : Dict[str, Dependency] = {}
self.st_groups : Dict[str, Group] = {}
self.st_behaviors : Set[str] = set()
......@@ -151,6 +152,7 @@ class Component (object, metaclass=ABCMeta):
self.create()
self.add_places(self.places)
self.add_switches(self.switches)
self.add_groups(self.groups)
self.add_transitions(self.transitions)
self.add_dependencies(self.dependencies)
......@@ -242,6 +244,7 @@ class Component (object, metaclass=ABCMeta):
self.st_places[name] = Place(name, override_f, cp=self) # TODO: Remove cp
self.place_dependencies[name] = []
self.place_groups[name] = []
self.st_switches.add(name)
if initial:
self.set_initial_place(initial)
......@@ -365,13 +368,21 @@ class Component (object, metaclass=ABCMeta):
:param binding: the name of the binding of the dependency (place or transition)
"""
if type == DepType.DATA_USE:
transitions = []
switches = []
for bind in bindings:
if bind not in self.st_transitions:
raise Exception("Trying to bind dependency %s (of type %s) to something else than a transition"%(name,str(type)))
if bind in self.st_transitions:
transitions.append(bind)
elif bind in self.st_switches:
switches.append(bind)
else:
raise Exception("Trying to bind dependency %s (of type %s) to something else than a transition or a switch"%(name,str(type)))
self.st_dependencies[name] = Dependency(self, name, type)
for transition_name in bindings:
for transition_name in transitions:
self.trans_dependencies[transition_name].append(self.st_dependencies[name])
for switch_name in switches:
self.place_dependencies[switch_name].append(self.st_dependencies[name])
elif type == DepType.USE:
......
#!/usr/bin/python3
from concerto.all import *
class SwitchComp(Component):
def create(self):
self.places = [
'initial',
'small',
'big1',
'big2'
]
def choosing_f(component_instance, current_behavior):
if component_instance.read('value') < 5:
return [0]
else:
return [1, 2]
self.switches = [
('choosing', choosing_f)
]
self.transitions = {
'enter_switch': ('initial', 'choosing', 'run', 0, self.enter_switch),
'leave_small': ('choosing', 'small', 'run', 0, self.leave_small),
'leave_big1': ('choosing', 'big1', 'run', 0, self.leave_big1),
'leave_big2': ('choosing', 'big2', 'run', 0, self.leave_big2)
}
self.dependencies = {
'value': (DepType.DATA_USE, ['choosing'])
}
self.initial_place = 'initial'
def enter_switch(self):
self.print_color("entering switch")
def leave_small(self):
self.print_color("small")
def leave_big1(self):
self.print_color("big (1)")
def leave_big2(self):
self.print_color("big (2)")
class ValueProvider(Component):
def create(self):
self.places = [
'initial',
'providing'
]
self.transitions = {
'provide': ('initial', 'providing', 'run', 0, self.provide)
}
self.dependencies = {
'value': (DepType.DATA_PROVIDE, ['providing'])
}
self.initial_place = 'initial'
def provide(self):
import time
time.sleep(5)
self.write('value', 6)
class SwitchTestAssembly(Assembly):
def __init__(self):
self.switch_comp = SwitchComp()
self.value_provider = ValueProvider()
Assembly.__init__(self)
def run(self):
self.print("### RUNNING ####")
self.add_component('switch_comp', self.switch_comp)
self.add_component('value_provider', self.value_provider)
self.connect('switch_comp', 'value',
'value_provider', 'value')
self.push_b('switch_comp', 'run')
self.push_b('value_provider', 'run')
self.wait_all()
self.synchronize()
if __name__ == '__main__':
sta = SwitchTestAssembly()
sta.set_verbosity(1)
sta.run()
sta.terminate()
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