Search in sources :

Example 1 with TrailAssignment

use of at.ac.tuwien.kr.alpha.core.solver.TrailAssignment in project Alpha by alpha-asp.

the class LiteralInstantiationStrategyTest method defaultLazyGroundingCheckTrueGroundLiteral.

/**
 * 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.TRUE, 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 defaultLazyGroundingCheckTrueGroundLiteral() {
    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.TRUE);
    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 2 with TrailAssignment

use of at.ac.tuwien.kr.alpha.core.solver.TrailAssignment in project Alpha by alpha-asp.

the class LiteralInstantiationStrategyTest method defaultLazyGroundingCheckUnassignedGroundLiteral.

/**
 * 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 empty assignment,
 * so we expect the instantiation strategy to determine that p(a) is UNASSIGNED.
 * Since UNASSIGNED and FALSE atoms are (potentially) stale in working memory,
 * we expect the atom "p(a)" to be added to the stale set by the instantiation
 * strategy.
 */
@Test
public void defaultLazyGroundingCheckUnassignedGroundLiteral() {
    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);
    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.UNASSIGNED, assignmentStatus);
    assertEquals(1, staleSet.size());
    assertTrue(staleSet.contains(pOfA));
}
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 3 with TrailAssignment

use of at.ac.tuwien.kr.alpha.core.solver.TrailAssignment in project Alpha by alpha-asp.

the class AnalyzeUnjustifiedTest method justifyLargerRules.

@Test
public void justifyLargerRules() {
    String program = "p(X) :- q(X,Y), r(Y), not s(X,Y)." + "{ q(1,X)} :- dom(X)." + "dom(1..3)." + "{r(X)} :- p(X)." + "{r(2)}." + "{s(1,2)}." + ":- not p(1).";
    CompiledProgram internalProgram = parseAndPreprocess.apply(program);
    AtomStore atomStore = new AtomStoreImpl();
    NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
    grounder.getNoGoods(null);
    TrailAssignment assignment = new TrailAssignment(atomStore);
    Atom p1 = parser.parse("p(1).").getFacts().get(0);
    Atom r2 = parser.parse("r(2).").getFacts().get(0);
    Atom s12 = parser.parse("s(1,2).").getFacts().get(0);
    Atom q11 = parser.parse("q(1,1).").getFacts().get(0);
    Atom q12 = parser.parse("q(1,2).").getFacts().get(0);
    Atom q13 = parser.parse("q(1,3).").getFacts().get(0);
    int p1Id = atomStore.get(p1);
    int r2Id = atomStore.get(r2);
    int s12Id = atomStore.get(s12);
    int q11Id = atomStore.get(q11);
    int q12Id = atomStore.get(q12);
    int q13Id = atomStore.get(q13);
    assignment.growForMaxAtomId();
    assignment.assign(p1Id, ThriceTruth.MBT);
    assignment.assign(r2Id, ThriceTruth.TRUE);
    assignment.assign(s12Id, ThriceTruth.TRUE);
    assignment.assign(q11Id, ThriceTruth.TRUE);
    assignment.assign(q12Id, ThriceTruth.TRUE);
    assignment.assign(q13Id, ThriceTruth.FALSE);
    Set<Literal> reasons = grounder.justifyAtom(p1Id, 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) 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) Test(org.junit.jupiter.api.Test)

Example 4 with TrailAssignment

use of at.ac.tuwien.kr.alpha.core.solver.TrailAssignment in project Alpha by alpha-asp.

the class AnalyzeUnjustifiedTest method justifyNegatedFactsRemovedFromReasons.

@Test
public void justifyNegatedFactsRemovedFromReasons() {
    String program = "forbidden(2,9). forbidden(1,9)." + "p(X) :- q(X)." + "q(X) :- p(X)." + "q(5) :- r." + "r :- not nr, not forbidden(2,9), not forbidden(1,9)." + "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());
    for (Literal literal : reasons) {
        // Check that facts are not present in justification.
        assertNotEquals(literal.getPredicate(), Predicates.getPredicate("forbidden", 2));
    }
}
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 5 with TrailAssignment

use of at.ac.tuwien.kr.alpha.core.solver.TrailAssignment in project Alpha by alpha-asp.

the class NaiveGrounderTest method groundConstraintAlreadyGround.

/**
 * Asserts that a ground constraint whose positive body is not satisfied by the empty assignment
 * is grounded immediately.
 */
@Test
public void groundConstraintAlreadyGround() {
    ASPCore2Program program = PROGRAM_PARSER.parse("a :- not b. " + "b :- not a. " + ":- b.");
    NormalProgram normal = NORMALIZE_TRANSFORM.apply(program);
    InternalProgram prog = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normal));
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance("naive", prog, atomStore, true);
    Map<Integer, NoGood> noGoods = grounder.getNoGoods(new TrailAssignment(atomStore));
    int litB = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("b")));
    assertTrue(noGoods.containsValue(NoGood.fromConstraint(Collections.singletonList(litB), Collections.emptyList())));
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) Test(org.junit.jupiter.api.Test)

Aggregations

TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)25 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)23 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)20 Test (org.junit.jupiter.api.Test)17 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)11 WritableAssignment (at.ac.tuwien.kr.alpha.core.solver.WritableAssignment)10 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)9 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)8 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)7 NoGood (at.ac.tuwien.kr.alpha.core.common.NoGood)7 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)7 LinkedHashSet (java.util.LinkedHashSet)7 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)6 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)6 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)6 StratifiedEvaluation (at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation)6 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)5 NaiveGrounder (at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder)5