Exemple #1
0
        protected void ConnectLeftoversToParameters(ModelOutput mf, ParameterValues parameters)
        {
            var parametersToFindValueFor = from component in Components
                                           from parameter in component.Parameters
                                           where parameter.Binding is ParameterValueFile
                                           select new
                                           {
                                               ComponentName = component.Name,
                                               ParameterName = parameter.Name,
                                               DimensionTypes = parameter.DimensionTypes,
                                               DataType = parameter.DataType,
                                               DefaultValue = parameter.Binding.DefaultValue,
                                               paer = parameter
                                           };

            var parametersWithManualValues = from component in Components
                                             from parameter in component.Parameters
                                             where parameter.Binding is ParameterValueManualConstant
                                             select new
                                             {
                                                 ComponentName = component.Name,
                                                 ParameterName = parameter.Name,
                                                 DimensionTypes = parameter.DimensionTypes,
                                                 DataType = parameter.DataType,
                                                 Value = ((ParameterValueManualConstant)parameter.Binding).Value,
                                                 paer = parameter
                                             };

            var parametersWithLambdas = from component in Components
                                        from parameter in component.Parameters
                                        where parameter.Binding is ParameterValueManuelLambda
                                        select new
                                        {
                                            ComponentName = component.Name,
                                            ParameterName = parameter.Name,
                                            DimensionTypes = parameter.DimensionTypes,
                                            DataType = parameter.DataType,
                                            Value = ((ParameterValueManuelLambda)parameter.Binding).Lambda,
                                            paer = parameter
                                        };

            var parametersInFileThatAreNotBound = from p in parameters
                                                  let pName = p.Name.ToLowerInvariant()
                                                  where !parametersToFindValueFor.Any(i => pName == i.ParameterName.ToLowerInvariant()) && pName != "region"
                                                  select p.Name;

            if (parametersInFileThatAreNotBound.Count() > 0)
            {
                Console.WriteLine();
                foreach (var p in parametersInFileThatAreNotBound)
                {
                    Console.WriteLine("WARNING: Parameter '{0}' is not used by model", p);
                }
            }

            foreach (var p in parametersWithManualValues)
            {
                mf.AddNonDimensionalVariable(p.ComponentName, p.ParameterName, p.Value);
            }

            foreach (var p in parametersWithLambdas)
            {
                if (p.Value.GetType().GetGenericTypeDefinition() == typeof(Func<,,>))
                {
                    var types = p.Value.GetType().GetGenericArguments();

                    var method = mf.GetType().GetMethod("Add2DimensionalParameterLambda").MakeGenericMethod(types);
                    method.Invoke(mf, new object[] { p.ComponentName, p.ParameterName, p.Value });
                }
                else
                    throw new NotImplementedException();
            }

            foreach (var p in parametersToFindValueFor)
            {
                if (parameters.Contains(p.ParameterName))
                {
                    mf.LoadVariableFromParameter(p.ComponentName, p.ParameterName, parameters, p.DataType, p.DimensionTypes.ToArray());
                }
                else if (p.DefaultValue != null)
                {
                    mf.AddNonDimensionalVariable(p.ComponentName, p.ParameterName, p.DefaultValue);
                }
                else
                    throw new InvalidOperationException();
            }
        }
        protected void ConnectLeftoversToParameters(ModelOutput mf, ParameterValues parameters)
        {
            var parametersToFindValueFor = from component in Components
                                           from parameter in component.Parameters
                                           where parameter.Binding is ParameterValueFile
                                           select new
            {
                ComponentName  = component.Name,
                ParameterName  = parameter.Name,
                DimensionTypes = parameter.DimensionTypes,
                DataType       = parameter.DataType,
                DefaultValue   = parameter.Binding.DefaultValue,
                paer           = parameter
            };

            var parametersWithManualValues = from component in Components
                                             from parameter in component.Parameters
                                             where parameter.Binding is ParameterValueManualConstant
                                             select new
            {
                ComponentName  = component.Name,
                ParameterName  = parameter.Name,
                DimensionTypes = parameter.DimensionTypes,
                DataType       = parameter.DataType,
                Value          = ((ParameterValueManualConstant)parameter.Binding).Value,
                paer           = parameter
            };

            var parametersWithLambdas = from component in Components
                                        from parameter in component.Parameters
                                        where parameter.Binding is ParameterValueManuelLambda
                                        select new
            {
                ComponentName  = component.Name,
                ParameterName  = parameter.Name,
                DimensionTypes = parameter.DimensionTypes,
                DataType       = parameter.DataType,
                Value          = ((ParameterValueManuelLambda)parameter.Binding).Lambda,
                paer           = parameter
            };

            var parametersInFileThatAreNotBound = from p in parameters
                                                  let pName = p.Name.ToLowerInvariant()
                                                              where !parametersToFindValueFor.Any(i => pName == i.ParameterName.ToLowerInvariant()) && pName != "region"
                                                              select p.Name;



            foreach (var p in parametersWithManualValues)
            {
                mf.AddNonDimensionalVariable(p.ComponentName, p.ParameterName, p.Value);
            }

            foreach (var p in parametersWithLambdas)
            {
                if (p.Value.GetType().GetGenericTypeDefinition() == typeof(Func <, ,>))
                {
                    var types = p.Value.GetType().GetGenericArguments();

                    var method = mf.GetType().GetMethod("Add2DimensionalParameterLambda").MakeGenericMethod(types);
                    method.Invoke(mf, new object[] { p.ComponentName, p.ParameterName, p.Value });
                }
                else
                {
                    throw new NotImplementedException();
                }
            }

            foreach (var p in parametersToFindValueFor)
            {
                if (parameters.Contains(p.ParameterName))
                {
                    mf.LoadVariableFromParameter(p.ComponentName, p.ParameterName, parameters, p.DataType, p.DimensionTypes.ToArray());
                }
                else if (p.DefaultValue != null)
                {
                    mf.AddNonDimensionalVariable(p.ComponentName, p.ParameterName, p.DefaultValue);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
        }
Exemple #3
0
        protected void ConnectLeftoversToParameters(ModelOutput mf, ParameterValues parameters)
        {
            var parametersToFindValueFor = from component in Components
                                           from parameter in component.Parameters
                                           where parameter.Binding is ParameterValueFile
                                           select new
            {
                ComponentName  = component.Name,
                ParameterName  = parameter.Name,
                DimensionTypes = parameter.DimensionTypes,
                DataType       = parameter.DataType,
                DefaultValue   = parameter.Binding.DefaultValue,
                paer           = parameter
            };

            var forcedVariablesToFindValueFor = from component in Components
                                                from variable in component.Variables
                                                where variable.Forced != null && variable.Forced is ParameterValueFile
                                                select new
            {
                ComponentName  = component.Name,
                VariableName   = variable.Name,
                DimensionTypes = variable.DimensionTypes,
                DataType       = variable.DataType,
                vaer           = variable
            };

            var parametersWithManualValues = from component in Components
                                             from parameter in component.Parameters
                                             where parameter.Binding is ParameterValueManualConstant
                                             select new
            {
                ComponentName  = component.Name,
                ParameterName  = parameter.Name,
                DimensionTypes = parameter.DimensionTypes,
                DataType       = parameter.DataType,
                Value          = ((ParameterValueManualConstant)parameter.Binding).Value,
                paer           = parameter
            };

            var parametersWithLambdas = from component in Components
                                        from parameter in component.Parameters
                                        where parameter.Binding is ParameterValueManuelLambda
                                        select new
            {
                ComponentName  = component.Name,
                ParameterName  = parameter.Name,
                DimensionTypes = parameter.DimensionTypes,
                DataType       = parameter.DataType,
                Value          = ((ParameterValueManuelLambda)parameter.Binding).Lambda,
                paer           = parameter
            };

            var parametersInFileThatAreNotBound = from p in parameters
                                                  let pName = p.Name.ToLowerInvariant()
                                                              where !parametersToFindValueFor.Any(i => pName == i.ParameterName.ToLowerInvariant()) && pName != "region"
                                                              select p.Name;

            if (_logLevel > 0)
            {
                if (parametersInFileThatAreNotBound.Count() > 0)
                {
                    Console.WriteLine();
                    foreach (var p in parametersInFileThatAreNotBound)
                    {
                        Console.WriteLine("WARNING: Parameter '{0}' is not used by model", p);
                    }
                }
            }


            foreach (var p in parametersWithManualValues)
            {
                mf.Add0DimensionalVariable(p.ComponentName, p.ParameterName, p.Value.GetType(), p.Value);
            }

            foreach (var p in parametersWithLambdas)
            {
                if (p.Value.GetType().GetGenericTypeDefinition() == typeof(Func <, ,>))
                {
                    var types = p.Value.GetType().GetGenericArguments();

                    var method = mf.GetType().GetMethod("Add2DimensionalParameterLambda").MakeGenericMethod(types);
                    method.Invoke(mf, new object[] { p.ComponentName, p.ParameterName, p.Value });
                }
                else
                {
                    throw new NotImplementedException();
                }
            }

            foreach (var p in parametersToFindValueFor)
            {
                if (parameters.Contains(p.ParameterName))
                {
                    mf.LoadVariableFromParameter(p.ComponentName, p.ParameterName, parameters, p.DataType, p.DimensionTypes.ToArray());
                }
                else if (p.DefaultValue != null)
                {
                    mf.Add0DimensionalVariable(p.ComponentName, p.ParameterName, p.DataType, p.DefaultValue);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }

            foreach (var v in forcedVariablesToFindValueFor)
            {
                var forced = (ParameterValueFile)v.vaer.Forced;

                if (forced.Parametername == null && parameters.Contains(v.VariableName))
                {
                    mf.LoadVariableFromParameter("asdfasddf", v.VariableName, parameters, v.DataType, v.DimensionTypes.ToArray());

                    ((FieldVariable1DimensionalForceTypeless)mf[v.ComponentName, v.VariableName]).SetSource(mf["asdfasddf", v.VariableName]);
                }
                if (forced.Parametername != null && parameters.Contains(forced.Parametername))
                {
                    mf.LoadVariableFromParameter("asdfasddf", forced.Parametername, parameters, v.DataType, v.DimensionTypes.ToArray());

                    ((FieldVariable1DimensionalForceTypeless)mf[v.ComponentName, v.VariableName]).SetSource(mf["asdfasddf", forced.Parametername]);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
        }