Search in sources :

Example 51 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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());
            }
        }
    }
}
Also used : CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) IndexedInstanceStorage(at.ac.tuwien.kr.alpha.core.grounder.IndexedInstanceStorage) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 52 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.

the class AbstractAggregateEncoder method buildElementRuleHead.

/**
 * Builds a the head atom for an aggregate element encoding rule of form
 * <code>HEAD :- $element_literals$, $aggregate_dependencies$</code>, e.g.
 * <code>count_1_element_tuple(count_1_args(Y), X) :- p(X, Y), q(Y)</code> for the rule body
 * <code>N = #count{X : p(X, Y)}, q(Y)</code>.
 *
 * @param aggregateId
 * @param element
 * @param aggregateArguments
 * @return
 */
protected BasicAtom buildElementRuleHead(String aggregateId, AggregateElement element, Term aggregateArguments) {
    Predicate headPredicate = Predicates.getPredicate(this.getElementTuplePredicateSymbol(aggregateId), 2);
    FunctionTerm elementTuple = Terms.newFunctionTerm(ELEMENT_TUPLE_FUNCTION_SYMBOL, element.getElementTerms());
    return Atoms.newBasicAtom(headPredicate, aggregateArguments, elementTuple);
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 53 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.

the class MinMaxEncoder method buildElementRuleHead.

@Override
protected BasicAtom buildElementRuleHead(String aggregateId, AggregateElement element, Term aggregateArguments) {
    Predicate headPredicate = Predicates.getPredicate(this.getElementTuplePredicateSymbol(aggregateId), 2);
    Term elementTerm = element.getElementTerms().get(0);
    return Atoms.newBasicAtom(headPredicate, aggregateArguments, elementTerm);
}
Also used : Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 54 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.

the class SumEncoder method buildElementRuleHead.

/**
 * In contrast to encoders for other aggregate functions, the "element tuple" atom for sum encodings is ternary - apart
 * from the aggregate arguments and the variable tuple identifying the aggregated element, it also holds the value to
 * add to the result sum as its third argument.
 */
@Override
protected BasicAtom buildElementRuleHead(String aggregateId, AggregateElement element, Term aggregateArguments) {
    Predicate headPredicate = Predicates.getPredicate(this.getElementTuplePredicateSymbol(aggregateId), 3);
    FunctionTerm elementTuple = Terms.newFunctionTerm(AbstractAggregateEncoder.ELEMENT_TUPLE_FUNCTION_SYMBOL, element.getElementTerms());
    return Atoms.newBasicAtom(headPredicate, aggregateArguments, elementTuple, element.getElementTerms().get(0));
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 55 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.

the class InternalProgram method recordFacts.

private void recordFacts(List<Atom> facts) {
    for (Atom fact : facts) {
        List<Instance> tmpInstances = FactIntervalEvaluator.constructFactInstances(fact);
        Predicate tmpPredicate = fact.getPredicate();
        factsByPredicate.putIfAbsent(tmpPredicate, new LinkedHashSet<>());
        factsByPredicate.get(tmpPredicate).addAll(tmpInstances);
    }
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Aggregations

Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)69 Test (org.junit.jupiter.api.Test)37 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)27 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)19 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)15 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)14 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)13 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)13 LinkedHashSet (java.util.LinkedHashSet)13 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)11 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)10 ArrayList (java.util.ArrayList)10 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)9 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)8 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)8 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)8 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)8 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)8 HashMap (java.util.HashMap)8