use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.
the class CompoundTheoryWithDifferenceArithmeticTest method makeTheoryTestingSupport.
@Override
protected TheoryTestingSupport makeTheoryTestingSupport() {
TheoryTestingSupport result = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
// using different testing variables and types to test distribution of testing information
// to sub constraint theories.
Categorical booleanType = BOOLEAN_TYPE;
Categorical dogsType = new Categorical("Dogs", 4, arrayList(parse("fido"), parse("rex")));
IntegerInterval oneTwoThree = new IntegerInterval(1, 3);
Map<String, Type> variablesAndTypes = map("F", booleanType, "G", booleanType, "R", dogsType, "S", dogsType, "T", oneTwoThree, "U", oneTwoThree);
result.setVariableNamesAndTypesForTesting(variablesAndTypes);
return result;
}
use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory 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());
unificationStepSolver = new UnificationStepSolver(parse("binary_dar(I, unary_dar(I))"), parse("binary_dar(unary_dar(J), J)"));
step = unificationStepSolver.step(rootContext);
Assert.assertEquals(true, step.itDepends());
Assert.assertEquals(Expressions.parse("I = unary_dar(J)"), step.getSplitter());
}
use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.
the class UnificationStepSolverTest method advancedDifferenceArithmeticTest.
@Ignore("TODO - context implementation currently does not support these more advanced/indirect comparisons")
@Test
public void advancedDifferenceArithmeticTest() {
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/1", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE), "binary_dar/2", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE)));
Context rootContext = theoryTestingSupport.makeContextWithTestingInformation();
UnificationStepSolver unificationStepSolver = new UnificationStepSolver(parse("binary_dar(I, unary_dar(I))"), parse("binary_dar(unary_dar(J), J)"));
Context localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0 and J = 1 and unary_dar(J) = 0 and unary_dar(I) = 1"), rootContext);
StepSolver.Step<Boolean> step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 1 and J = 1 and unary_dar(J) = 0 and unary_dar(I) = 1"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0 and J = 1 and unary_dar(1) = 0 and unary_dar(0) = 1"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
}
use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.
the class EvaluationTest method testEvaluationOfGroupOperationsOnSets.
@Test
public void testEvaluationOfGroupOperationsOnSets() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
Map<String, Type> variablesAndTypes = new LinkedHashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
Type booleanType = variablesAndTypes.get("P");
variablesAndTypes.put("S", booleanType);
variablesAndTypes.put("T", booleanType);
variablesAndTypes.put("U", booleanType);
theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
Context context = theoryTestingSupport.makeContextWithTestingInformation();
String expressionString;
Expression expected;
expressionString = "sum( {{ (on I in 1..10) 3 : I != 4 and P }} )";
expected = parse("if P then 27 else 0");
runTest(expressionString, expected, context);
expressionString = "sum( {{ (on ) 3 : I != 4 and P }} )";
expected = parse("if I != 4 then if P then 3 else 0 else 0");
runTest(expressionString, expected, context);
expressionString = "sum( {{ (on ) 3 : P and not P }} )";
expected = parse("0");
runTest(expressionString, expected, context);
expressionString = "sum( {{ (on I in 1..10, J in 1..2) 3 : I != 4 }} )";
expected = parse("54");
runTest(expressionString, expected, context);
expressionString = "sum( {{ (on I in 1..10, P in Boolean) 3 : I != 4 }} )";
expected = parse("54");
runTest(expressionString, expected, context);
expressionString = "max( {{ (on I in 1..10) 3 : I != 4 and P }} )";
expected = parse("if P then 3 else -infinity");
runTest(expressionString, expected, context);
expressionString = "max( {{ (on ) 3 : I != 4 and P }} )";
expected = parse("if I != 4 then if P then 3 else -infinity else -infinity");
runTest(expressionString, expected, context);
expressionString = "max( {{ (on ) 3 : P and not P }} )";
expected = parse("-infinity");
runTest(expressionString, expected, context);
expressionString = "max( {{ (on I in 1..10, J in 1..2) 3 : I != 4 }} )";
expected = parse("3");
runTest(expressionString, expected, context);
expressionString = "max( {{ (on I in 1..10, P in Boolean) 3 : I != 4 }} )";
expected = parse("3");
runTest(expressionString, expected, context);
}
use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.
the class EvaluationTest method testEvaluationOfFunctionApplications.
@Test
public void testEvaluationOfFunctionApplications() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
Map<String, Type> variablesAndTypes = new LinkedHashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
Type booleanType = variablesAndTypes.get("P");
variablesAndTypes.put("S", booleanType);
variablesAndTypes.put("T", booleanType);
variablesAndTypes.put("U", booleanType);
theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
Context context = theoryTestingSupport.makeContextWithTestingInformation();
String expressionString;
Expression expected;
expressionString = "0";
expected = parse("0");
runTest(expressionString, expected, context);
expressionString = "I > J";
expected = parse("I > J");
runTest(expressionString, expected, context);
expressionString = "I > J and I < J";
expected = parse("false");
runTest(expressionString, expected, context);
expressionString = "(if I > J then 1 else 2) + (if I <= J then 30 else 40)";
expected = parse("if I > J then 41 else 32");
runTest(expressionString, expected, context);
expressionString = "(if I > J then 1 else 2) + (if I <= J then 3 else 4)";
expected = parse("5");
runTest(expressionString, expected, context);
expressionString = "(if I > J then if P or Q then 1 else 2 else 5) + (if I <= J then 3 else if not Q then 4 else -3)";
expected = parse("if I > J then if P then if not Q then 5 else -2 else if Q then -2 else 6 else 8");
runTest(expressionString, expected, context);
expressionString = "(if I > J then if P or X = a or Y != b then 1 else 2 else 5) + (if I <= J then 3 else if not (X != a or Y = c and Q) then 4 else -3)";
expected = parse("if I > J then if P then if X != a then -2 else if Y = c then if Q then -2 else 5 else 5 else if X = a then if Y = c then if Q then -2 else 5 else 5 else if Y != b then -2 else -1 else 8");
runTest(expressionString, expected, context);
expressionString = "if P and Q and R then 1 else 0";
expected = parse("if P then if Q then if R then 1 else 0 else 0 else 0");
runTest(expressionString, expected, context);
expressionString = "if P and Q and R and S and T and U then 1 else 0";
expected = parse("if P then if Q then if R then if S then if T then if U then 1 else 0 else 0 else 0 else 0 else 0 else 0");
runTest(expressionString, expected, context);
}
Aggregations