Search in sources :

Example 26 with ASPCore2Program

use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program 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 27 with ASPCore2Program

use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program 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 28 with ASPCore2Program

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

the class StratificationAlgorithmTest method stratifyTwoRulesTest.

@Test
public void stratifyTwoRulesTest() {
    StringBuilder bld = new StringBuilder();
    bld.append("b :- a.").append("\n");
    bld.append("c :- b.").append("\n");
    ASPCore2Program prog = parser.parse(bld.toString());
    NormalProgram normalProg = normalizeTransform.apply(prog);
    AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
    DependencyGraph dg = analyzed.getDependencyGraph();
    ComponentGraph cg = ComponentGraphImpl.buildComponentGraph(dg, StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg));
    List<SCComponent> strata = StratificationAlgorithm.calculateStratification(cg);
    Predicate a = Predicates.getPredicate("a", 0);
    Predicate b = Predicates.getPredicate("b", 0);
    Predicate c = Predicates.getPredicate("c", 0);
    assertEquals(3, strata.size());
    assertTrue(predicateIsBeforePredicateInOrder(a, b, strata));
    assertTrue(predicateIsBeforePredicateInOrder(b, c, strata));
    assertTrue(predicateIsBeforePredicateInOrder(a, c, strata));
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) SCComponent(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 29 with ASPCore2Program

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

the class StratificationAlgorithmTest method avoidDuplicatesTest1.

@Test
public void avoidDuplicatesTest1() {
    StringBuilder bld = new StringBuilder();
    bld.append("b :- a.");
    bld.append("c :- b.");
    bld.append("c :- a.");
    ASPCore2Program prog = parser.parse(bld.toString());
    NormalProgram normalProg = normalizeTransform.apply(prog);
    AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
    DependencyGraph dg = analyzed.getDependencyGraph();
    ComponentGraph cg = ComponentGraphImpl.buildComponentGraph(dg, StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg));
    List<SCComponent> strata = StratificationAlgorithm.calculateStratification(cg);
    Predicate a = Predicates.getPredicate("a", 0);
    Predicate b = Predicates.getPredicate("b", 0);
    Predicate c = Predicates.getPredicate("c", 0);
    assertTrue(predicateIsBeforePredicateInOrder(a, b, strata));
    assertTrue(predicateIsBeforePredicateInOrder(b, c, strata));
    assertTrue(predicateIsBeforePredicateInOrder(a, c, strata));
    assertEquals(3, strata.size());
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) SCComponent(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 30 with ASPCore2Program

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

the class StratificationAlgorithmTest method stratifyOneRuleTest.

@Test
public void stratifyOneRuleTest() {
    ASPCore2Program prog = parser.parse("a :- b.");
    NormalProgram normalProg = normalizeTransform.apply(prog);
    AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
    DependencyGraph dg = analyzed.getDependencyGraph();
    ComponentGraph cg = ComponentGraphImpl.buildComponentGraph(dg, StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg));
    List<SCComponent> strata = StratificationAlgorithm.calculateStratification(cg);
    Predicate a = Predicates.getPredicate("a", 0);
    Predicate b = Predicates.getPredicate("b", 0);
    assertEquals(2, strata.size());
    assertTrue(predicateIsBeforePredicateInOrder(b, a, strata));
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) SCComponent(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Aggregations

ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)89 Test (org.junit.jupiter.api.Test)70 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)23 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)22 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)21 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)19 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)15 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)14 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)12 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)11 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)10 Disabled (org.junit.jupiter.api.Disabled)9 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)8 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)8 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)8 HashSet (java.util.HashSet)8 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)7 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)7 SCComponent (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent)7 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)7