use of at.ac.tuwien.kr.alpha.core.programs.CompiledProgram in project Alpha by alpha-asp.
the class NaiveGrounderTest method testPermissiveGrounderHeuristicTolerance.
/**
* 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 the following
* additional conditions are established:
* <ul>
* <li>The atoms {@code b([startingInstance], 1), ..., b([startingInstance], n)} are added to the grounder's
* working memory without changing the assignment, where {@code arityOfB-1} occurences of {@code startingInstance}
* are used instead of {@code [startingInstance]} and {@code n} is the length of the {@code truthsOfB} array.
* For example, if the length of {@code truthsOfB} is 2 and {@code arityOfB} is also 2, these atoms are
* {@code b(1,1), b(1,2)}.
* </li>
* <li>The same atoms are assigned the truth values in the {@code truthsOfB} array.</li>
* </ul>
* It is asserted that ground instantiations are produced if and only if {@code expectNoGoods} is true.
* If ground instantiations are produced, it is also asserted that the numbers of unassigned positive body atoms
* determined by {@code getGroundInstantiations} match those given in {@code expectedNumbersOfUnassignedPositiveBodyAtoms}.
*/
private void testPermissiveGrounderHeuristicTolerance(ASPCore2Program program, int ruleID, Literal startingLiteral, int startingInstance, int tolerance, ThriceTruth[] truthsOfB, int arityOfB, boolean expectNoGoods, List<Integer> expectedNumbersOfUnassignedPositiveBodyAtoms) {
CompiledProgram internalPrg = InternalProgram.fromNormalProgram(NORMALIZE_TRANSFORM.apply(program));
AtomStore atomStore = new AtomStoreImpl();
TrailAssignment currentAssignment = new TrailAssignment(atomStore);
GrounderHeuristicsConfiguration heuristicConfiguration = GrounderHeuristicsConfiguration.getInstance(tolerance, tolerance);
NaiveGrounder grounder = (NaiveGrounder) GrounderFactory.getInstance("naive", internalPrg, atomStore, p -> true, heuristicConfiguration, true);
int[] bAtomIDs = new int[truthsOfB.length];
for (int i = 0; i < truthsOfB.length; i++) {
int[] bTerms = new int[arityOfB];
for (int n = 0; n < arityOfB; n++) {
bTerms[n] = (n == arityOfB - 1) ? i + 1 : startingInstance;
}
bAtomIDs[i] = atomStore.putIfAbsent(atom("b", bTerms));
}
addAtomsToWorkingMemoryWithoutChangingTheAssignment(atomStore, grounder, bAtomIDs);
assign(currentAssignment, bAtomIDs, truthsOfB);
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);
if (bindingResult.size() > 0) {
assertEquals(expectedNumbersOfUnassignedPositiveBodyAtoms, bindingResult.getNumbersOfUnassignedPositiveBodyAtoms());
} else {
assertTrue(bindingResult.getNumbersOfUnassignedPositiveBodyAtoms().isEmpty());
}
}
use of at.ac.tuwien.kr.alpha.core.programs.CompiledProgram in project Alpha by alpha-asp.
the class RuleGroundingOrderTest method testPositionFromWhichAllVarsAreBound_simpleNonGround.
@Test
public void testPositionFromWhichAllVarsAreBound_simpleNonGround() {
String aspStr = "a(X) :- b(X), not c(X).";
CompiledProgram internalPrg = PARSE_AND_PREPROCESS.apply(aspStr);
RuleGroundingInfo rgo0 = computeGroundingOrdersForRule(internalPrg, 0);
assertEquals(1, rgo0.getStartingLiterals().size());
for (Literal startingLiteral : rgo0.getStartingLiterals()) {
assertEquals(0, rgo0.orderStartingFrom(startingLiteral).getPositionFromWhichAllVarsAreBound());
}
}
use of at.ac.tuwien.kr.alpha.core.programs.CompiledProgram in project Alpha by alpha-asp.
the class RuleGroundingOrderTest method groundingOrder.
@Test
public void groundingOrder() {
String aspStr = "h(X,C) :- p(X,Y), q(A,B), r(Y,A), s(C)." + "j(A,B,X,Y) :- r1(A,B), r1(X,Y), r1(A,X), r1(B,Y), A = B." + "p(a) :- b = a.";
CompiledProgram prog = PARSE_AND_PREPROCESS.apply(aspStr);
CompiledRule rule0 = prog.getRules().get(0);
RuleGroundingInfo rgo0 = new RuleGroundingInfoImpl(rule0);
rgo0.computeGroundingOrders();
assertEquals(4, rgo0.getStartingLiterals().size());
CompiledRule rule1 = prog.getRules().get(1);
RuleGroundingInfo rgo1 = new RuleGroundingInfoImpl(rule1);
rgo1.computeGroundingOrders();
assertEquals(4, rgo1.getStartingLiterals().size());
CompiledRule rule2 = prog.getRules().get(2);
RuleGroundingInfo rgo2 = new RuleGroundingInfoImpl(rule2);
rgo2.computeGroundingOrders();
assertTrue(rgo2.hasFixedInstantiation());
}
use of at.ac.tuwien.kr.alpha.core.programs.CompiledProgram in project Alpha by alpha-asp.
the class RuleGroundingOrderTest method testPositionFromWhichAllVarsAreBound_joinedNonGround.
@Test
public void testPositionFromWhichAllVarsAreBound_joinedNonGround() {
String aspStr = "a(X) :- b(X), c(X,Y), d(X,Z), not e(X).";
CompiledProgram internalPrg = PARSE_AND_PREPROCESS.apply(aspStr);
RuleGroundingInfo rgo0 = computeGroundingOrdersForRule(internalPrg, 0);
final Literal litBX = PROGRAM_PART_PARSER.parseLiteral("b(X)");
final Literal litCXY = PROGRAM_PART_PARSER.parseLiteral("c(X,Y)");
final Literal litDXZ = PROGRAM_PART_PARSER.parseLiteral("d(X,Z)");
assertTrue(2 <= rgo0.orderStartingFrom(litBX).getPositionFromWhichAllVarsAreBound());
assertTrue(1 <= rgo0.orderStartingFrom(litCXY).getPositionFromWhichAllVarsAreBound());
assertTrue(1 <= rgo0.orderStartingFrom(litDXZ).getPositionFromWhichAllVarsAreBound());
}
use of at.ac.tuwien.kr.alpha.core.programs.CompiledProgram in project Alpha by alpha-asp.
the class ChoiceManagerTests method setUp.
@BeforeEach
public void setUp() {
String testProgram = "h :- b1, b2, not b3, not b4.";
ASPCore2Program parsedProgram = new ProgramParserImpl().parse(testProgram);
CompiledProgram internalProgram = InternalProgram.fromNormalProgram(new NormalizeProgramTransformation(SystemConfig.DEFAULT_AGGREGATE_REWRITING_CONFIG).apply(parsedProgram));
atomStore = new AtomStoreImpl();
grounder = new NaiveGrounder(internalProgram, atomStore, true);
WritableAssignment assignment = new TrailAssignment(atomStore);
NoGoodStore store = new NoGoodStoreAlphaRoaming(assignment);
choiceManager = new ChoiceManager(assignment, store);
}
Aggregations