Search in sources :

Example 1 with Head

use of at.ac.tuwien.kr.alpha.api.rules.heads.Head 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)

Example 2 with Head

use of at.ac.tuwien.kr.alpha.api.rules.heads.Head in project Alpha by alpha-asp.

the class AggregateRewriting method rewriteRulesWithAggregates.

/**
 * Transforms (restricted) aggregate literals of format "VAR OP #AGG_FN{...}" into literals of format
 * "<result_predicate>(ARGS, VAR)" where ARGS is a function term wrapping the aggregate's global variables.
 *
 * @param ctx the {@link AggregateRewritingContext} containing information about all aggregates.
 * @return for each rule, its rewritten version where aggregates are replaced with output atoms of the encoding.
 */
private static List<Rule<Head>> rewriteRulesWithAggregates(AggregateRewritingContext ctx) {
    List<Rule<Head>> rewrittenRules = new ArrayList<>();
    for (Rule<Head> rule : ctx.getRulesWithAggregates()) {
        List<Literal> rewrittenBody = new ArrayList<>();
        for (Literal lit : rule.getBody()) {
            if (lit instanceof AggregateLiteral) {
                AggregateInfo aggregateInfo = ctx.getAggregateInfo((AggregateLiteral) lit);
                rewrittenBody.add(Literals.fromAtom(aggregateInfo.getOutputAtom(), !lit.isNegated()));
            } else {
                rewrittenBody.add(lit);
            }
        }
        rewrittenRules.add(new BasicRule(rule.getHead(), rewrittenBody));
    }
    return rewrittenRules;
}
Also used : BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) 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) ArrayList(java.util.ArrayList) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) AggregateInfo(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo)

Example 3 with Head

use of at.ac.tuwien.kr.alpha.api.rules.heads.Head in project Alpha by alpha-asp.

the class SubstitutionTest method substitutionFromString.

@Test
public void substitutionFromString() {
    Rule<Head> rule = PARSER.parse("x :- p(X,Y), not q(X,Y).").getRules().get(0);
    CompiledRule nonGroundRule = InternalRule.fromNormalRule(NormalRuleImpl.fromBasicRule(rule));
    Substitution substitution1 = BasicSubstitution.specializeSubstitution(PX, PA, BasicSubstitution.EMPTY_SUBSTITUTION);
    Substitution substitution = BasicSubstitution.specializeSubstitution(PY, PB, substitution1);
    RuleAtom ruleAtom = new RuleAtom(nonGroundRule, substitution);
    String substitutionString = (String) ((ConstantTerm<?>) ruleAtom.getTerms().get(1)).getObject();
    Substitution fromString = Substitutions.fromString(substitutionString);
    assertEquals(substitution, fromString);
}
Also used : Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom) Test(org.junit.jupiter.api.Test)

Example 4 with Head

use of at.ac.tuwien.kr.alpha.api.rules.heads.Head in project Alpha by alpha-asp.

the class LiteralBindingNonBindingVariablesTest method testPositiveExternalLiteral.

@Test
public void testPositiveExternalLiteral() {
    externals.put("ext", new IntPredicateInterpretation(i -> i > 0));
    Rule<Head> rule = parser.parse("p(X) :- q(Y), &ext[Y](X).", externals).getRules().get(0);
    Literal literal = rule.getBody().stream().filter((lit) -> lit.getPredicate().getName().equals("ext")).findFirst().get();
    assertEquals(false, literal.isNegated());
    expectVariables(literal.getBindingVariables(), "X");
    expectVariables(literal.getNonBindingVariables(), "Y");
}
Also used : Arrays(java.util.Arrays) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) Collection(java.util.Collection) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Set(java.util.Set) HashMap(java.util.HashMap) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Disabled(org.junit.jupiter.api.Disabled) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) IntPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.IntPredicateInterpretation) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ComparisonOperators(at.ac.tuwien.kr.alpha.commons.comparisons.ComparisonOperators) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) IntPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.IntPredicateInterpretation) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Test(org.junit.jupiter.api.Test)

Example 5 with Head

use of at.ac.tuwien.kr.alpha.api.rules.heads.Head in project Alpha by alpha-asp.

the class RuleTest method testRulesEqual.

@Test
public void testRulesEqual() {
    ASPCore2Program p1 = parser.parse("p(X, Y) :- bla(X), blub(Y), foo(X, Y), not bar(X).");
    Rule<Head> r1 = p1.getRules().get(0);
    ASPCore2Program p2 = parser.parse("p(X, Y) :- bla(X), blub(Y), foo(X, Y), not bar(X).");
    Rule<Head> r2 = p2.getRules().get(0);
    ASPCore2Program p3 = parser.parse("p(X, Y) :- bla(X), blub(X), foo(X, X), not bar(X).");
    Rule<Head> r3 = p3.getRules().get(0);
    assertTrue(r1.equals(r2));
    assertTrue(r2.equals(r1));
    assertTrue(r1.hashCode() == r2.hashCode());
    assertFalse(r1.equals(r3));
    assertFalse(r3.equals(r1));
    assertTrue(r1.hashCode() != r3.hashCode());
    assertFalse(r2.equals(r3));
    assertFalse(r3.equals(r2));
    assertTrue(r2.hashCode() != r3.hashCode());
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) Test(org.junit.jupiter.api.Test)

Aggregations

Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)21 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)12 Test (org.junit.jupiter.api.Test)12 Rule (at.ac.tuwien.kr.alpha.api.rules.Rule)6 BasicRule (at.ac.tuwien.kr.alpha.core.rules.BasicRule)6 ArrayList (java.util.ArrayList)6 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)5 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)4 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)3 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)3 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)2 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)2 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)2 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)2 BasicLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral)2 Term (at.ac.tuwien.kr.alpha.api.terms.Term)2 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)2 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)2 InlineDirectivesImpl (at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl)2 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)2