11from ngcsimlib .operations import BaseOp , overwrite
22from ngcsimlib .utils import Set_Compartment_Batch , get_current_path
3+ from ngcsimlib .logger import error
34import uuid
45
56
67class Compartment :
78 """
8- Compartments in ngcsimlib are container objects for storing the stateful values of components. Compartments are
9- tracked globaly and are automatically linked to components and methods during compiling to allow for stateful
10- mechanics to be run without the need for the class object. Compartments also provide an entry and exit point for
11- values inside of components allowing for cables to be connected for sending and receiving values.
9+ Compartments in ngcsimlib are container objects for storing the stateful
10+ values of components. Compartments are
11+ tracked globaly and are automatically linked to components and methods
12+ during compiling to allow for stateful
13+ mechanics to be run without the need for the class object. Compartments
14+ also provide an entry and exit point for
15+ values inside of components allowing for cables to be connected for
16+ sending and receiving values.
1217 """
1318
1419 @classmethod
1520 def is_compartment (cls , obj ):
1621 """
17- A method for verifying if a provided object is a compartment. All compartments have `_is_compartment` set to
22+ A method for verifying if a provided object is a compartment. All
23+ compartments have `_is_compartment` set to
1824 true by default and this is
1925
2026 Args:
@@ -25,14 +31,18 @@ def is_compartment(cls, obj):
2531 """
2632 return hasattr (obj , "_is_compartment" )
2733
28- def __init__ (self , initial_value = None , static = False ):
34+ def __init__ (self , initial_value = None , static = False , is_input = False ):
2935 """
30- Builds a compartment to be used inside a component. It is important to note that building compartments
31- outside of components may cause unexpected behavior as components interact with their compartments during
36+ Builds a compartment to be used inside a component. It is important
37+ to note that building compartments
38+ outside of components may cause unexpected behavior as components
39+ interact with their compartments during
3240 construction to finish initializing them.
3341 Args:
34- initial_value: The initial value of the compartment. As a general practice it is a good idea to
35- provide a value that is similar to the values that will normally be stored here, such as an array of
42+ initial_value: The initial value of the compartment. As a general
43+ practice it is a good idea to
44+ provide a value that is similar to the values that will
45+ normally be stored here, such as an array of
3646 zeros of the correct length. (default: None)
3747
3848 static: a flag to lock a compartment to be static (default: False)
@@ -44,11 +54,14 @@ def __init__(self, initial_value=None, static=False):
4454 self ._uid = uuid .uuid4 ()
4555 self .name = None
4656 self .path = None
57+ self .is_input = is_input
58+ self ._is_destination = False
4759
4860 def _setup (self , current_component , key ):
4961 """
50- Finishes initializing the compartment, called by the component that builds the compartment
51- (Handel automatically)
62+ Finishes initializing the compartment, called by the component that
63+ builds the compartment
64+ (Handled automatically)
5265 """
5366 self .__add_connection = current_component .add_connection
5467 self .name = current_component .name + "/" + key
@@ -57,14 +70,15 @@ def _setup(self, current_component, key):
5770
5871 def set (self , value ):
5972 """
60- Sets the value of the compartment if it not static (Raises a runtime error)
73+ Sets the value of the compartment if it not static (Raises a runtime
74+ error)
6175 Args:
6276 value: the new value to be set
6377 """
6478 if not self ._static :
6579 self .value = value
6680 else :
67- raise RuntimeError ("Can not assign value to static compartment" )
81+ error ("Can not assign value to static compartment" )
6882
6983 def clamp (self , value ):
7084 """
@@ -88,8 +102,10 @@ def __str__(self):
88102
89103 def __lshift__ (self , other ) -> None :
90104 """
91- Overrides the left shift operation to be used for wiring compartments into one another
92- if other is not an Operation it will create an overwrite operation with other as the argument,
105+ Overrides the left shift operation to be used for wiring compartments
106+ into one another
107+ if other is not an Operation it will create an overwrite operation
108+ with other as the argument,
93109 otherwise it will use the provided operation
94110
95111 Args:
@@ -102,3 +118,16 @@ def __lshift__(self, other) -> None:
102118 op = overwrite (other )
103119 op .set_destination (self )
104120 self .__add_connection (op )
121+
122+ self ._is_destination = True
123+
124+ def is_wired (self ):
125+ """
126+ Returns: if this compartment not marked as an input, or is marked and
127+ has an input
128+
129+ """
130+ if not self .is_input :
131+ return True
132+
133+ return self ._is_destination
0 commit comments