Example #1
0
        public ControllerModel ImportController(ControllerModel c = null)
        {
            if (c != null)
            {
                throw new NullReferenceException("TabC importer: Controller parsed is not null, which it should be for importing into new.");
            }


            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.CheckFileExists = true;
            openFileDialog.Title           = "Selecteer tab.c file voor importeren";
            openFileDialog.Filter          = "Import files|*tab.c;*.ccol|Alle files|*.*";

            ControllerModel newc = new ControllerModel();

            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    DefaultsProvider.Default.SetDefaultsOnModel(newc.Data);
                    newc.Data.GarantieOntruimingsTijden = false;

                    string[] lines = File.ReadAllLines(openFileDialog.FileName);
                    if (lines.Length <= 1)
                    {
                        throw new IndexOutOfRangeException("Het bestand heeft minder dan 2 regels.");
                    }

                    // Build a list of the Phases with conflicts from the tab.c file
                    TabCImportHelperOutcome NewData = TabCImportHelper.GetNewData(lines);
                    NewData.Fasen.BubbleSort();
                    foreach (FaseCyclusModel fcm in NewData.Fasen)
                    {
                        fcm.Type = Settings.Utilities.FaseCyclusUtilities.GetFaseTypeFromNaam(fcm.Naam);
                        DefaultsProvider.Default.SetDefaultsOnModel(fcm, fcm.Type.ToString());
                        newc.Fasen.Add(fcm);
                        var fcdm = new FaseCyclusModuleDataModel()
                        {
                            FaseCyclus = fcm.Naam
                        };
                        DefaultsProvider.Default.SetDefaultsOnModel(fcdm, fcm.Type.ToString());
                        newc.ModuleMolen.FasenModuleData.Add(fcdm);
                    }
                    NewData.Conflicten.BubbleSort();
                    foreach (ConflictModel cm in NewData.Conflicten)
                    {
                        newc.InterSignaalGroep.Conflicten.Add(cm);
                    }
                    return(newc);
                }
                catch (Exception e)
                {
                    System.Windows.MessageBox.Show("Fout bij uitlezen tab.c.:\n" + e.Message, "Fout bij importeren tab.c");
                    return(null);
                }
            }
            return(null);
        }
        public ControllerModel ImportController(ControllerModel c = null)
        {
            if (c != null)
            {
                throw new NotImplementedException("TabC importer: Controller parsed is not null, which it should be for importing into new.");
            }


            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.CheckFileExists = true;
            openFileDialog.Title           = "Selecteer tab.c file voor importeren";
            openFileDialog.Filter          = "tab.c files|*tab.c|Alle files|*.*";

            ControllerModel newc = new ControllerModel();

            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    string[] lines = File.ReadAllLines(openFileDialog.FileName);
                    if (lines.Count() <= 1)
                    {
                        throw new NotImplementedException("Het bestand heeft minder dan 2 regels.");
                    }

                    // Build a list of the Phases with conflicts from the tab.c file
                    TabCImportHelperOutcome NewData = TabCImportHelper.GetNewData(lines);
                    NewData.Fasen.BubbleSort();
                    foreach (FaseCyclusModel fcm in NewData.Fasen)
                    {
                        newc.Fasen.Add(fcm);
                    }
                    NewData.Conflicten.BubbleSort();
                    foreach (ConflictModel cm in NewData.Conflicten)
                    {
                        newc.InterSignaalGroep.Conflicten.Add(cm);
                    }
                    return(newc);
                }
                catch (Exception e)
                {
                    System.Windows.MessageBox.Show("Fout bij uitlezen tab.c.:\n" + e.Message, "Fout bij importeren tab.c");
                    return(null);
                }
            }
            return(null);
        }
Example #3
0
        public static TabCImportHelperOutcome GetNewData(string[] lines, bool newReg)
        {
            var outcome = new TabCImportHelperOutcome();

            if (lines.Length <= 1)
            {
                return(null);
            }

            var tabCType = TabCType.UNKNOWN;

            if (lines.Any(x => ReTypeOTTO.IsMatch(x)))
            {
                tabCType = TabCType.OTTO;
            }
            if (tabCType == TabCType.UNKNOWN && lines.Any(x => ReTypeTLCGen.IsMatch(x)))
            {
                tabCType = TabCType.TLCGEN;
            }
            if (tabCType == TabCType.UNKNOWN && lines.Any(x => ReTypeTPA.IsMatch(x)))
            {
                tabCType = TabCType.TPA;
            }
            if (tabCType == TabCType.UNKNOWN && lines.Any(x => ReTypeATB.IsMatch(x)))
            {
                tabCType = TabCType.ATB;
            }
            if (tabCType == TabCType.UNKNOWN && lines.Any(x => ReTypeFICK.IsMatch(x)))
            {
                tabCType = TabCType.FICK;
            }
            if (tabCType == TabCType.UNKNOWN && lines.Any(x => ReTypeHUIJSKES.IsMatch(x)))
            {
                tabCType = TabCType.HUIJSKES;
            }
            if (tabCType == TabCType.UNKNOWN && lines.Any(x => ReTypeGC.IsMatch(x)))
            {
                tabCType = TabCType.GC;
            }
            var intergroen = lines.Any(x => ReIntergreen.IsMatch(x));
            var garantie   = lines.Any(x => ReGarantie.IsMatch(x));

            outcome.Intergroen = intergroen;
            outcome.Garantie   = garantie;

            var  applyDefaults = false;
            bool importD;
            bool importT;
            var  importG        = false;
            var  importNalopen  = false;
            var  importDeelconf = "";

            if (TLCGenDialogProvider.Default.ShowDialogs)
            {
                var dlg = new ChooseTabTypeWindow
                {
                    Intergroen       = intergroen,
                    ImportGarantie   = garantie,
                    HasIntergroen    = intergroen,
                    TabType          = tabCType,
                    ImportInExisting = !newReg
                };
                var res = dlg.ShowDialog();
                tabCType = dlg.TabType;
                if (res == false)
                {
                    return(null);
                }
                applyDefaults    = dlg.ToepassenDefaults;
                importD          = dlg.ImportDetectoren;
                importT          = dlg.ImportTijden;
                outcome.Garantie = importG = dlg.ImportGarantie;
                importDeelconf   = dlg.ImportDeelconflicten;
                importNalopen    = dlg.ImportNalopen;
            }
            else
            {
                importD = true;
                importT = true;
            }

            // get meta data
            if (tabCType == TabCType.OTTO)
            {
                var kNaamRegex    = new Regex(@"^\s*/\*\sKruispunt\snaam:\s*(.*)\s*\*/", RegexOptions.Compiled);
                var kLocatieRegex = new Regex(@"^\s*/\*\sKruispunt\slocatie:\s*(.*)\s*\*/", RegexOptions.Compiled);
                foreach (var l in lines)
                {
                    var naamM = kNaamRegex.Match(l);
                    if (naamM.Success && naamM.Groups.Count > 1)
                    {
                        outcome.KruisingNaam = naamM.Groups[1].Value.Trim().TrimEnd(' ');
                    }
                    else
                    {
                        var locatieM = kLocatieRegex.Match(l);
                        if (locatieM.Success)
                        {
                            if (locatieM.Success && locatieM.Groups.Count > 1)
                            {
                                var streets = locatieM.Groups[1].Value.Split('-');
                                if (streets.Length >= 1)
                                {
                                    var str1 = Regex.Replace(streets[0], @"^\s*", "");
                                    outcome.KruisingStraat1 = Regex.Replace(str1, @"\s*$", "");
                                }
                                if (streets.Length >= 2)
                                {
                                    var str2 = Regex.Replace(streets[1], @"^\s*", "");
                                    outcome.KruisingStraat2 = Regex.Replace(str2, @"\s*$", "");
                                }
                            }
                        }
                    }
                }
            }

            // get phases
            Regex fasenRegex = null;

            switch (tabCType)
            {
            case TabCType.UNKNOWN:
            case TabCType.OTTO:
                if (intergroen)
                {
                    fasenRegex = new Regex(@"^\s*TIG_max\s*\[\s*(?<name>fc[0-9]+).*", RegexOptions.Compiled);
                }
                else
                {
                    fasenRegex = new Regex(@"^\s*TO_max\s*\[\s*(?<name>fc[0-9]+).*", RegexOptions.Compiled);
                }

                break;

            case TabCType.TLCGEN:
            case TabCType.TPA:
            case TabCType.FICK:
            case TabCType.HUIJSKES:
                fasenRegex = new Regex(@"^\s*FC_code\s*\[\s*(?<name>fc[0-9]+).*", RegexOptions.Compiled);
                break;

            case TabCType.ATB:
            case TabCType.GC:
                fasenRegex = new Regex(@"^\s*FC\s*\(\s*(?<name>fc[0-9]+).*", RegexOptions.Compiled);
                break;
            }
            foreach (var l in lines)
            {
                var m = fasenRegex.Match(l);
                if (m.Success)
                {
                    var name = m.Groups["name"].Value.Replace("fc", "");
                    if (outcome.Fasen.All(x => x.Naam != name))
                    {
                        var fcm = new FaseCyclusModel
                        {
                            Naam = name.ToLower()
                        };
                        fcm.Type = Settings.Utilities.FaseCyclusUtilities.GetFaseTypeFromNaam(fcm.Naam);
                        if (applyDefaults)
                        {
                            DefaultsProvider.Default.SetDefaultsOnModel(fcm, fcm.Type.ToString());
                        }
                        outcome.Fasen.Add(fcm);
                    }
                }
            }

            // import conflicts
            Regex confRegex  = null;
            Regex gconfRegex = null;
            Regex dconfRegex = null;
            Regex geelRegex  = null;

            switch (tabCType)
            {
            case TabCType.TLCGEN:
            case TabCType.UNKNOWN:
            case TabCType.OTTO:
            case TabCType.TPA:
            case TabCType.FICK:
            case TabCType.HUIJSKES:
                if (intergroen)
                {
                    dconfRegex = new Regex(@"^\s*\/\*\s*TIG_max\s*\[\s*fc(?<fc1>[0-9]+)\s*\]\s*\[\s*fc(?<fc2>[0-9]+)\s*\]\s*=\s*(?<conf>([0-9]+|FK|GK|GKL));\s*=\s*deelconflict\s*\*\/.*");
                    gconfRegex = new Regex(@"^\s*TIG_min\s*\[\s*fc(?<fc1>[0-9]+)\s*\]\s*\[\s*fc(?<fc2>[0-9]+)\s*\]\s*=\s*(?<conf>([0-9]+|FK|GK|GKL)).*");
                    confRegex  = new Regex(@"^\s*TIG_max\s*\[\s*fc(?<fc1>[0-9]+)\s*\]\s*\[\s*fc(?<fc2>[0-9]+)\s*\]\s*=\s*(?<conf>([0-9]+|FK|GK|GKL)).*");
                    geelRegex  = new Regex(@"^\s*TGL_max\s*\[\s*fc(?<fc1>[0-9]+)\s*\]\s?=\s?(?<geel>[0-9]+).*");
                }
                else
                {
                    dconfRegex = new Regex(@"^\s*\/\*\s*TO_max\s*\[\s*fc(?<fc1>[0-9]+)\s*\]\s*\[\s*fc(?<fc2>[0-9]+)\s*\]\s*=\s*(?<conf>([0-9]+|FK|GK|GKL));\s*=\s*deelconflict\s*\*\/.*");
                    gconfRegex = new Regex(@"^\s*TO_min\s*\[\s*fc(?<fc1>[0-9]+)\s*\]\s*\[\s*fc(?<fc2>[0-9]+)\s*\]\s*=\s*(?<conf>([0-9]+|FK|GK|GKL)).*");
                    confRegex  = new Regex(@"^\s*TO_max\s*\[\s*fc(?<fc1>[0-9]+)\s*\]\s*\[\s*fc(?<fc2>[0-9]+)\s*\]\s*=\s*(?<conf>([0-9]+|FK|GK|GKL)).*");
                }
                break;

            case TabCType.ATB:
            case TabCType.GC:
                confRegex  = new Regex(@"^\s*TO\(\s*fc(?<fc1>[0-9]+)\s*,\s*fc(?<fc2>[0-9]+)\s*,\s*(?<conf>([0-9]+|FK|GK|GKL)).*");
                gconfRegex = new Regex(@"^\s*TO\(\s*fc(?<fc1>[0-9]+)\s*,\s*fc(?<fc2>[0-9]+)\s*,\s*([0-9]+|FK|GK|GKL),\s*(?<conf>([0-9]+)).*");
                break;
            }
            var clines = lines.Where(x => !ReComment.IsMatch(x));

            if (!string.IsNullOrWhiteSpace(importDeelconf))
            {
                clines = lines;
            }

            foreach (var l in clines)
            {
                var m = confRegex.Match(l);
                if (m.Success)
                {
                    var fc1  = m.Groups["fc1"].Value;
                    var fc2  = m.Groups["fc2"].Value;
                    var conf = m.Groups["conf"].Value;
                    if (int.TryParse(conf, out var iconf))
                    {
                        var cconf = outcome.Conflicten.FirstOrDefault(x => x.FaseVan == fc1 && x.FaseNaar == fc2);
                        if (cconf != null)
                        {
                            cconf.Waarde = iconf;
                        }
                        else
                        {
                            outcome.Conflicten.Add(new ConflictModel
                            {
                                FaseVan  = fc1,
                                FaseNaar = fc2,
                                Waarde   = iconf
                            });
                        }
                    }
                }

                if (importG)
                {
                    m = gconfRegex.Match(l);
                    if (m.Success)
                    {
                        var fc1  = m.Groups["fc1"].Value;
                        var fc2  = m.Groups["fc2"].Value;
                        var conf = m.Groups["conf"].Value;
                        if (int.TryParse(conf, out var iconf))
                        {
                            var cconf = outcome.Conflicten.FirstOrDefault(x => x.FaseVan == fc1 && x.FaseNaar == fc2);
                            if (cconf != null)
                            {
                                cconf.GarantieWaarde = iconf;
                            }
                            else
                            {
                                outcome.Conflicten.Add(new ConflictModel
                                {
                                    FaseVan        = fc1,
                                    FaseNaar       = fc2,
                                    GarantieWaarde = iconf
                                });
                            }
                        }
                    }
                }

                if (intergroen)
                {
                    m = geelRegex.Match(l);
                    if (m.Success)
                    {
                        var fc1  = m.Groups["fc1"].Value;
                        var geel = m.Groups["geel"].Value;
                        if (int.TryParse(geel, out var igeel))
                        {
                            var fc = outcome.Fasen.FirstOrDefault(x => x.Naam == fc1);
                            if (fc != null)
                            {
                                fc.TGL_min = igeel;
                                fc.TGL     = igeel;
                            }
                        }
                    }
                }
                else if (!string.IsNullOrWhiteSpace(importDeelconf))
                {
                    m = dconfRegex.Match(l);
                    if (m.Success)
                    {
                        var fc1  = m.Groups["fc1"].Value;
                        var fc2  = m.Groups["fc2"].Value;
                        var conf = m.Groups["conf"].Value;
                        if (int.TryParse(conf, out var iconf) &&
                            !outcome.Conflicten.Any(x => x.FaseVan == fc1 && x.FaseNaar == fc2))
                        {
                            var nfc1 = outcome.Fasen.FirstOrDefault(x => x.Naam == fc1);
                            var nfc2 = outcome.Fasen.FirstOrDefault(x => x.Naam == fc2);

                            var skip = false;
                            if (int.TryParse(nfc1.Naam, out var iFc1) && int.TryParse(nfc2.Naam, out var iFc2))
                            {
                                if (iFc1 > 60 && iFc1 < 80 || iFc2 > 60 && iFc2 < 80)
                                {
                                    skip = true;
                                }
                            }

                            if (!skip)
                            {
                                // deelconflicten auto verkeer: gelijkstart
                                if ((nfc1.Type == FaseTypeEnum.Auto || nfc1.Type == FaseTypeEnum.OV) &&
                                    (nfc2.Type == FaseTypeEnum.Auto || nfc2.Type == FaseTypeEnum.OV))
                                {
                                    var gs = outcome.Gelijkstarten.FirstOrDefault(x =>
                                                                                  x.FaseNaar == fc1 && x.FaseVan == fc2);
                                    if (gs != null)
                                    {
                                        gs.GelijkstartOntruimingstijdFaseNaar = iconf;
                                    }
                                    else
                                    {
                                        outcome.Gelijkstarten.Add(new GelijkstartModel
                                        {
                                            FaseVan      = fc1,
                                            FaseNaar     = fc2,
                                            DeelConflict = true,
                                            GelijkstartOntruimingstijdFaseVan = iconf
                                        });
                                    }
                                }
                                // deelconflicten auto <> langzaam
                                else if ((nfc1.Type != FaseTypeEnum.Auto && nfc1.Type != FaseTypeEnum.OV) &&
                                         (nfc2.Type == FaseTypeEnum.Auto || nfc2.Type == FaseTypeEnum.OV) ||
                                         (nfc2.Type != FaseTypeEnum.Auto && nfc2.Type != FaseTypeEnum.OV) &&
                                         (nfc1.Type == FaseTypeEnum.Auto || nfc1.Type == FaseTypeEnum.OV))
                                {
                                    switch (importDeelconf)
                                    {
                                    case "Voorstarten":
                                        var vs = outcome.Voorstarten.FirstOrDefault(x =>
                                                                                    x.FaseNaar == fc1 && x.FaseVan == fc2 ||
                                                                                    x.FaseVan == fc1 && x.FaseNaar == fc2);
                                        // langzaam > auto
                                        if ((nfc1.Type != FaseTypeEnum.Auto && nfc1.Type != FaseTypeEnum.OV) &&
                                            (nfc2.Type == FaseTypeEnum.Auto || nfc2.Type == FaseTypeEnum.OV))
                                        {
                                            if (vs != null)
                                            {
                                                vs.VoorstartTijd = iconf;
                                            }
                                            else
                                            {
                                                outcome.Voorstarten.Add(new VoorstartModel
                                                {
                                                    FaseVan                  = fc1,
                                                    FaseNaar                 = fc2,
                                                    VoorstartTijd            = iconf,
                                                    VoorstartOntruimingstijd = 0
                                                });
                                                outcome.MeeAanvragen.Add(new MeeaanvraagModel
                                                {
                                                    FaseVan  = fc2,
                                                    FaseNaar = fc1,
                                                    Type     = MeeaanvraagTypeEnum.RoodVoorAanvraag,
                                                    AanUit   = AltijdAanUitEnum.SchAan
                                                });
                                            }
                                        }
                                        // auto > langzaam
                                        else
                                        {
                                            if (vs != null)
                                            {
                                                vs.VoorstartOntruimingstijd = iconf;
                                            }
                                            else
                                            {
                                                outcome.Voorstarten.Add(new VoorstartModel
                                                {
                                                    FaseVan                  = fc2,
                                                    FaseNaar                 = fc1,
                                                    VoorstartTijd            = 0,
                                                    VoorstartOntruimingstijd = iconf
                                                });
                                                outcome.MeeAanvragen.Add(new MeeaanvraagModel
                                                {
                                                    FaseVan  = fc1,
                                                    FaseNaar = fc2,
                                                    Type     = MeeaanvraagTypeEnum.RoodVoorAanvraag,
                                                    AanUit   = AltijdAanUitEnum.SchAan
                                                });
                                            }
                                        }

                                        break;

                                    case "Late release":
                                        var lr = outcome.LateReleases.FirstOrDefault(x =>
                                                                                     x.FaseNaar == fc1 && x.FaseVan == fc2 ||
                                                                                     x.FaseVan == fc1 && x.FaseNaar == fc2);
                                        // langzaam > auto
                                        if ((nfc1.Type != FaseTypeEnum.Auto && nfc1.Type != FaseTypeEnum.OV) &&
                                            (nfc2.Type == FaseTypeEnum.Auto || nfc2.Type == FaseTypeEnum.OV))
                                        {
                                            if (lr != null)
                                            {
                                                lr.LateReleaseTijd = iconf;
                                            }
                                            else
                                            {
                                                outcome.LateReleases.Add(new LateReleaseModel
                                                {
                                                    FaseVan                    = fc1,
                                                    FaseNaar                   = fc2,
                                                    LateReleaseTijd            = iconf,
                                                    LateReleaseOntruimingstijd = 0
                                                });
                                            }
                                        }
                                        // auto > langzaam
                                        else
                                        {
                                            if (lr != null)
                                            {
                                                lr.LateReleaseOntruimingstijd = iconf;
                                            }
                                            else
                                            {
                                                outcome.LateReleases.Add(new LateReleaseModel
                                                {
                                                    FaseVan                    = fc2,
                                                    FaseNaar                   = fc1,
                                                    LateReleaseTijd            = 0,
                                                    LateReleaseOntruimingstijd = iconf
                                                });
                                            }
                                        }

                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (importNalopen)
            {
                foreach (var fc in outcome.Fasen)
                {
                    if (int.TryParse(fc.Naam, out var iFc))
                    {
                        // Auto
                        if (iFc % 100 > 0 && iFc % 100 <= 12)
                        {
                            foreach (var fc2 in outcome.Fasen)
                            {
                                if (int.TryParse(fc2.Naam, out var iFc2))
                                {
                                    if (iFc2 > iFc && (iFc2 - iFc) == 60)
                                    {
                                        var nm = new NaloopModel
                                        {
                                            DetectieAfhankelijk = fc.Detectoren.Any(x => x.Type == DetectorTypeEnum.Kop),
                                            Detectoren          = fc.Detectoren.Where(x => x.Type == DetectorTypeEnum.Kop).Select(x => new NaloopDetectorModel {
                                                Detector = x.Naam, Type = NaloopDetectorTypeEnum.Hiaat
                                            }).ToList(),
                                            FaseVan              = fc.Naam,
                                            FaseNaar             = fc2.Naam,
                                            InrijdenTijdensGroen = false,
                                            MaximaleVoorstart    = null,
                                            Type = NaloopTypeEnum.EindeGroen
                                        };
                                        SetNaloopTijden(nm);
                                        outcome.Nalopen.Add(nm);
                                    }
                                }
                            }
                        }
                        // Voetganger
                        if (iFc % 100 > 30 && iFc % 100 < 40)
                        {
                            foreach (var fc2 in outcome.Fasen)
                            {
                                if (int.TryParse(fc2.Naam, out var iFc2))
                                {
                                    if (iFc % 2 == 1 && iFc2 % 2 == 0 && iFc2 - iFc == 1 ||
                                        iFc % 2 == 0 && iFc2 % 2 == 1 && iFc2 - iFc == -1)
                                    {
                                        var nm = new NaloopModel
                                        {
                                            DetectieAfhankelijk = fc.Detectoren.Any(x => x.Type == DetectorTypeEnum.KnopBuiten),
                                            Detectoren          = fc.Detectoren.Where(x => x.Type == DetectorTypeEnum.KnopBuiten).Select(x => new NaloopDetectorModel {
                                                Detector = x.Naam, Type = NaloopDetectorTypeEnum.Hiaat
                                            }).ToList(),
                                            FaseVan              = fc.Naam,
                                            FaseNaar             = fc2.Naam,
                                            InrijdenTijdensGroen = false,
                                            MaximaleVoorstart    = null,
                                            Type = NaloopTypeEnum.StartGroen
                                        };
                                        SetNaloopTijden(nm);
                                        outcome.Nalopen.Add(nm);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // for importing into existing controllers, do not import detectors and settings
            if (!newReg)
            {
                return(outcome);
            }

            // get detectors
            if (importD)
            {
                Regex detectorenRegex = null;
                switch (tabCType)
                {
                case TabCType.UNKNOWN:
                case TabCType.TLCGEN:
                case TabCType.TPA:
                case TabCType.FICK:
                case TabCType.HUIJSKES:
                    detectorenRegex = new Regex(@"^\s*D_code\s*\[\s*(?<name>d[a-zA-Z0-9_]+).*", RegexOptions.Compiled);
                    break;

                case TabCType.ATB:
                    detectorenRegex = new Regex(@"^\s*DP\s*\(\s*(?<name>d[a-zA-Z0-9_]+).*", RegexOptions.Compiled);
                    break;

                case TabCType.GC:
                    detectorenRegex = new Regex(@"^\s*D\s*\(\s*(?<name>d[a-zA-Z0-9_]+).*", RegexOptions.Compiled);
                    break;
                }
                if (detectorenRegex != null)
                {
                    foreach (var l in lines.Where(x => !ReComment.IsMatch(x)))
                    {
                        var m = detectorenRegex.Match(l);
                        if (m.Success)
                        {
                            var name = m.Groups["name"].Value.ToLower().Replace("d", "");
                            if (outcome.Detectoren.All(x => x.Naam != name))
                            {
                                outcome.Detectoren.Add(new DetectorModel
                                {
                                    Naam      = name.ToLower(),
                                    Rijstrook = 1
                                });
                            }
                        }
                    }

                    // assign detectors based on d[a-zA-Z]+## in the sg name
                    var assigned = new List <DetectorModel>();
                    foreach (var d in outcome.Detectoren)
                    {
                        foreach (var fc in outcome.Fasen)
                        {
                            if (fc.Naam.Length < d.Naam.Length &&
                                Regex.IsMatch(d.Naam, $@"^[a-zA-Z]*{fc.Naam}"))
                            {
                                d.FaseCyclus = fc.Naam;
                                fc.Detectoren.Add(d);
                                assigned.Add(d);
                                break;
                            }
                        }
                    }
                    foreach (var d in assigned)
                    {
                        outcome.Detectoren.Remove(d);
                    }
                }
            }

            if (applyDefaults)
            {
                foreach (var fc in outcome.Fasen.Where(x => x.Detectoren.Any()))
                {
                    switch (fc.Type)
                    {
                    case FaseTypeEnum.Auto:
                        fc.Detectoren.ForEach(x => x.Type = DetectorTypeEnum.Lang);
                        fc.Detectoren.First().Type = DetectorTypeEnum.Kop;
                        break;

                    case FaseTypeEnum.Fiets:
                        fc.Detectoren.ForEach(x => x.Type = DetectorTypeEnum.Knop);
                        fc.Detectoren.First().Type = DetectorTypeEnum.Kop;
                        break;

                    case FaseTypeEnum.Voetganger:
                        fc.Detectoren.ForEach(x => x.Type = DetectorTypeEnum.Knop);
                        break;

                    case FaseTypeEnum.OV:
                        fc.Detectoren.ForEach(x => x.Type = DetectorTypeEnum.Lang);
                        fc.Detectoren.First().Type = DetectorTypeEnum.Kop;
                        break;
                    }
                    foreach (var d in fc.Detectoren)
                    {
                        DefaultsProvider.Default.SetDefaultsOnModel(d, fc.Type.ToString(), d.Type.ToString());
                    }
                }
            }
            if (importT)
            {
                Regex dsetRegex1  = null;
                Regex dsetRegex2  = null;
                Regex fcsetRegex1 = null;
                var   setREs      = new Tuple <setRegex, Regex> [(int)setRegex.setMax];
                setREs[(int)setRegex.tdh]    = new Tuple <setRegex, Regex>(setRegex.tdh, new Regex(@"\s*TDH_max\s*\[\s*(?<name>d[a-zA-Z0-9_]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.tdb]    = new Tuple <setRegex, Regex>(setRegex.tdb, new Regex(@"\s*TDB_max\s*\[\s*(?<name>d[a-zA-Z0-9_]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.tog]    = new Tuple <setRegex, Regex>(setRegex.tog, new Regex(@"\s*TOG_max\s*\[\s*(?<name>d[a-zA-Z0-9_]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.tbg]    = new Tuple <setRegex, Regex>(setRegex.tbg, new Regex(@"\s*TBG_max\s*\[\s*(?<name>d[a-zA-Z0-9_]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.tfl]    = new Tuple <setRegex, Regex>(setRegex.tfl, new Regex(@"\s*TFL_max\s*\[\s*(?<name>d[a-zA-Z0-9_]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.cfl]    = new Tuple <setRegex, Regex>(setRegex.cfl, new Regex(@"\s*CFL_max\s*\[\s*(?<name>d[a-zA-Z0-9_]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.trg]    = new Tuple <setRegex, Regex>(setRegex.trg, new Regex(@"\s*TRG_max\s*\[\s*(?<name>fc[0-9]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.trgmin] = new Tuple <setRegex, Regex>(setRegex.trgmin, new Regex(@"\s*TRG_min\s*\[\s*(?<name>fc[0-9]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.tgg]    = new Tuple <setRegex, Regex>(setRegex.tgg, new Regex(@"\s*TGG_max\s*\[\s*(?<name>fc[0-9]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.tggmin] = new Tuple <setRegex, Regex>(setRegex.tggmin, new Regex(@"\s*TGG_min\s*\[\s*(?<name>fc[0-9]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.tfg]    = new Tuple <setRegex, Regex>(setRegex.tfg, new Regex(@"\s*TFG_max\s*\[\s*(?<name>fc[0-9]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.tgl]    = new Tuple <setRegex, Regex>(setRegex.tgl, new Regex(@"\s*TGL_max\s*\[\s*(?<name>fc[0-9]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.tggl]   = new Tuple <setRegex, Regex>(setRegex.tggl, new Regex(@"\s*TGGL_max\s*\[\s*(?<name>fc[0-9]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));
                setREs[(int)setRegex.tglmin] = new Tuple <setRegex, Regex>(setRegex.tglmin, new Regex(@"\s*TGL_min\s*\[\s*(?<name>fc[0-9]+)\s*]\s*=\s*(?<val>[0-9NG]+)\s*;", RegexOptions.Compiled));

                switch (tabCType)
                {
                case TabCType.TLCGEN:
                case TabCType.TPA:
                case TabCType.HUIJSKES:
                    dsetRegex2 = new Regex(@"IS_type\s*\[\s*(?<name>d[a-zA-Z0-9_]+)\s*]\s*=\s*(?<type>[a-zA-Z_]+).*", RegexOptions.Compiled);
                    break;

                case TabCType.FICK:
                    dsetRegex2 = new Regex(@"DE_type\s*\[\s*(?<name>d[a-zA-Z0-9_]+)\s*]\s*=\s*(?<type>[a-zA-Z_]+).*", RegexOptions.Compiled);
                    break;

                case TabCType.ATB:
                    dsetRegex1  = new Regex(@"^\s*DP\s*\(\s*(?<name>d[a-zA-Z0-9_]+)\s*,\s*""[a-zA-Z0-9_]+""\s*,\s*(?<tdb>[0-9NG]+)\s*,\s*(?<tdh>[0-9NG]+)\s*,\s*(?<tbg>[0-9NG]+)\s*,\s*(?<tog>[0-9NG]+).*", RegexOptions.Compiled);
                    dsetRegex2  = new Regex(@"^\s*DPT\s*\(\s*(?<name>d[a-zA-Z0-9_]+)\s*,\s*(?<type>[a-zA-Z_]+).*", RegexOptions.Compiled);
                    fcsetRegex1 = new Regex(@"^\s*FC\s*\(\s*(?<name>fc[0-9]+)\s*,\s*""[a-zA-Z0-9_]+""\s*,\s*(?<trg>[0-9NG]+)\s*,\s*(?<tgg>[0-9NG]+)\s*,\s*(?<tfg>[0-9NG]+)\s*,\s*(?<tggl>[0-9NG]+)\s*,\s*(?<tgl>[0-9NG]+).*");
                    break;

                case TabCType.GC:
                    dsetRegex1  = new Regex(@"^\s*D\s*\(\s*(?<name>d[a-zA-Z0-9_]+)\s*,\s*""[a-zA-Z0-9_]+""\s*,\s*""[a-zA-Z0-9_]+""\s*,\s*(?<tdb>[0-9NG]+)\s*,\s*(?<tdh>[0-9NG]+)\s*,\s*(?<tbg>[0-9NG]+)\s*,\s*(?<tog>[0-9NG]+)\s*,\s*(?<type>[a-zA-Z_]+).*", RegexOptions.Compiled);
                    dsetRegex2  = new Regex(@"^\s*DFL\s*\(\s*(?<name>d[a-zA-Z0-9_]+)\s*,\s*(?<tfl>[0-9NG]+)\s*,\s*(?<cfl>[0-9NG]+).*", RegexOptions.Compiled);
                    fcsetRegex1 = new Regex(@"^\s*FC\s*\(\s*(?<name>fc[0-9]+)\s*,\s*""[a-zA-Z0-9_]+""\s*,\s*""[a-zA-Z0-9_]+""\s*,\s*(?<tgg>[0-9NG]+)\s*,\s*(?<tfg>[0-9NG]+)\s*,\s*(?<tgl>[0-9NG]+)\s*,\s*(?<trg>[0-9NG]+).*");
                    break;
                }
                foreach (var l in lines.Where(x => !ReComment.IsMatch(x)))
                {
                    if (tabCType == TabCType.TPA || tabCType == TabCType.FICK || tabCType == TabCType.HUIJSKES)
                    {
                        foreach (var re in setREs)
                        {
                            var m = re.Item2.Match(l);
                            if (m.Success)
                            {
                                var named  = m.Groups["name"].Value.Replace("d", "");
                                var namefc = m.Groups["name"].Value.Replace("fc", "");
                                var d      = outcome.Detectoren.FirstOrDefault(x => x.Naam == named.ToLower());
                                if (d == null)
                                {
                                    d = outcome.Fasen.SelectMany(x => x.Detectoren).FirstOrDefault(x => x.Naam == named.ToLower());
                                    if (d == null)
                                    {
                                        continue;
                                    }
                                }
                                var fc = outcome.Fasen.FirstOrDefault(x => x.Naam == namefc.ToLower());
                                switch (re.Item1)
                                {
                                case setRegex.tdh: d.TDH = m.Groups["val"].Value == "NG" ? null : new int?(int.Parse(m.Groups["val"].Value)); break;

                                case setRegex.tdb: d.TDB = m.Groups["val"].Value == "NG" ? null : new int?(int.Parse(m.Groups["val"].Value)); break;

                                case setRegex.tog: d.TOG = m.Groups["val"].Value == "NG" ? null : new int?(int.Parse(m.Groups["val"].Value)); break;

                                case setRegex.tbg: d.TBG = m.Groups["val"].Value == "NG" ? null : new int?(int.Parse(m.Groups["val"].Value)); break;

                                case setRegex.tfl: d.TFL = m.Groups["val"].Value == "NG" ? null : new int?(int.Parse(m.Groups["val"].Value)); break;

                                case setRegex.cfl: d.CFL = m.Groups["val"].Value == "NG" ? null : new int?(int.Parse(m.Groups["val"].Value)); break;

                                case setRegex.trg: fc.TRG = int.Parse(m.Groups["val"].Value); break;

                                case setRegex.tgg: fc.TGG = int.Parse(m.Groups["val"].Value); break;

                                case setRegex.tfg: fc.TFG = int.Parse(m.Groups["val"].Value); break;

                                case setRegex.tggl:
                                    if (!intergroen)
                                    {
                                        fc.TGL_min = int.Parse(m.Groups["val"].Value);
                                    }
                                    break;

                                case setRegex.tgl:
                                    if (!intergroen)
                                    {
                                        fc.TGL = int.Parse(m.Groups["val"].Value);
                                    }
                                    else
                                    {
                                        fc.TGL = int.Parse(m.Groups["val"].Value) > fc.TGL ? int.Parse(m.Groups["val"].Value) : fc.TGL;
                                    }
                                    break;

                                case setRegex.trgmin: fc.TRG_min = int.Parse(m.Groups["val"].Value); break;

                                case setRegex.tggmin: fc.TGG_min = int.Parse(m.Groups["val"].Value); break;

                                case setRegex.tglmin: fc.TGL_min = int.Parse(m.Groups["val"].Value); break;
                                }
                            }
                        }
                    }
                    if (fcsetRegex1 != null)
                    {
                        var m = fcsetRegex1.Match(l);
                        if (m.Success)
                        {
                            var name = m.Groups["name"].Value.Replace("fc", "");
                            var fc   = outcome.Fasen.FirstOrDefault(x => x.Naam == name.ToLower());
                            if (fc != null)
                            {
                                fc.TGG = int.Parse(m.Groups["tgg"].Value);
                                fc.TFG = int.Parse(m.Groups["tfg"].Value);
                                fc.TGL = int.Parse(m.Groups["tgl"].Value);
                                fc.TRG = int.Parse(m.Groups["trg"].Value);
                                if (tabCType == TabCType.ATB)
                                {
                                    fc.TGL_min = int.Parse(m.Groups["tggl"].Value);
                                }
                            }
                        }
                    }
                    if (dsetRegex1 != null)
                    {
                        var m = dsetRegex1.Match(l);
                        if (m.Success)
                        {
                            var name = m.Groups["name"].Value.Replace("d", "");
                            var d    = outcome.Detectoren.FirstOrDefault(x => x.Naam == name.ToLower());
                            if (d != null)
                            {
                                d.TDB = m.Groups["tdb"].Value == "NG" ? null : new int?(int.Parse(m.Groups["tdb"].Value));
                                d.TDH = m.Groups["tdh"].Value == "NG" ? null : new int?(int.Parse(m.Groups["tdh"].Value));
                                d.TOG = m.Groups["tog"].Value == "NG" ? null : new int?(int.Parse(m.Groups["tog"].Value));
                                d.TBG = m.Groups["tbg"].Value == "NG" ? null : new int?(int.Parse(m.Groups["tbg"].Value));
                                if (tabCType == TabCType.GC)
                                {
                                    d.Type = GetDetType(m.Groups["type"].Value);
                                }
                            }
                        }
                    }
                    if (dsetRegex2 != null)
                    {
                        var m = dsetRegex2.Match(l);
                        if (m.Success)
                        {
                            var name = m.Groups["name"].Value.Replace("d", "");
                            var d    = outcome.Detectoren.FirstOrDefault(x => x.Naam == name.ToLower());
                            if (d != null)
                            {
                                switch (tabCType)
                                {
                                case TabCType.TPA:
                                case TabCType.HUIJSKES:
                                case TabCType.FICK:
                                case TabCType.ATB:
                                    d.Type = GetDetType(m.Groups["type"].Value);
                                    break;

                                case TabCType.GC:
                                    d.TFL = int.Parse(m.Groups["tfl"].Value);
                                    d.CFL = int.Parse(m.Groups["cfl"].Value);
                                    break;
                                }
                            }
                        }
                    }
                }
                foreach (var fc in outcome.Fasen)
                {
                    foreach (var d in fc.Detectoren)
                    {
                        switch (d.Type)
                        {
                        case DetectorTypeEnum.Kop:
                            d.Aanvraag  = DetectorAanvraagTypeEnum.RnietTRG;
                            d.Verlengen = DetectorVerlengenTypeEnum.Kopmax;
                            break;

                        case DetectorTypeEnum.Lang:
                            d.Aanvraag  = DetectorAanvraagTypeEnum.RnietTRG;
                            d.Verlengen = DetectorVerlengenTypeEnum.MK2;
                            break;

                        case DetectorTypeEnum.Verweg:
                            d.Aanvraag  = DetectorAanvraagTypeEnum.Uit;
                            d.Verlengen = DetectorVerlengenTypeEnum.MK2;
                            break;

                        case DetectorTypeEnum.Knop:
                            d.Aanvraag  = DetectorAanvraagTypeEnum.RoodGeel;
                            d.Verlengen = DetectorVerlengenTypeEnum.Geen;
                            break;
                        }
                    }
                }
            }

            return(outcome);
        }
Example #4
0
        public static TabCImportHelperOutcome GetNewData(string[] lines)
        {
            var dz = MessageBoxResult.None;

            TabCImportHelperOutcome outcome = new TabCImportHelperOutcome();

            if (lines.Count() <= 1)
            {
                return(null);
            }

            // Compile a list of Phases with conflicts from the file
            foreach (var _line in lines)
            {
                var line = Regex.Replace(_line, @"/\*.*\*/", "");
                if (Regex.IsMatch(line, @"^\s+TO_max\["))
                {
                    string fc1   = Regex.Replace(line, @"^\s*TO_max\s*\[\s*(fc[0-9]+).*", "$1");
                    string fc2   = Regex.Replace(line, @"^\s*TO_max\s*\[\s*fc[0-9]+\s*\]\s*\[\s*(fc[0-9]+).*", "$1");
                    string _conf = Regex.Replace(line, @"^\s*TO_max\s*\[\s*fc[0-9]+\s*\]\s*\[\s*fc[0-9]+\s*\]\s*=\s*(([0-9]+|FK|GK|GKL)).*", "$1");

                    int conf = 0;
                    if (_conf == "FK" || _conf == "GK" || _conf == "GKL")
                    {
                        continue;
                    }

                    if (!Int32.TryParse(_conf, out conf))
                    {
                        throw new InvalidOperationException($"Conflict van {fc1} naar {fc2} heeft een foutieve waarde: {_conf}");
                    }

                    FaseCyclusModel _fcm1 = null;
                    foreach (var fcm in outcome.Fasen)
                    {
                        if (fcm.Naam == fc1.Replace("fc", ""))
                        {
                            _fcm1 = fcm;
                            break;
                        }
                    }
                    if (_fcm1 == null)
                    {
                        _fcm1      = new FaseCyclusModel();
                        _fcm1.Naam = fc1.Replace("fc", "");
                        outcome.Fasen.Add(_fcm1);
                    }

                    FaseCyclusModel _fcm2 = null;
                    foreach (FaseCyclusModel fcm in outcome.Fasen)
                    {
                        if (fcm.Naam == fc2.Replace("fc", ""))
                        {
                            _fcm2 = fcm;
                            break;
                        }
                    }
                    if (_fcm2 == null)
                    {
                        _fcm2      = new FaseCyclusModel();
                        _fcm2.Naam = fc2.Replace("fc", "");
                        outcome.Fasen.Add(_fcm2);
                    }
                    outcome.Conflicten.Add(new ConflictModel()
                    {
                        FaseVan = _fcm1.Naam, FaseNaar = _fcm2.Naam, Waarde = conf
                    });
                }

                if (dz != MessageBoxResult.No &&
                    Regex.IsMatch(line, @"^\s+D_code\[") || Regex.IsMatch(line, @"^\s+TBG_max\["))
                {
                    if (dz != MessageBoxResult.Yes)
                    {
                        dz = MessageBox.Show("Detectoren zoeken in tab.c?", "Detectoren zoeken?", MessageBoxButton.YesNo);
                        if (dz == MessageBoxResult.No)
                        {
                            continue;
                        }
                    }
                    string d  = Regex.Replace(line, @"^\s*D_code\s*\[\s*(d[0-9a-zA-Z_]+).*", "$1");
                    string dd = Regex.Replace(d, @"^d", "");
                    if (dd.StartsWith("r"))
                    {
                        dd = "k" + dd.Substring(1);
                    }
                    var tdb = -1;
                    var tdh = -1;
                    var tog = -1;
                    var tbg = -1;
                    if (Regex.IsMatch(line, @"TDB_max"))
                    {
                        var mtdb = Regex.Match(line, @"TDB_max\[.*?\]\s*=\s*([0-9]+);");
                        if (mtdb.Groups.Count > 1)
                        {
                            int.TryParse(mtdb.Groups[1].Value, out tdb);
                        }
                    }
                    if (Regex.IsMatch(line, @"TDH_max"))
                    {
                        var mtdb = Regex.Match(line, @"TDH_max\[.*?\]\s*=\s*([0-9]+);");
                        if (mtdb.Groups.Count > 1)
                        {
                            int.TryParse(mtdb.Groups[1].Value, out tdh);
                        }
                    }
                    if (Regex.IsMatch(line, @"TBG_max"))
                    {
                        var mtdb = Regex.Match(line, @"TBG_max\[.*?\]\s*=\s*([0-9]+);");
                        if (mtdb.Groups.Count > 1)
                        {
                            int.TryParse(mtdb.Groups[1].Value, out tbg);
                        }
                    }
                    if (Regex.IsMatch(line, @"TOG_max"))
                    {
                        var mtdb = Regex.Match(line, @"TOG_max\[.*?\]\s*=\s*([0-9]+);");
                        if (mtdb.Groups.Count > 1)
                        {
                            int.TryParse(mtdb.Groups[1].Value, out tog);
                        }
                    }
                    var nd = new DetectorModel
                    {
                        Naam = dd, Rijstrook = 1
                    };
                    if (tdb != -1)
                    {
                        nd.TDB = tdb;
                    }
                    if (tdh != -1)
                    {
                        nd.TDH = tdh;
                    }
                    if (tbg != -1)
                    {
                        nd.TBG = tbg;
                    }
                    if (tog != -1)
                    {
                        nd.TOG = tog;
                    }
                    if (nd.Naam.StartsWith("k"))
                    {
                        nd.Type      = DetectorTypeEnum.Knop;
                        nd.Aanvraag  = DetectorAanvraagTypeEnum.RoodGeel;
                        nd.Verlengen = DetectorVerlengenTypeEnum.Geen;
                    }
                    else if (Regex.IsMatch(nd.Naam, "1[a-z]?$"))
                    {
                        nd.Type           = DetectorTypeEnum.Kop;
                        nd.Aanvraag       = DetectorAanvraagTypeEnum.RnietTRG;
                        nd.AanvraagDirect = true;
                        nd.Verlengen      = DetectorVerlengenTypeEnum.Kopmax;
                    }
                    else if (Regex.IsMatch(nd.Naam, "2[a-z]?$"))
                    {
                        nd.Type      = DetectorTypeEnum.Lang;
                        nd.Aanvraag  = DetectorAanvraagTypeEnum.RnietTRG;
                        nd.Verlengen = DetectorVerlengenTypeEnum.MK2;
                    }
                    else if (Regex.IsMatch(nd.Naam, "3[a-z]?$"))
                    {
                        nd.Type      = DetectorTypeEnum.Verweg;
                        nd.Aanvraag  = DetectorAanvraagTypeEnum.RnietTRG;
                        nd.Verlengen = DetectorVerlengenTypeEnum.MK2;
                    }
                    else
                    {
                        nd.Type     = DetectorTypeEnum.Overig;
                        nd.Aanvraag = DetectorAanvraagTypeEnum.Uit;
                    }
                    foreach (var fc in outcome.Fasen)
                    {
                        if (fc.Naam.Length < nd.Naam.Length &&
                            Regex.IsMatch(nd.Naam, $@"^k?{fc.Naam}"))
                        {
                            fc.Detectoren.Add(nd);
                        }
                    }
                }
            }
            return(outcome);
        }
        public ControllerModel ImportController(ControllerModel c = null)
        {
            if (c == null)
            {
                throw new NotImplementedException("TabC importer: Controller to import into cannot be null.");
            }

            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.CheckFileExists = true;
            openFileDialog.Title           = "Selecteer tab.c file voor importeren";
            openFileDialog.Filter          = "tab.c files|*tab.c|Alle files|*.*";

            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    string[] lines = File.ReadAllLines(openFileDialog.FileName);

                    // Check if at least all Phases in the Controller occur in the tab.c file
                    List <string> _Fasen = new List <string>();
                    foreach (string line in lines)
                    {
                        if (Regex.IsMatch(line, @"^\s+TO_max\["))
                        {
                            string fc1 = Regex.Replace(line, @"^\s*TO_max\s*\[\s*(fc[0-9]+).*", "$1");
                            if (!_Fasen.Contains(fc1))
                            {
                                _Fasen.Add(fc1);
                            }
                        }
                    }
                    string AllPhasesMessage       = "";
                    List <FaseCyclusModel> newfcs = new List <FaseCyclusModel>();
                    foreach (FaseCyclusModel fcm in c.Fasen)
                    {
                        if (!_Fasen.Contains(fcm.Define))
                        {
                            AllPhasesMessage = AllPhasesMessage + fcm.Define + "\n";
                            newfcs.Add(fcm);
                        }
                    }
                    MessageBoxResult result = MessageBoxResult.Yes;
                    if (!string.IsNullOrEmpty(AllPhasesMessage))
                    {
                        result = MessageBox.Show("Niet alle fasen uit de regeling komen voor in de tab.c file.\nConflicten van de volgende fasen worden verwijderd:\n\n" +
                                                 AllPhasesMessage + "\nDoorgaan?", "Niet alle fasen gevonden", MessageBoxButton.YesNo);
                    }

                    // Continue...
                    if (result == MessageBoxResult.Yes)
                    {
                        // Clear conflicts from Phases not in tab.c file
#warning TODO
                        //foreach (FaseCyclusModel fcm in newfcs)
                        //{
                        //    fcm.Conflicten.Clear();
                        //}

                        // Build a list of the Phases with conflicts from the tab.c file
                        TabCImportHelperOutcome NewData = TabCImportHelper.GetNewData(lines);

                        // Copy the results into the ControllerVM
                        string NewPhasesMessage = "";

                        // Store current conflicts
                        List <ConflictModel> OldConflicts = new List <ConflictModel>();
                        foreach (ConflictModel _cm in c.InterSignaalGroep.Conflicten)
                        {
                            OldConflicts.Add(_cm);
                        }

                        foreach (FaseCyclusModel newfcm in NewData.Fasen)
                        {
                            // Search for existing phases
                            bool found = false;
                            foreach (FaseCyclusModel fcm in c.Fasen)
                            {
                                if (newfcm.Define == fcm.Define)
                                {
                                    found = true;
                                }
                            }
                            if (!found)
                            {
                                c.Fasen.Add(newfcm);
                                NewPhasesMessage = NewPhasesMessage + newfcm.Define + "\n";
                            }
                        }

                        foreach (ConflictModel cm in NewData.Conflicten)
                        {
                            ConflictModel _cm = new ConflictModel();
                            _cm.FaseVan  = cm.FaseVan;
                            _cm.FaseNaar = cm.FaseNaar;
                            _cm.Waarde   = cm.Waarde;
                            c.InterSignaalGroep.Conflicten.Add(_cm);

                            // Check for new conflicts
#warning TODO - At this point: check if new conflicts have been added, and act accordingly
                        }
                        if (!string.IsNullOrEmpty(NewPhasesMessage))
                        {
                            MessageBox.Show("De volgende fasen uit de tab.c file zijn nieuw toegevoegd in de regeling:\n\n" +
                                            NewPhasesMessage, "Nieuwe fasen toegevoegd", MessageBoxButton.OK);
                        }
                        return(c);
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (Exception e)
                {
                    System.Windows.MessageBox.Show("Fout bij uitlezen tab.c.:\n" + e.Message, "Fout bij importeren tab.c");
                    return(null);
                }
            }

            return(null);
        }
Example #6
0
        public static TabCImportHelperOutcome GetNewData(string[] lines)
        {
            TabCImportHelperOutcome outcome = new TabCImportHelperOutcome();

            // Compile a list of Phases with conflicts from the file
            foreach (string line in lines)
            {
                if (Regex.IsMatch(line, @"^\s+TO_max\["))
                {
                    string fc1   = Regex.Replace(line, @"^\s*TO_max\s*\[\s*(fc[0-9]+).*", "$1");
                    string fc2   = Regex.Replace(line, @"^\s*TO_max\s*\[\s*fc[0-9]+\s*\]\s*\[\s*(fc[0-9]+).*", "$1");
                    string _conf = Regex.Replace(line, @"^\s*TO_max\s*\[\s*fc[0-9]+\s*\]\s*\[\s*fc[0-9]+\s*\]\s*=\s*(([0-9]+|FK|GK|GKL)).*", "$1");

                    int conf = 0;
                    if (_conf == "FK")
                    {
                        conf = -2;
                    }
                    else if (_conf == "GK")
                    {
                        conf = -3;
                    }
                    else if (_conf == "GKL")
                    {
                        conf = -4;
                    }
                    else if (!Int32.TryParse(_conf, out conf))
                    {
                        if (lines.Count() <= 1)
                        {
                            throw new NotImplementedException($"Conflict van {fc1} naar {fc2} heeft een foutieve waarde: {_conf}");
                        }
                    }

                    FaseCyclusModel _fcm1 = null;
                    foreach (FaseCyclusModel fcm in outcome.Fasen)
                    {
                        if (fcm.Define == fc1)
                        {
                            _fcm1 = fcm;
                            break;
                        }
                    }
                    if (_fcm1 == null)
                    {
                        _fcm1        = new FaseCyclusModel();
                        _fcm1.Define = fc1;
                        _fcm1.Naam   = fc1.Replace("fc", "");
                        SettingsProvider.Instance.ApplyDefaultFaseCyclusSettings(_fcm1, fc1);
                        outcome.Fasen.Add(_fcm1);
                    }

                    FaseCyclusModel _fcm2 = null;
                    foreach (FaseCyclusModel fcm in outcome.Fasen)
                    {
                        if (fcm.Define == fc2)
                        {
                            _fcm2 = fcm;
                            break;
                        }
                    }
                    if (_fcm2 == null)
                    {
                        _fcm2        = new FaseCyclusModel();
                        _fcm2.Define = fc2;
                        _fcm2.Naam   = fc2.Replace("fc", "");
                        SettingsProvider.Instance.ApplyDefaultFaseCyclusSettings(_fcm2, fc2);
                        outcome.Fasen.Add(_fcm2);
                    }
                    outcome.Conflicten.Add(new ConflictModel()
                    {
                        FaseVan = _fcm1.Define, FaseNaar = _fcm2.Define, Waarde = conf
                    });
                }
            }
            return(outcome);
        }
Example #7
0
        public ControllerModel ImportController(ControllerModel c = null)
        {
            if (c != null)
            {
                throw new NullReferenceException("TabC importer: Controller parsed is not null, which it should be for importing into new.");
            }

            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                CheckFileExists = true,
                Title           = "Selecteer tab.c file voor importeren",
                Filter          = "Import files|*tab.c;*.ccol|Alle files|*.*"
            };

            ControllerModel newc = new ControllerModel();

            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    DefaultsProvider.Default.SetDefaultsOnModel(newc.Data);
                    newc.Data.GarantieOntruimingsTijden = false;

                    string[] lines = File.ReadAllLines(openFileDialog.FileName);
                    if (lines.Length <= 1)
                    {
                        throw new IndexOutOfRangeException("Het bestand heeft minder dan 2 regels.");
                    }

                    // Build a list of the Phases with conflicts from the tab.c file
                    TabCImportHelperOutcome NewData = TabCImportHelper.GetNewData(lines, true);
                    if (NewData == null)
                    {
                        return(null);
                    }

                    NewData.Fasen.BubbleSort();
                    foreach (FaseCyclusModel fcm in NewData.Fasen)
                    {
                        newc.Fasen.Add(fcm);
                        var fcdm = new FaseCyclusModuleDataModel()
                        {
                            FaseCyclus = fcm.Naam
                        };
                        DefaultsProvider.Default.SetDefaultsOnModel(fcdm, fcm.Type.ToString());
                        newc.ModuleMolen.FasenModuleData.Add(fcdm);
                    }
                    NewData.Conflicten.BubbleSort();
                    foreach (ConflictModel cm in NewData.Conflicten)
                    {
                        newc.InterSignaalGroep.Conflicten.Add(cm);
                    }
                    newc.Data.Intergroen = newc.Data.Intergroen;
                    if (newc.Data.Intergroen)
                    {
                        newc.Data.CCOLVersie = Models.Enumerations.CCOLVersieEnum.CCOL95;
                    }
                    foreach (var gs in NewData.Gelijkstarten)
                    {
                        newc.InterSignaalGroep.Gelijkstarten.Add(gs);
                    }
                    foreach (var gs in NewData.Voorstarten)
                    {
                        newc.InterSignaalGroep.Voorstarten.Add(gs);
                    }
                    foreach (var gs in NewData.LateReleases)
                    {
                        newc.InterSignaalGroep.LateReleases.Add(gs);
                    }
                    if (!string.IsNullOrWhiteSpace(NewData.KruisingNaam))
                    {
                        newc.Data.Naam = NewData.KruisingNaam;
                    }
                    if (!string.IsNullOrWhiteSpace(NewData.KruisingStraat1))
                    {
                        newc.Data.Straat1 = NewData.KruisingStraat1;
                    }
                    if (!string.IsNullOrWhiteSpace(NewData.KruisingStraat2))
                    {
                        newc.Data.Straat2 = NewData.KruisingStraat2;
                    }
                    return(newc);
                }
                catch (Exception e)
                {
                    System.Windows.MessageBox.Show("Fout bij uitlezen tab.c.:\n" + e.Message, "Fout bij importeren tab.c");
                    return(null);
                }
            }
            return(null);
        }