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

Introduced slots

parent d2736ebd
No related branches found
No related tags found
1 merge request!69Introduce slots and resolution of named variables
Showing with 98 additions and 4 deletions
from traits.api import HasStrictTraits, String
class Slot(HasStrictTraits):
"""Describes an input or output slot in the DataSource or
KPICalculator"""
#: A textual description of the slot
description = String("No description")
#: The CUBA key of the slot
type = String()
import unittest
from force_bdss.core.slot import Slot
class TestSlot(unittest.TestCase):
def test_initialization(self):
slot = Slot()
self.assertEqual(slot.type, "")
self.assertEqual(slot.description, "No description")
import csv
from force_bdss.api import BaseDataSource, DataValue
from force_bdss.core.slot import Slot
class CSVExtractorDataSource(BaseDataSource):
......@@ -20,3 +21,11 @@ class CSVExtractorDataSource(BaseDataSource):
break
raise IndexError("Could not find specified data.")
def slots(self, model):
return (
(),
(
Slot(type=model.cuba_type),
)
)
......@@ -4,3 +4,6 @@ from force_bdss.api import BaseDataSource
class DummyDataSource(BaseDataSource):
def run(self, model, parameters):
return parameters
def slots(self, model):
return (), ()
......@@ -4,3 +4,6 @@ from force_bdss.api import BaseKPICalculator
class DummyKPICalculator(BaseKPICalculator):
def run(self, model, data_source_results):
return data_source_results
def slots(self, model):
return (), ()
from force_bdss.api import BaseKPICalculator, DataValue
from force_bdss.core.slot import Slot
class KPIAdderCalculator(BaseKPICalculator):
......@@ -16,3 +17,13 @@ class KPIAdderCalculator(BaseKPICalculator):
type=model.cuba_type_out,
value=sum
)]
def slots(self, model):
return (
(
Slot(type=model.cuba_type_in),
),
(
Slot(type=model.cuba_type_out),
)
)
......@@ -55,8 +55,14 @@ class BaseDataSource(ABCHasStrictTraits):
Returns
-------
list[tuple, tuple]
A list containing two tuples, the first element is the input slots,
the second element is the output slots. Each slot must be an
instance of the Slot class.
(input_slots, output_slots): tuple[tuple, tuple]
A tuple containing two tuples.
The first element is the input slots, the second element is
the output slots. Each slot must be an instance of the Slot class.
It is possible for each of the two inside tuples to be empty.
The case of an empty input slot is common: the DataSource does
not need any information from the MCO to operate.
The case of an empty output slot is uncommon, but supported:
the DataSource does not produce any output and is therefore
useless.
"""
......@@ -13,6 +13,9 @@ class DummyDataSource(BaseDataSource):
def run(self, *args, **kwargs):
pass
def slots(self, model):
return (), ()
class TestBaseDataSource(unittest.TestCase):
def test_initialization(self):
......
......@@ -38,3 +38,32 @@ class BaseKPICalculator(ABCHasStrictTraits):
List[DataValue]:
The result of this KPI evaluation, as a list of DataValues.
"""
@abc.abstractmethod
def slots(self, model):
"""Returns the input (and output) slots of the KPI Calculator.
Slots are the entities that are needed (and produced) by this
KPI Calculator.
The slots may depend on the configuration options, and thus the model.
This allows, for example, to change the slots depending if an option
is enabled or not.
Parameters
----------
model: BaseKPICalculatorModel
The model of the KPICalculator, instantiated through create_model()
Returns
-------
(input_slots, output_slots): tuple[tuple, tuple]
A tuple containing two tuples.
The first element is the input slots, the second element is
the output slots. Each slot must be an instance of the Slot class.
It is possible for each of the two inside tuples to be empty.
The case of an empty input slot is common: the KPICalculator does
not need any information from the MCO to operate.
The case of an empty output slot is uncommon, but supported:
the KPICalculator does not produce any output and is therefore
useless.
"""
......@@ -12,6 +12,9 @@ class DummyKPICalculator(BaseKPICalculator):
def run(self, *args, **kwargs):
pass
def slots(self, model):
return (), ()
class TestBaseKPICalculator(unittest.TestCase):
def test_initialization(self):
......
......@@ -80,6 +80,9 @@ class NullKPICalculator(BaseKPICalculator):
def run(self, model, data_source_results):
return []
def slots(self, model):
return (), ()
class NullKPICalculatorBundle(BaseKPICalculatorBundle):
def create_model(self, model_data=None):
......@@ -97,6 +100,9 @@ class NullDataSource(BaseDataSource):
def run(self, model, parameters):
return []
def slots(self, model):
return (), ()
class NullDataSourceBundle(BaseDataSourceBundle):
def create_model(self, model_data=None):
......
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