Search in sources :

Example 91 with Context

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

the class UnificationStepSolverTest method advancedLinearRealArithmeticTest.

@Ignore("TODO - context implementation currently does not support these more advanced/indirect comparisons")
@Test
public void advancedLinearRealArithmeticTest() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(seededRandom, new LinearRealArithmeticTheory(true, true));
    // NOTE: passing explicit FunctionTypes will prevent the general variables' argument types being randomly changed.
    theoryTestingSupport.setVariableNamesAndTypesForTesting(map("X", TESTING_REAL_INTERVAL_TYPE, "Y", TESTING_REAL_INTERVAL_TYPE, "Z", TESTING_REAL_INTERVAL_TYPE, "unary_lra/1", new FunctionType(TESTING_REAL_INTERVAL_TYPE, TESTING_REAL_INTERVAL_TYPE), "binary_lra/2", new FunctionType(TESTING_REAL_INTERVAL_TYPE, TESTING_REAL_INTERVAL_TYPE, TESTING_REAL_INTERVAL_TYPE)));
    Context rootContext = theoryTestingSupport.makeContextWithTestingInformation();
    UnificationStepSolver unificationStepSolver = new UnificationStepSolver(parse("binary_lra(X, unary_lra(X))"), parse("binary_lra(unary_lra(Y), Y)"));
    Context localTestContext = rootContext.conjoinWithConjunctiveClause(parse("X = 0 and Y = 1 and unary_lra(Y) = 0 and unary_lra(X) = 1"), rootContext);
    StepSolver.Step<Boolean> step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(true, step.getValue());
    localTestContext = rootContext.conjoinWithConjunctiveClause(parse("X = 1 and Y = 1 and unary_lra(Y) = 0 and unary_lra(X) = 1"), rootContext);
    step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(false, step.getValue());
    localTestContext = rootContext.conjoinWithConjunctiveClause(parse("X = 0 and Y = 1 and unary_lra(1) = 0 and unary_lra(0) = 1"), rootContext);
    step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(true, step.getValue());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) FunctionType(com.sri.ai.expresso.type.FunctionType) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) UnificationStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver) StepSolver(com.sri.ai.grinder.sgdpllt.api.StepSolver) UnificationStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 92 with Context

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

the class UnificationStepSolverTest method advancedPropositionalTest.

@Ignore("TODO - context implementation currently does not support these more advanced/indirect comparisons")
@Test
public void advancedPropositionalTest() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(seededRandom, 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/1", new FunctionType(BOOLEAN_TYPE, BOOLEAN_TYPE), "binary_prop/2", new FunctionType(BOOLEAN_TYPE, BOOLEAN_TYPE, BOOLEAN_TYPE)));
    Context rootContext = theoryTestingSupport.makeContextWithTestingInformation();
    UnificationStepSolver unificationStepSolver = new UnificationStepSolver(parse("binary_prop(P, unary_prop(P))"), parse("binary_prop(unary_prop(Q), Q)"));
    Context localTestContext = rootContext.conjoinWithConjunctiveClause(parse("not P and Q and not unary_prop(true) and unary_prop(false)"), rootContext);
    StepSolver.Step<Boolean> step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(true, step.getValue());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) FunctionType(com.sri.ai.expresso.type.FunctionType) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) UnificationStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver) StepSolver(com.sri.ai.grinder.sgdpllt.api.StepSolver) UnificationStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 93 with Context

use of com.sri.ai.grinder.sgdpllt.api.Context 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());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) FunctionType(com.sri.ai.expresso.type.FunctionType) UnificationStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver) StepSolver(com.sri.ai.grinder.sgdpllt.api.StepSolver) UnificationStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 94 with Context

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

the class SwitchTest method testSimpleSwitchRewriter.

@Test
public void testSimpleSwitchRewriter() {
    RewriterFromStepMaker baseRewriterSum = (Expression e, Context c) -> {
        int argument1 = e.get(0).intValue();
        int argument2 = e.get(1).intValue();
        Symbol resultValue = DefaultSymbol.createSymbol(argument1 + argument2);
        return new Solution(resultValue);
    };
    RewriterFromStepMaker baseRewriterSubtraction = (Expression e, Context c) -> {
        int argument1 = e.get(0).intValue();
        int argument2 = e.get(1).intValue();
        Symbol resultValue = DefaultSymbol.createSymbol(argument1 - argument2);
        return new Solution(resultValue);
    };
    Switch rewriter = new Switch<String>(e -> e.getFunctor() != null ? e.getFunctor().toString() : "", map("+", baseRewriterSum, "-", baseRewriterSubtraction));
    Expression initial;
    Expression expected;
    initial = parse("7");
    expected = parse("7");
    runTest(rewriter, initial, expected, map());
    initial = parse("10 - 7");
    expected = parse("3");
    runTest(rewriter, initial, expected, map());
    initial = parse("10 + 3");
    expected = parse("13");
    runTest(rewriter, initial, expected, map());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) Expression(com.sri.ai.expresso.api.Expression) Symbol(com.sri.ai.expresso.api.Symbol) DefaultSymbol(com.sri.ai.expresso.core.DefaultSymbol) RewriterFromStepMaker(com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Example 95 with Context

use of com.sri.ai.grinder.sgdpllt.api.Context 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);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

Context (com.sri.ai.grinder.sgdpllt.api.Context)105 Expression (com.sri.ai.expresso.api.Expression)80 Test (org.junit.Test)48 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)36 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)30 Type (com.sri.ai.expresso.api.Type)28 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)28 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)22 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)20 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)18 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)17 Map (java.util.Map)17 Beta (com.google.common.annotations.Beta)16 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)16 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)14 LinearRealArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory)14 FunctionType (com.sri.ai.expresso.type.FunctionType)13 Util (com.sri.ai.util.Util)13 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)12