Skip to content

JSON Logic ASP Evaluator

Evaluate JSON Logic rules against data using Clingo.

Usage


Documentation

evaluate_multiple_json_logic_rules_against_single_data(json_logic_rules, json_logic_data, simplify=False, custom_nodes=None)

Given a multiple JSON Logic rules and data, evaluate it using Clingo.

Parameters:

Name Type Description Default
json_logic_rules List[RuleInput]

list of rule input with JSON Logic definitions

required
json_logic_data DataInput

data input object

required
simplify bool

if True, simplifies the JSON Logic definition

False
custom_nodes Optional[Dict[str, Type]]

optional dictionary of custom nodes to parse

None

Returns:

Type Description
List[str]

list of rule ids matching the data

Source code in json_logic_asp/evaluator.py
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
def evaluate_multiple_json_logic_rules_against_single_data(
    json_logic_rules: List[RuleInput],
    json_logic_data: DataInput,
    simplify: bool = False,
    custom_nodes: Optional[Dict[str, Type]] = None,
) -> List[str]:
    """
    Given a multiple JSON Logic rules and data, evaluate it using Clingo.
    :param json_logic_rules: list of rule input with JSON Logic definitions
    :param json_logic_data: data input object
    :param simplify: if True, simplifies the JSON Logic definition
    :param custom_nodes: optional dictionary of custom nodes to parse
    :return: list of rule ids matching the data
    """
    if simplify:
        new_json_logic_rules: List[RuleInput] = []
        for json_logic_rule in json_logic_rules:
            new_json_logic_rules.append(
                RuleInput(
                    rule_id=json_logic_rule.rule_id,
                    rule_tree=simplify_json_logic(json_logic_rule.rule_tree),
                )
            )
        json_logic_rules = new_json_logic_rules

    asp_rules_definition, rule_id_mapping = generate_multiple_rule_asp_definition(
        rule_inputs=json_logic_rules,
        with_comments=False,
        custom_nodes=custom_nodes,
    )

    return evaluate_pregenerated_json_logic_rules_against_single_data(
        json_logic_rules_in_asp_definition=asp_rules_definition,
        json_logic_data=json_logic_data,
        rule_id_mapping=rule_id_mapping,
    )

evaluate_pregenerated_json_logic_rules_against_single_data(json_logic_rules_in_asp_definition, json_logic_data, rule_id_mapping=None)

Given some rules in ASP definition and a data object, return the matching rules.

Parameters:

Name Type Description Default
json_logic_rules_in_asp_definition str

JSON Logic rules in ASP definition

required
json_logic_data DataInput

data object to check against

required
rule_id_mapping Optional[Dict[str, str]]

optional mapping of rule ids

None

Returns:

Type Description
List[str]

list of matching rules

Source code in json_logic_asp/evaluator.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def evaluate_pregenerated_json_logic_rules_against_single_data(
    json_logic_rules_in_asp_definition: str,
    json_logic_data: DataInput,
    rule_id_mapping: Optional[Dict[str, str]] = None,
) -> List[str]:
    """
    Given some rules in ASP definition and a data object, return the matching rules.
    :param json_logic_rules_in_asp_definition: JSON Logic rules in ASP definition
    :param json_logic_data: data object to check against
    :param rule_id_mapping: optional mapping of rule ids
    :return: list of matching rules
    """
    asp_data_definition = generate_single_data_asp_definition(
        data_input=json_logic_data,
        with_comments=False,
    )

    matching_rules = get_matching_rules_for_asp_rules_and_data(
        asp_data_definition=asp_data_definition,
        asp_rules_definition=json_logic_rules_in_asp_definition,
        mapping=rule_id_mapping,
    )

    return matching_rules

evaluate_single_json_logic_rule_against_single_data(json_logic_rule, json_logic_data, simplify=False, custom_nodes=None)

Given a single JSON Logic rule and data, evaluate it using Clingo.

Parameters:

Name Type Description Default
json_logic_rule RuleInput

single rule input with JSON Logic definition

required
json_logic_data DataInput

data input object

required
simplify bool

if True, simplifies the JSON Logic definition

False
custom_nodes Optional[Dict[str, Type]]

optional dictionary of custom nodes to parse

None

Returns:

Type Description
bool

whether the rule matches or not the data

Source code in json_logic_asp/evaluator.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def evaluate_single_json_logic_rule_against_single_data(
    json_logic_rule: RuleInput,
    json_logic_data: DataInput,
    simplify: bool = False,
    custom_nodes: Optional[Dict[str, Type]] = None,
) -> bool:
    """
    Given a single JSON Logic rule and data, evaluate it using Clingo.
    :param json_logic_rule: single rule input with JSON Logic definition
    :param json_logic_data: data input object
    :param simplify: if True, simplifies the JSON Logic definition
    :param custom_nodes: optional dictionary of custom nodes to parse
    :return: whether the rule matches or not the data
    """
    matching_rules = evaluate_multiple_json_logic_rules_against_single_data(
        json_logic_rules=[json_logic_rule],
        json_logic_data=json_logic_data,
        simplify=simplify,
        custom_nodes=custom_nodes,
    )

    return len(matching_rules) == 1