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")));
}
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);
}
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);
}
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);
}
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);
}
Aggregations