/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

  1. using System;
  2. using System.CodeDom;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Otis.CodeGen;
  6. namespace Otis.Functions
  7. {
  8. public class AvgFunction : SimpleFunctionBase
  9. {
  10. public override IEnumerable<CodeStatement> GetInitializationStatements(AggregateFunctionContext context)
  11. {
  12. List<CodeStatement> statements = new List<CodeStatement>(base.GetInitializationStatements(context));
  13. CodeStatement st = new CodeVariableDeclarationStatement(typeof(int),
  14. GetCounterObjectName(context),
  15. new CodeSnippetExpression("0"));
  16. statements.Add(st);
  17. return statements;
  18. }
  19. protected override string GetResultExpression()
  20. {
  21. return string.Format("{0}/{1}", Context.FunctionObjectName, GetCounterObjectName(Context));
  22. }
  23. private static string GetCounterObjectName(AggregateFunctionContext context)
  24. {
  25. return context.FunctionObjectName + "_Cnt";
  26. }
  27. protected override string GetFormatForExecutedExpression(AggregateFunctionContext context)
  28. {
  29. // todo: test
  30. return string.Format("FN_OBJ = FN_OBJ + CURR_ITEM; {0}++;", GetCounterObjectName(context));
  31. }
  32. protected override Type GetDefaultTargetType()
  33. {
  34. return typeof(double);
  35. }
  36. }
  37. }