Search in sources :

Example 21 with BasicAtom

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

the class AnalyzeUnjustifiedTest method justifySimpleRules.

@Test
public void justifySimpleRules() {
    String program = "p(X) :- q(X)." + "q(X) :- p(X)." + "q(5) :- r." + "r :- not nr." + "nr :- not r." + ":- not p(5).";
    CompiledProgram internalProgram = parseAndPreprocess.apply(program);
    AtomStore atomStore = new AtomStoreImpl();
    NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
    grounder.getNoGoods(null);
    TrailAssignment assignment = new TrailAssignment(atomStore);
    int rId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("r", 0)));
    int nrId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("nr", 0)));
    assignment.growForMaxAtomId();
    assignment.assign(rId, ThriceTruth.FALSE);
    assignment.assign(nrId, ThriceTruth.TRUE);
    BasicAtom p5 = Atoms.newBasicAtom(Predicates.getPredicate("p", 1), Collections.singletonList(Terms.newConstant(5)));
    assignment.assign(atomStore.get(p5), ThriceTruth.MBT);
    Set<Literal> reasons = grounder.justifyAtom(atomStore.get(p5), assignment);
    assertFalse(reasons.isEmpty());
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) Test(org.junit.jupiter.api.Test)

Example 22 with BasicAtom

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

the class LiteralInstantiationStrategyTest method defaultLazyGroundingCheckMustBeTrueGroundLiteral.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to check the truth
 * (i.e. {@link AssignmentStatus}) of the positive ground literal "p(a)".
 *
 * In this case, the instantiation strategy has an assignment where the atom
 * "p(a)" is assigned ThriceTruth.MBT, so we expect the instantiation strategy
 * to determine that p(a) is TRUE. Furthermore, the stale atom set
 * (used by {@link NaiveGrounder} to clean up atoms that should be deleted from
 * working memory) must stay empty.
 */
@Test
public void defaultLazyGroundingCheckMustBeTrueGroundLiteral() {
    Predicate p = Predicates.getPredicate("p", 1);
    BasicAtom pOfA = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("a"));
    WorkingMemory workingMemory = new WorkingMemory();
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    atomStore.putIfAbsent(pOfA);
    assignment.growForMaxAtomId();
    assignment.assign(atomStore.get(pOfA), ThriceTruth.MBT);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, atomStore, Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(assignment);
    AssignmentStatus assignmentStatus = strategy.getTruthForGroundLiteral(Literals.fromAtom(pOfA, true));
    assertEquals(AssignmentStatus.TRUE, assignmentStatus);
    assertTrue(staleSet.isEmpty());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) 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 23 with BasicAtom

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

the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithUnassignedInstance.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to find the ground
 * instance "q(a, b)" for the partially ground positive literal "q(a, X)".
 *
 * In this case, the instantiation strategy has an empty assignment, so we
 * expect the assignment status (i.e. assignment status of the found ground
 * instance) passed back with the substitution to be UNASSIGNED. Since
 * UNASSIGNED and FALSE atoms are (potentially) stale in working memory, we
 * expect the atom "q(a, b)" to be added to the stale set by the instantiation
 * strategy.
 */
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithUnassignedInstance() {
    Predicate q = Predicates.getPredicate("q", 2);
    BasicAtom nonGroundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newVariable("X"));
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(q);
    workingMemory.addInstance(Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b")), true);
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, atomStore, Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(assignment);
    List<ImmutablePair<Substitution, AssignmentStatus>> result = strategy.getAcceptedSubstitutions(Literals.fromAtom(nonGroundAtom, true), new BasicSubstitution());
    assertEquals(1, result.size());
    ImmutablePair<Substitution, AssignmentStatus> substitutionInfo = result.get(0);
    Substitution substitution = substitutionInfo.left;
    AssignmentStatus assignmentStatus = substitutionInfo.right;
    assertEquals(AssignmentStatus.UNASSIGNED, assignmentStatus);
    assertTrue(substitution.isVariableSet(Terms.newVariable("X")));
    assertEquals(Terms.newSymbolicConstant("b"), substitution.eval(Terms.newVariable("X")));
    assertEquals(1, staleSet.size());
    assertTrue(staleSet.contains(Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b"))));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) 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) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Test(org.junit.jupiter.api.Test)

Example 24 with BasicAtom

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

the class AtomCounterTests method createRuleAtom.

private void createRuleAtom() {
    BasicAtom atomAA = Atoms.newBasicAtom(Predicates.getPredicate("aa", 0));
    CompiledRule ruleAA = new InternalRule(Heads.newNormalHead(atomAA), Collections.singletonList(Atoms.newBasicAtom(Predicates.getPredicate("bb", 0)).toLiteral(false)));
    atomStore.putIfAbsent(new RuleAtom(ruleAA, new BasicSubstitution()));
}
Also used : CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom) InternalRule(at.ac.tuwien.kr.alpha.core.rules.InternalRule)

Example 25 with BasicAtom

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

the class ChoiceHeadToNormal method apply.

@Override
public ASPCore2Program apply(ASPCore2Program inputProgram) {
    InputProgram.Builder programBuilder = InputProgram.builder();
    List<Rule<Head>> additionalRules = new ArrayList<>();
    List<Rule<Head>> srcRules = new ArrayList<>(inputProgram.getRules());
    Iterator<Rule<Head>> ruleIterator = srcRules.iterator();
    while (ruleIterator.hasNext()) {
        Rule<Head> rule = ruleIterator.next();
        Head ruleHead = rule.getHead();
        if (!(ruleHead instanceof ChoiceHead)) {
            // Rule is constraint or without choice in the head. Leave as is.
            continue;
        }
        // Remove this rule, as it will be transformed.
        ruleIterator.remove();
        ChoiceHead choiceHead = (ChoiceHead) ruleHead;
        // Choice rules with boundaries are not yet supported.
        if (choiceHead.getLowerBound() != null || choiceHead.getUpperBound() != null) {
            throw new UnsupportedOperationException("Found choice rule with bounds, which are not yet supported. Rule is: " + rule);
        }
        // Only rewrite rules with a choice in their head.
        for (ChoiceElement choiceElement : choiceHead.getChoiceElements()) {
            // Create two guessing rules for each choiceElement.
            // Construct common body to both rules.
            BasicAtom head = choiceElement.getChoiceAtom();
            List<Literal> ruleBody = new ArrayList<>(rule.getBody());
            ruleBody.addAll(choiceElement.getConditionLiterals());
            if (containsIntervalTerms(head)) {
                throw new RuntimeException("Program contains a choice rule with interval terms in its head. This is not supported (yet).");
            }
            // Construct head atom for the choice.
            Predicate headPredicate = head.getPredicate();
            Predicate negPredicate = Predicates.getPredicate(PREDICATE_NEGATION_PREFIX + headPredicate.getName(), headPredicate.getArity() + 1, true);
            List<Term> headTerms = new ArrayList<>(head.getTerms());
            // FIXME: when introducing classical negation, this is 1 for classical positive atoms and 0 for
            headTerms.add(0, Terms.newConstant("1"));
            // classical negative atoms.
            BasicAtom negHead = Atoms.newBasicAtom(negPredicate, headTerms);
            // Construct two guessing rules.
            List<Literal> guessingRuleBodyWithNegHead = new ArrayList<>(ruleBody);
            guessingRuleBodyWithNegHead.add(Atoms.newBasicAtom(head.getPredicate(), head.getTerms()).toLiteral(false));
            additionalRules.add(new BasicRule(Heads.newNormalHead(negHead), guessingRuleBodyWithNegHead));
            List<Literal> guessingRuleBodyWithHead = new ArrayList<>(ruleBody);
            guessingRuleBodyWithHead.add(Atoms.newBasicAtom(negPredicate, headTerms).toLiteral(false));
            additionalRules.add(new BasicRule(Heads.newNormalHead(head), guessingRuleBodyWithHead));
        // TODO: when cardinality constraints are possible, process the boundaries by adding a constraint with a cardinality check.
        }
    }
    return programBuilder.addRules(srcRules).addRules(additionalRules).addFacts(inputProgram.getFacts()).addInlineDirectives(inputProgram.getInlineDirectives()).build();
}
Also used : BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) ChoiceHead(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) ArrayList(java.util.ArrayList) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) ChoiceElement(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead.ChoiceElement) ChoiceHead(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Aggregations

BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)30 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)16 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)15 Test (org.junit.jupiter.api.Test)15 LinkedHashSet (java.util.LinkedHashSet)12 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)11 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)10 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)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 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)8 WritableAssignment (at.ac.tuwien.kr.alpha.core.solver.WritableAssignment)7 ArrayList (java.util.ArrayList)6 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)4 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)4 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)3 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)3 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)3 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)2