Search in sources :

Example 1 with Predicate

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

the class LiteralInstantiatorTest method workingMemoryBasedVerifyPositiveGroundLiteralSatisfied.

@Test
public void workingMemoryBasedVerifyPositiveGroundLiteralSatisfied() {
    Predicate p = Predicates.getPredicate("p", 2);
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(p);
    workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("y")), true);
    VariableTerm x = Terms.newVariable("X");
    VariableTerm y = Terms.newVariable("Y");
    Literal lit = Literals.fromAtom(Atoms.newBasicAtom(p, x, y), true);
    Substitution substitution = new BasicSubstitution();
    substitution.put(x, Terms.newSymbolicConstant("x"));
    substitution.put(y, Terms.newSymbolicConstant("y"));
    LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
    LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
    assertEquals(LiteralInstantiationResult.Type.CONTINUE, result.getType());
    List<ImmutablePair<Substitution, AssignmentStatus>> substitutions = result.getSubstitutions();
    assertEquals(1, substitutions.size());
    assertEquals(AssignmentStatus.TRUE, substitutions.get(0).right);
    Substitution resultSubstitution = substitutions.get(0).left;
    // With the given input substitution, lit is ground and satisfied -
    // we expect the instantiator to verify that.
    assertEquals(substitution, resultSubstitution);
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 2 with Predicate

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

the class LiteralInstantiatorTest method workingMemoryBasedVerifyPositiveGroundLiteralUnsatisfied.

@Test
public void workingMemoryBasedVerifyPositiveGroundLiteralUnsatisfied() {
    Predicate p = Predicates.getPredicate("p", 2);
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(p);
    VariableTerm x = Terms.newVariable("X");
    VariableTerm y = Terms.newVariable("Y");
    Literal lit = Literals.fromAtom(Atoms.newBasicAtom(p, x, y), true);
    Substitution substitution = new BasicSubstitution();
    substitution.put(x, Terms.newSymbolicConstant("x"));
    substitution.put(y, Terms.newSymbolicConstant("y"));
    LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
    LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
    // With the given input substitution, lit is ground, but not satisfied -
    // we expect the instantiator to verify that and return an empty list of
    // substitutions.
    assertEquals(LiteralInstantiationResult.Type.STOP_BINDING, result.getType());
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 3 with Predicate

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

the class StratifiedEvaluationTest method testRecursiveRanking.

@Test
public void testRecursiveRanking() {
    // @formatter:off
    String asp = "thing(a).\n" + "thing(b).\n" + "thing(c).\n" + "thing_before(a, b).\n" + "thing_before(b, c).\n" + "has_prev_thing(X) :- thing(X), thing_succ(_, X).\n" + "first_thing(X) :- thing(X), not has_prev_thing(X).\n" + "thing_not_succ(X, Y) :-\n" + "	thing(X),\n" + "	thing(Y),\n" + "	thing(INTM),\n" + "	thing_before(X, Y),\n" + "	thing_before(X, INTM),\n" + "	thing_before(INTM, X).\n" + "thing_succ(X, Y) :-\n" + "	thing(X),\n" + "	thing(Y),\n" + "	thing_before(X, Y),\n" + "	not thing_not_succ(X, Y).\n" + "thing_rank(X, 1) :- first_thing(X).\n" + "thing_rank(X, R) :-\n" + "	thing(X),\n" + "	thing_succ(Y, X),\n" + "	thing_rank(Y, K),\n" + "	R = K + 1.";
    // @formatter:on
    CompiledProgram evaluated = parseAndEvaluate.apply(asp);
    Predicate rank = Predicates.getPredicate("thing_rank", 2);
    BasicAtom rank1 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("a"), Terms.newConstant(1));
    BasicAtom rank2 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("b"), Terms.newConstant(2));
    BasicAtom rank3 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("c"), Terms.newConstant(3));
    List<Atom> evaluatedFacts = evaluated.getFacts();
    assertTrue(evaluatedFacts.contains(rank1));
    assertTrue(evaluatedFacts.contains(rank2));
    assertTrue(evaluatedFacts.contains(rank3));
}
Also used : CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 4 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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)

Example 5 with Predicate

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

the class StratifiedEvaluation method getRulesToEvaluate.

private ComponentEvaluationInfo getRulesToEvaluate(ComponentGraph.SCComponent comp) {
    Set<CompiledRule> nonRecursiveRules = new HashSet<>();
    Set<CompiledRule> recursiveRules = new HashSet<>();
    // Collect all predicates occurring in heads of rules of the given component.
    Set<Predicate> headPredicates = new HashSet<>();
    for (DependencyGraph.Node node : comp.getNodes()) {
        headPredicates.add(node.getPredicate());
    }
    // cycle.
    for (Predicate headPredicate : headPredicates) {
        HashSet<CompiledRule> definingRules = predicateDefiningRules.get(headPredicate);
        if (definingRules == null) {
            // Predicate only occurs in facts, skip.
            continue;
        }
        // Note: here we assume that all rules defining a predicate belong to the same SC component.
        for (CompiledRule rule : definingRules) {
            boolean isRuleRecursive = false;
            for (Literal lit : rule.getPositiveBody()) {
                if (headPredicates.contains(lit.getPredicate())) {
                    // The rule body contains a predicate that is defined in the same
                    // component, the rule is therefore part of a cyclic dependency within
                    // this component.
                    isRuleRecursive = true;
                }
            }
            if (isRuleRecursive) {
                recursiveRules.add(rule);
            } else {
                nonRecursiveRules.add(rule);
            }
        }
    }
    return new ComponentEvaluationInfo(nonRecursiveRules, recursiveRules);
}
Also used : CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) 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