Search in sources :

Example 26 with Alpha

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

the class AlphaImplTest method withExternalSubtype.

@Test
public void withExternalSubtype() throws Exception {
    SubThingy thingy = new SubThingy();
    BasicRule rule = new BasicRule(Heads.newNormalHead(Atoms.newBasicAtom(Predicates.getPredicate("p", 1), Terms.newConstant("x"))), singletonList(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 AlphaImpl();
    InputProgram prog = new InputProgram(singletonList(rule), emptyList(), new InlineDirectivesImpl());
    Set<AnswerSet> actual = system.solve(prog).collect(Collectors.toSet());
    Set<AnswerSet> expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("p").instance("x").build()));
    assertEquals(expected, actual);
}
Also used : BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) MethodPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.MethodPredicateInterpretation) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) InlineDirectivesImpl(at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 27 with Alpha

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

the class AlphaImplTest method smallGraphCoolNode.

@Test
public void smallGraphCoolNode() throws Exception {
    Alpha system = new AlphaImpl();
    InputConfig cfg = InputConfig.forString("node(1..2). in(X) :- node(X), &coolNode[X].");
    cfg.addPredicateMethod("coolNode", Externals.processPredicateMethod(this.getClass().getMethod("coolNode", int.class)));
    ASPCore2Program prog = system.readProgram(cfg);
    Set<AnswerSet> actual = system.solve(prog).collect(Collectors.toSet());
    Set<AnswerSet> expected = AnswerSetsParser.parse("{ in(1), node(1), node(2) }");
    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 28 with Alpha

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

the class AlphaImplTest method noInput.

@Test
public void noInput() throws Exception {
    Alpha system = new AlphaImpl();
    InputConfig cfg = InputConfig.forString("node(1). a :- &bestNode(X), node(X).");
    cfg.addPredicateMethod("bestNode", Externals.processPredicateMethod(this.getClass().getMethod("bestNode")));
    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 29 with Alpha

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

the class AlphaImplTest method smallGraphNoNeighbors.

@Test
@Disabled("Test program is not safe (external lacking output variables). This should throw some exception.")
public void smallGraphNoNeighbors() throws Exception {
    Alpha system = new AlphaImpl();
    InputConfig cfg = InputConfig.forString("noNeighbors(2) :- not &neighbors[2].");
    cfg.addPredicateMethod("neighbors", Externals.processPredicateMethod(this.getClass().getMethod("neighbors", int.class)));
    ASPCore2Program prog = system.readProgram(cfg);
    Set<AnswerSet> expected = AnswerSetsParser.parse("{ noNeighbors(2) }");
    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 30 with Alpha

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

the class AlphaImplTest method withNegatedExternal.

@Test
@SuppressWarnings("unchecked")
public void withNegatedExternal() throws IOException {
    Alpha alpha = new AlphaImpl();
    ASPCore2Program prog = alpha.readProgram(InputConfig.forString(NEGATED_EXTERNAL_ASP));
    Set<AnswerSet> answerSets = alpha.solve(prog).collect(Collectors.toSet());
    assertEquals(31, answerSets.size());
    // Verify every result string has length 6 and contains "foo"
    for (AnswerSet as : answerSets) {
        for (Atom atom : as.getPredicateInstances(Predicates.getPredicate("resultstring", 1))) {
            String resultstring = ((ConstantTerm<String>) atom.getTerms().get(0)).getObject();
            LOGGER.debug("ResultString is {}", resultstring);
            assertEquals(6, resultstring.length());
            assertTrue(resultstring.contains("foo"));
        }
    }
}
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) 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

Alpha (at.ac.tuwien.kr.alpha.api.Alpha)30 Test (org.junit.jupiter.api.Test)28 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)24 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)21 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)20 HashSet (java.util.HashSet)13 AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)12 Disabled (org.junit.jupiter.api.Disabled)6 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)4 AlphaImpl (at.ac.tuwien.kr.alpha.api.impl.AlphaImpl)4 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)4 DebugSolvingContext (at.ac.tuwien.kr.alpha.api.DebugSolvingContext)3 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)3 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 ProgramParser (at.ac.tuwien.kr.alpha.api.programs.ProgramParser)2 MethodPredicateInterpretation (at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.MethodPredicateInterpretation)2 InlineDirectivesImpl (at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl)2 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)2