88from typing import Any , Dict , Final , List , Optional , Tuple
99
1010from .constant import Constant
11- from .output import Output
11+ from .dataset import DataFrameData , Dataset , DataSource
1212from .entity import Entity
13+ from .generator import Generator
1314from .logging import log , LogLevel
14- from .dataset import DataFrameData , Dataset , DataSource
15+ from .output import Output
1516from .quantity import Quantity
1617from .queue import Queue
1718from .resource import Resource
@@ -36,6 +37,7 @@ class World(ABC):
3637 _entity_registry : Final [dict [type , int ]] = {}
3738 datasets : Final [Dict [str , Dataset ]]
3839 constants : Final [Dict [str , Any ]]
40+ generators : Final [Dict [str , Generator ]]
3941 resources : Final [Dict [str , Resource ]]
4042 queues : Final [Dict [str , Queue ]]
4143 quantities : Final [Dict [str , Quantity ]]
@@ -119,6 +121,7 @@ def __init__(
119121 self ._entity_dict = {}
120122 self .datasets = {}
121123 self .constants = {}
124+ self .generators = {}
122125 self .resources = {}
123126 self .queues = {}
124127 self .quantities = {}
@@ -137,6 +140,10 @@ def __init__(
137140 for constant in definition ["constants" ]:
138141 Constant ._from_yaml (self , constant )
139142
143+ if "generators" in definition :
144+ for generator in definition ["generators" ]:
145+ Generator ._from_yaml (self , generator )
146+
140147 if "resources" in definition :
141148 for resource in definition ["resources" ]:
142149 Resource ._from_yaml (self , resource )
@@ -158,7 +165,7 @@ def reset(self):
158165 """Reset the World so you can start a different simulation."""
159166 self .active = False
160167
161- def add (self , obj : Constant | Entity | Resource | Queue | Quantity ):
168+ def add (self , obj : Constant | Generator | Entity | Resource | Queue | Quantity ):
162169 """Add an entity to this :class:`World`.
163170
164171 Args:
@@ -176,6 +183,8 @@ def add(self, obj: Constant | Entity | Resource | Queue | Quantity):
176183
177184 if isinstance (obj , Constant ):
178185 self .constants [obj .id ] = obj
186+ elif isinstance (obj , Generator ):
187+ self .generators [obj .id ] = obj
179188 elif isinstance (obj , Entity ):
180189 self .entities .append (obj )
181190 self ._entity_dict [obj .id ] = obj
@@ -186,7 +195,9 @@ def add(self, obj: Constant | Entity | Resource | Queue | Quantity):
186195 elif isinstance (obj , Quantity ):
187196 self .quantities [obj .id ] = obj
188197
189- def remove (self , obj : Constant | Entity | Resource | Queue | Quantity ) -> bool :
198+ def remove (
199+ self , obj : Constant | Generator | Entity | Resource | Queue | Quantity
200+ ) -> bool :
190201 """Remove an entity from this :class:`World`.
191202
192203 Args:
@@ -200,6 +211,8 @@ def remove(self, obj: Constant | Entity | Resource | Queue | Quantity) -> bool:
200211
201212 if isinstance (obj , Constant ):
202213 self .constants .pop (obj .id )
214+ elif isinstance (obj , Generator ):
215+ self .generators .pop (obj .id )
203216 elif isinstance (obj , Entity ):
204217 self .entities .remove (obj )
205218 self ._entity_dict .pop (obj .id )
@@ -223,17 +236,28 @@ def _set_variation(self, selector: str, value: Value):
223236 obj_path = selector .split ("." )
224237 current = self
225238 for path_part in obj_path [:- 1 ]:
226- current = getattr (current , path_part )
239+ current = (
240+ current .get (path_part )
241+ if isinstance (current , dict )
242+ else getattr (current , path_part )
243+ )
227244
228- destination = getattr (current , obj_path [- 1 ])
245+ destination = (
246+ current .get (obj_path [- 1 ])
247+ if isinstance (current , dict )
248+ else getattr (current , obj_path [- 1 ])
249+ )
229250
230251 if (
231252 destination is None
232253 or isinstance (destination , int )
233254 or isinstance (destination , float )
234255 or isinstance (destination , str )
235256 ):
236- setattr (current , obj_path [- 1 ], value )
257+ if isinstance (current , dict ):
258+ current [obj_path [- 1 ]] = value
259+ else :
260+ setattr (current , obj_path [- 1 ], value )
237261 elif isinstance (destination , Constant ):
238262 destination .value = value
239263
0 commit comments