-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcondition.py
221 lines (164 loc) · 7.54 KB
/
condition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""Condition implementation.
Classes: BaseCondition, StandardCondition, SimpleCondition
"""
from __future__ import annotations
import re
from abc import ABC, abstractmethod
from typing import Any, Callable
from arta.exceptions import ConditionExecutionError
from arta.utils import ParsingErrorStrategy, parse_dynamic_parameter
class BaseCondition(ABC):
"""Base class of a Condition object (Strategy Pattern).
Is an abstract class and can't be instantiated.
Attributes:
condition_id: Id of a condition.
description: Description of a condition.
validation_function: Validation function of a condition.
validation_function_parameters: Arguments of the validation function.
"""
# Class constants
CONDITION_DATA_LABEL: str = "Undefined condition data (not needed)"
CONDITION_ID_PATTERN: str = r"\b[A-Z_0-9]+\b"
def __init__(
self,
condition_id: str,
description: str,
validation_function: Callable | None = None,
validation_function_parameters: dict[str, Any] | None = None,
) -> None:
"""
Initialize attributes.
Args:
condition_id: Id of a condition.
description: Description of a condition.
validation_function: Validation function of a condition.
validation_function_parameters: Arguments of the validation function.
"""
self._condition_id = condition_id
self._description = description
self._validation_function = validation_function
self._validation_function_parameters = validation_function_parameters
@abstractmethod
def verify(self, input_data: dict[str, Any], parsing_error_strategy: ParsingErrorStrategy, **kwargs: Any) -> bool:
"""(Abstract)
Return True if the condition is verified.
Args:
input_data: Input data to apply rules on.
parsing_error_strategy: Error handling strategy for parameter parsing.
**kwargs: For user extra arguments.
Returns:
True if the condition is verified, otherwise False.
"""
raise NotImplementedError
def get_sanitized_id(self) -> str:
"""Return the sanitized (regex) condition id.
E.g., 'CONDITION_2' --> '\\bCONDITION_2\\b'
Returns:
A sanitized regex pattern string.
"""
return rf"\b{self._condition_id}\b"
@classmethod
def extract_condition_ids_from_expression(cls, condition_expr: str | None = None) -> set[str]:
"""Get the condition ids from a string (e.g., UPPERCASE words).
E.g., CONDITION_1 and not CONDITION_2
Warning: implementation is based on the current class constant CONDITION_SPLIT_PATTERN.
Args:
condition_expr: A boolean expression (string).
Returns:
A set of extracted condition ids.
"""
cond_ids: set[str] = set()
if condition_expr is not None:
cond_ids = set(re.findall(cls.CONDITION_ID_PATTERN, condition_expr))
return cond_ids
class StandardCondition(BaseCondition):
"""Class implementing a built-in condition, named standard condition.
Attributes:
condition_id: Id of a condition.
description: Description of a condition.
validation_function: Validation function of a condition.
validation_function_parameters: Arguments of the validation function.
"""
# Class constants
CONDITION_DATA_LABEL: str = "Standard condition (will be overwritten)"
def verify(self, input_data: dict[str, Any], parsing_error_strategy: ParsingErrorStrategy, **kwargs: Any) -> bool:
"""Return True if the condition is verified.
Example of a unitary standard condition: CONDITION_1
Args:
input_data: Request or input data to apply rules on.
parsing_error_strategy: Error handling strategy for parameter parsing.
**kwargs: For user extra arguments.
Returns:
True if the condition is verified, otherwise False.
Raises:
AttributeError: Check the validation function or its parameters.
"""
if self._validation_function is None:
raise AttributeError("Validation function should not be None")
if self._validation_function_parameters is None:
raise AttributeError("Validation function parameters should not be None")
# Parse dynamic parameters
parameters: dict[str, Any] = {}
for key, value in self._validation_function_parameters.items():
parameters[key] = parse_dynamic_parameter(
parameter=value, input_data=input_data, parsing_error_strategy=parsing_error_strategy
)
# Run validation_function
return self._validation_function(**parameters)
class SimpleCondition(BaseCondition):
"""Class implementing a built-in simple condition.
Attributes:
condition_id: Id of a condition.
description: Description of a condition.
validation_function: Validation function of a condition.
validation_function_parameters: Arguments of the validation function.
"""
# Class constants
CONDITION_DATA_LABEL: str = "Simple condition data (not needed)"
CONDITION_ID_PATTERN: str = r"(?:input\.|output\.)(?:[a-zA-Z0-9!=<>\"NTF\.\*\+\-_/]*)(?:[a-zA-Z\s\-_]*\"|)"
def verify(self, input_data: dict[str, Any], parsing_error_strategy: ParsingErrorStrategy, **kwargs: Any) -> bool:
"""Return True if the condition is verified.
Example of a unitary simple condition to be verified: 'input.age>=100'
Args:
input_data: Request or input data to apply rules on.
parsing_error_strategy: Error handling strategy for parameter parsing.
**kwargs: For user extra arguments.
Returns:
True if the condition is verified, otherwise False.
Raises:
AttributeError: Check the validation function or its parameters.
"""
bool_var: bool = False
unitary_expr: str = self._condition_id
data_path_patt: str = r"(?:input\.|output\.)(?:[a-z_\.]*)"
# Retrieve only the data path
path_matches: list[str] = re.findall(data_path_patt, unitary_expr)
if len(path_matches) > 0:
# Regular case: we have a data paths
for idx, path in enumerate(path_matches):
# Read data from the path
locals()[f"data_{idx}"] = parse_dynamic_parameter( # noqa
parameter=path, input_data=input_data, parsing_error_strategy=parsing_error_strategy
)
# Replace with the variable name in the expression
unitary_expr = unitary_expr.replace(path, f"data_{idx}")
# Evaluate the expression
try:
bool_var = eval(unitary_expr) # noqa
except TypeError:
# Ignore evaluation --> False
pass
elif parsing_error_strategy == ParsingErrorStrategy.RAISE:
# Raise an error because of no match for a data path
raise ConditionExecutionError(f"Error when verifying simple condition: '{unitary_expr}'")
else:
# Other case: ignore, default value => return False
pass
return bool_var
def get_sanitized_id(self) -> str:
"""Return the sanitized (regex) condition id.
E.g., 'input.power=="fly"' --> 'input\\.power==\\"fly\\"'
Returns:
A sanitized regex pattern string.
"""
return re.escape(self._condition_id)