Search in sources :

Example 46 with Context

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

the class MultiVariableContextWithCheckedProperty method conjoin.

@Override
public Context conjoin(Expression formula, Context context) {
    Context result;
    Pair<Boolean, Context> specializedResult = conjoinSpecializedForConstraintsIfApplicable(formula, context);
    if (specializedResult.first) {
        result = specializedResult.second;
    } else {
        // fall back to default implementation
        result = Context.super.conjoin(formula, context);
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context)

Example 47 with Context

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

the class MultiVariableContextWithCheckedProperty method conjoinWithLiteral.

@Override
public Context conjoinWithLiteral(Expression literal, Context context) {
    Context result;
    if (literal.equals(TRUE)) {
        result = this;
    } else if (literal.equals(FALSE)) {
        result = makeContradiction();
    } else {
        Collection<Expression> variablesInLiteral = getTheory().getVariablesIn(literal, context);
        if (variablesInLiteral.isEmpty()) {
            Expression literalSimplifiedToConstant = getTheory().simplify(literal, context);
            if (literalSimplifiedToConstant == literal) {
                throw new Error("Literal " + literal + " should have been simplified to a boolean constant, but was not. Sometimes this is caused by using a symbol as a variable, but which has not been declared as a variable in the context, or has been declared as a uniquely named constant in the Context (for example by constructing the Context with the default PrologConstantPredicate as a default predicate for recognizing constants, which recognizes all non-capitalized identifiers as such)");
            }
            result = conjoinWithLiteral(literalSimplifiedToConstant, context);
        } else if (head != null) {
            SingleVariableConstraint newHead;
            Context newTail;
            if (variablesInLiteral.contains(head.getVariable())) {
                newHead = head.conjoin(literal, context);
                newTail = tail;
            } else {
                newHead = head;
                newTail = tail.conjoin(literal, context);
            }
            // up the chain so they are integrated and simplified in the corresponding single-variable constraints
            if (!newHead.isContradiction()) {
                for (Expression externalLiteral : newHead.getExternalLiterals()) {
                    if (!newTail.isContradiction()) {
                        newTail = newTail.conjoin(externalLiteral, context);
                    }
                }
                newHead = newHead.makeSimplificationWithoutExternalLiterals();
            }
            if (newHead == head && newTail == tail) {
                // in case nothing changed
                result = this;
            } else {
                result = makeAndCheck(getTheory(), newHead, newTail, contextDependentProblemStepSolverMaker, context);
            }
        } else {
            Expression firstVariable = getFirstOrNull(variablesInLiteral);
            SingleVariableConstraint newSingleVariableConstraint = getTheory().makeSingleVariableConstraint(firstVariable, getTheory(), context);
            newSingleVariableConstraint = newSingleVariableConstraint.conjoin(literal, context);
            result = makeAndCheck(getTheory(), newSingleVariableConstraint, this, contextDependentProblemStepSolverMaker, context);
        }
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) Expression(com.sri.ai.expresso.api.Expression) Collection(java.util.Collection)

Example 48 with Context

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

the class MultiVariableContextWithCheckedProperty method registerAdditionalSymbolsAndTypes.

@Override
public MultiVariableContextWithCheckedProperty registerAdditionalSymbolsAndTypes(Map<Expression, Expression> indicesAndTypes) {
    MultiVariableContextWithCheckedProperty result = clone();
    Context newTail = tail.registerAdditionalSymbolsAndTypes(indicesAndTypes);
    result.tail = newTail;
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context)

Example 49 with Context

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

the class AbstractTheoryTestingSupport method extendWithTestingInformation.

@Override
public Context extendWithTestingInformation(Context context) {
    // we only need to provide the variables types, and not the known constant types, because the latter will be extracted from the already registered types.
    Map<String, String> mapFromSymbolNamesToTypeNames = new LinkedHashMap<String, String>();
    for (Map.Entry<String, Type> symbolAndType : getVariableNamesAndTypesForTesting().entrySet()) {
        mapFromSymbolNamesToTypeNames.put(symbolAndType.getKey(), symbolAndType.getValue().toString());
    }
    for (Map.Entry<String, Type> symbolAndType : getExtendedVariableNamesAndTypesForTesting().entrySet()) {
        mapFromSymbolNamesToTypeNames.put(symbolAndType.getKey(), symbolAndType.getValue().toString());
    }
    Context result = (Context) GrinderUtil.extendRegistryWith(mapFromSymbolNamesToTypeNames, getTypesForTesting(), context);
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Type(com.sri.ai.expresso.api.Type) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 50 with Context

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

the class SGDPLLTTester method runGroupProblemSolvingTesterForSuccessiveConstraints.

private static void runGroupProblemSolvingTesterForSuccessiveConstraints(String problemName, TestRunner tester, boolean testAgainstBruteForce, AssociativeCommutativeGroup group, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) throws Error {
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    NullaryFunction<Constraint> makeInitialConstraint = () -> theoryTestingSupport.getTheory().makeSingleVariableConstraint(parse(theoryTestingSupport.pickTestingVariableAtRandom()), theoryTestingSupport.getTheory(), context);
    Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteralOn(((SingleVariableConstraint) c).getVariable().toString(), context);
    runTesterGivenOnSuccessiveConjunctionsOfLiterals(problemName, 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) 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) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression)

Aggregations

Context (com.sri.ai.grinder.sgdpllt.api.Context)105 Expression (com.sri.ai.expresso.api.Expression)80 Test (org.junit.Test)48 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)36 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)30 Type (com.sri.ai.expresso.api.Type)28 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)28 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)22 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)20 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)18 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)17 Map (java.util.Map)17 Beta (com.google.common.annotations.Beta)16 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)16 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)14 LinearRealArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory)14 FunctionType (com.sri.ai.expresso.type.FunctionType)13 Util (com.sri.ai.util.Util)13 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)12