use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class UnificationStepSolverTest method compoundTest.
@Test
public void compoundTest() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(seededRandom, new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new LinearRealArithmeticTheory(false, true), new PropositionalTheory()));
// NOTE: passing explicit FunctionTypes will prevent the general variables' argument types being randomly changed.
theoryTestingSupport.setVariableNamesAndTypesForTesting(map("P", BOOLEAN_TYPE, "Q", BOOLEAN_TYPE, "R", BOOLEAN_TYPE, "unary_prop", new FunctionType(BOOLEAN_TYPE, BOOLEAN_TYPE), "binary_prop", new FunctionType(BOOLEAN_TYPE, BOOLEAN_TYPE, BOOLEAN_TYPE), "S", TESTING_CATEGORICAL_TYPE, "T", TESTING_CATEGORICAL_TYPE, "U", TESTING_CATEGORICAL_TYPE, "unary_eq", new FunctionType(TESTING_CATEGORICAL_TYPE, TESTING_CATEGORICAL_TYPE), "binary_eq", new FunctionType(TESTING_CATEGORICAL_TYPE, TESTING_CATEGORICAL_TYPE, TESTING_CATEGORICAL_TYPE), "I", TESTING_INTEGER_INTERVAL_TYPE, "J", TESTING_INTEGER_INTERVAL_TYPE, "K", TESTING_INTEGER_INTERVAL_TYPE, "unary_dar", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE), "binary_dar", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE), "X", TESTING_REAL_INTERVAL_TYPE, "Y", TESTING_REAL_INTERVAL_TYPE, "Z", TESTING_REAL_INTERVAL_TYPE, "unary_lra", new FunctionType(TESTING_REAL_INTERVAL_TYPE, TESTING_REAL_INTERVAL_TYPE), "binary_lra", new FunctionType(TESTING_REAL_INTERVAL_TYPE, TESTING_REAL_INTERVAL_TYPE, TESTING_REAL_INTERVAL_TYPE)));
Context rootContext = theoryTestingSupport.makeContextWithTestingInformation();
UnificationStepSolver unificationStepSolver = new UnificationStepSolver(parse("unary_prop(P)"), parse("unary_prop(P)"));
StepSolver.Step<Boolean> step = unificationStepSolver.step(rootContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_prop(P)"), parse("unary_prop(Q)"));
step = unificationStepSolver.step(rootContext);
Assert.assertEquals(true, step.itDepends());
Assert.assertEquals(Expressions.parse("P = Q"), step.getSplitter());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).itDepends());
Assert.assertEquals(true, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).getValue());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).itDepends());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).getValue());
Context localTestContext = rootContext.conjoinWithConjunctiveClause(parse("P and not Q"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_prop(P)"), parse("unary_prop(true)"));
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("P"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("not P"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
//
//
unificationStepSolver = new UnificationStepSolver(parse("unary_eq(S)"), parse("unary_eq(S)"));
step = unificationStepSolver.step(rootContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_eq(S)"), parse("unary_eq(T)"));
step = unificationStepSolver.step(rootContext);
Assert.assertEquals(true, step.itDepends());
Assert.assertEquals(Expressions.parse("S = T"), step.getSplitter());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).itDepends());
Assert.assertEquals(true, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).getValue());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).itDepends());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("S = a and T = b"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_eq(S)"), parse("unary_eq(a)"));
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("S = a"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("S = b"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
//
//
unificationStepSolver = new UnificationStepSolver(parse("unary_dar(I)"), parse("unary_dar(I)"));
step = unificationStepSolver.step(rootContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_dar(I)"), parse("unary_dar(J)"));
step = unificationStepSolver.step(rootContext);
Assert.assertEquals(true, step.itDepends());
Assert.assertEquals(Expressions.parse("I = J"), step.getSplitter());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).itDepends());
Assert.assertEquals(true, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).getValue());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).itDepends());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0 and J = 1"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_dar(I)"), parse("unary_dar(0)"));
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 1"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
//
//
unificationStepSolver = new UnificationStepSolver(parse("unary_lra(X)"), parse("unary_lra(X)"));
step = unificationStepSolver.step(rootContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_lra(X)"), parse("unary_lra(Y)"));
step = unificationStepSolver.step(rootContext);
Assert.assertEquals(true, step.itDepends());
Assert.assertEquals(Expressions.parse("X = Y"), step.getSplitter());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).itDepends());
Assert.assertEquals(true, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).getValue());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).itDepends());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("X = 0 and Y = 1"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_lra(X)"), parse("unary_lra(0)"));
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("X = 0"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("X = 1"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class UnificationStepSolverTest method differenceArithmeticTest.
@Test
public void differenceArithmeticTest() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(seededRandom, new DifferenceArithmeticTheory(true, true));
// NOTE: passing explicit FunctionTypes will prevent the general variables' argument types being randomly changed.
theoryTestingSupport.setVariableNamesAndTypesForTesting(map("I", TESTING_INTEGER_INTERVAL_TYPE, "J", TESTING_INTEGER_INTERVAL_TYPE, "K", TESTING_INTEGER_INTERVAL_TYPE, "unary_dar", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE), "binary_dar", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE)));
Context rootContext = theoryTestingSupport.makeContextWithTestingInformation();
UnificationStepSolver unificationStepSolver = new UnificationStepSolver(parse("unary_dar(I)"), parse("unary_dar(I)"));
StepSolver.Step<Boolean> step = unificationStepSolver.step(rootContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_dar(I)"), parse("unary_dar(J)"));
step = unificationStepSolver.step(rootContext);
Assert.assertEquals(true, step.itDepends());
Assert.assertEquals(Expressions.parse("I = J"), step.getSplitter());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).itDepends());
Assert.assertEquals(true, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).getValue());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).itDepends());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).getValue());
Context localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0 and J = 1"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_dar(I)"), parse("unary_dar(0)"));
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 1"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class UnificationStepSolverTest method equalityTest.
@Test
public void equalityTest() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(seededRandom, new EqualityTheory(true, true));
// NOTE: passing explicit FunctionTypes will prevent the general variables' argument types being randomly changed.
theoryTestingSupport.setVariableNamesAndTypesForTesting(map("X", TESTING_CATEGORICAL_TYPE, "Y", TESTING_CATEGORICAL_TYPE, "Z", TESTING_CATEGORICAL_TYPE, "unary_eq", new FunctionType(TESTING_CATEGORICAL_TYPE, TESTING_CATEGORICAL_TYPE), "binary_eq", new FunctionType(TESTING_CATEGORICAL_TYPE, TESTING_CATEGORICAL_TYPE, TESTING_CATEGORICAL_TYPE)));
Context rootContext = theoryTestingSupport.makeContextWithTestingInformation();
UnificationStepSolver unificationStepSolver = new UnificationStepSolver(parse("unary_eq(X)"), parse("unary_eq(X)"));
StepSolver.Step<Boolean> step = unificationStepSolver.step(rootContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_eq(X)"), parse("unary_eq(Y)"));
step = unificationStepSolver.step(rootContext);
Assert.assertEquals(true, step.itDepends());
Assert.assertEquals(Expressions.parse("X = Y"), step.getSplitter());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).itDepends());
Assert.assertEquals(true, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).getValue());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).itDepends());
Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).getValue());
Context localTestContext = rootContext.conjoinWithConjunctiveClause(parse("X = a and Y = b"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("unary_eq(X)"), parse("unary_eq(a)"));
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("X = a"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("X = b"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
unificationStepSolver = new UnificationStepSolver(parse("binary_eq(X, unary_eq(X))"), parse("binary_eq(unary_eq(Y), Y)"));
step = unificationStepSolver.step(rootContext);
Assert.assertEquals(true, step.itDepends());
Assert.assertEquals(Expressions.parse("X = unary_eq(Y)"), step.getSplitter());
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class BruteForceFallbackTheoryTest method runTest.
private static void runTest(String expressionString, String expectedString, String... symbolsAndTypes) {
Theory theory = new BruteForceFallbackTheory(new DifferenceArithmeticTheory(false, true));
Context context = new TrueContext(theory);
context = context.extendWithSymbolsAndTypes(symbolsAndTypes);
Expression expression = parse(expressionString);
Expression expected = parse(expectedString);
Expression actual = theory.evaluate(expression, context);
println(actual);
assertEquals(expected, actual);
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class CompoundTheoryWithDifferenceArithmeticTest method basicTests.
@Test
public void basicTests() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
Expression condition = parse("X = Y and Y = X and P and not Q and P and X = a and X != b");
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
constraint = constraint.conjoin(condition, context);
Expression expected = parse("(Y = a) and not Q and P and (X = Y)");
assertEquals(expected, constraint);
// nested indices
Expression expression = parse("sum({{(on I in 1..2, J in 2..3) sum({{ (on I in 1..10, J in 1..2) I + J : I != J }}) }})");
context = new TrueContext(theoryTestingSupport.getTheory());
expected = parse("536");
Expression actual = theoryTestingSupport.getTheory().evaluate(expression, context);
println(actual);
assertEquals(expected, actual);
}
Aggregations