def test_connective(self): for input_conn, expected_conn in [ (logic.BinaryConnective.CONJUNCTION, "&"), (logic.BinaryConnective.DISJUNCTION, "|"), ]: with self.subTest(input_conn=input_conn, expected_conn=expected_conn): self.assert_compiler( logic.BinaryFormula( logic.Variable("X"), input_conn, logic.Variable("Y") ), "X%sY" % expected_conn, )
def test_verum(self): inp = """fof(mElmSort,axiom,( ! [W0] : ( aElement0(W0) => $true ) )).""" result = problem.AnnotatedFormula( logic="fof", name="mElmSort", role=problem.FormulaRole.AXIOM, formula=logic.QuantifiedFormula( quantifier=logic.Quantifier.UNIVERSAL, variables=[logic.Variable("W0")], formula=logic.BinaryFormula( left=logic.PredicateExpression( predicate="aElement0", arguments=[logic.Variable("W0")]), operator=logic.BinaryConnective.IMPLICATION, right=logic.DefinedConstant.VERUM, ), ), ) self.check_parser(inp, result)
def test_functor(self): inp = """cnf(and_definition1,axiom,( and(X,n0) = n0 )).""" result = problem.AnnotatedFormula( logic="cnf", name="and_definition1", role=problem.FormulaRole.AXIOM, formula=logic.BinaryFormula( left=logic.FunctorExpression( functor="and", arguments=[logic.Variable("X"), logic.Constant("n0")]), operator=logic.BinaryConnective.EQ, right=logic.Constant("n0"), ), ) self.check_parser(inp, result)
def parseJavaToPython(node): if node.getVisitName() == "quantifier": return logic.Quantifier(node.getId()) elif node.getVisitName() == "binary_connective": return logic.BinaryConnective(node.getId()) elif node.getVisitName() == "defined_predicate": return logic.DefinedPredicate(node.getId()) elif node.getVisitName() == "unary_connective": return logic.UnaryConnective(node.getId()) elif node.getVisitName() == "unary_formula": return logic.UnaryFormula( OWLParser.parseJavaToPython(node.getConnective()), OWLParser.parseJavaToPython(node.getFormula())) elif node.getVisitName() == "quantified_formula": variables = [] for var in node.getVariables(): variables.append(OWLParser.parseJavaToPython(var)) return logic.QuantifiedFormula( OWLParser.parseJavaToPython(node.getQuantifier()), variables, OWLParser.parseJavaToPython(node.getFormula())) elif node.getVisitName() == "binary_formula": return logic.BinaryFormula( OWLParser.parseJavaToPython(node.getLeft()), OWLParser.parseJavaToPython(node.getOp()), OWLParser.parseJavaToPython(node.getRight())) elif node.getVisitName() == "predicate_expression": # TODO: find out how to handle lists like arguments arguments = [] for arg in node.getArguments(): arguments.append(OWLParser.parseJavaToPython(arg)) return logic.PredicateExpression(node.getPredicate(), arguments) elif node.getVisitName() == "typed_variable": return logic.TypedVariable( node.getName(), OWLParser.parseJavaToPython(node.getVType())) elif node.getVisitName() == "variable": return logic.Variable(node.getSymbol()) elif node.getVisitName() == "constant": return logic.Constant(node.getSymbol()) elif node.getVisitName() == "defined_constant": return logic.DefinedConstant(node.getId()) elif node.getVisitName() == "subtype": return logic.Subtype(OWLParser.parseJavaToPython(node.getLeft), OWLParser.parseJavaToPython(node.getRight)) elif node.getVisitName() == "type": return logic.Type(node.getName())
def test_quantifier(self): inp = "![X1,X2]:?[Y1,Y2]:p(X1,X2,Y1,Y2)" result = logic.QuantifiedFormula( logic.Quantifier.UNIVERSAL, [logic.Variable("X1"), logic.Variable("X2")], logic.QuantifiedFormula( logic.Quantifier.EXISTENTIAL, [logic.Variable("Y1"), logic.Variable("Y2")], logic.PredicateExpression( "p", [ logic.Variable("X1"), logic.Variable("X2"), logic.Variable("Y1"), logic.Variable("Y2"), ], ), ), ) return inp, result
def parse_variable(self, variable: dict) -> logic.Variable: return logic.Variable(symbol=variable["symbol"])
def visit_variable(self, obj, **kwargs): assert len(obj.children) == 1 and isinstance(obj.children[0], str) return logic.Variable(obj.children[0])
def test_variable(self): self.assert_compiler(logic.Variable("X"), "X")