Search in sources :

Example 16 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class ExhaustiveTest method testSimpleExhaustiveConditionalRewriter.

@Test
public void testSimpleExhaustiveConditionalRewriter() {
    class FunkyStepSolver implements ExpressionLiteralSplitterStepSolver {

        private Expression expression;

        public FunkyStepSolver(Expression expression) {
            this.expression = expression;
        }

        @Override
        public FunkyStepSolver clone() {
            FunkyStepSolver result = null;
            try {
                result = (FunkyStepSolver) super.clone();
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            return result;
        }

        @Override
        public Step step(Context context) {
            if (Expressions.isNumber(expression) && expression.intValue() % 10 != 0) {
                if (expression.intValue() == 5) {
                    Expression literal = parse("JumpAt5");
                    ContextSplitting splitting = new ContextSplitting(literal, context);
                    switch(splitting.getResult()) {
                        case LITERAL_IS_TRUE:
                            return new Solution(parse("11"));
                        case LITERAL_IS_FALSE:
                            return new Solution(parse("6"));
                        case LITERAL_IS_UNDEFINED:
                            return new ItDependsOn(literal, splitting, this, this);
                        default:
                            throw new Error("Unpredicted case");
                    }
                } else {
                    return new Solution(DefaultSymbol.createSymbol(expression.intValue() + 1));
                }
            } else
                return new Solution(expression);
        }
    }
    ;
    Rewriter rewriter = (Expression e) -> new FunkyStepSolver(e);
    Expression initial = parse("1");
    Expression expected = parse("if JumpAt5 then 20 else 10");
    runTest(rewriter, initial, expected, map(parse("JumpAt5"), parse("Boolean")));
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Example 17 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class FirstOfTest method runTest.

private void runTest(List<Rewriter> rewriters, Expression initial, Expression expected, Map<Expression, Expression> symbolsAndTypes) {
    CompoundTheory theory = new CompoundTheory(new PropositionalTheory(), new DifferenceArithmeticTheory(false, true));
    Context context = new TrueContext(theory);
    context = context.registerAdditionalSymbolsAndTypes(symbolsAndTypes);
    Rewriter firstOf = new FirstOf(rewriters);
    Expression solution = firstOf.apply(initial, context);
    System.out.println("Solution: " + solution);
    assertEquals(expected, solution);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) FirstOf(com.sri.ai.grinder.sgdpllt.rewriter.core.FirstOf) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext)

Example 18 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolverTest method runTest.

private void runTest(Expression variable, String constraintString, Expression expected, Context context) {
    Constraint constraint = new SingleVariableDifferenceArithmeticConstraint(variable, true, context.getTheory());
    constraint = constraint.conjoin(parse(constraintString), context);
    ExpressionLiteralSplitterStepSolver stepSolver = new ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver((SingleVariableDifferenceArithmeticConstraint) constraint);
    Expression actual = stepSolver.solve(context);
    System.out.println("Variable " + variable + "\nhas possible values:\n" + actual + "\nsatisfying constraint:\n" + constraintString + "\n");
    assertEquals(expected, actual);
}
Also used : ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)

Example 19 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class CompoundTheoryWithoutDifferenceArithmeticTest method testCompleteSatisfiabilitySpecialCases.

@Test
public void testCompleteSatisfiabilitySpecialCases() {
    // This test is to make sure that some more tricky cases are indeed tested,
    // even though hopefully the large amount of generated random problems include them.
    // These are copied from the equality theory test,
    // so it is really just to check whether things hold up
    // if equality theory is embedded in a compound theory.
    String conjunction;
    Expression expected;
    Categorical someType = AbstractTheoryTestingSupport.getDefaultTestingType();
    // need W besides the other defaults -- somehow not doing this in equality theory alone does not cause a problem, probably because the type for W is never needed when we have only equality theory
    Map<String, Type> variableNamesAndTypesForTesting = map("X", someType, "Y", someType, "Z", someType, "W", someType);
    conjunction = "X != a and X != b and X != sometype5 and X != Z and X != W and Z = c and W = d";
    expected = FALSE;
    runCompleteSatisfiabilityTest(conjunction, expected, variableNamesAndTypesForTesting);
    conjunction = "X = Y and X != a and X != b and X != sometype5 and X != Z and X != W and Z = c and W = d";
    expected = FALSE;
    runCompleteSatisfiabilityTest(conjunction, expected, variableNamesAndTypesForTesting);
    conjunction = "X = a and X != b and X != sometype5 and X != Z and X != W and Z = c and W = d";
    expected = parse("(W = d) and (Z = c) and (X = a)");
    runCompleteSatisfiabilityTest(conjunction, expected, variableNamesAndTypesForTesting);
}
Also used : Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) Categorical(com.sri.ai.expresso.type.Categorical) AbstractTheoryTest(com.sri.ai.test.grinder.sgdpllt.theory.base.AbstractTheoryTest) Test(org.junit.Test)

Example 20 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class SummationOnDifferenceArithmeticAndPolynomialStepSolverTest method polynomialBodyAndConstraintWithADifferentVariableTest.

@Test
public void polynomialBodyAndConstraintWithADifferentVariableTest() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new DifferenceArithmeticTheory(true, true));
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Expression variable;
    String constraintString;
    Expression body;
    Expression expected;
    variable = parse("I");
    body = parse("I^2 - J + 1");
    constraintString = "I != J";
    expected = parse("-1*J^2 + -4*J + 34");
    runTest(variable, constraintString, body, expected, context);
    constraintString = "I <= J and I != J";
    expected = parse("1/3 * J ^ 3 + -1.5 * J ^ 2 + 7/6 * J");
    runTest(variable, constraintString, body, expected, context);
    constraintString = "I < J and I > J";
    expected = parse("0");
    runTest(variable, constraintString, body, expected, context);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) Test(org.junit.Test)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)1392 Test (org.junit.Test)259 ArrayList (java.util.ArrayList)196 Context (com.sri.ai.grinder.api.Context)187 Type (com.sri.ai.expresso.api.Type)124 TrueContext (com.sri.ai.grinder.core.TrueContext)113 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)100 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)91 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)90 Context (com.sri.ai.grinder.sgdpllt.api.Context)87 Theory (com.sri.ai.grinder.api.Theory)78 Map (java.util.Map)78 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)71 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)68 List (java.util.List)68 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)63 CommonTheory (com.sri.ai.grinder.application.CommonTheory)55 LinkedHashMap (java.util.LinkedHashMap)55 LinkedHashSet (java.util.LinkedHashSet)54 Pair (com.sri.ai.util.base.Pair)52