private static IExecutionStep AddOrUpdate(this _ExecutionStepFactory factory, Type dependency, string dependencyName)
        {
            IExecutionStep result = null;

            if (!factory.ExecutionSteps.ContainsKey(dependencyName))
            {
                factory.ExecutionSteps.Add(dependencyName, (result = Activator.CreateInstance(dependency) as IExecutionStep));
                result.Factory = factory;
                result.Level   = 1;
            }
            else
            {
                result = factory.ExecutionSteps[dependencyName];
            }
            return(result);
        }
        public bool Execute(IFreeformEntity_Msg request)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "ProcessMessage"))
            {
                bool result = default(bool);

                try
                {
                    if (request == null ||
                        request.IpAddress.IsEmpty())
                    {
                        return(false);
                    }

                    string key = request.IpAddress;
                    _ExecutionStepFactory factory = GetFactory(request);

                    // new or existing transaction id
                    if (!factory.PersistRequestResponseMapItem(request) &&
                        request.TransactionID <= 0)
                    {
                        request.TransactionID = NewTransactionId;
                    }

                    // immediate execution or delayed execution
                    if (factory.IsExecuting)
                    {
                        _executorDelayLoading.QueueWorkerItem(request);
                        result = true;
                    }
                    else
                    {
                        result = factory.Execute(request);
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return(result);
            }
        }
        internal static void CreateExecutionSteps(this _ExecutionStepFactory factory)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "CreateExecutionSteps"))
            {
                try
                {
                    ExecutionStepDictionary  execSteps  = factory.ExecutionSteps;
                    ExecutionStepDeviceTypes deviceType = ExecutionStepFactory.Current.DeviceType;
                    IDictionary <string, ExecutionStepCollection> messageWiseSteps = factory.MessageWiseSteps;
                    IDictionary <int, ExecutionStepCollection>    groupedSteps     = factory.GroupedSteps;

                    var mappings = (from m in _mappings
                                    from j in m.Attributes
                                    where j.DeviceType == deviceType
                                    select m).ToArray();

                    foreach (var mapping in mappings)//.AsParallel())
                    {
                        Type           classType = mapping.Type;
                        IExecutionStep step      = factory.AddOrUpdate(classType, classType.Name);

                        foreach (var mappingAttribute in mapping.Attributes
                                 .Where(d => d.DeviceType == deviceType))
                        {
                            if (mappingAttribute.IsStart && (execSteps.Start == null))
                            {
                                execSteps.Start = step;
                            }
                            if (mappingAttribute.IsEnd && (execSteps.End == null))
                            {
                                execSteps.End = step;
                            }
                            step.PostTypeG2H    = mappingAttribute.PostTypeG2H;
                            step.PostTypeH2G    = mappingAttribute.PostTypeH2G;
                            step.MoveToNextStep = mappingAttribute.MoveToNextStep;

                            //if (mappingAttribute.Dependencies != null)
                            //{
                            //    foreach (var dependency in mappingAttribute.Dependencies)
                            //    {
                            //        IExecutionStep parentStep = execSteps.AddOrUpdate(dependency, dependency.Name);
                            //        if (!parentStep.NextSteps.Contains(step))
                            //            parentStep.NextSteps.Add(step);
                            //    }
                            //}

                            //if (mappingAttribute.NextDependency != null)
                            //{
                            //    var dependency = mappingAttribute.NextDependency;
                            //    IExecutionStep nextStep = execSteps.AddOrUpdate(dependency, dependency.Name);
                            //    if (!step.NextSteps.Contains(nextStep))
                            //        step.NextSteps.Add(nextStep);
                            //    step.NextStep = nextStep;
                            //    step.SetNextStep = mappingAttribute.SetNextStep;
                            //    nextStep.PrevStep = step;
                            //}
                            if (mappingAttribute.NextSteps != null)
                            {
                                foreach (var dependency in mappingAttribute.NextSteps)
                                {
                                    IExecutionStep nextStep = factory.AddOrUpdate(dependency, dependency.Name);
                                    //nextStep.Level = step.Level + 1;
                                    bool resetNextStep = false;

                                    if (!step.NextSteps.Contains(nextStep))
                                    {
                                        step.NextSteps.Add(nextStep);
                                        resetNextStep = (step.NextSteps.Count > 1);
                                    }
                                    step.NextStep = (resetNextStep ? null : nextStep);

                                    if (!nextStep.PrevSteps.Contains(step))
                                    {
                                        nextStep.PrevSteps.Add(step);
                                    }
                                    //step.SetNextStep = mappingAttribute.SetNextStep;
                                    //nextStep.PrevStep = step;
                                }
                            }

                            if (messageWiseSteps != null &&
                                mappingAttribute.AllowedMessages != null)
                            {
                                var direction = mappingAttribute.AllowedMessageDirectionAll;
                                int length    = mappingAttribute.AllowedMessages.Length;

                                for (int i = 0; i < mappingAttribute.AllowedMessages.Length; i++)
                                {
                                    if (mappingAttribute.AllowedMessageDirections != null)
                                    {
                                        direction = mappingAttribute.AllowedMessageDirections[i];
                                    }

                                    ExecutionStepKeyValue pair = new ExecutionStepKeyValue(mappingAttribute.AllowedMessages[i].Name, direction);
                                    string pairKey             = pair.FullKey;
                                    if (!step.AllowedMessages.ContainsKey(pair))
                                    {
                                        step.AllowedMessages.Add(pair, 1);
                                    }

                                    ExecutionStepCollection execSteps2 = null;
                                    if (messageWiseSteps.ContainsKey(pairKey))
                                    {
                                        execSteps2 = messageWiseSteps[pairKey];
                                    }
                                    else
                                    {
                                        messageWiseSteps.Add(pairKey, (execSteps2 = new ExecutionStepCollection()));
                                    }
                                    execSteps2.Add(step);
                                }

                                if (mappingAttribute.AllowedReplyMessages != null &&
                                    mappingAttribute.AllowedReplyMessages.Length == length)
                                {
                                    for (int j = 0; j < length; j++)
                                    {
                                        Type type1 = mappingAttribute.AllowedMessages[j];
                                        Type type2 = mappingAttribute.AllowedReplyMessages[j];
                                        if (type1 != null && type2 != null)
                                        {
                                            factory.RequestResponseMappings.Add(type1.Name, type2.Name, new RequestResponseMapItem());
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // execution step grouping
                    if (messageWiseSteps != null &&
                        groupedSteps != null)
                    {
                        execSteps.ArrangeExecutionSteps(messageWiseSteps, groupedSteps);
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }
            }
        }