use of com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting 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")));
}
use of com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting 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")));
}
use of com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting in project aic-expresso by aic-sri-international.
the class AbstractQuantifierEliminationStepSolver method step.
@Override
public Step step(Context context) {
Step result;
Context contextForBody = getContextForBody(context);
if (contextForBody.isContradiction()) {
result = new Solution(group.additiveIdentityElement());
} else {
ExpressionLiteralSplitterStepSolver bodyStepSolver = getInitialBodyStepSolver(context.getTheory());
ExpressionLiteralSplitterStepSolver.Step bodyStep = bodyStepSolver.step(contextForBody);
// Check (**) in this file to see where this happens
if (!bodyStep.itDepends()) {
ExpressionLiteralSplitterStepSolver evaluatorStepSolver = getTheory().makeEvaluatorStepSolver(bodyStep.getValue());
bodyStep = evaluatorStepSolver.step(context);
}
if (bodyStep.itDepends()) {
// "intercept" literals containing the index and split the quantifier based on it
if (isSubExpressionOf(getIndex(), bodyStep.getSplitterLiteral())) {
Expression literalOnIndex = bodyStep.getSplitterLiteral();
result = resultIfLiteralContainsIndex(literalOnIndex, bodyStep, contextForBody, context);
} else {
// not on index, just pass the expression on which we depend on, but with appropriate sub-step solvers (this, for now)
AbstractQuantifierEliminationStepSolver ifTrue = clone();
AbstractQuantifierEliminationStepSolver ifFalse = clone();
ifTrue.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsTrue();
ifFalse.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsFalse();
ifTrue.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteral();
ifFalse.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteralNegation();
// to compute the result's constraint splitting,
// we cannot directly re-use bodyStep.getConstraintSplitting() because it was not obtained from
// the context it is returning to,
// but from the context conjoined with the index constraint.
// In order to provide two contexts to work with the sequel step solvers,
// we calculate the splittings here.
// TODO: In the future, we expect it possible to efficiently extract the contextForBody component relative
// to the original context only, excluding the index.
ContextSplitting split = new ContextSplitting(bodyStep.getSplitterLiteral(), context);
result = new ItDependsOn(bodyStep.getSplitterLiteral(), split, ifTrue, ifFalse);
}
} else {
// body is already literal free
result = eliminateQuantifierForLiteralFreeBodyAndSingleVariableConstraint(indexConstraint, bodyStep.getValue(), context);
}
}
return result;
}
use of com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting in project aic-expresso by aic-sri-international.
the class ContextDependentExpressionProblemSolver method solve.
/**
* Returns the solution for a problem using a step solver.
* @param stepSolver
* @param context
* @return
*/
public Expression solve(ExpressionLiteralSplitterStepSolver stepSolver, Context context) {
if (interrupted) {
throw new Error("Solver interrupted.");
}
Expression result;
ExpressionLiteralSplitterStepSolver.Step step = stepSolver.step(context);
if (step.itDepends()) {
Expression splitter = step.getSplitter();
ContextSplitting split = (ContextSplitting) step.getContextSplittingWhenSplitterIsLiteral();
myAssert(() -> split.isUndefined(), () -> "Undefined " + ContextSplitting.class + " result value: " + split.getResult());
Expression subSolution1 = solve(step.getStepSolverForWhenSplitterIsTrue(), split.getConstraintAndLiteral());
Expression subSolution2 = solve(step.getStepSolverForWhenSplitterIsFalse(), split.getConstraintAndLiteralNegation());
result = IfThenElse.make(splitter, subSolution1, subSolution2, true);
} else {
result = step.getValue();
}
return result;
}
use of com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting in project aic-expresso by aic-sri-international.
the class LiteralExpressionStepSolver method step.
@Override
public StepSolver.Step<Expression> step(Context context) {
ContextSplitting split = new ContextSplitting(literal, context);
switch(split.getResult()) {
case CONSTRAINT_IS_CONTRADICTORY:
return null;
case LITERAL_IS_TRUE:
return new Solution<>(TRUE);
case LITERAL_IS_FALSE:
return new Solution<>(FALSE);
case LITERAL_IS_UNDEFINED:
StepSolver<Expression> ifTrue = constantStepSolver(TRUE);
StepSolver<Expression> ifFalse = constantStepSolver(FALSE);
return new ItDependsOn<Expression>(literal, split, ifTrue, ifFalse);
default:
throw new Error("Unrecognized splitting result.");
}
}
Aggregations