Search in sources :

Example 6 with Instance

use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.

the class SubstitutionTest method specializeBasicAtom.

@Test
public void specializeBasicAtom() {
    Predicate p = Predicates.getPredicate("p", 2);
    BasicAtom atom = Atoms.newBasicAtom(p, Arrays.asList(X, Y));
    Instance instance = new Instance(A, B);
    Substitution substitution = BasicSubstitution.specializeSubstitution(atom, instance, BasicSubstitution.EMPTY_SUBSTITUTION);
    BasicAtom substituted = atom.substitute(substitution);
    assertEquals(p, substituted.getPredicate());
    assertEquals(A, substituted.getTerms().get(0));
    assertEquals(B, substituted.getTerms().get(1));
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 7 with Instance

use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.

the class IndexedInstanceStorageTest method testIndexedInstanceStorage.

@Test
public void testIndexedInstanceStorage() {
    IndexedInstanceStorage storage = new IndexedInstanceStorage(Predicates.getPredicate("p", 4), true);
    storage.addIndexPosition(0);
    storage.addIndexPosition(2);
    ConstantTerm<String> t0 = Terms.newConstant("0");
    ConstantTerm<String> t1 = Terms.newConstant("1");
    ConstantTerm<String> t2 = Terms.newConstant("2");
    ConstantTerm<String> t3 = Terms.newConstant("3");
    ConstantTerm<String> t4 = Terms.newConstant("4");
    ConstantTerm<String> t5 = Terms.newConstant("5");
    Instance badInst1 = new Instance(t1, t1, t0);
    Instance badInst2 = new Instance(t5, t5, t5, t5, t5);
    try {
        storage.addInstance(badInst1);
        fail();
    } catch (Exception e) {
        assertTrue(e.getMessage().startsWith("Instance length does not match arity of IndexedInstanceStorage"));
    }
    try {
        storage.addInstance(badInst2);
        fail();
    } catch (Exception e) {
        assertTrue(e.getMessage().startsWith("Instance length does not match arity of IndexedInstanceStorage"));
    }
    Instance inst1 = new Instance(t1, t1, t1, t1);
    Instance inst2 = new Instance(t1, t2, t3, t4);
    Instance inst3 = new Instance(t4, t3, t3, t5);
    Instance inst4 = new Instance(t1, t2, t1, t1);
    Instance inst5 = new Instance(t5, t4, t3, t2);
    storage.addInstance(inst1);
    storage.addInstance(inst2);
    storage.addInstance(inst3);
    storage.addInstance(inst4);
    storage.addInstance(inst5);
    List<Instance> matching3 = storage.getInstancesMatchingAtPosition(t3, 2);
    assertEquals(matching3.size(), 3);
    assertTrue(matching3.contains(new Instance(t1, t2, t3, t4)));
    assertTrue(matching3.contains(new Instance(t4, t3, t3, t5)));
    assertTrue(matching3.contains(new Instance(t5, t4, t3, t2)));
    assertFalse(matching3.contains(new Instance(t1, t1, t1, t1)));
    List<Instance> matching1 = storage.getInstancesMatchingAtPosition(t2, 0);
    assertEquals(matching1.size(), 0);
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) Test(org.junit.jupiter.api.Test)

Example 8 with Instance

use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.

the class StratifiedEvaluationTest method testDuplicateFacts.

@Test
public void testDuplicateFacts() {
    String aspStr = "p(a). p(b). q(b). q(X) :- p(X).";
    CompiledProgram evaluated = parseAndEvaluate.apply(aspStr);
    Instance qOfB = new Instance(TestUtils.basicAtomWithSymbolicTerms("q", "b").getTerms());
    Set<Instance> facts = evaluated.getFactsByPredicate().get(Predicates.getPredicate("q", 1));
    int numQOfB = 0;
    for (Instance at : facts) {
        if (at.equals(qOfB)) {
            numQOfB++;
        }
    }
    assertEquals(1, numQOfB);
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 9 with Instance

use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.

the class NaiveGrounder method assignmentToAnswerSet.

@Override
public AnswerSet assignmentToAnswerSet(Iterable<Integer> trueAtoms) {
    Map<Predicate, SortedSet<Atom>> predicateInstances = new LinkedHashMap<>();
    SortedSet<Predicate> knownPredicates = new TreeSet<>();
    // Iterate over all true atomIds, computeNextAnswerSet instances from atomStore and add them if not filtered.
    for (int trueAtom : trueAtoms) {
        final Atom atom = atomStore.get(trueAtom);
        Predicate predicate = atom.getPredicate();
        // Skip atoms over internal predicates.
        if (predicate.isInternal()) {
            continue;
        }
        // Skip filtered predicates.
        if (!filter.test(predicate)) {
            continue;
        }
        knownPredicates.add(predicate);
        predicateInstances.putIfAbsent(predicate, new TreeSet<>());
        Set<Atom> instances = predicateInstances.get(predicate);
        instances.add(atom);
    }
    // Add true atoms from facts.
    for (Map.Entry<Predicate, LinkedHashSet<Instance>> facts : factsFromProgram.entrySet()) {
        Predicate factPredicate = facts.getKey();
        // Skip atoms over internal predicates.
        if (factPredicate.isInternal()) {
            continue;
        }
        // Skip filtered predicates.
        if (!filter.test(factPredicate)) {
            continue;
        }
        // Skip predicates without any instances.
        if (facts.getValue().isEmpty()) {
            continue;
        }
        knownPredicates.add(factPredicate);
        predicateInstances.putIfAbsent(factPredicate, new TreeSet<>());
        for (Instance factInstance : facts.getValue()) {
            SortedSet<Atom> instances = predicateInstances.get(factPredicate);
            instances.add(Atoms.newBasicAtom(factPredicate, factInstance.terms));
        }
    }
    if (knownPredicates.isEmpty()) {
        return AnswerSets.EMPTY_SET;
    }
    return AnswerSets.newAnswerSet(knownPredicates, predicateInstances);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) SortedSet(java.util.SortedSet) ChoiceAtom(at.ac.tuwien.kr.alpha.core.atoms.ChoiceAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) LinkedHashMap(java.util.LinkedHashMap) TreeSet(java.util.TreeSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with Instance

use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.

the class NaiveGrounder method getRulesWithUniqueHead.

private Set<CompiledRule> getRulesWithUniqueHead() {
    // FIXME: below optimisation (adding support nogoods if there is only one rule instantiation per unique atom over the interpretation) could
    // be done as a transformation (adding a non-ground constraint corresponding to the nogood that is generated by the grounder).
    // Record all unique rule heads.
    final Set<CompiledRule> uniqueGroundRulePerGroundHead = new HashSet<>();
    for (Map.Entry<Predicate, LinkedHashSet<CompiledRule>> headDefiningRules : program.getPredicateDefiningRules().entrySet()) {
        if (headDefiningRules.getValue().size() != 1) {
            continue;
        }
        CompiledRule nonGroundRule = headDefiningRules.getValue().iterator().next();
        // Check that all variables of the body also occur in the head (otherwise grounding is not unique).
        Atom headAtom = nonGroundRule.getHeadAtom();
        // Rule is not guaranteed unique if there are facts for it.
        HashSet<Instance> potentialFacts = factsFromProgram.get(headAtom.getPredicate());
        if (potentialFacts != null && !potentialFacts.isEmpty()) {
            continue;
        }
        // Collect head and body variables.
        HashSet<VariableTerm> occurringVariablesHead = new HashSet<>(headAtom.toLiteral().getBindingVariables());
        HashSet<VariableTerm> occurringVariablesBody = new HashSet<>();
        for (Literal lit : nonGroundRule.getPositiveBody()) {
            occurringVariablesBody.addAll(lit.getBindingVariables());
        }
        occurringVariablesBody.removeAll(occurringVariablesHead);
        // Check if ever body variables occurs in the head.
        if (occurringVariablesBody.isEmpty()) {
            uniqueGroundRulePerGroundHead.add(nonGroundRule);
        }
    }
    return uniqueGroundRulePerGroundHead;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) ChoiceAtom(at.ac.tuwien.kr.alpha.core.atoms.ChoiceAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)25 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)10 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)9 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)9 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)9 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)7 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)7 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 RuleAtom (at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)5 Test (org.junit.jupiter.api.Test)5 Term (at.ac.tuwien.kr.alpha.api.terms.Term)4 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)4 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)4 InternalProgram (at.ac.tuwien.kr.alpha.core.programs.InternalProgram)4 Collections (java.util.Collections)4 List (java.util.List)4 GrounderHeuristicsConfiguration (at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration)3 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)3 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)3