Search in sources :

Example 6 with AnswerSetBuilder

use of at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder in project Alpha by alpha-asp.

the class AlphaImplTest method withExternalInvocationCounted3.

@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();
    InputConfig cfg = InputConfig.forString("a :- &isOne[1], not &isOne[2].");
    cfg.addPredicateMethod("isOne", Externals.processPredicateMethod(this.getClass().getMethod("isOne", int.class)));
    ASPCore2Program prog = system.readProgram(cfg);
    int before = invocations;
    Set<AnswerSet> actual = system.solve(prog).collect(Collectors.toSet());
    int after = invocations;
    assertEquals(1, after - before);
    Set<AnswerSet> expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("a").build()));
    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) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 7 with AnswerSetBuilder

use of at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder in project Alpha by alpha-asp.

the class AlphaImplTest method withExternalInvocationCounted2.

@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();
    InputConfig cfg = InputConfig.forString("a. b :- &isOne[1], &isOne[2].");
    cfg.addPredicateMethod("isOne", Externals.processPredicateMethod(this.getClass().getMethod("isOne", int.class)));
    ASPCore2Program prog = system.readProgram(cfg);
    int before = invocations;
    Set<AnswerSet> actual = system.solve(prog).collect(Collectors.toSet());
    int after = invocations;
    assertEquals(2, after - before);
    Set<AnswerSet> expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("a").build()));
    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) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 8 with AnswerSetBuilder

use of at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder in project Alpha by alpha-asp.

the class AlphaImplTest method withNativeExternal.

@Test
public void withNativeExternal() throws Exception {
    Alpha system = new AlphaImpl();
    InputConfig cfg = InputConfig.forString("a :- &isTwo[2].");
    cfg.addPredicateMethod("isTwo", Externals.processPredicate((Integer t) -> t == 2));
    ASPCore2Program prog = system.readProgram(cfg);
    Set<AnswerSet> actual = system.solve(prog).collect(Collectors.toSet());
    Set<AnswerSet> expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("a").build()));
    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) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 9 with AnswerSetBuilder

use of at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder in project Alpha by alpha-asp.

the class SolverTests method testObjectProgram.

@RegressionTest
public void testObjectProgram(RegressionTestConfig cfg) {
    final Thingy thingy = new Thingy();
    final Atom fact = Atoms.newBasicAtom(Predicates.getPredicate("foo", 1), Terms.newConstant(thingy));
    final InputProgram program = new InputProgram(Collections.emptyList(), Collections.singletonList(fact), new InlineDirectivesImpl());
    assertEquals(singleton(new AnswerSetBuilder().predicate("foo").instance(thingy).build()), collectRegressionTestAnswerSets(program, cfg));
}
Also used : AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) InlineDirectivesImpl(at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram) TestUtils.buildSolverForRegressionTest(at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest)

Example 10 with AnswerSetBuilder

use of at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder in project Alpha by alpha-asp.

the class AnswerSetQueryTest method matchEvenIntegers.

@Test
public void matchEvenIntegers() {
    AnswerSetBuilder bld = new AnswerSetBuilder();
    bld.predicate("p").instance(1).instance(2).instance(3).instance(4).instance(5).instance("bla").symbolicInstance("blubb");
    AnswerSet as = bld.build();
    java.util.function.Predicate<Term> isInteger = (term) -> {
        if (!(term instanceof ConstantTerm<?>)) {
            return false;
        }
        String strValue = ((ConstantTerm<?>) term).getObject().toString();
        return strValue.matches("[0-9]+");
    };
    AnswerSetQueryImpl evenIntegers = AnswerSetQueryImpl.forPredicate(Predicates.getPredicate("p", 1)).withFilter(0, isInteger.and((term) -> Integer.valueOf(((ConstantTerm<?>) term).getObject().toString()) % 2 == 0));
    List<Atom> queryResult = as.query(evenIntegers);
    assertEquals(2, queryResult.size());
    for (Atom atom : queryResult) {
        ConstantTerm<?> term = (ConstantTerm<?>) atom.getTerms().get(0);
        assertTrue(Integer.valueOf(term.getObject().toString()) % 2 == 0);
    }
}
Also used : Term(at.ac.tuwien.kr.alpha.api.terms.Term) SortedSet(java.util.SortedSet) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) AnswerSets(at.ac.tuwien.kr.alpha.commons.AnswerSets) HashMap(java.util.HashMap) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Atoms(at.ac.tuwien.kr.alpha.commons.atoms.Atoms) TreeSet(java.util.TreeSet) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Test(org.junit.jupiter.api.Test) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) List(java.util.List) Predicates(at.ac.tuwien.kr.alpha.commons.Predicates) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Aggregations

AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)21 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)20 Test (org.junit.jupiter.api.Test)20 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)12 HashSet (java.util.HashSet)12 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)8 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)7 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)7 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)4 Disabled (org.junit.jupiter.api.Disabled)4 InlineDirectivesImpl (at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl)3 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)2 Predicates (at.ac.tuwien.kr.alpha.commons.Predicates)2 Atoms (at.ac.tuwien.kr.alpha.commons.atoms.Atoms)2 Terms (at.ac.tuwien.kr.alpha.commons.terms.Terms)2 MethodPredicateInterpretation (at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.MethodPredicateInterpretation)2 BasicRule (at.ac.tuwien.kr.alpha.core.rules.BasicRule)2 Path (java.nio.file.Path)2 List (java.util.List)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2