Search in sources :

Example 1 with Atom

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);
}
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 2 with Atom

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));
}
Also used : CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) 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)

Example 3 with Atom

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));
}
Also used : CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Example 4 with Atom

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;
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) IntervalAtom(at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom)

Example 5 with Atom

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();
}
Also used : Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

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