Skip to content

Component manager base

ComponentManagerBase

Bases: Generic[ComponentTypeVar]

The ComponentManagerBase class, which is used to define the base class of the component manager.

Source code in agentuniverse/base/component/component_manager_base.py
Python
class ComponentManagerBase(Generic[ComponentTypeVar]):
    """The ComponentManagerBase class, which is used to define the base class of the component manager."""

    def __init__(self, component_type: ComponentEnum):
        """Initialize the ComponentManagerBase."""
        # The component pool map, which is used to store the component instance.
        # _instance_obj_map - Format: {component_instance_name: component_instance_obj}.
        self._instance_obj_map: dict[str, ComponentTypeVar] = {}
        self._component_type: ComponentEnum = component_type

    def register(self, component_instance_name: str, component_instance_obj: ComponentTypeVar):
        """Register the component instance."""
        if component_instance_name in self._instance_obj_map.keys():
            raise ValueError(f"{self._component_type.value} component object instance with name "
                             f"'{component_instance_name}' already exists.")
        self._instance_obj_map[component_instance_name] = component_instance_obj

    def unregister(self, component_instance_name: str):
        """Unregister the component instance abstractmethod."""
        self._instance_obj_map.pop(component_instance_name)

    def get_instance_obj(self, component_instance_name: str,
                         appname: str = None) -> ComponentTypeVar:
        """Return the component instance object."""
        appname = appname or ApplicationConfigManager().app_configer.base_info_appname
        instance_code = f'{appname}.{self._component_type.value.lower()}.{component_instance_name}'
        return self._instance_obj_map.get(instance_code)

    def get_instance_name_list(self) -> list[str]:
        """Return the component instance list."""
        return list(self._instance_obj_map.keys())

    def get_instance_obj_list(self) -> list[ComponentTypeVar]:
        """Return the component instance object list."""
        return list(self._instance_obj_map.values())

__init__(component_type)

Initialize the ComponentManagerBase.

Source code in agentuniverse/base/component/component_manager_base.py
Python
def __init__(self, component_type: ComponentEnum):
    """Initialize the ComponentManagerBase."""
    # The component pool map, which is used to store the component instance.
    # _instance_obj_map - Format: {component_instance_name: component_instance_obj}.
    self._instance_obj_map: dict[str, ComponentTypeVar] = {}
    self._component_type: ComponentEnum = component_type

get_instance_name_list()

Return the component instance list.

Source code in agentuniverse/base/component/component_manager_base.py
Python
def get_instance_name_list(self) -> list[str]:
    """Return the component instance list."""
    return list(self._instance_obj_map.keys())

get_instance_obj(component_instance_name, appname=None)

Return the component instance object.

Source code in agentuniverse/base/component/component_manager_base.py
Python
def get_instance_obj(self, component_instance_name: str,
                     appname: str = None) -> ComponentTypeVar:
    """Return the component instance object."""
    appname = appname or ApplicationConfigManager().app_configer.base_info_appname
    instance_code = f'{appname}.{self._component_type.value.lower()}.{component_instance_name}'
    return self._instance_obj_map.get(instance_code)

get_instance_obj_list()

Return the component instance object list.

Source code in agentuniverse/base/component/component_manager_base.py
Python
def get_instance_obj_list(self) -> list[ComponentTypeVar]:
    """Return the component instance object list."""
    return list(self._instance_obj_map.values())

register(component_instance_name, component_instance_obj)

Register the component instance.

Source code in agentuniverse/base/component/component_manager_base.py
Python
def register(self, component_instance_name: str, component_instance_obj: ComponentTypeVar):
    """Register the component instance."""
    if component_instance_name in self._instance_obj_map.keys():
        raise ValueError(f"{self._component_type.value} component object instance with name "
                         f"'{component_instance_name}' already exists.")
    self._instance_obj_map[component_instance_name] = component_instance_obj

unregister(component_instance_name)

Unregister the component instance abstractmethod.

Source code in agentuniverse/base/component/component_manager_base.py
Python
def unregister(self, component_instance_name: str):
    """Unregister the component instance abstractmethod."""
    self._instance_obj_map.pop(component_instance_name)