Search in sources :

Example 11 with Theory

use of com.sri.ai.grinder.sgdpllt.api.Theory in project aic-expresso by aic-sri-international.

the class CompoundTheoryTestingSupport method setVariableNamesAndTypesForTesting.

/**
	 * This is overridden so that given variables and types for testing are distributed to their
	 * respective theories according to {@link #isSuitableFor(Expression, Type)}.
	 */
@Override
public void setVariableNamesAndTypesForTesting(Map<String, Type> variableNamesAndTypesForTesting) {
    // First ensure the compound set of variables names and type information is setup correctly
    super.setVariableNamesAndTypesForTesting(variableNamesAndTypesForTesting);
    // Then update the sub-theories so that they share appropriate subsets of this information
    Map<Theory, Map<String, Type>> mapForSubTheory = map();
    for (Theory subTheory : getTheory().getSubTheories()) {
        mapForSubTheory.put(subTheory, map());
    }
    for (Map.Entry<String, Type> variableNameAndType : getVariableNamesAndTypesForTesting().entrySet()) {
        String variableName = variableNameAndType.getKey();
        Expression variable = Expressions.parse(variableName);
        Type type = variableNameAndType.getValue();
        for (Theory subTheory : getTheory().getSubTheories()) {
            if (subTheory.isSuitableFor(variable, type) || (type instanceof FunctionType && subTheory.isSuitableFor(variable, ((FunctionType) type).getCodomain()))) {
                mapForSubTheory.get(subTheory).put(variableName, type);
            }
        }
    }
    for (Map.Entry<Theory, TheoryTestingSupport> entry : getTheoryToTestingSupport().entrySet()) {
        Map<String, Type> forThisSubTheory = mapForSubTheory.get(entry.getKey());
        entry.getValue().setVariableNamesAndTypesForTesting(forThisSubTheory);
    }
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Expression(com.sri.ai.expresso.api.Expression) AbstractTheoryTestingSupport(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) FunctionType(com.sri.ai.expresso.type.FunctionType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 12 with Theory

use of com.sri.ai.grinder.sgdpllt.api.Theory in project aic-expresso by aic-sri-international.

the class CompoundTheoryTestingSupport method getTheoryTestingSupport.

private TheoryTestingSupport getTheoryTestingSupport(String variable) {
    Type variableType = getTestingVariableType(variable);
    Theory subConstraintTheory = getTheory().getTheory(parse(variable), variableType);
    check(() -> subConstraintTheory != null, () -> "There is no sub-theory suitable for " + variable + ", which has type " + variableType);
    TheoryTestingSupport result = getTheoryToTestingSupport().get(subConstraintTheory);
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) AbstractTheoryTestingSupport(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)

Example 13 with Theory

use of com.sri.ai.grinder.sgdpllt.api.Theory in project aic-expresso by aic-sri-international.

the class SGDPLLTTester method testModelCountingForSingleVariableConstraints.

/**
	 * Given a theory and a number <code>n</code> of single-variable constraint tests,
	 * generates <code>n</code> formulas in the theory
	 * and see if the model counting solver works (checked by brute force).
	 * Throws an {@link Error} with the failure description if a test fails.
	 * @param theoryTestingSupport
	 * @param numberOfTests
	 * @param maxNumberOfLiterals
	 * @param outputCount
	 */
public static void testModelCountingForSingleVariableConstraints(boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) {
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Expression variable = parse(theoryTestingSupport.pickTestingVariableAtRandom());
    NullaryFunction<Constraint> makeInitialConstraint = () -> theoryTestingSupport.getTheory().makeSingleVariableConstraint(variable, theoryTestingSupport.getTheory(), context);
    Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteralOn(((SingleVariableConstraint) c).getVariable().toString(), context);
    TestRunner tester = (ls, c, tB, cT, p) -> runModelCountingTestForSingleVariableConstraint(variable, ls, c, tB, cT.getTheory(), p);
    runTesterGivenOnSuccessiveConjunctionsOfLiterals("model counting", tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
Also used : CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) BinaryFunction(com.sri.ai.util.base.BinaryFunction) Util.removeFromSetNonDestructively(com.sri.ai.util.Util.removeFromSetNonDestructively) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) Util.in(com.sri.ai.util.Util.in) ThereExists(com.sri.ai.grinder.sgdpllt.library.boole.ThereExists) AbstractIterativeMultiIndexQuantifierElimination(com.sri.ai.grinder.sgdpllt.interpreter.AbstractIterativeMultiIndexQuantifierElimination) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) And(com.sri.ai.grinder.sgdpllt.library.boole.And) DefaultMultiVariableConstraint(com.sri.ai.grinder.sgdpllt.core.constraint.DefaultMultiVariableConstraint) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) LinkedHashSet(java.util.LinkedHashSet) Sets(com.sri.ai.grinder.sgdpllt.library.set.Sets) Type(com.sri.ai.expresso.api.Type) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) Expressions.getVariableReferences(com.sri.ai.expresso.helper.Expressions.getVariableReferences) Context(com.sri.ai.grinder.sgdpllt.api.Context) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Beta(com.google.common.annotations.Beta) IndexExpressions(com.sri.ai.grinder.sgdpllt.library.indexexpression.IndexExpressions) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Util(com.sri.ai.util.Util) AssociativeCommutativeGroup(com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeGroup) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) BruteForceCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) DefaultMultiVariableConstraint(com.sri.ai.grinder.sgdpllt.core.constraint.DefaultMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)

Example 14 with Theory

use of com.sri.ai.grinder.sgdpllt.api.Theory in project aic-expresso by aic-sri-international.

the class CompoundTheory method getSingleVariableConstraintQuantifierEliminatorStepSolver.

@Override
public ExpressionLiteralSplitterStepSolver getSingleVariableConstraintQuantifierEliminatorStepSolver(AssociativeCommutativeGroup group, SingleVariableConstraint constraint, Expression body, Context context) {
    Theory theory = getTheory(constraint.getVariable(), context);
    ExpressionLiteralSplitterStepSolver result;
    if (theory != null) {
        result = theory.getSingleVariableConstraintQuantifierEliminatorStepSolver(group, constraint, body, context);
    } else {
        result = null;
    }
    return result;
}
Also used : Theory(com.sri.ai.grinder.sgdpllt.api.Theory) AbstractTheory(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheory) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)

Example 15 with Theory

use of com.sri.ai.grinder.sgdpllt.api.Theory in project aic-expresso by aic-sri-international.

the class CompoundTheory method getAtomNegation.

@Override
public Expression getAtomNegation(Expression atom, Context context) {
    Theory theory = getFirstSatisfyingPredicateOrNull(getSubTheories(), t -> t.isLiteralOrBooleanConstant(atom, context));
    Expression result;
    if (theory == null) {
        // this covers cases in which the theory has testing literals, but not constraint literals
        result = Not.make(atom);
    } else {
        result = theory.getAtomNegation(atom, context);
    }
    return result;
}
Also used : Theory(com.sri.ai.grinder.sgdpllt.api.Theory) AbstractTheory(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheory) Expression(com.sri.ai.expresso.api.Expression)

Aggregations

Theory (com.sri.ai.grinder.sgdpllt.api.Theory)29 Expression (com.sri.ai.expresso.api.Expression)21 Context (com.sri.ai.grinder.sgdpllt.api.Context)13 Type (com.sri.ai.expresso.api.Type)10 Map (java.util.Map)9 Beta (com.google.common.annotations.Beta)8 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)8 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)8 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)8 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)8 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)8 Collection (java.util.Collection)8 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)7 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)7 AssociativeCommutativeGroup (com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeGroup)7 And (com.sri.ai.grinder.sgdpllt.library.boole.And)7 Util (com.sri.ai.util.Util)7 Util.join (com.sri.ai.util.Util.join)7 Util.list (com.sri.ai.util.Util.list)7 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)6