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.");
}
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());
}
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"))));
}
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);
}
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.");
}
Aggregations