def _decomposer(self, event: Union[str, Tuple], suffix_tree: SuffixTree):
        """To generate suffix tree depending on the conditional event."""
        operation_and_list = ["&&", "AND"]
        operation_or_list = ["||", "OR"]

        # Check it is unit conditional event(str) or conditional event(tuple).
        if isinstance(event, str):
            self._unit_event_syntax_check(event)
            self._unit_event_message_dict[event] = []
            suffix_tree.value = event
        elif isinstance(event, tuple):
            if event[-1] in operation_and_list:
                suffix_tree.value = Operation.AND
            elif event[-1] in operation_or_list:
                suffix_tree.value = Operation.OR
            else:
                raise ConditionalEventSyntaxError(
                    "The last of the conditional event tuple must be one of ['AND', 'OR', '&&', '||]"
                )

            for slot in event[:-1]:
                node = SuffixTree()
                if isinstance(slot, tuple):
                    self._decomposer(slot, node)
                else:
                    self._unit_event_syntax_check(slot)
                    self._unit_event_message_dict[slot] = []
                    node.value = slot
                suffix_tree.nodes.append(node)
        else:
            raise ConditionalEventSyntaxError(
                "Conditional event should be string or tuple.")
Exemple #2
0
    def _unit_event_syntax_check(self, unit_event: str):
        """
        To check unit conditional event expression.

        Rules:
            unit event must be three parts and divided by ':',
                the first part of unit event represent the message's source,
                    i.e. 'learner' or '*'
                the second part of unit event represent the message's type,
                    i.e. 'experience' or '*'
                the third part of unit event represent how much messages needed,
                    i.e. '1' or '90%'
            Do not use special symbol in the unit event, such as ',', '(', ')'.

        Args:
            unit_event (str): the description of the requisite messages.
        """
        slots = unit_event.split(":")
        if len(slots) != 3:
            raise ConditionalEventSyntaxError(
                f"The conditional event: {unit_event}, \
                                              must have three parts, and divided by ':'."
            )

        # the third part of unit conditional event must be an integer or percentage(*%)
        if slots[-1][-1] == "%":
            slots[-1] = slots[-1][:-1]

        try:
            int(slots[-1])
        except Exception as e:
            raise ConditionalEventSyntaxError(
                f"The third part of conditional event must be an integer or \
                                              percentage with % in the end. {str(e)}"
            )
    def _unit_event_syntax_check(self, unit_event: str):
        """To check unit conditional event expression.

        Args:
            unit_event (str): The description of the requisite messages.
        """
        slots = unit_event.split(":")
        if len(slots) != 3:
            raise ConditionalEventSyntaxError(
                f"The conditional event: {unit_event}, must have three parts, and divided by ':'."
            )

        # The third part of unit conditional event must be an integer or percentage(*%).
        if slots[-1][-1] == "%":
            slots[-1] = slots[-1][:-1]

        try:
            int(slots[-1])
        except Exception as e:
            raise ConditionalEventSyntaxError(
                f"The third part of conditional event must be an integer or percentage with % in the end. {str(e)}"
            )