Search in sources :

Example 1 with ChoiceHead

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

the class ParserTest method parseChoiceRule.

@Test
public void parseChoiceRule() {
    ASPCore2Program parsedProgram = parser.parse("dom(1). dom(2). { a ; b } :- dom(X).");
    ChoiceHead choiceHead = (ChoiceHead) parsedProgram.getRules().get(0).getHead();
    assertEquals(2, choiceHead.getChoiceElements().size());
    assertTrue(choiceHead.getChoiceElements().get(0).getChoiceAtom().toString().equals("a"));
    assertTrue(choiceHead.getChoiceElements().get(1).getChoiceAtom().toString().equals("b"));
    assertEquals(null, choiceHead.getLowerBound());
    assertEquals(null, choiceHead.getUpperBound());
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) ChoiceHead(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead) Test(org.junit.jupiter.api.Test)

Example 2 with ChoiceHead

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

the class ParserTest method parseChoiceRuleBounded.

@Test
public void parseChoiceRuleBounded() {
    ASPCore2Program parsedProgram = parser.parse("dom(1). dom(2). 1 < { a: p(v,w), not r; b } <= 13 :- dom(X). foo.");
    ChoiceHead choiceHead = (ChoiceHead) parsedProgram.getRules().get(0).getHead();
    assertEquals(2, choiceHead.getChoiceElements().size());
    assertTrue(choiceHead.getChoiceElements().get(0).getChoiceAtom().toString().equals("a"));
    assertTrue(choiceHead.getChoiceElements().get(1).getChoiceAtom().toString().equals("b"));
    List<Literal> conditionalLiterals = choiceHead.getChoiceElements().get(0).getConditionLiterals();
    assertEquals(2, conditionalLiterals.size());
    assertFalse(conditionalLiterals.get(0).isNegated());
    assertTrue(conditionalLiterals.get(1).isNegated());
    assertEquals(Terms.newConstant(1), choiceHead.getLowerBound());
    assertEquals(ComparisonOperators.LT, choiceHead.getLowerOperator());
    assertEquals(Terms.newConstant(13), choiceHead.getUpperBound());
    assertEquals(ComparisonOperators.LE, choiceHead.getUpperOperator());
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) ChoiceHead(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Test(org.junit.jupiter.api.Test)

Example 3 with ChoiceHead

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

the class ChoiceHeadToNormal method apply.

@Override
public ASPCore2Program apply(ASPCore2Program inputProgram) {
    InputProgram.Builder programBuilder = InputProgram.builder();
    List<Rule<Head>> additionalRules = new ArrayList<>();
    List<Rule<Head>> srcRules = new ArrayList<>(inputProgram.getRules());
    Iterator<Rule<Head>> ruleIterator = srcRules.iterator();
    while (ruleIterator.hasNext()) {
        Rule<Head> rule = ruleIterator.next();
        Head ruleHead = rule.getHead();
        if (!(ruleHead instanceof ChoiceHead)) {
            // Rule is constraint or without choice in the head. Leave as is.
            continue;
        }
        // Remove this rule, as it will be transformed.
        ruleIterator.remove();
        ChoiceHead choiceHead = (ChoiceHead) ruleHead;
        // Choice rules with boundaries are not yet supported.
        if (choiceHead.getLowerBound() != null || choiceHead.getUpperBound() != null) {
            throw new UnsupportedOperationException("Found choice rule with bounds, which are not yet supported. Rule is: " + rule);
        }
        // Only rewrite rules with a choice in their head.
        for (ChoiceElement choiceElement : choiceHead.getChoiceElements()) {
            // Create two guessing rules for each choiceElement.
            // Construct common body to both rules.
            BasicAtom head = choiceElement.getChoiceAtom();
            List<Literal> ruleBody = new ArrayList<>(rule.getBody());
            ruleBody.addAll(choiceElement.getConditionLiterals());
            if (containsIntervalTerms(head)) {
                throw new RuntimeException("Program contains a choice rule with interval terms in its head. This is not supported (yet).");
            }
            // Construct head atom for the choice.
            Predicate headPredicate = head.getPredicate();
            Predicate negPredicate = Predicates.getPredicate(PREDICATE_NEGATION_PREFIX + headPredicate.getName(), headPredicate.getArity() + 1, true);
            List<Term> headTerms = new ArrayList<>(head.getTerms());
            // FIXME: when introducing classical negation, this is 1 for classical positive atoms and 0 for
            headTerms.add(0, Terms.newConstant("1"));
            // classical negative atoms.
            BasicAtom negHead = Atoms.newBasicAtom(negPredicate, headTerms);
            // Construct two guessing rules.
            List<Literal> guessingRuleBodyWithNegHead = new ArrayList<>(ruleBody);
            guessingRuleBodyWithNegHead.add(Atoms.newBasicAtom(head.getPredicate(), head.getTerms()).toLiteral(false));
            additionalRules.add(new BasicRule(Heads.newNormalHead(negHead), guessingRuleBodyWithNegHead));
            List<Literal> guessingRuleBodyWithHead = new ArrayList<>(ruleBody);
            guessingRuleBodyWithHead.add(Atoms.newBasicAtom(negPredicate, headTerms).toLiteral(false));
            additionalRules.add(new BasicRule(Heads.newNormalHead(head), guessingRuleBodyWithHead));
        // TODO: when cardinality constraints are possible, process the boundaries by adding a constraint with a cardinality check.
        }
    }
    return programBuilder.addRules(srcRules).addRules(additionalRules).addFacts(inputProgram.getFacts()).addInlineDirectives(inputProgram.getInlineDirectives()).build();
}
Also used : BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) ChoiceHead(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) ArrayList(java.util.ArrayList) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) ChoiceElement(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead.ChoiceElement) ChoiceHead(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Aggregations

ChoiceHead (at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead)3 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)2 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)2 Test (org.junit.jupiter.api.Test)2 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)1 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)1 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)1 Rule (at.ac.tuwien.kr.alpha.api.rules.Rule)1 ChoiceElement (at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead.ChoiceElement)1 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)1 Term (at.ac.tuwien.kr.alpha.api.terms.Term)1 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)1 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)1 BasicRule (at.ac.tuwien.kr.alpha.core.rules.BasicRule)1 ArrayList (java.util.ArrayList)1