Search in sources :

Example 26 with Atom

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

the class SolverTests method instanceEnumerationMultipleIdentifiers.

@RegressionTest
public void instanceEnumerationMultipleIdentifiers(RegressionTestConfig cfg) {
    Set<AnswerSet> answerSets = buildSolverForRegressionTest("# enumeration_predicate_is enum." + "dom(a). dom(b). dom(c). dom(d)." + "p(X) :- dom(X)." + "unique_position1(Term,Pos) :- p(Term), enum(id,Term,Pos)." + "unique_position2(Term,Pos) :- p(Term), enum(otherid,Term,Pos)." + "wrong_double_occurrence :- unique_position(T1,P), unique_position(T2,P), T1 != T2." + "wrong_double_occurrence :- unique_position2(T1,P), unique_position(T2,P), T1 != T2.", cfg).collectSet();
    // Since enumeration depends on evaluation, we do not know which unique_position is actually assigned.
    // Check manually that there is one answer set, wrong_double_occurrence has not been derived, and enum yielded a unique position for each term.
    assertEquals(1, answerSets.size());
    AnswerSet answerSet = answerSets.iterator().next();
    assertPropositionalPredicateFalse(answerSet, Predicates.getPredicate("wrong_double_occurrence", 0));
    SortedSet<Atom> positions = answerSet.getPredicateInstances(Predicates.getPredicate("unique_position1", 2));
    assertEnumerationPositions(positions, 4);
    SortedSet<Atom> positions2 = answerSet.getPredicateInstances(Predicates.getPredicate("unique_position2", 2));
    assertEnumerationPositions(positions2, 4);
}
Also used : TestUtils.assertRegressionTestAnswerSet(at.ac.tuwien.kr.alpha.core.test.util.TestUtils.assertRegressionTestAnswerSet) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) TestUtils.buildSolverForRegressionTest(at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest)

Example 27 with Atom

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

the class SolverTests method testObjectProgram.

@RegressionTest
public void testObjectProgram(RegressionTestConfig cfg) {
    final Thingy thingy = new Thingy();
    final Atom fact = Atoms.newBasicAtom(Predicates.getPredicate("foo", 1), Terms.newConstant(thingy));
    final InputProgram program = new InputProgram(Collections.emptyList(), Collections.singletonList(fact), new InlineDirectivesImpl());
    assertEquals(singleton(new AnswerSetBuilder().predicate("foo").instance(thingy).build()), collectRegressionTestAnswerSets(program, cfg));
}
Also used : AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) InlineDirectivesImpl(at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram) TestUtils.buildSolverForRegressionTest(at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest)

Example 28 with Atom

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

the class SolverTests method assertEnumerationPositions.

private void assertEnumerationPositions(SortedSet<Atom> positions, int numPositions) {
    assertEquals(numPositions, positions.size());
    boolean[] usedPositions = new boolean[numPositions];
    for (Atom position : positions) {
        @SuppressWarnings("unchecked") Integer atomPos = ((ConstantTerm<Integer>) position.getTerms().get(1)).getObject() - 1;
        assertTrue(atomPos < numPositions);
        usedPositions[atomPos] = true;
    }
    for (int i = 0; i < numPositions; i++) {
        assertTrue(usedPositions[i]);
    }
}
Also used : Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)

Example 29 with Atom

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

the class ThreeColouringRandomGraphTest method createEdges.

private List<Atom> createEdges(int vertices, int edges) {
    Random rand = new Random(0);
    List<Atom> facts = new ArrayList<>(edges);
    for (int i = 0; i < edges; i++) {
        int v1 = 0;
        int v2 = 0;
        while (v1 == v2) {
            v1 = rand.nextInt(vertices);
            v2 = rand.nextInt(vertices);
        }
        facts.add(fact("e", v1, v2));
        facts.add(fact("e", v2, v1));
    }
    return facts;
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)

Example 30 with Atom

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

the class ThreeColouringWheelTest method createColors.

private List<Atom> createColors(String... colours) {
    List<Atom> facts = new ArrayList<>(colours.length);
    Predicate predicate = Predicates.getPredicate("c", 1);
    for (String colour : colours) {
        List<Term> terms = new ArrayList<>(1);
        terms.add(Terms.newConstant(colour));
        facts.add(Atoms.newBasicAtom(predicate, terms));
    }
    return facts;
}
Also used : ArrayList(java.util.ArrayList) Term(at.ac.tuwien.kr.alpha.api.terms.Term) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

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