Source code for tag_extractor
#!/usr/bin/python3
import re
from tag import Tag
[docs]
class ExtractRunParametersTagsOperation():
r"""
Extract the run parameters of from a database and create a `Tag` object for each of them
"""
[docs]
@staticmethod
def add_raw_tags_for_row(row, key_column, value_column, tags):
r"""
"""
key = row[key_column]
value = row[value_column]
tags.append(Tag({key: value}))
[docs]
@staticmethod
def add_regex_matches_for_row(row, key_column, value_column, regex_map, tags):
r"""
"""
key = row[key_column]
value = row[value_column]
for tag_key in regex_map:
for pair in regex_map[tag_key]:
regex = pair['regex']
transform = pair['transform']
match = re.search(regex, key)
if match:
transformed_value = transform(value)
tags.append(Tag({tag_key: transformed_value}))
[docs]
@staticmethod
def add_tags(data, key_column, value_column, regex_map, tags):
r"""
Create run-specific parameters & attributes
"""
def add_raw(row):
ExtractRunParametersTagsOperation.add_raw_tags_for_row(row, key_column, value_column, tags)
data.apply(add_raw, axis=1)
def add_matches(row):
ExtractRunParametersTagsOperation.add_regex_matches_for_row(row, key_column, value_column, regex_map, tags)
data.apply(add_matches, axis=1)
[docs]
@staticmethod
def add_parameters(data, tags, parameters_regex_map, parameter_key_name:str='paramKey', parameter_value_name:str='paramValue'):
ExtractRunParametersTagsOperation.add_tags(data, parameter_key_name, parameter_value_name, parameters_regex_map, tags)
[docs]
@staticmethod
def add_iterationvars(data, tags, iterationvars_regex_map):
iterationvars_value = data[data['attrName'] == 'iterationvars']['attrValue'].iat[0]
for tag_key in iterationvars_regex_map:
for pair in iterationvars_regex_map[tag_key]:
regex = pair['regex']
transform = pair['transform']
match = re.search(regex, iterationvars_value)
if match:
value = match.group(0)
transformed_value = transform(value)
tags.append(Tag({tag_key: transformed_value}))
[docs]
@staticmethod
def add_attributes(data, tags, attributes_regex_map, iterationvars_regex_map):
ExtractRunParametersTagsOperation.add_tags(data, 'attrName', 'attrValue', attributes_regex_map, tags)
# process the `iterationsvars` attribute
ExtractRunParametersTagsOperation.add_iterationvars(data, tags, iterationvars_regex_map)
[docs]
@staticmethod
def extract_attributes_and_params(parameter_extractor, attribute_extractor
, parameters_regex_map, attributes_regex_map, iterationvars_regex_map
, isOmnetv6:bool=False
):
r"""
Parameters
----------
parameter_extractor : Callable
A function that extracts the contents of the `runParam` table and returns it as a pandas.DataFrame
attribute_extractor : Callable
A function that extracts the contents of the `runAttr` table and returns it as a pandas.DataFrame
attributes_regex_map : dict
The dictionary containing the definitions for the tags to extract from the `runAttr` table
iterationvars_regex_map : dict
The dictionary containing the definitions for the tags to extract from the `iterationvars` attribute
parameters_regex_map : dict
The dictionary containing the definitions for the tags to extract from the `runParam` table
"""
tags:list[Tag] = []
parameters_data = parameter_extractor()
if not isOmnetv6:
ExtractRunParametersTagsOperation.add_parameters(parameters_data, tags, parameters_regex_map)
else:
ExtractRunParametersTagsOperation.add_parameters(parameters_data, tags, parameters_regex_map
, parameter_key_name='configKey', parameter_value_name='configValue'
)
attributes_data = attribute_extractor()
ExtractRunParametersTagsOperation.add_attributes(attributes_data, tags, attributes_regex_map, iterationvars_regex_map)
return tags