Files
PrimAITE/src/primaite/simulator/sim_container.py

59 lines
1.9 KiB
Python
Raw Normal View History

2023-08-17 15:32:12 +01:00
from typing import Dict
from primaite.simulator.core import Action, ActionManager, AllowAllValidator, SimComponent
from primaite.simulator.domain.controller import DomainController
2023-08-24 12:41:46 +01:00
from primaite.simulator.network.container import Network
2023-08-17 15:32:12 +01:00
class Simulation(SimComponent):
2023-08-21 10:04:23 +01:00
"""Top-level simulation object which holds a reference to all other parts of the simulation."""
2023-08-24 12:41:46 +01:00
network: Network
domain: DomainController
def __init__(self, **kwargs):
2023-08-21 10:04:23 +01:00
"""Initialise the Simulation."""
2023-08-17 15:32:12 +01:00
if not kwargs.get("network"):
2023-08-24 12:41:46 +01:00
kwargs["network"] = Network()
2023-08-17 15:32:12 +01:00
if not kwargs.get("domain"):
kwargs["domain"] = DomainController()
super().__init__(**kwargs)
2023-08-17 15:32:12 +01:00
def describe_state(self) -> Dict:
"""
Produce a dictionary describing the current state of this object.
Please see :py:meth:`primaite.simulator.core.SimComponent.describe_state` for a more detailed explanation.
:return: Current state of this object and child objects.
:rtype: Dict
"""
state = super().describe_state()
state.update(
{
"network": self.network.describe_state(),
"domain": self.domain.describe_state(),
}
)
return state
2023-08-28 22:34:20 +01:00
def _init_action_manager(self) -> ActionManager:
am = super()._init_action_manager()
# pass through network actions to the network objects
am.add_action(
"network",
Action(
func=lambda request, context: self.network.apply_action(request, context), validator=AllowAllValidator()
),
)
# pass through domain actions to the domain object
am.add_action(
"domain",
Action(
func=lambda request, context: self.domain.apply_action(request, context), validator=AllowAllValidator()
),
)
return am