Skip to content

Commit f338eeb

Browse files
committed
Evolog Modules: Permit external atom interpretations to return Set<List<Term>> rather than just Set<List<ConstantTerm>> so we can have module interpretations returning function terms
1 parent 60b819f commit f338eeb

File tree

7 files changed

+18
-31
lines changed

7 files changed

+18
-31
lines changed

alpha-api/src/main/java/at/ac/tuwien/kr/alpha/api/common/fixedinterpretations/PredicateInterpretation.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@
3737

3838
@FunctionalInterface
3939
public interface PredicateInterpretation {
40-
Set<List<ConstantTerm<?>>> TRUE = singleton(emptyList());
41-
Set<List<ConstantTerm<?>>> FALSE = emptySet();
40+
41+
Set<List<Term>> TRUE = singleton(emptyList());
42+
Set<List<Term>> FALSE = emptySet();
4243

4344
String EVALUATE_RETURN_TYPE_NAME_PREFIX = Set.class.getName() + "<" + List.class.getName() + "<" + ConstantTerm.class.getName();
4445

45-
Set<List<ConstantTerm<?>>> evaluate(List<Term> terms);
46+
Set<List<Term>> evaluate(List<Term> terms);
4647
}

alpha-commons/src/main/java/at/ac/tuwien/kr/alpha/commons/externals/BindingMethodPredicateInterpretation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public BindingMethodPredicateInterpretation(Method method) {
5252

5353
@Override
5454
@SuppressWarnings("unchecked")
55-
public Set<List<ConstantTerm<?>>> evaluate(List<Term> terms) {
55+
public Set<List<Term>> evaluate(List<Term> terms) {
5656
if (terms.size() != method.getParameterCount()) {
5757
throw new IllegalArgumentException(
5858
"Parameter count mismatch when calling " + method.getName() + ". " +
@@ -90,7 +90,7 @@ public Set<List<ConstantTerm<?>>> evaluate(List<Term> terms) {
9090
}
9191

9292
try {
93-
return (Set<List<ConstantTerm<?>>>) method.invoke(null, arguments);
93+
return (Set<List<Term>>) method.invoke(null, arguments);
9494
} catch (IllegalAccessException | InvocationTargetException ex) {
9595
throw new RuntimeException("Error invoking method " + method + "with args [" + StringUtils.join(arguments) + "], expection is: " + ex.getMessage());
9696
}

alpha-commons/src/main/java/at/ac/tuwien/kr/alpha/commons/externals/Externals.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import at.ac.tuwien.kr.alpha.api.externals.Predicate;
3030
import at.ac.tuwien.kr.alpha.api.programs.atoms.Atom;
3131
import at.ac.tuwien.kr.alpha.api.programs.terms.ConstantTerm;
32+
import at.ac.tuwien.kr.alpha.api.programs.terms.Term;
3233
import at.ac.tuwien.kr.alpha.commons.Predicates;
3334
import at.ac.tuwien.kr.alpha.commons.programs.atoms.Atoms;
3435
import at.ac.tuwien.kr.alpha.commons.programs.terms.Terms;
@@ -114,7 +115,7 @@ public static <T, U> PredicateInterpretation processPredicate(java.util.function
114115
return new BinaryPredicateInterpretation<>(predicate);
115116
}
116117

117-
public static PredicateInterpretation processPredicate(java.util.function.Supplier<Set<List<ConstantTerm<?>>>> supplier) {
118+
public static PredicateInterpretation processPredicate(java.util.function.Supplier<Set<List<Term>>> supplier) {
118119
return new SuppliedPredicateInterpretation(supplier);
119120
}
120121

alpha-commons/src/main/java/at/ac/tuwien/kr/alpha/commons/externals/NonBindingPredicateInterpretation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public NonBindingPredicateInterpretation() {
5151
}
5252

5353
@Override
54-
public Set<List<ConstantTerm<?>>> evaluate(List<Term> terms) {
54+
public Set<List<Term>> evaluate(List<Term> terms) {
5555
if (terms.size() != arity) {
5656
throw new IllegalArgumentException("Exactly " + arity + " term(s) required.");
5757
}

alpha-commons/src/main/java/at/ac/tuwien/kr/alpha/commons/externals/PredicateInterpretationImpl.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

alpha-commons/src/main/java/at/ac/tuwien/kr/alpha/commons/externals/SuppliedPredicateInterpretation.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,21 @@
2828
package at.ac.tuwien.kr.alpha.commons.externals;
2929

3030
import at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.BindingPredicateInterpretation;
31-
import at.ac.tuwien.kr.alpha.api.programs.terms.ConstantTerm;
3231
import at.ac.tuwien.kr.alpha.api.programs.terms.Term;
3332

3433
import java.util.List;
3534
import java.util.Set;
3635
import java.util.function.Supplier;
3736

3837
public class SuppliedPredicateInterpretation implements BindingPredicateInterpretation {
39-
private final Supplier<Set<List<ConstantTerm<?>>>> supplier;
38+
private final Supplier<Set<List<Term>>> supplier;
4039

41-
public SuppliedPredicateInterpretation(Supplier<Set<List<ConstantTerm<?>>>> supplier) {
40+
public SuppliedPredicateInterpretation(Supplier<Set<List<Term>>> supplier) {
4241
this.supplier = supplier;
4342
}
4443

4544
@Override
46-
public Set<List<ConstantTerm<?>>> evaluate(List<Term> terms) {
45+
public Set<List<Term>> evaluate(List<Term> terms) {
4746
if (!terms.isEmpty()) {
4847
throw new IllegalArgumentException("Can only be used without any arguments.");
4948
}

alpha-commons/src/main/java/at/ac/tuwien/kr/alpha/commons/programs/literals/ExternalLiteralImpl.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public List<Substitution> getSatisfyingSubstitutions(Substitution partialSubstit
127127
for (Term t : input) {
128128
substitutes.add(t.substitute(partialSubstitution));
129129
}
130-
Set<List<ConstantTerm<?>>> results = getAtom().getInterpretation().evaluate(substitutes);
130+
Set<List<Term>> results = getAtom().getInterpretation().evaluate(substitutes);
131+
// TODO verify all results are ground
131132
if (results == null) {
132133
throw new NullPointerException("Predicate " + getPredicate().getName() + " returned null. It must return a Set.");
133134
}
@@ -160,10 +161,10 @@ public List<Substitution> getSatisfyingSubstitutions(Substitution partialSubstit
160161
* @return true iff no list in externalMethodResult equals the external atom's output term
161162
* list as substituted by the grounder, false otherwise
162163
*/
163-
private boolean isNegatedLiteralSatisfied(Set<List<ConstantTerm<?>>> externalMethodResult) {
164+
private boolean isNegatedLiteralSatisfied(Set<List<Term>> externalMethodResult) {
164165
List<Term> externalAtomOutTerms = this.getAtom().getOutput();
165166
boolean outputMatches;
166-
for (List<ConstantTerm<?>> resultTerms : externalMethodResult) {
167+
for (List<Term> resultTerms : externalMethodResult) {
167168
outputMatches = true;
168169
for (int i = 0; i < externalAtomOutTerms.size(); i++) {
169170
if (!resultTerms.get(i).equals(externalAtomOutTerms.get(i))) {
@@ -182,10 +183,10 @@ private boolean isNegatedLiteralSatisfied(Set<List<ConstantTerm<?>>> externalMet
182183
return true;
183184
}
184185

185-
private List<Substitution> buildSubstitutionsForOutputs(Substitution partialSubstitution, Set<List<ConstantTerm<?>>> outputs) {
186+
private List<Substitution> buildSubstitutionsForOutputs(Substitution partialSubstitution, Set<List<Term>> outputs) {
186187
List<Substitution> retVal = new ArrayList<>();
187188
List<Term> externalAtomOutputTerms = this.getAtom().getOutput();
188-
for (List<ConstantTerm<?>> bindings : outputs) {
189+
for (List<Term> bindings : outputs) {
189190
if (bindings.size() < externalAtomOutputTerms.size()) {
190191
throw new RuntimeException(
191192
"Predicate " + getPredicate().getName() + " returned " + bindings.size() + " terms when at least " + externalAtomOutputTerms.size()

0 commit comments

Comments
 (0)