psykei / psyki-python

PSyKI: a (Python) platform for symbolic knowledge injection
https://psykei.github.io/psyki-python/
Apache License 2.0
12 stars 1 forks source link

for now it works with just propositional rules without clauses except 'class'. #15

Closed github-actions[bot] closed 1 year ago

github-actions[bot] commented 2 years ago

https://github.com/psykei/psyki-python/blob/e65594f3fce2013a9f81b03813c476e0f6c3bb87/psyki/logic/datalog/grammar/adapters/antlr4.py#L20


    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))
github-actions[bot] commented 1 year ago

Closed in d5ad247697e69175f865caa9cb152481c48a0bcd