use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class AggregateRewritingRuleAnalysisTest method nonBindingAggregateNoGlobals2.
@Test
public void nonBindingAggregateNoGlobals2() {
AggregateRewritingRuleAnalysis analysis = analyze(NONBINDING_AGGREGATE_NO_GLOBALS_2);
assertEquals(1, analysis.globalVariablesPerAggregate.size());
assertEquals(1, analysis.dependenciesPerAggregate.size());
AggregateLiteral aggregate = new ArrayList<>(analysis.globalVariablesPerAggregate.keySet()).get(0);
assertTrue(analysis.globalVariablesPerAggregate.get(aggregate).isEmpty());
assertFalse(analysis.dependenciesPerAggregate.get(aggregate).isEmpty());
Set<Literal> dependencies = analysis.dependenciesPerAggregate.get(aggregate);
assertEquals(3, dependencies.size());
Literal threePlusY = Literals.fromAtom(Atoms.newComparisonAtom(Terms.newVariable("X"), Terms.newArithmeticTerm(Terms.newConstant(3), ArithmeticOperator.PLUS, Terms.newVariable("Y")), ComparisonOperators.EQ), true);
assertTrue(dependencies.contains(threePlusY));
Literal zPlusFour = Literals.fromAtom(Atoms.newComparisonAtom(Terms.newVariable("Y"), Terms.newArithmeticTerm(Terms.newVariable("Z"), ArithmeticOperator.PLUS, Terms.newConstant(4)), ComparisonOperators.EQ), true);
assertTrue(dependencies.contains(zPlusFour));
Literal rSZ = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("r", 2), Terms.newVariable("S"), Terms.newVariable("Z")), true);
assertTrue(dependencies.contains(rSZ));
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class AggregateRewritingRuleAnalysisTest method bindingAggregateWithGlobals1.
@Test
public void bindingAggregateWithGlobals1() {
AggregateRewritingRuleAnalysis analysis = analyze(BINDING_AGGREGATE_WITH_GLOBALS_1);
assertEquals(1, analysis.globalVariablesPerAggregate.size());
assertEquals(1, analysis.dependenciesPerAggregate.size());
AggregateLiteral aggregate = new ArrayList<>(analysis.globalVariablesPerAggregate.keySet()).get(0);
assertFalse(analysis.globalVariablesPerAggregate.get(aggregate).isEmpty());
assertFalse(analysis.dependenciesPerAggregate.get(aggregate).isEmpty());
Set<VariableTerm> globalVars = analysis.globalVariablesPerAggregate.get(aggregate);
assertTrue(globalVars.contains(Terms.newVariable("G")));
assertTrue(globalVars.contains(Terms.newVariable("V")));
Set<Literal> dependencies = analysis.dependenciesPerAggregate.get(aggregate);
assertEquals(2, dependencies.size());
Literal graph = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph", 1), Terms.newVariable("G")), true);
assertTrue(dependencies.contains(graph));
Literal graphVertex = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph_vertex", 2), Terms.newVariable("G"), Terms.newVariable("V")), true);
assertTrue(dependencies.contains(graphVertex));
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class PredicateInternalizer method makePrefixedPredicatesInternal.
public static Rule<Head> makePrefixedPredicatesInternal(Rule<Head> rule, String prefix) {
Head newHead = null;
if (rule.getHead() != null) {
if (!(rule.getHead() instanceof NormalHead)) {
throw new UnsupportedOperationException("Cannot make predicates in rules internal whose head is not normal.");
}
NormalHead head = (NormalHead) rule.getHead();
if (head.getAtom().getPredicate().getName().startsWith(prefix)) {
newHead = Heads.newNormalHead(makePredicateInternal(head.getAtom()));
} else {
newHead = head;
}
}
List<Literal> newBody = new ArrayList<>();
for (Literal bodyElement : rule.getBody()) {
// Only rewrite BasicAtoms.
if (bodyElement instanceof BasicLiteral) {
if (bodyElement.getAtom().getPredicate().getName().startsWith(prefix)) {
newBody.add(makePredicateInternal((BasicAtom) bodyElement.getAtom()).toLiteral(!bodyElement.isNegated()));
} else {
newBody.add(bodyElement);
}
} else {
// Keep other body element as is.
newBody.add(bodyElement);
}
}
return new BasicRule(newHead, newBody);
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class StratifiedEvaluation method calculateSatisfyingSubstitutionsForRule.
private List<Substitution> calculateSatisfyingSubstitutionsForRule(CompiledRule rule, boolean checkAllStartingLiterals) {
LOGGER.debug("Grounding rule {}", rule);
RuleGroundingInfo groundingOrders = rule.getGroundingInfo();
// Treat rules with fixed instantiation first.
LOGGER.debug("Is fixed rule? {}", rule.getGroundingInfo().hasFixedInstantiation());
if (groundingOrders.hasFixedInstantiation()) {
RuleGroundingOrder fixedGroundingOrder = groundingOrders.getFixedGroundingOrder();
return calcSubstitutionsWithGroundingOrder(fixedGroundingOrder, Collections.singletonList(new BasicSubstitution()));
}
List<Literal> startingLiterals = groundingOrders.getStartingLiterals();
// Check only one starting literal if indicated by the parameter.
if (!checkAllStartingLiterals) {
// If this is the first evaluation run, it suffices to start from the first starting literal only.
Literal lit = startingLiterals.get(0);
return calcSubstitutionsWithGroundingOrder(groundingOrders.orderStartingFrom(lit), substituteFromRecentlyAddedInstances(lit));
}
// Ground from all starting literals.
// Collection of full ground substitutions for the given rule.
List<Substitution> groundSubstitutions = new ArrayList<>();
for (Literal lit : startingLiterals) {
List<Substitution> substitutionsForStartingLiteral = calcSubstitutionsWithGroundingOrder(groundingOrders.orderStartingFrom(lit), substituteFromRecentlyAddedInstances(lit));
groundSubstitutions.addAll(substitutionsForStartingLiteral);
}
return groundSubstitutions;
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class StratifiedEvaluation method prepareInitialEvaluation.
/**
* To be called at the start of evaluateComponent. Adds all known instances of the predicates occurring in the given set
* of rules to the "modifiedInLastEvaluationRun" map in order to "bootstrap" incremental grounding, i.e. making sure
* that those instances are taken into account for ground substitutions by evaluateRule.
*/
private void prepareInitialEvaluation(Set<CompiledRule> rulesToEvaluate) {
modifiedInLastEvaluationRun = new HashMap<>();
for (CompiledRule rule : rulesToEvaluate) {
// Register rule head instances.
Predicate headPredicate = rule.getHeadAtom().getPredicate();
IndexedInstanceStorage headInstances = workingMemory.get(headPredicate, true);
modifiedInLastEvaluationRun.putIfAbsent(headPredicate, new LinkedHashSet<>());
if (headInstances != null) {
modifiedInLastEvaluationRun.get(headPredicate).addAll(headInstances.getAllInstances());
}
// Register positive body literal instances.
for (Literal lit : rule.getPositiveBody()) {
Predicate bodyPredicate = lit.getPredicate();
IndexedInstanceStorage bodyInstances = workingMemory.get(bodyPredicate, true);
modifiedInLastEvaluationRun.putIfAbsent(bodyPredicate, new LinkedHashSet<>());
if (bodyInstances != null) {
modifiedInLastEvaluationRun.get(bodyPredicate).addAll(bodyInstances.getAllInstances());
}
}
}
}
Aggregations