Search in sources :

Example 1 with ASPCore2Program

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

the class ParserTest method parseFactWithFunctionTerms.

@Test
public void parseFactWithFunctionTerms() {
    ASPCore2Program parsedProgram = parser.parse("p(f(a),g(h(Y))).");
    assertEquals(1, parsedProgram.getFacts().size(), "Program contains one fact.");
    assertEquals("p", parsedProgram.getFacts().get(0).getPredicate().getName(), "Predicate name of fact is p.");
    assertEquals(2, parsedProgram.getFacts().get(0).getPredicate().getArity(), "Fact has two terms.");
    assertEquals("f", ((FunctionTerm) parsedProgram.getFacts().get(0).getTerms().get(0)).getSymbol(), "First term is function term f.");
    assertEquals("g", ((FunctionTerm) parsedProgram.getFacts().get(0).getTerms().get(1)).getSymbol(), "Second term is function term g.");
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Test(org.junit.jupiter.api.Test)

Example 2 with ASPCore2Program

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

the class ParserTest method cardinalityAggregate.

@Test
public void cardinalityAggregate() {
    ASPCore2Program parsedProgram = parser.parse("num(K) :-  K <= #count {X,Y,Z : p(X,Y,Z) }, dom(K).");
    Optional<Literal> optionalBodyElement = parsedProgram.getRules().get(0).getBody().stream().filter((lit) -> lit instanceof AggregateLiteral).findFirst();
    assertTrue(optionalBodyElement.isPresent());
    Literal bodyElement = optionalBodyElement.get();
    AggregateLiteral parsedAggregate = (AggregateLiteral) bodyElement;
    VariableTerm x = Terms.newVariable("X");
    VariableTerm y = Terms.newVariable("Y");
    VariableTerm z = Terms.newVariable("Z");
    List<Term> basicTerms = Arrays.asList(x, y, z);
    AggregateAtom.AggregateElement aggregateElement = Atoms.newAggregateElement(basicTerms, Collections.singletonList(Atoms.newBasicAtom(Predicates.getPredicate("p", 3), x, y, z).toLiteral()));
    AggregateAtom expectedAggregate = Atoms.newAggregateAtom(ComparisonOperators.LE, Terms.newVariable("K"), null, null, AggregateAtom.AggregateFunctionSymbol.COUNT, Collections.singletonList(aggregateElement));
    assertEquals(expectedAggregate, parsedAggregate.getAtom());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Arrays(java.util.Arrays) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) InlineDirectives(at.ac.tuwien.kr.alpha.api.programs.InlineDirectives) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) CharStreams(org.antlr.v4.runtime.CharStreams) CharStream(org.antlr.v4.runtime.CharStream) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ChoiceHead(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead) IOException(java.io.IOException) Atoms(at.ac.tuwien.kr.alpha.commons.atoms.Atoms) Test(org.junit.jupiter.api.Test) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) List(java.util.List) Predicates(at.ac.tuwien.kr.alpha.commons.Predicates) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Util(at.ac.tuwien.kr.alpha.commons.util.Util) Optional(java.util.Optional) Collections(java.util.Collections) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonOperators(at.ac.tuwien.kr.alpha.commons.comparisons.ComparisonOperators) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) Test(org.junit.jupiter.api.Test)

Example 3 with ASPCore2Program

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

the class ParserTest method parseInterval.

@Test
public void parseInterval() {
    ASPCore2Program parsedProgram = parser.parse("fact(2..5). p(X) :- q(a, 3 .. X).");
    IntervalTerm factInterval = (IntervalTerm) parsedProgram.getFacts().get(0).getTerms().get(0);
    assertTrue(factInterval.equals(IntervalTerm.getInstance(Terms.newConstant(2), Terms.newConstant(5))));
    IntervalTerm bodyInterval = (IntervalTerm) parsedProgram.getRules().get(0).getBody().stream().findFirst().get().getTerms().get(1);
    assertTrue(bodyInterval.equals(IntervalTerm.getInstance(Terms.newConstant(3), Terms.newVariable("X"))));
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) Test(org.junit.jupiter.api.Test)

Example 4 with ASPCore2Program

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

the class ParserTest method stringWithEscapedQuotes.

@Test
public void stringWithEscapedQuotes() throws IOException {
    CharStream stream = CharStreams.fromStream(ParserTest.class.getResourceAsStream("/escaped_quotes.asp"));
    ASPCore2Program prog = parser.parse(stream);
    assertEquals(1, prog.getFacts().size());
    Atom stringAtom = prog.getFacts().get(0);
    String stringWithQuotes = stringAtom.getTerms().get(0).toString();
    assertEquals("\"a string with \"quotes\"\"", stringWithQuotes);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) CharStream(org.antlr.v4.runtime.CharStream) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Example 5 with ASPCore2Program

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

the class ParserTest method parseSmallProgram.

@Test
public void parseSmallProgram() {
    ASPCore2Program parsedProgram = parser.parse("a :- b, not d." + System.lineSeparator() + "c(X) :- p(X,a,_), q(Xaa,xaa)." + System.lineSeparator() + ":- f(Y).");
    assertEquals(3, parsedProgram.getRules().size(), "Program contains three rules.");
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Test(org.junit.jupiter.api.Test)

Aggregations

ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)89 Test (org.junit.jupiter.api.Test)70 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)23 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)22 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)21 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)19 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)15 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)14 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)12 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)11 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)10 Disabled (org.junit.jupiter.api.Disabled)9 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)8 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)8 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)8 HashSet (java.util.HashSet)8 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)7 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)7 SCComponent (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent)7 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)7