Skip to content
Snippets Groups Projects
Commit 4c90e0f2 authored by Stefano Borini's avatar Stefano Borini
Browse files

Added fast creation by just defining the classes

parent 766163d2
No related branches found
No related tags found
1 merge request!114Introduces fast declaration style for factory
import abc
import logging
from traits.api import ABCHasStrictTraits, provides, String, Instance
from envisage.plugin import Plugin
from force_bdss.data_sources.base_data_source import BaseDataSource
from force_bdss.data_sources.base_data_source_model import BaseDataSourceModel
from .i_data_source_factory import IDataSourceFactory
log = logging.getLogger(__name__)
@provides(IDataSourceFactory)
class BaseDataSourceFactory(ABCHasStrictTraits):
......@@ -19,14 +23,23 @@ class BaseDataSourceFactory(ABCHasStrictTraits):
#: A human readable name of the factory. Spaces allowed
name = String()
#: The data source to be instantiated. Define this to your DataSource
data_source_class = Instance(BaseDataSource)
#: The model associated to the data source.
#: Define this to your DataSourceModel
model_class = Instance(BaseDataSourceModel)
#: Reference to the plugin that carries this factory
#: This is automatically set by the system. you should not define it
#: in your subclass.
plugin = Instance(Plugin)
def __init__(self, plugin, *args, **kwargs):
self.plugin = plugin
super(BaseDataSourceFactory, self).__init__(*args, **kwargs)
@abc.abstractmethod
def create_data_source(self):
"""Factory method.
Must return the factory-specific BaseDataSource instance.
......@@ -36,8 +49,15 @@ class BaseDataSourceFactory(ABCHasStrictTraits):
BaseDataSource
The specific instance of the generated DataSource
"""
if self.data_source_class is None:
msg = ("data_source_class cannot be None in {}. Either define "
"data_source_class or reimplement create_data_source on "
"your factory class.".format(self.__class__.__name__))
log.error(msg)
raise RuntimeError(msg)
return self.data_source_class(self)
@abc.abstractmethod
def create_model(self, model_data=None):
"""Factory method.
Creates the model object (or network of model objects) of the KPI
......@@ -55,3 +75,14 @@ class BaseDataSourceFactory(ABCHasStrictTraits):
BaseDataSourceModel
The model
"""
if model_data is None:
model_data = {}
if self.model_class is None:
msg = ("model_class cannot be None in {}. Either define "
"model_class or reimplement create_model on your "
"factory class.".format(self.__class__.__name__))
log.error(msg)
raise RuntimeError(msg)
return self.model_class(self, **model_data)
from envisage.api import Plugin
from traits.api import Interface, String, Instance
from force_bdss.data_sources.base_data_source import BaseDataSource
from force_bdss.data_sources.base_data_source_model import BaseDataSourceModel
class IDataSourceFactory(Interface):
"""Envisage required interface for the BaseDataSourceFactory.
......@@ -12,6 +15,10 @@ class IDataSourceFactory(Interface):
name = String()
data_source_class = Instance(BaseDataSource)
model_class = Instance(BaseDataSourceModel)
plugin = Instance(Plugin)
def create_data_source(self):
......
import abc
import logging
from envisage.plugin import Plugin
from traits.api import ABCHasStrictTraits, provides, String, Instance
from force_bdss.kpi.base_kpi_calculator import BaseKPICalculator
from force_bdss.kpi.base_kpi_calculator_model import BaseKPICalculatorModel
from .i_kpi_calculator_factory import IKPICalculatorFactory
log = logging.getLogger(__name__)
@provides(IKPICalculatorFactory)
class BaseKPICalculatorFactory(ABCHasStrictTraits):
"""Base class for the Key Performance Indicator calculator factories.
......@@ -20,6 +25,13 @@ class BaseKPICalculatorFactory(ABCHasStrictTraits):
#: A UI friendly name for the factory. Can contain spaces.
name = String()
#: The KPI calculator to be instantiated. Define this to your KPICalculator
kpi_calculator_class = Instance(BaseKPICalculator)
#: The model associated to the KPI calculator.
#: Define this to your KPICalculatorModel
model_class = Instance(BaseKPICalculatorModel)
#: A reference to the plugin that holds this factory.
plugin = Instance(Plugin)
......@@ -34,7 +46,6 @@ class BaseKPICalculatorFactory(ABCHasStrictTraits):
self.plugin = plugin
super(BaseKPICalculatorFactory, self).__init__(*args, **kwargs)
@abc.abstractmethod
def create_kpi_calculator(self):
"""Factory method.
Creates and returns an instance of a KPI Calculator, associated
......@@ -45,8 +56,15 @@ class BaseKPICalculatorFactory(ABCHasStrictTraits):
BaseKPICalculator
The specific instance of the generated KPICalculator
"""
if self.kpi_calculator_class is None:
msg = ("kpi_calculator_class cannot be None in {}. Either define "
"kpi_calculator_class or reimplement create_kpi_calculator "
"on your factory class.".format(self.__class__.__name__))
log.error(msg)
raise RuntimeError(msg)
return self.data_source_class(self)
@abc.abstractmethod
def create_model(self, model_data=None):
"""Factory method.
Creates the model object (or network of model objects) of the KPI
......@@ -64,3 +82,14 @@ class BaseKPICalculatorFactory(ABCHasStrictTraits):
BaseKPICalculatorModel
The model
"""
if model_data is None:
model_data = {}
if self.model_class is None:
msg = ("model_class cannot be None in {}. Either define "
"model_class or reimplement create_model on your "
"factory class.".format(self.__class__.__name__))
log.error(msg)
raise RuntimeError(msg)
return self.model_class(self, **model_data)
from traits.api import Interface, String, Instance
from envisage.plugin import Plugin
from force_bdss.kpi.base_kpi_calculator import BaseKPICalculator
from force_bdss.kpi.base_kpi_calculator_model import BaseKPICalculatorModel
class IKPICalculatorFactory(Interface):
"""Envisage required interface for the BaseKPICalculatorFactory.
......@@ -12,6 +15,10 @@ class IKPICalculatorFactory(Interface):
name = String()
kpi_calculator_class = Instance(BaseKPICalculator)
model_class = Instance(BaseKPICalculatorModel)
plugin = Instance(Plugin)
def create_kpi_calculator(self):
......
import abc
import logging
from traits.api import ABCHasStrictTraits, String, provides, Instance
from envisage.plugin import Plugin
from force_bdss.mco.base_mco import BaseMCO
from force_bdss.mco.base_mco_communicator import BaseMCOCommunicator
from force_bdss.mco.base_mco_model import BaseMCOModel
from .i_mco_factory import IMCOFactory
log = logging.getLogger(__name__)
@provides(IMCOFactory)
class BaseMCOFactory(ABCHasStrictTraits):
......@@ -19,6 +23,15 @@ class BaseMCOFactory(ABCHasStrictTraits):
#: A user friendly name of the factory. Spaces allowed.
name = String()
#: The optimizer class to instantiate. Define this to your MCO class.
optimizer_class = Instance(BaseMCO)
#: The model associated to the MCO. Define this to your MCO model class.
model_class = Instance(BaseMCOModel)
#: The communicator associated to the MCO. Define this to your MCO comm.
communicator_class = Instance(BaseMCOCommunicator)
#: A reference to the Plugin that holds this factory.
plugin = Instance(Plugin)
......@@ -26,7 +39,6 @@ class BaseMCOFactory(ABCHasStrictTraits):
self.plugin = plugin
super(BaseMCOFactory, self).__init__(*args, **kwargs)
@abc.abstractmethod
def create_optimizer(self):
"""Factory method.
Creates the optimizer with the given application
......@@ -34,11 +46,18 @@ class BaseMCOFactory(ABCHasStrictTraits):
Returns
-------
BaseMCOOptimizer
BaseMCO
The optimizer
"""
if self.optimizer_class is None:
msg = ("optimizer_class cannot be None in {}. Either define "
"optimizer_class or reimplement create_optimizer on "
"your factory class.".format(self.__class__.__name__))
log.error(msg)
raise RuntimeError(msg)
return self.optimizer_class(self)
@abc.abstractmethod
def create_model(self, model_data=None):
"""Factory method.
Creates the model object (or network of model objects) of the MCO.
......@@ -57,8 +76,18 @@ class BaseMCOFactory(ABCHasStrictTraits):
BaseMCOModel
The MCOModel
"""
if model_data is None:
model_data = {}
if self.model_class is None:
msg = ("model_class cannot be None in {}. Either define "
"model_class or reimplement create_model on your "
"factory class.".format(self.__class__.__name__))
log.error(msg)
raise RuntimeError(msg)
return self.model_class(self, **model_data)
@abc.abstractmethod
def create_communicator(self):
"""Factory method. Returns the communicator class that allows
exchange between the MCO and the evaluator code.
......@@ -68,8 +97,15 @@ class BaseMCOFactory(ABCHasStrictTraits):
BaseMCOCommunicator
An instance of the communicator
"""
if self.communicator_class is None:
msg = ("communicator_class cannot be None in {}. Either define "
"communicator_class or reimplement create_communicator on "
"your factory class.".format(self.__class__.__name__))
log.error(msg)
raise RuntimeError(msg)
return self.communicator_class(self)
@abc.abstractmethod
def parameter_factories(self):
"""Returns the parameter factories supported by this MCO
......
from traits.api import Interface, String, Instance
from envisage.plugin import Plugin
from force_bdss.mco.base_mco import BaseMCO
from force_bdss.mco.base_mco_communicator import BaseMCOCommunicator
from force_bdss.mco.base_mco_model import BaseMCOModel
class IMCOFactory(Interface):
"""Interface for the BaseMCOFactory.
......@@ -12,6 +16,12 @@ class IMCOFactory(Interface):
name = String()
optimizer_class = Instance(BaseMCO)
model_class = Instance(BaseMCOModel)
communicator_class = Instance(BaseMCOCommunicator)
plugin = Instance(Plugin)
def create_optimizer(self):
......
import abc
import logging
from traits.api import ABCHasStrictTraits, Instance, String, provides
from envisage.plugin import Plugin
from force_bdss.notification_listeners.base_notification_listener import \
BaseNotificationListener
from force_bdss.notification_listeners.base_notification_listener_model \
import \
BaseNotificationListenerModel
from .i_notification_listener_factory import INotificationListenerFactory
log = logging.getLogger(__name__)
@provides(INotificationListenerFactory)
class BaseNotificationListenerFactory(ABCHasStrictTraits):
......@@ -18,6 +24,14 @@ class BaseNotificationListenerFactory(ABCHasStrictTraits):
#: Name of the factory. User friendly for UI
name = String()
#: The listener class that must be instantiated. Define this to your
#: listener class.
listener_class = Instance(BaseNotificationListener)
#: The associated model to the listener. Define this to your
#: listener model class.
model_class = Instance(BaseNotificationListenerModel)
#: A reference to the containing plugin
plugin = Instance(Plugin)
......@@ -32,13 +46,19 @@ class BaseNotificationListenerFactory(ABCHasStrictTraits):
self.plugin = plugin
super(BaseNotificationListenerFactory, self).__init__(*args, **kwargs)
@abc.abstractmethod
def create_listener(self):
"""
Creates an instance of the listener.
"""
if self.listener_class is None:
msg = ("listener_class cannot be None in {}. Either define "
"listener_class or reimplement create_listener on "
"your factory class.".format(self.__class__.__name__))
log.error(msg)
raise RuntimeError(msg)
return self.listener_class(self)
@abc.abstractmethod
def create_model(self, model_data=None):
"""
Creates an instance of the model.
......@@ -48,3 +68,14 @@ class BaseNotificationListenerFactory(ABCHasStrictTraits):
model_data: dict
Data to use to fill the model.
"""
if model_data is None:
model_data = {}
if self.model_class is None:
msg = ("model_class cannot be None in {}. Either define "
"model_class or reimplement create_model on your "
"factory class.".format(self.__class__.__name__))
log.error(msg)
raise RuntimeError(msg)
return self.model_class(self, **model_data)
from traits.api import Interface, String, Instance
from envisage.plugin import Plugin
from force_bdss.notification_listeners.base_notification_listener import \
BaseNotificationListener
from force_bdss.notification_listeners.base_notification_listener_model \
import \
BaseNotificationListenerModel
class INotificationListenerFactory(Interface):
"""Envisage required interface for the BaseNotificationListenerFactory.
......@@ -12,6 +18,10 @@ class INotificationListenerFactory(Interface):
name = String()
listener_class = Instance(BaseNotificationListener)
model_class = Instance(BaseNotificationListenerModel)
plugin = Instance(Plugin)
def create_listener(self):
......
import abc
import logging
from traits.api import ABCHasStrictTraits, Instance, String, provides
from envisage.plugin import Plugin
from force_bdss.ui_hooks.base_ui_hooks_manager import BaseUIHooksManager
from .i_ui_hooks_factory import IUIHooksFactory
log = logging.getLogger(__name__)
@provides(IUIHooksFactory)
class BaseUIHooksFactory(ABCHasStrictTraits):
......@@ -18,6 +20,10 @@ class BaseUIHooksFactory(ABCHasStrictTraits):
#: Name of the factory. User friendly for UI
name = String()
#: The UI Hooks manager class to instantiate. Define this to your
#: base hook managers.
ui_hooks_manager_class = Instance(BaseUIHooksManager)
#: A reference to the containing plugin
plugin = Instance(Plugin)
......@@ -32,7 +38,6 @@ class BaseUIHooksFactory(ABCHasStrictTraits):
self.plugin = plugin
super(BaseUIHooksFactory, self).__init__(*args, **kwargs)
@abc.abstractmethod
def create_ui_hooks_manager(self):
"""Creates an instance of the hook manager.
The hooks manager contains a set of methods that are applicable in
......@@ -42,3 +47,12 @@ class BaseUIHooksFactory(ABCHasStrictTraits):
-------
BaseUIHooksManager
"""
if self.ui_hooks_manager_class is None:
msg = ("ui_hooks_manager_class cannot be None in {}. Either "
"define ui_hooks_manager_class or reimplement "
"create_ui_hooks_manager on "
"your factory class.".format(self.__class__.__name__))
log.error(msg)
raise RuntimeError(msg)
return self.ui_hooks_manager_class(self)
from traits.api import Interface, String, Instance
from envisage.plugin import Plugin
from force_bdss.ui_hooks.base_ui_hooks_manager import BaseUIHooksManager
class IUIHooksFactory(Interface):
"""Envisage required interface for the BaseUIHooksFactory.
......@@ -12,6 +14,8 @@ class IUIHooksFactory(Interface):
name = String()
ui_hooks_manager_class = Instance(BaseUIHooksManager)
plugin = Instance(Plugin)
def create_hook_manager(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment