Search in sources :

Example 1 with Rewriter

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

the class DifferenceArithmeticTheory method makeEvaluatorStepSolver.

@Override
public ExpressionLiteralSplitterStepSolver makeEvaluatorStepSolver(Expression expression) {
    Rewriter literalExternalizer = new LiteralRewriter(new Recursive(new Exhaustive(getTopRewriter())));
    ExpressionLiteralSplitterStepSolver result = new Recursive(new Exhaustive(new FirstOf(getTopRewriter(), literalExternalizer))).makeStepSolver(expression);
    return result;
}
Also used : Exhaustive(com.sri.ai.grinder.sgdpllt.rewriter.core.Exhaustive) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) LiteralRewriter(com.sri.ai.grinder.sgdpllt.library.boole.LiteralRewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) FirstOf(com.sri.ai.grinder.sgdpllt.rewriter.core.FirstOf) LiteralRewriter(com.sri.ai.grinder.sgdpllt.library.boole.LiteralRewriter) Recursive(com.sri.ai.grinder.sgdpllt.rewriter.core.Recursive)

Example 2 with Rewriter

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

the class SwitchTest method runTest.

private void runTest(Rewriter rewriter, 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 recursive = new Recursive(rewriter);
    Expression solution = recursive.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) DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) Recursive(com.sri.ai.grinder.sgdpllt.rewriter.core.Recursive) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext)

Example 3 with Rewriter

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

the class ExhaustiveTest method runTest.

private void runTest(Rewriter rewriter, 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 exhaustive = new Exhaustive(rewriter);
    Expression solution = exhaustive.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) Exhaustive(com.sri.ai.grinder.sgdpllt.rewriter.core.Exhaustive) 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) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext)

Example 4 with Rewriter

use of com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter 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 5 with Rewriter

use of com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter 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)

Aggregations

Rewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter)16 TopRewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter)10 Expression (com.sri.ai.expresso.api.Expression)9 Context (com.sri.ai.grinder.sgdpllt.api.Context)9 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)8 Recursive (com.sri.ai.grinder.sgdpllt.rewriter.core.Recursive)8 FirstOf (com.sri.ai.grinder.sgdpllt.rewriter.core.FirstOf)7 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)6 Exhaustive (com.sri.ai.grinder.sgdpllt.rewriter.core.Exhaustive)6 Test (org.junit.Test)6 DefaultTopRewriter (com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter)5 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)5 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)5 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)5 Solution (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution)4 ContextSplitting (com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)3 LiteralRewriter (com.sri.ai.grinder.sgdpllt.library.boole.LiteralRewriter)3 Switch (com.sri.ai.grinder.sgdpllt.rewriter.core.Switch)3 ForAllRewriter (com.sri.ai.grinder.sgdpllt.library.boole.ForAllRewriter)2 ThereExistsRewriter (com.sri.ai.grinder.sgdpllt.library.boole.ThereExistsRewriter)2