Search in sources :

Example 16 with AnswerSet

use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.

the class AlphaImplTest method withExternalTypeConflict.

@Test
public void withExternalTypeConflict() {
    assertThrows(IllegalArgumentException.class, () -> {
        Alpha system = new AlphaImpl();
        InputConfig inputCfg = InputConfig.forString("a :- &isFoo[\"adsfnfdsf\"].");
        inputCfg.addPredicateMethod("isFoo", Externals.processPredicateMethod(this.getClass().getMethod("isFoo", Integer.class)));
        Set<AnswerSet> actual = system.solve(system.readProgram(inputCfg)).collect(Collectors.toSet());
        Set<AnswerSet> expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("a").build()));
        assertEquals(expected, actual);
    });
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 17 with AnswerSet

use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.

the class AlphaImplTest method smallGraphSingleNeighborNoTerm.

@Test
@Disabled("Test program is not safe (external lacking output variables). This should throw some exception.")
public void smallGraphSingleNeighborNoTerm() throws Exception {
    Alpha system = new AlphaImpl();
    InputConfig cfg = InputConfig.forString("success :- &neighbors[1], not &neighbors[2].");
    cfg.addPredicateMethod("neighbors", Externals.processPredicateMethod(this.getClass().getMethod("neighbors", int.class)));
    ASPCore2Program prog = system.readProgram(cfg);
    Set<AnswerSet> expected = AnswerSetsParser.parse("{ success }");
    Set<AnswerSet> actual = system.solve(prog).collect(Collectors.toSet());
    assertEquals(expected, actual);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 18 with AnswerSet

use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.

the class AlphaImplTest method filterTest.

/**
 * Verifies that filters are handled correctly (regression test case introduced when fixing issue #189).
 */
@Test
public void filterTest() {
    String progstr = "a. b. c. d :- c. e(a, b) :- d.";
    Alpha system = new AlphaImpl();
    ASPCore2Program prog = system.readProgramString(progstr);
    Set<AnswerSet> actual = system.solve(prog, (p) -> p.equals(Predicates.getPredicate("a", 0)) || p.equals(Predicates.getPredicate("e", 2))).collect(Collectors.toSet());
    Set<AnswerSet> expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("a").predicate("e").symbolicInstance("a", "b").build()));
    assertEquals(expected, actual);
}
Also used : Arrays(java.util.Arrays) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) LoggerFactory(org.slf4j.LoggerFactory) Heads(at.ac.tuwien.kr.alpha.commons.rules.heads.Heads) Disabled(org.junit.jupiter.api.Disabled) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig) Collections.singletonList(java.util.Collections.singletonList) Externals(at.ac.tuwien.kr.alpha.core.externals.Externals) Collections.singleton(java.util.Collections.singleton) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Arrays.asList(java.util.Arrays.asList) Literals(at.ac.tuwien.kr.alpha.commons.literals.Literals) Path(java.nio.file.Path) Collections.emptyList(java.util.Collections.emptyList) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) StandardOpenOption(java.nio.file.StandardOpenOption) Set(java.util.Set) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Atoms(at.ac.tuwien.kr.alpha.commons.atoms.Atoms) Collectors(java.util.stream.Collectors) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) Test(org.junit.jupiter.api.Test) List(java.util.List) AspStandardLibrary(at.ac.tuwien.kr.alpha.core.externals.AspStandardLibrary) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Optional(java.util.Optional) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) ArrayList(java.util.ArrayList) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) HashSet(java.util.HashSet) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) Files(java.nio.file.Files) MethodPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.MethodPredicateInterpretation) Heuristic(at.ac.tuwien.kr.alpha.api.config.Heuristic) InlineDirectivesImpl(at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl) IOException(java.io.IOException) Predicates(at.ac.tuwien.kr.alpha.commons.Predicates) Paths(java.nio.file.Paths) SystemConfig(at.ac.tuwien.kr.alpha.api.config.SystemConfig) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 19 with AnswerSet

use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.

the class AlphaImplTest method supplier.

@Test
public void supplier() throws Exception {
    Alpha system = new AlphaImpl();
    InputConfig cfg = InputConfig.forString("node(1). a :- &bestNode(X), node(X).");
    cfg.addPredicateMethod("bestNode", Externals.processPredicate(() -> singleton(singletonList(Terms.newConstant(1)))));
    ASPCore2Program prog = system.readProgram(cfg);
    Set<AnswerSet> expected = AnswerSetsParser.parse("{ node(1), a }");
    Set<AnswerSet> actual = system.solve(prog).collect(Collectors.toSet());
    assertEquals(expected, actual);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig) Test(org.junit.jupiter.api.Test)

Example 20 with AnswerSet

use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.

the class ThreeColouringTestWithRandom method testThreeColouring.

private void testThreeColouring(int n, boolean shuffle, int seed, RegressionTestConfig cfg) {
    ASPCore2Program 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).");
    InputProgram.Builder prgBuilder = InputProgram.builder().accumulate(tmpPrg);
    prgBuilder.addFacts(createColors("1", "2", "3"));
    prgBuilder.addFacts(createVertices(n));
    prgBuilder.addFacts(createEdges(n, shuffle, seed));
    InputProgram program = prgBuilder.build();
    Solver solver = buildSolverForRegressionTest(program, cfg);
    @SuppressWarnings("unused") Optional<AnswerSet> answerSet = solver.stream().findAny();
// System.out.println(answerSet);
// TODO: check correctness of answer set
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Solver(at.ac.tuwien.kr.alpha.api.Solver) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Aggregations

AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)80 Test (org.junit.jupiter.api.Test)64 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)25 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)24 AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)20 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)17 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)14 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)14 HashSet (java.util.HashSet)13 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)10 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)9 Solver (at.ac.tuwien.kr.alpha.api.Solver)8 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)7 Disabled (org.junit.jupiter.api.Disabled)7 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)4 ProgramParser (at.ac.tuwien.kr.alpha.api.programs.ProgramParser)4 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)4 TestUtils.assertRegressionTestAnswerSet (at.ac.tuwien.kr.alpha.core.test.util.TestUtils.assertRegressionTestAnswerSet)4 TestUtils.buildSolverForRegressionTest (at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest)4 HashMap (java.util.HashMap)4