Search in sources :

Example 66 with Atom

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

the class AnalyzeUnjustified method explainUnjust.

private ReturnExplainUnjust explainUnjust(LitSet x, Assignment currentAssignment) {
    padDepth += 2;
    log("Begin explainUnjust(): {}", x);
    Atom p = x.getAtom();
    ReturnExplainUnjust ret = new ReturnExplainUnjust();
    // Construct set of all 'rules' such that head unifies with p.
    List<RuleAndUnifier> rulesUnifyingWithP = rulesHeadUnifyingWith(p);
    log("Rules unifying with {} are {}", p, rulesUnifyingWithP);
    rulesLoop: for (RuleAndUnifier ruleUnifier : rulesUnifyingWithP) {
        Unifier sigma = ruleUnifier.unifier;
        Set<Literal> bodyR = ruleUnifier.ruleBody;
        Atom sigmaHr = ruleUnifier.originalHead.substitute(sigma);
        log("Considering now: {}", ruleUnifier);
        Set<Unifier> vN = new LinkedHashSet<>(x.getComplementSubstitutions());
        for (Unifier sigmaN : vN) {
            if (Unification.instantiate(p.substitute(sigmaN), sigmaHr) != null) {
                log("Unifier is excluded by: {}", sigmaN);
                continue rulesLoop;
            }
        }
        Set<Unifier> vNp = new LinkedHashSet<>();
        for (Unifier substitution : vN) {
            Unifier merged = Unifier.mergeIntoLeft(substitution, sigma);
            // Ignore inconsistent merges.
            if (merged == null) {
                continue;
            }
            vNp.add(merged);
        }
        log("Adapting N to N'. Original N is {}", vN);
        log("Adapted N' is {}", vNp);
        log("Searching for falsified negated literals in the body: {}", bodyR);
        for (Literal lit : bodyR) {
            if (!lit.isNegated()) {
                continue;
            }
            Atom lb = lit.getAtom().substitute(sigma);
            log("Found: {}, searching falsifying ground instances of {} (with unifier from the head) now.", lit, lb);
            AssignedAtomsIterator assignedAtomsOverPredicate = getAssignedAtomsOverPredicate(lb.getPredicate());
            while (assignedAtomsOverPredicate.hasNext()) {
                Atom lg = assignedAtomsOverPredicate.next();
                log("Considering: {}", lg);
                if (atomStore.contains(lg)) {
                    int atomId = atomStore.get(lg);
                    if (!currentAssignment.getTruth(atomId).toBoolean()) {
                        log("{} is not assigned TRUE or MBT. Skipping.", lg);
                        continue;
                    }
                }
                // Note: in case the atom is not in the atomStore, it came from a fact and hence is true.
                log("{} is TRUE or MBT.", lg);
                Unifier sigmagb = Unification.unifyAtoms(lg, lb);
                if (sigmagb == null) {
                    log("{} does not unify with {}", lg, lb);
                    continue;
                }
                log("Checking if {} is already covered.", lb);
                boolean isCovered = false;
                for (Unifier sigmaN : vN) {
                    if (Unification.instantiate(p.substitute(sigmaN), sigmaHr.substitute(sigmagb)) != null) {
                        log("{} is already covered by {}", lb, sigmaN);
                        isCovered = true;
                        break;
                    }
                }
                if (!isCovered) {
                    Unifier sigmacirc = new Unifier(sigma).extendWith(sigmagb);
                    vNp.add(sigmacirc);
                    log("Literal {} is not excluded and falsifies body literal {}", lg, lit);
                    ret.vL.add(lg.toLiteral());
                    log("Reasons extended by: {}", lg);
                }
            }
        }
        List<Literal> bodyPos = new ArrayList<>();
        for (Literal literal : bodyR) {
            if (!literal.isNegated()) {
                bodyPos.add(literal);
            }
        }
        log("Calling UnjustCover() for positive body.");
        ret.vToDo.addAll(unjustCover(bodyPos, Collections.singleton(sigma), vNp, currentAssignment));
    }
    log("End explainUnjust().");
    padDepth -= 2;
    return ret;
}
Also used : HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) ArrayList(java.util.ArrayList) List(java.util.List) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

Example 67 with Atom

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

the class AnalyzeUnjustified method rulesHeadUnifyingWith.

private List<RuleAndUnifier> rulesHeadUnifyingWith(Atom p) {
    List<RuleAndUnifier> rulesWithUnifier = new ArrayList<>();
    Predicate predicate = p.getPredicate();
    ArrayList<FactOrNonGroundRule> definingRulesAndFacts = new ArrayList<>();
    // Get facts over the same predicate.
    LinkedHashSet<Instance> factInstances = factsFromProgram.get(predicate);
    if (factInstances != null) {
        for (Instance factInstance : factInstances) {
            definingRulesAndFacts.add(new FactOrNonGroundRule(factInstance));
        }
    }
    HashSet<CompiledRule> rulesDefiningPredicate = programAnalysis.getPredicateDefiningRules().get(predicate);
    if (rulesDefiningPredicate != null) {
        for (CompiledRule nonGroundRule : rulesDefiningPredicate) {
            definingRulesAndFacts.add(new FactOrNonGroundRule(nonGroundRule));
        }
    }
    for (FactOrNonGroundRule factOrNonGroundRule : definingRulesAndFacts) {
        boolean isNonGroundRule = factOrNonGroundRule.nonGroundRule != null;
        Set<Literal> renamedBody;
        Atom headAtom;
        if (isNonGroundRule) {
            // First rename all variables in the rule.
            CompiledRule rule = factOrNonGroundRule.nonGroundRule.renameVariables("_" + renamingCounter++);
            renamedBody = rule.getBody();
            headAtom = rule.getHeadAtom();
        } else {
            // Create atom and empty rule body out of instance.
            headAtom = Atoms.newBasicAtom(p.getPredicate(), factOrNonGroundRule.factInstance.terms);
            renamedBody = Collections.emptySet();
        }
        // Unify rule head with literal to justify.
        Unifier unifier = Unification.unifyAtoms(p, headAtom);
        // Skip if unification failed.
        if (unifier == null) {
            continue;
        }
        rulesWithUnifier.add(new RuleAndUnifier(renamedBody, unifier, headAtom));
    }
    return rulesWithUnifier;
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) ArrayList(java.util.ArrayList) 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) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

Example 68 with Atom

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

the class ParseTreeVisitor method visitAnswer_set.

@Override
public AnswerSet visitAnswer_set(ASPCore2Parser.Answer_setContext ctx) {
    SortedSet<Predicate> predicates = new TreeSet<>();
    Map<Predicate, SortedSet<Atom>> predicateInstances = new TreeMap<>();
    for (ASPCore2Parser.Classical_literalContext classicalLiteralContext : ctx.classical_literal()) {
        Atom atom = visitClassical_literal(classicalLiteralContext);
        predicates.add(atom.getPredicate());
        predicateInstances.compute(atom.getPredicate(), (k, v) -> {
            if (v == null) {
                v = new TreeSet<>();
            }
            v.add(atom);
            return v;
        });
    }
    return AnswerSets.newAnswerSet(predicates, predicateInstances);
}
Also used : TreeSet(java.util.TreeSet) TreeMap(java.util.TreeMap) SortedSet(java.util.SortedSet) ASPCore2Parser(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 69 with Atom

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

the class NaiveGrounder method initializeFactsAndRules.

private void initializeFactsAndRules() {
    // Initialize all facts.
    for (Atom fact : program.getFacts()) {
        final Predicate predicate = fact.getPredicate();
        // Record predicate
        workingMemory.initialize(predicate);
    }
    // Register internal atoms.
    workingMemory.initialize(RuleAtom.PREDICATE);
    workingMemory.initialize(ChoiceAtom.OFF);
    workingMemory.initialize(ChoiceAtom.ON);
    // Initialize rules and constraints in working memory.
    for (CompiledRule nonGroundRule : program.getRulesById().values()) {
        // Create working memories for all predicates occurring in the rule.
        for (Predicate predicate : nonGroundRule.getOccurringPredicates()) {
            // FIXME: this also contains interval/builtin predicates that are not needed.
            workingMemory.initialize(predicate);
        }
        // If the rule has fixed ground instantiations, it is not registered but grounded once like facts.
        if (nonGroundRule.getGroundingInfo().hasFixedInstantiation()) {
            fixedRules.add(nonGroundRule);
            continue;
        }
        // Register each starting literal at the corresponding working memory.
        for (Literal literal : nonGroundRule.getGroundingInfo().getStartingLiterals()) {
            registerLiteralAtWorkingMemory(literal, nonGroundRule);
        }
    }
}
Also used : CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) 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)

Example 70 with Atom

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

the class UnifierTest method extendUnifier.

@Test
public void extendUnifier() {
    VariableTerm varX = Terms.newVariable("X");
    VariableTerm varY = Terms.newVariable("Y");
    Unifier sub1 = new Unifier();
    sub1.put(varX, varY);
    Unifier sub2 = new Unifier();
    sub2.put(varY, Terms.newConstant("a"));
    sub1.extendWith(sub2);
    BasicAtom atom1 = parseAtom("p(X)");
    Atom atomSubstituted = atom1.substitute(sub1);
    assertEquals(Terms.newConstant("a"), atomSubstituted.getTerms().get(0));
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) 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