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 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))
def test_override_subentity_in__init__throws_error_when_sub_input_connected_by_update( self): res = crest.Resource("test", crest.REAL) class SubEntity(crest.Entity): state = current = crest.State() sub_in = crest.Input(res, 111) 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()) with self.assertRaises(AttributeError) as context: TestEntity() self.assertEqual( str(context.exception), "Cannot reassign SubEntity 'sub' since one of its Input ports was used as target of Update 'update'." )
def test_override_port_in__init__throws_error_when_already_used_as_action_target( self): res = crest.Resource("test", crest.REAL) class TestEntity(crest.Entity): port_out = crest.Output(res, 987) state = current = crest.State() second_state = crest.State() transition = crest.Transition(source=state, target=second_state, guard=(lambda self: True)) action = crest.Action(transition=transition, target=port_out, function=(lambda self: 12345)) def __init__(self): api.add(self, "port_out", crest.Output(res, 7777)) with self.assertRaises(AttributeError) as context: TestEntity() self.assertEqual( str(context.exception), f"Cannot reassign Output port 'port_out' after it was used as target of Action 'action'." )
def test_override_port_in_subentity_corrects_update(self): newres = crest.Resource("new res", crest.INT) class Subtype(self.basetype): outport = crest.Output(newres, 12) testinstance = Subtype() self.assertEqual(testinstance.up.target, testinstance.outport)
def test_override_port_in_subentity_corrects_influence(self): newres = crest.Resource("new res", crest.INT) class Subtype(self.basetype): inport = crest.Input(newres, 12) testinstance = Subtype() self.assertEqual(testinstance.inf.source, testinstance.inport)
def test_override_subentity_in__init__throws_error_when_sub_input_connected_by_action( self): res = crest.Resource("test", crest.REAL) class SubEntity(crest.Entity): state = current = crest.State() sub_in = crest.Input(res, 111) class TestEntity(crest.Entity): port_out = crest.Output(res, 987) state = current = crest.State() second_state = crest.State() transition = crest.Transition(source=state, target=second_state, guard=(lambda self: True)) sub = SubEntity() action = crest.Action(transition=transition, target=sub.sub_in, function=(lambda self: 12345)) def __init__(self): api.add(self, "sub", SubEntity()) with self.assertRaises(AttributeError) as context: TestEntity() self.assertEqual( str(context.exception), "Cannot reassign SubEntity 'sub' since one of its Input ports was used as target of Action 'action'." )
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 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
def test_override_state_in__init__updates_current_state_to_new_state(self): res = crest.Resource("test", crest.REAL) newstate = crest.State() class TestEntity(crest.Entity): port_out = crest.Output(res, 987) state = current = crest.State() def __init__(self): api.add(self, "state", newstate) entity = TestEntity() self.assertEqual(newstate, entity.current)
def setUp(self): res = crest.Resource("testres", crest.REAL) 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)) self.testentity = TestEntity() self.testentity.sub1 = TestEntity() # create a subetity that's just self.testentity.sub2 = TestEntity() # create a subetity that's just
def test_override_port_in__init__throws_error_when_already_used_as_influence_target( self): res = crest.Resource("test", crest.REAL) 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)) with self.assertRaises(AttributeError) as context: TestEntity() self.assertEqual( str(context.exception), f"Cannot reassign Output port 'port_out' after it was used as target of Influence 'influence'." )
def test_override_state_in__init__throws_error_when_already_used_as_transition_target( self): res = crest.Resource("test", crest.REAL) class TestEntity(crest.Entity): state = current = crest.State() second_state = crest.State() trans = crest.Transition(source=state, target=second_state, guard=(lambda self: True)) def __init__(self): api.add(self, "second_state", crest.State()) with self.assertRaises(AttributeError) as context: TestEntity() self.assertEqual( str(context.exception), f"Cannot reassign State 'second_state' after it was used as target of Transition 'trans'." )
def setUp(self): """Create an entity with subentities from which we can pullup and relay""" res = crest.Resource("test", crest.REAL) 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 TestEntity(crest.Entity): state = current = crest.State() sub1 = TestSubEntity() sub2 = TestSubEntity() self.testclass = TestEntity
def test_override_state_in__init__throws_error_when_already_used_in_update( self): res = crest.Resource("test", crest.REAL) class TestEntity(crest.Entity): port_out = crest.Output(res, 987) state = current = crest.State() second_state = crest.State() update = crest.Update(state=second_state, target=port_out, function=(lambda self: 12345)) def __init__(self): api.add(self, "second_state", crest.State()) with self.assertRaises(AttributeError) as context: TestEntity() self.assertEqual( str(context.exception), f"Cannot reassign State 'second_state' after it was used in Update 'update'." )
def test_override_subentity_in__init__throws_error_when_sub_input_connected_by_influence_source( self): res = crest.Resource("test", crest.REAL) class SubEntity(crest.Entity): state = current = crest.State() sub_out = crest.Output(res, 111) class TestEntity(crest.Entity): port_out = crest.Output(res, 987) state = current = crest.State() sub = SubEntity() influence = crest.Influence(source=sub.sub_out, target=port_out) def __init__(self): api.add(self, "sub", SubEntity()) with self.assertRaises(AttributeError) as context: TestEntity() self.assertEqual( str(context.exception), "Cannot reassign SubEntity 'sub' since one of its Output ports was used as source of Influence 'influence'." )
import unittest import crestdsl.model as crest real_res = crest.Resource("RealRes", crest.REAL) int_res = crest.Resource("intres", crest.INT) string_res = crest.Resource("stringres", crest.STRING) bool_res = crest.Resource("boolres", crest.BOOL) list_res = crest.Resource("ListRes", [1, 2, "five"]) class Test_Ports(unittest.TestCase): def test_raise_error_on_None_assignment(self): p = crest.Input(resource=int_res, value=0) with self.assertRaises(AssertionError): p.value = None def test_assign_int_value_to_real_port(self): p = crest.Input(resource=real_res, value=0) p.value = 15 def test_assign_float_value_to_real_port(self): p = crest.Input(resource=real_res, value=0) p.value = 3.1415 def test_assign_int_value_to_int_port(self): p = crest.Input(resource=int_res, value=0) p.value = 15 def test_assign_float_value_to_int_port_should_fail(self): p = crest.Input(resource=int_res, value=0) with self.assertRaises(AssertionError):
from crestdsl.verification.pointwise import PointwiseModelChecker import crestdsl.model as crest import operator import math import networkx as nx import unittest import unittest.mock as mock import logging logging.disable(logging.WARNING) # shut up unless it's a warning or info # resources onOff = crest.Resource(unit="onOff", domain=["on", "off"]) celsius = crest.Resource(unit="Celsius", domain=crest.REAL) res_time = crest.Resource(unit="Time", domain=crest.REAL) 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)
class Test(crest.Entity): A = current = crest.State() port = crest.Input(crest.Resource("watt", crest.REAL), 3.14)
def test_override_entity_port_with_port(self): obj = self.instance obj.port = crest.Input(crest.Resource("other res", crest.INT), 11)
import unittest import crestdsl.model as crest from crestdsl.simulation.dependencyOrder import ordered_modifiers testRes = crest.Resource("float-resource", crest.Types.REAL) 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_getDependencyOrder(unittest.TestCase): def test_subentity_no_dependencies(self): class TestEntity(crest.Entity): sub = TestSubEntity() ent = TestEntity() self.assertListEqual([ent.sub], ordered_modifiers(ent)) def test_assert_throws_cyclic_exception(self): class TestEntity(crest.Entity): sub = TestSubEntity() inf = crest.Influence(source=sub.out1, target=sub.in1) ent = TestEntity() self.assertRaises(AssertionError, ordered_modifiers, ent)
class SubClass(self.testclass): port = crest.Input(crest.Resource("dummy", crest.REAL), 12345) def __init__(self): api.pullup(self.port)
import unittest import crestdsl.model as crest import pprint res = crest.Resource("Resource", crest.REAL) class TestEntity_get_XY(unittest.TestCase): def assertCompareDicts(self, dict1, dict2): for k1, v1 in dict1.items(): self.assertIn(k1, dict2) self.assertEqual(v1, dict2.get(k1)) for k2, v2 in dict2.items(): self.assertIn(k2, dict2) self.assertEqual(v2, dict1.get(k2)) self.assertEqual(len(dict1), len(dict2), "dicts don't have equal size") """ ports """ def test_get_ports_from_class_instance(self): 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) instance = Test() ports = crest.get_ports(instance) self.assertCountEqual( ports, [instance.in1, instance.in2, instance.local, instance.out])