-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathengine.py
More file actions
941 lines (805 loc) · 38.4 KB
/
engine.py
File metadata and controls
941 lines (805 loc) · 38.4 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
import functools
import operator
from collections import defaultdict
from copy import copy
from datetime import date, datetime
from typing import Any
import pandas as pd
from dateutil.relativedelta import relativedelta
from .context import PathNode, RuleContext, TypeSpec, logger
class RulesEngine:
"""Rules engine for evaluating business rules"""
def __init__(self, spec: dict[str, Any], service_provider: Any | None = None) -> None:
self.spec = spec
self.service_name = spec.get("service")
self.law = spec.get("law")
self.requirements = spec.get("requirements", [])
self.actions = spec.get("actions", [])
self.parameter_specs = spec.get("properties", {}).get("parameters", {})
self.property_specs = self._build_property_specs(spec.get("properties", {}))
self.output_specs = self._build_output_specs(spec.get("properties", {}))
self.definitions = spec.get("properties", {}).get("definitions", {})
self.service_provider = service_provider
@staticmethod
def _build_property_specs(properties: dict[str, Any]) -> dict[str, dict[str, Any]]:
"""Build mapping of property paths to their specifications"""
specs = {}
# Add parameter properties
for param in properties.get("parameters", []):
if "name" in param:
specs[param["name"]] = param
# Add input properties
for prop in properties.get("input", []):
if "name" in prop:
specs[prop["name"]] = prop
# Add source properties
for source in properties.get("sources", []):
if "name" in source:
specs[source["name"]] = source
return specs
@staticmethod
def _build_output_specs(properties: dict[str, Any]) -> dict[str, TypeSpec]:
"""Build mapping of output names to their type specifications"""
specs = {}
for output in properties.get("output", []):
if "name" in output:
type_spec = output.get("type_spec", {})
specs[output["name"]] = TypeSpec(
type=output.get("type"),
unit=type_spec.get("unit"),
precision=type_spec.get("precision"),
min=type_spec.get("min"),
max=type_spec.get("max"),
)
return specs
def _enforce_output_type(self, name: str, value: Any) -> Any:
"""Enforce type specifications on output value"""
if name in self.output_specs:
result = self.output_specs[name].enforce(value)
if not operator.eq(value, result):
logger.debug(f"Enforcing type spec changed value from: {value} to {result}")
return result
return value
@staticmethod
def topological_sort(dependencies: dict[str, set]) -> list[str]:
"""
Perform topological sort on dependencies.
Returns outputs in order they should be calculated.
"""
# First create complete set of all nodes including leaf nodes
all_nodes = set(dependencies.keys())
for deps in dependencies.values():
all_nodes.update(deps)
# Initialize complete dependency map
complete_dependencies = {node: set() for node in all_nodes}
complete_dependencies.update(dependencies)
# Build adjacency list
graph = defaultdict(set)
for output, deps in complete_dependencies.items():
for dep in deps:
graph[dep].add(output)
# Find nodes with no dependencies
ready = [node for node, deps in complete_dependencies.items() if not deps]
sorted_outputs = []
while ready:
node = ready.pop(0)
sorted_outputs.append(node)
# Remove this node as dependency
dependents = graph[node]
for dependent in list(dependents):
complete_dependencies[dependent].remove(node)
# If no more dependencies, add to ready
if not complete_dependencies[dependent]:
ready.append(dependent)
dependents.remove(dependent)
if any(deps for deps in complete_dependencies.values()):
raise ValueError("Circular dependency detected")
return sorted_outputs
@staticmethod
def analyze_dependencies(action):
"""Find all outputs this action depends on"""
deps = set()
def traverse(obj) -> None:
if isinstance(obj, str):
if obj.startswith("$"):
value = obj[1:] # Remove $ prefix
if value.islower(): # Output reference
deps.add(value)
elif isinstance(obj, dict):
for v in obj.values():
traverse(v)
elif isinstance(obj, list):
for item in obj:
traverse(item)
traverse(action)
return deps
@staticmethod
def get_required_actions(requested_output: str, actions: list) -> list:
"""Get all actions needed to compute requested output in dependency order"""
if not requested_output:
return actions
# Build dependency graph
dependencies = {}
action_by_output = {}
for action in actions:
output = action["output"]
action_by_output[output] = action
dependencies[output] = RulesEngine.analyze_dependencies(action)
# Find all required outputs
required = set()
to_process = {requested_output}
while to_process:
output = to_process.pop()
required.add(output)
# Add dependencies to processing queue
deps = dependencies.get(output, set())
to_process.update(deps - required)
# Get execution order via topological sort
ordered_outputs = RulesEngine.topological_sort(
{output: deps for output, deps in dependencies.items() if output in required}
)
# Return actions in dependency order
return [action_by_output[output] for output in ordered_outputs if output in action_by_output]
def evaluate(
self,
parameters: dict[str, Any] | None = None,
overwrite_input: dict[str, Any] | None = None,
overwrite_definitions: dict[str, Any] | None = None,
sources: dict[str, pd.DataFrame] | None = None,
calculation_date=None,
requested_output: str | None = None,
approved: bool = False,
) -> dict[str, Any]:
"""Evaluate rules using service context and sources"""
parameters = parameters or {}
for p in self.parameter_specs:
if p["required"] and p["name"] not in parameters:
logger.warning(f"Required parameter {p} not found in {parameters}")
logger.debug(f"Evaluating rules for {self.service_name} {self.law} ({calculation_date} {requested_output})")
root = PathNode(type="root", name="evaluation", result=None)
claims = None
if "BSN" in parameters:
bsn = parameters["BSN"]
claims = self.service_provider.claim_manager.get_claim_by_bsn_service_law(
bsn, self.service_name, self.law, approved=approved
)
context = RuleContext(
definitions=self.definitions,
service_provider=self.service_provider,
parameters=parameters,
property_specs=self.property_specs,
output_specs=self.output_specs,
sources=sources,
path=[root],
overwrite_input=overwrite_input or {},
overwrite_definitions=overwrite_definitions or {},
calculation_date=calculation_date,
service_name=self.service_name,
claims=claims,
approved=approved,
)
# Proactively resolve required parameters to ensure they appear in the value_tree
# even if they're not evaluated due to short-circuit logic
for p in self.parameter_specs:
if p.get("required") and p["name"] not in parameters:
# Create a resolve node for this missing required parameter
context.resolve_value(f"${p['name']}")
# Check requirements
requirements_node = PathNode(type="requirements", name="Check all requirements", result=None)
context.add_to_path(requirements_node)
try:
requirements_met = self._evaluate_requirements(self.requirements, context)
requirements_node.result = requirements_met
finally:
context.pop_path()
output_values = {}
if requirements_met:
# Get required actions including dependencies in order
required_actions = self.get_required_actions(requested_output, self.actions)
for action in required_actions:
output_def, output_name = self._evaluate_action(action, context)
context.outputs[output_name] = output_def["value"]
output_values[output_name] = output_def
if context.missing_required:
logger.warning("Missing required values, breaking")
break
if context.missing_required:
logger.warning("Missing required values, requirements not met, setting outputs to empty.")
output_values = {}
requirements_met = False
if not output_values:
logger.warning(f"No output values computed for {calculation_date} {requested_output}")
return {
"input": context.resolved_paths,
"output": output_values,
"requirements_met": requirements_met,
"path": root,
"missing_required": context.missing_required,
}
def _evaluate_action(self, action, context):
with logger.indent_block(f"Computing {action.get('output', '')}"):
if action["output"] == "partner_bsn":
pass
action_node = PathNode(
type="action",
name=f"Evaluate action for {action.get('output', '')}",
result=None,
)
context.add_to_path(action_node)
output_name = action["output"]
# Find output specification
output_spec = next(
(spec for spec in self.spec.get("properties", {}).get("output", []) if spec.get("name") == output_name),
{},
)
if (
self.service_name in context.overwrite_input
and output_name in context.overwrite_input[self.service_name]
):
raw_result = context.overwrite_input[self.service_name][output_name]
logger.debug(f"Resolving value {self.service_name}/{output_name} from OVERWRITE {raw_result}")
elif "operation" in action:
raw_result = self._evaluate_operation(action, context)
elif "value" in action:
raw_result = self._evaluate_value(action["value"], context)
elif "subject" in action:
# Direct subject assignment (e.g., subject: "$SOURCE.field")
raw_result = self._evaluate_value(action["subject"], context)
else:
raw_result = None
result = self._enforce_output_type(output_name, raw_result)
action_node.result = result
logger.debug(f"Result of {action.get('output', '')}: {result}")
# Build output with metadata
output_def = {
"value": result,
"type": output_spec.get("type", "unknown"),
"description": output_spec.get("description", ""),
}
# Add type_spec if present
if "type_spec" in output_spec:
output_def["type_spec"] = output_spec["type_spec"]
# Add temporal if present
if "temporal" in output_spec:
output_def["temporal"] = output_spec["temporal"]
return output_def, output_name
def _evaluate_requirements(self, requirements: list, context: RuleContext) -> bool:
"""Evaluate all requirements"""
if not requirements:
logger.debug("No requirements found")
return True
for req in requirements:
with logger.indent_block(f"Requirements {req}"):
node = PathNode(
type="requirement",
name="Check ALL conditions"
if "all" in req
else "Check OR conditions"
if "or" in req
else "Test condition",
result=None,
)
context.add_to_path(node)
if "all" in req:
results = []
for r in req["all"]:
result = self._evaluate_requirements([r], context)
results.append(result)
if not bool(result):
logger.debug("False value found in an ALL, no need to compute the rest, breaking.")
break
result = all(results)
elif "or" in req:
results = []
for r in req["or"]:
result = self._evaluate_requirements([r], context)
results.append(result)
if bool(result):
logger.debug("True value found in an OR, no need to compute the rest, breaking.")
break
result = any(results)
else:
result = self._evaluate_operation(req, context)
logger.debug("Requirement met" if result else "Requirement NOT met")
node.result = result
context.pop_path()
if not result:
return False
return True
def _evaluate_if_operation(self, operation: dict[str, Any], context: RuleContext) -> Any:
"""Evaluate an IF operation"""
with logger.indent_block("Evaluating IF"):
if_node = PathNode(
type="operation",
name="IF conditions",
result=None,
details={"condition_results": []},
)
context.add_to_path(if_node)
result = 0
conditions = operation.get("conditions", [])
for i, condition in enumerate(conditions):
condition_result = {
"condition_index": i,
"type": "test" if "test" in condition else "else",
}
if "test" in condition:
test_result = self._evaluate_operation(condition["test"], context)
condition_result["test_result"] = test_result
if test_result:
result = self._evaluate_value(condition["then"], context)
if_node.details["condition_results"].append(condition_result)
logger.debug(f"THEN condition: {result}")
break
elif "else" in condition:
result = self._evaluate_value(condition["else"], context)
condition_result["else_value"] = result
if_node.details["condition_results"].append(condition_result)
logger.debug(f"ELSE condition: {result}")
break
if_node.details["condition_results"].append(condition_result)
if_node.result = result
context.pop_path()
return result
def _evaluate_foreach(self, operation, context):
"""Handle FOREACH operation"""
logger.debug("For each condition")
combine = operation.get("combine")
array_data = self._evaluate_value(operation["subject"], context)
if "DECLARED_HOURS" in operation["subject"]:
pass
if not array_data:
logger.warning("No data found to run FOREACH on")
return self._evaluate_aggregate_ops(combine, [])
if not isinstance(array_data, list):
array_data = [array_data]
with logger.indent_block(f"Foreach({combine})"):
values = []
for item in array_data:
with logger.indent_block(f"Item {item}"):
item_context = copy(context)
# Create a new local dict to avoid polluting the parent context
item_context.local = dict(context.local)
if isinstance(item, dict):
item_context.local.update(item)
# Add 'current' as an alias that always refers to the current item
item_context.local["current"] = item
# Also set current_N for nested FOREACH compatibility
for i in range(100):
if f"current_{i}" not in item_context.local:
item_context.local[f"current_{i}"] = item
break
# Check where clause if present - skip item if condition is not met
if "where" in operation:
where_result = self._evaluate_value(operation["where"], item_context)
if not where_result:
logger.debug(f"Skipping item due to where clause: {item}")
continue
value_to_evaluate = (
operation["value"][0] if isinstance(operation["value"], list) else operation["value"]
)
result = self._evaluate_value(value_to_evaluate, item_context)
context.missing_required = context.missing_required or item_context.missing_required
context.path = item_context.path
# When combine is specified, flatten results for aggregation
# When combine is not specified, keep results as separate items (supports nested arrays)
if combine:
values.extend(result if isinstance(result, list) else [result])
else:
values.append(result)
logger.debug(f"Foreach values: {values}")
result = self._evaluate_aggregate_ops(combine, values) if combine else values
logger.debug(f"Foreach result: {result}")
return result
COMPARISON_OPS = {
"EQUALS": operator.eq,
"NOT_EQUALS": operator.ne,
"GREATER_THAN": operator.gt,
"LESS_THAN": operator.lt,
"GREATER_OR_EQUAL": operator.ge,
"LESS_OR_EQUAL": operator.le,
}
AGGREGATE_OPS = {
"OR": any,
"AND": all,
"MIN": min,
"MAX": max,
"ADD": sum,
"CONCAT": lambda vals: "".join(str(x) for x in vals),
"MULTIPLY": lambda vals: functools.reduce(
lambda x, y: int(x * y) if isinstance(y, int) and y < 1 else x * y,
vals[1:],
vals[0],
),
"SUBTRACT": lambda vals: functools.reduce(operator.sub, vals[1:], vals[0]),
"DIVIDE": lambda vals: (
functools.reduce(lambda x, y: x / y if y != 0 else 0, vals[1:], float(vals[0])) if 0 not in vals[1:] else 0
),
}
@staticmethod
def _evaluate_aggregate_ops(op: str, values: list[Any]) -> int | float | bool:
"""Handle aggregate operations"""
filtered_values = [v for v in values if v is not None]
if not filtered_values:
logger.warning(f"No values found (or they where None), returning 0 for {op}({values})")
return 0
elif len(filtered_values) < len(values):
logger.warning(f"Dropped {len(values) - len(filtered_values)} values because they where None")
result = RulesEngine.AGGREGATE_OPS[op](filtered_values)
logger.debug(f"Compute {op}({filtered_values}) = {result}")
return result
@staticmethod
def _evaluate_comparison(op: str, left: Any, right: Any) -> bool | None:
"""Handle comparison operations"""
# Handle None values in comparisons
if left is None or right is None:
# For EQUALS/NOT_EQUALS, we can compare None values
if op == "EQUALS":
result = left == right
logger.debug(f"Compute {op}({left}, {right}) = {result}")
return result
elif op == "NOT_EQUALS":
result = left != right
logger.debug(f"Compute {op}({left}, {right}) = {result}")
return result
else:
# For other comparisons (GREATER_THAN, etc), None is not comparable
logger.warning(f"Cannot compute {op}({left}, {right}): one or both values are None")
return None
if isinstance(left, date) and isinstance(right, str):
right = datetime.strptime(right, "%Y-%m-%d").date()
elif isinstance(right, date) and isinstance(left, str):
left = datetime.strptime(left, "%Y-%m-%d").date()
try:
result = RulesEngine.COMPARISON_OPS[op](left, right)
logger.debug(f"Compute {op}({left}, {right}) = {result}")
except TypeError as e:
logger.warning(f"Error computing {op}({left}, {right}): {e}")
result = None
return result
@staticmethod
def _evaluate_date_operation(op: str, values: list[Any], unit: str, context: RuleContext) -> int | None:
"""Handle date-specific operations with comprehensive validation"""
def validate_and_convert_date(date_val: Any, name: str) -> datetime | None:
"""Validate and convert a date value to datetime with comprehensive error handling"""
if date_val is None:
context.missing_required = True
logger.warning(f"Missing date value for {name} in {op} operation")
return None
if isinstance(date_val, datetime):
return date_val
# Check for empty or invalid string types
if isinstance(date_val, str):
if not date_val.strip():
context.missing_required = True
logger.warning(f"Empty date string for {name} in {op} operation")
return None
elif not isinstance(date_val, str | int | float):
# Invalid type that can't be converted to string
context.missing_required = True
logger.warning(f"Invalid date type for {name}: {type(date_val)} = {date_val}")
return None
try:
return datetime.fromisoformat(str(date_val))
except (ValueError, TypeError) as e:
context.missing_required = True
logger.warning(f"Cannot parse date for {name}: {date_val} ({e})")
return None
if op == "SUBTRACT_DATE":
# Validate quantity
if len(values) != 2:
context.missing_required = True
logger.warning(f"SUBTRACT_DATE requires exactly 2 values, got {len(values)}")
return None
end_date, start_date = values
# Apply default for end_date if falsy (but not None, which is handled in validation)
if not end_date and end_date is not None:
end_date = context.calculation_date
# Validate and convert both dates
end_date_converted = validate_and_convert_date(end_date, "end_date")
start_date_converted = validate_and_convert_date(start_date, "start_date")
# Return None if either date is invalid
if end_date_converted is None or start_date_converted is None:
return None
# Calculate the difference
delta = end_date_converted - start_date_converted
# Convert to requested unit
if unit == "hours":
result = int(delta.total_seconds() // 3600)
elif unit == "days":
result = delta.days
elif unit == "years":
result = (
end_date_converted.year
- start_date_converted.year
- (
(end_date_converted.month, end_date_converted.day)
< (start_date_converted.month, start_date_converted.day)
)
)
elif unit == "months":
result = (
(end_date_converted.year - start_date_converted.year) * 12
+ end_date_converted.month
- start_date_converted.month
)
else:
logger.warning(f"Unknown date unit '{unit}', defaulting to days")
result = delta.days
logger.debug(f"Compute {op}({values}, {unit}) = {result}")
return result
if op == "ADD_DATE":
# ADD_DATE adds a duration to a date
if len(values) != 2:
context.missing_required = True
logger.warning(f"ADD_DATE requires exactly 2 values, got {len(values)}")
return None
base_date, amount = values
# Validate and convert the base date
base_date_converted = validate_and_convert_date(base_date, "base_date")
if base_date_converted is None:
return None
# Amount must be a number
try:
amount = int(amount)
except (ValueError, TypeError):
context.missing_required = True
logger.warning(f"ADD_DATE amount must be a number, got {amount}")
return None
# Calculate the new date based on unit
if unit == "days":
result_date = base_date_converted + relativedelta(days=amount)
elif unit == "years":
result_date = base_date_converted + relativedelta(years=amount)
elif unit == "months":
result_date = base_date_converted + relativedelta(months=amount)
else:
logger.warning(f"Unknown date unit '{unit}', defaulting to days")
result_date = base_date_converted + relativedelta(days=amount)
result = result_date.strftime("%Y-%m-%d")
logger.debug(f"Compute {op}({values}, {unit}) = {result}")
return result
# Handle other date operations if added in the future
logger.warning(f"Unknown date operation: {op}")
return None
def _evaluate_operation(self, operation: dict[str, Any], context: RuleContext) -> Any:
"""Evaluate an operation or condition"""
if not isinstance(operation, dict):
node = PathNode(
type="value",
name="Direct value evaluation",
result=None,
details={"raw_value": operation},
)
context.add_to_path(node)
result = self._evaluate_value(operation, context)
node.result = result
context.pop_path()
return result
# Direct value assignment - no operation needed
if "value" in operation and not operation.get("operation"):
node = PathNode(
type="direct_value",
name="Direct value assignment",
result=None,
details={"raw_value": operation["value"]},
)
context.add_to_path(node)
result = self._evaluate_value(operation["value"], context)
node.result = result
context.pop_path()
return result
op_type = operation.get("operation")
node = PathNode(
type="operation",
name=f"Operation: {op_type}",
result=None,
details={"operation_type": op_type},
)
context.add_to_path(node)
if op_type is None:
logger.warning("Operation type is None (or missing).")
result = None
elif op_type == "IF":
result = self._evaluate_if_operation(operation, context)
elif op_type == "FOREACH":
result = self._evaluate_foreach(operation, context)
node.details.update({"raw_values": operation["value"], "arithmetic_type": op_type})
elif op_type in ["IN", "NOT_IN"]:
with logger.indent_block(op_type):
subject = self._evaluate_value(operation["subject"], context)
allowed_values = self._evaluate_value(operation.get("values", []), context)
# Ensure allowed_values is a list/set for membership testing
allowed_set = allowed_values if isinstance(allowed_values, list | dict | set) else [allowed_values]
# If subject is a list, check if ANY element is in allowed_values
result = any(s in allowed_set for s in subject) if isinstance(subject, list) else subject in allowed_set
if op_type == "NOT_IN":
result = not result
node.details.update({"subject_value": subject, "allowed_values": allowed_values})
logger.debug(f"Result {subject} {op_type} {allowed_values}: {result}")
elif op_type == "NOT_NULL":
subject = self._evaluate_value(operation["subject"], context)
result = subject is not None
node.details["subject_value"] = subject
logger.debug(f"NOT_NULL result: {result}")
elif op_type == "IS_NULL":
subject = self._evaluate_value(operation["subject"], context)
result = subject is None
node.details["subject_value"] = subject
logger.debug(f"IS_NULL result: {result}")
elif op_type == "EXISTS":
subject = self._evaluate_value(operation["subject"], context)
# EXISTS returns True if subject is not None and not empty
if subject is None:
result = False
elif isinstance(subject, list | tuple | dict) or hasattr(subject, "__len__"):
result = len(subject) > 0
else:
result = bool(subject)
node.details["subject_value"] = subject
logger.debug(f"EXISTS result: {result}")
elif op_type == "LENGTH":
subject = self._evaluate_value(operation["subject"], context)
# LENGTH returns the count of items in a list/array, or 0 if None/empty
if subject is None:
result = 0
elif isinstance(subject, list | tuple | dict) or hasattr(subject, "__len__"):
result = len(subject)
else:
# For non-collection types, return 1 if truthy, 0 if falsy
result = 1 if subject else 0
node.details["subject_value"] = subject
logger.debug(f"LENGTH result: {result}")
elif op_type == "AND":
with logger.indent_block("AND"):
values = []
for v in operation["values"]:
r = self._evaluate_value(v, context)
values.append(r)
if not bool(r):
logger.debug("False value found in an AND, no need to compute the rest, breaking.")
break
result = all(bool(v) for v in values)
node.details["evaluated_values"] = values
logger.debug(f"Result {list(values)} AND: {result}")
elif op_type == "OR":
with logger.indent_block("OR"):
values = []
for v in operation["values"]:
r = self._evaluate_value(v, context)
values.append(r)
if bool(r):
logger.debug("True value found in an OR, no need to compute the other, breaking.")
break
result = any(bool(v) for v in values)
node.details["evaluated_values"] = values
logger.debug(f"Result {list(values)} OR: {result}")
elif op_type == "COALESCE":
# Returns the first non-null value from the list (lazy evaluation)
with logger.indent_block("COALESCE"):
result = None
evaluated_values = []
for v in operation["values"]:
r = self._evaluate_value(v, context)
evaluated_values.append(r)
if r is not None:
result = r
logger.debug(f"Non-null value found in COALESCE: {r}")
break
node.details["evaluated_values"] = evaluated_values
logger.debug(f"COALESCE result: {result}")
elif op_type == "COMBINE_DATETIME":
# Combine a date and time into a datetime
date_val = self._evaluate_value(operation.get("date"), context)
time_val = self._evaluate_value(operation.get("time"), context)
if date_val is None or time_val is None:
logger.warning(f"COMBINE_DATETIME: missing date ({date_val}) or time ({time_val})")
context.missing_required = True
result = None
else:
try:
# Parse date if it's a string
if isinstance(date_val, str):
date_val = (
datetime.fromisoformat(date_val).date()
if "T" in date_val
else datetime.strptime(date_val, "%Y-%m-%d").date()
)
elif isinstance(date_val, datetime):
date_val = date_val.date()
# Parse time - expect format like "20:00" or "20:00:00"
if isinstance(time_val, str):
time_parts = time_val.split(":")
hour = int(time_parts[0])
minute = int(time_parts[1]) if len(time_parts) > 1 else 0
second = int(time_parts[2]) if len(time_parts) > 2 else 0
from datetime import time as time_type
time_val = time_type(hour, minute, second)
result = datetime.combine(date_val, time_val)
logger.debug(f"COMBINE_DATETIME({date_val}, {time_val}) = {result}")
except (ValueError, TypeError) as e:
logger.warning(f"COMBINE_DATETIME failed: {e}")
result = None
node.details.update({"date": date_val, "time": time_val})
elif op_type == "DAY_OF_WEEK":
# Get day of week from a date (0=Monday, 6=Sunday)
date_val = self._evaluate_value(operation.get("subject"), context)
if date_val is None:
logger.warning("DAY_OF_WEEK: missing date value")
result = None
else:
try:
# Parse date if it's a string
if isinstance(date_val, str):
date_val = (
datetime.fromisoformat(date_val).date()
if "T" in date_val
else datetime.strptime(date_val, "%Y-%m-%d").date()
)
elif isinstance(date_val, datetime):
date_val = date_val.date()
# weekday() returns 0=Monday, 6=Sunday
result = date_val.weekday()
logger.debug(f"DAY_OF_WEEK({date_val}) = {result}")
except (ValueError, TypeError) as e:
logger.warning(f"DAY_OF_WEEK failed: {e}")
result = None
node.details.update({"date": date_val, "day_of_week": result})
elif "_DATE" in op_type:
values = [self._evaluate_value(v, context) for v in operation["values"]]
unit = operation.get("unit", "days")
result = self._evaluate_date_operation(op_type, values, unit, context)
node.details.update({"evaluated_values": values, "unit": unit})
elif op_type in self.COMPARISON_OPS:
subject = None
value = None
if "subject" in operation:
subject = self._evaluate_value(operation["subject"], context)
value = self._evaluate_value(operation["value"], context)
elif "values" in operation:
values = [self._evaluate_value(v, context) for v in operation["values"]]
subject = values[0]
value = values[1]
else:
logger.warning("Comparison operation expects two values or subject/value.")
result = self._evaluate_comparison(op_type, subject, value)
node.details.update(
{
"subject_value": subject,
"comparison_value": value,
"comparison_type": op_type,
}
)
elif op_type in self.AGGREGATE_OPS and "values" in operation:
# The operation dict has legal_basis as metadata alongside operation/values
# but we only need to evaluate the 'values' list, ignoring legal_basis metadata
values = [self._evaluate_value(v, context) for v in operation["values"]]
result = self._evaluate_aggregate_ops(op_type, values)
node.details.update(
{
"raw_values": operation["values"],
"evaluated_values": values,
"arithmetic_type": op_type,
}
)
elif op_type == "GET":
subject = self._evaluate_value(operation["subject"], context)
values = self._evaluate_value(operation.get("values", []), context)
result = values.get(subject)
node.details.update({"subject_value": subject, "allowed_values": values})
logger.debug(f"GET {subject} from {values}: {result}")
else:
result = None
node.details["error"] = "Invalid operation format"
logger.warning(f"Not matched to any operation {op_type}")
node.result = result
context.pop_path()
return result
def _evaluate_value(self, value: Any, context: RuleContext) -> Any:
"""Evaluate a value which might be a number, operation, or reference"""
if isinstance(value, int | float | bool | date | datetime) or value is None:
return value
elif isinstance(value, dict) and "operation" in value:
return self._evaluate_operation(value, context)
else:
return context.resolve_value(value)