class TestSubEntity(crest.Entity): state = current = crest.State() port_in = crest.Input(res, 111) port_in2 = crest.Input(res, 222) port_out = crest.Output(res, 11111) port_out2 = crest.Output(res, 22222)
class TestEntity(crest.Entity): in_port = crest.Input(res, 12) in_port2 = crest.Input(res, 33) state = current = crest.State() other_state = crest.State() # the evaluation of the guard of these will be mocked anyway transA = crest.Transition(source=state, target=other_state, guard=(lambda self: True)) transB = crest.Transition(source=state, target=other_state, guard=(lambda self: True))
def _pullup(portname, port): """Worker for pullup. Pulls up one individual port to a name""" if not isinstance(port, (crest.Input, crest.Output)): raise ValueError( f"Error during pullup. '{portname}' cannot be pulled up because it is not an Input or Output port." ) parent = get_parent(port) parents_parent = get_parent(parent) if parents_parent is None: raise ValueError( f"Error during pullup. Port {portname}'s parent entity is not a subentity. Cannot pull up." ) if hasattr(parents_parent, portname): raise ValueError( f"Error during pullup. There exists already an object called '{portname}'." ) connect_name = portname + "_connect" if hasattr(parents_parent, connect_name): raise ValueError( f"Error during pullup. Cannot create connection influence. Name '{connect_name}' already exists." ) if isinstance(port, crest.Input): newport = add(parents_parent, portname, crest.Input(port.resource, port.value)) add(parents_parent, connect_name, crest.Influence(source=newport, target=port)) else: newport = add(parents_parent, portname, crest.Output(port.resource, port.value)) add(parents_parent, connect_name, crest.Influence(source=port, target=newport))
class TestEntity(crest.Entity): port_in = crest.Input(res, 1234) state = current = crest.State() sub = SubEntity() influence = crest.Influence(source=port_in, target=sub.sub_in) def __init__(self): api.add(self, "sub", SubEntity())
class TestEntity(crest.Entity): port_in = crest.Input(res, 1234) port_out = crest.Output(res, 987) state = current = crest.State() influence = crest.Influence(source=port_in, target=port_out) def __init__(self): api.add(self, "port_out", crest.Output(res, 5555))
class TestEntity(crest.Entity): port_in = crest.Input(res, 1234) sub = SubEntity() state = current = crest.State() update = crest.Update(state=state, target=sub.sub_in, function=(lambda self: 12345)) def __init__(self): api.add(self, "sub", SubEntity())
class RootType(crest.Entity): stateA = current = crest.State() stateB = crest.State() inport = crest.Input(crest.Resource("watt", crest.REAL), 0) local = crest.Local(crest.Resource("watt", crest.REAL), 1234) local2 = crest.Local(crest.Resource("watt", crest.REAL), 1234) outport = crest.Output(crest.Resource("watt", crest.REAL), 3.14) trans = crest.Transition(source=stateA, target=stateB, guard=(lambda self: False)) inf = crest.Influence(source=inport, target=local, function=(lambda value: value * 2 + 15)) up = crest.Update(state=stateA, target=outport, function=(lambda self: 1337)) action = crest.Action(transition=trans, target=local2, function=(lambda self: self.local2 + 1))
class GerminationBox(crest.Entity): switch = crest.Input(resource=onOff, value="on") temperature = crest.Local(resource=celsius, value=22) state = current = crest.State() @crest.update(state=state, target=temperature) def update_temp(self, dt): if self.switch.value == "on": # don't exceed 40 (this is the maximum temperature) return min(40, self.temperature.value + 0.5 * dt) else: # don't go below 22 (minimum = current room temperature) return max(22, self.temperature.value - 0.25 * dt)
def test_assign_int_value_to_int_port(self): p = crest.Input(resource=int_res, value=0) p.value = 15
class Test(crest.Entity): A = current = crest.State() port = crest.Input(crest.Resource("watt", crest.REAL), 3.14)
def __init__(self): api.add(self, "port_in", crest.Input(res, 5555))
def test_assign_string_value_to_int_port_should_fail(self): p = crest.Input(resource=int_res, value=0) with self.assertRaises(AssertionError): p.value = "a_string_value"
class SubEntity(crest.Entity): state = current = crest.State() sub_in = crest.Input(res, 111)
class SubClass(self.testclass): port = crest.Input(crest.Resource("dummy", crest.REAL), 12345) def __init__(self): api.pullup(self.port)
def test_assign_value_to_list_port___string(self): p = crest.Input(resource=list_res, value=0) p.value = "five"
def test_assign_value_to_list_port___number(self): p = crest.Input(resource=list_res, value=0) p.value = 1
class TestSubEntity(crest.Entity): in1 = crest.Input(resource=testRes, value=3) in2 = crest.Input(resource=testRes, value=3) out1 = crest.Output(resource=testRes, value=3) out2 = crest.Output(resource=testRes, value=3)
class Test(crest.Entity): in1 = crest.Input(resource=res, value=0) in2 = crest.Input(resource=res, value=0) local = crest.Local(resource=res, value=0) out = crest.Output(resource=res, value=0)
def test_assign_float_value_to_real_port(self): p = crest.Input(resource=real_res, value=0) p.value = 3.1415
class TestSystem(crest.Entity): switch = crest.Input(resource=onOff, value="on") timer = crest.Local(resource=res_time, value=0) germinationbox_one = GerminationBox() germinationbox_two = GerminationBox() off = current = crest.State() on = crest.State() pause = crest.State() boxOne = crest.State() boxTwo = crest.State() # transitions @crest.transition(source=[on, boxOne, boxTwo, pause], target=off) def on_to_off(self): return self.switch.value == "off" @crest.transition(source=off, target=on) def off_to_on(self): return self.switch.value == "on" @crest.transition(source=[boxOne, boxTwo, pause], target=on) def return_to_on(self): return self.timer.value <= 0 @crest.transition(source=on, target=[boxOne, boxTwo]) def on_to_box(self): return self.timer.value <= 0 @crest.transition(source=on, target=pause) def on_to_pause(self): return self.timer.value <= 0 """ updates """ @crest.update(state=[on, boxOne, boxTwo, pause], target=timer) def reduce_timer(self, dt): return max(0, self.timer.value - dt) @crest.update(state=boxOne, target=germinationbox_one.switch) def turn_on_boxOne(self, dt): return "on" @crest.update(state=[on, pause, off, boxTwo], target=germinationbox_one.switch) def turn_off_boxOne(self, dt): return "off" @crest.update(state=boxTwo, target=germinationbox_two.switch) def turn_on_boxTwo(self, dt): return "on" @crest.update(state=[on, pause, off, boxOne], target=germinationbox_two.switch) def turn_off_boxTwo(self, dt): return "off" """ transition actions """ @crest.action(transition=off_to_on, target=timer) def set_timer_zero_when_turn_on(self): return 0 @crest.action(transition=on_to_box, target=timer) def set_timer_thirty(self): return 30 @crest.action(transition=on_to_pause, target=timer) def set_timer_ten(self): return 10
class Subtype(self.basetype): inport = crest.Input(newres, 12)
def test_assign_wrong_value_to_list_port_should_fail(self): p = crest.Input(resource=list_res, value=0) with self.assertRaises(AssertionError): p.value = "abcdefg"
def test_assign_wrong_value_to_list_port_should_fail___number_as_string( self): p = crest.Input(resource=list_res, value=0) with self.assertRaises(AssertionError): p.value = "2"
def test_override_entity_port_with_port(self): obj = self.instance obj.port = crest.Input(crest.Resource("other res", crest.INT), 11)
def test_raise_error_on_None_assignment(self): p = crest.Input(resource=int_res, value=0) with self.assertRaises(AssertionError): p.value = None