/src/Otis/Functions/AvgFunction.cs
http://otis-lib.googlecode.com/ · C# · 45 lines · 36 code · 8 blank · 1 comment · 0 complexity · 31e5da03318b34857e118dfdebc9d408 MD5 · raw file
- using System;
- using System.CodeDom;
- using System.Collections.Generic;
- using System.Text;
- using Otis.CodeGen;
-
- namespace Otis.Functions
- {
- public class AvgFunction : SimpleFunctionBase
- {
- public override IEnumerable<CodeStatement> GetInitializationStatements(AggregateFunctionContext context)
- {
- List<CodeStatement> statements = new List<CodeStatement>(base.GetInitializationStatements(context));
-
- CodeStatement st = new CodeVariableDeclarationStatement(typeof(int),
- GetCounterObjectName(context),
- new CodeSnippetExpression("0"));
-
- statements.Add(st);
-
- return statements;
- }
-
- protected override string GetResultExpression()
- {
- return string.Format("{0}/{1}", Context.FunctionObjectName, GetCounterObjectName(Context));
- }
-
- private static string GetCounterObjectName(AggregateFunctionContext context)
- {
- return context.FunctionObjectName + "_Cnt";
- }
-
- protected override string GetFormatForExecutedExpression(AggregateFunctionContext context)
- {
- // todo: test
- return string.Format("FN_OBJ = FN_OBJ + CURR_ITEM; {0}++;", GetCounterObjectName(context));
- }
-
- protected override Type GetDefaultTargetType()
- {
- return typeof(double);
- }
- }
- }