use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class AggregateRewriting method rewriteRulesWithAggregates.
/**
* Transforms (restricted) aggregate literals of format "VAR OP #AGG_FN{...}" into literals of format
* "<result_predicate>(ARGS, VAR)" where ARGS is a function term wrapping the aggregate's global variables.
*
* @param ctx the {@link AggregateRewritingContext} containing information about all aggregates.
* @return for each rule, its rewritten version where aggregates are replaced with output atoms of the encoding.
*/
private static List<Rule<Head>> rewriteRulesWithAggregates(AggregateRewritingContext ctx) {
List<Rule<Head>> rewrittenRules = new ArrayList<>();
for (Rule<Head> rule : ctx.getRulesWithAggregates()) {
List<Literal> rewrittenBody = new ArrayList<>();
for (Literal lit : rule.getBody()) {
if (lit instanceof AggregateLiteral) {
AggregateInfo aggregateInfo = ctx.getAggregateInfo((AggregateLiteral) lit);
rewrittenBody.add(Literals.fromAtom(aggregateInfo.getOutputAtom(), !lit.isNegated()));
} else {
rewrittenBody.add(lit);
}
}
rewrittenRules.add(new BasicRule(rule.getHead(), rewrittenBody));
}
return rewrittenRules;
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class AggregateRewritingRuleAnalysis method findGlobalVariablesPerAggregate.
private void findGlobalVariablesPerAggregate() {
// First, compute all global variables, that is all variables occurring in a rule except those occurring
// inside aggregate elements.
Set<VariableTerm> globalVariables = new HashSet<>();
if (!rule.isConstraint()) {
// Head must be normal at this point.
NormalHead head = (NormalHead) rule.getHead();
globalVariables.addAll(head.getAtom().getOccurringVariables());
}
for (Literal literal : rule.getBody()) {
if (literal instanceof AggregateLiteral) {
aggregatesInRule.add((AggregateLiteral) literal);
AggregateAtom aggregateAtom = (AggregateAtom) literal.getAtom();
// All variables in the bounds of an aggregate are also global variables.
// Note that at this point, only lower bounds appear in aggregates.
globalVariables.addAll(aggregateAtom.getLowerBoundTerm().getOccurringVariables());
} else {
globalVariables.addAll(literal.getOccurringVariables());
}
}
// Second, compute for each aggregate those of its variables that are global.
for (AggregateLiteral aggregateLiteral : aggregatesInRule) {
Set<VariableTerm> globalVariablesInAggregate = new HashSet<>();
for (VariableTerm aggregateVariable : aggregateLiteral.getAtom().getAggregateVariables()) {
if (globalVariables.contains(aggregateVariable)) {
globalVariablesInAggregate.add(aggregateVariable);
}
}
globalVariablesPerAggregate.put(aggregateLiteral, globalVariablesInAggregate);
}
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class AggregateRewritingRuleAnalysis method analyzeRuleDependencies.
private void analyzeRuleDependencies() {
for (AggregateLiteral lit : globalVariablesPerAggregate.keySet()) {
Set<VariableTerm> nonBindingVars = new HashSet<>(globalVariablesPerAggregate.get(lit));
Term leftHandTerm = lit.getAtom().getLowerBoundTerm();
if (lit.getBindingVariables().isEmpty() && leftHandTerm instanceof VariableTerm) {
/*
* If the "left-hand" term LT of the literal is a variable and not binding, it has to be non-binding,
* i.e. the aggregate literal depends on the literals binding LT.
*/
nonBindingVars.add((VariableTerm) leftHandTerm);
}
Set<Literal> dependencies = new HashSet<>();
Set<Literal> bodyWithoutLit = SetUtils.difference(rule.getBody(), Collections.singleton(lit));
findBindingLiterals(nonBindingVars, new HashSet<>(), dependencies, bodyWithoutLit, globalVariablesPerAggregate);
dependenciesPerAggregate.put(lit, dependencies);
}
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class AnalyzeUnjustifiedTest method justifyLargerRules.
@Test
public void justifyLargerRules() {
String program = "p(X) :- q(X,Y), r(Y), not s(X,Y)." + "{ q(1,X)} :- dom(X)." + "dom(1..3)." + "{r(X)} :- p(X)." + "{r(2)}." + "{s(1,2)}." + ":- not p(1).";
CompiledProgram internalProgram = parseAndPreprocess.apply(program);
AtomStore atomStore = new AtomStoreImpl();
NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
grounder.getNoGoods(null);
TrailAssignment assignment = new TrailAssignment(atomStore);
Atom p1 = parser.parse("p(1).").getFacts().get(0);
Atom r2 = parser.parse("r(2).").getFacts().get(0);
Atom s12 = parser.parse("s(1,2).").getFacts().get(0);
Atom q11 = parser.parse("q(1,1).").getFacts().get(0);
Atom q12 = parser.parse("q(1,2).").getFacts().get(0);
Atom q13 = parser.parse("q(1,3).").getFacts().get(0);
int p1Id = atomStore.get(p1);
int r2Id = atomStore.get(r2);
int s12Id = atomStore.get(s12);
int q11Id = atomStore.get(q11);
int q12Id = atomStore.get(q12);
int q13Id = atomStore.get(q13);
assignment.growForMaxAtomId();
assignment.assign(p1Id, ThriceTruth.MBT);
assignment.assign(r2Id, ThriceTruth.TRUE);
assignment.assign(s12Id, ThriceTruth.TRUE);
assignment.assign(q11Id, ThriceTruth.TRUE);
assignment.assign(q12Id, ThriceTruth.TRUE);
assignment.assign(q13Id, ThriceTruth.FALSE);
Set<Literal> reasons = grounder.justifyAtom(p1Id, assignment);
assertFalse(reasons.isEmpty());
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class AnalyzeUnjustifiedTest method justifyNegatedFactsRemovedFromReasons.
@Test
public void justifyNegatedFactsRemovedFromReasons() {
String program = "forbidden(2,9). forbidden(1,9)." + "p(X) :- q(X)." + "q(X) :- p(X)." + "q(5) :- r." + "r :- not nr, not forbidden(2,9), not forbidden(1,9)." + "nr :- not r." + ":- not p(5).";
CompiledProgram internalProgram = parseAndPreprocess.apply(program);
AtomStore atomStore = new AtomStoreImpl();
NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
grounder.getNoGoods(null);
TrailAssignment assignment = new TrailAssignment(atomStore);
int rId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("r", 0)));
int nrId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("nr", 0)));
assignment.growForMaxAtomId();
assignment.assign(rId, ThriceTruth.FALSE);
assignment.assign(nrId, ThriceTruth.TRUE);
BasicAtom p5 = Atoms.newBasicAtom(Predicates.getPredicate("p", 1), Collections.singletonList(Terms.newConstant(5)));
assignment.assign(atomStore.get(p5), ThriceTruth.MBT);
Set<Literal> reasons = grounder.justifyAtom(atomStore.get(p5), assignment);
assertFalse(reasons.isEmpty());
for (Literal literal : reasons) {
// Check that facts are not present in justification.
assertNotEquals(literal.getPredicate(), Predicates.getPredicate("forbidden", 2));
}
}
Aggregations