Search in sources :

Example 21 with ParsedProgram

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
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet)

Example 22 with ParsedProgram

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);
}
Also used : ASPCore2Lexer(at.ac.tuwien.kr.alpha.antlr.ASPCore2Lexer) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) ParsedTreeVisitor(at.ac.tuwien.kr.alpha.grounder.parser.ParsedTreeVisitor) ASPCore2Parser(at.ac.tuwien.kr.alpha.antlr.ASPCore2Parser)

Example 23 with ParsedProgram

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());
}
Also used : ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) Test(org.junit.Test)

Example 24 with ParsedProgram

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();
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet)

Example 25 with ParsedProgram

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");
}
Also used : ANTLRFileStream(org.antlr.v4.runtime.ANTLRFileStream) NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet)

Aggregations

ParsedProgram (at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram)35 NaiveGrounder (at.ac.tuwien.kr.alpha.grounder.NaiveGrounder)29 AnswerSet (at.ac.tuwien.kr.alpha.common.AnswerSet)28 Test (org.junit.Test)27 BasicAnswerSet (at.ac.tuwien.kr.alpha.common.BasicAnswerSet)23 Grounder (at.ac.tuwien.kr.alpha.grounder.Grounder)7 ChoiceGrounder (at.ac.tuwien.kr.alpha.grounder.ChoiceGrounder)6 DummyGrounder (at.ac.tuwien.kr.alpha.grounder.DummyGrounder)6 ASPCore2Lexer (at.ac.tuwien.kr.alpha.antlr.ASPCore2Lexer)2 ASPCore2Parser (at.ac.tuwien.kr.alpha.antlr.ASPCore2Parser)2 ParsedTreeVisitor (at.ac.tuwien.kr.alpha.grounder.parser.ParsedTreeVisitor)2 ANTLRFileStream (org.antlr.v4.runtime.ANTLRFileStream)2 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)2 Before (org.junit.Before)2 Predicate (at.ac.tuwien.kr.alpha.common.Predicate)1 GrounderFactory (at.ac.tuwien.kr.alpha.grounder.GrounderFactory)1 IdentityProgramTransformation (at.ac.tuwien.kr.alpha.grounder.transformation.IdentityProgramTransformation)1 Solver (at.ac.tuwien.kr.alpha.solver.Solver)1 SolverFactory (at.ac.tuwien.kr.alpha.solver.SolverFactory)1 Heuristic (at.ac.tuwien.kr.alpha.solver.heuristics.BranchingHeuristicFactory.Heuristic)1