Search in sources :

Example 1 with InlineDirectivesImpl

use of at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl in project Alpha by alpha-asp.

the class SolverTests method testObjectProgram.

@RegressionTest
public void testObjectProgram(RegressionTestConfig cfg) {
    final Thingy thingy = new Thingy();
    final Atom fact = Atoms.newBasicAtom(Predicates.getPredicate("foo", 1), Terms.newConstant(thingy));
    final InputProgram program = new InputProgram(Collections.emptyList(), Collections.singletonList(fact), new InlineDirectivesImpl());
    assertEquals(singleton(new AnswerSetBuilder().predicate("foo").instance(thingy).build()), collectRegressionTestAnswerSets(program, cfg));
}
Also used : AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) InlineDirectivesImpl(at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram) TestUtils.buildSolverForRegressionTest(at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest)

Example 2 with InlineDirectivesImpl

use of at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl 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)

Example 3 with InlineDirectivesImpl

use of at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl in project Alpha by alpha-asp.

the class StringtemplateBasedAggregateEncoder method encodeAggregateResult.

@Override
protected ASPCore2Program encodeAggregateResult(AggregateInfo aggregateToEncode) {
    String aggregateId = aggregateToEncode.getId();
    /*
		 * Create a rule deriving a "bound" value for the core aggregate encoding.
		 * The bound is (in case of encodings for "<=" comparisons) the value that should be tested for being a lower bound, or
		 * else zero.
		 */
    Rule<Head> boundRule = null;
    if (this.needsBoundRule) {
        boundRule = this.buildBoundRule(aggregateToEncode);
    } else {
        /*
			 * Even if we don't have to create a bound rule because the aggregate encoding generates its own candidate values,
			 * we still generate a rule deriving zero as a bound, so that sums and counts over empty sets correctly return 0.
			 */
        boundRule = this.buildZeroBoundRule(aggregateToEncode);
    }
    // Generate encoding
    ST coreEncodingTemplate = new ST(this.encodingTemplate);
    coreEncodingTemplate.add("result_predicate", aggregateToEncode.getOutputAtom().getPredicate().getName());
    coreEncodingTemplate.add("id", aggregateId);
    coreEncodingTemplate.add("element_tuple", this.getElementTuplePredicateSymbol(aggregateId));
    coreEncodingTemplate.add("bound", this.getBoundPredicateName(aggregateId));
    String coreEncodingAsp = coreEncodingTemplate.render();
    // Create the basic program
    ASPCore2Program coreEncoding = new EnumerationRewriting().apply(parser.parse(coreEncodingAsp));
    // Add the programatically created bound rule and return
    return new InputProgram(ListUtils.union(coreEncoding.getRules(), Collections.singletonList(boundRule)), coreEncoding.getFacts(), new InlineDirectivesImpl());
}
Also used : EnumerationRewriting(at.ac.tuwien.kr.alpha.core.programs.transformation.EnumerationRewriting) ST(org.stringtemplate.v4.ST) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) InlineDirectivesImpl(at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Example 4 with InlineDirectivesImpl

use of at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl in project Alpha by alpha-asp.

the class AlphaImplTest method withExternalSubtype.

@Test
public void withExternalSubtype() throws Exception {
    SubThingy thingy = new SubThingy();
    BasicRule rule = new BasicRule(Heads.newNormalHead(Atoms.newBasicAtom(Predicates.getPredicate("p", 1), Terms.newConstant("x"))), singletonList(Literals.fromAtom(Atoms.newExternalAtom(Predicates.getPredicate("thinger", 1), new MethodPredicateInterpretation(this.getClass().getMethod("thinger", Thingy.class)), singletonList(Terms.newConstant(thingy)), emptyList()), true)));
    Alpha system = new AlphaImpl();
    InputProgram prog = new InputProgram(singletonList(rule), emptyList(), new InlineDirectivesImpl());
    Set<AnswerSet> actual = system.solve(prog).collect(Collectors.toSet());
    Set<AnswerSet> expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("p").instance("x").build()));
    assertEquals(expected, actual);
}
Also used : BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) MethodPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.MethodPredicateInterpretation) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) InlineDirectivesImpl(at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

InlineDirectivesImpl (at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl)4 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)4 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)2 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)2 AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)2 BasicRule (at.ac.tuwien.kr.alpha.core.rules.BasicRule)2 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)1 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)1 AggregateElement (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateElement)1 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)1 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)1 Rule (at.ac.tuwien.kr.alpha.api.rules.Rule)1 MethodPredicateInterpretation (at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.MethodPredicateInterpretation)1 EnumerationRewriting (at.ac.tuwien.kr.alpha.core.programs.transformation.EnumerationRewriting)1 TestUtils.buildSolverForRegressionTest (at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Test (org.junit.jupiter.api.Test)1 ST (org.stringtemplate.v4.ST)1