use of at.ac.tuwien.kr.alpha.core.grounder.IndexedInstanceStorage in project Alpha by alpha-asp.
the class StratifiedEvaluation method evaluateComponent.
private void evaluateComponent(ComponentGraph.SCComponent comp) {
LOGGER.debug("Evaluating component {}", comp);
ComponentEvaluationInfo evaluationInfo = getRulesToEvaluate(comp);
if (evaluationInfo.isEmpty()) {
LOGGER.debug("No rules to evaluate for component {}", comp);
return;
}
// Rules outside of dependency cycles only need to be evaluated once.
if (!evaluationInfo.nonRecursiveRules.isEmpty()) {
prepareInitialEvaluation(evaluationInfo.nonRecursiveRules);
evaluateRules(evaluationInfo.nonRecursiveRules, true);
for (IndexedInstanceStorage instanceStorage : workingMemory.modified()) {
// Directly record all newly derived instances as additional facts.
for (Instance recentlyAddedInstance : instanceStorage.getRecentlyAddedInstances()) {
additionalFacts.add(Atoms.newBasicAtom(instanceStorage.getPredicate(), recentlyAddedInstance.terms));
}
instanceStorage.markRecentlyAddedInstancesDone();
}
}
boolean isInitialRun = true;
if (!evaluationInfo.recursiveRules.isEmpty()) {
do {
// evaluate these until nothing new can be derived any more.
if (isInitialRun) {
prepareInitialEvaluation(evaluationInfo.recursiveRules);
}
evaluateRules(evaluationInfo.recursiveRules, isInitialRun);
isInitialRun = false;
modifiedInLastEvaluationRun = new HashMap<>();
// Since we are stratified we never have to backtrack, therefore just collect the added instances.
for (IndexedInstanceStorage instanceStorage : workingMemory.modified()) {
// Directly record all newly derived instances as additional facts.
for (Instance recentlyAddedInstance : instanceStorage.getRecentlyAddedInstances()) {
additionalFacts.add(Atoms.newBasicAtom(instanceStorage.getPredicate(), recentlyAddedInstance.terms));
}
modifiedInLastEvaluationRun.putIfAbsent(instanceStorage.getPredicate(), new LinkedHashSet<>());
modifiedInLastEvaluationRun.get(instanceStorage.getPredicate()).addAll(instanceStorage.getRecentlyAddedInstances());
instanceStorage.markRecentlyAddedInstancesDone();
}
// If the evaluation of rules did not modify the working memory we have a fixed-point.
} while (!workingMemory.modified().isEmpty());
}
LOGGER.debug("Evaluation done - reached a fixed point on component {}", comp);
SetUtils.union(evaluationInfo.nonRecursiveRules, evaluationInfo.recursiveRules).forEach((rule) -> solvedRuleIds.add(rule.getRuleId()));
}
use of at.ac.tuwien.kr.alpha.core.grounder.IndexedInstanceStorage 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