use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class ArithmeticTermsRewritingTest method rewriteExternalAtom.
@Test
public void rewriteExternalAtom() {
NormalProgram inputProgram = NormalProgramImpl.fromInputProgram(parser.parse("p :- Y = 13, &extArithTest[Y*5](Y-4)."));
assertEquals(1, inputProgram.getRules().size());
ArithmeticTermsRewriting arithmeticTermsRewriting = new ArithmeticTermsRewriting();
NormalProgram rewrittenProgram = arithmeticTermsRewriting.apply(inputProgram);
assertEquals(1, rewrittenProgram.getRules().size());
NormalRule rewrittenRule = rewrittenProgram.getRules().get(0);
assertEquals(4, rewrittenRule.getBody().size());
List<Literal> externalLiterals = rewrittenRule.getBody().stream().filter(lit -> lit instanceof ExternalLiteral).collect(toList());
assertEquals(1, externalLiterals.size());
ExternalAtom rewrittenExternal = ((ExternalLiteral) externalLiterals.get(0)).getAtom();
assertEquals(1, rewrittenExternal.getInput().size());
assertTrue(rewrittenExternal.getInput().get(0) instanceof VariableTerm);
assertEquals(1, rewrittenExternal.getOutput().size());
assertTrue(rewrittenExternal.getOutput().get(0) instanceof VariableTerm);
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal 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.api.programs.literals.Literal 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.api.programs.literals.Literal 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.api.programs.literals.Literal in project Alpha by alpha-asp.
the class AggregateRewritingContext method registerRule.
/**
* Registers a rule that potentially contains one or more {@link AggregateLiteral}s with this context.
* In case aggregates are found in the rule, global variables and dependencies of the aggregate are calculated and the
* aggregate literal is stored in the rewriting context along with a reference to the rule it occurs in
*
* @param rule
* @return true if the given rule contains one or more aggregate literals, false otherwise
*/
public boolean registerRule(Rule<Head> rule) {
AggregateRewritingRuleAnalysis ruleAnalysis = AggregateRewritingRuleAnalysis.analyzeRuleDependencies(rule);
if (ruleAnalysis.aggregatesInRule.isEmpty()) {
// Rule has no aggregates.
return false;
}
// Do initial registration of each aggregate literal and keep the ids.
for (Map.Entry<AggregateLiteral, Set<VariableTerm>> entry : ruleAnalysis.globalVariablesPerAggregate.entrySet()) {
registerAggregateLiteral(entry.getKey(), entry.getValue());
}
// Now go through dependencies and replace the actual aggregate literals with their rewritten versions
for (Map.Entry<AggregateLiteral, Set<Literal>> entry : ruleAnalysis.dependenciesPerAggregate.entrySet()) {
AggregateInfo aggregateInfo = getAggregateInfo(entry.getKey());
for (Literal dependency : entry.getValue()) {
if (dependency instanceof AggregateLiteral) {
AggregateInfo dependencyInfo = getAggregateInfo((AggregateLiteral) dependency);
aggregateInfo.addDependency(dependencyInfo.getOutputAtom().toLiteral(!dependency.isNegated()));
} else {
aggregateInfo.addDependency(dependency);
}
}
}
rulesWithAggregates.add(rule);
return true;
}
Aggregations