Search in sources :

Example 31 with VariableTerm

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

the class EnumerationAtom method substitute.

@Override
public EnumerationAtom substitute(Substitution substitution) {
    Term substEnumIdTerm = enumIdTerm.substitute(substitution);
    Term substValueTerm = valueTerm.substitute(substitution);
    Term substIndexTerm = indexTerm.substitute(substitution);
    return new EnumerationAtom(substEnumIdTerm, substValueTerm, (VariableTerm) substIndexTerm);
}
Also used : Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 32 with VariableTerm

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

the class EnumerationAtom method addEnumerationIndexToSubstitution.

/**
 * Based on a given substitution, substitutes the first two terms of this {@link EnumerationAtom} with the values from the substitution,
 * and returns a new substitution with all mappings from the input substitution plus a binding for the third term of the enum atom to the
 * integer index that is mapped to the first two terms in the internal <code>ENUMERATIONS</code> map.
 *
 * @param substitution an input substitution which must provide ground terms for the first two terms of the enumeration atom.
 * @return a new substitution where the third term of the enumeration atom is bound to an integer.
 */
public Substitution addEnumerationIndexToSubstitution(Substitution substitution) {
    Term idTerm = this.getTerms().get(0).substitute(substitution);
    Term enumerationTerm = this.getTerms().get(1).substitute(substitution);
    if (!enumerationTerm.isGround()) {
        throw new RuntimeException("Enumeration term is not ground after substitution. Should not happen.");
    }
    Integer enumerationIndex = getEnumerationIndex(idTerm, enumerationTerm);
    BasicSubstitution retVal = new BasicSubstitution(substitution);
    retVal.put((VariableTerm) getTerms().get(2), Terms.newConstant(enumerationIndex));
    return retVal;
}
Also used : BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 33 with VariableTerm

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

the class EnumerationLiteral method getNonBindingVariables.

@Override
public Set<VariableTerm> getNonBindingVariables() {
    Set<VariableTerm> ret = new HashSet<>(2);
    Term idTerm = getTerms().get(0);
    Term enumTerm = getTerms().get(1);
    if (idTerm instanceof VariableTerm) {
        ret.add((VariableTerm) idTerm);
    }
    if (enumTerm instanceof VariableTerm) {
        ret.add((VariableTerm) enumTerm);
    }
    return ret;
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) HashSet(java.util.HashSet)

Example 34 with VariableTerm

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

the class VariableTermImpl method normalizeVariables.

@Override
public Term normalizeVariables(String renamePrefix, Term.RenameCounter counter) {
    VariableTerm renamedThis = counter.getRenamedVariables().get(this);
    if (renamedThis != null) {
        return renamedThis;
    } else {
        VariableTerm renamedVariable = VariableTermImpl.getInstance(renamePrefix + counter.getAndIncrement());
        counter.getRenamedVariables().put(this, renamedVariable);
        return renamedVariable;
    }
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 35 with VariableTerm

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

the class ComparisonLiteralImpl method getNonBindingVariables.

@Override
public Set<VariableTerm> getNonBindingVariables() {
    final Term left = getTerms().get(0);
    final Term right = getTerms().get(1);
    HashSet<VariableTerm> occurringVariables = new HashSet<>();
    List<VariableTerm> leftOccurringVariables = new LinkedList<>(left.getOccurringVariables());
    List<VariableTerm> rightOccurringVariables = new LinkedList<>(right.getOccurringVariables());
    if (assignable(left)) {
        leftOccurringVariables.remove(left);
    }
    if (assignable(right)) {
        rightOccurringVariables.remove(right);
    }
    occurringVariables.addAll(leftOccurringVariables);
    occurringVariables.addAll(rightOccurringVariables);
    if (assignable(left) || assignable(right)) {
        return occurringVariables;
    }
    // Neither left- nor right-assigning, hence no variable is binding.
    return occurringVariables;
}
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) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Aggregations

VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)45 Term (at.ac.tuwien.kr.alpha.api.terms.Term)25 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)18 Test (org.junit.jupiter.api.Test)13 ArrayList (java.util.ArrayList)12 HashSet (java.util.HashSet)10 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)9 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)8 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)6 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)6 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)6 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)6 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)6 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)6 Map (java.util.Map)6 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)5 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)5 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)4 AggregateAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)4 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)4