Search in sources :

Example 1 with ProgramParser

use of at.ac.tuwien.kr.alpha.api.programs.ProgramParser in project Alpha by alpha-asp.

the class StratifiedEvaluationTest method testNegatedExternalLiteral.

@Test
public void testNegatedExternalLiteral() throws Exception {
    String asp = "claimedTruth(bla). truth(X) :- claimedTruth(X), &sayTrue[X]. lie(X) :- claimedTruth(X), not &sayTrue[X].";
    Map<String, PredicateInterpretation> externals = new HashMap<>();
    externals.put("sayTrue", Externals.processPredicateMethod(this.getClass().getMethod("sayTrue", Object.class)));
    ProgramParser parserWithExternals = new ProgramParserImpl();
    AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalizer.apply(parserWithExternals.parse(asp, externals)));
    CompiledProgram evaluated = new StratifiedEvaluation().apply(analyzed);
    Set<AnswerSet> answerSets = solveCompiledProg.apply(evaluated);
    TestUtils.assertAnswerSetsEqual("claimedTruth(bla), truth(bla)", answerSets);
}
Also used : AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) HashMap(java.util.HashMap) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 2 with ProgramParser

use of at.ac.tuwien.kr.alpha.api.programs.ProgramParser in project Alpha by alpha-asp.

the class StratifiedEvaluationRegressionTest method runTest.

@ParameterizedTest
@MethodSource("at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluationRegressionTest#params")
public void runTest(String aspString, Consumer<CompiledProgram> programVerifier, Consumer<Set<AnswerSet>> resultVerifier) {
    // Parse and pre-evaulate program
    ProgramParser parser = new ProgramParserImpl();
    ASPCore2Program prog = parser.parse(aspString);
    AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(new NormalizeProgramTransformation(SystemConfig.DEFAULT_AGGREGATE_REWRITING_CONFIG).apply(prog));
    CompiledProgram evaluated = new StratifiedEvaluation().apply(analyzed);
    // Verify stratified evaluation result
    programVerifier.accept(evaluated);
    // Solve remaining program
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance("naive", evaluated, atomStore, false);
    Solver solver = SolverFactory.getInstance(new SystemConfig(), atomStore, grounder);
    Set<AnswerSet> answerSets = solver.collectSet();
    resultVerifier.accept(answerSets);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Solver(at.ac.tuwien.kr.alpha.api.Solver) SystemConfig(at.ac.tuwien.kr.alpha.api.config.SystemConfig) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Grounder(at.ac.tuwien.kr.alpha.core.grounder.Grounder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with ProgramParser

use of at.ac.tuwien.kr.alpha.api.programs.ProgramParser in project Alpha by alpha-asp.

the class UnifierTest method parseAtom.

private BasicAtom parseAtom(String atom) {
    ProgramParser programParser = new ProgramParserImpl();
    ASPCore2Program program = programParser.parse(atom + ".");
    return (BasicAtom) program.getFacts().get(0);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)

Example 4 with ProgramParser

use of at.ac.tuwien.kr.alpha.api.programs.ProgramParser in project Alpha by alpha-asp.

the class AlphaImplTest method testLearnedUnaryNoGoodCausingOutOfOrderLiteralsConflict.

// Detailed reproduction test-case for github issue #239.
@Test
public void testLearnedUnaryNoGoodCausingOutOfOrderLiteralsConflict() throws IOException {
    final ProgramParser parser = new ProgramParserImpl();
    InputProgram.Builder bld = InputProgram.builder();
    bld.accumulate(parser.parse(Files.newInputStream(Paths.get("src", "test", "resources", "HanoiTower_Alpha.asp"), StandardOpenOption.READ)));
    bld.accumulate(parser.parse(Files.newInputStream(Paths.get("src", "test", "resources", "HanoiTower_instances", "simple.asp"), StandardOpenOption.READ)));
    InputProgram parsedProgram = bld.build();
    SystemConfig config = new SystemConfig();
    config.setSolverName("default");
    config.setNogoodStoreName("alpharoaming");
    config.setSeed(0);
    config.setBranchingHeuristic(Heuristic.valueOf("VSIDS"));
    config.setDebugInternalChecks(true);
    config.setDisableJustificationSearch(false);
    config.setEvaluateStratifiedPart(false);
    config.setReplayChoices(Arrays.asList(21, 26, 36, 56, 91, 96, 285, 166, 101, 290, 106, 451, 445, 439, 448, 433, 427, 442, 421, 415, 436, 409, 430, 397, 391, 424, 385, 379, 418, 373, 412, 406, 394, 388, 382, 245, 232, 208));
    Alpha alpha = new AlphaImpl(config);
    Optional<AnswerSet> answerSet = alpha.solve(parsedProgram).findFirst();
    assertTrue(answerSet.isPresent());
}
Also used : SystemConfig(at.ac.tuwien.kr.alpha.api.config.SystemConfig) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram) Test(org.junit.jupiter.api.Test)

Example 5 with ProgramParser

use of at.ac.tuwien.kr.alpha.api.programs.ProgramParser in project Alpha by alpha-asp.

the class RuleParser method parse.

public static Rule<Head> parse(String str) {
    ProgramParser parser = new ProgramParserImpl();
    ASPCore2Program prog = parser.parse(str);
    if (!prog.getFacts().isEmpty()) {
        throw new IllegalArgumentException("Expected exactly one rule and no facts!");
    }
    if (prog.getRules().size() != 1) {
        throw new IllegalArgumentException("Expected exactly one rule");
    }
    return prog.getRules().get(0);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)

Aggregations

ProgramParser (at.ac.tuwien.kr.alpha.api.programs.ProgramParser)5 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)5 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)3 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)3 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)2 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)2 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)2 Test (org.junit.jupiter.api.Test)2 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)1 Solver (at.ac.tuwien.kr.alpha.api.Solver)1 PredicateInterpretation (at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation)1 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)1 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)1 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)1 Grounder (at.ac.tuwien.kr.alpha.core.grounder.Grounder)1 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)1 HashMap (java.util.HashMap)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1