Search in sources :

Example 6 with NaiveGrounder

use of at.ac.tuwien.kr.alpha.grounder.NaiveGrounder in project Alpha by alpha-asp.

the class SolverTests method guessingAndConstraints.

@Test
public void guessingAndConstraints() throws IOException {
    String testProgram = "node(a).\n" + "node(b).\n" + "edge(b,a).\n" + "in(X) :- not out(X), node(X).\n" + "out(X) :- not in(X), node(X).\n" + ":- in(X), in(Y), edge(X,Y).";
    ParsedProgram parsedProgram = parseVisit(testProgram);
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    final BasicAnswerSet.Builder base = new BasicAnswerSet.Builder().predicate("node").instance("a").instance("b");
    Set<AnswerSet> expected = new HashSet<>(Arrays.asList(new BasicAnswerSet.Builder(base).predicate("in").instance("b").predicate("out").instance("a").predicate("edge").instance("b", "a").build(), new BasicAnswerSet.Builder(base).predicate("in").instance("a").predicate("out").instance("b").predicate("edge").instance("b", "a").build(), new BasicAnswerSet.Builder(base).predicate("out").instance("a").instance("b").predicate("edge").instance("b", "a").build()));
    Set<AnswerSet> answerSets = solver.collectSet();
    assertEquals(expected, answerSets);
}
Also used : BasicAnswerSet(at.ac.tuwien.kr.alpha.common.BasicAnswerSet) NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) BasicAnswerSet(at.ac.tuwien.kr.alpha.common.BasicAnswerSet) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet) Test(org.junit.Test)

Example 7 with NaiveGrounder

use of at.ac.tuwien.kr.alpha.grounder.NaiveGrounder in project Alpha by alpha-asp.

the class SolverTests method builtinAtoms.

@Test
public void builtinAtoms() throws IOException {
    String testProgram = "dom(1). dom(2). dom(3). dom(4). dom(5)." + "p(X) :- dom(X), X = 4." + "r(Y) :- dom(Y), Y <= 2.";
    ParsedProgram parsedProgram = parseVisit(testProgram);
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    Set<AnswerSet> expected = new HashSet<>(Collections.singletonList(new BasicAnswerSet.Builder().predicate("dom").instance("1").instance("2").instance("3").instance("4").instance("5").predicate("p").instance("4").predicate("r").instance("1").instance("2").build()));
    Set<AnswerSet> answerSets = solver.collectSet();
    assertEquals(expected, answerSets);
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) BasicAnswerSet(at.ac.tuwien.kr.alpha.common.BasicAnswerSet) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet) Test(org.junit.Test)

Example 8 with NaiveGrounder

use of at.ac.tuwien.kr.alpha.grounder.NaiveGrounder in project Alpha by alpha-asp.

the class SolverTests method guessingConstraintsInequality.

@Test
public void guessingConstraintsInequality() throws IOException {
    String program = "assign(L, R) :- not nassign(L, R), possible(L, R).\n" + "nassign(L, R) :- not assign(L, R), possible(L, R).\n" + "\n" + "assigned(L) :- assign(L, R).\n" + ":- possible(L,_), not assigned(L).\n" + ":- assign(L, R1), assign(L, R2), R1 != R2.\n" + "\n" + "possible(l1, r1). possible(l3, r3). possible(l4, r1). possible(l4, r3). possible(l5, r4). possible(l6, r2). possible(l7, r3). possible(l8, r2). possible(l9, r1). possible(l9, r4).\n";
    ParsedProgram parsedProgram = parseVisit(program);
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    final BasicAnswerSet.Builder base = new BasicAnswerSet.Builder().predicate("possible").instance("l1", "r1").instance("l3", "r3").instance("l4", "r1").instance("l4", "r3").instance("l5", "r4").instance("l6", "r2").instance("l7", "r3").instance("l8", "r2").instance("l9", "r1").instance("l9", "r4").predicate("assign").instance("l1", "r1").instance("l3", "r3").instance("l5", "r4").instance("l6", "r2").instance("l7", "r3").instance("l8", "r2").predicate("assigned").instance("l1").instance("l3").instance("l4").instance("l5").instance("l6").instance("l7").instance("l8").instance("l9");
    Set<AnswerSet> expected = new HashSet<>(Arrays.asList(new BasicAnswerSet.Builder(base).predicate("assign").instance("l4", "r1").instance("l9", "r4").predicate("nassign").instance("l4", "r3").instance("l9", "r1").build(), new BasicAnswerSet.Builder(base).predicate("assign").instance("l4", "r1").instance("l9", "r1").predicate("nassign").instance("l4", "r3").instance("l9", "r4").build(), new BasicAnswerSet.Builder(base).predicate("assign").instance("l4", "r3").instance("l9", "r4").predicate("nassign").instance("l4", "r1").instance("l9", "r1").build(), new BasicAnswerSet.Builder(base).predicate("assign").instance("l4", "r3").instance("l9", "r1").predicate("nassign").instance("l4", "r1").instance("l9", "r4").build()));
    Set<AnswerSet> answerSets = solver.collectSet();
    assertEquals(expected, answerSets);
}
Also used : BasicAnswerSet(at.ac.tuwien.kr.alpha.common.BasicAnswerSet) NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) BasicAnswerSet(at.ac.tuwien.kr.alpha.common.BasicAnswerSet) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet) Test(org.junit.Test)

Example 9 with NaiveGrounder

use of at.ac.tuwien.kr.alpha.grounder.NaiveGrounder in project Alpha by alpha-asp.

the class PartSubpartConfigurationTest method testPartSubpart.

private void testPartSubpart(int n) throws IOException {
    List<String> rules = new ArrayList<>();
    for (int i = 1; i <= n; i++) {
        rules.add(String.format("n(%d).", i));
    }
    rules.add("part(N) :- n(N), not no_part(N).");
    rules.add("no_part(N) :- n(N), not part(N).");
    rules.add("subpartid(SP,ID) :- subpart(SP,P), n(ID), not no_subpartid(SP,ID).");
    rules.add("no_subpartid(SP,ID) :- subpart(SP,P), n(ID), not subpartid(SP,ID).");
    rules.add("subpart(SP,P) :- part(P), part(SP), P != SP, not no_subpart(SP,P).");
    rules.add("no_subpart(SP,P) :- part(P), part(SP), P != SP, not subpart(SP,P).");
    rules.add(":- subpart(SP,P1), subpart(SP,P2), P1 != P2.");
    rules.add(":- subpart(SP1,P), subpart(SP2, P), SP1!=SP2, subpartid(SP1,ID), subpartid(SP2,ID).");
    String testProgram = concat(rules);
    ParsedProgram parsedProgram = parseVisit(testProgram);
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    Optional<AnswerSet> answerSet = solver.stream().findAny();
    System.out.println(answerSet);
// TODO: check correctness of answer set
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet)

Example 10 with NaiveGrounder

use of at.ac.tuwien.kr.alpha.grounder.NaiveGrounder in project Alpha by alpha-asp.

the class SolverTests method testFunctionTermEquality.

@Test
public void testFunctionTermEquality() throws IOException {
    String testProgram = "r1(f(a,b)). r2(f(a,b)). a :- r1(X), r2(Y), X = Y.";
    ParsedProgram parsedProgram = parseVisit(testProgram);
    Grounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    Set<AnswerSet> expected = new HashSet<>(Collections.singletonList(new BasicAnswerSet.Builder().predicate("r1").instance("f(a,b)").predicate("r2").instance("f(a,b)").predicate("a").build()));
    Set<AnswerSet> answerSets = solver.collectSet();
    assertEquals(expected, answerSets);
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) BasicAnswerSet(at.ac.tuwien.kr.alpha.common.BasicAnswerSet) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet) ChoiceGrounder(at.ac.tuwien.kr.alpha.grounder.ChoiceGrounder) DummyGrounder(at.ac.tuwien.kr.alpha.grounder.DummyGrounder) Grounder(at.ac.tuwien.kr.alpha.grounder.Grounder) NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) Test(org.junit.Test)

Aggregations

NaiveGrounder (at.ac.tuwien.kr.alpha.grounder.NaiveGrounder)34 AnswerSet (at.ac.tuwien.kr.alpha.common.AnswerSet)31 ParsedProgram (at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram)29 BasicAnswerSet (at.ac.tuwien.kr.alpha.common.BasicAnswerSet)24 Test (org.junit.Test)24 ChoiceGrounder (at.ac.tuwien.kr.alpha.grounder.ChoiceGrounder)6 DummyGrounder (at.ac.tuwien.kr.alpha.grounder.DummyGrounder)6 Grounder (at.ac.tuwien.kr.alpha.grounder.Grounder)6 ANTLRFileStream (org.antlr.v4.runtime.ANTLRFileStream)3 Before (org.junit.Before)2