-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Evolog Modules: Parsing of module literals
- Loading branch information
1 parent
2b4f19d
commit 49233d9
Showing
13 changed files
with
446 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
alpha-api/src/main/java/at/ac/tuwien/kr/alpha/api/programs/atoms/ModuleAtom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package at.ac.tuwien.kr.alpha.api.programs.atoms; | ||
|
||
import at.ac.tuwien.kr.alpha.api.grounder.Substitution; | ||
import at.ac.tuwien.kr.alpha.api.programs.terms.Term; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* An atom that is implemented using an additional ASP program (i.e. a module). | ||
* Note that a module atom itself can not be instantiated, but needs to be compiled | ||
* into some kind of instantiable atom by linking it to an ASP program that implements | ||
* the referenced module. | ||
*/ | ||
public interface ModuleAtom extends Atom { | ||
|
||
String getModuleName(); | ||
|
||
List<Term> getInput(); | ||
|
||
List<Term> getOutput(); | ||
|
||
ModuleInstantiationMode getInstantiationMode(); | ||
|
||
@Override | ||
ModuleAtom substitute(Substitution substitution); | ||
|
||
interface ModuleInstantiationMode { | ||
Optional<Integer> requestedAnswerSets(); | ||
|
||
ModuleInstantiationMode ALL = Optional::empty; | ||
|
||
static ModuleInstantiationMode forNumAnswerSets(int answerSets) { | ||
return () -> Optional.of(answerSets); | ||
} | ||
|
||
} | ||
|
||
} |
10 changes: 5 additions & 5 deletions
10
alpha-api/src/main/java/at/ac/tuwien/kr/alpha/api/programs/literals/Literal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
alpha-api/src/main/java/at/ac/tuwien/kr/alpha/api/programs/literals/ModuleLiteral.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package at.ac.tuwien.kr.alpha.api.programs.literals; | ||
|
||
import at.ac.tuwien.kr.alpha.api.programs.atoms.ModuleAtom; | ||
import at.ac.tuwien.kr.alpha.api.grounder.Substitution; | ||
|
||
public interface ModuleLiteral extends Literal{ | ||
|
||
@Override | ||
ModuleAtom getAtom(); | ||
|
||
@Override | ||
ModuleLiteral negate(); | ||
|
||
@Override | ||
ModuleLiteral substitute(Substitution substitution); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
alpha-commons/src/main/java/at/ac/tuwien/kr/alpha/commons/programs/atoms/ModuleAtomImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package at.ac.tuwien.kr.alpha.commons.programs.atoms; | ||
|
||
import at.ac.tuwien.kr.alpha.api.grounder.Substitution; | ||
import at.ac.tuwien.kr.alpha.api.programs.Predicate; | ||
import at.ac.tuwien.kr.alpha.api.programs.atoms.Atom; | ||
import at.ac.tuwien.kr.alpha.api.programs.atoms.ModuleAtom; | ||
import at.ac.tuwien.kr.alpha.api.programs.literals.ModuleLiteral; | ||
import at.ac.tuwien.kr.alpha.api.programs.terms.Term; | ||
import at.ac.tuwien.kr.alpha.commons.Predicates; | ||
import at.ac.tuwien.kr.alpha.commons.programs.literals.Literals; | ||
import at.ac.tuwien.kr.alpha.commons.util.Util; | ||
import org.apache.commons.collections4.ListUtils; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
class ModuleAtomImpl extends AbstractAtom implements ModuleAtom { | ||
|
||
private final String moduleName; | ||
private final List<Term> input; | ||
private final List<Term> output; | ||
private final ModuleInstantiationMode instantiationMode; | ||
|
||
ModuleAtomImpl(String moduleName, List<Term> input, List<Term> output, ModuleInstantiationMode instantiationMode) { | ||
this.moduleName = Objects.requireNonNull(moduleName); | ||
this.input = Objects.requireNonNull(input); | ||
this.output = Objects.requireNonNull(output); | ||
this.instantiationMode = Objects.requireNonNull(instantiationMode); | ||
} | ||
|
||
@Override | ||
public String getModuleName() { | ||
return moduleName; | ||
} | ||
|
||
@Override | ||
public List<Term> getInput() { | ||
return input; | ||
} | ||
|
||
@Override | ||
public List<Term> getOutput() { | ||
return output; | ||
} | ||
|
||
@Override | ||
public ModuleInstantiationMode getInstantiationMode() { | ||
return instantiationMode; | ||
} | ||
|
||
@Override | ||
public Atom withTerms(List<Term> terms) { | ||
if (terms.size() != this.input.size() + this.output.size()) { | ||
throw new IllegalArgumentException( | ||
"Cannot apply term list " + terms + " to module atom " + this + ", terms has invalid size!"); | ||
} | ||
List<Term> newInput = terms.subList(0, this.input.size()); | ||
List<Term> newOutput = terms.subList(this.input.size(), terms.size()); | ||
return new ModuleAtomImpl(this.moduleName, newInput, newOutput, this.instantiationMode); | ||
} | ||
|
||
@Override | ||
public ModuleAtom substitute(Substitution substitution) { | ||
List<Term> substitutedInput = this.input.stream().map(t -> t.substitute(substitution)).collect(Collectors.toList()); | ||
List<Term> substitutedOutput = this.output.stream().map(t -> t.substitute(substitution)).collect(Collectors.toList()); | ||
return new ModuleAtomImpl(this.moduleName, substitutedInput, substitutedOutput, this.instantiationMode); | ||
} | ||
|
||
@Override | ||
public List<Term> getTerms() { | ||
return ListUtils.union(input, output); | ||
} | ||
|
||
@Override | ||
public Predicate getPredicate() { | ||
return Predicates.getPredicate(moduleName, output.size()); | ||
} | ||
|
||
@Override | ||
public boolean isGround() { | ||
for (Term t : input) { | ||
if (!t.isGround()) { | ||
return false; | ||
} | ||
} | ||
for (Term t : output) { | ||
if (!t.isGround()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public ModuleLiteral toLiteral(boolean positive) { | ||
return Literals.fromAtom(this, positive); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
ModuleAtomImpl that = (ModuleAtomImpl) o; | ||
return Objects.equals(moduleName, that.moduleName) | ||
&& Objects.equals(input, that.input) | ||
&& Objects.equals(output, that.output) | ||
&& Objects.equals(instantiationMode, that.instantiationMode); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(moduleName, input, output, instantiationMode); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String result = "#" + moduleName; | ||
if (!input.isEmpty()) { | ||
result += Util.join("[", input, "]"); | ||
} | ||
if (!output.isEmpty()) { | ||
result += Util.join("(", output, ")"); | ||
} | ||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...mons/src/main/java/at/ac/tuwien/kr/alpha/commons/programs/literals/ModuleLiteralImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package at.ac.tuwien.kr.alpha.commons.programs.literals; | ||
|
||
import at.ac.tuwien.kr.alpha.api.grounder.Substitution; | ||
import at.ac.tuwien.kr.alpha.api.programs.atoms.ModuleAtom; | ||
import at.ac.tuwien.kr.alpha.api.programs.literals.ModuleLiteral; | ||
import at.ac.tuwien.kr.alpha.api.programs.terms.Term; | ||
import at.ac.tuwien.kr.alpha.api.programs.terms.VariableTerm; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
class ModuleLiteralImpl extends AbstractLiteral implements ModuleLiteral { | ||
|
||
ModuleLiteralImpl(ModuleAtom atom, boolean positive) { | ||
super(atom, positive); | ||
} | ||
|
||
@Override | ||
public ModuleAtom getAtom() { | ||
return (ModuleAtom) atom; | ||
} | ||
|
||
@Override | ||
public ModuleLiteral negate() { | ||
return new ModuleLiteralImpl(getAtom(), isNegated()); | ||
} | ||
|
||
@Override | ||
public ModuleLiteral substitute(Substitution substitution) { | ||
return new ModuleLiteralImpl(getAtom().substitute(substitution), positive); | ||
} | ||
|
||
// TODO introduce common abstract supertype for external an module literals to avoid code duplication (same goes for atoms!) | ||
@Override | ||
public Set<VariableTerm> getBindingVariables() { | ||
// If the external atom is negative, then all variables of input and output are non-binding | ||
// and there are no binding variables (like for ordinary atoms). | ||
// If the external atom is positive, then variables of output are binding. | ||
|
||
if (this.isNegated()) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
List<Term> output = getAtom().getOutput(); | ||
|
||
Set<VariableTerm> binding = new HashSet<>(output.size()); | ||
|
||
for (Term out : output) { | ||
if (out instanceof VariableTerm) { | ||
binding.add((VariableTerm) out); | ||
} | ||
} | ||
|
||
return binding; | ||
} | ||
|
||
@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()); | ||
} | ||
|
||
// If the external atom is negative, then all variables of input and output are | ||
// non-binding. | ||
if (this.isNegated()) { | ||
for (Term out : output) { | ||
if (out instanceof VariableTerm) { | ||
nonbindingVariables.add((VariableTerm) out); | ||
} | ||
} | ||
} | ||
|
||
return nonbindingVariables; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.