public void TestSimpleAddition() { MEFUtilities.Compose(new TypeHandlers.TypeHandlerCache()); AggregateFromSeedResultOperator agg = new AggregateFromSeedResultOperator(Expression.Constant(1), Expression.Lambda(Expression.MakeBinary(ExpressionType.Add, Expression.Parameter(typeof(int), "count"), Expression.Constant(1)), Expression.Parameter(typeof(int), "count")), null); ROAggregate processor = new ROAggregate(); GeneratedCode gc = new GeneratedCode(); var result = ProcessResultOperator(processor, agg, null, gc); Assert.AreEqual(typeof(int), result.Type, "Expected the type to be an integer!"); Assert.IsInstanceOfType(result, typeof(DeclarableParameter), "Expected a var simple!"); var vs = result as DeclarableParameter; Assert.AreEqual("1", vs.InitialValue.RawValue, "Incorrect seed value"); /// /// Now make sure the statements came back ok! /// gc.DumpCodeToConsole(); Assert.AreEqual(0, gc.CodeBody.DeclaredVariables.Count(), "Expected no bookings"); Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "expected a statement!"); Assert.IsInstanceOfType(gc.CodeBody.Statements.First(), typeof(Statements.StatementAggregate), "expected an assignment statement!"); var ass = gc.CodeBody.Statements.First() as Statements.StatementAggregate; StringBuilder bld = new StringBuilder(); bld.AppendFormat("{0}+1", ass.ResultVariable.ParameterName); Assert.AreEqual(bld.ToString(), ass.Expression.RawValue, "the raw value of hte expression is not right"); }
public void GetConstantSeed_NoConstantExpression() { var resultOperator = new AggregateFromSeedResultOperator( new QuerySourceReferenceExpression(ExpressionHelper.CreateMainFromClause_Int()), _func, _resultSelector); resultOperator.GetConstantSeed <object> (); }
/// <summary> /// Deal with the aggregate operator coming in here. /// </summary> /// <param name="resultOperator"></param> /// <param name="queryModel"></param> /// <param name="_codeEnv"></param> /// <returns></returns> public Expression ProcessResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, IGeneratedQueryCode _codeEnv, ICodeContext context, CompositionContainer container) { /// /// Basic code checks /// AggregateFromSeedResultOperator a = resultOperator as AggregateFromSeedResultOperator; if (a == null) { throw new ArgumentNullException("result Operator must not be null and must be of type AggregateFromSeedResultOperator!"); } if (a.Func.Parameters.Count != 1) { throw new InvalidOperationException("Aggregate only allows for a function with one parameters!"); } if (a.OptionalResultSelector != null) { throw new NotImplementedException("Can't do a selector function yet"); } // We need to declare a variable to hold the seed and its updates - the accumulator // We then need to write the code that does the update to the seed. // Finally, if there is a final function, we need to call that after the loop is done! var accumulator = DeclarableParameter.CreateDeclarableParameterExpression(a.Seed.Type); var newGC = new GeneratedCode(blockShouldBeBraced: false); var newCC = new CodeContext(); accumulator.InitialValue = ExpressionToCPP.GetExpression(a.Seed, newGC, newCC, container); if (newGC.CodeBody.Statements.Count() > 0) { accumulator.InitialValueCode = newGC; } _codeEnv.QueueForTransferFromGC(newGC); /// /// Now, parse the lambda expression, doing a substitution with this guy! Note that the only argument is our /// accumulator - the other arguments have all been replaced with subqueryexpressions and the like! /// var p1 = context.Add(a.Func.Parameters[0].Name, accumulator); var funcResolved = ExpressionToCPP.GetExpression(a.Func.Body, _codeEnv, context, container); p1.Pop(); if (accumulator.RawValue != funcResolved.RawValue) { _codeEnv.Add(new Statements.StatementAggregate(accumulator, funcResolved)); } return(accumulator); }
public void TestWithAgPostSelector() { Expression <Func <int, int> > selector = cnt => cnt * 2; AggregateFromSeedResultOperator agg = new AggregateFromSeedResultOperator(Expression.Constant(1), Expression.Lambda(Expression.MakeBinary(ExpressionType.Add, Expression.Parameter(typeof(int), "count"), Expression.Constant(1)), Expression.Parameter(typeof(int), "count")), selector); ROAggregate processor = new ROAggregate(); GeneratedCode gc = new GeneratedCode(); var result = ProcessResultOperator(processor, agg, null, gc); }
private T ExecuteSeedAggregate <T>(QueryModel queryModel, AggregateFromSeedResultOperator seedAggregate) { var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildAggregateProjector(queryModel, seedAggregate.Func); var resultItems = LoadData(queryModel); var result = seedAggregate.GetConstantSeed <T>(); foreach (var item in resultItems) { result = projector(item)(result); } return(result); }
/// Pex seems to hang when it tries to explore this one. ///[PexMethod] public Expression ProcessResultOperator( ROAggregate target, AggregateFromSeedResultOperator resultOperator, QueryModel queryModel, IGeneratedQueryCode _codeEnv ) { CodeContext c = new CodeContext(); Expression result = target.ProcessResultOperator(resultOperator, queryModel, _codeEnv, c, MEFUtilities.MEFContainer); return result; // TODO: add assertions to method ROAggregateTest.ProcessResultOperator(ROAggregate, ResultOperatorBase, QueryModel, IGeneratedCode) }
public void SetUp () { _querySource = ExpressionHelper.CreateMainFromClause_Int (); _sourceExpression = new QuerySourceReferenceExpression (_querySource); var originalFunc = ExpressionHelper.CreateLambdaExpression<int, int, int> ((total, i) => total + i); _func = Expression.Lambda ( ReplacingExpressionTreeVisitor.Replace (originalFunc.Parameters[1], _sourceExpression, originalFunc.Body), originalFunc.Parameters[0]); _resultSelector = ExpressionHelper.CreateLambdaExpression<int, string> (total => total.ToString ()); _seed = Expression.Constant (12); _resultOperatorWithoutResultSelector = new AggregateFromSeedResultOperator (_seed, _func, null); _resultOperatorWithResultSelector = new AggregateFromSeedResultOperator (_seed, _func, _resultSelector); }
public void SetUp() { _querySource = ExpressionHelper.CreateMainFromClause_Int(); _sourceExpression = new QuerySourceReferenceExpression(_querySource); var originalFunc = ExpressionHelper.CreateLambdaExpression <int, int, int> ((total, i) => total + i); _func = Expression.Lambda( ReplacingExpressionTreeVisitor.Replace(originalFunc.Parameters[1], _sourceExpression, originalFunc.Body), originalFunc.Parameters[0]); _resultSelector = ExpressionHelper.CreateLambdaExpression <int, string> (total => total.ToString()); _seed = Expression.Constant(12); _resultOperatorWithoutResultSelector = new AggregateFromSeedResultOperator(_seed, _func, null); _resultOperatorWithResultSelector = new AggregateFromSeedResultOperator(_seed, _func, _resultSelector); }
public void GetOutputDataInfo_WithDifferentAssignableSeedType() { var itemExpression = Expression.Constant(0); var input = new StreamedSequenceInfo(typeof(int[]), itemExpression); var seed = Expression.Constant("string"); var func = Expression.Lambda( Expression.Constant(null, typeof(object)), Expression.Parameter(typeof(object), "acc")); var resultOperator = new AggregateFromSeedResultOperator(seed, func, null); var result = resultOperator.GetOutputDataInfo(input); Assert.That(result, Is.InstanceOf(typeof(StreamedScalarValueInfo))); Assert.That(result.DataType, Is.SameAs(typeof(object))); }
public void GetOutputDataInfo_WithDifferentAssignableSeedType () { var itemExpression = Expression.Constant (0); var input = new StreamedSequenceInfo (typeof (int[]), itemExpression); var seed = Expression.Constant ("string"); var func = Expression.Lambda ( Expression.Constant (null, typeof (object)), Expression.Parameter (typeof (object), "acc")); var resultOperator = new AggregateFromSeedResultOperator (seed, func, null); var result = resultOperator.GetOutputDataInfo (input); Assert.That (result, Is.InstanceOf (typeof (StreamedScalarValueInfo))); Assert.That (result.DataType, Is.SameAs (typeof (object))); }
public void GetConstantSeed_NoConstantExpression () { var resultOperator = new AggregateFromSeedResultOperator ( new QuerySourceReferenceExpression (ExpressionHelper.CreateMainFromClause_Int ()), _func, _resultSelector); resultOperator.GetConstantSeed<object> (); }
public void TestWithAgPostSelector() { Expression<Func<int, int>> selector = cnt => cnt * 2; AggregateFromSeedResultOperator agg = new AggregateFromSeedResultOperator(Expression.Constant(1), Expression.Lambda(Expression.MakeBinary(ExpressionType.Add, Expression.Parameter(typeof(int), "count"), Expression.Constant(1)), Expression.Parameter(typeof(int), "count")), selector); ROAggregate processor = new ROAggregate(); GeneratedCode gc = new GeneratedCode(); var result = ProcessResultOperator(processor, agg, null, gc); }