use of at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram in project Alpha by alpha-asp.
the class PartSubpartConfigurationTest method testPartSubpart.
private void testPartSubpart(int n) throws IOException {
List<String> rules = new ArrayList<>();
for (int i = 1; i <= n; i++) {
rules.add(String.format("n(%d).", i));
}
rules.add("part(N) :- n(N), not no_part(N).");
rules.add("no_part(N) :- n(N), not part(N).");
rules.add("subpartid(SP,ID) :- subpart(SP,P), n(ID), not no_subpartid(SP,ID).");
rules.add("no_subpartid(SP,ID) :- subpart(SP,P), n(ID), not subpartid(SP,ID).");
rules.add("subpart(SP,P) :- part(P), part(SP), P != SP, not no_subpart(SP,P).");
rules.add("no_subpart(SP,P) :- part(P), part(SP), P != SP, not subpart(SP,P).");
rules.add(":- subpart(SP,P1), subpart(SP,P2), P1 != P2.");
rules.add(":- subpart(SP1,P), subpart(SP2, P), SP1!=SP2, subpartid(SP1,ID), subpartid(SP2,ID).");
String testProgram = concat(rules);
ParsedProgram parsedProgram = parseVisit(testProgram);
NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
Solver solver = getInstance(grounder);
Optional<AnswerSet> answerSet = solver.stream().findAny();
System.out.println(answerSet);
// TODO: check correctness of answer set
}
use of at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram in project Alpha by alpha-asp.
the class Main method parseVisit.
public static ParsedProgram parseVisit(ANTLRInputStream is) throws IOException {
/*
// In order to require less memory: use unbuffered streams and avoid constructing a full parse tree.
ASPCore2Lexer lexer = new ASPCore2Lexer(new UnbufferedCharStream(is));
lexer.setTokenFactory(new CommonTokenFactory(true));
final ASPCore2Parser parser = new ASPCore2Parser(new UnbufferedTokenStream<>(lexer));
parser.setBuildParseTree(false);
*/
CommonTokenStream tokens = new CommonTokenStream(new ASPCore2Lexer(is));
final ASPCore2Parser parser = new ASPCore2Parser(tokens);
// Try SLL parsing mode (faster but may terminate incorrectly).
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.removeErrorListeners();
parser.setErrorHandler(new BailErrorStrategy());
final CustomErrorListener errorListener = new CustomErrorListener(is.getSourceName());
ASPCore2Parser.ProgramContext programContext;
try {
// Parse program
programContext = parser.program();
} catch (ParseCancellationException e) {
// retry with LL parser and DefaultErrorStrategy printing errors to console.
if (e.getCause() instanceof RecognitionException) {
tokens.reset();
parser.addErrorListener(errorListener);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
// Re-run parse.
programContext = parser.program();
} else {
throw e;
}
}
// is attempted) and the user will only see the first error encountered.
if (errorListener.getRecognitionException() != null) {
throw errorListener.getRecognitionException();
}
// Construct internal program representation.
ParsedTreeVisitor visitor = new ParsedTreeVisitor();
return (ParsedProgram) visitor.visitProgram(programContext);
}
use of at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram in project Alpha by alpha-asp.
the class ParserTest method parseFact.
@Test
public void parseFact() throws IOException {
ParsedProgram parsedProgram = parseVisit("p(a,b).");
assertEquals("Program contains one fact.", 1, parsedProgram.facts.size());
assertEquals("Predicate name of fact is p.", "p", parsedProgram.facts.get(0).getFact().getPredicate());
assertEquals("Fact has two terms.", 2, parsedProgram.facts.get(0).getFact().getArity());
assertEquals("First term is a.", "a", ((ParsedConstant) parsedProgram.facts.get(0).getFact().getTerms().get(0)).getContent());
assertEquals("Second term is b.", "b", ((ParsedConstant) parsedProgram.facts.get(0).getFact().getTerms().get(1)).getContent());
}
use of at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram in project Alpha by alpha-asp.
the class PigeonHoleTest method testPigeonsHoles.
/**
* Tries to solve the problem of assigning P pigeons to H holes.
*/
private void testPigeonsHoles(int pigeons, int holes) throws IOException {
List<String> rules = new ArrayList<>();
rules.add("pos(P,H) :- pigeon(P), hole(H), not negpos(P,H).");
rules.add("negpos(P,H) :- pigeon(P), hole(H), not pos(P,H).");
rules.add(":- pigeon(P), hole(H1), hole(H2), pos(P,H1), pos(P,H2), H1 != H2.");
rules.add(":- pigeon(P), not hashole(P).");
rules.add("hashole(P) :- pigeon(P), hole(H), pos(P,H).");
rules.add(":- pigeon(P1), pigeon(P2), hole(H), pos(P1,H), pos(P2,H), P1 != P2.");
addPigeons(rules, pigeons);
addHoles(rules, holes);
String testProgram = concat(rules);
ParsedProgram parsedProgram = parseVisit(testProgram);
NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
Solver solver = getInstance(grounder);
Set<AnswerSet> answerSets = solver.collectSet();
Assert.assertEquals(numberOfSolutions(pigeons, holes), answerSets.size());
solver.stream().findAny();
}
use of at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram in project Alpha by alpha-asp.
the class RacksTest method test.
private void test() throws IOException {
ANTLRFileStream programInputStream = new ANTLRFileStream(Paths.get("benchmarks", "siemens", "racks", "racks.lp").toString());
ParsedProgram parsedProgram = parseVisit(programInputStream);
NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
Solver solver = getInstance(grounder);
Optional<AnswerSet> answerSet = solver.stream().findFirst();
System.out.println(answerSet);
// TODO: check correctness of answer set
System.out.println(((DefaultSolver) solver).getDecisionCounter() + " choices," + ((DefaultSolver) solver).getConflictCounter() + " conflicts");
}
Aggregations