Rework apply_actions to make it more standard

This commit is contained in:
Marek Wolan
2023-07-28 15:14:43 +01:00
parent c1bcc372b1
commit 3b4a01760b
2 changed files with 18 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
"""Core of the PrimAITE Simulator.""" """Core of the PrimAITE Simulator."""
from abc import abstractmethod from abc import abstractmethod
from typing import Dict, List from typing import Callable, Dict, List
from pydantic import BaseModel from pydantic import BaseModel
@@ -19,7 +19,6 @@ class SimComponent(BaseModel):
""" """
return {} return {}
@abstractmethod
def apply_action(self, action: List[str]) -> None: 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. 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. :param action: List describing the action to apply to this object.
:type action: List[str] :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 {}

View File

@@ -1,4 +1,4 @@
from typing import Dict, List, Literal, Tuple from typing import Callable, Dict, List, Literal, Tuple
import pytest import pytest
from pydantic import ValidationError from pydantic import ValidationError
@@ -25,9 +25,6 @@ class TestIsolatedSimComponent:
def describe_state(self) -> Dict: def describe_state(self) -> Dict:
return {} return {}
def apply_action(self, action: List[str]) -> None:
pass
comp = TestComponent(name="computer", size=(5, 10)) comp = TestComponent(name="computer", size=(5, 10))
assert isinstance(comp, TestComponent) assert isinstance(comp, TestComponent)
@@ -44,9 +41,6 @@ class TestIsolatedSimComponent:
def describe_state(self) -> Dict: def describe_state(self) -> Dict:
return {} return {}
def apply_action(self, action: List[str]) -> None:
pass
comp = TestComponent(name="computer", size=(5, 10)) comp = TestComponent(name="computer", size=(5, 10))
dump = comp.model_dump() dump = comp.model_dump()
assert dump == {"name": "computer", "size": (5, 10)} assert dump == {"name": "computer", "size": (5, 10)}
@@ -61,20 +55,18 @@ class TestIsolatedSimComponent:
def describe_state(self) -> Dict: def describe_state(self) -> Dict:
return {} return {}
def apply_action(self, action: List[str]) -> None: def _possible_actions(self) -> Dict[str, Callable[[List[str]], None]]:
possible_actions = { return {
"turn_off": self._turn_off, "turn_off": self._turn_off,
"turn_on": self._turn_on, "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" 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" self.status = "on"
comp = TestComponent(name="computer", status="off") comp = TestComponent(name="computer", status="off")