Search in sources :

Example 36 with Atom

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

Example 37 with Atom

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

the class SimpleAnswerSetFormatter method format.

@Override
public String format(AnswerSet answerSet) {
    List<String> predicateInstanceStrings = new ArrayList<>();
    for (Predicate p : answerSet.getPredicates()) {
        SortedSet<Atom> instances;
        if ((instances = answerSet.getPredicateInstances(p)) == null || instances.isEmpty()) {
            predicateInstanceStrings.add(p.getName());
        } else {
            List<String> atomStrings = instances.stream().map((atom) -> atom.toString()).collect(Collectors.toList());
            predicateInstanceStrings.add(String.join(this.atomSeparator, atomStrings));
        }
    }
    return "{ " + String.join(this.atomSeparator, predicateInstanceStrings) + " }";
}
Also used : List(java.util.List) SortedSet(java.util.SortedSet) AnswerSetFormatter(at.ac.tuwien.kr.alpha.api.util.AnswerSetFormatter) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Collectors(java.util.stream.Collectors) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 38 with Atom

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

the class AnswerSetQueryTest method matchXWithFuncTerm.

@Test
public void matchXWithFuncTerm() {
    Predicate p = Predicates.getPredicate("p", 2);
    Atom a1 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("x")));
    Atom a2 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("y"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("y")));
    Atom a3 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("y"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("x")));
    Atom a4 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("y")));
    Atom a5 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newFunctionTerm("f"));
    SortedSet<Predicate> predicates = new TreeSet<>();
    predicates.add(p);
    Map<Predicate, SortedSet<Atom>> instances = new HashMap<>();
    SortedSet<Atom> ps = new TreeSet<>();
    ps.add(a1);
    ps.add(a2);
    ps.add(a3);
    ps.add(a4);
    ps.add(a5);
    instances.put(p, ps);
    AnswerSet as = AnswerSets.newAnswerSet(predicates, instances);
    AnswerSetQueryImpl query = AnswerSetQueryImpl.forPredicate(Predicates.getPredicate("p", 2)).withConstantEquals(0, "x").withFunctionTerm(1, "f", 1);
    List<Atom> queryResult = as.query(query);
    assertEquals(2, queryResult.size());
}
Also used : HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) SortedSet(java.util.SortedSet) 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 39 with Atom

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

the class AnswerSetQueryTest method matchEvenIntegers.

@Test
public void matchEvenIntegers() {
    AnswerSetBuilder bld = new AnswerSetBuilder();
    bld.predicate("p").instance(1).instance(2).instance(3).instance(4).instance(5).instance("bla").symbolicInstance("blubb");
    AnswerSet as = bld.build();
    java.util.function.Predicate<Term> isInteger = (term) -> {
        if (!(term instanceof ConstantTerm<?>)) {
            return false;
        }
        String strValue = ((ConstantTerm<?>) term).getObject().toString();
        return strValue.matches("[0-9]+");
    };
    AnswerSetQueryImpl evenIntegers = AnswerSetQueryImpl.forPredicate(Predicates.getPredicate("p", 1)).withFilter(0, isInteger.and((term) -> Integer.valueOf(((ConstantTerm<?>) term).getObject().toString()) % 2 == 0));
    List<Atom> queryResult = as.query(evenIntegers);
    assertEquals(2, queryResult.size());
    for (Atom atom : queryResult) {
        ConstantTerm<?> term = (ConstantTerm<?>) atom.getTerms().get(0);
        assertTrue(Integer.valueOf(term.getObject().toString()) % 2 == 0);
    }
}
Also used : Term(at.ac.tuwien.kr.alpha.api.terms.Term) SortedSet(java.util.SortedSet) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) AnswerSets(at.ac.tuwien.kr.alpha.commons.AnswerSets) HashMap(java.util.HashMap) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Atoms(at.ac.tuwien.kr.alpha.commons.atoms.Atoms) TreeSet(java.util.TreeSet) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Test(org.junit.jupiter.api.Test) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) List(java.util.List) Predicates(at.ac.tuwien.kr.alpha.commons.Predicates) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Example 40 with Atom

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

the class AnswerSetQueryTest method matchPredicate.

@Test
public void matchPredicate() {
    AnswerSetBuilder bld = new AnswerSetBuilder();
    // @formatter:off
    bld.predicate("p").symbolicInstance("a").symbolicInstance("b").predicate("q").symbolicInstance("x");
    // @formatter:on
    AnswerSet as = bld.build();
    List<Atom> queryResult = as.query(AnswerSetQueryImpl.forPredicate(Predicates.getPredicate("p", 1)));
    assertEquals(2, queryResult.size());
    for (Atom a : queryResult) {
        assertTrue(a.getPredicate().equals(Predicates.getPredicate("p", 1)));
    }
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Aggregations

Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)73 Test (org.junit.jupiter.api.Test)38 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)27 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)27 LinkedHashSet (java.util.LinkedHashSet)16 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)15 ArrayList (java.util.ArrayList)14 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)13 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)12 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)11 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)9 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)9 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)9 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)9 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)8 RuleAtom (at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)8 WritableAssignment (at.ac.tuwien.kr.alpha.core.solver.WritableAssignment)7 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)6 ExternalAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom)6 AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)6