public static void ProcessViewResultCodegen(
            ResultSetProcessorRowPerEventForge forge,
            CodegenClassScope classScope,
            CodegenMethod method,
            CodegenInstanceAux instance)
        {
            method.Block
                .DeclareVar<EventBean[]>("selectOldEvents", ConstantNull())
                .DeclareVarNoInit(typeof(EventBean[]), "selectNewEvents")
                .DeclareVar<EventBean[]>("eventsPerStream", NewArrayByLength(typeof(EventBean), Constant(1)))
                .StaticMethod(
                    typeof(ResultSetProcessorUtil),
                    METHOD_APPLYAGGVIEWRESULT,
                    MEMBER_AGGREGATIONSVC,
                    MEMBER_AGENTINSTANCECONTEXT,
                    REF_NEWDATA,
                    REF_OLDDATA,
                    Ref("eventsPerStream"));

            ResultSetProcessorUtil.ProcessViewResultCodegen(
                method,
                classScope,
                instance,
                forge.OptionalHavingNode != null,
                forge.IsSelectRStream,
                forge.IsSorting,
                true);
        }
        private static void ProcessOutputLimitedLastAllNonBufferedCodegen(
            ResultSetProcessorRowPerEventForge forge,
            string methodName,
            CodegenClassScope classScope,
            CodegenMethod method,
            CodegenInstanceAux instance)
        {
            var factory = classScope.AddOrGetDefaultFieldSharable(ResultSetProcessorHelperFactoryField.INSTANCE);

            if (forge.IsOutputAll) {
                instance.AddMember(NAME_OUTPUTALLUNORDHELPER, typeof(ResultSetProcessorRowPerEventOutputAllHelper));
                instance.ServiceCtor.Block.AssignRef(
                    NAME_OUTPUTALLUNORDHELPER,
                    ExprDotMethod(factory, "MakeRSRowPerEventOutputAll", Ref("this"), MEMBER_AGENTINSTANCECONTEXT));
                method.Block.ExprDotMethod(
                    Member(NAME_OUTPUTALLUNORDHELPER),
                    methodName,
                    REF_NEWDATA,
                    REF_OLDDATA,
                    REF_ISSYNTHESIZE);
            }
            else if (forge.IsOutputLast) {
                instance.AddMember(NAME_OUTPUTLASTUNORDHELPER, typeof(ResultSetProcessorRowPerEventOutputLastHelper));
                instance.ServiceCtor.Block.AssignRef(
                    NAME_OUTPUTLASTUNORDHELPER,
                    ExprDotMethod(factory, "MakeRSRowPerEventOutputLast", Ref("this"), MEMBER_AGENTINSTANCECONTEXT));
                method.Block.ExprDotMethod(
                    Member(NAME_OUTPUTLASTUNORDHELPER),
                    methodName,
                    REF_NEWDATA,
                    REF_OLDDATA,
                    REF_ISSYNTHESIZE);
            }
        }
        public static void ProcessJoinResultCodegen(
            ResultSetProcessorRowPerEventForge forge,
            CodegenClassScope classScope,
            CodegenMethod method,
            CodegenInstanceAux instance)
        {
            method.Block.DeclareVar<EventBean[]>("selectOldEvents", ConstantNull())
                .DeclareVarNoInit(typeof(EventBean[]), "selectNewEvents");

            if (forge.IsUnidirectional) {
                method.Block.ExprDotMethod(Ref("this"), "Clear");
            }

            method.Block.StaticMethod(
                typeof(ResultSetProcessorUtil),
                METHOD_APPLYAGGJOINRESULT,
                MEMBER_AGGREGATIONSVC,
                MEMBER_AGENTINSTANCECONTEXT,
                REF_NEWDATA,
                REF_OLDDATA);

            ResultSetProcessorUtil.ProcessJoinResultCodegen(
                method,
                classScope,
                instance,
                forge.OptionalHavingNode != null,
                forge.IsSelectRStream,
                forge.IsSorting,
                true);
        }
        public static void GetEnumeratorViewCodegen(
            ResultSetProcessorRowPerEventForge forge,
            CodegenClassScope classScope,
            CodegenMethod method)
        {
            if (!forge.IsHistoricalOnly) {
                method.Block.MethodReturn(LocalMethod(ObtainEnumeratorCodegen(forge, classScope, method), REF_VIEWABLE));
                return;
            }

            method.Block
                .StaticMethod(
                    typeof(ResultSetProcessorUtil),
                    METHOD_CLEARANDAGGREGATEUNGROUPED,
                    MEMBER_AGENTINSTANCECONTEXT,
                    MEMBER_AGGREGATIONSVC,
                    REF_VIEWABLE)
                .DeclareVar<IEnumerator<EventBean>>(
                    "enumerator",
                    LocalMethod(ObtainEnumeratorCodegen(forge, classScope, method), REF_VIEWABLE))
                .DeclareVar<ArrayDeque<EventBean>>(
                    "deque",
                    StaticMethod(typeof(ResultSetProcessorUtil), METHOD_ITERATORTODEQUE, Ref("enumerator")))
                .ExprDotMethod(MEMBER_AGGREGATIONSVC, "ClearResults", MEMBER_AGENTINSTANCECONTEXT)
                .MethodReturn(ExprDotMethod(Ref("deque"), "GetEnumerator"));
        }
 public static void ProcessOutputLimitedLastAllNonBufferedJoinCodegen(
     ResultSetProcessorRowPerEventForge forge,
     CodegenClassScope classScope,
     CodegenMethod method,
     CodegenInstanceAux instance)
 {
     ProcessOutputLimitedLastAllNonBufferedCodegen(forge, "ProcessJoin", classScope, method, instance);
 }
 public static void ProcessOutputLimitedViewCodegen(
     ResultSetProcessorRowPerEventForge forge,
     CodegenClassScope classScope,
     CodegenMethod method,
     CodegenInstanceAux instance)
 {
     if (forge.IsOutputLast) {
         ProcessOutputLimitedViewLastCodegen(forge, classScope, method, instance);
     }
     else {
         ProcessOutputLimitedViewDefaultCodegen(forge, classScope, method, instance);
     }
 }
 public static void ContinueOutputLimitedLastAllNonBufferedJoinCodegen(
     ResultSetProcessorRowPerEventForge forge,
     CodegenMethod method)
 {
     if (forge.IsOutputAll) {
         method.Block.MethodReturn(ExprDotMethod(Member(NAME_OUTPUTALLUNORDHELPER), "Output"));
     }
     else if (forge.IsOutputLast) {
         method.Block.MethodReturn(ExprDotMethod(Member(NAME_OUTPUTLASTUNORDHELPER), "Output"));
     }
     else {
         method.Block.MethodReturn(ConstantNull());
     }
 }
        private static void ProcessOutputLimitedViewLastCodegen(
            ResultSetProcessorRowPerEventForge forge,
            CodegenClassScope classScope,
            CodegenMethod method,
            CodegenInstanceAux instance)
        {
            method.Block
                .DeclareVar<EventBean>("lastOldEvent", ConstantNull())
                .DeclareVar<EventBean>("lastNewEvent", ConstantNull())
                .DeclareVar<EventBean[]>("eventsPerStream", NewArrayByLength(typeof(EventBean), Constant(1)));

            {
                var forEach = method.Block.ForEach(typeof(UniformPair<EventBean[]>), "pair", REF_VIEWEVENTSLIST);
                forEach.DeclareVar<EventBean[]>(
                        "newData",
                        ExprDotName(Ref("pair"), "First"))
                    .DeclareVar<EventBean[]>(
                        "oldData",
                        ExprDotName(Ref("pair"), "Second"))
                    .StaticMethod(
                        typeof(ResultSetProcessorUtil),
                        METHOD_APPLYAGGVIEWRESULT,
                        MEMBER_AGGREGATIONSVC,
                        MEMBER_AGENTINSTANCECONTEXT,
                        Ref("newData"),
                        Ref("oldData"),
                        Ref("eventsPerStream"));

                if (forge.IsSelectRStream) {
                    if (forge.OptionalHavingNode == null) {
                        forEach.DeclareVar<EventBean[]>(
                            "selectOldEvents",
                            StaticMethod(
                                typeof(ResultSetProcessorUtil),
                                METHOD_GETSELECTEVENTSNOHAVING,
                                MEMBER_SELECTEXPRPROCESSOR,
                                Ref("oldData"),
                                ConstantFalse(),
                                REF_ISSYNTHESIZE,
                                MEMBER_AGENTINSTANCECONTEXT));
                    }
                    else {
                        var select = GetSelectEventsHavingCodegen(classScope, instance);
                        forEach.DeclareVar<EventBean[]>(
                            "selectOldEvents",
                            LocalMethod(
                                select,
                                MEMBER_SELECTEXPRPROCESSOR,
                                Ref("oldData"),
                                ConstantFalse(),
                                REF_ISSYNTHESIZE,
                                MEMBER_AGENTINSTANCECONTEXT));
                    }

                    forEach.IfCondition(
                            And(
                                NotEqualsNull(Ref("selectOldEvents")),
                                Relational(
                                    ArrayLength(Ref("selectOldEvents")),
                                    CodegenExpressionRelational.CodegenRelational.GT,
                                    Constant(0))))
                        .AssignRef(
                            "lastOldEvent",
                            ArrayAtIndex(
                                Ref("selectOldEvents"),
                                Op(ArrayLength(Ref("selectOldEvents")), "-", Constant(1))))
                        .BlockEnd();
                }

                // generate new events using select expressions
                if (forge.OptionalHavingNode == null) {
                    forEach.DeclareVar<EventBean[]>(
                        "selectNewEvents",
                        StaticMethod(
                            typeof(ResultSetProcessorUtil),
                            METHOD_GETSELECTEVENTSNOHAVING,
                            MEMBER_SELECTEXPRPROCESSOR,
                            Ref("newData"),
                            ConstantTrue(),
                            REF_ISSYNTHESIZE,
                            MEMBER_AGENTINSTANCECONTEXT));
                }
                else {
                    var select = GetSelectEventsHavingCodegen(classScope, instance);
                    forEach.DeclareVar<EventBean[]>(
                        "selectNewEvents",
                        LocalMethod(
                            select,
                            MEMBER_SELECTEXPRPROCESSOR,
                            Ref("newData"),
                            ConstantTrue(),
                            REF_ISSYNTHESIZE,
                            MEMBER_AGENTINSTANCECONTEXT));
                }

                forEach.IfCondition(
                        And(
                            NotEqualsNull(Ref("selectNewEvents")),
                            Relational(
                                ArrayLength(Ref("selectNewEvents")),
                                CodegenExpressionRelational.CodegenRelational.GT,
                                Constant(0))))
                    .AssignRef(
                        "lastNewEvent",
                        ArrayAtIndex(Ref("selectNewEvents"), Op(ArrayLength(Ref("selectNewEvents")), "-", Constant(1))))
                    .BlockEnd();
            }

            method.Block
                .DeclareVar<EventBean[]>(
                    "lastNew",
                    StaticMethod(typeof(CollectionUtil), CollectionUtil.METHOD_TOARRAYMAYNULL, Ref("lastNewEvent")))
                .DeclareVar<EventBean[]>(
                    "lastOld",
                    StaticMethod(typeof(CollectionUtil), CollectionUtil.METHOD_TOARRAYMAYNULL, Ref("lastOldEvent")))
                .IfCondition(And(EqualsNull(Ref("lastNew")), EqualsNull(Ref("lastOld"))))
                .BlockReturn(ConstantNull())
                .MethodReturn(NewInstance<UniformPair<EventBean[]>>(Ref("lastNew"), Ref("lastOld")));
        }
        private static void ProcessOutputLimitedViewDefaultCodegen(
            ResultSetProcessorRowPerEventForge forge,
            CodegenClassScope classScope,
            CodegenMethod method,
            CodegenInstanceAux instance)
        {
            PrefixCodegenNewOldEvents(method.Block, forge.IsSorting, forge.IsSelectRStream);

            {
                var forEach = method.Block.ForEach(typeof(UniformPair<EventBean[]>), "pair", REF_VIEWEVENTSLIST);
                forEach.DeclareVar<EventBean[]>(
                        "newData",
                        ExprDotName(Ref("pair"), "First"))
                    .DeclareVar<EventBean[]>(
                        "oldData",
                        ExprDotName(Ref("pair"), "Second"))
                    .DeclareVar<EventBean[]>(
                        "eventsPerStream",
                        NewArrayByLength(typeof(EventBean), Constant(1)))
                    .StaticMethod(
                        typeof(ResultSetProcessorUtil),
                        METHOD_APPLYAGGVIEWRESULT,
                        MEMBER_AGGREGATIONSVC,
                        MEMBER_AGENTINSTANCECONTEXT,
                        REF_NEWDATA,
                        REF_OLDDATA,
                        Ref("eventsPerStream"));

                // generate old events using select expressions
                if (forge.IsSelectRStream) {
                    if (forge.OptionalHavingNode == null) {
                        if (!forge.IsSorting) {
                            forEach.StaticMethod(
                                typeof(ResultSetProcessorUtil),
                                METHOD_POPULATESELECTEVENTSNOHAVING,
                                MEMBER_SELECTEXPRPROCESSOR,
                                Ref("oldData"),
                                ConstantFalse(),
                                REF_ISSYNTHESIZE,
                                Ref("oldEvents"),
                                MEMBER_AGENTINSTANCECONTEXT);
                        }
                        else {
                            forEach.StaticMethod(
                                typeof(ResultSetProcessorUtil),
                                METHOD_POPULATESELECTEVENTSNOHAVINGWITHORDERBY,
                                MEMBER_SELECTEXPRPROCESSOR,
                                REF_ORDERBYPROCESSOR,
                                Ref("oldData"),
                                ConstantFalse(),
                                REF_ISSYNTHESIZE,
                                Ref("oldEvents"),
                                MEMBER_AGENTINSTANCECONTEXT);
                        }
                    }
                    else {
                        // generate old events using having then select
                        if (!forge.IsSorting) {
                            var select = PopulateSelectEventsHavingCodegen(classScope, instance);
                            forEach.LocalMethod(
                                select,
                                MEMBER_SELECTEXPRPROCESSOR,
                                REF_OLDDATA,
                                ConstantFalse(),
                                REF_ISSYNTHESIZE,
                                Ref("oldEvents"),
                                MEMBER_AGENTINSTANCECONTEXT);
                        }
                        else {
                            var select = PopulateSelectEventsHavingWithOrderByCodegen(classScope, instance);
                            forEach.LocalMethod(
                                select,
                                MEMBER_SELECTEXPRPROCESSOR,
                                REF_ORDERBYPROCESSOR,
                                REF_OLDDATA,
                                ConstantFalse(),
                                REF_ISSYNTHESIZE,
                                Ref("oldEvents"),
                                Ref("oldEventsSortKey"),
                                MEMBER_AGENTINSTANCECONTEXT);
                            throw new UnsupportedOperationException();
                        }
                    }
                }

                // generate new events using select expressions
                if (forge.OptionalHavingNode == null) {
                    if (!forge.IsSorting) {
                        forEach.StaticMethod(
                            typeof(ResultSetProcessorUtil),
                            METHOD_POPULATESELECTEVENTSNOHAVING,
                            MEMBER_SELECTEXPRPROCESSOR,
                            Ref("newData"),
                            ConstantTrue(),
                            REF_ISSYNTHESIZE,
                            Ref("newEvents"),
                            MEMBER_AGENTINSTANCECONTEXT);
                    }
                    else {
                        forEach.StaticMethod(
                            typeof(ResultSetProcessorUtil),
                            METHOD_POPULATESELECTEVENTSNOHAVINGWITHORDERBY,
                            MEMBER_SELECTEXPRPROCESSOR,
                            REF_ORDERBYPROCESSOR,
                            Ref("newData"),
                            ConstantTrue(),
                            REF_ISSYNTHESIZE,
                            Ref("newEvents"),
                            Ref("newEventsSortKey"),
                            MEMBER_AGENTINSTANCECONTEXT);
                    }
                }
                else {
                    if (!forge.IsSorting) {
                        var select = PopulateSelectEventsHavingCodegen(classScope, instance);
                        forEach.LocalMethod(
                            select,
                            MEMBER_SELECTEXPRPROCESSOR,
                            REF_NEWDATA,
                            ConstantTrue(),
                            REF_ISSYNTHESIZE,
                            Ref("newEvents"),
                            MEMBER_AGENTINSTANCECONTEXT);
                    }
                    else {
                        var select = PopulateSelectEventsHavingWithOrderByCodegen(classScope, instance);
                        forEach.LocalMethod(
                            select,
                            MEMBER_SELECTEXPRPROCESSOR,
                            REF_ORDERBYPROCESSOR,
                            REF_NEWDATA,
                            ConstantTrue(),
                            REF_ISSYNTHESIZE,
                            Ref("newEvents"),
                            Ref("newEventsSortKey"),
                            MEMBER_AGENTINSTANCECONTEXT);
                    }
                }
            }

            FinalizeOutputMaySortMayRStreamCodegen(
                method.Block,
                Ref("newEvents"),
                Ref("newEventsSortKey"),
                Ref("oldEvents"),
                Ref("oldEventsSortKey"),
                forge.IsSelectRStream,
                forge.IsSorting);
        }
        private static void ProcessOutputLimitedJoinDefaultCodegen(
            ResultSetProcessorRowPerEventForge forge,
            CodegenClassScope classScope,
            CodegenMethod method,
            CodegenInstanceAux instance)
        {
            PrefixCodegenNewOldEvents(method.Block, forge.IsSorting, forge.IsSelectRStream);

            {
                var forEach = method.Block.ForEach(
                    typeof(UniformPair<ISet<MultiKeyArrayOfKeys<EventBean>>>), "pair", REF_JOINEVENTSSET);
                forEach
                    .DeclareVar<ISet<MultiKeyArrayOfKeys<EventBean>>>("newData", ExprDotName(Ref("pair"), "First"))
                    .DeclareVar<ISet<MultiKeyArrayOfKeys<EventBean>>>("oldData", ExprDotName(Ref("pair"), "Second"));
                if (forge.IsUnidirectional) {
                    forEach.ExprDotMethod(Ref("this"), "Clear");
                }

                forEach.StaticMethod(
                    typeof(ResultSetProcessorUtil),
                    METHOD_APPLYAGGJOINRESULT,
                    MEMBER_AGGREGATIONSVC,
                    MEMBER_AGENTINSTANCECONTEXT,
                    REF_NEWDATA,
                    REF_OLDDATA);

                // generate old events using select expressions
                if (forge.IsSelectRStream) {
                    if (forge.OptionalHavingNode == null) {
                        if (!forge.IsSorting) {
                            forEach.StaticMethod(
                                typeof(ResultSetProcessorUtil),
                                METHOD_POPULATESELECTJOINEVENTSNOHAVING,
                                MEMBER_SELECTEXPRPROCESSOR,
                                Ref("oldData"),
                                ConstantFalse(),
                                REF_ISSYNTHESIZE,
                                Ref("oldEvents"),
                                MEMBER_AGENTINSTANCECONTEXT);
                        }
                        else {
                            forEach.StaticMethod(
                                typeof(ResultSetProcessorUtil),
                                METHOD_POPULATESELECTJOINEVENTSNOHAVINGWITHORDERBY,
                                MEMBER_SELECTEXPRPROCESSOR,
                                REF_ORDERBYPROCESSOR,
                                REF_OLDDATA,
                                ConstantFalse(),
                                REF_ISSYNTHESIZE,
                                Ref("oldEvents"),
                                Ref("oldEventsSortKey"),
                                MEMBER_AGENTINSTANCECONTEXT);
                        }
                    }
                    else {
                        // generate old events using having then select
                        if (!forge.IsSorting) {
                            var select = PopulateSelectJoinEventsHavingCodegen(classScope, instance);
                            forEach.LocalMethod(
                                select,
                                MEMBER_SELECTEXPRPROCESSOR,
                                REF_OLDDATA,
                                ConstantFalse(),
                                REF_ISSYNTHESIZE,
                                Ref("oldEvents"),
                                MEMBER_AGENTINSTANCECONTEXT);
                        }
                        else {
                            var select = PopulateSelectJoinEventsHavingWithOrderByCodegen(classScope, instance);
                            forEach.LocalMethod(
                                select,
                                MEMBER_SELECTEXPRPROCESSOR,
                                REF_ORDERBYPROCESSOR,
                                REF_OLDDATA,
                                ConstantFalse(),
                                REF_ISSYNTHESIZE,
                                Ref("oldEvents"),
                                Ref("oldEventsSortKey"),
                                MEMBER_AGENTINSTANCECONTEXT);
                        }
                    }
                }

                // generate new events using select expressions
                if (forge.OptionalHavingNode == null) {
                    if (!forge.IsSorting) {
                        forEach.StaticMethod(
                            typeof(ResultSetProcessorUtil),
                            METHOD_POPULATESELECTJOINEVENTSNOHAVING,
                            MEMBER_SELECTEXPRPROCESSOR,
                            Ref("newData"),
                            ConstantTrue(),
                            REF_ISSYNTHESIZE,
                            Ref("newEvents"),
                            MEMBER_AGENTINSTANCECONTEXT);
                    }
                    else {
                        forEach.StaticMethod(
                            typeof(ResultSetProcessorUtil),
                            METHOD_POPULATESELECTJOINEVENTSNOHAVINGWITHORDERBY,
                            MEMBER_SELECTEXPRPROCESSOR,
                            REF_ORDERBYPROCESSOR,
                            Ref("newData"),
                            ConstantTrue(),
                            REF_ISSYNTHESIZE,
                            Ref("newEvents"),
                            Ref("newEventsSortKey"),
                            MEMBER_AGENTINSTANCECONTEXT);
                    }
                }
                else {
                    if (!forge.IsSorting) {
                        var select = PopulateSelectJoinEventsHavingCodegen(classScope, instance);
                        forEach.LocalMethod(
                            select,
                            MEMBER_SELECTEXPRPROCESSOR,
                            REF_NEWDATA,
                            ConstantTrue(),
                            REF_ISSYNTHESIZE,
                            Ref("newEvents"),
                            MEMBER_AGENTINSTANCECONTEXT);
                    }
                    else {
                        var select = PopulateSelectJoinEventsHavingWithOrderByCodegen(classScope, instance);
                        forEach.LocalMethod(
                            select,
                            MEMBER_SELECTEXPRPROCESSOR,
                            REF_ORDERBYPROCESSOR,
                            Ref("newData"),
                            ConstantTrue(),
                            REF_ISSYNTHESIZE,
                            Ref("newEvents"),
                            Ref("newEventsSortKey"),
                            MEMBER_AGENTINSTANCECONTEXT);
                    }
                }
            }

            FinalizeOutputMaySortMayRStreamCodegen(
                method.Block,
                Ref("newEvents"),
                Ref("newEventsSortKey"),
                Ref("oldEvents"),
                Ref("oldEventsSortKey"),
                forge.IsSelectRStream,
                forge.IsSorting);
        }
        public static void GetEnumeratorJoinCodegen(
            ResultSetProcessorRowPerEventForge forge,
            CodegenClassScope classScope,
            CodegenMethod method,
            CodegenInstanceAux instance)
        {
            if (forge.OptionalHavingNode == null) {
                if (!forge.IsSorting) {
                    method.Block.DeclareVar<EventBean[]>(
                        "result",
                        StaticMethod(
                            typeof(ResultSetProcessorUtil),
                            METHOD_GETSELECTJOINEVENTSNOHAVING,
                            MEMBER_SELECTEXPRPROCESSOR,
                            REF_JOINSET,
                            ConstantTrue(),
                            ConstantTrue(),
                            MEMBER_AGENTINSTANCECONTEXT));
                }
                else {
                    method.Block.DeclareVar<EventBean[]>(
                        "result",
                        StaticMethod(
                            typeof(ResultSetProcessorUtil),
                            METHOD_GETSELECTJOINEVENTSNOHAVINGWITHORDERBY,
                            MEMBER_AGGREGATIONSVC,
                            MEMBER_SELECTEXPRPROCESSOR,
                            REF_ORDERBYPROCESSOR,
                            REF_JOINSET,
                            ConstantTrue(),
                            ConstantTrue(),
                            MEMBER_AGENTINSTANCECONTEXT));
                }
            }
            else {
                if (!forge.IsSorting) {
                    var select = GetSelectJoinEventsHavingCodegen(classScope, instance);
                    method.Block.DeclareVar<EventBean[]>(
                        "result",
                        LocalMethod(
                            select,
                            MEMBER_SELECTEXPRPROCESSOR,
                            REF_JOINSET,
                            ConstantTrue(),
                            ConstantTrue(),
                            MEMBER_AGENTINSTANCECONTEXT));
                }
                else {
                    var select = GetSelectJoinEventsHavingWithOrderByCodegen(classScope, instance);
                    method.Block.DeclareVar<EventBean[]>(
                        "result",
                        LocalMethod(
                            select,
                            MEMBER_AGGREGATIONSVC,
                            MEMBER_SELECTEXPRPROCESSOR,
                            REF_ORDERBYPROCESSOR,
                            REF_JOINSET,
                            ConstantTrue(),
                            ConstantTrue(),
                            MEMBER_AGENTINSTANCECONTEXT));
                }
            }

            method.Block.MethodReturn(NewInstance<ArrayEventEnumerator>(Ref("result")));
        }
        private static CodegenMethod ObtainEnumeratorCodegen(
            ResultSetProcessorRowPerEventForge forge,
            CodegenClassScope classScope,
            CodegenMethod parent)
        {
            var iterator = parent.MakeChild(
                    typeof(IEnumerator<EventBean>),
                    typeof(ResultSetProcessorRowPerEventImpl),
                    classScope)
                .AddParam(typeof(Viewable), NAME_VIEWABLE);
            if (!forge.IsSorting) {
                iterator.Block.MethodReturn(
                    StaticMethod(
                        typeof(ResultSetProcessorRowPerEventEnumerator),
                        "For",
                        ExprDotMethod(REF_VIEWABLE, "GetEnumerator"),
                        Ref("this"),
                        MEMBER_AGENTINSTANCECONTEXT));
                return iterator;
            }

            iterator.Block.DeclareVar<EventBean[]>(
                    "eventsPerStream",
                    NewArrayByLength(typeof(EventBean), Constant(1)))
                .DeclareVar<IList<EventBean>>("outgoingEvents", NewInstance(typeof(List<EventBean>)))
                .DeclareVar<IList<object>>("orderKeys", NewInstance(typeof(List<object>)));

            {
                var forEach = iterator.Block.ForEach(typeof(EventBean), "candidate", REF_VIEWABLE);
                forEach.AssignArrayElement("eventsPerStream", Constant(0), Ref("candidate"));
                if (forge.OptionalHavingNode != null) {
                    forEach.IfCondition(
                            Not(
                                ExprDotMethod(
                                    Ref("this"),
                                    "EvaluateHavingClause",
                                    Ref("eventsPerStream"),
                                    Constant(true),
                                    MEMBER_AGENTINSTANCECONTEXT)))
                        .BlockContinue();
                }

                forEach.ExprDotMethod(
                        Ref("outgoingEvents"),
                        "Add",
                        ExprDotMethod(
                            MEMBER_SELECTEXPRPROCESSOR,
                            "Process",
                            Ref("eventsPerStream"),
                            ConstantTrue(),
                            ConstantTrue(),
                            MEMBER_AGENTINSTANCECONTEXT))
                    .ExprDotMethod(
                        Ref("orderKeys"),
                        "Add",
                        ExprDotMethod(
                            REF_ORDERBYPROCESSOR,
                            "GetSortKey",
                            Ref("eventsPerStream"),
                            ConstantTrue(),
                            MEMBER_AGENTINSTANCECONTEXT));
            }

            iterator.Block.MethodReturn(
                StaticMethod(
                    typeof(ResultSetProcessorUtil),
                    METHOD_ORDEROUTGOINGGETITERATOR,
                    Ref("outgoingEvents"),
                    Ref("orderKeys"),
                    REF_ORDERBYPROCESSOR,
                    MEMBER_AGENTINSTANCECONTEXT));
            return iterator;
        }