Search in sources :

Example 56 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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)

Example 57 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.

the class EnumerationRewriting method apply.

@Override
public ASPCore2Program apply(ASPCore2Program inputProgram) {
    // Read enumeration predicate from directive.
    String enumDirective = inputProgram.getInlineDirectives().getDirectiveValue(InlineDirectivesImpl.DIRECTIVE.enum_predicate_is);
    if (enumDirective == null) {
        // Directive not set, nothing to rewrite.
        return inputProgram;
    }
    Predicate enumPredicate = Predicates.getPredicate(enumDirective, 3);
    InputProgram.Builder programBuilder = InputProgram.builder().addInlineDirectives(inputProgram.getInlineDirectives());
    checkFactsAreEnumerationFree(inputProgram.getFacts(), enumPredicate);
    programBuilder.addFacts(inputProgram.getFacts());
    List<Rule<Head>> srcRules = new ArrayList<>(inputProgram.getRules());
    programBuilder.addRules(rewriteRules(srcRules, enumPredicate));
    return programBuilder.build();
}
Also used : ArrayList(java.util.ArrayList) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Example 58 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.

the class AnalyzeUnjustified method rulesHeadUnifyingWith.

private List<RuleAndUnifier> rulesHeadUnifyingWith(Atom p) {
    List<RuleAndUnifier> rulesWithUnifier = new ArrayList<>();
    Predicate predicate = p.getPredicate();
    ArrayList<FactOrNonGroundRule> definingRulesAndFacts = new ArrayList<>();
    // Get facts over the same predicate.
    LinkedHashSet<Instance> factInstances = factsFromProgram.get(predicate);
    if (factInstances != null) {
        for (Instance factInstance : factInstances) {
            definingRulesAndFacts.add(new FactOrNonGroundRule(factInstance));
        }
    }
    HashSet<CompiledRule> rulesDefiningPredicate = programAnalysis.getPredicateDefiningRules().get(predicate);
    if (rulesDefiningPredicate != null) {
        for (CompiledRule nonGroundRule : rulesDefiningPredicate) {
            definingRulesAndFacts.add(new FactOrNonGroundRule(nonGroundRule));
        }
    }
    for (FactOrNonGroundRule factOrNonGroundRule : definingRulesAndFacts) {
        boolean isNonGroundRule = factOrNonGroundRule.nonGroundRule != null;
        Set<Literal> renamedBody;
        Atom headAtom;
        if (isNonGroundRule) {
            // First rename all variables in the rule.
            CompiledRule rule = factOrNonGroundRule.nonGroundRule.renameVariables("_" + renamingCounter++);
            renamedBody = rule.getBody();
            headAtom = rule.getHeadAtom();
        } else {
            // Create atom and empty rule body out of instance.
            headAtom = Atoms.newBasicAtom(p.getPredicate(), factOrNonGroundRule.factInstance.terms);
            renamedBody = Collections.emptySet();
        }
        // Unify rule head with literal to justify.
        Unifier unifier = Unification.unifyAtoms(p, headAtom);
        // Skip if unification failed.
        if (unifier == null) {
            continue;
        }
        rulesWithUnifier.add(new RuleAndUnifier(renamedBody, unifier, headAtom));
    }
    return rulesWithUnifier;
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) ArrayList(java.util.ArrayList) 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) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

Example 59 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.

the class ParseTreeVisitor method visitAnswer_set.

@Override
public AnswerSet visitAnswer_set(ASPCore2Parser.Answer_setContext ctx) {
    SortedSet<Predicate> predicates = new TreeSet<>();
    Map<Predicate, SortedSet<Atom>> predicateInstances = new TreeMap<>();
    for (ASPCore2Parser.Classical_literalContext classicalLiteralContext : ctx.classical_literal()) {
        Atom atom = visitClassical_literal(classicalLiteralContext);
        predicates.add(atom.getPredicate());
        predicateInstances.compute(atom.getPredicate(), (k, v) -> {
            if (v == null) {
                v = new TreeSet<>();
            }
            v.add(atom);
            return v;
        });
    }
    return AnswerSets.newAnswerSet(predicates, predicateInstances);
}
Also used : TreeSet(java.util.TreeSet) TreeMap(java.util.TreeMap) SortedSet(java.util.SortedSet) ASPCore2Parser(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 60 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.

the class NaiveGrounder method initializeFactsAndRules.

private void initializeFactsAndRules() {
    // Initialize all facts.
    for (Atom fact : program.getFacts()) {
        final Predicate predicate = fact.getPredicate();
        // Record predicate
        workingMemory.initialize(predicate);
    }
    // Register internal atoms.
    workingMemory.initialize(RuleAtom.PREDICATE);
    workingMemory.initialize(ChoiceAtom.OFF);
    workingMemory.initialize(ChoiceAtom.ON);
    // Initialize rules and constraints in working memory.
    for (CompiledRule nonGroundRule : program.getRulesById().values()) {
        // Create working memories for all predicates occurring in the rule.
        for (Predicate predicate : nonGroundRule.getOccurringPredicates()) {
            // FIXME: this also contains interval/builtin predicates that are not needed.
            workingMemory.initialize(predicate);
        }
        // If the rule has fixed ground instantiations, it is not registered but grounded once like facts.
        if (nonGroundRule.getGroundingInfo().hasFixedInstantiation()) {
            fixedRules.add(nonGroundRule);
            continue;
        }
        // Register each starting literal at the corresponding working memory.
        for (Literal literal : nonGroundRule.getGroundingInfo().getStartingLiterals()) {
            registerLiteralAtWorkingMemory(literal, nonGroundRule);
        }
    }
}
Also used : CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ChoiceAtom(at.ac.tuwien.kr.alpha.core.atoms.ChoiceAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Aggregations

Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)69 Test (org.junit.jupiter.api.Test)37 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)27 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)19 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)15 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)14 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)13 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)13 LinkedHashSet (java.util.LinkedHashSet)13 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)11 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)10 ArrayList (java.util.ArrayList)10 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)9 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)8 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)8 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)8 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)8 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)8 HashMap (java.util.HashMap)8