-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstraint_engine.py
More file actions
34 lines (24 loc) · 890 Bytes
/
constraint_engine.py
File metadata and controls
34 lines (24 loc) · 890 Bytes
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
from typing import Any, Dict, List
class ConstraintFailure(Exception):
pass
class Constraint:
"""
v0.1 Constraint:
A simple rule that checks the intent inputs before execution.
"""
def __init__(self, name: str, rule):
self.name = name
self.rule = rule # rule: Callable[[Dict[str, Any]], bool]
def check(self, inputs: Dict[str, Any]) -> None:
if not self.rule(inputs):
raise ConstraintFailure(f"Constraint failed: {self.name}")
class ConstraintEngine:
"""
v0.1 Constraint Engine:
Runs a list of constraints before capsule execution.
"""
def __init__(self, constraints: List[Constraint]):
self.constraints = constraints
def validate(self, inputs: Dict[str, Any]) -> None:
for constraint in self.constraints:
constraint.check(inputs)