brica1.module のソースコード

# -*- coding: utf-8 -*-

"""
module.py
=====

This module contains the class `Module` and `Agent` which serve
as abstractions for distict areas in the brain or sub-regions of those areas.
`Module`s together with `Component`s are collectively reffered to as `Unit`s.

"""

__all__ = ["Module", "Agent"]

import copy
import numpy

# BriCA imports
from component import *
from unit import *

[ドキュメント]class Module(Unit): """ A `Module` may not have an implementation and may only exchange `numpy.ndarray` of the type `numpy.short` through in/out ports with another module. """ def __init__(self): """ Create a new `Module` instance. Args: None. Returns: Module: a new `Module` instance. """ super(Module, self).__init__() self.components = {} self.submodules = {}
[ドキュメント] def add_submodule(self, id, submodule): """ Add a `Module` to this `Module`. Args: id (str): a string ID. submodule (Module): a module to add for `id`. Returns: None. """ if not isinstance(submodule, Module): raise AssertionError("Not a Module instance") return if id in self.components: raise LookupError("There is already a component of the same name") return self.submodules[id] = submodule
[ドキュメント] def get_submodule(self, id): """ Get a `Module` for a given `id`. Args: id (str): a string ID. Returns: Module: a module for the given `id`. """ array = id.split(".") head = array.pop(0) child = self.submodules[head] if len(array) == 0: return child return child.get_submodule(".".join(array))
[ドキュメント] def get_all_submodules(self): """ Get all `Module`s recursively. Args: None. Returns: array: a array of all `Module`s. """ array = self.submodules.values() for submodule in array: array.extend(submodule.get_all_submodules()) return list(set(array))
[ドキュメント] def remove_submodule(self, id): """ Remove a module from this `Module`. Args: id (str): a string ID. Returns: None. """ del self.submodules[id]
[ドキュメント] def add_component(self, id, component): """ Add a `Component` to this `Module`. Args: id (str): a string ID. component (Component): a component to add for `id`. Returns: None. """ if not isinstance(component, Component): raise AssertionError("Not a Component instance") return if id in self.submodules: raise LookupError("There is already a submodule of the same name") return self.components[id] = component
[ドキュメント] def get_component(self, id): """ Get a `Component` for a given `id`. Args: id (str): a string ID. Returns: Component: a component for the given `id`. """ return self.components[id]
[ドキュメント] def get_all_components(self): """ Get all `Component`s of all `Module`s. Args: None. Returns: array: a array of all `Component`s. """ array = self.components.values() for submodule in self.get_all_submodules(): array.extend(submodule.get_all_components()) return list(set(array))
[ドキュメント] def remove_component(self, id): """ Remove a component from this `Module`. Args: id (str): a string ID. Returns: None. """ del self.components[id]
[ドキュメント]class Agent(Module): """ A `Agent` is a `Module` which serves as a top-level container for functional `Module`s. """ def __init__(self, scheduler): """ Create a new `Agent` instance. Args: scheduler (Scheduler): a scheduler to schedule `Component` firing. Returns: Agent: a new `Agent` instance. """ super(Agent, self).__init__() self.scheduler = scheduler
[ドキュメント] def step(self): """ Step the `Scheduler`. Args: None. Returns: float: the current time of the scheduler """ return self.scheduler.step()
[ドキュメント] def update_scheduler(self): """ Udpate the `Scheduler` with this `Agent`. Args: None. Returns: None. """ self.scheduler.update(self)
[ドキュメント] def add_submodule(self, id, submodule): """ Add a `Module` and update the `Scheduler`. Args: id (str): a string ID. submodule (Module): a module to add for `id`. Returns: None. """ super(Agent, self).add_submodule(id, submodule) self.update_scheduler()