private bool EM3_Transform()
        {
            try
            {
                lock (transformLock)
                {
                    string transformerErrors = string.Empty;

                    foreach (string country in countriesToTransform)
                    {
                        EM3Country.Transform(EM_AppContext.FolderEuromodFiles, country, out List <string> errors);
                        foreach (string error in errors)
                        {
                            transformerErrors += error + Environment.NewLine;
                        }
                    }
                    foreach (string addOn in addOnsToTransform)
                    {
                        EM3Country.TransformAddOn(EM_AppContext.FolderEuromodFiles, addOn, out List <string> errors);
                        foreach (string error in errors)
                        {
                            transformerErrors += error + Environment.NewLine;
                        }
                    }

                    EM3Global.Transform(EM_AppContext.FolderEuromodFiles, out List <string> gErrors);
                    foreach (string error in gErrors)
                    {
                        transformerErrors += error + Environment.NewLine;
                    }

                    EM3Variables.Transform(EM_AppContext.FolderEuromodFiles, out List <string> vErrors);
                    foreach (string error in vErrors)
                    {
                        transformerErrors += error + Environment.NewLine;
                    }

                    if (transformerErrors == string.Empty)
                    {
                        return(true);
                    }
                    return(UserInfoHandler.GetInfo("Errors on transforming to EM3 structure:" + Environment.NewLine + transformerErrors + Environment.NewLine +
                                                   "Do you want to continue?", MessageBoxButtons.YesNo) == DialogResult.Yes);
                }
            }
            catch (Exception exception)
            {
                return(UserInfoHandler.GetInfo("Errors on transforming to EM3 structure:" + Environment.NewLine + exception.Message +
                                               Environment.NewLine + Environment.NewLine + "Do you want to continue?",
                                               MessageBoxButtons.YesNo) == DialogResult.Yes);
            }
        }
        private void EM3_Transform(string country, string addOn)
        {
            try
            {
                EMPath emPath = new EMPath(EM_AppContext.FolderEuromodFiles);
                lock (transformLock)
                {
                    bool success = TransformCountry() && TransformAddOn() &&
                                   (em3_transformGlobals == false || (TransformGlobals() && TransformVariables()));
                }
                bool TransformCountry()
                {
                    bool ok = EM3Country.Transform(emPath.GetFolderEuromodFiles(), country, out List <string> transErrors);

                    AddErrorRange(transErrors); return(ok);
                }
                bool TransformGlobals()
                {
                    bool ok = EM3Global.Transform(emPath.GetFolderEuromodFiles(), out List <string> transErrors);

                    AddErrorRange(transErrors); return(ok);
                }
                bool TransformVariables()
                {
                    bool ok = EM3Variables.Transform(emPath.GetFolderEuromodFiles(), out List <string> transErrors);

                    AddErrorRange(transErrors); return(ok);
                }
                bool TransformAddOn()
                {
                    if (string.IsNullOrEmpty(addOn))
                    {
                        return(true);
                    }
                    bool ok = EM3Country.TransformAddOn(emPath.GetFolderEuromodFiles(), addOn, out List <string> transErrors);

                    AddErrorRange(transErrors); return(ok);
                }
            }
            catch (Exception exception) { em3_petInfo.AddSystemIndependentError(exception.Message); }

            void AddErrorRange(List <string> errors)
            {
                foreach (string error in errors)
                {
                    em3_petInfo.AddSystemIndependentError(error);
                }
            }
        }
        internal static int GoUI(string[] args)
        {
            try
            {
                if (args.Length < 2)
                {
                    return(WrongArgumentError());
                }

                string emPath = EM_Helpers.RemoveQuotes(args[0].Trim());
                string cc = args[1], ao = args.Length > 2 ? args[2] : string.Empty;

                foreach (string country in cc.Split('|', StringSplitOptions.RemoveEmptyEntries))
                {
                    EM3Country.Transform(emPath, country, out List <string> errors);
                    foreach (string error in errors)
                    {
                        Console.Error.WriteLine(error);
                    }
                }
                foreach (string addOn in ao.Split('|', StringSplitOptions.RemoveEmptyEntries))
                {
                    EM3Country.TransformAddOn(emPath, addOn, out List <string> errors);
                    foreach (string error in errors)
                    {
                        Console.Error.WriteLine(error);
                    }
                }

                EM3Global.Transform(emPath, out List <string> gErrors);
                foreach (string error in gErrors)
                {
                    Console.Error.WriteLine(error);
                }

                EM3Variables.Transform(emPath, out List <string> vErrors);
                foreach (string error in vErrors)
                {
                    Console.Error.WriteLine(error);
                }

                return(0); // as it is unlikely that one can run (with useful results) if there are errors, the UI shows the errors and asks the user
            }
            catch (Exception exception)
            {
                Console.Error.WriteLine("EUROMOD3 TRANSFORM: " + exception.Message);
                return(1);
            }

            int WrongArgumentError()
            {
                string actArgs = string.Empty; foreach (string arg in args)

                {
                    actArgs += arg + " ";
                }

                Console.Error.WriteLine("EUROMOD3 TRANSFORM: invalid use. Correct: EM_ExecutableCaller UI_TRANSFORM emPath cc1[|cc2|...|ccN] [ao1[|ao2|...|aoN]]" +
                                        Environment.NewLine + $"Acutal use: EM_ExecutableCaller TRANSFORM {actArgs}");
                return(1);
            }
        }
Example #4
0
        internal static int Go(string[] args)
        {
            try
            {
                args = Test.TPlay();

                if (args.Length == 0)
                {
                    return(WrongUseError());
                }

                // get arguments into a dictionary
                List <string> _args = (from a in args select a.Trim()).ToList();
                Dictionary <string, string> arguments = args[0].Trim().StartsWith("-") ? GetArgsEM3Style(_args) : GetArgsEM2Style(_args);
                if (arguments == null)
                {
                    return(WrongUseError());
                }

                // prepare logging
                RunLogger.RunInfo runInfo = new RunLogger.RunInfo();

                // translate or create config
                Dictionary <string, string> em3Config = null; List <KeyValuePair <string, string> > em2Config = null;
                if (arguments.ContainsKey(ARG_CONFIG))
                {
                    if (!TransformEMConfig.Transform(arguments[ARG_CONFIG], out em2Config, out em3Config,
                                                     err => { WriteErr($"EM3 CONFIG-TRANSFORMER", err); }))
                    {
                        return(1);
                    }
                    arguments.Remove(ARG_CONFIG);
                }
                else
                {
                    if (!arguments.ContainsKey(ARG_EMPATH) || !arguments.ContainsKey(ARG_SYS) || !arguments.ContainsKey(ARG_DATA))
                    {
                        return(WrongUseError("Compulsory argument missing."));
                    }
                    CreateConfig(arguments[ARG_EMPATH], arguments[ARG_SYS], arguments[ARG_DATA], out em3Config); // create a minimal-config
                    arguments.Remove(ARG_EMPATH); arguments.Remove(ARG_SYS); arguments.ContainsKey(ARG_DATA);
                }

                // handle optional parameters
                int addOnCnt = 0, extCnt = 0;
                foreach (var optArg in arguments)
                {
                    if (optArg.Key.ToLower() == ARG_OUTPATH.ToLower())
                    {
                        em3Config.AddOrReplace(TAGS.CONFIG_PATH_OUTPUT, optArg.Value);
                    }
                    else if (optArg.Key.ToLower() == ARG_DATAPATH.ToLower())
                    {
                        em3Config.AddOrReplace(TAGS.CONFIG_PATH_DATA, optArg.Value);
                    }
                    else if (optArg.Key.ToLower() == ARG_GLOBALPATH.ToLower())
                    {
                        em3Config.AddOrReplace(TAGS.CONFIG_PATH_GLOBAL, optArg.Value);
                    }
                    else if (optArg.Key.ToLower() == ARG_SEQUENTIAL_RUN.ToLower())
                    {
                        em3Config.AddOrReplace(TAGS.CONFIG_FORCE_SEQUENTIAL_RUN, DefPar.Value.YES);
                    }
                    else if (optArg.Key.ToLower() == ARG_SEQUENTIAL_OUTPUT.ToLower())
                    {
                        em3Config.AddOrReplace(TAGS.CONFIG_FORCE_SEQUENTIAL_OUTPUT, DefPar.Value.YES);
                    }
                    else if (optArg.Key.ToLower() == ARG_OUTPUT_IN_EURO.ToLower())
                    {
                        em3Config.AddOrReplace(TAGS.CONFIG_FORCE_OUTPUT_EURO, DefPar.Value.YES);
                    }
                    else if (optArg.Key.ToLower().StartsWith(ARG_ADDON.ToLower()))
                    {
                        if (optArg.Value.Split("|").Count() != 2)
                        {
                            return(WrongUseError($"Invalid {ARG_ADDON} argument '{optArg.Value}' (correct: add-on-name|add-on-system, e.g. MTR|MTR_EL)"));
                        }
                        em3Config.Add($"{TAGS.CONFIG_ADDON}{addOnCnt++}", optArg.Value);
                    }
                    else if (optArg.Key.ToLower().StartsWith(ARG_EXTENSION_SWITCH.ToLower()))
                    {
                        if (optArg.Value.Split("=").Count() != 2)
                        {
                            return(WrongUseError($"Invalid {ARG_EXTENSION_SWITCH} argument '{optArg.Value}' (correct: extension-name=switch, e.g. BTA_??=off)"));
                        }
                        em3Config.Add($"{TAGS.EXTENSION_SWITCH}{extCnt++}", optArg.Value); // still extension-name needs to be replaced by id, see below
                    }
                    else if (optArg.Key.ToLower() == ARG_PATH_PAR_MODIFICATIONS.ToLower())
                    {
                        em3Config.AddOrReplace(TAGS.CONFIG_PATH_PAR_MODIFICATIONS, optArg.Value);
                    }
                    else if (optArg.Key.ToLower() == ARG_INPUT_PASSWORD.ToLower())
                    {
                        em3Config.AddOrReplace(TAGS.CONFIG_INPUT_PASSWORD, optArg.Value);
                    }
                    // more to come ...
                }

                if (!arguments.ContainsKey(ARG_NOTRANSLATE))
                {
                    // translate country and global files (but do not stop on error - maybe still works ...)
                    if (!em3Config.ContainsKey(TAGS.CONFIG_COUNTRY))
                    {
                        return(WrongUseError($"Configuration file does not contain tag {TAGS.CONFIG_COUNTRY}"));
                    }
                    if (!em3Config.ContainsKey(TAGS.CONFIG_PATH_EUROMODFILES))
                    {
                        return(WrongUseError($"Configuration file does not contain tag {TAGS.CONFIG_PATH_EUROMODFILES}"));
                    }
                    EM3Country.Transform(em3Config[TAGS.CONFIG_PATH_EUROMODFILES], em3Config[TAGS.CONFIG_COUNTRY], out List <string> errors);
                    foreach (string err in errors)
                    {
                        WriteErr("EM3 COUNTRY-TRANSFORMER", err);
                    }
                    EM3Global.Transform(em3Config[TAGS.CONFIG_PATH_EUROMODFILES], out errors);
                    foreach (string err in errors)
                    {
                        WriteErr("EM3 GLOBAL-FILE-TRANSFORMER", err);
                    }
                    EM3Variables.Transform(em3Config[TAGS.CONFIG_PATH_EUROMODFILES], out errors);
                    foreach (string err in errors)
                    {
                        WriteErr("EM3 VARIABLES-TRANSFORMER", err);
                    }

                    // translate possible add-ons
                    foreach (var entry in em3Config)
                    {
                        if (!entry.Key.StartsWith(TAGS.CONFIG_ADDON))
                        {
                            continue;
                        }
                        EM3Country.TransformAddOn(em3Config[TAGS.CONFIG_PATH_EUROMODFILES], entry.Value.Split('|').First(), out errors);
                        foreach (string err in errors)
                        {
                            WriteErr("EM3 ADD-ON-TRANSFORMER", err);
                        }
                    }
                }

                // replace extension-name by id in config, if necessary
                if (!HandleExtensionSwitches(em3Config, out Dictionary <string, string> modifiedExtensionSwitches))
                {
                    return(1);
                }

                // finally run
                DateTime startTime = DateTime.Now;
                int      returnCode = new EM_Executable.Control().Run(em3Config,
                                                                      prog =>
                {
                    Console.WriteLine(prog.message);
                    runInfo.ExtractOutputFiles(prog.detailedInfo);
                    return(true);
                },
                                                                      err => { WriteError(err); }) ? 0 : 1;

                WriteHeader(runInfo, returnCode, startTime, DateTime.Now, em2Config, em3Config, modifiedExtensionSwitches);

                if (returnCode == 1)
                {
                    Console.WriteLine($"{DefGeneral.BRAND_TITLE} run aborted with errors!");
                }
                return(returnCode);

                void WriteErr(string guiltyProgramme, string err)
                {
                    WriteError(new Communicator.ErrorInfo()
                    {
                        message = $"{guiltyProgramme}: {err}", isWarning = true
                    });
                }
                void WriteError(Communicator.ErrorInfo err)
                {
                    Console.Error.WriteLine((err.isWarning ? "warning: " : "error: ") + err.message);
                    runInfo.errorInfo.Add(err);
                }
            }
            catch (Exception exception) { return(WrongUseError(exception.Message)); }
        }