Search in sources :

Example 26 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class ArithmeticTermImpl method normalizeVariables.

@Override
public Term normalizeVariables(String renamePrefix, Term.RenameCounter counter) {
    Term normalizedLeft = left.normalizeVariables(renamePrefix, counter);
    Term normalizedRight = right.normalizeVariables(renamePrefix, counter);
    return ArithmeticTermImpl.getInstance(normalizedLeft, arithmeticOperator, normalizedRight);
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)

Example 27 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class IndexedInstanceStorage method getMostSelectiveGroundTermPosition.

private int getMostSelectiveGroundTermPosition(Atom atom) {
    int smallestNumberOfInstances = Integer.MAX_VALUE;
    int mostSelectiveTermPosition = -1;
    for (int i = 0; i < atom.getTerms().size(); i++) {
        Term testTerm = atom.getTerms().get(i);
        if (testTerm.isGround()) {
            ArrayList<Instance> instancesMatchingTest = indices.get(i).get(testTerm);
            if (instancesMatchingTest == null) {
                // Ground term at i matches zero instances, it is most selective.
                return i;
            }
            int numInstancesTestTerm = instancesMatchingTest.size();
            if (numInstancesTestTerm < smallestNumberOfInstances) {
                smallestNumberOfInstances = numInstancesTestTerm;
                mostSelectiveTermPosition = i;
            }
        }
    }
    return mostSelectiveTermPosition;
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) Term(at.ac.tuwien.kr.alpha.api.terms.Term)

Example 28 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class TestMinusTerm method testNormalizeVariablesNoVariable.

@Test
public void testNormalizeVariablesNoVariable() {
    Term m2 = MinusTerm.getInstance(ConstantTermImpl.getInstance(2));
    assertEquals(m2, m2.normalizeVariables(renamePrefix, counter));
}
Also used : Term(at.ac.tuwien.kr.alpha.api.terms.Term) MinusTerm(at.ac.tuwien.kr.alpha.commons.terms.ArithmeticTermImpl.MinusTerm) Test(org.junit.jupiter.api.Test)

Example 29 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class TestMinusTerm method testNormalizeVariablesWithVariable.

@Test
public void testNormalizeVariablesWithVariable() {
    Term mX = MinusTerm.getInstance(VariableTermImpl.getInstance("X"));
    Term expected = MinusTerm.getInstance(VariableTermImpl.getInstance(renamePrefix + 0));
    assertEquals(expected, mX.normalizeVariables(renamePrefix, counter));
}
Also used : Term(at.ac.tuwien.kr.alpha.api.terms.Term) MinusTerm(at.ac.tuwien.kr.alpha.commons.terms.ArithmeticTermImpl.MinusTerm) Test(org.junit.jupiter.api.Test)

Example 30 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class IntervalLiteral method getIntervalSubstitutions.

private List<Substitution> getIntervalSubstitutions(Substitution partialSubstitution) {
    List<Substitution> substitutions = new ArrayList<>();
    List<Term> terms = getTerms();
    Term intervalRepresentingVariable = terms.get(1);
    IntervalTerm intervalTerm = (IntervalTerm) terms.get(0);
    // Check whether intervalRepresentingVariable is bound already.
    if (intervalRepresentingVariable instanceof VariableTerm) {
        // Still a variable, generate all elements in the interval.
        for (int i = intervalTerm.getLowerBound(); i <= intervalTerm.getUpperBound(); i++) {
            Substitution ith = new BasicSubstitution(partialSubstitution);
            ith.put((VariableTerm) intervalRepresentingVariable, Terms.newConstant(i));
            substitutions.add(ith);
        }
        return substitutions;
    } else {
        // The intervalRepresentingVariable is bound already, check if it is in the interval.
        if (!(intervalRepresentingVariable instanceof ConstantTerm) || !(((ConstantTerm<?>) intervalRepresentingVariable).getObject() instanceof Integer)) {
            // Term is not bound to an integer constant, not in the interval.
            return Collections.emptyList();
        }
        // TODO to avoid that type of unchecked cast, we may want interval terms to not extend AbstractTerm
        @SuppressWarnings("unchecked") Integer integer = ((ConstantTerm<Integer>) intervalRepresentingVariable).getObject();
        if (intervalTerm.getLowerBound() <= integer && integer <= intervalTerm.getUpperBound()) {
            return Collections.singletonList(partialSubstitution);
        }
        return Collections.emptyList();
    }
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ArrayList(java.util.ArrayList) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Aggregations

Term (at.ac.tuwien.kr.alpha.api.terms.Term)52 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)36 ArrayList (java.util.ArrayList)16 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)15 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)15 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)12 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)10 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)9 Test (org.junit.jupiter.api.Test)8 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)7 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)6 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)6 HashSet (java.util.HashSet)6 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)4 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)4 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)3 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)3 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)3 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)3 MinusTerm (at.ac.tuwien.kr.alpha.commons.terms.ArithmeticTermImpl.MinusTerm)3