Skip to content

run

get_matching_rules_for_asp_rules_and_data(asp_data_definition, asp_rules_definition, mapping=None)

Given some data definition and rule definition, evaluate it with Clingo and return the matching rules.

Parameters:

Name Type Description Default
asp_data_definition str

encoded data in ASP language

required
asp_rules_definition str

encoded rules in ASP language

required
mapping Optional[Dict[str, str]]

optional mapping for the ASP rule ids

None

Returns:

Type Description
List[str]

list of matching rules, mapped if provided

Source code in json_logic_asp/invoker/run.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def get_matching_rules_for_asp_rules_and_data(
    asp_data_definition: str, asp_rules_definition: str, mapping: Optional[Dict[str, str]] = None
) -> List[str]:
    """
    Given some data definition and rule definition, evaluate it with Clingo and return the matching rules.
    :param asp_data_definition: encoded data in ASP language
    :param asp_rules_definition: encoded rules in ASP language
    :param mapping: optional mapping for the ASP rule ids
    :return: list of matching rules, mapped if provided
    """
    asp_definition_parts = [
        asp_data_definition,
        asp_rules_definition,
        ShowStatement("rule", 1).to_asp_statement(),
    ]
    asp_definition = "\n\n\n".join(asp_definition_parts)

    return get_matching_rules_from_asp_problem(problem=asp_definition, mapping=mapping)

get_matching_rules_from_asp_problem(problem, mapping=None)

Given an ASP problem, return the matching rules.

Parameters:

Name Type Description Default
problem str

ASP problem with data, rules and show statement.

required
mapping Optional[Dict[str, str]]

optional mapping for the ASP rule ids

None

Returns:

Type Description
List[str]

list of matching rules, mapped if provided

Source code in json_logic_asp/invoker/run.py
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
def get_matching_rules_from_asp_problem(problem: str, mapping: Optional[Dict[str, str]] = None) -> List[str]:
    """
    Given an ASP problem, return the matching rules.
    :param problem: ASP problem with data, rules and show statement.
    :param mapping: optional mapping for the ASP rule ids
    :return: list of matching rules, mapped if provided
    """
    file_path = __store_clingo_temp_file(problem)
    log.debug(f"Temp File Path: {file_path}")

    status, matching_rules, stats = run_clingo(absolute_file_path=str(file_path.absolute()))
    log.debug(stats)

    file_path.unlink()
    log.debug("Removed Temp File")

    output = ClingoOutput(
        success=status == "SAT",
        matching_rules=matching_rules,
    )

    if output.success and mapping:
        return [mapping[rule_id] if rule_id in mapping else rule_id for rule_id in output.matching_rules]

    return output.matching_rules