Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions adf_core_python/core/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time as _time
from abc import abstractmethod
from threading import Event
from typing import Any, Callable, NoReturn
from typing import Any, Callable, NoReturn, Optional

from bitarray import bitarray
from rcrs_core.commands.AKClear import AKClear
Expand All @@ -24,10 +24,10 @@
from rcrs_core.connection.URN import Entity as EntityURN
from rcrs_core.messages.AKAcknowledge import AKAcknowledge
from rcrs_core.messages.AKConnect import AKConnect
from rcrs_core.messages.controlMessageFactory import ControlMessageFactory
from rcrs_core.messages.KAConnectError import KAConnectError
from rcrs_core.messages.KAConnectOK import KAConnectOK
from rcrs_core.messages.KASense import KASense
from rcrs_core.messages.controlMessageFactory import ControlMessageFactory
from rcrs_core.worldmodel.changeSet import ChangeSet
from rcrs_core.worldmodel.entityID import EntityID
from rcrs_core.worldmodel.worldmodel import WorldModel
Expand Down Expand Up @@ -79,6 +79,7 @@
CommunicationModule,
)
from adf_core_python.core.config.config import Config
from adf_core_python.core.gateway.gateway_agent import GatewayAgent
from adf_core_python.core.launcher.config_key import ConfigKey
from adf_core_python.core.logger.logger import get_agent_logger, get_logger

Expand All @@ -94,6 +95,7 @@ def __init__(
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
gateway_agent: Optional[GatewayAgent],
) -> None:
self.name = name
self.connect_request_id = None
Expand Down Expand Up @@ -124,6 +126,8 @@ def __init__(
self._message_manager: MessageManager = MessageManager()
self._communication_module: CommunicationModule = StandardCommunicationModule()

self._gateway_agent: Optional[GatewayAgent] = gateway_agent

def get_entity_id(self) -> EntityID:
return self.agent_id

Expand All @@ -146,14 +150,20 @@ def post_connect(self) -> None:
self._ignore_time: int = int(
self.config.get_value("kernel.agents.ignoreuntil", 3)
)

self._scenario_info: ScenarioInfo = ScenarioInfo(self.config, self._mode)
self._world_info: WorldInfo = WorldInfo(self.world_model)
self._agent_info = AgentInfo(self, self.world_model)

if isinstance(self._gateway_agent, GatewayAgent):
self._gateway_agent.set_initialize_data(
self._agent_info, self._world_info, self._scenario_info
)

self.logger = get_agent_logger(
f"{self.__class__.__module__}.{self.__class__.__qualname__}",
self._agent_info,
)

self.logger.debug(f"agent_config: {self.config}")

def update_step_info(
Expand Down Expand Up @@ -193,6 +203,12 @@ def update_step_info(
self._agent_info.set_change_set(change_set)
self._world_info.set_change_set(change_set)

if (
isinstance(self._gateway_agent, GatewayAgent)
and self._gateway_agent.is_initialized()
):
self._gateway_agent.update()

self._message_manager.refresh()
self._communication_module.receive(self, self._message_manager)

Expand Down Expand Up @@ -288,9 +304,11 @@ def handler_sense(self, msg: Any) -> None:
if herad_command.urn == CommandURN.AK_SPEAK:
heard_commands.append(
AKSpeak(
herad_command.components[
ComponentControlMessageID.AgentID
].entityID,
EntityID(
herad_command.components[
ComponentControlMessageID.AgentID
].entityID
),
herad_command.components[
ComponentControlMessageID.Time
].intValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def register_message_class(
"""
if index >= self.MAX_MESSAGE_CLASS_COUNT:
raise ValueError(
f"Possible index values are 0 to {self.MAX_MESSAGE_CLASS_COUNT-1}"
f"Possible index values are 0 to {self.MAX_MESSAGE_CLASS_COUNT - 1}"
)
self.__message_classes[index] = message_class

Expand Down
4 changes: 2 additions & 2 deletions adf_core_python/core/agent/info/scenario_info.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from enum import Enum
from enum import IntEnum
from typing import TypeVar

from adf_core_python.core.config.config import Config

T = TypeVar("T")


class Mode(Enum):
class Mode(IntEnum):
NON_PRECOMPUTE = 0
PRECOMPUTED = 1
PRECOMPUTATION = 2
Expand Down
93 changes: 71 additions & 22 deletions adf_core_python/core/agent/module/module_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import importlib
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Optional

from adf_core_python.core.component.action.extend_action import ExtendAction
from adf_core_python.core.component.communication.channel_subscriber import (
Expand All @@ -11,7 +11,13 @@
MessageCoordinator,
)
from adf_core_python.core.component.module.abstract_module import AbstractModule
from adf_core_python.core.logger.logger import get_logger
from adf_core_python.core.gateway.component.module.gateway_abstract_module import (
GatewayAbstractModule,
)
from adf_core_python.core.gateway.gateway_agent import GatewayAgent
from adf_core_python.core.gateway.gateway_module import GatewayModule
from adf_core_python.core.gateway.module_dict import ModuleDict
from adf_core_python.core.logger.logger import get_agent_logger

if TYPE_CHECKING:
from adf_core_python.core.agent.config.module_config import ModuleConfig
Expand All @@ -29,12 +35,14 @@ def __init__(
scenario_info: ScenarioInfo,
module_config: ModuleConfig,
develop_data: DevelopData,
gateway_agent: Optional[GatewayAgent] = None,
) -> None:
self._agent_info = agent_info
self._world_info = world_info
self._scenario_info = scenario_info
self._module_config = module_config
self._develop_data = develop_data
self._gateway_agent = gateway_agent
self._modules: dict[str, AbstractModule] = {}
self._actions: dict[str, ExtendAction] = {}

Expand All @@ -43,38 +51,79 @@ def __init__(
self._channel_subscribers: dict[str, Any] = {}
self._message_coordinators: dict[str, Any] = {}

def get_module(self, module_name: str, default_module_name: str) -> AbstractModule:
class_name = self._module_config.get_value(module_name)
if class_name is None:
get_logger("ModuleManager").warning(
f"Module key {module_name} not found in config, using default module {default_module_name}"
)
class_name = default_module_name
self._module_dict: ModuleDict = ModuleDict()

module_class: type = self._load_module(class_name)
self._logger = get_agent_logger(
f"{self.__class__.__module__}.{self.__class__.__qualname__}",
self._agent_info,
)

def get_module(
self, module_name: str, default_module_class_name: str
) -> AbstractModule:
instance = self._modules.get(module_name)
if instance is not None:
return instance

if issubclass(module_class, AbstractModule):
instance = module_class(
self._agent_info,
self._world_info,
self._scenario_info,
self,
self._develop_data,
)
self._modules[module_name] = instance
return instance
class_name = self._module_config.get_value(module_name)
if class_name is not None:
try:
module_class: type = self._load_module(class_name)
if issubclass(module_class, AbstractModule):
instance = module_class(
self._agent_info,
self._world_info,
self._scenario_info,
self,
self._develop_data,
)
self._modules[module_name] = instance
return instance
except ModuleNotFoundError:
self._logger.warning(
f"Module {module_name} not found in python. "
f"If gateway flag is active, using module {module_name} in java"
)

if isinstance(self._gateway_agent, GatewayAgent):
gateway_module = GatewayModule(self._gateway_agent)
java_class_name = gateway_module.initialize(module_name, "")
class_name = self._module_dict[java_class_name]
if class_name is not None:
module_class = self._load_module(class_name)
if issubclass(module_class, GatewayAbstractModule):
instance = module_class(
self._agent_info,
self._world_info,
self._scenario_info,
self,
self._develop_data,
gateway_module,
)
self._modules[module_name] = instance
return instance

class_name = default_module_class_name
if class_name is not None:
module_class = self._load_module(class_name)
if issubclass(module_class, AbstractModule):
instance = module_class(
self._agent_info,
self._world_info,
self._scenario_info,
self,
self._develop_data,
)
self._modules[module_name] = instance
return instance

raise RuntimeError(f"Module {class_name} is not a subclass of AbstractModule")

def get_extend_action(
self, action_name: str, default_action_name: str
self, action_name: str, default_action_class_name: str
) -> ExtendAction:
class_name = self._module_config.get_value_or_default(
action_name, default_action_name
action_name, default_action_class_name
)

action_class: type = self._load_module(class_name)
Expand Down
4 changes: 4 additions & 0 deletions adf_core_python/core/agent/office/office.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from threading import Event
from typing import Optional

from adf_core_python.core.agent.agent import Agent
from adf_core_python.core.agent.config.module_config import ModuleConfig
Expand All @@ -7,6 +8,7 @@
from adf_core_python.core.agent.module.module_manager import ModuleManager
from adf_core_python.core.agent.precompute.precompute_data import PrecomputeData
from adf_core_python.core.component.tactics.tactics_center import TacticsCenter
from adf_core_python.core.gateway.gateway_agent import GatewayAgent
from adf_core_python.core.logger.logger import get_agent_logger


Expand All @@ -21,6 +23,7 @@ def __init__(
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
gateway_agent: Optional[GatewayAgent],
) -> None:
super().__init__(
is_precompute,
Expand All @@ -31,6 +34,7 @@ def __init__(
module_config,
develop_data,
finish_post_connect_event,
gateway_agent,
)
self._tactics_center = tactics_center
self._team_name = team_name
Expand Down
4 changes: 4 additions & 0 deletions adf_core_python/core/agent/office/office_ambulance.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from threading import Event
from typing import Optional

from rcrs_core.connection.URN import Entity as EntityURN

from adf_core_python.core.agent.config.module_config import ModuleConfig
from adf_core_python.core.agent.develop.develop_data import DevelopData
from adf_core_python.core.agent.office.office import Office
from adf_core_python.core.component.tactics.tactics_center import TacticsCenter
from adf_core_python.core.gateway.gateway_agent import GatewayAgent


class OfficeAmbulance(Office):
Expand All @@ -19,6 +21,7 @@ def __init__(
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
gateway_agent: Optional[GatewayAgent],
) -> None:
super().__init__(
tactics_center,
Expand All @@ -29,6 +32,7 @@ def __init__(
module_config,
develop_data,
finish_post_connect_event,
gateway_agent,
)

def precompute(self) -> None:
Expand Down
4 changes: 4 additions & 0 deletions adf_core_python/core/agent/office/office_fire.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from threading import Event
from typing import Optional

from rcrs_core.connection.URN import Entity as EntityURN

from adf_core_python.core.agent.config.module_config import ModuleConfig
from adf_core_python.core.agent.develop.develop_data import DevelopData
from adf_core_python.core.agent.office.office import Office
from adf_core_python.core.component.tactics.tactics_center import TacticsCenter
from adf_core_python.core.gateway.gateway_agent import GatewayAgent


class OfficeFire(Office):
Expand All @@ -19,6 +21,7 @@ def __init__(
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
gateway_agent: Optional[GatewayAgent],
) -> None:
super().__init__(
tactics_center,
Expand All @@ -29,6 +32,7 @@ def __init__(
module_config,
develop_data,
finish_post_connect_event,
gateway_agent,
)

def get_requested_entities(self) -> list[EntityURN]:
Expand Down
4 changes: 4 additions & 0 deletions adf_core_python/core/agent/office/office_police.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from threading import Event
from typing import Optional

from rcrs_core.connection.URN import Entity as EntityURN

from adf_core_python.core.agent.config.module_config import ModuleConfig
from adf_core_python.core.agent.develop.develop_data import DevelopData
from adf_core_python.core.agent.office.office import Office
from adf_core_python.core.component.tactics.tactics_center import TacticsCenter
from adf_core_python.core.gateway.gateway_agent import GatewayAgent


class OfficePolice(Office):
Expand All @@ -19,6 +21,7 @@ def __init__(
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
gateway_agent: Optional[GatewayAgent],
) -> None:
super().__init__(
tactics_center,
Expand All @@ -29,6 +32,7 @@ def __init__(
module_config,
develop_data,
finish_post_connect_event,
gateway_agent,
)

def get_requested_entities(self) -> list[EntityURN]:
Expand Down
Loading
Loading