Model
model
The model
module defines the structure and behavior of a Model
class which is an extension of the Element
class. This module also provides utility functions to interact with model instances and to perform various operations such as model creation and instance retrieval.
Model class
The Model
class is an abstract base class that represents a generic model in the system. It is designed to be subclassed to create specific types of models. It includes special methods for model initialization (__init_subclass__
) and instance creation (__create__
). The Model
class keeps track of all instances of itself and derived classes using a class attribute __all_instances__
, which is a dictionary of model instances indexed by their unique identifiers.
Class Variables
__all_instances__
: A dictionary that keeps track of all created model instances.preprocessor
: A variable that should be assigned aPreprocessor
class. This preprocessor is applied to the model class during subclass initialization.validator
: A variable that should be assigned aValidator
class. This validator is applied during subclass initialization for the purpose of validating the model class.interpreter
: An instance of theInterpreter
protocol that is intended to be used with the model.
Special Methods
__init_subclass__
: This method is automatically called when a subclass ofModel
is created. It is responsible for running the associated preprocessor and validator, if they are provided.__create__
: This protected class method creates an instance of the model and registers it in__all_instances__
.
Utility Functions
of
: A function that takes anElementType
and returns either theModel
class or an instance of Model associated with that element, if one exists.all_instances
: A function that returns a dictionary of all model instances tracked byModel.__all_instances__
.dump
: A function used for debugging, which prints out a representation of the model elements hierarchy. It takes anElement
object, an optional level of indentation, and an optional associated name. The output shows the model elements' types or object instances, qualified names, and associations.
Typing Imports
The module includes relevant imports from the typing
module and other components from within the stateforward package that are used for type annotations. These imports also make use of conditional import statements to prevent circular dependencies when types are used for annotations only (typing.TYPE_CHECKING
).
Model
Bases: Element
A base class for creating model elements with built-in pre-processing and validation capabilities. This class is designed to be subclassed for creating different types of model elements that require initial pre-processing and validation steps upon creation. It maintains a registry dictionary of all instances of its subclasses.
Attributes:
Name | Type | Description |
---|---|---|
__all_instances__ |
dict[str, Model]
|
A class-level dictionary mapping unique identifiers to instances of Model subclasses. |
preprocessor |
ClassVar[type[Preprocessor]]
|
A class attribute that should be overridden with a specific Preprocessor subclass if pre-processing is needed. |
validator |
ClassVar[type[Validator]]
|
A class attribute that should be overridden with a specific Validator subclass if validation is needed. |
interpreter |
Interpreter
|
An instance attribute that can be set to an Interpreter object if needed. |
Methods:
Name | Description |
---|---|
__init_subclass__ |
type['Model'], **kwargs): A class method automatically called when a subclass is created. This method applies pre-processing and validation to the subclass using its preprocessor and validator class attributes. |
__create__ |
A class method that is used to create instances of the subclass. It registers the instance in the all_instances dictionary using its unique identifier and then returns the instance. Subclasses should define their own preprocessor and validator if specific actions should be taken during instantiation. |
Source code in stateforward/model/model.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
__create__(**kwargs)
classmethod
Creates a new instance of a model and registers it in the model's instance dictionary. This method is a class method that creates a new instance of the class using the provided keyword arguments. It utilizes the built-in create method to initialize the instance and then stores it in a class-level dictionary identified by the instance's unique identifier. This allows for easy access to all instances of the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
Arbitrary keyword arguments passed to the model's constructor during instance creation. |
{}
|
Returns:
Raises:
Source code in stateforward/model/model.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
__init_subclass__(**kwargs)
Initializes the subclass of a Model
class.
This method is automatically invoked during the creation of a subclass of Model
. It starts by invoking the same method in the superclass (if there is one) to ensure that any higher-level initialization occurs as usual. Following this, it checks if the subclass has defined either a preprocessor
or a validator
. If a preprocessor
is defined, it creates an instance of this preprocessor and calls its preprocess
method, passing in the subclass itself as an argument. Similarly, if a validator
is defined, this method creates an instance of the validator and calls its validate
method with the subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
type[Model]
|
The subclass of |
required |
Keyword |
Args
|
|
required |
**kwargs |
Variable length keyword arguments that are passed to the superclass's |
{}
|
Raises:
Type | Description |
---|---|
TypeError
|
If any mandatory base class initializations are omitted or incorrect arguments are provided. |
Source code in stateforward/model/model.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
of(element)
Retrieves a corresponding model instance or class associated with a given element.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element |
ElementType
|
An instance or subclass of ElementType for which to retrieve the associated model. |
required |
Returns:
Type | Description |
---|---|
Optional[Union[type[Model], Model]]
|
Optional[Union[type[Model], Model]]: The model class or model instance associated with the provided element if it exists, otherwise None. |
Source code in stateforward/model/model.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
all_instances()
Returns a dictionary containing all instances of the Model class.
Returns:
Type | Description |
---|---|
dict[str, Model]
|
dict[str, Model]: A dictionary where keys are strings and values are instances of the Model class. |
Source code in stateforward/model/model.py
156 157 158 159 160 161 162 163 164 165 166 167 |
|
dump(element, level=0, associated_name=None)
Generates a hierarchical representation of an element and its associated elements, printing out the hierarchy to the console. The function traverses the element's owned elements and any associated elements that are not owned, recursively printing out their details, including the element's qualified name, type, base type information, and memory address. The hierarchy is presented in an indented format to illustrate the relationships between elements. If 'associated_name' is provided, the printed information will reflect the associated relationship rather than the element's actual qualified name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element |
The ElementType instance to be dumped. |
required | |
level |
int
|
The current level in the hierarchy, used for indentation. Defaults to 0. |
0
|
associated_name |
Optional[str]
|
An optional name to describe the association relationship of 'element'. Defaults to None. The function does not return any value; it outputs directly to the console. |
None
|
Source code in stateforward/model/model.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
|