/// <summary>
        /// reads an AddOn's EM2 country-XML-files and transfers it to EM3 style
        /// creates the AddOn folder, if it does not exist, and overwrites any existing country AddOn
        /// note: the intended usage is "single AddOn transformation"
        ///       the EM3All class is responsible for complete EM-content transformation (using the EM3Country.Write function)
        /// </summary>
        /// <param name="emPath"> EuromodFiles folder (containing EM2-files in XMLParam and (will contain) EM3-files in EM3Translation\XMLParam) </param>
        /// <param name="addOn"> short-name of AddOn </param>
        /// <param name="errors"> critical and non-critical erros during the transformation-process, empty structure for no errors </param>
        public static bool TransformAddOn(string emPath, string addOn, out List <string> errors)
        {
            errors = new List <string>(); EMPath pathHandler = new EMPath(emPath);
            try
            {
                DirectoryInfo di = Directory.CreateDirectory(pathHandler.GetAddOnFolderPath(addOn));

                // read EM2-files
                string em2AddOnFile = pathHandler.GetAddOnFilePath(addOn: addOn, em2: true);

                if (TransformerCommon.IsFileUpToDate(em2AddOnFile, pathHandler.GetAddOnFolderPath(addOn), out string hashCode))
                {
                    return(true);
                }

                EM2Country.Content addOnContent = EM2Country.Read(em2AddOnFile, out errors);
                if (addOnContent == null)
                {
                    return(false);
                }

                // write EM3-file (includes EM2->EM3 adaptations, via EM23Adapt class)
                string em3AddOnFile = pathHandler.GetAddOnFilePath(addOn);
                bool   success      = Write(addOnContent, null, new List <List <MultiProp> >(), em3AddOnFile, out List <string> wErrors);
                errors.AddRange(wErrors);

                if (success && errors.Count == 0)
                {
                    TransformerCommon.WriteUpToDate(em2AddOnFile, pathHandler.GetAddOnFolderPath(addOn), hashCode);
                }
                return(success);
            }
            catch (Exception exception)
            {
                errors.Add($"{addOn}: {exception.Message}");
                return(false);
            }
        }
        /// <summary> reads all components of an EM2 version </summary>
        /// <param name="emPath"> path to EUROMOD version (e.g. C:\euromod\EuromodContent\EuromodFiles_H0.13\) </param>
        /// <param name="errors"> list of errors, only critical errors lead to failure-return (null), i.e. there may be errors on "success"</param>
        /// <param name="progressAction">
        /// optional function for progress reporting, in the form 'void funName(string message)'
        /// is called for each item (country, add-on, global file) and sets message to e.g. 'Finished reading ...', 'Failed reading ...'
        /// </param>
        /// <param name="cancelSrc"> optional cancellation item </param>
        /// <returns> Content structure upon success, null upon failure </returns>
        public static Content Read(string emPath, out List <string> errors,
                                   Action <string> progressAction = null, CancellationTokenSource cancelSrc = null)
        {
            Content       content = new Content();
            List <string> _errors = new List <string>();

            object          writeLock       = new object();
            ParallelOptions parallelOptions = new ParallelOptions();

            if (cancelSrc != null)
            {
                parallelOptions.CancellationToken = cancelSrc.Token;
            }

            try
            {
                EMPath pathHandler = new EMPath(emPath);

                // READ COUNTRIES IN PARALLEL
                Parallel.ForEach(new DirectoryInfo(pathHandler.GetFolderCountries(em2: true)).GetDirectories(), parallelOptions, folder =>
                {
                    parallelOptions.CancellationToken.ThrowIfCancellationRequested();

                    string ctryFileName = pathHandler.GetCountryFilePath(country: folder.Name, em2: true);
                    string dataFileName = pathHandler.GetEM2DataConfigFilePath(country: folder.Name);

                    EM2Country.Content c = EM2Country.Read(ctryFileName, out List <string> cErrors);
                    EM2Data.Content d    = EM2Data.Read(dataFileName, out List <string> dErrors);

                    bool success = c != null && d != null;
                    lock (writeLock)
                    {
                        if (success)
                        {
                            content.countries.Add(new CountryContent()
                            {
                                country = c, data = d
                            });
                        }
                        _errors.AddRange(cErrors); _errors.AddRange(dErrors);
                    }
                    ReportSuccess(folder.Name, success, cErrors.Count + dErrors.Count);
                });
                // the rest is read sequentially (parallel would be unnecessary overhead)

                // READ ADD-ONS
                foreach (DirectoryInfo folder in new DirectoryInfo(pathHandler.GetFolderAddOns(em2: true)).GetDirectories())
                {
                    if ((new List <string>()
                    {
                        "HypoData", "SumStat", "XYZ"
                    }).Contains(folder.Name))
                    {
                        continue; // that's just old stuff, I think hardcoding isn't a faux pas
                    }
                    string             aoFileName = pathHandler.GetAddOnFilePath(addOn: folder.Name, em2: true);
                    EM2Country.Content ao         = EM2Country.Read(aoFileName, out List <string> aoErrors);
                    if (ao != null)
                    {
                        content.addOns.Add(ao);
                    }
                    _errors.AddRange(aoErrors); ReportSuccess(folder.Name, ao != null, aoErrors.Count);
                }
                ;

                // READ GLOBAL FILES
                string configPath = pathHandler.GetFolderConfig(em2: true);
                content.exRates = EM2Global.ReadExRates(configPath, out List <string> exErrors); _errors.AddRange(exErrors);
                ReportSuccess("Exchange rates", exErrors.Count == 0, exErrors.Count);
                content.hicp = EM2Global.ReadHICP(configPath, out List <string> hicpErrors); _errors.AddRange(hicpErrors);
                ReportSuccess("HICP", hicpErrors.Count == 0, hicpErrors.Count);
                content.switchPol = EM2Global.ReadSwitchPol(configPath, out List <string> spErrors); _errors.AddRange(spErrors);
                ReportSuccess("Policy switches", spErrors.Count == 0, spErrors.Count);

                // READ VARIABLES
                content.varConfig = EM2Variables.Read(configPath, out List <string> vErrors); _errors.AddRange(vErrors);
                ReportSuccess(EM2Variables.FILE_VARCONFIG, content.varConfig != null, vErrors.Count);

                return(content);
            }
            catch (OperationCanceledException) { progressAction($"Reading {emPath} cancelled!"); return(content); }
            catch (Exception exception)
            {
                lock (writeLock) { _errors.Add(exception.Message); } return(null);
            }
            finally { errors = _errors; }

            void ReportSuccess(string what, bool success, int cntErrors)
            {
                if (success)
                {
                    progressAction($"Finished reading {what} with " + $"{(cntErrors == 0 ? "success" : $"{cntErrors} errors") }");
                }
示例#3
0
        /// <summary> note: this is a private function, exclusively called by EM3All.Transform, just for reasons of clearer arrangement </summary>
        private static bool Write(EM2All.Content content, string emPath, out List <string> errors,
                                  Action <string> report = null, CancellationTokenSource cancelSrc = null)
        {
            List <string> _errors = new List <string>();

            object          errWriteLock    = new object();
            ParallelOptions parallelOptions = new ParallelOptions();

            if (cancelSrc != null)
            {
                parallelOptions.CancellationToken = cancelSrc.Token;
            }

            try
            {
                EMPath pathHandler = new EMPath(emPath);

                // WRITE COUNTRIES IN PARALLEL
                Parallel.ForEach(content.countries, parallelOptions, country =>
                {
                    parallelOptions.CancellationToken.ThrowIfCancellationRequested();

                    string countryName = country.country.general.properties[EM2TAGS.SHORTNAME];
                    DirectoryInfo di   = Directory.CreateDirectory(pathHandler.GetCountryFolderPath(countryName));
                    bool success       = EM3Country.Write(country.country, country.data, content.switchPol,
                                                          pathHandler.GetCountryFilePath(countryName), out List <string> cErrors);
                    if (cErrors.Count > 0)
                    {
                        lock (errWriteLock) { _errors.AddRange(cErrors); }
                    }
                    ReportSuccess(countryName, success, cErrors.Count);

                    if (success && cErrors.Count == 0) // produce up2Date-files
                    {
                        TransformerCommon.WriteUpToDate(pathHandler.GetCountryFilePath(countryName, true), pathHandler.GetCountryFolderPath(countryName));
                        TransformerCommon.WriteUpToDate(pathHandler.GetEM2DataConfigFilePath(countryName), pathHandler.GetCountryFolderPath(countryName));
                    }
                });
                // the rest is written sequentially (parallel would be unnecessary overhead)

                // WRITE ADD-ONS
                foreach (var addOn in content.addOns)
                {
                    string        addOnName = addOn.general.properties[EM2TAGS.SHORTNAME];
                    DirectoryInfo di        = Directory.CreateDirectory(pathHandler.GetAddOnFolderPath(addOnName));
                    bool          success   = EM3Country.Write(addOn, null, content.switchPol,
                                                               pathHandler.GetAddOnFilePath(addOnName), out List <string> aoErrors);
                    if (aoErrors.Count > 0)
                    {
                        _errors.AddRange(aoErrors);
                    }
                    ReportSuccess(addOnName, success, aoErrors.Count);

                    // produce up2Date-file
                    if (success && aoErrors.Count == 0)
                    {
                        TransformerCommon.WriteUpToDate(pathHandler.GetAddOnFilePath(addOnName, true), pathHandler.GetAddOnFolderPath(addOnName));
                    }
                }

                // WRITE GLOBAL FILES
                if (!Directory.Exists(pathHandler.GetFolderConfig()))
                {
                    Directory.CreateDirectory(pathHandler.GetFolderConfig());
                }
                bool erSuccess = EM3Global.WriteExRates(content.exRates, emPath, out List <string> erErrors);
                ReportSuccess("Exchangerates", erSuccess, erErrors.Count); _errors.AddRange(erErrors);
                bool hicSuccess = EM3Global.WriteHICP(content.hicp, emPath, out List <string> hicErrors);
                ReportSuccess("HICP", hicSuccess, hicErrors.Count); _errors.AddRange(hicErrors);
                bool gsSuccess = EM3Global.WriteExtensions(content.switchPol, emPath, out List <string> gsErrors);
                ReportSuccess("Extensions", gsSuccess, gsErrors.Count); _errors.AddRange(gsErrors);

                // WRITE VARIABLES
                bool varSuccess = EM3Variables.Write(content.varConfig, emPath, out List <string> vErrors);
                ReportSuccess("Variables", varSuccess, vErrors.Count); _errors.AddRange(vErrors);
                if (varSuccess && vErrors.Count == 0)
                {
                    TransformerCommon.WriteUpToDate(pathHandler.GetVarFilePath(true), pathHandler.GetFolderConfig());                                   // produce up2Date-file
                }
                return(true);
            }
            catch (OperationCanceledException) { report($"Writing {emPath} cancelled!"); return(true); }
            catch (Exception exception)
            {
                lock (errWriteLock) { _errors.Add(exception.Message); }
                return(false);
            }
            finally { errors = _errors; }

            void ReportSuccess(string what, bool success, int cntErrors)
            {
                if (success)
                {
                    report($"Finished writing {what} with " + $"{(cntErrors == 0 ? "success" : $"{cntErrors} errors") }");
                }