Search in sources :

Example 31 with Predicate

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

the class NaiveGrounder method bootstrap.

/**
 * Prepares facts of the input program for joining and derives all NoGoods representing ground rules. May only be called once.
 *
 * @return
 */
protected HashMap<Integer, NoGood> bootstrap() {
    final HashMap<Integer, NoGood> groundNogoods = new LinkedHashMap<>();
    for (Predicate predicate : factsFromProgram.keySet()) {
        // Instead of generating NoGoods, add instance to working memories directly.
        workingMemory.addInstances(predicate, true, factsFromProgram.get(predicate));
    }
    for (CompiledRule nonGroundRule : fixedRules) {
        // Generate NoGoods for all rules that have a fixed grounding.
        RuleGroundingOrder groundingOrder = nonGroundRule.getGroundingInfo().getFixedGroundingOrder();
        BindingResult bindingResult = getGroundInstantiations(nonGroundRule, groundingOrder, new BasicSubstitution(), null);
        groundAndRegister(nonGroundRule, bindingResult.getGeneratedSubstitutions(), groundNogoods);
    }
    fixedRules = null;
    return groundNogoods;
}
Also used : BindingResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.BindingResult) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) LinkedHashMap(java.util.LinkedHashMap) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 32 with Predicate

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

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

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

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

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