Search in sources :

Example 56 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-praise by aic-sri-international.

the class AbstractHOGMv1_to_Target_Translator method translate.

// END-Translator
//
@Override
protected void translate(String inputIdentifier, Reader[] inputModelReaders, PrintWriter[] translatedOutputs) throws Exception {
    //
    // 1. Get the HOGM Model Definition and Parse It
    String hogmv1Model = Util.readAll(inputModelReaders[0]);
    HOGMParserWrapper parser = new HOGMParserWrapper();
    ParsedHOGModel parsedModel = parser.parseModel(hogmv1Model);
    FactorsAndTypes factorsAndTypes = new ExpressionFactorsAndTypes(parsedModel);
    // Each additional input is treated as an evidence expression
    List<Expression> evidence = new ArrayList<>();
    if (inputModelReaders.length > 1) {
        for (int i = 1; i < inputModelReaders.length; i++) {
            evidence.add(Expressions.parse(Util.readAll(inputModelReaders[i])));
        }
    }
    translate(inputIdentifier, factorsAndTypes, evidence, translatedOutputs);
}
Also used : ParsedHOGModel(com.sri.ai.praise.model.v1.hogm.antlr.ParsedHOGModel) FactorsAndTypes(com.sri.ai.praise.sgsolver.solver.FactorsAndTypes) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper)

Example 57 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class TupleRewriterTest method testTupleSetSimplification.

@Test
public void testTupleSetSimplification() {
    Simplifier tupleSetSimplifier = new TupleSetSimplifier();
    Assert.assertEquals(parse("(e,b,c)"), tupleSetSimplifier.apply(parse("set((a,b,c),1,e)"), context));
    Assert.assertEquals(parse("(a,e,c)"), tupleSetSimplifier.apply(parse("set((a,b,c),2,e)"), context));
    Assert.assertEquals(parse("(a,b,e)"), tupleSetSimplifier.apply(parse("set((a,b,c),3,e)"), context));
    Assert.assertEquals(parse("tuple(e)"), tupleSetSimplifier.apply(parse("set(tuple(a),I,e)"), context));
    Assert.assertEquals(parse("if I = 1 then (e,b) else (a,e)"), tupleSetSimplifier.apply(parse("set((a,b),I,e)"), context));
    Assert.assertEquals(parse("if I = 1 then (e,b,c) else if I = 2 then (a,e,c) else (a,b,e)"), tupleSetSimplifier.apply(parse("set((a,b,c),I,e)"), context));
    Expression expr = parse("set(I, (a,b,c), e)");
    Assert.assertTrue(expr == tupleSetSimplifier.apply(expr, context));
}
Also used : TupleSetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleSetSimplifier) Expression(com.sri.ai.expresso.api.Expression) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) TupleEqualitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualitySimplifier) TupleGetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleQuantifierSimplifier) TupleDisequalitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleDisequalitySimplifier) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) TupleSetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleSetSimplifier) Test(org.junit.Test)

Example 58 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class TupleRewriterTest method testTupleValuedFreeVariablesSimplifier.

@Test
public void testTupleValuedFreeVariablesSimplifier() {
    Context tupleTheoryContext = new TrueContext(new CompoundTheory(new DifferenceArithmeticTheory(false, false), new TupleTheory()));
    TupleType nTupleType = new TupleType(new IntegerInterval(1, 10), new IntegerInterval(1, 10));
    tupleTheoryContext = (Context) GrinderUtil.extendRegistryWith(map("N", nTupleType.toString()), Arrays.asList(nTupleType), tupleTheoryContext);
    TupleValuedFreeVariablesSimplifier simplifier = new TupleValuedFreeVariablesSimplifier();
    Expression expression = parse("sum( {{ (on X in 1..10) if N = (2, X) then 2 else 3 }} )");
    Expression simplified = simplifier.apply(expression, tupleTheoryContext);
    Assert.assertEquals(parse("if get(N, 1) = 2 then 29 else 30"), simplified);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) TupleType(com.sri.ai.expresso.type.TupleType) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) Test(org.junit.Test)

Example 59 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class TupleRewriterTest method testTupleEqualityTopRewriter.

@Test
public void testTupleEqualityTopRewriter() {
    TopRewriter tupleEqualityTopRewriter = new TupleEqualityTopRewriter();
    Assert.assertEquals(parse("A1 = B1 and A2 = B2 and A3 = B3"), tupleEqualityTopRewriter.apply(parse("(A1, A2, A3) = (B1, B2, B3)"), context));
    Assert.assertEquals(parse("(A1_1, A1_2, A1_3) = (B1_1, B1_2, B1_3) and A2 = B2 and A3 = B3"), tupleEqualityTopRewriter.apply(parse("((A1_1, A1_2, A1_3), A2, A3) = ((B1_1, B1_2, B1_3), B2, B3)"), context));
    Assert.assertEquals(parse("A1 != B1 or A2 != B2 or A3 != B3"), tupleEqualityTopRewriter.apply(parse("(A1, A2, A3) != (B1, B2, B3)"), context));
    Assert.assertEquals(parse("(A1_1, A1_2, A1_3) != (B1_1, B1_2, B1_3) or A2 != B2 or A3 != B3"), tupleEqualityTopRewriter.apply(parse("((A1_1, A1_2, A1_3), A2, A3) != ((B1_1, B1_2, B1_3), B2, B3)"), context));
    Expression expr = parse("(A1, A2) = (B1, B2, B3)");
    Assert.assertTrue(expr == tupleEqualityTopRewriter.apply(expr, context));
    expr = parse("A1 = B1");
    Assert.assertTrue(expr == tupleEqualityTopRewriter.apply(expr, context));
    expr = parse("(A1, A2) != (B1, B2, B3)");
    Assert.assertTrue(expr == tupleEqualityTopRewriter.apply(expr, context));
    expr = parse("A1 != B1");
    Assert.assertTrue(expr == tupleEqualityTopRewriter.apply(expr, context));
}
Also used : TupleEqualityTopRewriter(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualityTopRewriter) Expression(com.sri.ai.expresso.api.Expression) TupleEqualityTopRewriter(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualityTopRewriter) TupleGetSetTopRewriter(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSetTopRewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) Test(org.junit.Test)

Example 60 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class TupleRewriterTest method testTupleGetSimplification.

@Test
public void testTupleGetSimplification() {
    Simplifier tupleGetSimplifier = new TupleGetSimplifier();
    Assert.assertEquals(parse("a"), tupleGetSimplifier.apply(parse("get((a,b,c),1)"), context));
    Assert.assertEquals(parse("b"), tupleGetSimplifier.apply(parse("get((a,b,c),2)"), context));
    Assert.assertEquals(parse("c"), tupleGetSimplifier.apply(parse("get((a,b,c),3)"), context));
    Assert.assertEquals(parse("a"), tupleGetSimplifier.apply(parse("get(tuple(a),I)"), context));
    Assert.assertEquals(parse("if I = 1 then a else b"), tupleGetSimplifier.apply(parse("get((a,b),I)"), context));
    Assert.assertEquals(parse("if I = 1 then a else if I = 2 then b else c"), tupleGetSimplifier.apply(parse("get((a,b,c),I)"), context));
    Expression expr = parse("get(I, (a,b,c))");
    Assert.assertTrue(expr == tupleGetSimplifier.apply(expr, context));
    expr = parse("get(N, 1)");
    Assert.assertTrue(expr == tupleGetSimplifier.apply(expr, context));
}
Also used : Expression(com.sri.ai.expresso.api.Expression) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) TupleEqualitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualitySimplifier) TupleGetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleQuantifierSimplifier) TupleDisequalitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleDisequalitySimplifier) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) TupleSetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleSetSimplifier) TupleGetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSimplifier) Test(org.junit.Test)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)1392 Test (org.junit.Test)259 ArrayList (java.util.ArrayList)196 Context (com.sri.ai.grinder.api.Context)187 Type (com.sri.ai.expresso.api.Type)124 TrueContext (com.sri.ai.grinder.core.TrueContext)113 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)100 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)91 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)90 Context (com.sri.ai.grinder.sgdpllt.api.Context)87 Theory (com.sri.ai.grinder.api.Theory)78 Map (java.util.Map)78 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)71 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)68 List (java.util.List)68 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)63 CommonTheory (com.sri.ai.grinder.application.CommonTheory)55 LinkedHashMap (java.util.LinkedHashMap)55 LinkedHashSet (java.util.LinkedHashSet)54 Pair (com.sri.ai.util.base.Pair)52