Skip to content

Commit

Permalink
CHORE: split tree_config.value into its own submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
joshc-slac committed Nov 27, 2024
1 parent 2ac8df7 commit 478c6dd
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 40 deletions.
3 changes: 2 additions & 1 deletion beams/tests/artifacts/egg_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from apischema import serialize

from beams.tree_config.action import IncPVActionItem, SetPVActionItem
from beams.tree_config.base import BehaviorTreeItem, EPICSValue, FixedValue
from beams.tree_config.base import BehaviorTreeItem
from beams.tree_config.value import EPICSValue, FixedValue
from beams.tree_config.composite import SequenceItem
from beams.tree_config.condition import (BinaryConditionItem,
BoundedConditionItem,
Expand Down
2 changes: 1 addition & 1 deletion beams/tests/test_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from beams.bin.main import main
from beams.tests.conftest import cli_args, restore_logging
from beams.tree_config import save_tree_item_to_path
from beams.tree_config.base import EPICSValue
from beams.tree_config.value import EPICSValue
from beams.tree_config.composite import SequenceItem
from beams.tree_config.condition import BinaryConditionItem

Expand Down
3 changes: 2 additions & 1 deletion beams/tests/test_serialize.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from apischema import deserialize, serialize

from beams.tree_config.action import IncPVActionItem, SetPVActionItem
from beams.tree_config.base import BehaviorTreeItem, EPICSValue, FixedValue
from beams.tree_config.base import BehaviorTreeItem
from beams.tree_config.value import EPICSValue, FixedValue
from beams.tree_config.composite import SequenceItem
from beams.tree_config.condition import BinaryConditionItem, ConditionOperator
from beams.tree_config.idiom import CheckAndDoItem
Expand Down
34 changes: 0 additions & 34 deletions beams/tree_config/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import logging
from dataclasses import dataclass
from typing import Any

import py_trees
from epics import caget
from py_trees.behaviour import Behaviour

from beams.serialization import as_tagged_union
Expand Down Expand Up @@ -38,35 +36,3 @@ def get_tree(self) -> Behaviour:
# grab file
# de-serialize tree, return it
raise NotImplementedError


@as_tagged_union
@dataclass
class BaseValue:
def get_value(self) -> Any:
raise NotImplementedError


@dataclass
class FixedValue(BaseValue):
value: Any

def get_value(self) -> Any:
return self.value


@dataclass
class EPICSValue(BaseValue):
pv_name: str
as_string: bool = False

def get_value(self) -> Any:
value = caget(self.pv_name, as_string=self.as_string)
logger.debug(f" <<-- (EPICSValue): caget({self.pv_name}) -> {value}")
return value


@dataclass
class OphydTarget(BaseValue):
device_name: str
component_path: list[str]
3 changes: 2 additions & 1 deletion beams/tree_config/condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from beams.behavior_tree.condition_node import ConditionNode
from beams.serialization import as_tagged_union
from beams.tree_config.base import BaseItem, BaseValue, FixedValue
from beams.tree_config.base import BaseItem
from beams.tree_config.value import BaseValue, FixedValue
from beams.typing_helper import Evaluatable


Expand Down
1 change: 0 additions & 1 deletion beams/tree_config/idiom.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ class UseCheckConditionItem(BaseConditionItem):
If used in any other context the tree will not be constructable.
"""
...
59 changes: 59 additions & 0 deletions beams/tree_config/value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import logging
from typing import Any
from dataclasses import dataclass

import py_trees
from epics import caget
from beams.serialization import as_tagged_union

logger = logging.getLogger(__name__)


@as_tagged_union
@dataclass
class BaseValue:
def get_value(self) -> Any:
raise NotImplementedError


@dataclass
class FixedValue(BaseValue):
value: Any

def get_value(self) -> Any:
return self.value


@dataclass
class EPICSValue(BaseValue):
pv_name: str
as_string: bool = False

def get_value(self) -> Any:
value = caget(self.pv_name, as_string=self.as_string)
logger.debug(f" <<-- (EPICSValue): caget({self.pv_name}) -> {value}")
return value


class BlackBoardValue(BaseValue):
bb_name: str = ""
key_name: str = ""

def get_value(self) -> Any:
logger.debug(f" <<-- (BlackBoardValue): checking blackboard: {self.bb_name} \
for key {self.key_name}")
bb_client = py_trees.blackboard.Client(name=self.bb_name)
try:
value = bb_client.get(self.key_name)
except AttributeError: # Note: seems like it should be KeyError, but in practice getting this one
logger.error(f"<<-- In blackboard: {self.bb_name} \
key {self.key_name} does not exist \
returning None")
return None
return value


@dataclass
class OphydTarget(BaseValue):
device_name: str
component_path: list[str]
2 changes: 1 addition & 1 deletion examples/mfx_dg1/mfx_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from beams.tree_config import save_tree_item_to_path
from beams.tree_config.action import SetPVActionItem
from beams.tree_config.base import EPICSValue, FixedValue
from beams.tree_config.value import EPICSValue, FixedValue
from beams.tree_config.composite import (SelectorItem, SequenceConditionItem,
SequenceItem)
from beams.tree_config.condition import (BinaryConditionItem,
Expand Down

0 comments on commit 478c6dd

Please sign in to comment.