Search in sources :

Example 1 with Solution

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

the class RecursiveTest method testConditionalRecursiveRewriter.

@Test
public void testConditionalRecursiveRewriter() {
    Expression xIs0 = parse("XIs0");
    RewriterFromStepMaker rewriter = (Expression e, Context c) -> {
        if (Expressions.isNumber(e)) {
            return new Solution(DefaultSymbol.createSymbol(e.intValue() + 1));
        } else if (e.equals(parse("X"))) {
            ContextSplitting splitting = new ContextSplitting(xIs0, c);
            switch(splitting.getResult()) {
                case LITERAL_IS_TRUE:
                    return new Solution(ZERO);
                case LITERAL_IS_FALSE:
                    return new Solution(ONE);
                case LITERAL_IS_UNDEFINED:
                    return new ItDependsOn(xIs0, splitting, new ConstantExpressionStepSolver(ZERO), new ConstantExpressionStepSolver(ONE));
                default:
                    throw new Error("Unpredicted case.");
            }
        }
        return new Solution(e);
    };
    Expression initial;
    Expression expected;
    initial = parse("X");
    expected = parse("if " + xIs0 + " then 0 else 1");
    runTest(rewriter, initial, expected, map(xIs0, parse("Boolean")));
    initial = parse("f(9,g(X,7,6))");
    expected = parse("if " + xIs0 + " then f(10,g(0,8,7)) else f(10,g(1,8,7))");
    runTest(rewriter, initial, expected, map(xIs0, parse("Boolean")));
    initial = parse("X(9,g(h(1,2,X),X,7,6))");
    expected = parse("if " + xIs0 + " then 0(10,g(h(2,3,0),0,8,7)) else 1(10,g(h(2,3,1),1,8,7))");
    runTest(rewriter, initial, expected, map(xIs0, 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) RewriterFromStepMaker(com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker) ItDependsOn(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.ItDependsOn) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting) ConstantExpressionStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver) Test(org.junit.Test)

Example 2 with Solution

use of com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution 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 3 with Solution

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

the class ExpressionConditionedOnLiteralSolutionStep method stepDependingOnLiteral.

/**
	 * Produces a solver step based on value of literal, with corresponding given solutions
	 * (if literal is not defined by context, a {@link ItDependsOn} step is returned).
	 * @param literal
	 * @param solutionIfTrue
	 * @param solutionIfFalse
	 * @param context
	 * @return
	 */
public static Step stepDependingOnLiteral(Expression literal, Expression solutionIfTrue, Expression solutionIfFalse, Context context) {
    Step result;
    LiteralStepSolver literalStepSolver = new LiteralStepSolver(literal);
    StepSolver.Step<Boolean> step = literalStepSolver.step(context);
    if (step.itDepends()) {
        result = new ItDependsOn(step.getSplitter(), step.getContextSplittingWhenSplitterIsLiteral(), constantExpressionStepSolver(solutionIfTrue), constantExpressionStepSolver(solutionIfFalse));
    } else {
        result = new Solution(step.getValue() ? solutionIfTrue : solutionIfFalse);
    }
    return result;
}
Also used : ItDependsOn(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.ItDependsOn) Step(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step) StepSolver(com.sri.ai.grinder.sgdpllt.api.StepSolver) ConstantExpressionStepSolver.constantExpressionStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver.constantExpressionStepSolver) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution)

Example 4 with Solution

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

the class Simplifier method step.

default default Step step(Expression expression, Context context) {
    // optimized version
    Expression simplifiedExpression = apply(expression, context);
    Solution result = new Solution(simplifiedExpression);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution)

Example 5 with Solution

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

the class SwitchTest method testSimpleSwitchRewriter.

@Test
public void testSimpleSwitchRewriter() {
    RewriterFromStepMaker baseRewriterSum = (Expression e, Context c) -> {
        int argument1 = e.get(0).intValue();
        int argument2 = e.get(1).intValue();
        Symbol resultValue = DefaultSymbol.createSymbol(argument1 + argument2);
        return new Solution(resultValue);
    };
    RewriterFromStepMaker baseRewriterSubtraction = (Expression e, Context c) -> {
        int argument1 = e.get(0).intValue();
        int argument2 = e.get(1).intValue();
        Symbol resultValue = DefaultSymbol.createSymbol(argument1 - argument2);
        return new Solution(resultValue);
    };
    Switch rewriter = new Switch<String>(e -> e.getFunctor() != null ? e.getFunctor().toString() : "", map("+", baseRewriterSum, "-", baseRewriterSubtraction));
    Expression initial;
    Expression expected;
    initial = parse("7");
    expected = parse("7");
    runTest(rewriter, initial, expected, map());
    initial = parse("10 - 7");
    expected = parse("3");
    runTest(rewriter, initial, expected, map());
    initial = parse("10 + 3");
    expected = parse("13");
    runTest(rewriter, initial, expected, map());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) Expression(com.sri.ai.expresso.api.Expression) Symbol(com.sri.ai.expresso.api.Symbol) DefaultSymbol(com.sri.ai.expresso.core.DefaultSymbol) RewriterFromStepMaker(com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Aggregations

Solution (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution)9 Expression (com.sri.ai.expresso.api.Expression)8 Context (com.sri.ai.grinder.sgdpllt.api.Context)7 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)7 Test (org.junit.Test)7 RewriterFromStepMaker (com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker)5 ContextSplitting (com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)3 Rewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter)3 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)2 ItDependsOn (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.ItDependsOn)2 Symbol (com.sri.ai.expresso.api.Symbol)1 DefaultSymbol (com.sri.ai.expresso.core.DefaultSymbol)1 Step (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step)1 StepSolver (com.sri.ai.grinder.sgdpllt.api.StepSolver)1 Switch (com.sri.ai.grinder.sgdpllt.rewriter.core.Switch)1 ConstantExpressionStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver)1 ConstantExpressionStepSolver.constantExpressionStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver.constantExpressionStepSolver)1