Skip to content
Snippets Groups Projects
Commit 781b4489 authored by Stefano Borini's avatar Stefano Borini Committed by GitHub
Browse files

Merge pull request #44 from force-h2020/send-to-mco-interface

Cleanup of the MCO communicator interface.
parents d67b47fa ad5665e0
No related branches found
No related tags found
No related merge requests found
import abc import abc
import six
from traits.api import ABCHasStrictTraits, Instance
from .base_mco_model import BaseMCOModel
from ..bdss_application import BDSSApplication
from .i_multi_criteria_optimizer_bundle import IMultiCriteriaOptimizerBundle
class BaseMCOCommunicator(ABCHasStrictTraits):
"""Communicator class that defines how the MCO communicates
with the evaluator program that does the actual heavylifting of the
calculation.
The model assumes that the MCO spawns a process to perform the evaluation,
and passes data to this process that define the parameters for the
evaluation. Once completed, the evaluation will return a set of results,
that we interpret as KPIs. These KPIs are encoded in some form, which is
again specified by the MCO.
"""
#: A reference to the bundle
bundle = Instance(IMultiCriteriaOptimizerBundle)
#: A reference to the application
application = Instance(BDSSApplication)
#: A reference to the model class
model = Instance(BaseMCOModel)
class BaseMCOCommunicator(six.with_metaclass(abc.ABCMeta)):
def __init__(self, bundle, application, model): def __init__(self, bundle, application, model):
self.bundle = bundle self.bundle = bundle
self.application = application self.application = application
...@@ -10,8 +32,30 @@ class BaseMCOCommunicator(six.with_metaclass(abc.ABCMeta)): ...@@ -10,8 +32,30 @@ class BaseMCOCommunicator(six.with_metaclass(abc.ABCMeta)):
@abc.abstractmethod @abc.abstractmethod
def receive_from_mco(self): def receive_from_mco(self):
pass """
Receives the parameters from the MCO.
The conversion is specific to the format of the communication
between the MCO and its evaluator program.
Must return a single DataSourceParameters object, containing
the parameters as passed by the MCO.
Returns
-------
DataSourceParameters
An instance of the DataSourceParameters with the appropriate
information filled in.
"""
@abc.abstractmethod @abc.abstractmethod
def send_to_mco(self): def send_to_mco(self, kpi_results):
pass """Send the KPI results from the evaluation to the MCO
Must be reimplemented to perform the conversion between the
two formats. This is of course dependent on the specifics of the
MCO and how it interacts with the external evaluator program.
Parameters
----------
kpi_results: List(KPICalculatorResult)
A list of KPI calculator results, one per each KPI calculator.
"""
import unittest
from force_bdss.mco.base_mco_communicator import BaseMCOCommunicator
from force_bdss.mco.base_mco_model import BaseMCOModel
from force_bdss.mco.i_multi_criteria_optimizer_bundle import \
IMultiCriteriaOptimizerBundle
try:
import mock
except ImportError:
from unittest import mock
from force_bdss.bdss_application import BDSSApplication
class DummyMCOCommunicator(BaseMCOCommunicator):
def receive_from_mco(self):
pass
def send_to_mco(self, kpi_results):
pass
class TestBaseMCOCommunicator(unittest.TestCase):
def test_initialization(self):
bundle = mock.Mock(spec=IMultiCriteriaOptimizerBundle)
application = mock.Mock(spec=BDSSApplication)
model = mock.Mock(spec=BaseMCOModel)
mcocomm = DummyMCOCommunicator(bundle, application, model)
self.assertEqual(mcocomm.bundle, bundle)
self.assertEqual(mcocomm.application, application)
self.assertEqual(mcocomm.model, model)
...@@ -19,7 +19,7 @@ class DummyMCO(BaseMultiCriteriaOptimizer): ...@@ -19,7 +19,7 @@ class DummyMCO(BaseMultiCriteriaOptimizer):
pass pass
class TestBaseKPICalculator(unittest.TestCase): class TestBaseMultiCriteriaOptimizer(unittest.TestCase):
def test_initialization(self): def test_initialization(self):
bundle = mock.Mock(spec=IMultiCriteriaOptimizerBundle) bundle = mock.Mock(spec=IMultiCriteriaOptimizerBundle)
application = mock.Mock(spec=BDSSApplication) application = mock.Mock(spec=BDSSApplication)
......
...@@ -16,11 +16,11 @@ class DummyMCOBundle(BaseMultiCriteriaOptimizerBundle): ...@@ -16,11 +16,11 @@ class DummyMCOBundle(BaseMultiCriteriaOptimizerBundle):
def create_model(self, model_data=None): def create_model(self, model_data=None):
pass pass
def create_communicator(self, model_data): def create_communicator(self, application, model):
pass pass
class TestBaseDataSourceBundle(unittest.TestCase): class TestBaseMCOBundle(unittest.TestCase):
def test_initialization(self): def test_initialization(self):
bundle = DummyMCOBundle() bundle = DummyMCOBundle()
self.assertEqual(bundle.id, 'foo') self.assertEqual(bundle.id, 'foo')
......
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