class TestEntity(crest.Entity): res = crest.Resource("float-resource", crest.Types.REAL) listres = crest.Resource("list-res", ["first", "TWO", "3.14abc"]) port = crest.Local(resource=res, value=314.13) port2 = crest.Local(resource=listres, value="TWO") state = current = crest.State() state2 = crest.State()
class TestEntity(crest.Entity): state = current = crest.State() local = crest.Local(res, 9999) local2 = crest.Local(res, 8888) sub1 = TestSubEntity() sub2 = TestSubEntity()
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 TestEntity(crest.Entity): res = crest.Resource("float-resource", crest.Types.REAL) port = crest.Local(resource=res, value=3) secondPort = crest.Local(resource=res, value=3) count = crest.Local(resource=res, value=2) init = current = crest.State() other = crest.State() otherother = crest.State() count_state = crest.State() @crest.transition(source=init, target=other) def t1a(self): return self.port.value >= 10 @crest.transition(source=init, target=otherother) def t1b(self): return self.port.value >= 10 @crest.transition(source=other, target=count_state) def t2a(self): return self.port.value >= 30 @crest.transition(source=otherother, target=count_state) def t2b(self): return self.port.value >= 50 @crest.transition(source=count_state, target=init) def t_count(self): return True @crest.update(state=[init, other], target=port) def increase_port(self, dt): return self.port.value + dt @crest.update(state=[init, other, otherother], target=secondPort) def increase_secondport(self, dt): return self.secondPort.value + dt @crest.update(state=otherother, target=port) def increase_port_otherother(self, dt): return self.port.value + 2 * dt @crest.update(state=count_state, target=count) def update_count(self, dt): return self.count.value + 1 @crest.update(state=count_state, target=port) def reset_port(self, dt): return 0
class TestSubEntity(crest.Entity): state = current = crest.State() port_in = crest.Input(res, 111) port_in2 = crest.Input(res, 222) local = crest.Local(res, 9999) port_out = crest.Output(res, 11111) port_out2 = crest.Output(res, 22222)
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_target_parent_is_None_throws_error(self): class SubClass(self.testclass): def __init__(self): pass local = crest.Local(crest.Resource("dummy", crest.REAL), 12345) testentity = SubClass() with self.assertRaises(ValueError) as context: api.relay(my_relay=(testentity.sub1.port_in, local)) self.assertEqual( str(context.exception), "Either the source or the target port are not inside an entity")
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 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)