Search in sources :

Example 56 with ASPCore2Program

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

the class ParserTest method parseFact.

@Test
public void parseFact() {
    ASPCore2Program parsedProgram = parser.parse("p(a,b).");
    assertEquals(1, parsedProgram.getFacts().size(), "Program contains one fact.");
    assertEquals("p", parsedProgram.getFacts().get(0).getPredicate().getName(), "Predicate name of fact is p.");
    assertEquals(2, parsedProgram.getFacts().get(0).getPredicate().getArity(), "Fact has two terms.");
    assertEquals("a", (parsedProgram.getFacts().get(0).getTerms().get(0)).toString(), "First term is a.");
    assertEquals("b", (parsedProgram.getFacts().get(0).getTerms().get(1)).toString(), "Second term is b.");
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Test(org.junit.jupiter.api.Test)

Example 57 with ASPCore2Program

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

the class ProgramTransformationTest method genericTransformationTest.

private <I extends Program<?>, O extends Program<?>> void genericTransformationTest(ProgramTransformation<I, O> transform, Function<ASPCore2Program, I> prepareFunc, String resourceSet) {
    try {
        String inputCode = ProgramTransformationTest.readTestResource(resourceSet + ".in");
        String expectedResult = ProgramTransformationTest.readTestResource(resourceSet + ".out");
        ASPCore2Program inputProg = PARSER.parse(inputCode, Externals.scan(ProgramTransformationTest.class));
        I transformInput = prepareFunc.apply(inputProg);
        String beforeTransformProg = transformInput.toString();
        O transformedProg = transform.apply(transformInput);
        assertEquals(expectedResult, transformedProg.toString(), "Transformation result doesn't match expected result");
        assertEquals(beforeTransformProg, transformInput.toString(), "Transformation modified source program (breaks immutability!)");
    } catch (Exception ex) {
        LOGGER.error("Exception in test, nested exception: " + ex.getMessage(), ex);
        throw new RuntimeException(ex);
    }
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) IOException(java.io.IOException)

Example 58 with ASPCore2Program

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

the class StratifiedEvaluationTest method testPartnerUnitsProblemTopologicalOrder.

/**
 * Tests an encoding associated with the partner units problem (PUP) that computes a topological order to be used by
 * domain-specific heuristics. The entire program can be solved by stratified evaluation.
 */
@Test
public void testPartnerUnitsProblemTopologicalOrder() throws IOException {
    ASPCore2Program prg = parser.parse(StratifiedEvaluationTest.class.getResourceAsStream("/partial-eval/pup_topological_order.asp"));
    CompiledProgram evaluated = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normalizer.apply(prg)));
    assertTrue(evaluated.getRules().isEmpty(), "Not all rules eliminated by stratified evaluation");
    assertEquals(57, evaluated.getFacts().size());
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 59 with ASPCore2Program

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

the class StratifiedEvaluationTest method testNegatedLiteralInRecursiveRule.

/**
 * Verifies correct handling of negated basic literals in StratifiedEvaluation.
 * For details, see comments in test program
 *
 * @throws IOException
 */
@Test
public void testNegatedLiteralInRecursiveRule() throws IOException {
    // @formatter:off
    String expectedAnswerSet = "basefact1(1), basefact2(1), max_value(10), min_value(1), " + "basefact1(9), basefact2(9), base(1), base(9), " + "inc_value(1), inc_value(2), inc_value(2), inc_value(3), " + "inc_value(4), inc_value(5), inc_value(6), inc_value(7), " + "inc_value(8)";
    // @formatter:on
    ASPCore2Program prog = Programs.fromInputStream(StratifiedEvaluationTest.class.getResourceAsStream("/partial-eval/recursive_w_negated_condition.asp"), new HashMap<>());
    // Run stratified evaluation and solve
    CompiledProgram inputStratEval = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normalizer.apply(prog)));
    Set<AnswerSet> asStrat = solveCompiledProg.apply(inputStratEval);
    TestUtils.assertAnswerSetsEqual(expectedAnswerSet, asStrat);
    // Solve without stratified evaluation
    CompiledProgram inputNoStratEval = InternalProgram.fromNormalProgram(normalizer.apply(prog));
    Set<AnswerSet> as = solveCompiledProg.apply(inputNoStratEval);
    TestUtils.assertAnswerSetsEqual(expectedAnswerSet, as);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 60 with ASPCore2Program

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

the class AbstractAggregateEncoder method encodeAggregateLiteral.

/**
 * Encodes the aggregate literal referenced by the given {@link AggregateInfo}.
 *
 * @param aggregateToEncode
 * @return
 */
public ASPCore2Program encodeAggregateLiteral(AggregateInfo aggregateToEncode) {
    AggregateLiteral literalToEncode = aggregateToEncode.getLiteral();
    if (literalToEncode.getAtom().getAggregateFunction() != this.aggregateFunctionToEncode) {
        throw new IllegalArgumentException("Encoder " + this.getClass().getSimpleName() + " cannot encode aggregate function " + literalToEncode.getAtom().getAggregateFunction());
    }
    if (!this.acceptedOperators.contains(literalToEncode.getAtom().getLowerBoundOperator())) {
        throw new IllegalArgumentException("Encoder " + this.getClass().getSimpleName() + " cannot encode aggregate function " + literalToEncode.getAtom().getAggregateFunction() + " with operator " + literalToEncode.getAtom().getLowerBoundOperator());
    }
    String aggregateId = aggregateToEncode.getId();
    ASPCore2Program literalEncoding = PredicateInternalizer.makePrefixedPredicatesInternal(encodeAggregateResult(aggregateToEncode), aggregateId);
    List<Rule<Head>> elementEncodingRules = new ArrayList<>();
    for (AggregateElement elementToEncode : literalToEncode.getAtom().getAggregateElements()) {
        Rule<Head> elementRule = encodeAggregateElement(aggregateToEncode, elementToEncode);
        elementEncodingRules.add(PredicateInternalizer.makePrefixedPredicatesInternal(elementRule, aggregateId));
    }
    return new InputProgram(ListUtils.union(literalEncoding.getRules(), elementEncodingRules), literalEncoding.getFacts(), new InlineDirectivesImpl());
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) AggregateElement(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateElement) ArrayList(java.util.ArrayList) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) InlineDirectivesImpl(at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Aggregations

ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)89 Test (org.junit.jupiter.api.Test)70 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)23 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)22 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)21 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)19 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)15 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)14 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)12 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)11 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)10 Disabled (org.junit.jupiter.api.Disabled)9 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)8 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)8 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)8 HashSet (java.util.HashSet)8 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)7 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)7 SCComponent (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent)7 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)7