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."""
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 {}

View File

@@ -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")