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.");
}
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);
}
}
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());
}
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);
}
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());
}
Aggregations