コード例 #1
0
    def __init__(self, pattern_structure: PatternStructure, pattern_matching_condition: Condition,
                 time_window: timedelta, consumption_policy: ConsumptionPolicy = None, pattern_id: int = None):
        self.id = pattern_id

        self.full_structure = pattern_structure
        self.positive_structure = pattern_structure.duplicate()
        self.negative_structure = self.__extract_negative_structure()

        self.condition = pattern_matching_condition
        if isinstance(self.condition, TrueCondition):
            self.condition = AndCondition()
        elif not isinstance(self.condition, CompositeCondition):
            self.condition = AndCondition(self.condition)

        self.window = time_window

        self.statistics_type = StatisticsTypes.NO_STATISTICS
        self.statistics = None
        self.consumption_policy = consumption_policy

        if consumption_policy is not None:
            if consumption_policy.single_event_strategy is not None and consumption_policy.single_types is None:
                # must be initialized to contain all event types in the pattern
                consumption_policy.single_types = self.get_all_event_types()
            if consumption_policy.contiguous_names is not None:
                self.__init_strict_conditions(pattern_structure)
コード例 #2
0
ファイル: Pattern.py プロジェクト: salehbahoty/OpenCEP
 def __init_strict_conditions(self, pattern_structure: PatternStructure):
     """
     Augment the pattern with the contiguity constraints specified as a part of the consumption policy.
     """
     if not isinstance(pattern_structure, CompositeStructure):
         return
     args = pattern_structure.args
     for i in range(len(args)):
         self.__init_strict_conditions(args[i])
     if pattern_structure.get_top_operator() != SeqOperator:
         return
     for contiguous_sequence in self.consumption_policy.contiguous_names:
         for i in range(len(contiguous_sequence) - 1):
             for j in range(len(args) - 1):
                 if not isinstance(args[i], PrimitiveEventStructure) or \
                         not isinstance(args[i + 1], PrimitiveEventStructure):
                     continue
                 if contiguous_sequence[i] != args[j].name:
                     continue
                 if contiguous_sequence[i + 1] != args[j + 1].name:
                     raise Exception(
                         "Contiguity constraints contradict the pattern structure: "
                         + "%s must follow %s" %
                         (contiguous_sequence[i],
                          contiguous_sequence[i + 1]))
                 self.__add_contiguity_condition(args[i].name,
                                                 args[i + 1].name)
コード例 #3
0
ファイル: Pattern.py プロジェクト: evaseb/OpenCEP
    def __init__(self,
                 pattern_structure: PatternStructure,
                 pattern_matching_condition: Formula = None,
                 time_window: timedelta = timedelta.max):

        self.window = time_window
        self.statistics_type = StatisticsTypes.NO_STATISTICS
        self.statistics = None
        self.condition = pattern_matching_condition
        """
        origin_structure is the pattern structure that we get in params - containing all the fields.
        structure is the origin structure without the negative events.
        negative_event contains only the negative events of the pattern.
        """
        self.origin_structure = pattern_structure
        self.structure = pattern_structure.create_top_operator()
        self.negative_event = pattern_structure.create_top_operator()

        self.split_structures()
コード例 #4
0
 def __create_pattern_for_new_structure(structure: PatternStructure,
                                        pattern: Pattern):
     """
     Creates a new pattern for the given transformed structure based on the given pattern.
     """
     if structure == pattern.full_structure:
         return pattern
     condition_for_new_pattern = pattern.condition.get_condition_of(
         structure.get_all_event_names())
     return Pattern(structure, condition_for_new_pattern, pattern.window,
                    pattern.consumption_policy, None, pattern.confidence,
                    pattern.statistics)
コード例 #5
0
 def __create_internal_node_by_operator(operator: PatternStructure, sliding_window: timedelta, parent: Node = None):
     """
     Creates an internal node representing a given operator.
     Note that negation node types are intentionally not supported here since the negative part of a pattern is
     added in a separate construction stage.
     """
     operator_type = operator.get_top_operator()
     if operator_type == SeqOperator:
         return SeqNode(sliding_window, parent)
     if operator_type == AndOperator:
         return AndNode(sliding_window, parent)
     if operator_type == KleeneClosureOperator:
         return KleeneClosureNode(sliding_window, operator.min_size, operator.max_size, parent)
     raise Exception("Unknown or unsupported operator %s" % (operator_type,))
コード例 #6
0
 def __extract_flat_sequences_aux(self, pattern_structure: PatternStructure) -> List[List[str]] or None:
     """
     An auxiliary method for extracting flat sequences from the pattern.
     """
     if isinstance(pattern_structure, PrimitiveEventStructure):
         return None
     if pattern_structure.get_top_operator() == SeqOperator:
         # note the double brackets - we return a list composed of a single list representing this sequence
         return [[arg.name for arg in pattern_structure.args if isinstance(arg, PrimitiveEventStructure)]]
     # the pattern is a composite pattern but not a flat sequence
     result = []
     for arg in pattern_structure.args:
         nested_sequences = self.__extract_flat_sequences_aux(arg)
         if nested_sequences is not None:
             result.extend(nested_sequences)
     return result
コード例 #7
0
 def __chop_matrix(pattern: Pattern, arg: PatternStructure,
                   selectivity_matrix: List[List[float]]):
     """
     Chop the matrix and returns only the rows and columns that are relevant to this arg (assuming this arg is in
     pattern's operators), based on the nested events' name in this arg.
     """
     if pattern.count_primitive_events(
             positive_only=True) != len(selectivity_matrix):
         raise Exception("size mismatch")
     event_names = [
         name for name in pattern.positive_structure.get_all_event_names()
     ]
     primitive_sons = [name for name in arg.get_all_event_names()]
     chop_start = event_names.index(primitive_sons[0])
     chop_end = event_names.index(primitive_sons[-1])
     return TreePlanBuilder.__chop_matrix_aux(selectivity_matrix,
                                              chop_start, chop_end)
コード例 #8
0
ファイル: Pattern.py プロジェクト: salehbahoty/OpenCEP
    def __init__(self,
                 pattern_structure: PatternStructure,
                 pattern_matching_condition: Condition,
                 time_window: timedelta,
                 consumption_policy: ConsumptionPolicy = None,
                 pattern_id: int = None,
                 confidence: float = None,
                 statistics: Dict = None):
        if confidence is not None and (confidence < 0.0 or confidence > 1.0):
            raise Exception("Invalid value for pattern confidence:%s" %
                            (confidence, ))
        self.id = pattern_id

        self.full_structure = pattern_structure
        self.positive_structure = pattern_structure.duplicate()
        self.negative_structure = self.__extract_negative_structure()

        self.condition = pattern_matching_condition
        if isinstance(self.condition, TrueCondition):
            self.condition = AndCondition()
        elif not isinstance(self.condition, CompositeCondition):
            self.condition = AndCondition(self.condition)

        self.window = time_window

        self.statistics = statistics

        self.consumption_policy = consumption_policy
        if consumption_policy is not None:
            if consumption_policy.single_event_strategy is not None and consumption_policy.single_types is None:
                # must be initialized to contain all event types in the pattern
                consumption_policy.single_types = self.get_all_event_types()
            if consumption_policy.contiguous_names is not None:
                self.__init_strict_conditions(pattern_structure)

        self.confidence = confidence