Skip to content

rule_generator

generate_multiple_rule_asp_definition(rule_inputs, with_comments=False, custom_nodes=None)

Given multiple rule inputs, generate the corresponding ASP definition.

Parameters:

Name Type Description Default
rule_inputs List[RuleInput]

list of rule input objects to translate

required
with_comments bool

whether to include ASP comments

False
custom_nodes Optional[Dict[str, Type]]

dictionary of node_key and corresponding class generating the node

None

Returns:

Type Description
Tuple[str, Dict[str, str]]

tuple of ASP definition and mapping dictionary (ASP rule to original rule id)

Source code in json_logic_asp/translator/rule_generator.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def generate_multiple_rule_asp_definition(
    rule_inputs: List[RuleInput],
    with_comments: bool = False,
    custom_nodes: Optional[Dict[str, Type]] = None,
) -> Tuple[str, Dict[str, str]]:
    """
    Given multiple rule inputs, generate the corresponding ASP definition.

    :param rule_inputs: list of rule input objects to translate
    :param with_comments: whether to include ASP comments
    :param custom_nodes: dictionary of node_key and corresponding class generating the node
    :return: tuple of ASP definition and mapping dictionary (ASP rule to original rule id)
    """
    rule_node_cache: Dict[str, JsonLogicNode] = {}

    statements = []
    root_statements = []

    mapping = {}

    for rule_input in rule_inputs:
        root_node = __parse_json_logic_node(
            node=rule_input.rule_tree,
            rule_node_cache=rule_node_cache,
            custom_nodes=custom_nodes or {},
        )
        statements.extend(root_node.to_asp(with_comment=with_comments))
        # statements = root_node.to_asp()

        hashed_id = generate_constant_string(rule_input.rule_id)
        mapping[hashed_id] = rule_input.rule_id

        root_statement = RuleStatement(
            atom=PredicateAtom(predicate_name=PredicateNames.RULE, terms=[hashed_id]),
            literals=[root_node.get_asp_atom()],
            comment=rule_input.rule_id,
        )
        if with_comments:
            root_statements.append(root_statement.to_asp_comment())
        root_statements.append(root_statement.to_asp_statement())

    statements = remove_duplicates(statements + root_statements)

    return "\n".join(statements), mapping

generate_single_rule_asp_definition(rule_input, with_comments=False, custom_nodes=None)

Given a single rule input, generate the corresponding ASP definition.

Parameters:

Name Type Description Default
rule_input RuleInput

rule input object to translate

required
with_comments bool

whether to include ASP comments

False
custom_nodes Optional[Dict[str, Type]]

dictionary of node_key and corresponding class generating the node

None

Returns:

Type Description
str

ASP definition

Source code in json_logic_asp/translator/rule_generator.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def generate_single_rule_asp_definition(
    rule_input: RuleInput,
    with_comments: bool = False,
    custom_nodes: Optional[Dict[str, Type]] = None,
) -> str:
    """
    Given a single rule input, generate the corresponding ASP definition.

    :param rule_input: rule input object to translate
    :param with_comments: whether to include ASP comments
    :param custom_nodes: dictionary of node_key and corresponding class generating the node
    :return: ASP definition
    """
    definition, _ = generate_multiple_rule_asp_definition(
        rule_inputs=[rule_input], with_comments=with_comments, custom_nodes=custom_nodes
    )
    return definition