use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class AnswerSetQueryTest method matchEvenIntegers.
@Test
public void matchEvenIntegers() {
AnswerSetBuilder bld = new AnswerSetBuilder();
bld.predicate("p").instance(1).instance(2).instance(3).instance(4).instance(5).instance("bla").symbolicInstance("blubb");
AnswerSet as = bld.build();
java.util.function.Predicate<Term> isInteger = (term) -> {
if (!(term instanceof ConstantTerm<?>)) {
return false;
}
String strValue = ((ConstantTerm<?>) term).getObject().toString();
return strValue.matches("[0-9]+");
};
AnswerSetQueryImpl evenIntegers = AnswerSetQueryImpl.forPredicate(Predicates.getPredicate("p", 1)).withFilter(0, isInteger.and((term) -> Integer.valueOf(((ConstantTerm<?>) term).getObject().toString()) % 2 == 0));
List<Atom> queryResult = as.query(evenIntegers);
assertEquals(2, queryResult.size());
for (Atom atom : queryResult) {
ConstantTerm<?> term = (ConstantTerm<?>) atom.getTerms().get(0);
assertTrue(Integer.valueOf(term.getObject().toString()) % 2 == 0);
}
}
use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class ExternalLiteralImpl method getNonBindingVariables.
@Override
public Set<VariableTerm> getNonBindingVariables() {
List<Term> input = getAtom().getInput();
List<Term> output = getAtom().getOutput();
// External atoms have their input always non-binding, since they cannot
// be queried without some concrete input.
Set<VariableTerm> nonbindingVariables = new HashSet<>();
for (Term term : input) {
nonbindingVariables.addAll(term.getOccurringVariables());
}
// non-binding.
if (this.isNegated()) {
for (Term out : output) {
if (out instanceof VariableTerm) {
nonbindingVariables.add((VariableTerm) out);
}
}
}
return nonbindingVariables;
}
use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class ExternalLiteralImpl method getSatisfyingSubstitutions.
@Override
public List<Substitution> getSatisfyingSubstitutions(Substitution partialSubstitution) {
List<Term> input = getAtom().getInput();
List<Term> substitutes = new ArrayList<>(input.size());
// to the partial substitution supplied by the grounder.
for (Term t : input) {
substitutes.add(t.substitute(partialSubstitution));
}
Set<List<ConstantTerm<?>>> results = getAtom().getInterpretation().evaluate(substitutes);
if (results == null) {
throw new NullPointerException("Predicate " + getPredicate().getName() + " returned null. It must return a Set.");
}
if (this.isNegated()) {
return this.isNegatedLiteralSatisfied(results) ? Collections.singletonList(partialSubstitution) : Collections.emptyList();
} else {
return this.buildSubstitutionsForOutputs(partialSubstitution, results);
}
}
use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class ExternalLiteralImpl method buildSubstitutionsForOutputs.
private List<Substitution> buildSubstitutionsForOutputs(Substitution partialSubstitution, Set<List<ConstantTerm<?>>> outputs) {
List<Substitution> retVal = new ArrayList<>();
List<Term> externalAtomOutputTerms = this.getAtom().getOutput();
for (List<ConstantTerm<?>> bindings : outputs) {
if (bindings.size() < externalAtomOutputTerms.size()) {
throw new RuntimeException("Predicate " + getPredicate().getName() + " returned " + bindings.size() + " terms when at least " + externalAtomOutputTerms.size() + " were expected.");
}
BasicSubstitution ith = new BasicSubstitution(partialSubstitution);
boolean skip = false;
for (int i = 0; i < externalAtomOutputTerms.size(); i++) {
Term out = externalAtomOutputTerms.get(i);
if (out instanceof VariableTerm) {
ith.put((VariableTerm) out, bindings.get(i));
} else {
if (!bindings.get(i).equals(out)) {
skip = true;
break;
}
}
}
if (!skip) {
retVal.add(ith);
}
}
return retVal;
}
use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class Unifier method mergeIntoLeft.
/**
* Merge substitution right into left as used in the AnalyzeUnjustified.
* Left mappings are seen as equalities, i.e.,
* if left has {@literal A -> B} and right has {@literal A -> t} then the result will have {@literal A -> t} and {@literal B -> t}.
* If both substitutions are inconsistent, i.e., {@literal A -> t1} in left and {@literal A -> t2} in right, then null is returned.
* @param left
* @param right
* @return
*/
public static Unifier mergeIntoLeft(Unifier left, Unifier right) {
// Note: we assume both substitutions are free of chains, i.e., no A->B, B->C but A->C, B->C.
Unifier ret = new Unifier(left);
for (Map.Entry<VariableTerm, Term> mapping : right.substitution.entrySet()) {
VariableTerm variable = mapping.getKey();
Term term = mapping.getValue();
// If variable is unset, simply add.
if (!ret.isVariableSet(variable)) {
ret.put(variable, term);
continue;
}
// Variable is already set.
Term setTerm = ret.eval(variable);
if (setTerm instanceof VariableTerm) {
// Variable maps to another variable in left.
// Add a new mapping of the setTerm variable into our right-assigned term.
ret.put((VariableTerm) setTerm, term);
// Note: Unifier.put takes care of resolving the chain variable->setTerm->term.
continue;
}
// Check for inconsistency.
if (setTerm != term) {
return null;
}
// Now setTerm equals term, no action needed.
}
return ret;
}
Aggregations