diff --git a/alpha-cli-app/src/main/java/at/ac/tuwien/kr/alpha/Main.java b/alpha-cli-app/src/main/java/at/ac/tuwien/kr/alpha/Main.java index 9fbaccc39..94a8eb285 100644 --- a/alpha-cli-app/src/main/java/at/ac/tuwien/kr/alpha/Main.java +++ b/alpha-cli-app/src/main/java/at/ac/tuwien/kr/alpha/Main.java @@ -86,7 +86,7 @@ public static void main(String[] args) { Main.exitWithMessage(commandLineParser.getUsageMessage(), 1); } - Alpha alpha = new AlphaFactory().newAlpha(cfg.getSystemConfig()); + Alpha alpha = AlphaFactory.newAlpha(cfg.getSystemConfig()); InputProgram program = null; try { diff --git a/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/ComponentGraphWriterTest.java b/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/ComponentGraphWriterTest.java index 9c5777159..23655f106 100644 --- a/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/ComponentGraphWriterTest.java +++ b/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/ComponentGraphWriterTest.java @@ -54,7 +54,7 @@ public void smokeTest() { "n1 -> n5 [xlabel=\"-\" labeldistance=0.1]" + LS + "n2 -> n5 [xlabel=\"+\" labeldistance=0.1]" + LS + "}" + LS; - Alpha alpha = new AlphaFactory().newAlpha(); + Alpha alpha = AlphaFactory.newAlpha(); DebugSolvingContext dbgResult = alpha.prepareDebugSolve(alpha.readProgramString(asp)); ComponentGraph compgraph = dbgResult.getComponentGraph(); ComponentGraphWriter writer = new ComponentGraphWriter(); diff --git a/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/DependencyGraphWriterTest.java b/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/DependencyGraphWriterTest.java index a738bf3e0..8d5929e80 100644 --- a/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/DependencyGraphWriterTest.java +++ b/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/DependencyGraphWriterTest.java @@ -51,7 +51,7 @@ public void smokeTest() { "n6 -> n4 [xlabel=\"+\" labeldistance=0.1]" + LS + "n6 -> n5 [xlabel=\"+\" labeldistance=0.1]" + LS + "}" + LS; - Alpha alpha = new AlphaFactory().newAlpha(); + Alpha alpha = AlphaFactory.newAlpha(); DebugSolvingContext dbgResult = alpha.prepareDebugSolve(alpha.readProgramString(asp)); DependencyGraph depgraph = dbgResult.getDependencyGraph(); DependencyGraphWriter writer = new DependencyGraphWriter(); diff --git a/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/mappers/AnswerSetToWorkbookMapperTest.java b/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/mappers/AnswerSetToWorkbookMapperTest.java index cafb17236..3b156426d 100644 --- a/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/mappers/AnswerSetToWorkbookMapperTest.java +++ b/alpha-cli-app/src/test/java/at/ac/tuwien/kr/alpha/app/mappers/AnswerSetToWorkbookMapperTest.java @@ -47,7 +47,7 @@ public void solveAndWriteWorkbookTest() { + "p(N) :- p(I), N = I + 1, N <= MX, maxP(MX)." + "q(A, B) :- p(A), p(B)."; //@formatter:on - Alpha alpha = new AlphaFactory().newAlpha(); + Alpha alpha = AlphaFactory.newAlpha(); List answerSets = alpha.solve(alpha.readProgramString(progstr)).collect(Collectors.toList()); assertEquals(1, answerSets.size()); AnswerSet as = answerSets.get(0); diff --git a/alpha-core/src/main/java/at/ac/tuwien/kr/alpha/core/parser/ProgramPartParser.java b/alpha-core/src/main/java/at/ac/tuwien/kr/alpha/core/parser/ProgramPartParser.java index 14a5bc1d9..b115e4cd9 100644 --- a/alpha-core/src/main/java/at/ac/tuwien/kr/alpha/core/parser/ProgramPartParser.java +++ b/alpha-core/src/main/java/at/ac/tuwien/kr/alpha/core/parser/ProgramPartParser.java @@ -43,7 +43,7 @@ import at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser; /** - * A parser that, in contrast to {@link ProgramParser}, does not parse full programs but only program parts like + * A parser that, in contrast to {@link at.ac.tuwien.kr.alpha.api.programs.ProgramParser}, does not parse full programs but only program parts like * atoms, terms and such. */ // TODO adapt this and create evolog version diff --git a/alpha-core/src/testFixtures/java/at/ac/tuwien/kr/alpha/test/AlphaAssertions.java b/alpha-core/src/testFixtures/java/at/ac/tuwien/kr/alpha/test/AlphaAssertions.java index 8c45bf662..cce21dafa 100644 --- a/alpha-core/src/testFixtures/java/at/ac/tuwien/kr/alpha/test/AlphaAssertions.java +++ b/alpha-core/src/testFixtures/java/at/ac/tuwien/kr/alpha/test/AlphaAssertions.java @@ -2,7 +2,6 @@ import at.ac.tuwien.kr.alpha.api.AnswerSet; -import at.ac.tuwien.kr.alpha.api.impl.AnswerSetsParser; import at.ac.tuwien.kr.alpha.api.programs.Program; import at.ac.tuwien.kr.alpha.api.programs.atoms.Atom; diff --git a/alpha-core/src/testFixtures/java/at/ac/tuwien/kr/alpha/test/AnswerSetsParser.java b/alpha-core/src/testFixtures/java/at/ac/tuwien/kr/alpha/test/AnswerSetsParser.java index 93515666b..34f60e7c5 100644 --- a/alpha-core/src/testFixtures/java/at/ac/tuwien/kr/alpha/test/AnswerSetsParser.java +++ b/alpha-core/src/testFixtures/java/at/ac/tuwien/kr/alpha/test/AnswerSetsParser.java @@ -1,4 +1,4 @@ -package at.ac.tuwien.kr.alpha.api.impl; +package at.ac.tuwien.kr.alpha.test; import java.util.Collections; import java.util.Set; diff --git a/alpha-solver/src/main/java/at/ac/tuwien/kr/alpha/api/impl/AlphaFactory.java b/alpha-solver/src/main/java/at/ac/tuwien/kr/alpha/api/impl/AlphaFactory.java index 53d5eb323..dcc85fdec 100644 --- a/alpha-solver/src/main/java/at/ac/tuwien/kr/alpha/api/impl/AlphaFactory.java +++ b/alpha-solver/src/main/java/at/ac/tuwien/kr/alpha/api/impl/AlphaFactory.java @@ -65,7 +65,7 @@ protected ActionImplementationProvider newActionImplementationProvider() { return new DefaultActionImplementationProvider(); } - public Alpha newAlpha(SystemConfig cfg) { + protected Alpha buildInstance(SystemConfig cfg) { ActionImplementationProvider actionImplementationProvider = newActionImplementationProvider(); ProgramParser parser = new ProgramParserImpl(actionImplementationProvider, Collections.emptyMap()); ProgramTransformation programNormalizer = new NormalizeProgramTransformation(cfg.getAggregateRewritingConfig()); @@ -87,8 +87,13 @@ public Alpha newAlpha(SystemConfig cfg) { } // Create Alpha instance with default config. - public Alpha newAlpha() { - return newAlpha(new SystemConfig()); + public static Alpha newAlpha() { + return AlphaFactory.newAlpha(new SystemConfig()); + } + + public static Alpha newAlpha(SystemConfig cfg) { + AlphaFactory factory = new AlphaFactory(); + return factory.buildInstance(cfg); } } diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/ActionsTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/ActionsTest.java index c00df590d..b52ab8273 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/ActionsTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/ActionsTest.java @@ -3,6 +3,7 @@ import at.ac.tuwien.kr.alpha.api.Alpha; import at.ac.tuwien.kr.alpha.api.AnswerSet; import at.ac.tuwien.kr.alpha.api.config.SystemConfig; +import at.ac.tuwien.kr.alpha.api.impl.AlphaFactory; import at.ac.tuwien.kr.alpha.api.programs.InputProgram; import at.ac.tuwien.kr.alpha.api.programs.terms.ConstantTerm; import at.ac.tuwien.kr.alpha.api.programs.terms.FunctionTerm; @@ -45,7 +46,7 @@ public class ActionsTest { @Test public void helloWorld() { MockedActionsAlphaFactory alphaFactory = new MockedActionsAlphaFactory(); - Alpha alpha = alphaFactory.newAlpha(new SystemConfig()); + Alpha alpha = AlphaFactory.newAlpha(); InputProgram program = alpha.readProgramString(HELLO_WORLD); alpha.solve(program); assertEquals("Hello World!", alphaFactory.getActionImplementationMock().getStdoutContent()); @@ -61,7 +62,7 @@ public void writeToFile() { MockedActionsAlphaFactory alphaFactory = new MockedActionsAlphaFactory(); alphaFactory.getActionImplementationMock().setMockedFileOutputs(mockedFileOutputs); ActionImplementationProvider actionProvider = alphaFactory.getActionImplementationMock(); - Alpha alpha = alphaFactory.newAlpha(new SystemConfig()); + Alpha alpha = AlphaFactory.newAlpha(); InputProgram program = alpha.readProgramString(WRITE_TO_FILE); Set answerSets = alpha.solve(program).collect(Collectors.toSet()); LOGGER.debug("Got answer sets: {}", answerSets); diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/AggregateRewritingTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/AggregateRewritingTest.java index 1d11e89d4..e4ca8acf3 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/AggregateRewritingTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/AggregateRewritingTest.java @@ -1,11 +1,14 @@ -package at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates; +package at.ac.tuwien.kr.alpha; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; import java.util.function.Function; +import java.util.stream.Collectors; +import at.ac.tuwien.kr.alpha.api.Alpha; +import at.ac.tuwien.kr.alpha.api.impl.AlphaFactory; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -31,18 +34,6 @@ public class AggregateRewritingTest { - private static final ProgramParser PARSER = new ProgramParserImpl(); - private static final Function> NORMALIZE_AND_SOLVE = (str) -> { - SystemConfig cfg = new SystemConfig(); - InputProgram prog = PARSER.parse(str); - NormalProgram normalized = new NormalizeProgramTransformation(cfg.getAggregateRewritingConfig()).apply(prog); - CompiledProgram compiled = InternalProgram.fromNormalProgram(normalized); - AtomStore atomStore = new AtomStoreImpl(); - Grounder grounder = GrounderFactory.getInstance("naive", compiled, atomStore, cfg.isDebugInternalChecks()); - Solver solver = SolverFactory.getInstance(cfg, atomStore, grounder); - return solver.collectList(); - }; - //@formatter:off // Smoke-test case for "X <= #count{...}" aggregate private static final String CNT_LE1_ASP = @@ -99,9 +90,16 @@ public class AggregateRewritingTest { + " Y = #count { X : p( X ) }, 1 <= #count { X : p( X ) }, Z = #max { W : p( W ) }."; //@formatter:on + // Use an alpha instance with default config for all test cases + private final Alpha alpha = AlphaFactory.newAlpha(); + private final Function> solve = (asp) -> { + InputProgram prog = alpha.readProgramString(asp); + return alpha.solve(prog).collect(Collectors.toList()); + }; + @Test public void countLeSortingGridSimple() { - List answerSets = NORMALIZE_AND_SOLVE.apply(CNT_LE1_ASP); + List answerSets = solve.apply(CNT_LE1_ASP); assertEquals(1, answerSets.size()); AnswerSet answerSet = answerSets.get(0); Predicate thing = Predicates.getPredicate("thing", 1); @@ -123,7 +121,7 @@ public void countLeSortingGridSimple() { @Test public void countEqSimple() { - List answerSets = NORMALIZE_AND_SOLVE.apply(CNT_EQ1_ASP); + List answerSets = solve.apply(CNT_EQ1_ASP); assertEquals(1, answerSets.size()); AnswerSet answerSet = answerSets.get(0); Predicate thing = Predicates.getPredicate("thing", 1); @@ -141,7 +139,7 @@ public void countEqSimple() { @Test public void countLeCountingGridSimple() { - List answerSets = NORMALIZE_AND_SOLVE.apply(CNT_LE1_ASP); + List answerSets = solve.apply(CNT_LE1_ASP); assertEquals(1, answerSets.size()); AnswerSet answerSet = answerSets.get(0); Predicate thing = Predicates.getPredicate("thing", 1); @@ -163,7 +161,7 @@ public void countLeCountingGridSimple() { @Test public void countEqGlobalVars() { - List answerSets = NORMALIZE_AND_SOLVE.apply(VERTEX_DEGREE_ASP); + List answerSets = solve.apply(VERTEX_DEGREE_ASP); assertEquals(1, answerSets.size()); AnswerSet answerSet = answerSets.get(0); Predicate vertexDegree = Predicates.getPredicate("graph_vertex_degree", 3); @@ -182,7 +180,7 @@ public void countEqGlobalVars() { @Test // Test "count eq" and "max eq" together with global vars public void graphVerticesOfMaxDegree() { - List answerSets = NORMALIZE_AND_SOLVE.apply(NUM_MAX_DEGREE_VERTICES_ASP); + List answerSets = solve.apply(NUM_MAX_DEGREE_VERTICES_ASP); assertEquals(1, answerSets.size()); AnswerSet answerSet = answerSets.get(0); Predicate maxDegreeVertices = Predicates.getPredicate("graph_max_degree_vertices", 3); @@ -196,7 +194,7 @@ public void graphVerticesOfMaxDegree() { @Test public void greaterMin() { - List answerSets = NORMALIZE_AND_SOLVE.apply(MIN_GT1_ASP); + List answerSets = solve.apply(MIN_GT1_ASP); assertEquals(1, answerSets.size()); AnswerSet answerSet = answerSets.get(0); Predicate greaterMin = Predicates.getPredicate("greater_min_acceptable", 1); @@ -210,7 +208,7 @@ public void greaterMin() { @Test public void sumEquals1() { - List answerSets = NORMALIZE_AND_SOLVE.apply(SUM_EQ1_ASP); + List answerSets = solve.apply(SUM_EQ1_ASP); assertEquals(1, answerSets.size()); AnswerSet answerSet = answerSets.get(0); Predicate sumThings = Predicates.getPredicate("sum_things", 1); @@ -224,7 +222,7 @@ public void sumEquals1() { @Test public void sumLessOrEqual1() { - List answerSets = NORMALIZE_AND_SOLVE.apply(SUM_LE1_ASP); + List answerSets = solve.apply(SUM_LE1_ASP); assertEquals(1, answerSets.size()); AnswerSet answerSet = answerSets.get(0); Predicate boundLe = Predicates.getPredicate("bound_le_sum", 1); @@ -238,7 +236,7 @@ public void sumLessOrEqual1() { @Test @Disabled("Open issue, as dependency analysis includes cyclic output-dependency, which it should not.") public void setComplexEqualityWithGlobals() { - List answerSets = NORMALIZE_AND_SOLVE.apply(COMPLEX_EQUALITY_WITH_GLOBALS); + List answerSets = solve.apply(COMPLEX_EQUALITY_WITH_GLOBALS); assertEquals(1, answerSets.size()); AnswerSet answerSet = answerSets.get(0); Predicate q = Predicates.getPredicate("q", 0); diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/ArithmeticTermsRewritingTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/ArithmeticTermsRewritingTest.java index e92589302..bbcb22734 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/ArithmeticTermsRewritingTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/ArithmeticTermsRewritingTest.java @@ -1,4 +1,4 @@ -package at.ac.tuwien.kr.alpha.core.programs.transformation; +package at.ac.tuwien.kr.alpha; import static java.util.stream.Collectors.toList; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -9,6 +9,7 @@ import java.util.Map; import java.util.Set; +import at.ac.tuwien.kr.alpha.core.programs.transformation.ArithmeticTermsRewriting; import org.junit.jupiter.api.Test; import at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation; @@ -23,7 +24,7 @@ import at.ac.tuwien.kr.alpha.api.programs.terms.VariableTerm; import at.ac.tuwien.kr.alpha.commons.programs.Programs; import at.ac.tuwien.kr.alpha.commons.programs.terms.Terms; -import at.ac.tuwien.kr.alpha.core.externals.Externals; +import at.ac.tuwien.kr.alpha.commons.externals.Externals; import at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl; /** diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/FixedInterpretationLiteralsTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/FixedInterpretationLiteralsTest.java index 690e39777..9915ef7b0 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/FixedInterpretationLiteralsTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/FixedInterpretationLiteralsTest.java @@ -1,4 +1,4 @@ -package at.ac.tuwien.kr.alpha.api.impl; +package at.ac.tuwien.kr.alpha; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -12,6 +12,7 @@ import java.util.Optional; import java.util.Set; +import at.ac.tuwien.kr.alpha.api.impl.AlphaFactory; import org.junit.jupiter.api.Test; import at.ac.tuwien.kr.alpha.api.Alpha; @@ -23,8 +24,8 @@ import at.ac.tuwien.kr.alpha.commons.Predicates; import at.ac.tuwien.kr.alpha.commons.programs.atoms.Atoms; import at.ac.tuwien.kr.alpha.commons.programs.terms.Terms; -import at.ac.tuwien.kr.alpha.core.externals.AspStandardLibrary; -import at.ac.tuwien.kr.alpha.core.externals.Externals; +import at.ac.tuwien.kr.alpha.commons.externals.Externals; +import at.ac.tuwien.kr.alpha.commons.externals.AspStandardLibrary; public class FixedInterpretationLiteralsTest { @@ -79,7 +80,7 @@ public static Set>> connection(String dummy) { private Map externals; public FixedInterpretationLiteralsTest() { - this.alpha = new AlphaImpl(); + this.alpha = AlphaFactory.newAlpha(); this.externals = new HashMap<>(); this.externals.putAll(Externals.scan(AspStandardLibrary.class)); this.externals.putAll(Externals.scan(FixedInterpretationLiteralsTest.class)); diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/StratifiedEvaluationTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/StratifiedEvaluationTest.java index 6c6def131..19ec176cb 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/StratifiedEvaluationTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/StratifiedEvaluationTest.java @@ -25,8 +25,9 @@ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package at.ac.tuwien.kr.alpha.core.programs.transformation; +package at.ac.tuwien.kr.alpha; +import static at.ac.tuwien.kr.alpha.test.AlphaAssertions.assertAnswerSetsEqual; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -36,7 +37,14 @@ import java.util.Map; import java.util.Set; import java.util.function.Function; +import java.util.stream.Collectors; +import at.ac.tuwien.kr.alpha.api.Alpha; +import at.ac.tuwien.kr.alpha.api.DebugSolvingContext; +import at.ac.tuwien.kr.alpha.api.impl.AlphaFactory; +import at.ac.tuwien.kr.alpha.api.programs.NormalProgram; +import at.ac.tuwien.kr.alpha.core.programs.transformation.NormalizeProgramTransformation; +import at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation; import org.junit.jupiter.api.Test; import at.ac.tuwien.kr.alpha.api.AnswerSet; @@ -54,7 +62,7 @@ import at.ac.tuwien.kr.alpha.commons.substitutions.Instance; import at.ac.tuwien.kr.alpha.core.common.AtomStore; import at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl; -import at.ac.tuwien.kr.alpha.core.externals.Externals; +import at.ac.tuwien.kr.alpha.commons.externals.Externals; import at.ac.tuwien.kr.alpha.core.grounder.Grounder; import at.ac.tuwien.kr.alpha.core.grounder.GrounderFactory; import at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl; @@ -62,33 +70,24 @@ import at.ac.tuwien.kr.alpha.core.programs.CompiledProgram; import at.ac.tuwien.kr.alpha.core.programs.InternalProgram; import at.ac.tuwien.kr.alpha.core.solver.SolverFactory; -import at.ac.tuwien.kr.alpha.core.test.util.TestUtils; public class StratifiedEvaluationTest { - private final ProgramParser parser = new ProgramParserImpl(); - private final NormalizeProgramTransformation normalizer = new NormalizeProgramTransformation(SystemConfig.DEFAULT_AGGREGATE_REWRITING_CONFIG); - private final StratifiedEvaluation evaluator = new StratifiedEvaluation(); - private final Function parseAndEvaluate = (str) -> { - return evaluator.apply(AnalyzedProgram.analyzeNormalProgram(normalizer.apply(parser.parse(str)))); - }; - - private final Function> solveCompiledProg = (prog) -> { - AtomStore atomStore = new AtomStoreImpl(); - Grounder grounder = GrounderFactory.getInstance("naive", prog, atomStore, false); - Solver solver = SolverFactory.getInstance(new SystemConfig(), atomStore, grounder); - return solver.collectSet(); - }; + // Alpha instance with default configuration (evolog support and stratified evaluation enabled) + private final Alpha alpha = AlphaFactory.newAlpha(); + /** + * Verifies that facts are not duplicated by stratified evaluation. + */ @Test public void testDuplicateFacts() { String aspStr = "p(a). p(b). q(b). q(X) :- p(X)."; - CompiledProgram evaluated = parseAndEvaluate.apply(aspStr); - Instance qOfB = new Instance(TestUtils.basicAtomWithSymbolicTerms("q", "b").getTerms()); - Set facts = evaluated.getFactsByPredicate().get(Predicates.getPredicate("q", 1)); + DebugSolvingContext dbgInfo = alpha.prepareDebugSolve(alpha.readProgramString(aspStr)); + NormalProgram evaluated = dbgInfo.getPreprocessedProgram(); + BasicAtom qOfB = Atoms.newBasicAtom(Predicates.getPredicate("q", 1), Terms.newSymbolicConstant("b")); int numQOfB = 0; - for (Instance at : facts) { - if (at.equals(qOfB)) { + for (Atom fact : evaluated.getFacts()) { + if (fact.equals(qOfB)) { numQOfB++; } } @@ -98,41 +97,39 @@ public void testDuplicateFacts() { @Test public void testEqualityWithConstantTerms() { String aspStr = "equal :- 1 = 1."; - CompiledProgram evaluated = parseAndEvaluate.apply(aspStr); - Atom equal = TestUtils.basicAtomWithSymbolicTerms("equal"); + DebugSolvingContext dbgInfo = alpha.prepareDebugSolve(alpha.readProgramString(aspStr)); + NormalProgram evaluated = dbgInfo.getPreprocessedProgram(); + Atom equal = Atoms.newBasicAtom(Predicates.getPredicate("equal", 0)); assertTrue(evaluated.getFacts().contains(equal)); } @Test public void testEqualityWithVarTerms() { String aspStr = "a(1). a(2). a(3). b(X) :- a(X), X = 1. c(X) :- a(X), X = 2. d(X) :- X = 3, a(X)."; - CompiledProgram evaluated = parseAndEvaluate.apply(aspStr); - Set answerSets = solveCompiledProg.apply(evaluated); - TestUtils.assertAnswerSetsEqual("a(1), a(2), a(3), b(1), c(2), d(3)", answerSets); + Set answerSets = alpha.solve(alpha.readProgramString(aspStr)).collect(Collectors.toSet()); + assertAnswerSetsEqual("a(1), a(2), a(3), b(1), c(2), d(3)", answerSets); } @Test public void testNonGroundableRule() { String asp = "p(a). q(a, b). s(X, Y) :- p(X), q(X, Y), r(Y)."; - CompiledProgram evaluated = parseAndEvaluate.apply(asp); - Set answerSets = solveCompiledProg.apply(evaluated); - TestUtils.assertAnswerSetsEqual("p(a), q(a,b)", answerSets); + Set answerSets = alpha.solve(alpha.readProgramString(asp)).collect(Collectors.toSet()); + assertAnswerSetsEqual("p(a), q(a,b)", answerSets); } + @Test public void testCountAggregate() { String asp = "a. b :- 1 <= #count { 1 : a }."; - CompiledProgram evaluated = parseAndEvaluate.apply(asp); - Set answerSets = solveCompiledProg.apply(evaluated); - TestUtils.assertAnswerSetsEqual("a, b", answerSets); + Set answerSets = alpha.solve(alpha.readProgramString(asp)).collect(Collectors.toSet()); + assertAnswerSetsEqual("a, b", answerSets); } @Test public void testIntervalFact() { String asp = "a(1..3)."; - CompiledProgram evaluated = parseAndEvaluate.apply(asp); - Set answerSets = solveCompiledProg.apply(evaluated); - TestUtils.assertAnswerSetsEqual("a(1), a(2), a(3)", answerSets); + Set answerSets = alpha.solve(alpha.readProgramString(asp)).collect(Collectors.toSet()); + assertAnswerSetsEqual("a(1), a(2), a(3)", answerSets); } @Test @@ -141,18 +138,18 @@ public void testAggregateSpecial() { + "{ chosenThing(X) : thing(X) }.\n" + "chosenSomething :- chosenThing(X).\n" + ":- not chosenSomething.\n" + ":- chosenThing(X), chosenThing(Y), X != Y.\n" + "allThings :- 3 <= #count{ X : thing(X)}. \n" + "chosenMaxThing :- allThings, chosenThing(3).\n" + ":- not chosenMaxThing."; - CompiledProgram evaluated = parseAndEvaluate.apply(asp); - assertTrue(evaluated.getFacts().contains(TestUtils.basicAtomWithSymbolicTerms("allThings"))); - Set answerSets = solveCompiledProg.apply(evaluated); - TestUtils.assertAnswerSetsEqual("allThings, thing(1), thing(2), thing(3), chosenMaxThing, chosenSomething, chosenThing(3)", answerSets); + DebugSolvingContext dbgInfo = alpha.prepareDebugSolve(alpha.readProgramString(asp)); + NormalProgram evaluated = dbgInfo.getPreprocessedProgram(); + assertTrue(evaluated.getFacts().contains(Atoms.newBasicAtom(Predicates.getPredicate("allThings", 0)))); + Set answerSets = dbgInfo.getSolver().collectSet(); + assertAnswerSetsEqual("allThings, thing(1), thing(2), thing(3), chosenMaxThing, chosenSomething, chosenThing(3)", answerSets); } @Test public void testNegatedFixedInterpretationLiteral() { String asp = "stuff(1). stuff(2). smallStuff(X) :- stuff(X), not X > 1."; - CompiledProgram evaluated = parseAndEvaluate.apply(asp); - Set answerSets = solveCompiledProg.apply(evaluated); - TestUtils.assertAnswerSetsEqual("stuff(1), stuff(2), smallStuff(1)", answerSets); + Set answerSets = alpha.solve(alpha.readProgramString(asp)).collect(Collectors.toSet()); + assertAnswerSetsEqual("stuff(1), stuff(2), smallStuff(1)", answerSets); } @SuppressWarnings("unused") @@ -161,16 +158,14 @@ public static boolean sayTrue(Object o) { return true; } + @Test public void testNegatedExternalLiteral() throws Exception { String asp = "claimedTruth(bla). truth(X) :- claimedTruth(X), &sayTrue[X]. lie(X) :- claimedTruth(X), not &sayTrue[X]."; Map externals = new HashMap<>(); externals.put("sayTrue", Externals.processPredicateMethod(this.getClass().getMethod("sayTrue", Object.class))); - ProgramParser parserWithExternals = new ProgramParserImpl(); - AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalizer.apply(parserWithExternals.parse(asp, externals))); - CompiledProgram evaluated = new StratifiedEvaluation().apply(analyzed); - Set answerSets = solveCompiledProg.apply(evaluated); - TestUtils.assertAnswerSetsEqual("claimedTruth(bla), truth(bla)", answerSets); + Set answerSets = alpha.solve(alpha.readProgramString(asp, externals)).collect(Collectors.toSet()); + assertAnswerSetsEqual("claimedTruth(bla), truth(bla)", answerSets); } /** @@ -179,8 +174,11 @@ public void testNegatedExternalLiteral() throws Exception { */ @Test public void testPartnerUnitsProblemTopologicalOrder() throws IOException { - InputProgram prg = parser.parse(StratifiedEvaluationTest.class.getResourceAsStream("/partial-eval/pup_topological_order.asp")); - CompiledProgram evaluated = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normalizer.apply(prg))); + InputProgram prg = alpha.readProgramStream( + StratifiedEvaluationTest.class.getResourceAsStream("/partial-eval/pup_topological_order.asp"), + new HashMap<>()); + DebugSolvingContext dbgInfo = alpha.prepareDebugSolve(prg); + NormalProgram evaluated = dbgInfo.getPreprocessedProgram(); assertTrue(evaluated.getRules().isEmpty(), "Not all rules eliminated by stratified evaluation"); assertEquals(57, evaluated.getFacts().size()); } @@ -191,6 +189,12 @@ public void testPartnerUnitsProblemTopologicalOrder() throws IOException { * * @throws IOException */ + /** + * Verifies correct handling of negated basic literals in StratifiedEvaluation. + * For details, see comments in test program + * + * @throws IOException + */ @Test public void testNegatedLiteralInRecursiveRule() throws IOException { //@formatter:off @@ -200,51 +204,48 @@ public void testNegatedLiteralInRecursiveRule() throws IOException { + "inc_value(4), inc_value(5), inc_value(6), inc_value(7), " + "inc_value(8)"; //@formatter:on - InputProgram prog = new ProgramParserImpl().parse( + InputProgram prog = alpha.readProgramStream( StratifiedEvaluationTest.class.getResourceAsStream("/partial-eval/recursive_w_negated_condition.asp"), new HashMap<>()); // Run stratified evaluation and solve - CompiledProgram inputStratEval = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normalizer.apply(prog))); - Set asStrat = solveCompiledProg.apply(inputStratEval); - TestUtils.assertAnswerSetsEqual(expectedAnswerSet, asStrat); - - // Solve without stratified evaluation - CompiledProgram inputNoStratEval = InternalProgram.fromNormalProgram(normalizer.apply(prog)); - Set as = solveCompiledProg.apply(inputNoStratEval); - TestUtils.assertAnswerSetsEqual(expectedAnswerSet, as); + DebugSolvingContext dbg = alpha.prepareDebugSolve(prog); + Set as = dbg.getSolver().collectSet(); + assertAnswerSetsEqual(expectedAnswerSet, as); } + @Test public void testRecursiveRanking() { //@formatter:off - String asp = "thing(a).\n" + - "thing(b).\n" + - "thing(c).\n" + - "thing_before(a, b).\n" + - "thing_before(b, c).\n" + - "has_prev_thing(X) :- thing(X), thing_succ(_, X).\n" + - "first_thing(X) :- thing(X), not has_prev_thing(X).\n" + - "thing_not_succ(X, Y) :-\n" + - " thing(X),\n" + - " thing(Y),\n" + - " thing(INTM),\n" + - " thing_before(X, Y),\n" + - " thing_before(X, INTM),\n" + - " thing_before(INTM, X).\n" + - "thing_succ(X, Y) :-\n" + - " thing(X),\n" + - " thing(Y),\n" + - " thing_before(X, Y),\n" + - " not thing_not_succ(X, Y).\n" + - "thing_rank(X, 1) :- first_thing(X).\n" + - "thing_rank(X, R) :-\n" + - " thing(X),\n" + - " thing_succ(Y, X),\n" + - " thing_rank(Y, K),\n" + + String asp = "thing(a).\n" + + "thing(b).\n" + + "thing(c).\n" + + "thing_before(a, b).\n" + + "thing_before(b, c).\n" + + "has_prev_thing(X) :- thing(X), thing_succ(_, X).\n" + + "first_thing(X) :- thing(X), not has_prev_thing(X).\n" + + "thing_not_succ(X, Y) :-\n" + + " thing(X),\n" + + " thing(Y),\n" + + " thing(INTM),\n" + + " thing_before(X, Y),\n" + + " thing_before(X, INTM),\n" + + " thing_before(INTM, X).\n" + + "thing_succ(X, Y) :-\n" + + " thing(X),\n" + + " thing(Y),\n" + + " thing_before(X, Y),\n" + + " not thing_not_succ(X, Y).\n" + + "thing_rank(X, 1) :- first_thing(X).\n" + + "thing_rank(X, R) :-\n" + + " thing(X),\n" + + " thing_succ(Y, X),\n" + + " thing_rank(Y, K),\n" + " R = K + 1."; //@formatter:on - CompiledProgram evaluated = parseAndEvaluate.apply(asp); + DebugSolvingContext dbgInfo = alpha.prepareDebugSolve(alpha.readProgramString(asp)); + NormalProgram evaluated = dbgInfo.getPreprocessedProgram(); Predicate rank = Predicates.getPredicate("thing_rank", 2); BasicAtom rank1 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("a"), Terms.newConstant(1)); BasicAtom rank2 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("b"), Terms.newConstant(2)); diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/api/impl/AlphaImplTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/api/impl/AlphaImplTest.java index 0e4086ea9..c26b6c66d 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/api/impl/AlphaImplTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/api/impl/AlphaImplTest.java @@ -33,7 +33,6 @@ import at.ac.tuwien.kr.alpha.api.config.InputConfig; import at.ac.tuwien.kr.alpha.api.config.SystemConfig; import at.ac.tuwien.kr.alpha.api.programs.InputProgram; -import at.ac.tuwien.kr.alpha.api.programs.NormalProgram; import at.ac.tuwien.kr.alpha.api.programs.ProgramParser; import at.ac.tuwien.kr.alpha.api.programs.atoms.Atom; import at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom; @@ -54,7 +53,7 @@ import at.ac.tuwien.kr.alpha.commons.programs.rules.heads.Heads; import at.ac.tuwien.kr.alpha.commons.programs.terms.Terms; import at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl; -import at.ac.tuwien.kr.alpha.core.programs.CompiledProgram; +import at.ac.tuwien.kr.alpha.test.AnswerSetsParser; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -147,7 +146,7 @@ public static boolean thinger(Thingy thingy) { @Test public void withExternal() throws Exception { - Alpha alpha = new AlphaFactory().newAlpha(); + Alpha alpha = AlphaFactory.newAlpha(); InputConfig inputCfg = InputConfig.forString("a :- &isOne[1]."); inputCfg.addPredicateMethod("isOne", Externals.processPredicateMethod(this.getClass().getMethod("isOne", int.class))); InputProgram program = alpha.readProgram(inputCfg); @@ -158,7 +157,7 @@ public void withExternal() throws Exception { @Test public void addsFacts() { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); Thingy a = new Thingy(); Thingy b = new Thingy(); List things = asList(a, b); @@ -171,7 +170,7 @@ public void addsFacts() { @Test public void withExternalTypeConflict() { assertThrows(IllegalArgumentException.class, () -> { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputConfig inputCfg = InputConfig.forString("a :- &isFoo[\"adsfnfdsf\"]."); inputCfg.addPredicateMethod("isFoo", Externals.processPredicateMethod(this.getClass().getMethod("isFoo", Integer.class))); Set actual = system.solve(system.readProgram(inputCfg)).collect(Collectors.toSet()); @@ -182,32 +181,32 @@ public void withExternalTypeConflict() { @Test public void smallGraph() throws Exception { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputConfig inputCfg = InputConfig.forString("node(1). node(2). node(3). a :- &connected[1,2]."); inputCfg.addPredicateMethod("connected", Externals.processPredicate((Integer a, Integer b) -> (a == 1 && b == 2) || (b == 2 || b == 3))); InputProgram program = system.readProgram(inputCfg); Set actual = system.solve(program).collect(Collectors.toSet()); - Set expected = at.ac.tuwien.kr.alpha.api.impl.AnswerSetsParser.parse("{ a, node(1), node(2), node(3) }"); + Set expected = AnswerSetsParser.parse("{ a, node(1), node(2), node(3) }"); assertEquals(expected, actual); } @Test public void filterOutput() throws Exception { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputConfig inputCfg = InputConfig.forString("node(1). node(2). outgoing13(X) :- node(X), &getLargeGraphEdges(13,X)."); inputCfg.addPredicateMethod("getLargeGraphEdges", Externals.processPredicate(() -> new HashSet<>(asList(asList(Terms.newConstant(1), Terms.newConstant(2)), asList(Terms.newConstant(2), Terms.newConstant(1)), asList(Terms.newConstant(13), Terms.newConstant(1)))))); InputProgram program = system.readProgram(inputCfg); Set actual = system.solve(program).collect(Collectors.toSet()); - Set expected = at.ac.tuwien.kr.alpha.api.impl.AnswerSetsParser.parse("{ node(1), node(2), outgoing13(1) }"); + Set expected = AnswerSetsParser.parse("{ node(1), node(2), outgoing13(1) }"); assertEquals(expected, actual); } @Test public void supplier() throws Exception { - Alpha system = new AlphaImpl(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("node(1). a :- &bestNode(X), node(X)."); cfg.addPredicateMethod("bestNode", Externals.processPredicate(() -> singleton(singletonList(Terms.newConstant(1))))); InputProgram prog = system.readProgram(cfg); @@ -224,7 +223,7 @@ public static Set>> bestNode() { @Test public void noInput() throws Exception { - Alpha system = new AlphaImpl(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("node(1). a :- &bestNode(X), node(X)."); cfg.addPredicateMethod("bestNode", Externals.processPredicateMethod(this.getClass().getMethod("bestNode"))); InputProgram prog = system.readProgram(cfg); @@ -237,7 +236,7 @@ public void noInput() throws Exception { @Test public void smallGraphWithWrongType() { assertThrows(IllegalArgumentException.class, () -> { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("a :- &connected[\"hello\",2]."); cfg.addPredicateMethod("connected", Externals.processPredicate((Integer a, Integer b) -> (a == 1 && b == 2) || (b == 2 || b == 3))); InputProgram prog = system.readProgram(cfg); @@ -263,7 +262,7 @@ public static Set>> coolNode(int node) { @Test @Disabled("Test program is not safe (external lacking output variables). This should throw some exception.") public void smallGraphNoNeighbors() throws Exception { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("noNeighbors(2) :- not &neighbors[2]."); cfg.addPredicateMethod("neighbors", Externals.processPredicateMethod(this.getClass().getMethod("neighbors", int.class))); InputProgram prog = system.readProgram(cfg); @@ -275,7 +274,7 @@ public void smallGraphNoNeighbors() throws Exception { @Test public void smallGraphCoolNode() throws Exception { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("node(1..2). in(X) :- node(X), &coolNode[X]."); cfg.addPredicateMethod("coolNode", Externals.processPredicateMethod(this.getClass().getMethod("coolNode", int.class))); InputProgram prog = system.readProgram(cfg); @@ -287,7 +286,7 @@ public void smallGraphCoolNode() throws Exception { @Test public void smallGraphSingleNeighbor() throws Exception { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("node(1..3). in(1,X) :- &neighbors[1](X), node(X)."); cfg.addPredicateMethod("neighbors", Externals.processPredicateMethod(this.getClass().getMethod("neighbors", int.class))); InputProgram prog = system.readProgram(cfg); @@ -300,7 +299,7 @@ public void smallGraphSingleNeighbor() throws Exception { @Test @Disabled("Test program is not safe (external lacking output variables). This should throw some exception.") public void smallGraphSingleNeighborNoTerm() throws Exception { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("success :- &neighbors[1], not &neighbors[2]."); cfg.addPredicateMethod("neighbors", Externals.processPredicateMethod(this.getClass().getMethod("neighbors", int.class))); InputProgram prog = system.readProgram(cfg); @@ -331,11 +330,11 @@ public void withExternalSubtype() throws Exception { Rule rule = Rules.newRule( Heads.newNormalHead(Atoms.newBasicAtom(Predicates.getPredicate("p", 1), Terms.newConstant("x"))), - singletonList(Literals.fromAtom(Atoms.newExternalAtom(Predicates.getPredicate("thinger", 1), + singleton(Literals.fromAtom(Atoms.newExternalAtom(Predicates.getPredicate("thinger", 1), new MethodPredicateInterpretation(this.getClass().getMethod("thinger", Thingy.class)), singletonList(Terms.newConstant(thingy)), emptyList()), true))); - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputProgram prog = Programs.newInputProgram(singletonList(rule), emptyList(), Programs.newInlineDirectives()); @@ -346,7 +345,7 @@ public void withExternalSubtype() throws Exception { @Test public void withExternalViaAnnotation() throws Exception { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("a :- &isOne[1]."); cfg.addPredicateMethods(Externals.scan(this.getClass())); InputProgram prog = system.readProgram(cfg); @@ -372,7 +371,7 @@ public void errorDuplicateExternal() { @Test public void withNativeExternal() throws Exception { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("a :- &isTwo[2]."); cfg.addPredicateMethod("isTwo", Externals.processPredicate((Integer t) -> t == 2)); InputProgram prog = system.readProgram(cfg); @@ -385,7 +384,7 @@ public void withNativeExternal() throws Exception { @Test @Disabled("External atom has state, which is not allowed. Caching of calls makes the number of invocations wrong.") public void withExternalInvocationCounted1() throws Exception { - Alpha system = new AlphaFactory().newAlpha(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("a :- &isOne[1], &isOne[1]."); cfg.addPredicateMethod("isOne", Externals.processPredicateMethod(this.getClass().getMethod("isOne", int.class))); InputProgram prog = system.readProgram(cfg); @@ -403,7 +402,7 @@ public void withExternalInvocationCounted1() throws Exception { @Test @Disabled("External atom has state, which is not allowed. Caching of calls makes the number of invocations wrong.") public void withExternalInvocationCounted2() throws Exception { - Alpha system = new AlphaImpl(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("a. b :- &isOne[1], &isOne[2]."); cfg.addPredicateMethod("isOne", Externals.processPredicateMethod(this.getClass().getMethod("isOne", int.class))); InputProgram prog = system.readProgram(cfg); @@ -421,7 +420,7 @@ public void withExternalInvocationCounted2() throws Exception { @Test @Disabled("External atom has state, which is not allowed. Caching of calls makes the number of invocations wrong.") public void withExternalInvocationCounted3() throws Exception { - Alpha system = new AlphaImpl(); + Alpha system = AlphaFactory.newAlpha(); InputConfig cfg = InputConfig.forString("a :- &isOne[1], not &isOne[2]."); cfg.addPredicateMethod("isOne", Externals.processPredicateMethod(this.getClass().getMethod("isOne", int.class))); InputProgram prog = system.readProgram(cfg); @@ -439,7 +438,7 @@ public void withExternalInvocationCounted3() throws Exception { @Test @SuppressWarnings("unchecked") public void programWithExternalStringStuff() throws IOException { - Alpha alpha = new AlphaImpl(); + Alpha alpha = AlphaFactory.newAlpha(); InputProgram prog = alpha.readProgram(InputConfig.forString(STRINGSTUFF_ASP)); Set answerSets = alpha.solve(prog).collect(Collectors.toSet()); // Verify every result string has length 6 and contains "foo" @@ -455,7 +454,7 @@ public void programWithExternalStringStuff() throws IOException { @Test @SuppressWarnings("unchecked") public void withNegatedExternal() throws IOException { - Alpha alpha = new AlphaImpl(); + Alpha alpha = AlphaFactory.newAlpha(); InputProgram prog = alpha.readProgram(InputConfig.forString(NEGATED_EXTERNAL_ASP)); Set answerSets = alpha.solve(prog).collect(Collectors.toSet()); assertEquals(31, answerSets.size()); @@ -473,7 +472,7 @@ public void withNegatedExternal() throws IOException { @Test public void reifyInput() { String aspInput = "p(X) :- q(X), not r(X)."; - Alpha system = new AlphaImpl(); + Alpha system = AlphaFactory.newAlpha(); InputProgram input = system.readProgramString(aspInput); Set reified = system.reify(input); @@ -493,7 +492,7 @@ public void reifyInput() { @Test public void basicUsage() throws Exception { - Alpha system = new AlphaImpl(); + Alpha system = AlphaFactory.newAlpha(); Set actual = system.solve(system.readProgram(InputConfig.forString("p(a)."))).collect(Collectors.toSet()); Set expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("p").symbolicInstance("a").build())); assertEquals(expected, actual); @@ -501,7 +500,7 @@ public void basicUsage() throws Exception { @Test public void basicUsageWithString() throws Exception { - Alpha system = new AlphaImpl(); + Alpha system = AlphaFactory.newAlpha(); Set actual = system.solve(system.readProgram(InputConfig.forString("p(\"a\")."))).collect(Collectors.toSet()); Set expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("p").instance("a").build())); assertEquals(expected, actual); @@ -513,7 +512,7 @@ public void basicUsageWithString() throws Exception { @Test public void filterTest() { String progstr = "a. b. c. d :- c. e(a, b) :- d."; - Alpha system = new AlphaImpl(); + Alpha system = AlphaFactory.newAlpha(); InputProgram prog = system.readProgramString(progstr); Set actual = system.solve(prog, (p) -> p.equals(Predicates.getPredicate("a", 0)) || p.equals(Predicates.getPredicate("e", 2))) .collect(Collectors.toSet()); @@ -521,42 +520,9 @@ public void filterTest() { assertEquals(expected, actual); } - /** - * Verifies that no stratified evaluation is performed up-front when disabled in config. - */ - @Test - public void disableStratifiedEvalTest() { - // Note: This might be cleaner if the test used the debugSolve method from the interface - String progstr = "p(a). q(X) :- p(X)."; - SystemConfig cfg = new SystemConfig(); - cfg.setEvaluateStratifiedPart(false); - AlphaImpl system = new AlphaImpl(cfg); - InputProgram input = system.readProgramString(progstr); - NormalProgram normal = system.normalizeProgram(input); - CompiledProgram preprocessed = system.performProgramPreprocessing(normal); - assertFalse(preprocessed.getFacts().contains(Atoms.newBasicAtom(Predicates.getPredicate("q", 1), Terms.newSymbolicConstant("a"))), - "Preprocessed program contains fact derived from stratifiable rule, but should not!"); - } - - /** - * Verifies that stratified evaluation is performed up-front if not otherwise configured. - */ - @Test - public void enableStratifiedEvalTest() { - // Note: This might be cleaner if the test used the debugSolve method from the interface - String progstr = "p(a). q(X) :- p(X)."; - SystemConfig cfg = new SystemConfig(); - AlphaImpl system = new AlphaImpl(cfg); - InputProgram input = system.readProgramString(progstr); - NormalProgram normal = system.normalizeProgram(input); - CompiledProgram preprocessed = system.performProgramPreprocessing(normal); - assertTrue(preprocessed.getFacts().contains(Atoms.newBasicAtom(Predicates.getPredicate("q", 1), Terms.newSymbolicConstant("a"))), - "Preprocessed program does not contain fact derived from stratifiable rule, but should!"); - } - @Test public void passingUnitTestExpectUnsat() { - Alpha alpha = new AlphaImpl(); + Alpha alpha = AlphaFactory.newAlpha(); InputProgram prog = alpha.readProgramString(UNIT_TEST_EXPECT_UNSAT); TestResult testResult = alpha.test(prog); assertTrue(testResult.isSuccess()); @@ -564,7 +530,7 @@ public void passingUnitTestExpectUnsat() { @Test public void passingUnitTestBasicTest() { - Alpha alpha = new AlphaImpl(); + Alpha alpha = AlphaFactory.newAlpha(); InputProgram prog = alpha.readProgramString(UNIT_TEST_BASIC_TEST); TestResult testResult = alpha.test(prog); assertTrue(testResult.isSuccess()); @@ -579,7 +545,7 @@ public void passingUnitTestBasicTest() { @Test public void passingUnitTestMultipleAssertions() { - Alpha alpha = new AlphaImpl(); + Alpha alpha = AlphaFactory.newAlpha(); InputProgram prog = alpha.readProgramString(UNIT_TEST_MORE_ASSERTIONS); TestResult testResult = alpha.test(prog); assertTrue(testResult.isSuccess()); @@ -594,7 +560,7 @@ public void passingUnitTestMultipleAssertions() { @Test public void passingUnitTestMultipleTestCases() { - Alpha alpha = new AlphaImpl(); + Alpha alpha = AlphaFactory.newAlpha(); InputProgram prog = alpha.readProgramString(UNIT_TEST_MORE_TCS); TestResult testResult = alpha.test(prog); assertTrue(testResult.isSuccess()); @@ -603,7 +569,7 @@ public void passingUnitTestMultipleTestCases() { @Test public void failingAssertionUnitTest() { - Alpha alpha = new AlphaImpl(); + Alpha alpha = AlphaFactory.newAlpha(); InputProgram prog = alpha.readProgramString(UNIT_TEST_FAILING_ASSERTION); TestResult testResult = alpha.test(prog); assertFalse(testResult.isSuccess()); @@ -618,7 +584,7 @@ public void failingAssertionUnitTest() { @Test public void failingAnswerSetCountUnitTest() { - Alpha alpha = new AlphaImpl(); + Alpha alpha = AlphaFactory.newAlpha(); InputProgram prog = alpha.readProgramString(UNIT_TEST_FAILING_COUNT); TestResult testResult = alpha.test(prog); assertFalse(testResult.isSuccess()); @@ -678,12 +644,11 @@ public void problematicRun_3col_1119718541727902_sorted_400() throws IOException * -DebugEnableInternalChecks -q -g naive -s default -sort -n 400 -i 3col-20-38.txt */ SystemConfig cfg = new SystemConfig(); - cfg.setGrounderName("naive"); cfg.setSolverName("default"); cfg.setNogoodStoreName("alpharoaming"); cfg.setDebugInternalChecks(true); cfg.setSeed(1119718541727902L); - final Alpha system = new AlphaImpl(cfg); + final Alpha system = AlphaFactory.newAlpha(cfg); final Path path = Paths.get("src", "test", "resources", "PreviouslyProblematic").resolve("3col-20-38.txt"); InputConfig inputCfg = new InputConfig(); @@ -698,12 +663,11 @@ public void problematicRun_3col_1119718541727902_sorted_400() throws IOException private void problematicRun(String program, long seed, int limit) throws IOException { final Path base = Paths.get("src", "test", "resources", "PreviouslyProblematic"); SystemConfig cfg = new SystemConfig(); - cfg.setGrounderName("naive"); cfg.setSolverName("default"); cfg.setNogoodStoreName("alpharoaming"); cfg.setDebugInternalChecks(true); cfg.setSeed(seed); - final Alpha system = new AlphaImpl(cfg); + final Alpha system = AlphaFactory.newAlpha(cfg); InputConfig inputCfg = new InputConfig(); List files = new ArrayList<>(); files.add(base.resolve(program).toString()); @@ -729,11 +693,10 @@ public void testLearnedUnaryNoGoodCausingOutOfOrderLiteralsConflict() throws IOE config.setBranchingHeuristic(Heuristic.valueOf("VSIDS")); config.setDebugInternalChecks(true); config.setDisableJustificationSearch(false); - config.setEvaluateStratifiedPart(false); config.setReplayChoices(Arrays.asList(21, 26, 36, 56, 91, 96, 285, 166, 101, 290, 106, 451, 445, 439, 448, 433, 427, 442, 421, 415, 436, 409, 430, 397, 391, 424, 385, 379, 418, 373, 412, 406, 394, 388, 382, 245, 232, 208)); - Alpha alpha = new AlphaImpl(config); + Alpha alpha = AlphaFactory.newAlpha(config); Optional answerSet = alpha.solve(parsedProgram).findFirst(); assertTrue(answerSet.isPresent()); } diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/api/impl/ReificationTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/api/impl/ReificationTest.java index 4ec2ea622..4131b6f52 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/api/impl/ReificationTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/api/impl/ReificationTest.java @@ -33,7 +33,7 @@ */ public class ReificationTest { - private final Alpha alpha = new AlphaImpl(); + private final Alpha alpha = AlphaFactory.newAlpha(); private static final Map> CMP_OP_IDS; diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/HanoiTowerTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/HanoiTowerTest.java index 7657b7d40..13021d7c5 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/HanoiTowerTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/HanoiTowerTest.java @@ -23,9 +23,9 @@ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package at.ac.tuwien.kr.alpha.core.solver; +package at.ac.tuwien.kr.alpha.regressiontests; -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.runWithTimeout; +import static at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTestUtils.*; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; @@ -33,6 +33,8 @@ import java.util.Optional; import java.util.SortedSet; +import at.ac.tuwien.kr.alpha.api.config.SystemConfig; +import at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTest; import org.junit.jupiter.api.Disabled; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,10 +49,9 @@ import at.ac.tuwien.kr.alpha.commons.programs.atoms.Atoms; import at.ac.tuwien.kr.alpha.commons.programs.terms.Terms; import at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl; -import at.ac.tuwien.kr.alpha.core.test.util.TestUtils; /** - * Tests {@link AbstractSolver} using some hanoi tower test cases (see https://en.wikipedia.org/wiki/Tower_of_Hanoi). + * Tests {@link Solver} using some hanoi tower test cases (see Towers of Hanoi). * */ public class HanoiTowerTest { @@ -62,49 +63,49 @@ public class HanoiTowerTest { @RegressionTest @Disabled("disabled to save resources during CI") - public void testInstance1(RegressionTestConfig cfg) { + public void testInstance1(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testHanoiTower(1, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testInstance2(RegressionTestConfig cfg) { + public void testInstance2(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testHanoiTower(2, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testInstance3(RegressionTestConfig cfg) { + public void testInstance3(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testHanoiTower(3, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testInstance4(RegressionTestConfig cfg) { + public void testInstance4(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testHanoiTower(4, cfg)); } @RegressionTest - public void testSimple(RegressionTestConfig cfg) { - TestUtils.ignoreTestForNaiveSolver(cfg); - TestUtils.ignoreTestForNonDefaultDomainIndependentHeuristics(cfg); + public void testSimple(SystemConfig cfg) { + ignoreTestForNaiveSolver(cfg); + ignoreTestForNonDefaultDomainIndependentHeuristics(cfg); long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testHanoiTower("simple", cfg)); } - private void testHanoiTower(int instance, RegressionTestConfig cfg) throws IOException { + private void testHanoiTower(int instance, SystemConfig cfg) throws IOException { testHanoiTower(String.valueOf(instance), cfg); } - private void testHanoiTower(String instance, RegressionTestConfig cfg) throws IOException { + private void testHanoiTower(String instance, SystemConfig cfg) throws IOException { InputProgram prog = new ProgramParserImpl().parse( Paths.get("src", "test", "resources", "HanoiTower_Alpha.asp"), Paths.get("src", "test", "resources", "HanoiTower_instances", instance + ".asp")); - Solver solver = TestUtils.buildSolverForRegressionTest(prog, cfg); + Solver solver = buildSolverForRegressionTest(prog, cfg); Optional answerSet = solver.stream().findFirst(); assertTrue(answerSet.isPresent()); checkGoal(prog, answerSet.get()); @@ -135,7 +136,7 @@ private int getSteps(InputProgram parsedProgram) { Predicate steps = Predicates.getPredicate("steps", 1); for (Atom atom : parsedProgram.getFacts()) { if (atom.getPredicate().getName().equals(steps.getName()) && atom.getPredicate().getArity() == steps.getArity()) { - return Integer.valueOf(atom.getTerms().get(0).toString()); + return Integer.parseInt(atom.getTerms().get(0).toString()); } } throw new IllegalArgumentException("No steps atom found in input program."); diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/HeadBodyTransformationTests.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/HeadBodyTransformationTests.java index 2f2b91204..07f80ac83 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/HeadBodyTransformationTests.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/HeadBodyTransformationTests.java @@ -36,13 +36,13 @@ import java.util.Optional; import java.util.stream.Collectors; +import at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl; import org.junit.jupiter.api.Disabled; import at.ac.tuwien.kr.alpha.api.AnswerSet; import at.ac.tuwien.kr.alpha.api.Solver; import at.ac.tuwien.kr.alpha.api.config.SystemConfig; import at.ac.tuwien.kr.alpha.api.programs.InputProgram; -import at.ac.tuwien.kr.alpha.core.parser.aspcore2.InputProgramParser; import at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTest; /** * Tests rule transformations described in the following research paper, and their effects on performance: @@ -246,7 +246,7 @@ private InputProgram constructProgramA_TransformationA(int n) { private InputProgram checkNumberOfRulesAndParse(List strRules, int numberOfRules) { assertEquals(numberOfRules, strRules.size()); String strProgram = strRules.stream().collect(Collectors.joining(System.lineSeparator())); - InputProgram parsedProgram = new InputProgramParser().parse(strProgram); + InputProgram parsedProgram = new ProgramParserImpl().parse(strProgram); assertEquals(numberOfRules, parsedProgram.getRules().size()); return parsedProgram; } diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/OmigaBenchmarksTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/OmigaBenchmarksTest.java index 171e0c514..9ae96e5cd 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/OmigaBenchmarksTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/OmigaBenchmarksTest.java @@ -35,17 +35,18 @@ import java.nio.file.Paths; import java.util.Optional; +import at.ac.tuwien.kr.alpha.api.Solver; +import at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl; import org.junit.jupiter.api.Disabled; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.kr.alpha.api.AnswerSet; import at.ac.tuwien.kr.alpha.api.config.SystemConfig; -import at.ac.tuwien.kr.alpha.core.parser.aspcore2.InputProgramParser; import at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTest; /** - * Tests {@link AbstractSolver} using Omiga benchmark problems. + * Tests {@link Solver} using Omiga benchmark problems. * */ // TODO This is actually a performance benchmark and should not be run with standard unit tests @@ -112,7 +113,7 @@ public void testReach_4(SystemConfig cfg) { private void test(String folder, String aspFileName, SystemConfig cfg) throws IOException { @SuppressWarnings("unused") Optional answerSet = buildSolverForRegressionTest( - new InputProgramParser().parse(Files.newInputStream(Paths.get("benchmarks", "omiga", "omiga-testcases", folder, aspFileName))), cfg) + new ProgramParserImpl().parse(Files.newInputStream(Paths.get("benchmarks", "omiga", "omiga-testcases", folder, aspFileName))), cfg) .stream().findFirst(); // System.out.println(answerSet); // TODO: check correctness of answer set diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/RacksTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/RacksTest.java index 6303ec475..7fd811e8a 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/RacksTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/RacksTest.java @@ -32,6 +32,7 @@ import java.nio.file.Paths; import java.util.Optional; +import at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; import org.junit.jupiter.api.Disabled; @@ -39,11 +40,10 @@ import at.ac.tuwien.kr.alpha.api.AnswerSet; import at.ac.tuwien.kr.alpha.api.Solver; import at.ac.tuwien.kr.alpha.api.config.SystemConfig; -import at.ac.tuwien.kr.alpha.core.parser.aspcore2.InputProgramParser; import at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTest; /** - * Tests {@link AbstractSolver} using a racks configuration problem. + * Tests {@link Solver} using a racks configuration problem. * */ // TODO This is a functional test and should not be run with standard unit tests @@ -61,7 +61,7 @@ public void testRacks(SystemConfig cfg) { private void test(SystemConfig cfg) throws IOException { CharStream programInputStream = CharStreams.fromPath( Paths.get("benchmarks", "siemens", "racks", "racks.lp")); - Solver solver = buildSolverForRegressionTest(new InputProgramParser().parse(programInputStream), cfg); + Solver solver = buildSolverForRegressionTest(new ProgramParserImpl().parse(programInputStream), cfg); @SuppressWarnings("unused") Optional answerSet = solver.stream().findFirst(); // System.out.println(answerSet); diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/SolverTests.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/SolverTests.java index da84e80a7..c992ae820 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/SolverTests.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/SolverTests.java @@ -25,16 +25,11 @@ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package at.ac.tuwien.kr.alpha.core.solver; +package at.ac.tuwien.kr.alpha.regressiontests; -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.assertRegressionTestAnswerSet; -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.assertRegressionTestAnswerSets; -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.assertRegressionTestAnswerSetsWithBase; -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest; -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.collectRegressionTestAnswerSets; +import static at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTestUtils.*; import static java.util.Collections.singleton; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import java.util.Collections; import java.util.HashSet; @@ -44,6 +39,7 @@ import at.ac.tuwien.kr.alpha.api.AnswerSet; import at.ac.tuwien.kr.alpha.api.Solver; +import at.ac.tuwien.kr.alpha.api.config.SystemConfig; import at.ac.tuwien.kr.alpha.api.programs.InputProgram; import at.ac.tuwien.kr.alpha.api.programs.Predicate; import at.ac.tuwien.kr.alpha.api.programs.atoms.Atom; @@ -55,9 +51,8 @@ import at.ac.tuwien.kr.alpha.commons.programs.terms.Terms; import at.ac.tuwien.kr.alpha.core.common.AtomStore; import at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl; -import at.ac.tuwien.kr.alpha.core.grounder.ChoiceGrounder; -import at.ac.tuwien.kr.alpha.core.grounder.DummyGrounder; -import at.ac.tuwien.kr.alpha.core.test.util.AnswerSetsParser; +import at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTest; +import at.ac.tuwien.kr.alpha.test.AnswerSetsParser; public class SolverTests { @@ -74,7 +69,7 @@ public int compareTo(Thingy o) { } @RegressionTest - public void testObjectProgram(RegressionTestConfig cfg) { + public void testObjectProgram(SystemConfig cfg) { final Thingy thingy = new Thingy(); final Atom fact = Atoms.newBasicAtom(Predicates.getPredicate("foo", 1), Terms.newConstant(thingy)); @@ -91,8 +86,8 @@ public void testObjectProgram(RegressionTestConfig cfg) { } @RegressionTest - public void testFactsOnlyProgram(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void testFactsOnlyProgram(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "p(a). p(b). foo(13). foo(16). q(a). q(c).", @@ -101,8 +96,8 @@ public void testFactsOnlyProgram(RegressionTestConfig cfg) { } @RegressionTest - public void testSimpleRule(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void testSimpleRule(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "p(a). p(b). r(X) :- p(X).", @@ -111,8 +106,8 @@ public void testSimpleRule(RegressionTestConfig cfg) { } @RegressionTest - public void testSimpleRuleWithGroundPart(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void testSimpleRuleWithGroundPart(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "p(1)." + "p(2)." + @@ -123,8 +118,8 @@ public void testSimpleRuleWithGroundPart(RegressionTestConfig cfg) { } @RegressionTest - public void testProgramZeroArityPredicates(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void testProgramZeroArityPredicates(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "a. p(X) :- b, r(X).", @@ -133,7 +128,7 @@ public void testProgramZeroArityPredicates(RegressionTestConfig cfg) { } @RegressionTest - public void testChoiceGroundProgram(RegressionTestConfig cfg) { + public void testChoiceGroundProgram(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "a :- not b. b :- not a.", @@ -144,7 +139,7 @@ public void testChoiceGroundProgram(RegressionTestConfig cfg) { } @RegressionTest - public void testChoiceProgramNonGround(RegressionTestConfig cfg) { + public void testChoiceProgramNonGround(SystemConfig cfg) { assertRegressionTestAnswerSetsWithBase( cfg, "dom(1). dom(2). dom(3)." + @@ -165,7 +160,7 @@ public void testChoiceProgramNonGround(RegressionTestConfig cfg) { } @RegressionTest - public void choiceProgram3Way(RegressionTestConfig cfg) { + public void choiceProgram3Way(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "a :- not b, not c." + @@ -179,12 +174,12 @@ public void choiceProgram3Way(RegressionTestConfig cfg) { } @RegressionTest - public void emptyProgramYieldsEmptyAnswerSet(RegressionTestConfig cfg) { + public void emptyProgramYieldsEmptyAnswerSet(SystemConfig cfg) { assertRegressionTestAnswerSets(cfg, "", ""); } @RegressionTest - public void chooseMultipleAnswerSets(RegressionTestConfig cfg) { + public void chooseMultipleAnswerSets(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "a :- not nota." + @@ -206,8 +201,8 @@ public void chooseMultipleAnswerSets(RegressionTestConfig cfg) { } @RegressionTest - public void builtinAtoms(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void builtinAtoms(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "dom(1). dom(2). dom(3). dom(4). dom(5)." + "p(X) :- dom(X), X = 4." + @@ -218,8 +213,8 @@ public void builtinAtoms(RegressionTestConfig cfg) { } @RegressionTest - public void builtinAtomsGroundRule(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void builtinAtomsGroundRule(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "a :- 13 != 4." + "b :- 2 != 3, 2 = 3." + @@ -231,8 +226,8 @@ public void builtinAtomsGroundRule(RegressionTestConfig cfg) { @RegressionTest - public void choiceProgramConstraintSimple(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void choiceProgramConstraintSimple(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "fact(a).\n" + "choice(either, X) :- fact(X), not choice(or, X).\n" + @@ -244,8 +239,8 @@ public void choiceProgramConstraintSimple(RegressionTestConfig cfg) { } @RegressionTest - public void choiceProgramConstraintSimple2(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void choiceProgramConstraintSimple2(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "fact(a).\n" + "desired(either).\n" + @@ -258,7 +253,7 @@ public void choiceProgramConstraintSimple2(RegressionTestConfig cfg) { } @RegressionTest - public void choiceProgramConstraint(RegressionTestConfig cfg) { + public void choiceProgramConstraint(SystemConfig cfg) { assertRegressionTestAnswerSetsWithBase( cfg, "eq(1,1)." + @@ -284,7 +279,7 @@ public void choiceProgramConstraint(RegressionTestConfig cfg) { } @RegressionTest - public void choiceProgramConstraintPermutation(RegressionTestConfig cfg) { + public void choiceProgramConstraintPermutation(SystemConfig cfg) { assertRegressionTestAnswerSetsWithBase( cfg, "eq(1,1)." + @@ -310,8 +305,8 @@ public void choiceProgramConstraintPermutation(RegressionTestConfig cfg) { } @RegressionTest - public void simpleNoPropagation(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void simpleNoPropagation(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "val(1,1)." + "val(2,2)." + @@ -322,7 +317,7 @@ public void simpleNoPropagation(RegressionTestConfig cfg) { } @RegressionTest - public void choiceAndPropagationAfterwards(RegressionTestConfig cfg) { + public void choiceAndPropagationAfterwards(SystemConfig cfg) { assertRegressionTestAnswerSetsWithBase( cfg, "node(a)." + @@ -341,7 +336,7 @@ public void choiceAndPropagationAfterwards(RegressionTestConfig cfg) { } @RegressionTest - public void choiceAndConstraints(RegressionTestConfig cfg) { + public void choiceAndConstraints(SystemConfig cfg) { assertRegressionTestAnswerSetsWithBase( cfg, "node(a)." + @@ -360,13 +355,13 @@ public void choiceAndConstraints(RegressionTestConfig cfg) { } @RegressionTest - public void testUnsatisfiableProgram(RegressionTestConfig cfg) { + public void testUnsatisfiableProgram(SystemConfig cfg) { assertRegressionTestAnswerSets(cfg, "p(a). p(b). :- p(a), p(b)."); } @RegressionTest - public void testFunctionTermEquality(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void testFunctionTermEquality(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "r1(f(a,b)). r2(f(a,b)). a :- r1(X), r2(Y), X = Y.", @@ -375,7 +370,7 @@ public void testFunctionTermEquality(RegressionTestConfig cfg) { } @RegressionTest - public void builtinInequality(RegressionTestConfig cfg) { + public void builtinInequality(SystemConfig cfg) { assertRegressionTestAnswerSetsWithBase( cfg, "location(a1)." + @@ -398,7 +393,7 @@ public void builtinInequality(RegressionTestConfig cfg) { } @RegressionTest - public void choiceConstraintsInequality(RegressionTestConfig cfg) { + public void choiceConstraintsInequality(SystemConfig cfg) { assertRegressionTestAnswerSetsWithBase( cfg, "assign(L, R) :- not nassign(L, R), possible(L, R)." + @@ -456,7 +451,7 @@ public void choiceConstraintsInequality(RegressionTestConfig cfg) { } @RegressionTest - public void sameVariableTwiceInAtom(RegressionTestConfig cfg) { + public void sameVariableTwiceInAtom(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "p(a, a)." + @@ -467,7 +462,7 @@ public void sameVariableTwiceInAtom(RegressionTestConfig cfg) { } @RegressionTest - public void sameVariableTwiceInAtomConstraint(RegressionTestConfig cfg) { + public void sameVariableTwiceInAtomConstraint(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "p(a, a)." + @@ -476,7 +471,7 @@ public void sameVariableTwiceInAtomConstraint(RegressionTestConfig cfg) { } @RegressionTest - public void noPositiveSelfFounding(RegressionTestConfig cfg) { + public void noPositiveSelfFounding(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "a :- b." + @@ -486,7 +481,7 @@ public void noPositiveSelfFounding(RegressionTestConfig cfg) { } @RegressionTest - public void noPositiveCycleSelfFoundingChoice(RegressionTestConfig cfg) { + public void noPositiveCycleSelfFoundingChoice(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "c :- not d." + @@ -498,8 +493,8 @@ public void noPositiveCycleSelfFoundingChoice(RegressionTestConfig cfg) { } @RegressionTest - public void conflictFromUnaryNoGood(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void conflictFromUnaryNoGood(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "d(b)." + "sel(X) :- not nsel(X), d(X)." + @@ -512,7 +507,7 @@ public void conflictFromUnaryNoGood(RegressionTestConfig cfg) { } @RegressionTest - public void intervalsInFacts(RegressionTestConfig cfg) { + public void intervalsInFacts(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "a." + @@ -549,7 +544,7 @@ public void intervalsInFacts(RegressionTestConfig cfg) { } @RegressionTest - public void intervalInRules(RegressionTestConfig cfg) { + public void intervalInRules(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "a :- 3 = 1..4 ." + @@ -570,7 +565,7 @@ public void intervalInRules(RegressionTestConfig cfg) { } @RegressionTest - public void emptyIntervals(RegressionTestConfig cfg) { + public void emptyIntervals(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "p(3..1)." + @@ -581,7 +576,7 @@ public void emptyIntervals(RegressionTestConfig cfg) { } @RegressionTest - public void intervalInFunctionTermsInRules(RegressionTestConfig cfg) { + public void intervalInFunctionTermsInRules(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "a :- q(f(1..3,g(4..5)))." + @@ -606,8 +601,8 @@ public void intervalInFunctionTermsInRules(RegressionTestConfig cfg) { } @RegressionTest - public void groundAtomInRule(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void groundAtomInRule(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "p :- dom(X), q, q2." + "dom(1)." + @@ -622,7 +617,7 @@ public void groundAtomInRule(RegressionTestConfig cfg) { } @RegressionTest - public void simpleChoiceRule(RegressionTestConfig cfg) { + public void simpleChoiceRule(SystemConfig cfg) { assertRegressionTestAnswerSetsWithBase( cfg, "{ a; b; c} :- d." + @@ -641,7 +636,7 @@ public void simpleChoiceRule(RegressionTestConfig cfg) { } @RegressionTest - public void conditionalChoiceRule(RegressionTestConfig cfg) { + public void conditionalChoiceRule(SystemConfig cfg) { assertRegressionTestAnswerSetsWithBase( cfg, "dom(1..3)." + @@ -665,7 +660,7 @@ public void conditionalChoiceRule(RegressionTestConfig cfg) { } @RegressionTest - public void doubleChoiceRule(RegressionTestConfig cfg) { + public void doubleChoiceRule(SystemConfig cfg) { Solver solver = buildSolverForRegressionTest("{ a }. { a }.", cfg); // Make sure that no superfluous answer sets that only differ on hidden atoms occur. List actual = solver.collectList(); @@ -674,8 +669,8 @@ public void doubleChoiceRule(RegressionTestConfig cfg) { } @RegressionTest - public void simpleArithmetics(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void simpleArithmetics(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "eight(X) :- X = 4 + 5 - 1." + "three(X) :- X = Z, Y = 1..10, Z = Y / 3, Z > 2, Z < 4.", @@ -684,8 +679,8 @@ public void simpleArithmetics(RegressionTestConfig cfg) { } @RegressionTest - public void arithmeticsMultiplicationBeforeAddition(RegressionTestConfig cfg) { - assertRegressionTestAnswerSet( + public void arithmeticsMultiplicationBeforeAddition(SystemConfig cfg) { + assertRegressionTestAnswerSets( cfg, "seven(X) :- 1+2 * 3 = X.", @@ -696,7 +691,7 @@ public void arithmeticsMultiplicationBeforeAddition(RegressionTestConfig cfg) { * Tests the fix for issue #101 */ @RegressionTest - public void involvedUnsatisfiableProgram(RegressionTestConfig cfg) { + public void involvedUnsatisfiableProgram(SystemConfig cfg) { assertRegressionTestAnswerSets( cfg, "x :- c1, c2, not x." + @@ -711,7 +706,7 @@ public void involvedUnsatisfiableProgram(RegressionTestConfig cfg) { } @RegressionTest - public void instanceEnumerationAtom(RegressionTestConfig cfg) { + public void instanceEnumerationAtom(SystemConfig cfg) { Set answerSets = buildSolverForRegressionTest("# enumeration_predicate_is enum." + "dom(1). dom(2). dom(3)." + "p(X) :- dom(X)." + @@ -728,7 +723,7 @@ public void instanceEnumerationAtom(RegressionTestConfig cfg) { } @RegressionTest - public void instanceEnumerationArbitraryTerms(RegressionTestConfig cfg) { + public void instanceEnumerationArbitraryTerms(SystemConfig cfg) { Set answerSets = buildSolverForRegressionTest("# enumeration_predicate_is enum." + "dom(a). dom(f(a,b)). dom(d)." + "p(X) :- dom(X)." + @@ -745,7 +740,7 @@ public void instanceEnumerationArbitraryTerms(RegressionTestConfig cfg) { } @RegressionTest - public void instanceEnumerationMultipleIdentifiers(RegressionTestConfig cfg) { + public void instanceEnumerationMultipleIdentifiers(SystemConfig cfg) { Set answerSets = buildSolverForRegressionTest("# enumeration_predicate_is enum." + "dom(a). dom(b). dom(c). dom(d)." + "p(X) :- dom(X)." + @@ -765,15 +760,15 @@ public void instanceEnumerationMultipleIdentifiers(RegressionTestConfig cfg) { } private void assertPropositionalPredicateFalse(AnswerSet answerSet, Predicate predicate) { - assertEquals(null, answerSet.getPredicateInstances(predicate)); + assertNull(answerSet.getPredicateInstances(predicate)); } private void assertEnumerationPositions(SortedSet positions, int numPositions) { assertEquals(numPositions, positions.size()); - boolean usedPositions[] = new boolean[numPositions]; + boolean[] usedPositions = new boolean[numPositions]; for (Atom position : positions) { @SuppressWarnings("unchecked") - Integer atomPos = ((ConstantTerm) position.getTerms().get(1)).getObject() - 1; + int atomPos = ((ConstantTerm) position.getTerms().get(1)).getObject() - 1; assertTrue(atomPos < numPositions); usedPositions[atomPos] = true; } @@ -783,7 +778,7 @@ private void assertEnumerationPositions(SortedSet positions, int numPositi } @RegressionTest - public void smallCardinalityAggregate(RegressionTestConfig cfg) { + public void smallCardinalityAggregate(SystemConfig cfg) { assertRegressionTestAnswerSetsWithBase( cfg, "dom(1..3)." + @@ -804,16 +799,17 @@ public void smallCardinalityAggregate(RegressionTestConfig cfg) { ); } - @RegressionTest - public void dummyGrounder(RegressionTestConfig cfg) { - AtomStore atomStore = new AtomStoreImpl(); - assertEquals(DummyGrounder.EXPECTED, buildSolverForRegressionTest(atomStore, new DummyGrounder(atomStore), cfg).collectSet()); - } - - @RegressionTest - public void choiceGrounder(RegressionTestConfig cfg) { - AtomStore atomStore = new AtomStoreImpl(); - assertEquals(ChoiceGrounder.EXPECTED, buildSolverForRegressionTest(atomStore, new ChoiceGrounder(atomStore), cfg).collectSet()); - } + // TODO these look obsolete - confirm if they can be removed +// @RegressionTest +// public void dummyGrounder(SystemConfig cfg) { +// AtomStore atomStore = new AtomStoreImpl(); +// assertEquals(DummyGrounder.EXPECTED, buildSolverForRegressionTest(atomStore, new DummyGrounder(atomStore), cfg).collectSet()); +// } +// +// @RegressionTest +// public void choiceGrounder(SystemConfig cfg) { +// AtomStore atomStore = new AtomStoreImpl(); +// assertEquals(ChoiceGrounder.EXPECTED, buildSolverForRegressionTest(atomStore, new ChoiceGrounder(atomStore), cfg).collectSet()); +// } } diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/StratifiedEvaluationRegressionTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/StratifiedEvaluationRegressionTest.java index 4884832c2..db0f40009 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/StratifiedEvaluationRegressionTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/StratifiedEvaluationRegressionTest.java @@ -10,6 +10,8 @@ import java.util.Set; import java.util.function.Consumer; +import at.ac.tuwien.kr.alpha.commons.programs.atoms.Atoms; +import at.ac.tuwien.kr.alpha.commons.programs.terms.Terms; import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -26,8 +28,6 @@ import at.ac.tuwien.kr.alpha.api.programs.NormalProgram; import at.ac.tuwien.kr.alpha.api.programs.Predicate; import at.ac.tuwien.kr.alpha.commons.Predicates; -import at.ac.tuwien.kr.alpha.commons.atoms.Atoms; -import at.ac.tuwien.kr.alpha.commons.terms.Terms; // TODO This is a functional test and should not be run with standard unit tests public class StratifiedEvaluationRegressionTest { @@ -113,7 +113,7 @@ public void runTest(String aspString, Consumer programVerifier, LOGGER.debug("Testing ASP String {}", aspString); // Parse and pre-evaluate program // Alpha instance with default config, stratified evaluation enabled - Alpha alpha = new AlphaFactory().newAlpha(); + Alpha alpha =AlphaFactory.newAlpha(); InputProgram input = alpha.readProgramString(aspString); DebugSolvingContext dbgInfo = alpha.prepareDebugSolve(input); NormalProgram evaluated = dbgInfo.getPreprocessedProgram(); @@ -129,7 +129,7 @@ private static void verifyProgramBasic(NormalProgram evaluated) { assertFactsContainedInProgram(evaluated, Atoms.newBasicAtom(Predicates.getPredicate("a", 0)), Atoms.newBasicAtom(Predicates.getPredicate("b", 0))); assertEquals(2, evaluated.getFacts().size()); - assertTrue(evaluated.getRules().size() == 0); + assertTrue(evaluated.getRules().isEmpty()); } private static void verifyAnswerSetsBasic(Set answerSets) { @@ -140,7 +140,7 @@ private static void verifyProgramBasicMultiInstance(NormalProgram evaluated) { assertFactsContainedInProgram(evaluated, Atoms.newBasicAtom(Predicates.getPredicate("q", 1), Terms.newSymbolicConstant("a")), Atoms.newBasicAtom(Predicates.getPredicate("q", 1), Terms.newSymbolicConstant("b"))); - assertTrue(evaluated.getRules().size() == 0); + assertTrue(evaluated.getRules().isEmpty()); } private static void verifyAnswerSetsBasicMultiInstance(Set answerSets) { diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringRandomGraphTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringRandomGraphTest.java index 9c8388297..1740d5133 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringRandomGraphTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringRandomGraphTest.java @@ -23,16 +23,15 @@ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package at.ac.tuwien.kr.alpha.core.solver; - -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest; -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.runWithTimeout; +package at.ac.tuwien.kr.alpha.regressiontests; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.Random; +import at.ac.tuwien.kr.alpha.api.config.SystemConfig; +import at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTest; import org.junit.jupiter.api.Disabled; import at.ac.tuwien.kr.alpha.api.AnswerSet; @@ -46,59 +45,62 @@ import at.ac.tuwien.kr.alpha.commons.programs.terms.Terms; import at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl; +import static at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTestUtils.buildSolverForRegressionTest; +import static at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTestUtils.runWithTimeout; + public class ThreeColouringRandomGraphTest { private static final long DEBUG_TIMEOUT_FACTOR = 5; @RegressionTest - public void testV3E3(RegressionTestConfig cfg) { + public void testV3E3(SystemConfig cfg) { long timeout = 1000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(3, 3, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testV10E18(RegressionTestConfig cfg) { + public void testV10E18(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(10, 18, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testV20E38(RegressionTestConfig cfg) { + public void testV20E38(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(20, 38, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testV30E48(RegressionTestConfig cfg) { + public void testV30E48(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(30, 48, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testV200E300(RegressionTestConfig cfg) { + public void testV200E300(SystemConfig cfg) { long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(200, 300, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testV300E200(RegressionTestConfig cfg) { + public void testV300E200(SystemConfig cfg) { long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(300, 200, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testV300E300(RegressionTestConfig cfg) { + public void testV300E300(SystemConfig cfg) { long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(300, 300, cfg)); } - private void testThreeColouring(int nVertices, int nEdges, RegressionTestConfig cfg) { + private void testThreeColouring(int nVertices, int nEdges, SystemConfig cfg) { InputProgram tmpPrg = new ProgramParserImpl().parse( "blue(N) :- v(N), not red(N), not green(N)." + "red(N) :- v(N), not blue(N), not green(N)." + diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringTestWithRandom.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringTestWithRandom.java index 8f0c32c05..7af25b780 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringTestWithRandom.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringTestWithRandom.java @@ -23,10 +23,7 @@ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package at.ac.tuwien.kr.alpha.core.solver; - -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest; -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.runWithTimeout; +package at.ac.tuwien.kr.alpha.regressiontests; import java.util.ArrayList; import java.util.Collections; @@ -34,6 +31,8 @@ import java.util.Optional; import java.util.Random; +import at.ac.tuwien.kr.alpha.api.config.SystemConfig; +import at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTest; import org.junit.jupiter.api.Disabled; import at.ac.tuwien.kr.alpha.api.AnswerSet; @@ -49,8 +48,11 @@ import at.ac.tuwien.kr.alpha.commons.programs.terms.Terms; import at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl; +import static at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTestUtils.buildSolverForRegressionTest; +import static at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTestUtils.runWithTimeout; + /** - * Tests {@link AbstractSolver} using some three-coloring test cases, as described in: + * Tests {@link Solver} using some three-coloring test cases, as described in: * Lefèvre, Claire; Béatrix, Christopher; Stéphan, Igor; Garcia, Laurent (2017): * ASPeRiX, a first-order forward chaining approach for answer set computing. * In Theory and Practice of Logic Programming, pp. 1-45. DOI: @@ -61,130 +63,130 @@ public class ThreeColouringTestWithRandom { private static final long DEBUG_TIMEOUT_FACTOR = 5; @RegressionTest - public void testN3(RegressionTestConfig cfg) { + public void testN3(SystemConfig cfg) { long timeout = 3000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(3, false, 0, cfg)); } @RegressionTest - public void testN4(RegressionTestConfig cfg) { + public void testN4(SystemConfig cfg) { long timeout = 4000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(4, false, 0, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN5(RegressionTestConfig cfg) { + public void testN5(SystemConfig cfg) { long timeout = 5000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(5, false, 0, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN6(RegressionTestConfig cfg) { + public void testN6(SystemConfig cfg) { long timeout = 6000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(6, false, 0, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN7(RegressionTestConfig cfg) { + public void testN7(SystemConfig cfg) { long timeout = 7000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(7, false, 0, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN8(RegressionTestConfig cfg) { + public void testN8(SystemConfig cfg) { long timeout = 8000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(8, false, 0, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN9(RegressionTestConfig cfg) { + public void testN9(SystemConfig cfg) { long timeout = 9000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(9, false, 0, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN10(RegressionTestConfig cfg) { + public void testN10(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(10, false, 0, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN10Random0(RegressionTestConfig cfg) { + public void testN10Random0(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(10, true, 0, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN10Random1(RegressionTestConfig cfg) { + public void testN10Random1(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(10, true, 1, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN10Random2(RegressionTestConfig cfg) { + public void testN10Random2(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(10, true, 2, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN10Random3(RegressionTestConfig cfg) { + public void testN10Random3(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(10, true, 3, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN19(RegressionTestConfig cfg) { + public void testN19(SystemConfig cfg) { long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(19, false, 0, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN19Random0(RegressionTestConfig cfg) { + public void testN19Random0(SystemConfig cfg) { long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(19, true, 0, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN19Random1(RegressionTestConfig cfg) { + public void testN19Random1(SystemConfig cfg) { long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(19, true, 1, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN19Random2(RegressionTestConfig cfg) { + public void testN19Random2(SystemConfig cfg) { long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(19, true, 2, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN19Random3(RegressionTestConfig cfg) { + public void testN19Random3(SystemConfig cfg) { long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(19, true, 3, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN101(RegressionTestConfig cfg) { + public void testN101(SystemConfig cfg) { long timeout = 10000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(101, false, 0, cfg)); } - private void testThreeColouring(int n, boolean shuffle, int seed, RegressionTestConfig cfg) { + private void testThreeColouring(int n, boolean shuffle, int seed, SystemConfig cfg) { InputProgram tmpPrg = new ProgramParserImpl() .parse("col(V,C) :- v(V), c(C), not ncol(V,C)." + "ncol(V,C) :- col(V,D), c(C), C != D." + ":- e(V,U), col(V,C), col(U,C)."); InputProgramBuilder prgBuilder = Programs.builder().accumulate(tmpPrg); diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringWheelTest.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringWheelTest.java index 1470708ca..a3ad1c447 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringWheelTest.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/ThreeColouringWheelTest.java @@ -23,15 +23,14 @@ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package at.ac.tuwien.kr.alpha.core.solver; - -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest; -import static at.ac.tuwien.kr.alpha.core.test.util.TestUtils.runWithTimeout; +package at.ac.tuwien.kr.alpha.regressiontests; import java.util.ArrayList; import java.util.List; import java.util.Optional; +import at.ac.tuwien.kr.alpha.api.config.SystemConfig; +import at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTest; import org.junit.jupiter.api.Disabled; import at.ac.tuwien.kr.alpha.api.AnswerSet; @@ -47,8 +46,11 @@ import at.ac.tuwien.kr.alpha.commons.programs.terms.Terms; import at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl; +import static at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTestUtils.buildSolverForRegressionTest; +import static at.ac.tuwien.kr.alpha.regressiontests.util.RegressionTestUtils.runWithTimeout; + /** - * Tests {@link AbstractSolver} using some three-coloring test cases, as described in: + * Tests {@link Solver} using some three-coloring test cases, as described in: * Lefèvre, Claire; Béatrix, Christopher; Stéphan, Igor; Garcia, Laurent (2017): * ASPeRiX, a first-order forward chaining approach for answer set computing. * In Theory and Practice of Logic Programming, pp. 1-45. @@ -59,46 +61,46 @@ public class ThreeColouringWheelTest { private static final long DEBUG_TIMEOUT_FACTOR = 5; @RegressionTest - public void testN4(RegressionTestConfig cfg) { + public void testN4(SystemConfig cfg) { long timeout = 1000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(4, cfg)); } @RegressionTest - public void testN5(RegressionTestConfig cfg) { + public void testN5(SystemConfig cfg) { long timeout = 1000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(5, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN6(RegressionTestConfig cfg) { + public void testN6(SystemConfig cfg) { long timeout = 6000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(6, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN3(RegressionTestConfig cfg) { + public void testN3(SystemConfig cfg) { long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(3, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN7(RegressionTestConfig cfg) { + public void testN7(SystemConfig cfg) { long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(7, cfg)); } @RegressionTest @Disabled("disabled to save resources during CI") - public void testN11(RegressionTestConfig cfg) { + public void testN11(SystemConfig cfg) { long timeout = 60000L; runWithTimeout(cfg, timeout, DEBUG_TIMEOUT_FACTOR, () -> testThreeColouring(11, cfg)); } - private void testThreeColouring(int n, RegressionTestConfig cfg) { + private void testThreeColouring(int n, SystemConfig cfg) { InputProgram tmpPrg = new ProgramParserImpl().parse( "col(V,C) :- v(V), c(C), not ncol(V,C)." + "ncol(V,C) :- col(V,D), c(C), C != D." + diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/util/RegressionTestConfigProvider.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/util/RegressionTestConfigProvider.java index 4d5823aa1..93a5e3ad7 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/util/RegressionTestConfigProvider.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/util/RegressionTestConfigProvider.java @@ -21,7 +21,7 @@ public class RegressionTestConfigProvider { private static final boolean DEFAULT_ENABLE_DEBUG_CHECKS = false; /** - * Creates a list of {@link RegressionTestConfig}s with all config combinations that are to be tested im methods tagged using + * Creates a list of {@link SystemConfig}s with all config combinations that are to be tested im methods tagged using * "RegressionTest" annotation. * Exact number of combinations depends on the "CI" environment variable that can be used to signal that a test is being run in a CI * environment. @@ -80,7 +80,7 @@ private static List buildConfigs() { } /** - * Provides {@link RegressionTestConfig}s specifically for tests concerned with AggregateRewriting. + * Provides {@link SystemConfig}s specifically for tests concerned with AggregateRewriting. * All parameters fixed to default values except stratified evaluation, sorting grid encoding for count rewriting * and negative sum element support. * @@ -135,7 +135,7 @@ public static List provideAggregateTestConfigs() { return retVal; } - private static final String[] nonDeprecatedHeuristics() { + private static String[] nonDeprecatedHeuristics() { final List nonDeprecatedHeuristicsNames = new ArrayList<>(); for (Field field : Heuristic.class.getFields()) { if (field.getAnnotation(Deprecated.class) == null) { diff --git a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/util/RegressionTestUtils.java b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/util/RegressionTestUtils.java index 686df4990..0629c8be1 100644 --- a/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/util/RegressionTestUtils.java +++ b/alpha-solver/src/test/java/at/ac/tuwien/kr/alpha/regressiontests/util/RegressionTestUtils.java @@ -32,30 +32,30 @@ public static void runWithTimeout(SystemConfig cfg, long baseTimeout, long timeo } public static Solver buildSolverForRegressionTest(String programString, SystemConfig cfg) { - Alpha alpha = new AlphaFactory().newAlpha(cfg); + Alpha alpha = AlphaFactory.newAlpha(cfg); InputProgram program = alpha.readProgramString(programString); return alpha.prepareSolverFor(program, InputConfig.DEFAULT_FILTER); } public static Solver buildSolverForRegressionTest(InputProgram program, SystemConfig cfg) { - Alpha alpha = new AlphaFactory().newAlpha(cfg); + Alpha alpha =AlphaFactory.newAlpha(cfg); return alpha.prepareSolverFor(program, InputConfig.DEFAULT_FILTER); } public static Set collectRegressionTestAnswerSets(String program, SystemConfig cfg) { - Alpha alpha = new AlphaFactory().newAlpha(cfg); + Alpha alpha = AlphaFactory.newAlpha(cfg); InputProgram in = alpha.readProgramString(program); return alpha.solve(in).collect(Collectors.toSet()); } public static Set collectRegressionTestAnswerSets(InputProgram program, SystemConfig cfg) { - return new AlphaFactory().newAlpha(cfg) + return AlphaFactory.newAlpha(cfg) .solve(program) .collect(Collectors.toSet()); } private static Set solveForConfig(String programString, SystemConfig cfg) { - Alpha alpha = new AlphaFactory().newAlpha(cfg); + Alpha alpha = AlphaFactory.newAlpha(cfg); InputProgram program = alpha.readProgramString(programString); return alpha.solve(program).collect(Collectors.toSet()); }