use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class NaiveGrounder method justifyAtom.
@Override
public Set<Literal> justifyAtom(int atomToJustify, Assignment currentAssignment) {
Set<Literal> literals = analyzeUnjustified.analyze(atomToJustify, currentAssignment);
// Remove facts from justification before handing it over to the solver.
for (Iterator<Literal> iterator = literals.iterator(); iterator.hasNext(); ) {
Literal literal = iterator.next();
if (literal.isNegated()) {
continue;
}
LinkedHashSet<Instance> factsOverPredicate = factsFromProgram.get(literal.getPredicate());
if (factsOverPredicate != null && factsOverPredicate.contains(new Instance(literal.getAtom().getTerms()))) {
iterator.remove();
}
}
return literals;
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class NaiveGrounderTest method testDeadEnd.
/**
* Tests the method {@link NaiveGrounder#getGroundInstantiations(InternalRule, RuleGroundingOrder, Substitution, Assignment)} on a
* predefined program:
* <code>
* p1(1). q1(1). <br/>
* x :- p1(X), p2(X), q1(Y), q2(Y). <br/>
* p2(X) :- something(X). <br/>
* q2(X) :- something(X). <br/>
* </code>
* Given one grounding order {@code groundingOrder} for the first rule in this program which starts with
* the literal whose predicate name is {@code predicateNameOfStartingLiteral} and a substitution substituting
* the variable in this literal by 1 it is attempted to ground the rule.
* It is then asserted that ground instantiations are produced if and only if {@code expectNoGoods} is true.
*
* @param predicateNameOfStartingLiteral the predicate name of the starting literal, either "p1" or "q1".
* @param groundingOrder a grounding order for the first rule in the predefined program that starts with the literal
* whose predicate name is {@code predicateNameOfStartingLiteral}.
* @param expectNoGoods {@code true} iff ground instantiations are expected to be produced under the conditions
* described above.
*/
private void testDeadEnd(String predicateNameOfStartingLiteral, RuleGroundingOrderImpl groundingOrder, boolean expectNoGoods) {
String aspStr = "p1(1). q1(1). " + "x :- p1(X), p2(X), q1(Y), q2(Y). " + "p2(X) :- something(X). " + "q2(X) :- something(X). ";
CompiledProgram program = InternalProgram.fromNormalProgram(NORMALIZE_TRANSFORM.apply(PROGRAM_PARSER.parse(aspStr)));
AtomStore atomStore = new AtomStoreImpl();
NaiveGrounder grounder = (NaiveGrounder) GrounderFactory.getInstance("naive", program, atomStore, p -> true, GrounderHeuristicsConfiguration.permissive(), true);
CompiledRule nonGroundRule = grounder.getNonGroundRule(0);
String strLiteral = "p1".equals(predicateNameOfStartingLiteral) ? "p1(X)" : "p1(Y)";
final Literal startingLiteral = PROGRAM_PART_PARSER.parseLiteral(strLiteral);
((RuleGroundingInfoImpl) nonGroundRule.getGroundingInfo()).groundingOrders.put(startingLiteral, groundingOrder);
grounder.bootstrap();
TrailAssignment currentAssignment = new TrailAssignment(atomStore);
final Substitution subst1 = BasicSubstitution.specializeSubstitution(startingLiteral, new Instance(Terms.newConstant(1)), BasicSubstitution.EMPTY_SUBSTITUTION);
final BindingResult bindingResult = grounder.getGroundInstantiations(nonGroundRule, groundingOrder, subst1, currentAssignment);
assertEquals(expectNoGoods, bindingResult.size() > 0);
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal 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.literals.Literal in project Alpha by alpha-asp.
the class RuleGroundingOrderTest method testToString_longerSimpleNonGround.
@Test
public void testToString_longerSimpleNonGround() {
String aspStr = "a(X) :- b(X), c(X), d(X), not e(X).";
CompiledProgram internalPrg = PARSE_AND_PREPROCESS.apply(aspStr);
RuleGroundingInfo rgo0 = computeGroundingOrdersForRule(internalPrg, 0);
assertEquals(3, rgo0.getStartingLiterals().size());
for (Literal startingLiteral : rgo0.getStartingLiterals()) {
switch(startingLiteral.getPredicate().getName()) {
case "b":
assertEquals("b(X) : | c(X), d(X), not e(X)", rgo0.orderStartingFrom(startingLiteral).toString());
break;
case "c":
assertEquals("c(X) : | b(X), d(X), not e(X)", rgo0.orderStartingFrom(startingLiteral).toString());
break;
case "d":
assertEquals("d(X) : | b(X), c(X), not e(X)", rgo0.orderStartingFrom(startingLiteral).toString());
break;
default:
fail("Unexpected starting literal: " + startingLiteral);
}
}
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class RuleGroundingOrderTest method testPositionFromWhichAllVarsAreBound_longerSimpleNonGround.
@Test
public void testPositionFromWhichAllVarsAreBound_longerSimpleNonGround() {
String aspStr = "a(X) :- b(X), c(X), d(X), not e(X).";
CompiledProgram internalPrg = PARSE_AND_PREPROCESS.apply(aspStr);
RuleGroundingInfo rgo0 = computeGroundingOrdersForRule(internalPrg, 0);
assertEquals(3, rgo0.getStartingLiterals().size());
for (Literal startingLiteral : rgo0.getStartingLiterals()) {
assertEquals(0, rgo0.orderStartingFrom(startingLiteral).getPositionFromWhichAllVarsAreBound());
}
}
Aggregations