generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.py
More file actions
75 lines (64 loc) · 2.02 KB
/
rules.py
File metadata and controls
75 lines (64 loc) · 2.02 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
"""
Symbolic Rules Engine - Constrains LLM decisions with verifiable logic
"""
from dataclasses import dataclass
from typing import Callable
@dataclass
class Rule:
name: str
condition: Callable[[dict], bool]
message: str
# Condition functions
def valid_dates_check(ctx: dict) -> bool:
return ctx.get("check_in") and ctx.get("check_out") and ctx["check_in"] < ctx["check_out"]
def max_guests_check(ctx: dict) -> bool:
return ctx.get("guests", 1) <= 10
def advance_booking_check(ctx: dict) -> bool:
return ctx.get("days_until_checkin", 0) >= 1
def payment_verified_check(ctx: dict) -> bool:
return ctx.get("payment_verified", False)
def cancellation_window_check(ctx: dict) -> bool:
return ctx.get("days_until_checkin", 0) >= 2
def booking_exists_check(ctx: dict) -> bool:
return ctx.get("booking_id") is not None
# Business rules that MUST be enforced
BOOKING_RULES = [
Rule(
name="valid_dates",
condition=valid_dates_check,
message="Check-in must be before check-out"
),
Rule(
name="max_guests",
condition=max_guests_check,
message="Maximum 10 guests per booking"
),
Rule(
name="advance_booking",
condition=advance_booking_check,
message="Must book at least 1 day in advance"
),
]
CONFIRMATION_RULES = [
Rule(
name="payment_before_confirm",
condition=payment_verified_check,
message="Payment must be verified before confirmation"
),
]
CANCELLATION_RULES = [
Rule(
name="cancellation_window",
condition=cancellation_window_check,
message="Cannot cancel within 48 hours of check-in"
),
Rule(
name="booking_exists",
condition=booking_exists_check,
message="No booking found to cancel"
),
]
def validate(rules: list[Rule], context: dict) -> tuple[bool, list[str]]:
"""Run all rules, return (passed, violations)"""
violations = [r.message for r in rules if not r.condition(context)]
return len(violations) == 0, violations