use of at.ac.tuwien.kr.alpha.api.terms.ConstantTerm in project Alpha by alpha-asp.
the class ArithmeticTermsRewriting method rewriteArithmeticSubterms.
private Term rewriteArithmeticSubterms(Term term, List<Literal> bodyLiterals) {
// Keep term as-is if it contains no ArithmeticTerm.
if (!containsArithmeticTerm(term)) {
return term;
}
// Switch on term type.
if (term instanceof ArithmeticTerm) {
VariableTerm replacementVariable = Terms.newVariable(ARITHMETIC_VARIABLES_PREFIX + numArithmeticVariables++);
bodyLiterals.add(Atoms.newComparisonAtom(replacementVariable, term, ComparisonOperators.EQ).toLiteral());
return replacementVariable;
} else if (term instanceof VariableTerm || term instanceof ConstantTerm) {
return term;
} else if (term instanceof FunctionTerm) {
List<Term> termList = ((FunctionTerm) term).getTerms();
List<Term> rewrittenTermList = new ArrayList<>();
for (Term subterm : termList) {
rewrittenTermList.add(rewriteArithmeticSubterms(subterm, bodyLiterals));
}
return Terms.newFunctionTerm(((FunctionTerm) term).getSymbol(), rewrittenTermList);
} else {
throw Util.oops("Rewriting unknown Term type: " + term.getClass());
}
}
use of at.ac.tuwien.kr.alpha.api.terms.ConstantTerm in project Alpha by alpha-asp.
the class AlphaImplTest method programWithExternalStringStuff.
@Test
@SuppressWarnings("unchecked")
public void programWithExternalStringStuff() throws IOException {
Alpha alpha = new AlphaImpl();
ASPCore2Program prog = alpha.readProgram(InputConfig.forString(STRINGSTUFF_ASP));
Set<AnswerSet> answerSets = alpha.solve(prog).collect(Collectors.toSet());
// Verify every result string has length 6 and contains "foo"
for (AnswerSet as : answerSets) {
for (Atom atom : as.getPredicateInstances(Predicates.getPredicate("resultstring", 1))) {
String resultstring = ((ConstantTerm<String>) atom.getTerms().get(0)).getObject();
assertEquals(6, resultstring.length());
assertTrue(resultstring.contains("foo"));
}
}
}
use of at.ac.tuwien.kr.alpha.api.terms.ConstantTerm 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.ConstantTerm 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.ConstantTerm 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;
}
Aggregations