This document provides a detailed API reference for the PYTHEN library.
The RuleTreeEvaluator is the main class for evaluating legal rule sets.
Initializes the evaluator.
rule_tree(List[Dict]): A list of rule dictionaries. The structure of these rules is validated upon initialization.enable_trace(bool, optional): IfTrue, the evaluator will record a detailed trace of its reasoning steps. Defaults toFalse.
from pythen import RuleTreeEvaluator
rules = [...] # Your list of rules
evaluator = RuleTreeEvaluator(rules, enable_trace=True)Evaluates a target predicate against a set of facts.
facts(List[str]): A list of strings representing the known facts for the case.target_predicate(str): The name of the proposition (p) you want to evaluate.
Returns: bool - True if the predicate is satisfied, False otherwise.
facts = ["is_citizen", "age_is_18_or_more"]
result = evaluator.evaluate(facts, "can_vote")Generates a human-readable explanation of the evaluation process.
This method automatically enables tracing for the evaluation to provide a step-by-step breakdown.
facts(List[str]): The facts of the case.target_predicate(str): The predicate to explain.
Returns: str - A formatted string detailing the evaluation steps.
explanation = evaluator.explain(facts, "can_vote")
print(explanation)Returns the evaluation trace if enable_trace was set to True during initialization.
Returns: List[Tuple[str, str, bool]] - A list of tuples, where each tuple represents a step in the evaluation: (predicate, evaluation_type, result).
trace = evaluator.get_trace()
for step in trace:
print(f"Predicate: {step[0]}, Type: {step[1]}, Result: {step[2]}")Retrieves the rule definition for a specific predicate.
target_predicate(str): The name of the predicate to look up.
Returns: Dict or None - The rule dictionary if found, otherwise None.
Returns a list of all predicates defined in the rule set.
Returns: List[str] - A list of all proposition names (p).
These functions are available in the pythen.utils module.
Validates a single rule dictionary. Raises a RuleValidationError if the rule is invalid.
Validates a list of rules. Raises a RuleValidationError if any rule is invalid or if there are duplicate proposition names.
Loads and validates a rule set from a JSON string.
Converts a rule set into a formatted JSON string.
Logs a message using the built-in logger. Levels are "DEBUG", "INFO", "WARNING", "ERROR".
Custom exception raised by the validation functions when a rule or rule set does not conform to the required structure.