Skip to content

data_generator

generate_single_data_asp_definition(data_input, with_comments=False)

Given a data input, generate the corresponding ASP definition.

Parameters:

Name Type Description Default
data_input DataInput

DataInput object with the containing data

required
with_comments bool

if true, generate the definition including ASP comments

False

Returns:

Type Description
str

data encoded in ASP definition

Source code in json_logic_asp/translator/data_generator.py
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
57
58
def generate_single_data_asp_definition(data_input: DataInput, with_comments: bool = False) -> str:
    """
    Given a data input, generate the corresponding ASP definition.

    :param data_input: DataInput object with the containing data
    :param with_comments: if true, generate the definition including ASP comments
    :return: data encoded in ASP definition
    """
    statements = []

    flattened_obj = __flatten_data(data_input.data_object)
    for var_name, var_value in flattened_obj.items():
        stmt = FactStatement(
            atom=PredicateAtom(
                predicate_name=PredicateNames.DATA_VAR,
                terms=[
                    generate_constant_string(var_name),
                    value_encoder(var_value),
                ],
            ),
            comment=f"{var_name} : {var_value}",
        )
        if with_comments:
            statements.append(stmt.to_asp_comment())
        statements.append(stmt.to_asp_statement())

    statements = remove_duplicates(statements)

    return "\n".join(statements)