Search in sources :

Example 21 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class IntervalTermToIntervalAtom method rewriteIntervalSpecifications.

/**
 * Rewrites intervals into a new variable and special IntervalAtom.
 *
 * @return true if some interval occurs in the rule.
 */
private static NormalRule rewriteIntervalSpecifications(NormalRule rule) {
    // Collect all intervals and replace them with variables.
    Map<VariableTerm, IntervalTerm> intervalReplacements = new LinkedHashMap<>();
    List<Literal> rewrittenBody = new ArrayList<>();
    for (Literal literal : rule.getBody()) {
        Literal rewrittenLiteral = rewriteLiteral(literal, intervalReplacements);
        if (rewrittenLiteral != null) {
            rewrittenBody.add(rewrittenLiteral);
        }
    }
    // Note that this cast is safe: NormalHead can only have a BasicAtom, so literalizing and getting back the Atom destroys type information,
    // but should never yield anything other than a BasicAtom
    NormalHead rewrittenHead = rule.isConstraint() ? null : Heads.newNormalHead((BasicAtom) rewriteLiteral(rule.getHead().getAtom().toLiteral(), intervalReplacements).getAtom());
    // If intervalReplacements is empty, no IntervalTerms have been found, keep rule as is.
    if (intervalReplacements.isEmpty()) {
        return rule;
    }
    // Add new IntervalAtoms representing the interval specifications.
    for (Map.Entry<VariableTerm, IntervalTerm> interval : intervalReplacements.entrySet()) {
        rewrittenBody.add(new IntervalAtom(interval.getValue(), interval.getKey()).toLiteral());
    }
    return new NormalRuleImpl(rewrittenHead, rewrittenBody);
}
Also used : IntervalAtom(at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ArrayList(java.util.ArrayList) NormalRuleImpl(at.ac.tuwien.kr.alpha.core.rules.NormalRuleImpl) LinkedHashMap(java.util.LinkedHashMap) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 22 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class LiteralBindingNonBindingVariablesTest method testNegativeBasicLiteral.

@Test
public void testNegativeBasicLiteral() {
    Literal literal = parser.parse("p(X,Y) :- q(X,Y), not r(X,Y).").getRules().get(0).getNegativeBody().stream().findFirst().get();
    assertEquals(true, literal.isNegated());
    expectVariables(literal.getBindingVariables());
    expectVariables(literal.getNonBindingVariables(), "X", "Y");
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Test(org.junit.jupiter.api.Test)

Example 23 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class LiteralBindingNonBindingVariablesTest method testNegativeComparisonLiteral_NEQ_RightAssigning.

@Test
public void testNegativeComparisonLiteral_NEQ_RightAssigning() {
    Literal literal = parser.parse("p(X) :- q(X,Y), not 5 != Y.").getRules().get(0).getNegativeBody().stream().findFirst().get();
    assertEquals(true, literal.isNegated());
    expectVariables(literal.getBindingVariables(), "Y");
    expectVariables(literal.getNonBindingVariables());
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Test(org.junit.jupiter.api.Test)

Example 24 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class LiteralBindingNonBindingVariablesTest method testNegativeComparisonLiteral_EQ_Bidirectional.

@Test
public void testNegativeComparisonLiteral_EQ_Bidirectional() {
    Literal literal = parser.parse("p(X) :- q(X,Y), not X = Y.").getRules().get(0).getNegativeBody().stream().findFirst().get();
    assertEquals(true, literal.isNegated());
    expectVariables(literal.getBindingVariables());
    expectVariables(literal.getNonBindingVariables(), "X", "Y");
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Test(org.junit.jupiter.api.Test)

Example 25 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class LiteralBindingNonBindingVariablesTest method testNegativeComparisonLiteral_EQ_RightAssigning.

@Test
public void testNegativeComparisonLiteral_EQ_RightAssigning() {
    Literal literal = parser.parse("p(X) :- q(X,Y), not 5 = Y.").getRules().get(0).getNegativeBody().stream().findFirst().get();
    assertEquals(true, literal.isNegated());
    expectVariables(literal.getBindingVariables());
    expectVariables(literal.getNonBindingVariables(), "Y");
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Test(org.junit.jupiter.api.Test)

Aggregations

Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)82 Test (org.junit.jupiter.api.Test)42 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)20 ArrayList (java.util.ArrayList)20 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)17 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)16 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)14 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)14 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)14 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)13 Map (java.util.Map)12 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)11 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)11 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)10 HashSet (java.util.HashSet)10 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)9 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)9 LinkedHashSet (java.util.LinkedHashSet)8 List (java.util.List)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)7