Skip to content

Commit

Permalink
Evolog Modules: Unit test for ModuleAtomImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
madmike200590 committed Jul 24, 2024
1 parent 49233d9 commit 90d3faf
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,34 @@ public interface ModuleAtom extends Atom {
@Override
ModuleAtom substitute(Substitution substitution);

@Override
ModuleAtom withTerms(List<Term> terms);

interface ModuleInstantiationMode {
Optional<Integer> requestedAnswerSets();

ModuleInstantiationMode ALL = Optional::empty;

static ModuleInstantiationMode forNumAnswerSets(int answerSets) {
return () -> Optional.of(answerSets);
return new ModuleInstantiationMode() {
@Override
public Optional<Integer> requestedAnswerSets() {
return Optional.of(answerSets);
}

@Override
public int hashCode() {
return answerSets;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof ModuleInstantiationMode)) {
return false;
}
return ((ModuleInstantiationMode) obj).requestedAnswerSets().equals(this.requestedAnswerSets());
}
};
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand Down Expand Up @@ -50,7 +49,7 @@ public ModuleInstantiationMode getInstantiationMode() {
}

@Override
public Atom withTerms(List<Term> terms) {
public ModuleAtom 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!");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package at.ac.tuwien.kr.alpha.commons.programs.atoms;

import java.util.List;

import at.ac.tuwien.kr.alpha.api.programs.atoms.ModuleAtom;
import at.ac.tuwien.kr.alpha.commons.programs.terms.Terms;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class ModuleAtomImplTest {

@Test
public void withTerms() {
ModuleAtom moduleAtom = new ModuleAtomImpl("someModule",
List.of(Terms.newVariable("X"), Terms.newVariable("Y")),
List.of(Terms.newVariable("Z")), ModuleAtom.ModuleInstantiationMode.ALL);
ModuleAtom newModuleAtom = moduleAtom.withTerms(List.of(Terms.newConstant(1), Terms.newConstant(2), Terms.newConstant(3)));
// Check correct construction of original atom (also check that withTerms didn't modify the original atom)
assertEquals(moduleAtom.getInput().size(), 2);
assertEquals(moduleAtom.getOutput().size(), 1);
assertEquals(moduleAtom.getModuleName(), "someModule");
assertEquals(moduleAtom.getInstantiationMode(), ModuleAtom.ModuleInstantiationMode.ALL);
// Check terms of new atom
assertEquals(newModuleAtom.getInput().size(), 2);
assertEquals(newModuleAtom.getOutput().size(), 1);
assertEquals(newModuleAtom.getModuleName(), "someModule");
assertEquals(newModuleAtom.getInstantiationMode(), ModuleAtom.ModuleInstantiationMode.ALL);
assertEquals(List.of(Terms.newConstant(1), Terms.newConstant(2)), newModuleAtom.getInput());
assertEquals(List.of(Terms.newConstant(3)), newModuleAtom.getOutput());
}

@Test
public void withTermsNewTermsTooLong() {
ModuleAtom moduleAtom = new ModuleAtomImpl("someModule",
List.of(Terms.newVariable("X"), Terms.newVariable("Y")),
List.of(Terms.newVariable("Z")), ModuleAtom.ModuleInstantiationMode.ALL);
assertThrows(IllegalArgumentException.class,
() -> moduleAtom.withTerms(
List.of(Terms.newConstant(1), Terms.newConstant(2),
Terms.newConstant(3), Terms.newConstant(4))));
}

@Test
public void withTermsNewTermsTooShort() {
ModuleAtom moduleAtom = new ModuleAtomImpl("someModule",
List.of(Terms.newVariable("X"), Terms.newVariable("Y")),
List.of(Terms.newVariable("Z")), ModuleAtom.ModuleInstantiationMode.ALL);
assertThrows(IllegalArgumentException.class,
() -> moduleAtom.withTerms(List.of(Terms.newConstant(1))));
}

@Test
public void moduleAtomsEqual() {
ModuleAtom m1 = new ModuleAtomImpl("someModule",
List.of(Terms.newVariable("X"), Terms.newVariable("Y")),
List.of(Terms.newVariable("Z")), ModuleAtom.ModuleInstantiationMode.ALL);
ModuleAtom m2 = new ModuleAtomImpl("someModule",
List.of(Terms.newVariable("X"), Terms.newVariable("Y")),
List.of(Terms.newVariable("Z")), ModuleAtom.ModuleInstantiationMode.ALL);
assertEquals(m1, m2);
assertEquals(m1.hashCode(), m2.hashCode());

ModuleAtom m3 = new ModuleAtomImpl("someModule",
List.of(Terms.newVariable("X"), Terms.newVariable("Y")),
List.of(Terms.newVariable("Z")), ModuleAtom.ModuleInstantiationMode.forNumAnswerSets(3));
ModuleAtom m4 = new ModuleAtomImpl("someModule",
List.of(Terms.newVariable("X"), Terms.newVariable("Y")),
List.of(Terms.newVariable("Z")), ModuleAtom.ModuleInstantiationMode.forNumAnswerSets(3));
assertNotEquals(m1, m3);
assertEquals(m3, m4);
}

}

0 comments on commit 90d3faf

Please sign in to comment.