Source code for loads.controller_command.api.controller_command_api

import typing

from more.api.exceptions.api_exception import NameNotFoundError
from more.api.simulation.load_cases.load_setup import LoadSetup, TranslationSettableMixin, RotationSettableMixin
from more.api.utils.interface_registries import register_api_implementation
from more.loads.controller_command import ControllerCommand
from more import log

log.init_logging()
logger = log.getLogger(__name__)

def _controller_command_api_setup(api):
    simulation_setup = api.create_simulation_setup()
    lcc_setup = simulation_setup.create_load_case_container_setup().set_name(name='Example LCC')
    lc_setup = lcc_setup.create_load_case_setup(lc_type='Mechanical load case').set_name(name='Example LC')
    return lc_setup.create_load_setup(load_type='Controller command')


[docs] @register_api_implementation(core_class=ControllerCommand) class ControllerCommandSetup(LoadSetup): """ The API handler for controller commands The following example shows how to create a controller command using the API, it is assumed that these steps were performed in each of the examples for other methods in this class. .. admonition:: Example :class: note .. >>> import more.project >>> proj = more.project.Project() >>> from more.api import ApiGateway >>> api = ApiGateway(proj=proj) >>> simulation_setup = api.create_simulation_setup() >>> lcc_setup = simulation_setup.create_load_case_container_setup().set_name(name='Example LCC') >>> lc_setup = lcc_setup.create_load_case_setup(lc_type='Mechanical load case').set_name(name='Example LC') >>> controller_command_setup = lc_setup.create_load_setup(load_type='Controller command') .. >>> isinstance(controller_command_setup, ControllerCommandSetup) True >>> isinstance(controller_command_setup._load, ControllerCommand) True The next example shows how to get an API object for an already existing load. .. admonition:: Example :class: note .. >>> import more.project >>> proj = more.project.Project() >>> from more.api import ApiGateway >>> api = ApiGateway(proj=proj) >>> simulation_setup = api.create_simulation_setup() >>> lcc_setup = simulation_setup.create_load_case_container_setup().set_name(name='Example LCC') >>> lc_setup = lcc_setup.create_load_case_setup(lc_type='Mechanical load case').set_name(name='Example LC') .. >>> controller_command_setup = lc_setup.create_load_setup(load_type='Controller command') >>> controller_command_setup = lc_setup.get_load_setup(index=0) # The load under this index must already exist .. >>> isinstance(controller_command_setup, ControllerCommandSetup) True >>> isinstance(controller_command_setup._load, ControllerCommand) True """
[docs] def set_controller_by_name(self, controller_name: str) -> 'ControllerCommandSetup': """ Sets the controller for the associated controller command See example at the top for how to create a controller command setup object .. admonition:: Example :class: note .. >>> import more.project >>> proj = more.project.Project() >>> from more.api import ApiGateway >>> api = ApiGateway(proj=proj) >>> controller_command_setup = _controller_command_api_setup(api) >>> proj.comp.add_controller() >>> controller = proj.comp.controller[0] >>> controller.name = 'example controller' >>> controller_command_setup = controller_command_setup.set_controller_by_name(controller_name='example controller') # The controller with this name must already exist .. >>> controller_command_setup._load.controller_selector.selected_entity == controller True >>> controller_command_setup = controller_command_setup.set_controller_by_name(controller_name='nonexistent name') Traceback (most recent call last): ... more.api.exceptions.api_exception.NameNotFoundError: ... Returns ------- self: ControllerCommandSetup Raises ------ NameNotFoundError Raised if a key in the name is not a viable controller """ self._load.controller_selector.select_entity_by_name(name=controller_name) return self
[docs] def set_command_value_by_name(self, command_value_name) -> 'ControllerCommandSetup': """ Sets the command value for the associated controller command See example at the top for how to create a controller command setup object .. admonition:: Example :class: note .. >>> import more.project >>> proj = more.project.Project() >>> from more.api import ApiGateway >>> api = ApiGateway(proj=proj) >>> controller_command_setup = _controller_command_api_setup(api) >>> proj.comp.add_controller() >>> controller = proj.comp.controller[0] >>> controller.name = 'example controller' >>> controller_command_setup = controller_command_setup.set_controller_by_name(controller_name='example controller') # The controller with this name must already exist >>> controller_command_setup = controller_command_setup.set_command_value_by_name(command_value_name='Position command value') .. >>> controller_command_setup._load.command_value.name == 'Position command value' True >>> controller_command_setup = controller_command_setup.set_command_value_by_name('nonexistent name') Traceback (most recent call last): ... more.api.exceptions.api_exception.NameNotFoundError: ... Returns ------- self: ControllerCommandSetup Raises ------ NameNotFoundError Raised if a key in there is no command value with the given name """ try: self._load.command_value = self._available_command_values_dict[command_value_name] except KeyError: raise NameNotFoundError('Command value with name: {} not found, available command values are: {}'.format(command_value_name, self._available_command_values_dict.keys())) return self
@property def available_command_value_names(self) -> typing.List[str]: """ Returns the list of available command value names See example at the top for how to create a controller command setup object .. admonition:: Example :class: note .. >>> import more.project >>> proj = more.project.Project() >>> from more.api import ApiGateway >>> api = ApiGateway(proj=proj) >>> controller_command_setup = _controller_command_api_setup(api) >>> proj.comp.add_controller() >>> controller = proj.comp.controller[0] >>> controller.name = 'example controller' >>> controller_command_setup.available_command_value_names ['Position command value', 'Velocity command value'] Returns ------- list of strings The list of available command values """ return list(self._available_command_values_dict.keys()) @property def available_command_values_dict(self): """ Deprecated method .. admonition:: Deprecated :class: warning `available_command_values_dict` will be removed in a future MORe release, it is replaced by :meth:`~.available_command_value_names` """ logger.warning( 'Deprecation: \"available_command_values_dict\" will be removed in a future MORe release, it is replaced by \"available_command_value_names\"') return self._available_command_values_dict @property def _available_command_values_dict(self): return {cv.name: cv for cv in self._load._available_command_values}
[docs] def set_controller(self, controller): """ Deprecated method .. admonition:: Deprecated :class: warning `set_controller` will be removed in a future MORe release, it is replaced by :meth:`~.set_controller_by_name` """ logger.warning( 'Deprecation: \"set_controller\" will be removed in a future MORe release, it is replaced by \"set_controller_by_name\"') return self.set_controller_by_name(controller_name=controller.name)
[docs] def set_command_value(self, command_value): """ Deprecated method .. admonition:: Deprecated :class: warning `set_command_value` will be removed in a future MORe release, it is replaced by :meth:`~.set_command_value_by_name` """ logger.warning( 'Deprecation: \"set_command_value\" will be removed in a future MORe release, it is replaced by \"set_command_value_by_name\"') return self.set_command_value_by_name(command_value_name=command_value.name)