public void TestBasicTakeSkip()
        {
            var t = new TypeHandlerCache();

            MEFUtilities.Compose(t);
            GeneratedCode gc = new GeneratedCode();

            gc.Add(new LINQToTTreeLib.Statements.StatementInlineBlock());
            gc.Add(new LINQToTTreeLib.Tests.TestUtils.SimpleLoop());
            var skipper = new SkipResultOperator(Expression.Constant(10));

            ProcessResultOperator(new ROTakeSkipOperators(), skipper, null, gc);
        }
        public void TestLiftSimpleStatement()
        {
            var v = new GeneratedCode();
            var loopP = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop = new StatementForLoop(loopP, new LINQToTTreeLib.Variables.ValSimple("10", typeof(int)));
            v.Add(loop);
            v.Add(new StatementWithNoSideEffects());

            StatementLifter.Optimize(v);

            var firstStatement = v.CodeBody.Statements.First();
            Assert.IsInstanceOfType(firstStatement, typeof(StatementWithNoSideEffects), "first statement");
        }
Example #3
0
        public void TestTranslatedArray()
        {
            var baseVar = Expression.Variable(typeof(SourceType1), "d");
            var jetRef  = Expression.MakeMemberAccess(baseVar, typeof(SourceType1).GetMember("jets").First());

            ArrayInfoVector vec = new ArrayInfoVector(jetRef);

            CodeContext   cc = new CodeContext();
            GeneratedCode gc = new GeneratedCode();

            var indexVar = vec.AddLoop(gc, cc, MEFUtilities.MEFContainer);

            gc.Add(new LINQToTTreeLib.Statements.StatementSimpleStatement("dude"));

            ///
            /// Make sure the indexvar is working correctly
            ///

            Assert.IsInstanceOfType(indexVar.Item1, typeof(BinaryExpression), "inproper expression variable type");
            Assert.AreEqual(typeof(SourceType1SubType), indexVar.Item1.Type, "index var type");
            var be = indexVar.Item1 as BinaryExpression;

            Assert.AreEqual(ExpressionType.ArrayIndex, be.NodeType, "not array index");
            Assert.AreEqual(typeof(int), be.Right.Type, "Indexer of array type");
            Assert.IsInstanceOfType(be.Left, typeof(MemberExpression), "now the same paraemter, I think!");
            Assert.AreEqual(jetRef, be.Left, "array isn't right");

            ///
            /// Now, make sure we got as far as a proper size variable
            ///

            var statements = gc.CodeBody.CodeItUp().ToArray();

            Assert.IsTrue(statements[1].Contains(".val1).size()"), "size statement incorrect: '" + statements[1] + "'");
        }
 public void ObtainCode()
 {
     GeneratedCode.Add("usings", GetUsings());
     GeneratedCode.Add("classNamespace", GetClassNamespace());
     GeneratedCode.Add("superclass", GetSuperClass());
     GeneratedCode.Add("defaultConstructor", GetDefaultConstructor());
     GeneratedCode.Add("attributes", GetJMSClassAttributes());
 }
 public void ObtainCode()
 {
     GeneratedCode.Add("usings", GetUsings());
     GeneratedCode.Add("classNamespace", GetClassNamespace());
     GeneratedCode.Add("superclass", GetSuperClass());
     GeneratedCode.Add("dataContract", IsDataContractString());
     GeneratedCode.Add("attributes", GetJMSClassAttributes());
 }
 public void ObtainCode()
 {
     GeneratedCode.Add("usings", GetUsings());
     GeneratedCode.Add("classNamespace", GetClassNamespace());
     GeneratedCode.Add("superclass", GetSuperClass());
     GeneratedCode.Add("dataContract", IsDataContractString());
     GeneratedCode.Add("declarations", GetJmsJobDeclarations());
     GeneratedCode.Add("properties", GetJmsJobProperties());
     GeneratedCode.Add("jobMethods", GetJMSJobMethods());
 }
Example #7
0
 public void ObtainCode()
 {
     GeneratedCode.Add("usings", GetUsings());
     GeneratedCode.Add("classNamespace", GetClassNamespace());
     GeneratedCode.Add("superclass", GetSuperClass());
     GeneratedCode.Add("dataContract", IsDataContractString());
     GeneratedCode.Add("description", GetDescription());
     GeneratedCode.Add("defaultConstructor", GetDefaultConstructor());
     GeneratedCode.Add("attributes", GetJMSClassAttributes());
 }
            /// <summary>
            /// We only support a sub-class of expressions for now - so we'd better make sure we are protected!
            /// </summary>
            /// <param name="expression"></param>
            /// <returns></returns>
            protected override Expression VisitConditional(ConditionalExpression expression)
            {
                // We can support complex sub-expressions so long as they don't leak out of the
                // comparison.
                if (expression.Type.IsClass &&
                    (
                        CheckForSubQueries.CheckExpression(expression.IfFalse) ||
                        CheckForSubQueries.CheckExpression(expression.IfTrue))
                    )
                {
                    throw new NotSupportedException(string.Format("Complex true/false clauses in a conditional expression are not supported: '{0}'", expression.ToString()));
                }

                // If this is a class as a result, then we can't do much extra processing here. So skip.
                if (expression.Type.IsClass)
                {
                    return(base.VisitConditional(expression));
                }

                // Run the code for the test, and then create the if/then/else that will support it.
                var testExpression           = base.Visit(expression.Test);
                var testExpressionEvaluation = ExpressionToCPP.GetExpression(testExpression, GeneratedCode, CodeContext, MEFContainer);
                var testBoolInCode           = testExpressionEvaluation is DeclarableParameter p ? p : DeclarableParameter.CreateDeclarableParameterExpression(typeof(bool));

                if (testBoolInCode != testExpressionEvaluation)
                {
                    GeneratedCode.Add(testBoolInCode);
                    GeneratedCode.Add(new Statements.StatementAssign(testBoolInCode, testExpressionEvaluation));
                }

                // The result
                var conditionalResult = DeclarableParameter.CreateDeclarableParameterExpression(expression.Type);

                GeneratedCode.Add(conditionalResult);

                // Do the if true statement
                var topScope = GeneratedCode.CurrentScope;

                GeneratedCode.Add(new Statements.StatementFilter(testBoolInCode));
                var iftrueExpression = Visit(expression.IfTrue);

                GeneratedCode.Add(new Statements.StatementAssign(conditionalResult, ExpressionToCPP.GetExpression(iftrueExpression, GeneratedCode, CodeContext, MEFContainer)));
                GeneratedCode.CurrentScope = topScope;

                // Do the if false statement
                GeneratedCode.Add(new Statements.StatementFilter(ExpressionToCPP.GetExpression(Expression.Not(testBoolInCode), GeneratedCode, CodeContext, MEFContainer)));
                var ifFalseExpression = Visit(expression.IfFalse);

                GeneratedCode.Add(new Statements.StatementAssign(conditionalResult, ExpressionToCPP.GetExpression(ifFalseExpression, GeneratedCode, CodeContext, MEFContainer)));
                GeneratedCode.CurrentScope = topScope;

                // Consider this expression now transformed, so return the result, not
                // the conditional expression itself.
                return(conditionalResult);
            }
        public void TestLiftSimpleStatementInFunction()
        {
            var v = new StatementInlineBlock();
            var loopP = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop = new StatementForLoop(loopP, new LINQToTTreeLib.Variables.ValSimple("10", typeof(int)));
            v.Add(loop);
            loop.Add(new StatementWithNoSideEffects());

            var f = QMFunctions.QMFuncUtils.GenerateFunction();
            f.SetCodeBody(v);

            var gc = new GeneratedCode();
            gc.Add(new StatementSimpleStatement("int i = 10;"));
            gc.Add(f);
            StatementLifter.Optimize(gc);

            Assert.AreEqual(1, gc.Functions.Count(), "# of functions after lifting");
            var firstStatement = gc.Functions.First().StatementBlock.Statements.First();
            Assert.IsInstanceOfType(firstStatement, typeof(StatementWithNoSideEffects), "first statement");

        }
Example #10
0
        public void TestSimpleArray()
        {
            var             simpleArrayExpr = Expression.Variable(typeof(int[]), "d");
            ArrayInfoVector vec             = new ArrayInfoVector(simpleArrayExpr);

            CodeContext   cc = new CodeContext();
            GeneratedCode gc = new GeneratedCode();

            var indexVar = vec.AddLoop(gc, cc, MEFUtilities.MEFContainer);

            ///
            /// Add a dumb statement to force the rendering of the loop (empty loops don't render)
            ///

            gc.Add(new LINQToTTreeLib.Statements.StatementSimpleStatement("d = d"));

            ///
            /// Make sure the index variable comes back correctly
            ///

            Assert.IsInstanceOfType(indexVar.Item1, typeof(BinaryExpression), "inproper expression variable type");
            Assert.AreEqual(typeof(int), indexVar.Item1.Type, "bad value type");
            var be = indexVar.Item1 as BinaryExpression;

            Assert.AreEqual(ExpressionType.ArrayIndex, be.NodeType, "not array index");
            Assert.AreEqual(typeof(int), be.Right.Type, "Indexer of array type");
            Assert.IsInstanceOfType(be.Left, typeof(ParameterExpression), "now the same paraemter, I think!");
            Assert.AreEqual(simpleArrayExpr, be.Left, "array isn't right");

            Assert.AreEqual(typeof(int), indexVar.Item2.Type, "Bad index variable");

            ///
            /// Next, we need to look at the statements that have come back
            ///

            var statements = gc.CodeBody.CodeItUp().ToArray();

            Assert.AreEqual("{", statements[0], "open brace");
            Assert.IsTrue(statements[1].Contains("int"), "statement 1 - int: '" + statements[1] + "'");
            Assert.IsTrue(statements[1].Contains("size();"), "statement 2 - x = 0;: '" + statements[2] + "'");
            Assert.IsTrue(statements[2].StartsWith("  for (int "), "statement 3 - for (): '" + statements[3] + "'");
            Assert.AreEqual("  {", statements[3], "for loop brace opening");
            Assert.AreEqual("    d = d;", statements[4], "the actual statement");
        }
        public void TestSimpleArray()
        {
            var simpleArrayExpr = Expression.Variable(typeof(int[]), "d");
            ArrayInfoVector vec = new ArrayInfoVector(simpleArrayExpr);

            CodeContext cc = new CodeContext();
            GeneratedCode gc = new GeneratedCode();

            var indexVar = vec.AddLoop(gc, cc, MEFUtilities.MEFContainer);

            ///
            /// Add a dumb statement to force the rendering of the loop (empty loops don't render)
            /// 

            gc.Add(new LINQToTTreeLib.Statements.StatementSimpleStatement("d = d"));

            ///
            /// Make sure the index variable comes back correctly
            /// 

            Assert.IsInstanceOfType(indexVar.Item1, typeof(BinaryExpression), "inproper expression variable type");
            Assert.AreEqual(typeof(int), indexVar.Item1.Type, "bad value type");
            var be = indexVar.Item1 as BinaryExpression;
            Assert.AreEqual(ExpressionType.ArrayIndex, be.NodeType, "not array index");
            Assert.AreEqual(typeof(int), be.Right.Type, "Indexer of array type");
            Assert.IsInstanceOfType(be.Left, typeof(ParameterExpression), "now the same paraemter, I think!");
            Assert.AreEqual(simpleArrayExpr, be.Left, "array isn't right");

            Assert.AreEqual(typeof(int), indexVar.Item2.Type, "Bad index variable");

            ///
            /// Next, we need to look at the statements that have come back
            /// 

            var statements = gc.CodeBody.CodeItUp().ToArray();

            Assert.AreEqual("{", statements[0], "open brace");
            Assert.IsTrue(statements[1].Contains("int"), "statement 1 - int: '" + statements[1] + "'");
            Assert.IsTrue(statements[1].Contains("size();"), "statement 2 - x = 0;: '" + statements[2] + "'");
            Assert.IsTrue(statements[2].StartsWith("  for (int "), "statement 3 - for (): '" + statements[3] + "'");
            Assert.AreEqual("  {", statements[3], "for loop brace opening");
            Assert.AreEqual("    d = d;", statements[4], "the actual statement");
        }
        /// <summary>Test stub for ProcessResultOperator(ResultOperatorBase, QueryModel, IGeneratedCode)</summary>
        internal GeneratedCode ProcessResultOperator(
            ROTakeSkipOperators target,
            ResultOperatorBase resultOperator,
            QueryModel queryModel,
            GeneratedCode codeEnv
        )
        {
            if (codeEnv.ResultValue != null)
                throw new ArgumentException("this should not be null for this test");
            if (codeEnv.CodeBody.DeclaredVariables == null)
                throw new ArgumentException("Need this declare variables to be defined");

            ///
            /// We always expect to be inside a loop - and depend on it for doing our declares, so add something...
            /// 

            var inlineBlock = new StatementInlineBlock();
            codeEnv.Add(inlineBlock);

            ///
            /// Get the environment setup and run it
            /// 

            CodeContext c = new CodeContext();
            c.SetLoopVariable(Expression.Variable(typeof(int), "d"), null);

            target.ProcessResultOperator(resultOperator, queryModel, codeEnv, c, MEFUtilities.MEFContainer);

            codeEnv.DumpCodeToConsole();

            ///
            /// First, there should be a counter now declared and ready to go in the current variable block - which will
            /// be the outer one for this test. If this is the outter most, then this is going to be burried.
            /// 

            var declBlock = inlineBlock.Parent.Parent as IBookingStatementBlock;
            Assert.IsNotNull(declBlock, "Expecting a declaration block above!");

            Assert.AreEqual(1, inlineBlock.Statements.Count(), "Expected an if block/increment!");
            Assert.IsInstanceOfType(inlineBlock.Statements.First(), typeof(StatementIfOnCount), "if statement not found!");

            var s = inlineBlock.Statements.First() as StatementIfOnCount;

            bool isTopLevel = codeEnv.DumpCode().Where(l => l.Contains("static int")).Any();
            if (!isTopLevel)
            {
                Assert.AreEqual(1, declBlock.DeclaredVariables.Count(), "Expected only 1 variable to be declared");
                Assert.IsInstanceOfType(declBlock.DeclaredVariables.First(), typeof(DeclarableParameter), "Expected it to be a counter");
            } else
            {
                Assert.AreEqual(1, (s.Parent as IBookingStatementBlock).DeclaredVariables.Count());
            }

            string count = "";
            if (resultOperator is SkipResultOperator)
            {
                count = (resultOperator as SkipResultOperator).Count.ToString();
            }
            else if (resultOperator is TakeResultOperator)
            {
                count = (resultOperator as TakeResultOperator).Count.ToString();
            }
            Assert.AreEqual(count, s.Limit.RawValue, "bad count made it through");

            ///
            /// Finally, the current loop variable should be identical, and there should be no result set.
            /// 

            Assert.IsNull(codeEnv.ResultValue, "result value");
            Assert.IsInstanceOfType(c.LoopVariable, typeof(ParameterExpression), "loop variable type");
            var lv = c.LoopVariable as ParameterExpression;
            Assert.AreEqual("d", lv.Name, "loop variable name");

            //
            // Dump everything and return. To force it out, add a dummy statement
            // (because if statements, etc., are smart enough to not print anything if they
            // are empty).
            //

            codeEnv.Add(new StatementSimpleStatement("fork = left"));
            codeEnv.DumpCodeToConsole();

            return codeEnv;
        }
        public void TestTranslatedArray()
        {
            var baseVar = Expression.Variable(typeof(SourceType1), "d");
            var jetRef = Expression.MakeMemberAccess(baseVar, typeof(SourceType1).GetMember("jets").First());

            ArrayInfoVector vec = new ArrayInfoVector(jetRef);

            CodeContext cc = new CodeContext();
            GeneratedCode gc = new GeneratedCode();

            var indexVar = vec.AddLoop(gc, cc, MEFUtilities.MEFContainer);
            gc.Add(new LINQToTTreeLib.Statements.StatementSimpleStatement("dude"));

            ///
            /// Make sure the indexvar is working correctly
            /// 

            Assert.IsInstanceOfType(indexVar.Item1, typeof(BinaryExpression), "inproper expression variable type");
            Assert.AreEqual(typeof(SourceType1SubType), indexVar.Item1.Type, "index var type");
            var be = indexVar.Item1 as BinaryExpression;
            Assert.AreEqual(ExpressionType.ArrayIndex, be.NodeType, "not array index");
            Assert.AreEqual(typeof(int), be.Right.Type, "Indexer of array type");
            Assert.IsInstanceOfType(be.Left, typeof(MemberExpression), "now the same paraemter, I think!");
            Assert.AreEqual(jetRef, be.Left, "array isn't right");

            ///
            /// Now, make sure we got as far as a proper size variable
            /// 

            var statements = gc.CodeBody.CodeItUp().ToArray();
            Assert.IsTrue(statements[1].Contains(".val1).size()"), "size statement incorrect: '" + statements[1] + "'");
        }
        public void TestLiftTwoDependentStatements()
        {
            var v = new GeneratedCode();
            var loopP = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop = new StatementForLoop(loopP, new LINQToTTreeLib.Variables.ValSimple("10", typeof(int)));
            v.Add(loop);

            var var1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var var2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));

            v.Add(new StatementWithSideEffects(var1, var2));
            v.Add(new StatementWithSideEffects(var2));

            StatementLifter.Optimize(v);

            var firstStatement = v.CodeBody.Statements.First();
            Assert.IsInstanceOfType(firstStatement, typeof(StatementWithSideEffects), "first statement");
            var secondStatement = v.CodeBody.Statements.Skip(1).First();
            Assert.IsInstanceOfType(firstStatement, typeof(StatementWithSideEffects), "second statement");
            var thirdstatement = v.CodeBody.Statements.Skip(2).First();
            Assert.IsInstanceOfType(thirdstatement, typeof(StatementForLoop), "third statement");
        }
 public void TestBasicTakeSkip()
 {
     var t = new TypeHandlerCache();
     MEFUtilities.Compose(t);
     GeneratedCode gc = new GeneratedCode();
     gc.Add(new LINQToTTreeLib.Statements.StatementInlineBlock());
     gc.Add(new LINQToTTreeLib.Tests.TestUtils.SimpleLoop());
     var skipper = new SkipResultOperator(Expression.Constant(10));
     ProcessResultOperator(new ROTakeSkipOperators(), skipper, null, gc);
 }
        public void TestNoLiftPastNoMoveCompound()
        {
            var v = new GeneratedCode();

            v.Add(new StatementWithNoSideEffects());

            v.Add(new DummyStatementCompoundNoCMInfo());
            v.Add(new StatementWithNoSideEffects());

            StatementLifter.Optimize(v);

            var firstStatement = v.CodeBody.Statements.First();
            Assert.IsInstanceOfType(firstStatement, typeof(StatementWithNoSideEffects), "first statement");
            var secondStatement = v.CodeBody.Statements.Skip(1).First();
            Assert.IsInstanceOfType(secondStatement, typeof(DummyStatementCompoundNoCMInfo), "Second statement");
        }
        public void LiftLoopInvarient()
        {
            var v = new GeneratedCode();

            var limit = new LINQToTTreeLib.Variables.ValSimple("5", typeof(int));
            var loopP1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop1 = new StatementForLoop(loopP1, limit);
            v.Add(loop1);

            var p2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var assign1 = new StatementAssign(p2, new ValSimple("f", typeof(int)));
            loop1.Add(p2);
            loop1.Add(assign1);

            Console.WriteLine("Unoptimized:");
            v.DumpCodeToConsole();

            StatementLifter.Optimize(v);

            Console.WriteLine("");
            Console.WriteLine("Optimized:");
            v.DumpCodeToConsole();

            Assert.AreEqual(0, loop1.Statements.Count());
        }
        /// <summary>
        /// Create a new declarable parameter that will sum these two things.
        /// </summary>
        /// <param name="gc"></param>
        /// <param name="c1"></param>
        /// <param name="c2"></param>
        public static IDeclaredParameter AddSum(GeneratedCode gc, IDeclaredParameter c1, IDeclaredParameter c2)
        {
            var r = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));

            gc.Add(new StatementAssign(r, new ValSimple($"{c1.RawValue}+{c2.RawValue}", c1.Type, new IDeclaredParameter[] { c1, c2 })));

            return r;
        }
 /// <summary>
 /// Add a simple if statement.
 /// </summary>
 /// <param name="gc"></param>
 public static void AddIf(GeneratedCode gc)
 {
     gc.Add(new StatementFilter(new ValSimple("5>10", typeof(bool))));
 }
        public void LiftNoSideEffectFromNestedIdenticalLoops()
        {
            var v = new GeneratedCode();

            var limit = new LINQToTTreeLib.Variables.ValSimple("10", typeof(int));
            var loopP1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop1 = new StatementForLoop(loopP1, limit);
            v.Add(loop1);
            var loopP2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop2 = new StatementForLoop(loopP2, limit);
            v.Add(loop2);
            v.Add(new StatementWithNoSideEffects());

            Console.WriteLine("Unoptimized:");
            v.DumpCodeToConsole();
            StatementLifter.Optimize(v);
            Console.WriteLine("");
            Console.WriteLine("Optimized:");
            v.DumpCodeToConsole();

            // Check to see if it got lifted.
            Assert.AreEqual(1, v.CodeBody.Statements.WhereCast<IStatement, StatementWithNoSideEffects>().Count(), "#of no side effect statements");
        }
        public void TestLiftTwoStatements()
        {
            var v = new GeneratedCode();
            var loopP = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop = new StatementForLoop(loopP, new LINQToTTreeLib.Variables.ValSimple("10", typeof(int)));
            v.Add(loop);
            v.Add(new StatementWithSideEffects(loopP));
            v.Add(new StatementWithNoSideEffects());
            v.Add(new StatementWithNoSideEffects());

            Console.WriteLine("Before optimization");
            v.DumpCodeToConsole();

            StatementLifter.Optimize(v);
            Console.WriteLine("After optimization");
            v.DumpCodeToConsole();

            var firstStatement = v.CodeBody.Statements.First();
            Assert.IsInstanceOfType(firstStatement, typeof(StatementWithNoSideEffects), "first statement");
            var thirdstatement = v.CodeBody.Statements.Skip(1).First();
            Assert.IsInstanceOfType(thirdstatement, typeof(StatementForLoop), "third statement");
        }
        public void TestLoopBelowNonLoopBlockNoSideEffects()
        {
            var v = new GeneratedCode();

            v.Add(new StatementNonOptimizing());
            v.Add(new StatementInlineBlock());
            var loopP = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop = new StatementForLoop(loopP, new LINQToTTreeLib.Variables.ValSimple("10", typeof(int)));
            v.Add(loop);

            v.Add(new StatementWithNoSideEffects());

            DoOptimizeTest(v);

            var firstStatement = v.CodeBody.Statements.First();
            Assert.IsInstanceOfType(firstStatement, typeof(StatementNonOptimizing), "first statement");
            var secondStatement = v.CodeBody.Statements.Skip(1).First();
            Assert.IsInstanceOfType(secondStatement, typeof(StatementWithNoSideEffects), "Second statement");
        }
        public void TwoSimilarAssignStatementsWithSimpleStatement()
        {
            var gc = new GeneratedCode();

            var s1 = AddSimpleAssign(gc);
            var s2 = AddSimpleAssign(gc);
            gc.Add(new StatementSimpleStatement("dude"));

            DoOptimizeTest(gc);

            Assert.AreEqual(2, gc.CodeBody.Statements.Count(), "# of statements");
            Assert.AreEqual(1, gc.CodeBody.DeclaredVariables.Count(), "# of vars");
        }
        public void IdenticalFiltersWithNestedIfAndElsesNested()
        {
            var gc = new GeneratedCode();

            var test = DeclarableParameter.CreateDeclarableParameterExpression("mt", typeof(bool));

            AddConditionalExpr(gc, doElseClause: false, ifStatementTest: test);
            AddLocalInteriorIfAndElse(gc, gc.CodeBody.Statements.Take(1).Cast<StatementFilter>().First());

            var loopP = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop = new StatementForLoop(loopP, new LINQToTTreeLib.Variables.ValSimple("10", typeof(int)));
            gc.Add(loop);

            //var test2 = DeclarableParameter.CreateDeclarableParameterExpression("test2", typeof(bool));
            //var hiderif = new StatementFilter(test2);
            //gc.Add(hiderif);

            AddConditionalExpr(gc, doElseClause: false, ifStatementTest: test);
            AddLocalInteriorIfAndElse(gc, loop.Statements.Take(1).Cast<StatementFilter>().First());

            DoOptimizeTest(gc);

            Assert.AreEqual(2, gc.CodeBody.DeclaredVariables.Count());
            Assert.AreEqual(2, gc.CodeBody.Statements.Count());
            Assert.AreEqual(0, loop.Statements.Count());
        }
Example #25
0
        private void CommonExecute(QueryModel queryModel)
        {
            LastQueryModel = queryModel;

            Result = new GeneratedCode();
            if (DoQMFunctions)
            {
                foreach (var f in LINQToTTreeLib.QMFunctions.QMFuncFinder.FindQMFunctions(queryModel))
                {
                    if (!f.Arguments.Any())
                    {
                        Result.Add(new QMFuncSource(f));
                    }
                }
            }

            if (!_doExecution)
            {
                return;
            }

            if (!GlobalInitalized)
            {
                GlobalInitalized = true;
                MEFUtilities.AddPart(new QVResultOperators());
                MEFUtilities.AddPart(new ROCount());
                MEFUtilities.AddPart(new ROTakeSkipOperators());
                MEFUtilities.AddPart(new ROAggregate());
                MEFUtilities.AddPart(new ROMinMax());
                MEFUtilities.AddPart(new ROAnyAll());
                MEFUtilities.AddPart(new ROUniqueCombinations());
                MEFUtilities.AddPart(new ROAsCSV());
                MEFUtilities.AddPart(new ROAsTTree());
                MEFUtilities.AddPart(new ROPairWiseAll());
                MEFUtilities.AddPart(new ROAsQueriable());
                MEFUtilities.AddPart(new ROSum());
                MEFUtilities.AddPart(new ROFirstLast());
                MEFUtilities.AddPart(new ROGroup());

                MEFUtilities.AddPart(new TypeHandlerROOT());
                MEFUtilities.AddPart(new TypeHandlerHelpers());
                MEFUtilities.AddPart(new TypeHandlerReplacementCall());
                MEFUtilities.AddPart(new TypeHandlerCache());
                MEFUtilities.AddPart(new TypeHandlerCPPCode());
                MEFUtilities.AddPart(new TypeHandlerOnTheFlyCPP());
                MEFUtilities.AddPart(new TypeHandlerTranslationClass());
                MEFUtilities.AddPart(new TypeHandlerConvert());
                MEFUtilities.AddPart(new GroupByArrayFactory());
                MEFUtilities.AddPart(new GroupByFactory());

                MEFUtilities.AddPart(new ArrayArrayInfoFactory());
                MEFUtilities.AddPart(new EnumerableRangeArrayTypeFactory());
                MEFUtilities.AddPart(new SubQueryArrayTypeFactory());
                MEFUtilities.AddPart(new TranslatedArrayInfoFactory());
                MEFUtilities.AddPart(new MemberAccessArrayTypeFactory());
                MEFUtilities.AddPart(new HandleGroupType());
                MEFUtilities.AddPart(new SubQueryExpressionArrayInfoFactory());

                //var b = new CompositionBatch();
                //b.AddPart(new DataSchemeHandlerFile());
                //b.AddPart(new DataSchemeHandlerRemoteBash());
                //b.AddPart(new DataSchemeHandlerLocalBash());
                //b.AddPart(new DataSchemeHandlerLocalWin());
                //b.AddPart(new DataSchemeHandlerProof());
                //b.AddPart(new CommandLineExecutorFactory());
                //b.AddPart(new LocalBashExecutorFactory());
                //b.AddPart(new RemoteBashExecutorFactory());
                //TTreeQueryExecutor.CContainer.Compose(b);

                MEFUtilities.AddPart(new AdderInt());
            }

            var cc = new CodeContext()
            {
                BaseNtupleObjectType = _baseType
            };

            cc.SetTopLevelQuery(queryModel);
            var qv = new QueryVisitor(Result, cc, MEFUtilities.MEFContainer);

            MEFUtilities.Compose(qv);

            MEFInitialPartCount = MEFUtilities.CountParts();

            qv.VisitQueryModel(queryModel);

            FinalResult = Result;
        }
        /// <summary>
        /// Add a simple loop to the current scope. It will have one statement in it, declared at the outer level.
        /// </summary>
        /// <param name="gc"></param>
        public static IDeclaredParameter AddLoop(GeneratedCode gc,
            bool addDependentStatement = false,
            IDeclaredParameter useCounter = null,
            MainStatementType mainStatementType = MainStatementType.IsCounter,
            bool defineCounterInsideBlock = false)
        {
            if (mainStatementType != MainStatementType.IsCounter && useCounter != null)
            {
                throw new ArgumentException("Can't do a counter when main statement type doesn't use a counter");
            }

            // Add a counter that gets... counted.
            var counter = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            if (!defineCounterInsideBlock)
            {
                gc.Add(counter);
            }

            // And an extra variable that is defined outside the loop
            var counterExtra = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            if (addDependentStatement)
            {
                gc.Add(counterExtra);
            }

            // Now do the loop.
            var loopVar = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop = new StatementForLoop(loopVar, new ValSimple("5", typeof(int)));
            gc.Add(loop);
            if (defineCounterInsideBlock)
            {
                gc.Add(counter);
            }

            // Now add statements to the loop
            if (mainStatementType == MainStatementType.IsCounter)
            {
                if (useCounter == null)
                {
                    gc.Add(new StatementAssign(counter, new ValSimple($"{counter.RawValue} + 1", typeof(int), new IDeclaredParameter[] { counter })));
                }
                else
                {
                    gc.Add(new StatementAssign(counter, new ValSimple($"{counter.RawValue}+{useCounter.RawValue}", typeof(int), new IDeclaredParameter[] { counter, useCounter })));
                }
            } else
            {
                gc.Add(new StatementAssign(counter, new ValSimple("1", typeof(int))));
            }

            if (addDependentStatement)
            {
                gc.Add(new StatementAssign(counterExtra, new ValSimple($"{counterExtra.RawValue}+{counter.RawValue}", typeof(int), new IDeclaredParameter[] { counterExtra, counter })));
            }

            return counter;
        }
        public void CombinationKilledByICMPStatement()
        {
            var gc = new GeneratedCode();

            var s1 = AddSimpleAssign(gc);
            var s2 = AddSimpleAssign(gc);
            gc.Add(new StatementNonOptimizing());

            DoOptimizeTest(gc);

            Assert.AreEqual(2, gc.CodeBody.Statements.Count(), "# of statements");
            Assert.AreEqual(1, gc.CodeBody.DeclaredVariables.Count(), "# of vars");
        }
        public void TestLoopBelowSortedLoopBlockSideEffectsNotImportant()
        {
            var v = new GeneratedCode();

            v.Add(new StatementNonOptimizing());
            var dict = DeclarableParameter.CreateDeclarableParameterMapExpression(typeof(int), typeof(int[]));
            v.Add(new StatementLoopOverSortedPairValue(dict, true));
            var loopP = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop = new StatementForLoop(loopP, new LINQToTTreeLib.Variables.ValSimple("10", typeof(int)));
            v.Add(loop);

            var lnp = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            v.Add(new StatementWithSideEffects(lnp));

            StatementLifter.Optimize(v);

            var firstStatement = v.CodeBody.Statements.First();
            Assert.IsInstanceOfType(firstStatement, typeof(StatementNonOptimizing), "first statement");
            var secondStatement = v.CodeBody.Statements.Skip(1).First();
            Assert.IsInstanceOfType(secondStatement, typeof(StatementLoopOverSortedPairValue), "Second statement");
        }
        /// <summary>Test stub for ProcessResultOperator(ResultOperatorBase, QueryModel, IGeneratedCode)</summary>
        internal GeneratedCode ProcessResultOperator(
            ROTakeSkipOperators target,
            ResultOperatorBase resultOperator,
            QueryModel queryModel,
            GeneratedCode codeEnv
            )
        {
            if (codeEnv.ResultValue != null)
            {
                throw new ArgumentException("this should not be null for this test");
            }
            if (codeEnv.CodeBody.DeclaredVariables == null)
            {
                throw new ArgumentException("Need this declare variables to be defined");
            }

            ///
            /// We always expect to be inside a loop - and depend on it for doing our declares, so add something...
            ///

            var inlineBlock = new StatementInlineBlock();

            codeEnv.Add(inlineBlock);

            ///
            /// Get the environment setup and run it
            ///

            CodeContext c = new CodeContext();

            c.SetLoopVariable(Expression.Variable(typeof(int), "d"), null);

            target.ProcessResultOperator(resultOperator, queryModel, codeEnv, c, MEFUtilities.MEFContainer);

            codeEnv.DumpCodeToConsole();

            ///
            /// First, there should be a counter now declared and ready to go in the current variable block - which will
            /// be the outer one for this test. If this is the outter most, then this is going to be burried.
            ///

            var declBlock = inlineBlock.Parent.Parent as IBookingStatementBlock;

            Assert.IsNotNull(declBlock, "Expecting a declaration block above!");

            Assert.AreEqual(1, inlineBlock.Statements.Count(), "Expected an if block/increment!");
            Assert.IsInstanceOfType(inlineBlock.Statements.First(), typeof(StatementIfOnCount), "if statement not found!");

            var s = inlineBlock.Statements.First() as StatementIfOnCount;

            bool isTopLevel = codeEnv.DumpCode().Where(l => l.Contains("static int")).Any();

            if (!isTopLevel)
            {
                Assert.AreEqual(1, declBlock.DeclaredVariables.Count(), "Expected only 1 variable to be declared");
                Assert.IsInstanceOfType(declBlock.DeclaredVariables.First(), typeof(DeclarableParameter), "Expected it to be a counter");
            }
            else
            {
                Assert.AreEqual(1, (s.Parent as IBookingStatementBlock).DeclaredVariables.Count());
            }

            string count = "";

            if (resultOperator is SkipResultOperator)
            {
                count = (resultOperator as SkipResultOperator).Count.ToString();
            }
            else if (resultOperator is TakeResultOperator)
            {
                count = (resultOperator as TakeResultOperator).Count.ToString();
            }
            Assert.AreEqual(count, s.Limit.RawValue, "bad count made it through");

            ///
            /// Finally, the current loop variable should be identical, and there should be no result set.
            ///

            Assert.IsNull(codeEnv.ResultValue, "result value");
            Assert.IsInstanceOfType(c.LoopVariable, typeof(ParameterExpression), "loop variable type");
            var lv = c.LoopVariable as ParameterExpression;

            Assert.AreEqual("d", lv.Name, "loop variable name");

            //
            // Dump everything and return. To force it out, add a dummy statement
            // (because if statements, etc., are smart enough to not print anything if they
            // are empty).
            //

            codeEnv.Add(new StatementSimpleStatement("fork = left"));
            codeEnv.DumpCodeToConsole();

            return(codeEnv);
        }
        public void DontLiftThroughTwoForStatements()
        {
            var gc = new GeneratedCode();

            var counter = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            counter.InitialValue = new ValSimple("0", typeof(int));
            gc.Add(counter);

            // The two for loops
            var fc1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var for1 = new StatementForLoop(fc1, new ValSimple("5", typeof(int)));
            gc.Add(for1);

            var innerCounter = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            innerCounter.InitialValue = new ValSimple("0", typeof(int));
            gc.Add(innerCounter);

            var fc2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var for2 = new StatementForLoop(fc2, new ValSimple("5", typeof(int)));
            gc.Add(for2);

            // Now, calculation based only on fc1
            var a1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var ass1 = new StatementAssign(a1, new ValSimple($"{fc1}*2", typeof(int), new IDeclaredParameter[] { fc1 }));
            gc.Add(ass1);
            var agg1 = new StatementAggregate(innerCounter, new ValSimple($"{innerCounter.RawValue}+{a1.RawValue}", typeof(int), new IDeclaredParameter[] { innerCounter, a1 }));
            gc.Add(agg1);

            // and the outer sum.
            gc.Pop();
            var agg2 = new StatementAggregate(counter, new ValSimple($"{counter.RawValue}+{innerCounter.RawValue}", typeof(int), new IDeclaredParameter[] { counter, innerCounter }));
            gc.Add(agg2);

            // Great!
            Console.WriteLine("Unoptimized");
            gc.DumpCodeToConsole();

            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");

            StatementLifter.Optimize(gc);

            Console.WriteLine("Optimized");
            Console.WriteLine("");
            gc.DumpCodeToConsole();

            // Make sure the inner aggregate got lifted out properly.
            Assert.AreEqual(1, for2.Statements.Count(), "# of statements in the inner for loop");
        }