Search in sources :

Example 21 with Atom

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

the class AtomsTest method testIsBasicAtomGround.

@Test
public void testIsBasicAtomGround() {
    ASPCore2Program p = parser.parse("bla(blubb, foo(bar)).");
    Atom a = p.getFacts().get(0);
    assertBasicAtomGround(a, true);
    ASPCore2Program p1 = parser.parse("foo(1, 2, 3, \"bar\").");
    Atom a1 = p1.getFacts().get(0);
    assertBasicAtomGround(a1, true);
    ASPCore2Program p2 = parser.parse("foo(BAR).");
    Atom a2 = p2.getFacts().get(0);
    assertBasicAtomGround(a2, false);
    ASPCore2Program p3 = parser.parse("foo(b, a, r(\"bla\", BLUBB)).");
    Atom a3 = p3.getFacts().get(0);
    assertBasicAtomGround(a3, false);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) Test(org.junit.jupiter.api.Test)

Example 22 with Atom

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

the class AlphaImplTest method programWithExternalStringStuff.

@Test
@SuppressWarnings("unchecked")
public void programWithExternalStringStuff() throws IOException {
    Alpha alpha = new AlphaImpl();
    ASPCore2Program prog = alpha.readProgram(InputConfig.forString(STRINGSTUFF_ASP));
    Set<AnswerSet> answerSets = alpha.solve(prog).collect(Collectors.toSet());
    // Verify every result string has length 6 and contains "foo"
    for (AnswerSet as : answerSets) {
        for (Atom atom : as.getPredicateInstances(Predicates.getPredicate("resultstring", 1))) {
            String resultstring = ((ConstantTerm<String>) atom.getTerms().get(0)).getObject();
            assertEquals(6, resultstring.length());
            assertTrue(resultstring.contains("foo"));
        }
    }
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Example 23 with Atom

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

the class AtomsTest method testExternalHasOutput.

@Test
public void testExternalHasOutput() {
    ASPCore2Program p = parser.parse("a:- &extWithOutput[1](OUT).", externals);
    Atom ext = p.getRules().get(0).getBody().stream().findFirst().get().getAtom();
    assertExternalAtomGround(ext, false);
    assertTrue(((ExternalAtom) ext).hasOutput());
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) Test(org.junit.jupiter.api.Test)

Example 24 with Atom

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

the class ThreeColouringTestWithRandom method createEdges.

/**
 * @param n
 * @param shuffle if true, the vertex indices are shuffled with the given seed
 * @param seed
 * @return
 */
private List<Atom> createEdges(int n, boolean shuffle, int seed) {
    List<Atom> facts = new ArrayList<>(n);
    List<Integer> indices = new ArrayList<>();
    for (int i = 1; i <= n; i++) {
        indices.add(i);
    }
    if (shuffle) {
        Collections.shuffle(indices, new Random(seed));
    }
    for (int i = 1; i < n; i++) {
        facts.add(fact("e", indices.get(0), indices.get(i)));
    }
    for (int i = 1; i < n - 1; i++) {
        facts.add(fact("e", indices.get(i), indices.get(i + 1)));
    }
    facts.add(fact("e", indices.get(1), indices.get(n - 1)));
    return facts;
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)

Example 25 with Atom

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

the class FixedInterpretationLiteralsTest method positiveExternalBindingOutput.

@Test
public void positiveExternalBindingOutput() {
    Optional<AnswerSet> answer = this.alpha.solve(this.alpha.readProgramString(TEST_PROG, this.externals)).findFirst();
    assertTrue(answer.isPresent());
    AnswerSet answerSet = answer.get();
    Predicate pred = Predicates.getPredicate("positive_external_binding_output", 2);
    assertTrue(answerSet.getPredicates().contains(pred));
    Set<Atom> instances = answerSet.getPredicateInstances(pred);
    assertEquals(3, instances.size());
    assertTrue(instances.contains(Atoms.newBasicAtom(pred, Terms.newConstant("Klagenfurt"), Terms.newConstant("Villach"))));
    assertTrue(instances.contains(Atoms.newBasicAtom(pred, Terms.newConstant("Klagenfurt"), Terms.newConstant("Graz"))));
    assertTrue(instances.contains(Atoms.newBasicAtom(pred, Terms.newConstant("Villach"), Terms.newConstant("Salzburg"))));
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) 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

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