Search in sources :

Example 11 with AtomStore

use of at.ac.tuwien.kr.alpha.core.common.AtomStore in project Alpha by alpha-asp.

the class AlphaImpl method prepareSolverFor.

/**
 * Prepares a solver (and accompanying grounder) instance pre-loaded with the given program. Use this if the
 * solver is needed after reading answer sets (e.g. for obtaining statistics).
 *
 * @param program the program to solve.
 * @param filter  a (java util) predicate that filters (asp-)predicates which should be contained in the answer
 *                set stream from the solver.
 * @return a solver (and accompanying grounder) instance pre-loaded with the given program.
 */
private Solver prepareSolverFor(CompiledProgram program, java.util.function.Predicate<Predicate> filter) {
    String grounderName = config.getGrounderName();
    boolean doDebugChecks = config.isDebugInternalChecks();
    GrounderHeuristicsConfiguration grounderHeuristicConfiguration = GrounderHeuristicsConfiguration.getInstance(config.getGrounderToleranceConstraints(), config.getGrounderToleranceRules());
    grounderHeuristicConfiguration.setAccumulatorEnabled(config.isGrounderAccumulatorEnabled());
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance(grounderName, program, atomStore, filter, grounderHeuristicConfiguration, doDebugChecks);
    return SolverFactory.getInstance(config, atomStore, grounder);
}
Also used : AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) GrounderHeuristicsConfiguration(at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) Grounder(at.ac.tuwien.kr.alpha.core.grounder.Grounder)

Example 12 with AtomStore

use of at.ac.tuwien.kr.alpha.core.common.AtomStore in project Alpha by alpha-asp.

the class TestUtils method buildSolverFromSystemConfig.

private static Solver buildSolverFromSystemConfig(ASPCore2Program prog, SystemConfig cfg) {
    AtomStore atomStore = new AtomStoreImpl();
    NormalProgram normalProg = new NormalizeProgramTransformation(cfg.getAggregateRewritingConfig()).apply(prog);
    InternalProgram preprocessed = cfg.isEvaluateStratifiedPart() ? new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normalProg)) : InternalProgram.fromNormalProgram(normalProg);
    return SolverFactory.getInstance(cfg, atomStore, GrounderFactory.getInstance(cfg.getGrounderName(), preprocessed, atomStore, cfg.isDebugInternalChecks()));
}
Also used : AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) 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) NormalizeProgramTransformation(at.ac.tuwien.kr.alpha.core.programs.transformation.NormalizeProgramTransformation) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation)

Example 13 with AtomStore

use of at.ac.tuwien.kr.alpha.core.common.AtomStore in project Alpha by alpha-asp.

the class NoGoodGeneratorTest method collectNeg_ContainsOnlyPositiveLiterals.

/**
 * Calls {@link NoGoodGenerator#collectNegLiterals(InternalRule, Substitution)}, which puts the atom occurring
 * negatively in a rule into the atom store. It is then checked whether the atom in the atom store is positive.
 */
@Test
public void collectNeg_ContainsOnlyPositiveLiterals() {
    ASPCore2Program input = PARSER.parse("p(a,b). " + "q(a,b) :- not nq(a,b). " + "nq(a,b) :- not q(a,b).");
    NormalProgram normal = NORMALIZE_TRANSFORM.apply(input);
    CompiledProgram program = InternalProgram.fromNormalProgram(normal);
    CompiledRule rule = program.getRules().get(1);
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance("naive", program, atomStore, true);
    NoGoodGenerator noGoodGenerator = ((NaiveGrounder) grounder).noGoodGenerator;
    Substitution substitution = new BasicSubstitution();
    substitution.put(X, A);
    substitution.put(Y, B);
    List<Integer> collectedNeg = noGoodGenerator.collectNegLiterals(rule, substitution);
    assertEquals(1, collectedNeg.size());
    String negAtomString = atomStore.atomToString(Literals.atomOf(collectedNeg.get(0)));
    assertEquals("q(a, b)", negAtomString);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) Test(org.junit.jupiter.api.Test)

Example 14 with AtomStore

use of at.ac.tuwien.kr.alpha.core.common.AtomStore in project Alpha by alpha-asp.

the class SolverTests method dummyGrounder.

@RegressionTest
public void dummyGrounder(RegressionTestConfig cfg) {
    AtomStore atomStore = new AtomStoreImpl();
    assertEquals(DummyGrounder.EXPECTED, buildSolverForRegressionTest(atomStore, new DummyGrounder(atomStore), cfg).collectSet());
}
Also used : AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) DummyGrounder(at.ac.tuwien.kr.alpha.core.grounder.DummyGrounder) TestUtils.buildSolverForRegressionTest(at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest)

Example 15 with AtomStore

use of at.ac.tuwien.kr.alpha.core.common.AtomStore in project Alpha by alpha-asp.

the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithTrueInstance.

/**
 * 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 assignment where q(a, b) is
 * assigned ThriceTruth.TRUE, so we expect the assignment status (i.e.
 * assignment status of the found ground instance) passed back with the
 * substitution to be TRUE. Furthermore, we expect the stale atom set to stay
 * empty.
 */
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithTrueInstance() {
    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);
    BasicAtom groundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b"));
    atomStore.putIfAbsent(groundAtom);
    assignment.growForMaxAtomId();
    assignment.assign(atomStore.get(groundAtom), ThriceTruth.TRUE);
    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.TRUE, assignmentStatus);
    assertTrue(substitution.isVariableSet(Terms.newVariable("X")));
    assertEquals(Terms.newSymbolicConstant("b"), substitution.eval(Terms.newVariable("X")));
    assertTrue(staleSet.isEmpty());
}
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)

Aggregations

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