Skip to content
Snippets Groups Projects
Commit 931fca7e authored by Jan David Mol's avatar Jan David Mol
Browse files

Task #10695: Work with parameterset objects instead of dicts

parent aa3dac8c
No related branches found
No related tags found
No related merge requests found
...@@ -26,7 +26,7 @@ posts them on the bus. ...@@ -26,7 +26,7 @@ posts them on the bus.
""" """
from lofar.messaging import FromBus, ToBus, EventMessage # RPC, from lofar.messaging import FromBus, ToBus, EventMessage # RPC,
from lofar.parameterset import PyParameterValue from lofar.parameterset import parameterset
from lofar.sas.otdb.OTDBBusListener import OTDBBusListener from lofar.sas.otdb.OTDBBusListener import OTDBBusListener
from lofar.common.util import waitForInterrupt from lofar.common.util import waitForInterrupt
from lofar.sas.otdb.otdbrpc import OTDBRPC from lofar.sas.otdb.otdbrpc import OTDBRPC
...@@ -78,37 +78,31 @@ OTDB_to_RADB_subtype_translation_table = { ...@@ -78,37 +78,31 @@ OTDB_to_RADB_subtype_translation_table = {
}, },
} }
def resourceIndicatorsFromParset( parsetDict ): def resourceIndicatorsFromParset( parset ):
""" Extract the parset keys that are required for resource assignment. """ """ Extract the parset keys that are required for resource assignment. """
subset = {} subset = {}
def get(key, default=None): def add(key, conversion=parameterset.getString, prefix=PARSET_PREFIX):
""" Return the value of parset key `key', or `default' if the key
is not defined. """
return parsetDict.get(PARSET_PREFIX + key, default)
def add(key, conversion=lambda x: x):
""" Add the given key to our subset selection, using an optional """ Add the given key to our subset selection, using an optional
conversion. """ conversion. """
value = get(key) if parset.isDefined(prefix + key):
if value is not None: subset[key] = conversion(parset, prefix + key)
subset[key] = conversion(value)
""" Some conversion functions for common parameter-value types.""" """ Some conversion functions for common parameter-value types."""
def strvector(value): def strvector(parset, key):
return PyParameterValue(str(value), True).getStringVector() return parset.getStringVector(key, True)
def intvector(value): def intvector(parset, key):
return PyParameterValue(str(value), True)._expand().getIntVector() return parset.getIntVector(key, True)
def bool(value): def bool(parset, key):
return PyParameterValue(str(value), True).getBool() return parset.getBool(key)
# ===================================== # =====================================
# Parset meta info # Parset meta info
# ===================================== # =====================================
subset["Version.number"] = parsetDict.get("Version.number") add("Version.number", prefix="")
# ===================================== # =====================================
# Observation settings # Observation settings
...@@ -123,7 +117,7 @@ def resourceIndicatorsFromParset( parsetDict ): ...@@ -123,7 +117,7 @@ def resourceIndicatorsFromParset( parsetDict ):
add("Observation.Scheduler.taskDuration") add("Observation.Scheduler.taskDuration")
add("Observation.nrBeams") add("Observation.nrBeams")
nrSAPs = int(get("Observation.nrBeams", 0)) nrSAPs = parset.getInt(PARSET_PREFIX + "Observation.nrBeams", 0)
for sap in xrange(0, nrSAPs): for sap in xrange(0, nrSAPs):
add("Observation.Beam[%d].subbandList" % (sap,), intvector) add("Observation.Beam[%d].subbandList" % (sap,), intvector)
...@@ -163,7 +157,7 @@ def resourceIndicatorsFromParset( parsetDict ): ...@@ -163,7 +157,7 @@ def resourceIndicatorsFromParset( parsetDict ):
add("Observation.Beam[%d].nrTabRings" % (sap,)) add("Observation.Beam[%d].nrTabRings" % (sap,))
add("Observation.Beam[%d].nrTiedArrayBeams" % (sap,)) add("Observation.Beam[%d].nrTiedArrayBeams" % (sap,))
nrTABs = int(get("Observation.Beam[%d].nrTiedArrayBeams" % (sap,), 0)) nrTABs = parset.getInt(PARSET_PREFIX + "Observation.Beam[%d].nrTiedArrayBeams" % (sap,), 0)
for tab in xrange(0, nrTABs): for tab in xrange(0, nrTABs):
add("Observation.Beam[%d].TiedArrayBeam[%d].coherent" % (sap,tab), bool) add("Observation.Beam[%d].TiedArrayBeam[%d].coherent" % (sap,tab), bool)
...@@ -238,26 +232,25 @@ class RATaskSpecified(OTDBBusListener): ...@@ -238,26 +232,25 @@ class RATaskSpecified(OTDBBusListener):
def get_predecessors(parset): def get_predecessors(parset):
""" Extract the list of predecessor obs IDs from the given parset. """ """ Extract the list of predecessor obs IDs from the given parset. """
key = PARSET_PREFIX + "Observation.Scheduler.predecessors" predecessors = parset.getStringVector(PARSET_PREFIX + "Observation.Scheduler.predecessors", True)
stringlist = PyParameterValue(str(parset[key]), True).getStringVector()
# Key contains values starting with 'S' = Scheduler, 'L'/'T' = OTDB, 'M' = MoM # Key contains values starting with 'S' = Scheduler, 'L'/'T' = OTDB, 'M' = MoM
# 'S' we can probably ignore? Might be only internal in the Scheduler? # 'S' we can probably ignore? Might be only internal in the Scheduler?
result = [] result = []
for s in stringlist: for p in predecessors:
try: # Made the source a string for readability, but it's not efficient try: # Made the source a string for readability, but it's not efficient
if s.startswith('M'): if p.startswith('M'):
result.append({'source': 'mom', 'id': int(s[1:])}) result.append({'source': 'mom', 'id': int(p[1:])})
elif s.startswith('L') or s.startswith('T'): elif p.startswith('L') or s.startswith('T'):
result.append({'source': 'otdb', 'id': int(s[1:])}) result.append({'source': 'otdb', 'id': int(p[1:])})
else: # 'S' else: # 'S'
logger.info("found a predecessor ID I can't handle: %s" % s) logger.info("found a predecessor ID I can't handle: %s", p)
result.append({'source': 'other', 'id': int(s[1:])}) result.append({'source': 'other', 'id': int(p[1:])})
except ValueError: except ValueError:
logger.warning("found a predecessor ID that I can't parse %s" % s) logger.warning("found a predecessor ID that I can't parse %s", p)
return result return result
def get_specification_with_predecessors(self, id, id_source, state, found_parsets={}): def get_specification_with_predecessors(self, id, id_source, state, found_parsets):
logger.info("Processing ID %s from %s" % (id, id_source)) logger.info("Processing ID %s from %s" % (id, id_source))
if id_source == "other": if id_source == "other":
return None return None
...@@ -278,14 +271,16 @@ class RATaskSpecified(OTDBBusListener): ...@@ -278,14 +271,16 @@ class RATaskSpecified(OTDBBusListener):
if otdb_id in found_parsets: if otdb_id in found_parsets:
parset = found_parsets[otdb_id] parset = found_parsets[otdb_id]
else: else:
parset = self.otdbrpc.taskGetSpecification( otdb_id=otdb_id )['specification'] # obtain parset & convert dict to parameterset object
parset = parameterset(self.otdbrpc.taskGetSpecification( otdb_id=otdb_id )['specification'])
# add it to our cache
found_parsets[otdb_id] = parset found_parsets[otdb_id] = parset
logger.info("parset [%s]: %s" % (otdb_id, parset)) logger.info("parset [%s]: %s" % (otdb_id, parset))
# parse & marshall task type information # parse & marshall task type information
otdb_task_type = parset[PARSET_PREFIX + "Observation.processType"] otdb_task_type = parset[PARSET_PREFIX + "Observation.processType"].getString()
otdb_task_subtype = parset[PARSET_PREFIX + "Observation.processSubType"] otdb_task_subtype = parset[PARSET_PREFIX + "Observation.processSubtype"].getString()
result['task_type'] = OTDB_to_RADB_type_translation_table[otdb_task_type] result['task_type'] = OTDB_to_RADB_type_translation_table[otdb_task_type]
result['task_subtype'] = OTDB_to_RADB_subtype_translation_table[otdb_task_type][otdb_task_subtype] result['task_subtype'] = OTDB_to_RADB_subtype_translation_table[otdb_task_type][otdb_task_subtype]
...@@ -309,7 +304,7 @@ class RATaskSpecified(OTDBBusListener): ...@@ -309,7 +304,7 @@ class RATaskSpecified(OTDBBusListener):
def createAndSendSpecifiedTask(self, main_id, status): def createAndSendSpecifiedTask(self, main_id, status):
# Construct root node of tree # Construct root node of tree
resultTree = self.get_specification_with_predecessors(main_id, "otdb", status) resultTree = self.get_specification_with_predecessors(main_id, "otdb", status, {})
logger.info("Sending result: %s" % resultTree) logger.info("Sending result: %s" % resultTree)
# Put result on bus # Put result on bus
......
...@@ -35,17 +35,17 @@ class TestGetPredecessors(unittest.TestCase): ...@@ -35,17 +35,17 @@ class TestGetPredecessors(unittest.TestCase):
""" Class containing tests to verify whether the uut properly obtains the predecessors from a parset """ """ Class containing tests to verify whether the uut properly obtains the predecessors from a parset """
def test_0_predecessors(self): def test_0_predecessors(self):
""" Verify proper functioning in case of no predecessors """ """ Verify proper functioning in case of no predecessors """
parset = { PARSET_PREFIX + "Observation.Scheduler.predecessors": "[]" } parset = parameterset({ PARSET_PREFIX + "Observation.Scheduler.predecessors": "[]" })
self.assertEqual(RATaskSpecified.get_predecessors(parset), []) self.assertEqual(RATaskSpecified.get_predecessors(parset), [])
def test_1_predecessor(self): def test_1_predecessor(self):
""" Verify proper functioning in case of a single predecessor """ """ Verify proper functioning in case of a single predecessor """
parset = { PARSET_PREFIX + "Observation.Scheduler.predecessors": "[L426528]" } parset = parameterset({ PARSET_PREFIX + "Observation.Scheduler.predecessors": "[L426528]" })
self.assertEqual(RATaskSpecified.get_predecessors(parset), [{'id': 426528, 'source': 'otdb'}]) self.assertEqual(RATaskSpecified.get_predecessors(parset), [{'id': 426528, 'source': 'otdb'}])
def test_2_predecessors(self): def test_2_predecessors(self):
""" Verify proper functioning in case of a two predecessors """ """ Verify proper functioning in case of a two predecessors """
parset = { PARSET_PREFIX + "Observation.Scheduler.predecessors": "[L426528,L1]" } parset = parameterset({ PARSET_PREFIX + "Observation.Scheduler.predecessors": "[L426528,L1]" })
self.assertEqual(sorted(RATaskSpecified.get_predecessors(parset)), self.assertEqual(sorted(RATaskSpecified.get_predecessors(parset)),
[{'id': 1, 'source': 'otdb'},{'id': 426528, 'source': 'otdb'}]) [{'id': 1, 'source': 'otdb'},{'id': 426528, 'source': 'otdb'}])
...@@ -281,7 +281,7 @@ class TestRASubset(unittest.TestCase): ...@@ -281,7 +281,7 @@ class TestRASubset(unittest.TestCase):
(stringified); and, the actual RA parset subset as output by the uut. (stringified); and, the actual RA parset subset as output by the uut.
""" """
input_parset, expected_keys = self.get_test_inputs(input_parset_file) input_parset, expected_keys = self.get_test_inputs(input_parset_file)
ra_subset = resourceIndicatorsFromParset(input_parset) ra_subset = resourceIndicatorsFromParset(parameterset(input_parset))
actual_keys = sorted(ra_subset.keys()) actual_keys = sorted(ra_subset.keys())
actual_ra_subset_str = self.get_ra_subset_as_string(ra_subset) actual_ra_subset_str = self.get_ra_subset_as_string(ra_subset)
...@@ -498,6 +498,8 @@ class TestService(unittest.TestCase): ...@@ -498,6 +498,8 @@ class TestService(unittest.TestCase):
return { "TaskSpecification": { return { "TaskSpecification": {
"Version.number": "1", "Version.number": "1",
PARSET_PREFIX + "Observation.processType": "Observation",
PARSET_PREFIX + "Observation.processSubtype": "Beam Observation",
PARSET_PREFIX + "Observation.ObsID": str(OtdbID), PARSET_PREFIX + "Observation.ObsID": str(OtdbID),
PARSET_PREFIX + "Observation.Scheduler.predecessors": self.fully_qualified_otdb_id_as_string(predecessors), PARSET_PREFIX + "Observation.Scheduler.predecessors": self.fully_qualified_otdb_id_as_string(predecessors),
} } } }
...@@ -614,4 +616,4 @@ class TestService(unittest.TestCase): ...@@ -614,4 +616,4 @@ class TestService(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
\ No newline at end of file
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