forked from botswana-harvard/edc-metadata
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logic.py
56 lines (44 loc) · 1.66 KB
/
logic.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
from __future__ import annotations
from typing import TYPE_CHECKING
from ..constants import DO_NOTHING, NOT_REQUIRED, REQUIRED
if TYPE_CHECKING:
from edc_visit_tracking.model_mixins import VisitModelMixin as Base
from ..model_mixins.creates import CreatesMetadataModelMixin
from .predicate import PF, P
class RelatedVisitModel(CreatesMetadataModelMixin, Base):
pass
class RuleLogicError(Exception):
pass
class Logic:
"""A class that serves as a container for the logic attribute
of a rule, the predicate, and the outcomes (result) of that
rule; consequence and alternative.
"""
valid_results = [REQUIRED, NOT_REQUIRED, DO_NOTHING]
def __init__(
self,
predicate: P | PF | callable = None,
consequence: str = None,
alternative: str = None,
comment: str | None = None,
) -> None:
if not hasattr(predicate, "__call__"):
raise RuleLogicError(
"Predicate must be a callable. For example a "
'predicate class such as "P" or "PF"'
)
self.predicate = predicate
self.consequence = consequence
self.alternative = alternative
self.comment = comment
for result in [self.consequence, self.alternative]:
if result not in self.valid_results:
raise RuleLogicError(
f"Invalid result on rule. Expected one of "
f"{self.valid_results}. Got {result}."
)
def __repr__(self) -> str:
return (
f"{self.__class__.__name__}({self.predicate}, "
f"{self.consequence}, {self.alternative})"
)