Reputation: 21
I'm trying to learn how to use coding for Logic Knowledge Representation and ultimately ML/AI.
I'm trying to make a java Tweety code about first order logic run but
I always get the same error: the formula is not closed.
I know the meaning of a formula not being the in FOL, I just can't understand where the problem is in my code.
Here is the code:
public class SimpleFolExample {
public static void main(String[] args) throws ParserException, IOException {
// Create FOL signature
FolSignature signature = new FolSignature(true);
// Add sorts
// Add sorts
Sort sortPerson = new Sort("Person");
signature.add(sortPerson);
// Add constants
Constant alice = new Constant("Alice", sortPerson);
Constant bob = new Constant("Bob", sortPerson);
Constant James = new Constant("James", sortPerson);
signature.add(alice, bob, James);
// Add predicates
// Add predicates
List<Sort> personPredicateSorts = new ArrayList<>();
personPredicateSorts.add(sortPerson);
Predicate person = new Predicate("Person", personPredicateSorts);
signature.add(person); // Add predicates to the signature
// Add the 'Knows' predicate (assuming it relates persons)
List<Sort> knowsPredicateSorts = new ArrayList<>();
knowsPredicateSorts.add(sortPerson);
knowsPredicateSorts.add(sortPerson);
Predicate knows = new Predicate("Knows", knowsPredicateSorts);
signature.add(knows);
// Display the signature
System.out.println("Signature: " + signature);
// Parse formulas using the signature
FolParser parser = new FolParser();
parser.setSignature(signature); // Set the FOL signature for the parser
FolBeliefSet beliefSet = new FolBeliefSet();
FolFormula f1 = (FolFormula) parser.parseFormula("Knows(Alice, Bob)");
FolFormula f2 = (FolFormula) parser.parseFormula("Knows(Bob, James)");
beliefSet.add(f1, f2);
System.out.println("\nParsed Belief Base: " + beliefSet);
// Use the reasoner to query the knowledge base
FolReasoner.setDefaultReasoner(new SimpleFolReasoner());
FolReasoner prover = FolReasoner.getDefaultReasoner();
FolFormula query = (FolFormula) parser.parseFormula("exists X:(Knows(Alice, X))");
System.out.println("Query: " + query + "\nResult: " + prover.query(beliefSet, query));
}
}
here is the error:
Signature: [_Any = {}, Person = {Bob, Alice, James}], [==(_Any,_Any), /==(_Any,_Any), Person(Person), Knows(Person,Person)], []
Parsed Belief Base: { Knows(Bob,James), Knows(Alice,Bob) }
Exception in thread "main" java.lang.IllegalArgumentException: The given formula exists X: (Knows(Alice,X)) is not closed.
at org.tweetyproject.logics.fol.reasoner.SimpleFolReasoner.query(SimpleFolReasoner.java:45)
at org.tweetyproject.logics.fol.examples.SimpleFolExample.main(SimpleFolExample.java:82
Upvotes: 2
Views: 103
Reputation: 33
The problem can be solved if the constants are in lower case: alice, bob, james instead of Alice, Bob, James.
Upvotes: 0