Search in sources :

Example 61 with ASPCore2Program

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;
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AggregateRewriting(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewriting) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram)

Example 62 with ASPCore2Program

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());
}
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 63 with ASPCore2Program

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);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) Test(org.junit.jupiter.api.Test)

Example 64 with ASPCore2Program

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));
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Test(org.junit.jupiter.api.Test)

Example 65 with ASPCore2Program

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));
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Test(org.junit.jupiter.api.Test)

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