return DatalogFormula(_get_definition_clause(ast.lhs), _get_clause(ast.rhs), ast.op.text)
def to_prolog_string(rule: DatalogFormula) -> str:
# TODO: for now it works with just propositional rules without clauses except 'class'.
op_mapping = {
'≤': '=<',
'<': '<',
'≥': '>',
'>': '>',
'∧': ',', # should not be necessary
'=': '=',
}
def unfold_body(e: Expression, result=[]) -> list[str]:
if isinstance(e.lhs, Expression):
result = result + unfold_body(e.lhs)
if isinstance(e.rhs, Expression):
result = result + unfold_body(e.rhs)
if not isinstance(e.lhs, Expression) and not isinstance(e.rhs, Expression):
return ['\'' + op_mapping[e.op] + '\'(' + str(e.rhs) + ', ' + str(e.rhs) + ')']
return result
head = str(rule.lhs)
if isinstance(rule.rhs, Expression):
body = ', '.join(unfold_body(rule.rhs))
return head + ' :- ' + body + '.'
else:
return head + '.'
def _get_definition_clause(node: DatalogParser.DefPredicateArgsContext):
return DefinitionClause(node.pred.text, _get_arguments(node.args))
https://github.com/psykei/psyki-python/blob/e65594f3fce2013a9f81b03813c476e0f6c3bb87/psyki/logic/datalog/grammar/adapters/antlr4.py#L20