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): sub = TestSubEntity() sub2 = TestSubEntity() inf = crest.Influence(source=sub.out1, target=sub.in1) influence2 = crest.Influence(source=sub.out2, target=sub2.in1) active = current = crest.State() inactive = crest.State() up_active = crest.Update(state=active, target=sub.in2, function=(lambda self, dt: 0)) up_inactive = crest.Update(state=inactive, target=sub.in2, function=(lambda self, dt: 0))
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) 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_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())
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 _install_relay(name, source, target): # find the entity in which we want to install it if not isinstance(source, crest.Port): raise ValueError(f"Source object '{get_name(source)}' is not a Port.") if not isinstance(target, crest.Port): raise ValueError(f"Target object '{get_name(target)}' is not a Port.") source_parent = get_parent(source) target_parent = get_parent(target) if source_parent is None or target_parent is None: raise ValueError( "Either the source or the target port are not inside an entity") entity = None if source_parent is target_parent: # we connect inside the same entity entity = source_parent elif source_parent is get_parent( target_parent): # target is in a subentity entity = source_parent elif get_parent( source_parent) is target_parent: # source is in a subentity entity = target_parent elif get_parent(source_parent) is get_parent(target_parent) and get_parent( source_parent) is not None: # both are in subentity entity = get_parent(source_parent) else: raise ValueError( "Could not find common parent or parent's parent for source and target. Check again." ) if entity is None: raise ValueError( f"Something went wrong. I cannot create an Influence in an undefined entity." ) if hasattr(entity, name): raise ValueError( f"Error during relay. Cannot create influence. Name '{name}' already exists." ) add(entity, name, crest.Influence(source=source, target=target))
def __init__(self): self.port_in_connect = crest.Influence( source=self.sub1.port_out, target=self.sub2.port_out) api.pullup(self.sub1.port_in) api.pullup(self.sub2.port_in)
class TestEntity(crest.Entity): sub = TestSubEntity() inf = crest.Influence(source=sub.out1, target=sub.in1)
class Subtype(self.basetype): newinfluence = crest.Influence(source="inport", target="local", function=(lambda self: 33))