Search in sources :

Example 16 with Literal

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

the class ArithmeticTermsRewritingTest method rewriteExternalAtom.

@Test
public void rewriteExternalAtom() {
    NormalProgram inputProgram = NormalProgramImpl.fromInputProgram(parser.parse("p :- Y = 13, &extArithTest[Y*5](Y-4)."));
    assertEquals(1, inputProgram.getRules().size());
    ArithmeticTermsRewriting arithmeticTermsRewriting = new ArithmeticTermsRewriting();
    NormalProgram rewrittenProgram = arithmeticTermsRewriting.apply(inputProgram);
    assertEquals(1, rewrittenProgram.getRules().size());
    NormalRule rewrittenRule = rewrittenProgram.getRules().get(0);
    assertEquals(4, rewrittenRule.getBody().size());
    List<Literal> externalLiterals = rewrittenRule.getBody().stream().filter(lit -> lit instanceof ExternalLiteral).collect(toList());
    assertEquals(1, externalLiterals.size());
    ExternalAtom rewrittenExternal = ((ExternalLiteral) externalLiterals.get(0)).getAtom();
    assertEquals(1, rewrittenExternal.getInput().size());
    assertTrue(rewrittenExternal.getInput().get(0) instanceof VariableTerm);
    assertEquals(1, rewrittenExternal.getOutput().size());
    assertTrue(rewrittenExternal.getOutput().get(0) instanceof VariableTerm);
}
Also used : NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) Set(java.util.Set) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Predicate(at.ac.tuwien.kr.alpha.api.externals.Predicate) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Externals(at.ac.tuwien.kr.alpha.core.externals.Externals) Test(org.junit.jupiter.api.Test) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) NormalProgramImpl(at.ac.tuwien.kr.alpha.core.programs.NormalProgramImpl) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Test(org.junit.jupiter.api.Test)

Example 17 with Literal

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

the class NaiveGrounderTest method testPermissiveGrounderHeuristicTolerance.

/**
 * Tests if {@link NaiveGrounder#getGroundInstantiations(InternalRule, RuleGroundingOrder, Substitution, Assignment)}
 * produces ground instantiations for the rule with ID {@code ruleID} in {@code program} when {@code startingLiteral}
 * unified with the numeric instance {@code startingInstance} is used as starting literal and the following
 * additional conditions are established:
 * <ul>
 * <li>The atoms {@code b([startingInstance], 1), ..., b([startingInstance], n)} are added to the grounder's
 * working memory without changing the assignment, where {@code arityOfB-1} occurences of {@code startingInstance}
 * are used instead of {@code [startingInstance]} and {@code n} is the length of the {@code truthsOfB} array.
 * For example, if the length of {@code truthsOfB} is 2 and {@code arityOfB} is also 2, these atoms are
 * {@code b(1,1), b(1,2)}.
 * </li>
 * <li>The same atoms are assigned the truth values in the {@code truthsOfB} array.</li>
 * </ul>
 * It is asserted that ground instantiations are produced if and only if {@code expectNoGoods} is true.
 * If ground instantiations are produced, it is also asserted that the numbers of unassigned positive body atoms
 * determined by {@code getGroundInstantiations} match those given in {@code expectedNumbersOfUnassignedPositiveBodyAtoms}.
 */
private void testPermissiveGrounderHeuristicTolerance(ASPCore2Program program, int ruleID, Literal startingLiteral, int startingInstance, int tolerance, ThriceTruth[] truthsOfB, int arityOfB, boolean expectNoGoods, List<Integer> expectedNumbersOfUnassignedPositiveBodyAtoms) {
    CompiledProgram internalPrg = InternalProgram.fromNormalProgram(NORMALIZE_TRANSFORM.apply(program));
    AtomStore atomStore = new AtomStoreImpl();
    TrailAssignment currentAssignment = new TrailAssignment(atomStore);
    GrounderHeuristicsConfiguration heuristicConfiguration = GrounderHeuristicsConfiguration.getInstance(tolerance, tolerance);
    NaiveGrounder grounder = (NaiveGrounder) GrounderFactory.getInstance("naive", internalPrg, atomStore, p -> true, heuristicConfiguration, true);
    int[] bAtomIDs = new int[truthsOfB.length];
    for (int i = 0; i < truthsOfB.length; i++) {
        int[] bTerms = new int[arityOfB];
        for (int n = 0; n < arityOfB; n++) {
            bTerms[n] = (n == arityOfB - 1) ? i + 1 : startingInstance;
        }
        bAtomIDs[i] = atomStore.putIfAbsent(atom("b", bTerms));
    }
    addAtomsToWorkingMemoryWithoutChangingTheAssignment(atomStore, grounder, bAtomIDs);
    assign(currentAssignment, bAtomIDs, truthsOfB);
    grounder.bootstrap();
    final CompiledRule nonGroundRule = grounder.getNonGroundRule(ruleID);
    final Substitution substStartingLiteral = BasicSubstitution.specializeSubstitution(startingLiteral, new Instance(Terms.newConstant(startingInstance)), BasicSubstitution.EMPTY_SUBSTITUTION);
    final BindingResult bindingResult = grounder.getGroundInstantiations(nonGroundRule, nonGroundRule.getGroundingInfo().orderStartingFrom(startingLiteral), substStartingLiteral, currentAssignment);
    assertEquals(expectNoGoods, bindingResult.size() > 0);
    if (bindingResult.size() > 0) {
        assertEquals(expectedNumbersOfUnassignedPositiveBodyAtoms, bindingResult.getNumbersOfUnassignedPositiveBodyAtoms());
    } else {
        assertTrue(bindingResult.getNumbersOfUnassignedPositiveBodyAtoms().isEmpty());
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) BeforeEach(org.junit.jupiter.api.BeforeEach) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) Arrays(java.util.Arrays) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) ThriceTruth(at.ac.tuwien.kr.alpha.core.solver.ThriceTruth) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) Disabled(org.junit.jupiter.api.Disabled) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Map(java.util.Map) BindingResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.BindingResult) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Literals(at.ac.tuwien.kr.alpha.core.atoms.Literals) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) NormalizeProgramTransformation(at.ac.tuwien.kr.alpha.core.programs.transformation.NormalizeProgramTransformation) Collection(java.util.Collection) GrounderHeuristicsConfiguration(at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation) ProgramPartParser(at.ac.tuwien.kr.alpha.core.parser.ProgramPartParser) TestUtils.atom(at.ac.tuwien.kr.alpha.core.test.util.TestUtils.atom) Test(org.junit.jupiter.api.Test) List(java.util.List) Assignment(at.ac.tuwien.kr.alpha.core.common.Assignment) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SystemConfig(at.ac.tuwien.kr.alpha.api.config.SystemConfig) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) InternalRule(at.ac.tuwien.kr.alpha.core.rules.InternalRule) Collections(java.util.Collections) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) BindingResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.BindingResult) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) GrounderHeuristicsConfiguration(at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule)

Example 18 with Literal

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

the class RuleGroundingOrderTest method testPositionFromWhichAllVarsAreBound_simpleNonGround.

@Test
public void testPositionFromWhichAllVarsAreBound_simpleNonGround() {
    String aspStr = "a(X) :- b(X), not c(X).";
    CompiledProgram internalPrg = PARSE_AND_PREPROCESS.apply(aspStr);
    RuleGroundingInfo rgo0 = computeGroundingOrdersForRule(internalPrg, 0);
    assertEquals(1, rgo0.getStartingLiterals().size());
    for (Literal startingLiteral : rgo0.getStartingLiterals()) {
        assertEquals(0, rgo0.orderStartingFrom(startingLiteral).getPositionFromWhichAllVarsAreBound());
    }
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 19 with Literal

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

the class RuleGroundingOrderTest method testPositionFromWhichAllVarsAreBound_joinedNonGround.

@Test
public void testPositionFromWhichAllVarsAreBound_joinedNonGround() {
    String aspStr = "a(X) :- b(X), c(X,Y), d(X,Z), not e(X).";
    CompiledProgram internalPrg = PARSE_AND_PREPROCESS.apply(aspStr);
    RuleGroundingInfo rgo0 = computeGroundingOrdersForRule(internalPrg, 0);
    final Literal litBX = PROGRAM_PART_PARSER.parseLiteral("b(X)");
    final Literal litCXY = PROGRAM_PART_PARSER.parseLiteral("c(X,Y)");
    final Literal litDXZ = PROGRAM_PART_PARSER.parseLiteral("d(X,Z)");
    assertTrue(2 <= rgo0.orderStartingFrom(litBX).getPositionFromWhichAllVarsAreBound());
    assertTrue(1 <= rgo0.orderStartingFrom(litCXY).getPositionFromWhichAllVarsAreBound());
    assertTrue(1 <= rgo0.orderStartingFrom(litDXZ).getPositionFromWhichAllVarsAreBound());
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 20 with Literal

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

the class AggregateRewritingContext method registerRule.

/**
 * Registers a rule that potentially contains one or more {@link AggregateLiteral}s with this context.
 * In case aggregates are found in the rule, global variables and dependencies of the aggregate are calculated and the
 * aggregate literal is stored in the rewriting context along with a reference to the rule it occurs in
 *
 * @param rule
 * @return true if the given rule contains one or more aggregate literals, false otherwise
 */
public boolean registerRule(Rule<Head> rule) {
    AggregateRewritingRuleAnalysis ruleAnalysis = AggregateRewritingRuleAnalysis.analyzeRuleDependencies(rule);
    if (ruleAnalysis.aggregatesInRule.isEmpty()) {
        // Rule has no aggregates.
        return false;
    }
    // Do initial registration of each aggregate literal and keep the ids.
    for (Map.Entry<AggregateLiteral, Set<VariableTerm>> entry : ruleAnalysis.globalVariablesPerAggregate.entrySet()) {
        registerAggregateLiteral(entry.getKey(), entry.getValue());
    }
    // Now go through dependencies and replace the actual aggregate literals with their rewritten versions
    for (Map.Entry<AggregateLiteral, Set<Literal>> entry : ruleAnalysis.dependenciesPerAggregate.entrySet()) {
        AggregateInfo aggregateInfo = getAggregateInfo(entry.getKey());
        for (Literal dependency : entry.getValue()) {
            if (dependency instanceof AggregateLiteral) {
                AggregateInfo dependencyInfo = getAggregateInfo((AggregateLiteral) dependency);
                aggregateInfo.addDependency(dependencyInfo.getOutputAtom().toLiteral(!dependency.isNegated()));
            } else {
                aggregateInfo.addDependency(dependency);
            }
        }
    }
    rulesWithAggregates.add(rule);
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) 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) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)82 Test (org.junit.jupiter.api.Test)42 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)20 ArrayList (java.util.ArrayList)20 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)17 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)16 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)14 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)14 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)14 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)13 Map (java.util.Map)12 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)11 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)11 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)10 HashSet (java.util.HashSet)10 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)9 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)9 LinkedHashSet (java.util.LinkedHashSet)8 List (java.util.List)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)7