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