use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.
the class NormalizeProgramTransformation method apply.
@Override
public NormalProgram apply(ASPCore2Program inputProgram) {
ASPCore2Program tmpPrg;
// Remove variable equalities.
tmpPrg = new VariableEqualityRemoval().apply(inputProgram);
// Transform choice rules.
tmpPrg = new ChoiceHeadToNormal().apply(tmpPrg);
// Transform aggregates.
tmpPrg = new AggregateRewriting(aggregateRewritingCfg.isUseSortingGridEncoding(), aggregateRewritingCfg.isSupportNegativeValuesInSums()).apply(tmpPrg);
// Transform enumeration atoms.
tmpPrg = new EnumerationRewriting().apply(tmpPrg);
EnumerationAtom.resetEnumerations();
// Construct the normal program.
NormalProgram retVal = NormalProgramImpl.fromInputProgram(tmpPrg);
// Transform intervals.
retVal = new IntervalTermToIntervalAtom().apply(retVal);
// Rewrite ArithmeticTerms.
retVal = new ArithmeticTermsRewriting().apply(retVal);
return retVal;
}
use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program 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.api.programs.ASPCore2Program in project Alpha by alpha-asp.
the class NaiveGrounderTest method groundRuleAlreadyGround.
/**
* Asserts that a ground rule whose positive body is not satisfied by the empty assignment
* is grounded immediately.
*/
@Test
public void groundRuleAlreadyGround() {
ASPCore2Program program = PROGRAM_PARSER.parse("a :- not b. " + "b :- not a. " + "c :- b.");
NormalProgram normal = NORMALIZE_TRANSFORM.apply(program);
CompiledProgram prog = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normal));
AtomStore atomStore = new AtomStoreImpl();
Grounder grounder = GrounderFactory.getInstance("naive", prog, atomStore, true);
Map<Integer, NoGood> noGoods = grounder.getNoGoods(new TrailAssignment(atomStore));
int litCNeg = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("c")), false);
int litB = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("b")));
assertExistsNoGoodContaining(noGoods.values(), litCNeg);
assertExistsNoGoodContaining(noGoods.values(), litB);
}
use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.
the class NaiveGrounderTest method testPermissiveGrounderHeuristicTolerance_2_accept.
@Test
public void testPermissiveGrounderHeuristicTolerance_2_accept() {
ASPCore2Program program = PROGRAM_PARSER.parse("a(1). " + "c(X) :- a(X), b(X), b(X+1). " + "b(X) :- something(X).");
testPermissiveGrounderHeuristicTolerance(program, 0, litAX, 1, 2, true, Arrays.asList(2));
}
use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.
the class NaiveGrounderTest method testPermissiveGrounderHeuristicTolerance_1_accept_two_substitutions.
@Test
public void testPermissiveGrounderHeuristicTolerance_1_accept_two_substitutions() {
ASPCore2Program program = PROGRAM_PARSER.parse("a(1). " + "c(X) :- a(X), b(X,Y). " + "b(X,Y) :- something(X,Y).");
testPermissiveGrounderHeuristicTolerance(program, 0, litAX, 1, 1, new ThriceTruth[] { ThriceTruth.TRUE, ThriceTruth.TRUE }, 2, true, Arrays.asList(0, 0));
}
Aggregations