Search in sources :

Example 1 with LiteralInstantiator

use of at.ac.tuwien.kr.alpha.core.grounder.instantiation.LiteralInstantiator in project Alpha by alpha-asp.

the class StratifiedEvaluation method apply.

@Override
public // memories created here rather than re-initialize everything.
InternalProgram apply(AnalyzedProgram inputProgram) {
    // Calculate a stratification and initialize the working memory.
    ComponentGraph componentGraph = inputProgram.getComponentGraph();
    List<ComponentGraph.SCComponent> strata = StratificationAlgorithm.calculateStratification(componentGraph);
    predicateDefiningRules = inputProgram.getPredicateDefiningRules();
    // Set up list of atoms which are known to be true - these will be expand by the evaluation.
    Map<Predicate, Set<Instance>> knownFacts = new LinkedHashMap<>(inputProgram.getFactsByPredicate());
    for (Map.Entry<Predicate, Set<Instance>> entry : knownFacts.entrySet()) {
        workingMemory.initialize(entry.getKey());
        workingMemory.addInstances(entry.getKey(), true, entry.getValue());
    }
    // Create working memories for all predicates occurring in each rule.
    for (CompiledRule nonGroundRule : inputProgram.getRulesById().values()) {
        for (Predicate predicate : nonGroundRule.getOccurringPredicates()) {
            workingMemory.initialize(predicate);
        }
    }
    workingMemory.reset();
    // Set up literal instantiator.
    literalInstantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
    // Evaluate the program part covered by the calculated stratification.
    for (ComponentGraph.SCComponent currComponent : strata) {
        evaluateComponent(currComponent);
    }
    // Build the program resulting from evaluating the stratified part.
    // Add original input facts to newly derived ones.
    additionalFacts.addAll(inputProgram.getFacts());
    List<CompiledRule> outputRules = new ArrayList<>();
    inputProgram.getRulesById().entrySet().stream().filter((entry) -> !solvedRuleIds.contains(entry.getKey())).forEach((entry) -> outputRules.add(entry.getValue()));
    // NOTE: if InternalProgram requires solved rules, they should be added here.
    return new InternalProgram(outputRules, additionalFacts);
}
Also used : InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Stack(java.util.Stack) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) LiteralInstantiationResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.LiteralInstantiationResult) Map(java.util.Map) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) SetUtils(org.apache.commons.collections4.SetUtils) LiteralInstantiator(at.ac.tuwien.kr.alpha.core.grounder.instantiation.LiteralInstantiator) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) LinkedHashSet(java.util.LinkedHashSet) AssignmentStatus(at.ac.tuwien.kr.alpha.core.grounder.instantiation.AssignmentStatus) Logger(org.slf4j.Logger) StratificationAlgorithm(at.ac.tuwien.kr.alpha.core.depgraph.StratificationAlgorithm) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Set(java.util.Set) WorkingMemoryBasedInstantiationStrategy(at.ac.tuwien.kr.alpha.core.grounder.instantiation.WorkingMemoryBasedInstantiationStrategy) Atoms(at.ac.tuwien.kr.alpha.commons.atoms.Atoms) RuleGroundingOrder(at.ac.tuwien.kr.alpha.core.grounder.RuleGroundingOrder) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) List(java.util.List) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) IndexedInstanceStorage(at.ac.tuwien.kr.alpha.core.grounder.IndexedInstanceStorage) Collections(java.util.Collections) RuleGroundingInfo(at.ac.tuwien.kr.alpha.core.grounder.RuleGroundingInfo) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) WorkingMemoryBasedInstantiationStrategy(at.ac.tuwien.kr.alpha.core.grounder.instantiation.WorkingMemoryBasedInstantiationStrategy) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LiteralInstantiator(at.ac.tuwien.kr.alpha.core.grounder.instantiation.LiteralInstantiator) ArrayList(java.util.ArrayList) InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) LinkedHashMap(java.util.LinkedHashMap) ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)1 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)1 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)1 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)1 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)1 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)1 Atoms (at.ac.tuwien.kr.alpha.commons.atoms.Atoms)1 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)1 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)1 StratificationAlgorithm (at.ac.tuwien.kr.alpha.core.depgraph.StratificationAlgorithm)1 IndexedInstanceStorage (at.ac.tuwien.kr.alpha.core.grounder.IndexedInstanceStorage)1 RuleGroundingInfo (at.ac.tuwien.kr.alpha.core.grounder.RuleGroundingInfo)1 RuleGroundingOrder (at.ac.tuwien.kr.alpha.core.grounder.RuleGroundingOrder)1 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)1 AssignmentStatus (at.ac.tuwien.kr.alpha.core.grounder.instantiation.AssignmentStatus)1 LiteralInstantiationResult (at.ac.tuwien.kr.alpha.core.grounder.instantiation.LiteralInstantiationResult)1 LiteralInstantiator (at.ac.tuwien.kr.alpha.core.grounder.instantiation.LiteralInstantiator)1 WorkingMemoryBasedInstantiationStrategy (at.ac.tuwien.kr.alpha.core.grounder.instantiation.WorkingMemoryBasedInstantiationStrategy)1 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)1 InternalProgram (at.ac.tuwien.kr.alpha.core.programs.InternalProgram)1