Search in sources :

Example 1 with BasicAtom

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

the class StratifiedEvaluationTest method testRecursiveRanking.

@Test
public void testRecursiveRanking() {
    // @formatter:off
    String asp = "thing(a).\n" + "thing(b).\n" + "thing(c).\n" + "thing_before(a, b).\n" + "thing_before(b, c).\n" + "has_prev_thing(X) :- thing(X), thing_succ(_, X).\n" + "first_thing(X) :- thing(X), not has_prev_thing(X).\n" + "thing_not_succ(X, Y) :-\n" + "	thing(X),\n" + "	thing(Y),\n" + "	thing(INTM),\n" + "	thing_before(X, Y),\n" + "	thing_before(X, INTM),\n" + "	thing_before(INTM, X).\n" + "thing_succ(X, Y) :-\n" + "	thing(X),\n" + "	thing(Y),\n" + "	thing_before(X, Y),\n" + "	not thing_not_succ(X, Y).\n" + "thing_rank(X, 1) :- first_thing(X).\n" + "thing_rank(X, R) :-\n" + "	thing(X),\n" + "	thing_succ(Y, X),\n" + "	thing_rank(Y, K),\n" + "	R = K + 1.";
    // @formatter:on
    CompiledProgram evaluated = parseAndEvaluate.apply(asp);
    Predicate rank = Predicates.getPredicate("thing_rank", 2);
    BasicAtom rank1 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("a"), Terms.newConstant(1));
    BasicAtom rank2 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("b"), Terms.newConstant(2));
    BasicAtom rank3 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("c"), Terms.newConstant(3));
    List<Atom> evaluatedFacts = evaluated.getFacts();
    assertTrue(evaluatedFacts.contains(rank1));
    assertTrue(evaluatedFacts.contains(rank2));
    assertTrue(evaluatedFacts.contains(rank3));
}
Also used : CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) 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 BasicAtom

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

the class PredicateInternalizer method makePrefixedPredicatesInternal.

public static ASPCore2Program makePrefixedPredicatesInternal(ASPCore2Program program, String prefix) {
    InputProgram.Builder prgBuilder = InputProgram.builder();
    for (Atom atom : program.getFacts()) {
        if (atom.getPredicate().getName().startsWith(prefix)) {
            prgBuilder.addFact(PredicateInternalizer.makePredicateInternal((BasicAtom) atom));
        } else {
            prgBuilder.addFact(atom);
        }
    }
    for (Rule<Head> rule : program.getRules()) {
        prgBuilder.addRule(PredicateInternalizer.makePrefixedPredicatesInternal(rule, prefix));
    }
    prgBuilder.addInlineDirectives(program.getInlineDirectives());
    return prgBuilder.build();
}
Also used : Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Example 3 with BasicAtom

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

the class LiteralInstantiationStrategyTest method defaultLazyGroundingNoAssignmentGroundLiteral.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to check the truth
 * (i.e. {@link AssignmentStatus}) of the positive ground literal "p(a)".
 *
 * In this case, the instantiation strategy does not have an assignment set (as
 * is the case when {@link NaiveGrounder} is in bootstrap),
 * 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 defaultLazyGroundingNoAssignmentGroundLiteral() {
    Predicate p = Predicates.getPredicate("p", 1);
    BasicAtom pOfA = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("a"));
    WorkingMemory workingMemory = new WorkingMemory();
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, new AtomStoreImpl(), Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(null);
    AssignmentStatus assignmentStatus = strategy.getTruthForGroundLiteral(Literals.fromAtom(pOfA, true));
    assertEquals(AssignmentStatus.TRUE, assignmentStatus);
    assertTrue(staleSet.isEmpty());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) 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 4 with BasicAtom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom 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 5 with BasicAtom

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

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