use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom 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.atoms.Atom in project Alpha by alpha-asp.
the class StratifiedEvaluationTest method testRecursiveRanking.
@Test
public void testRecursiveRanking() {
// @formatter:off
String asp = "thing(a).\n" + "thing(b).\n" + "thing(c).\n" + "thing_before(a, b).\n" + "thing_before(b, c).\n" + "has_prev_thing(X) :- thing(X), thing_succ(_, X).\n" + "first_thing(X) :- thing(X), not has_prev_thing(X).\n" + "thing_not_succ(X, Y) :-\n" + " thing(X),\n" + " thing(Y),\n" + " thing(INTM),\n" + " thing_before(X, Y),\n" + " thing_before(X, INTM),\n" + " thing_before(INTM, X).\n" + "thing_succ(X, Y) :-\n" + " thing(X),\n" + " thing(Y),\n" + " thing_before(X, Y),\n" + " not thing_not_succ(X, Y).\n" + "thing_rank(X, 1) :- first_thing(X).\n" + "thing_rank(X, R) :-\n" + " thing(X),\n" + " thing_succ(Y, X),\n" + " thing_rank(Y, K),\n" + " R = K + 1.";
// @formatter:on
CompiledProgram evaluated = parseAndEvaluate.apply(asp);
Predicate rank = Predicates.getPredicate("thing_rank", 2);
BasicAtom rank1 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("a"), Terms.newConstant(1));
BasicAtom rank2 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("b"), Terms.newConstant(2));
BasicAtom rank3 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("c"), Terms.newConstant(3));
List<Atom> evaluatedFacts = evaluated.getFacts();
assertTrue(evaluatedFacts.contains(rank1));
assertTrue(evaluatedFacts.contains(rank2));
assertTrue(evaluatedFacts.contains(rank3));
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.
the class StratifiedEvaluationTest method testEqualityWithConstantTerms.
@Test
public void testEqualityWithConstantTerms() {
String aspStr = "equal :- 1 = 1.";
CompiledProgram evaluated = parseAndEvaluate.apply(aspStr);
Atom equal = TestUtils.basicAtomWithSymbolicTerms("equal");
assertTrue(evaluated.getFacts().contains(equal));
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.
the class IntervalTermToIntervalAtom method rewriteLiteral.
/**
* Replaces every IntervalTerm by a new variable and returns a mapping of the replaced VariableTerm -> IntervalTerm.
*
* @return the rewritten literal or null if the literal should be dropped from the final rule.
*/
private static Literal rewriteLiteral(Literal lit, Map<VariableTerm, IntervalTerm> intervalReplacement) {
// final rule.
if (lit instanceof ComparisonLiteral && ((ComparisonLiteral) lit).isNormalizedEquality()) {
ComparisonAtom equalityLiteral = (ComparisonAtom) lit.getAtom();
if (equalityLiteral.getTerms().get(0) instanceof VariableTerm && equalityLiteral.getTerms().get(1) instanceof IntervalTerm) {
// Literal is of the form "X = A .. B".
intervalReplacement.put((VariableTerm) equalityLiteral.getTerms().get(0), (IntervalTerm) equalityLiteral.getTerms().get(1));
return null;
}
if (equalityLiteral.getTerms().get(1) instanceof VariableTerm && equalityLiteral.getTerms().get(0) instanceof IntervalTerm) {
// Literal is of the form "A .. B = X".
intervalReplacement.put((VariableTerm) equalityLiteral.getTerms().get(1), (IntervalTerm) equalityLiteral.getTerms().get(0));
return null;
}
}
Atom atom = lit.getAtom();
List<Term> termList = new ArrayList<>(atom.getTerms());
boolean didChange = false;
for (int i = 0; i < termList.size(); i++) {
Term term = termList.get(i);
if (term instanceof IntervalTerm) {
VariableTerm replacementVariable = Terms.newVariable(INTERVAL_VARIABLE_PREFIX + intervalReplacement.size());
intervalReplacement.put(replacementVariable, (IntervalTerm) term);
termList.set(i, replacementVariable);
didChange = true;
}
if (term instanceof FunctionTerm) {
// Rewrite function terms recursively.
FunctionTerm rewrittenFunctionTerm = rewriteFunctionTerm((FunctionTerm) term, intervalReplacement);
termList.set(i, rewrittenFunctionTerm);
didChange = true;
}
}
if (didChange) {
Atom rewrittenAtom = atom.withTerms(termList);
return lit.isNegated() ? rewrittenAtom.toLiteral().negate() : rewrittenAtom.toLiteral();
}
return lit;
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.
the class PredicateInternalizer method makePrefixedPredicatesInternal.
public static ASPCore2Program makePrefixedPredicatesInternal(ASPCore2Program program, String prefix) {
InputProgram.Builder prgBuilder = InputProgram.builder();
for (Atom atom : program.getFacts()) {
if (atom.getPredicate().getName().startsWith(prefix)) {
prgBuilder.addFact(PredicateInternalizer.makePredicateInternal((BasicAtom) atom));
} else {
prgBuilder.addFact(atom);
}
}
for (Rule<Head> rule : program.getRules()) {
prgBuilder.addRule(PredicateInternalizer.makePrefixedPredicatesInternal(rule, prefix));
}
prgBuilder.addInlineDirectives(program.getInlineDirectives());
return prgBuilder.build();
}
Aggregations