Search in sources :

Example 1 with NormalRule

use of at.ac.tuwien.kr.alpha.api.rules.NormalRule in project Alpha by alpha-asp.

the class ArithmeticTermsRewriting method apply.

@Override
public NormalProgram apply(NormalProgram inputProgram) {
    List<NormalRule> rewrittenRules = new ArrayList<>();
    boolean didRewrite = false;
    for (NormalRule inputProgramRule : inputProgram.getRules()) {
        if (containsArithmeticTermsToRewrite(inputProgramRule)) {
            rewrittenRules.add(rewriteRule(inputProgramRule));
            didRewrite = true;
        } else {
            // Keep rule as-is if no ArithmeticTerm occurs.
            rewrittenRules.add(inputProgramRule);
        }
    }
    if (!didRewrite) {
        return inputProgram;
    }
    // Create new program with rewritten rules.
    return new NormalProgramImpl(rewrittenRules, inputProgram.getFacts(), inputProgram.getInlineDirectives());
}
Also used : NormalProgramImpl(at.ac.tuwien.kr.alpha.core.programs.NormalProgramImpl) ArrayList(java.util.ArrayList) NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule)

Example 2 with NormalRule

use of at.ac.tuwien.kr.alpha.api.rules.NormalRule in project Alpha by alpha-asp.

the class IntervalTermToIntervalAtom method apply.

@Override
public NormalProgram apply(NormalProgram inputProgram) {
    boolean didChange = false;
    List<NormalRule> rewrittenRules = new ArrayList<>();
    for (NormalRule rule : inputProgram.getRules()) {
        NormalRule rewrittenRule = rewriteIntervalSpecifications(rule);
        rewrittenRules.add(rewrittenRule);
        // If no rewriting occurred, the output rule is the same as the input to the rewriting.
        if (rewrittenRule != rule) {
            didChange = true;
        }
    }
    // Return original program if no rule was actually rewritten.
    if (!didChange) {
        return inputProgram;
    }
    return new NormalProgramImpl(rewrittenRules, inputProgram.getFacts(), inputProgram.getInlineDirectives());
}
Also used : NormalProgramImpl(at.ac.tuwien.kr.alpha.core.programs.NormalProgramImpl) ArrayList(java.util.ArrayList) NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule)

Example 3 with NormalRule

use of at.ac.tuwien.kr.alpha.api.rules.NormalRule in project Alpha by alpha-asp.

the class ArithmeticTermsRewritingTest method rewriteRule.

@Test
public void rewriteRule() {
    NormalProgram inputProgram = NormalProgramImpl.fromInputProgram(parser.parse("p(X+1) :- q(Y/2), r(f(X*2),Y), X-2 = Y*3, X = 0..9."));
    assertEquals(1, inputProgram.getRules().size());
    ArithmeticTermsRewriting arithmeticTermsRewriting = new ArithmeticTermsRewriting();
    NormalProgram rewrittenProgram = arithmeticTermsRewriting.apply(inputProgram);
    // Expect the rewritten program to be one rule with: p(_A0) :- _A0 = X+1,  _A1 = Y/2, q(_A1), _A2 = X*2, r(f(_A2),Y), X-2 = Y*3, X = 0..9.
    assertEquals(1, rewrittenProgram.getRules().size());
    NormalRule rewrittenRule = rewrittenProgram.getRules().get(0);
    assertTrue(rewrittenRule.getHeadAtom().getTerms().get(0) instanceof VariableTerm);
    assertEquals(7, rewrittenRule.getBody().size());
}
Also used : NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule) Test(org.junit.jupiter.api.Test)

Example 4 with NormalRule

use of at.ac.tuwien.kr.alpha.api.rules.NormalRule in project Alpha by alpha-asp.

the class ArithmeticTermsRewritingTest method rewriteExternalAtom.

@Test
public void rewriteExternalAtom() {
    NormalProgram inputProgram = NormalProgramImpl.fromInputProgram(parser.parse("p :- Y = 13, &extArithTest[Y*5](Y-4)."));
    assertEquals(1, inputProgram.getRules().size());
    ArithmeticTermsRewriting arithmeticTermsRewriting = new ArithmeticTermsRewriting();
    NormalProgram rewrittenProgram = arithmeticTermsRewriting.apply(inputProgram);
    assertEquals(1, rewrittenProgram.getRules().size());
    NormalRule rewrittenRule = rewrittenProgram.getRules().get(0);
    assertEquals(4, rewrittenRule.getBody().size());
    List<Literal> externalLiterals = rewrittenRule.getBody().stream().filter(lit -> lit instanceof ExternalLiteral).collect(toList());
    assertEquals(1, externalLiterals.size());
    ExternalAtom rewrittenExternal = ((ExternalLiteral) externalLiterals.get(0)).getAtom();
    assertEquals(1, rewrittenExternal.getInput().size());
    assertTrue(rewrittenExternal.getInput().get(0) instanceof VariableTerm);
    assertEquals(1, rewrittenExternal.getOutput().size());
    assertTrue(rewrittenExternal.getOutput().get(0) instanceof VariableTerm);
}
Also used : NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) Set(java.util.Set) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Predicate(at.ac.tuwien.kr.alpha.api.externals.Predicate) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Externals(at.ac.tuwien.kr.alpha.core.externals.Externals) Test(org.junit.jupiter.api.Test) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) NormalProgramImpl(at.ac.tuwien.kr.alpha.core.programs.NormalProgramImpl) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Test(org.junit.jupiter.api.Test)

Aggregations

NormalRule (at.ac.tuwien.kr.alpha.api.rules.NormalRule)4 NormalProgramImpl (at.ac.tuwien.kr.alpha.core.programs.NormalProgramImpl)3 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)2 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 PredicateInterpretation (at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation)1 Predicate (at.ac.tuwien.kr.alpha.api.externals.Predicate)1 ProgramParser (at.ac.tuwien.kr.alpha.api.programs.ProgramParser)1 ExternalAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom)1 ExternalLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral)1 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)1 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)1 Terms (at.ac.tuwien.kr.alpha.commons.terms.Terms)1 Externals (at.ac.tuwien.kr.alpha.core.externals.Externals)1 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1