Skip to content

parser_step_function_rule

Module for ParserStepFunctionRule class !!! classes ParserStepFunctionRule

ParserStepFunctionRule (IParserRuleBase)

Class for creating a StepFunction

Source code in parsers/parser_step_function_rule.py
class ParserStepFunctionRule(IParserRuleBase):
    """Class for creating a StepFunction"""

    @property
    def rule_type_name(self) -> str:
        """Type name for the rule"""
        return "step_function_rule"

    def parse_dict(self, dictionary: dict[str, Any], logger: ILogger) -> IRuleData:
        """Parses the provided dictionary to a rule
        Args:
            dictionary (dict[str, Any]): Dictionary holding the values
                                         for making the rule
        Returns:
            RuleBase: Rule based on the provided data
        """
        name: str = get_dict_element("name", dictionary)
        input_variable_name: str = get_dict_element("input_variable", dictionary)
        limit_response_table_list = get_dict_element("limit_response_table", dictionary)
        limit_response_table = convert_table_element(limit_response_table_list)
        limits = limit_response_table["limit"]
        responses = limit_response_table["response"]

        output_variable_name: str = get_dict_element("output_variable", dictionary)
        description: str = get_dict_element("description", dictionary, False)

        if not all(a < b for a, b in zip(limits, limits[1:])):
            logger.log_warning(
                "Limits were not ordered. They have been sorted increasingly,"
                " and their respective responses accordingly too."
            )
            unsorted_map = list(zip(limits, responses))
            sorted_map = sorted(unsorted_map, key=lambda x: x[0])
            limits, responses = map(list, zip(*sorted_map))

        rule_data = StepFunctionRuleData(name, limits, responses, input_variable_name)

        rule_data.output_variable = output_variable_name
        rule_data.description = description

        return rule_data

    def _are_sorted(self, list_numbers: List[float]):
        return all(a < b for a, b in zip(list_numbers, list_numbers[1:]))

rule_type_name: str property readonly

Type name for the rule

parse_dict(self, dictionary, logger)

Parses the provided dictionary to a rule

Parameters:

Name Type Description Default
dictionary dict[str, Any]

Dictionary holding the values for making the rule

required

Returns:

Type Description
RuleBase

Rule based on the provided data

Source code in parsers/parser_step_function_rule.py
def parse_dict(self, dictionary: dict[str, Any], logger: ILogger) -> IRuleData:
    """Parses the provided dictionary to a rule
    Args:
        dictionary (dict[str, Any]): Dictionary holding the values
                                     for making the rule
    Returns:
        RuleBase: Rule based on the provided data
    """
    name: str = get_dict_element("name", dictionary)
    input_variable_name: str = get_dict_element("input_variable", dictionary)
    limit_response_table_list = get_dict_element("limit_response_table", dictionary)
    limit_response_table = convert_table_element(limit_response_table_list)
    limits = limit_response_table["limit"]
    responses = limit_response_table["response"]

    output_variable_name: str = get_dict_element("output_variable", dictionary)
    description: str = get_dict_element("description", dictionary, False)

    if not all(a < b for a, b in zip(limits, limits[1:])):
        logger.log_warning(
            "Limits were not ordered. They have been sorted increasingly,"
            " and their respective responses accordingly too."
        )
        unsorted_map = list(zip(limits, responses))
        sorted_map = sorted(unsorted_map, key=lambda x: x[0])
        limits, responses = map(list, zip(*sorted_map))

    rule_data = StepFunctionRuleData(name, limits, responses, input_variable_name)

    rule_data.output_variable = output_variable_name
    rule_data.description = description

    return rule_data