From 3b4a01760bc95a339a4d4653023041dc8f2d08f7 Mon Sep 17 00:00:00 2001 From: Marek Wolan Date: Fri, 28 Jul 2023 15:14:43 +0100 Subject: [PATCH] Rework apply_actions to make it more standard --- src/primaite/simulator/core.py | 14 +++++++++--- .../primaite/simulator/test_core.py | 22 ++++++------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/primaite/simulator/core.py b/src/primaite/simulator/core.py index d540daf3..67149414 100644 --- a/src/primaite/simulator/core.py +++ b/src/primaite/simulator/core.py @@ -1,6 +1,6 @@ """Core of the PrimAITE Simulator.""" from abc import abstractmethod -from typing import Dict, List +from typing import Callable, Dict, List from pydantic import BaseModel @@ -19,7 +19,6 @@ class SimComponent(BaseModel): """ return {} - @abstractmethod def apply_action(self, action: List[str]) -> None: """ Apply an action to a simulation component. Action data is passed in as a 'namespaced' list of strings. @@ -35,4 +34,13 @@ class SimComponent(BaseModel): :param action: List describing the action to apply to this object. :type action: List[str] """ - return + possible_actions = self._possible_actions() + if action[0] in possible_actions: + # take the first element off the action list and pass the remaining arguments to the corresponding action + # funciton + possible_actions[action.pop(0)](action) + else: + raise ValueError(f"{self} received invalid action {action}") + + def _possible_actions(self) -> Dict[str, Callable[[List[str]], None]]: + return {} diff --git a/tests/unit_tests/primaite/simulator/test_core.py b/tests/unit_tests/primaite/simulator/test_core.py index ea593a0b..de0732f9 100644 --- a/tests/unit_tests/primaite/simulator/test_core.py +++ b/tests/unit_tests/primaite/simulator/test_core.py @@ -1,4 +1,4 @@ -from typing import Dict, List, Literal, Tuple +from typing import Callable, Dict, List, Literal, Tuple import pytest from pydantic import ValidationError @@ -25,9 +25,6 @@ class TestIsolatedSimComponent: def describe_state(self) -> Dict: return {} - def apply_action(self, action: List[str]) -> None: - pass - comp = TestComponent(name="computer", size=(5, 10)) assert isinstance(comp, TestComponent) @@ -44,9 +41,6 @@ class TestIsolatedSimComponent: def describe_state(self) -> Dict: return {} - def apply_action(self, action: List[str]) -> None: - pass - comp = TestComponent(name="computer", size=(5, 10)) dump = comp.model_dump() assert dump == {"name": "computer", "size": (5, 10)} @@ -61,20 +55,18 @@ class TestIsolatedSimComponent: def describe_state(self) -> Dict: return {} - def apply_action(self, action: List[str]) -> None: - possible_actions = { + def _possible_actions(self) -> Dict[str, Callable[[List[str]], None]]: + return { "turn_off": self._turn_off, "turn_on": self._turn_on, } - if action[0] in possible_actions: - possible_actions[action[0]](action[1:]) - else: - raise ValueError(f"{self} received invalid action {action}") - def _turn_off(self): + def _turn_off(self, options: List[str]) -> None: + assert len(options) == 0, "This action does not support options." self.status = "off" - def _turn_on(self): + def _turn_on(self, options: List[str]) -> None: + assert len(options) == 0, "This action does not support options." self.status = "on" comp = TestComponent(name="computer", status="off")