use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.
the class NaiveGrounderTest method testGroundingOfRuleSwitchedOffByTrueNegativeBody.
@Test
@Disabled("Currently, rule grounding is not switched off by a true negative body atom")
public void testGroundingOfRuleSwitchedOffByTrueNegativeBody() {
ASPCore2Program program = PROGRAM_PARSER.parse("a(1). " + "c(X) :- a(X), not b(X). " + "b(X) :- something(X). ");
testIfGrounderGroundsRule(program, 0, litAX, 1, ThriceTruth.TRUE, false);
}
use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.
the class NaiveGrounderTest method testGroundingOfRuleNotSwitchedOffByFalseNegativeBody.
@Test
public void testGroundingOfRuleNotSwitchedOffByFalseNegativeBody() {
ASPCore2Program program = PROGRAM_PARSER.parse("a(1). " + "c(X) :- a(X), not b(X). " + "b(X) :- something(X). ");
testIfGrounderGroundsRule(program, 0, litAX, 1, ThriceTruth.FALSE, true);
}
use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.
the class NaiveGrounderTest method testIfGrounderGroundsRule.
/**
* Tests if {@link NaiveGrounder#getGroundInstantiations(InternalRule, RuleGroundingOrder, Substitution, Assignment)}
* produces ground instantiations for the rule with ID {@code ruleID} in {@code program} when {@code startingLiteral}
* unified with the numeric instance {@code startingInstance} is used as starting literal and {@code b(1)} is assigned
* {@code bTruth}.
* It is asserted that ground instantiations are produced if and only if {@code expectNoGoods} is true.
*/
private void testIfGrounderGroundsRule(ASPCore2Program program, int ruleID, Literal startingLiteral, int startingInstance, ThriceTruth bTruth, boolean expectNoGoods) {
CompiledProgram internalPrg = InternalProgram.fromNormalProgram(NORMALIZE_TRANSFORM.apply(program));
AtomStore atomStore = new AtomStoreImpl();
TrailAssignment currentAssignment = new TrailAssignment(atomStore);
NaiveGrounder grounder = (NaiveGrounder) GrounderFactory.getInstance("naive", internalPrg, atomStore, p -> true, GrounderHeuristicsConfiguration.permissive(), true);
int b = atomStore.putIfAbsent(atom("b", 1));
currentAssignment.growForMaxAtomId();
currentAssignment.assign(b, bTruth);
grounder.bootstrap();
final CompiledRule nonGroundRule = grounder.getNonGroundRule(ruleID);
final Substitution substStartingLiteral = BasicSubstitution.specializeSubstitution(startingLiteral, new Instance(Terms.newConstant(startingInstance)), BasicSubstitution.EMPTY_SUBSTITUTION);
final BindingResult bindingResult = grounder.getGroundInstantiations(nonGroundRule, nonGroundRule.getGroundingInfo().orderStartingFrom(startingLiteral), substStartingLiteral, currentAssignment);
assertEquals(expectNoGoods, bindingResult.size() > 0);
}
use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.
the class ProgramTest method testToString.
@Test
public void testToString() {
ASPCore2Program parsedProgram = new ProgramParserImpl().parse("p(a)." + System.lineSeparator() + "q(X) :- p(X)." + System.lineSeparator() + "p(b).");
assertEquals("p(a)." + System.lineSeparator() + "p(b)." + System.lineSeparator() + "q(X) :- p(X)." + System.lineSeparator(), parsedProgram.toString());
}
use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.
the class StratificationAlgorithmTest method stratifyLargeGraphTest.
@Test
public void stratifyLargeGraphTest() {
StringBuilder bld = new StringBuilder();
bld.append("b :- a.");
bld.append("c :- b.");
bld.append("d :- c.");
bld.append("e :- d.");
bld.append("f :- not e.");
bld.append("g :- d, j, not f.");
bld.append("h :- not c.");
bld.append("i :- h, not j.");
bld.append("j :- h, not i.");
bld.append("k :- g, not l.");
bld.append("l :- g, not k.");
bld.append("m :- not k, not l.");
bld.append("n :- m, not i, not j.");
bld.append("p :- not m, not 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);
Predicate d = Predicates.getPredicate("d", 0);
Predicate e = Predicates.getPredicate("e", 0);
Predicate f = Predicates.getPredicate("f", 0);
Predicate h = Predicates.getPredicate("h", 0);
assertTrue(predicateIsBeforePredicateInOrder(a, h, strata));
assertTrue(predicateIsBeforePredicateInOrder(b, h, strata));
assertTrue(predicateIsBeforePredicateInOrder(c, h, strata));
assertTrue(predicateIsBeforePredicateInOrder(a, f, strata));
assertTrue(predicateIsBeforePredicateInOrder(b, f, strata));
assertTrue(predicateIsBeforePredicateInOrder(c, f, strata));
assertTrue(predicateIsBeforePredicateInOrder(d, f, strata));
assertTrue(predicateIsBeforePredicateInOrder(e, f, strata));
}
Aggregations