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