public void TestMethod()
        {
            var r = new Replacement();

            var currentDirectory = Directory.GetCurrentDirectory();

            var backupDirectory = Path.Combine(currentDirectory, "Backup");

            if (!Directory.Exists(backupDirectory))
            {
                Directory.CreateDirectory(backupDirectory);
            }

            var sourceFilePath = Path.Combine(currentDirectory, "SourceFile.txt");
            var targetFilePath = Path.Combine(currentDirectory, "TargetFile.txt");
            var backupFilePath = Path.Combine(backupDirectory, "TargetFile.txt");

            var sourceFile       = File.ReadAllBytes(sourceFilePath);
            var targetFileBefore = File.ReadAllBytes(targetFilePath);

            r.Replace(sourceFilePath, backupFilePath, targetFilePath);

            var targetFileAfter = File.ReadAllBytes(targetFilePath);
            var backupFile      = File.ReadAllBytes(backupFilePath);

            Assert.AreEqual(EqualByteArray(sourceFile, targetFileAfter), true);
            Assert.AreEqual(EqualByteArray(targetFileBefore, backupFile), true);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        private void Initialize()
        {
            var text = Replacement.Replace(Strings.LFigureBracket, string.Empty);

            text = text.Replace(Strings.RFigureBracket, string.Empty);

            var propAndFormat = text.Split(Strings.TwoPoints.ToCharArray());

            if (propAndFormat.Length > 0)
            {
                Property = propAndFormat[0];
            }

            if (propAndFormat.Length > 1)
            {
                Format = propAndFormat[1];
            }
        }
Ejemplo n.º 3
0
        protected void findAction(FolderName folder)
        {
            folder.Must().Value.Must().Exist().OrThrow();

            var finder = new Finder(Pattern, Not, Unless, Include, IncludeExt, Exclude, ExcludeExt);

            if (Replacement.IsNotEmpty())
            {
                var replacement = Replacement.Replace("^", "$");
                truncFunc = line => line.Substitute(Pattern, replacement);
            }
            else if (Truncate.If(out var truncate))
            {
                if (truncate.IsIntegral())
                {
                    var amount = truncate.ToInt();
                    truncFunc = line => line.Exactly(amount);
                }
                else if (truncate == "-")
                {
                    truncFunc = line => line;
                }
                else
                {
                    truncFunc = line => line.Exactly(80);
                }
            }
            else
            {
                truncFunc = line => line;
            }

            fileTally = 0;
            lineTally = 0;

            if (Mark)
            {
                matchFolderMarked(folder, finder);
            }
            else
            {
                matchFolder(folder, finder, result => truncFunc(result.Line), true);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Executes one iteration of EA
        /// </summary>
        /// <param name="pop">population for this iteration</param>
        public void Evolve(Population pop)
        {
            if (fitness == null)
            {
                throw new Exception("No fitness function defined");
            }

            generationNo++;

            // informative output
            if ((!Settings.parallel) && Settings.showGap >= 5 && (generationNo + 1) % (Settings.showGap / 5) == 0)
            {
                Console.Write("|");
            }

            fitness.Evaluate(pop, false);

            Population parents = pop;

            Population matingPool = new Population();

            // mating pool creation - if n selectors, each will select 1 / n of the pool
            if (matingSelectors.Count() > 0)
            {
                int mateSel  = matingSelectors.Count;
                int toSelect = parents.GetPopulationSize() / mateSel;
                for (int i = 0; i < matingSelectors.Count; i++)
                {
                    Population sel = new Population();
                    matingSelectors[i].Select(toSelect, parents, sel);
                    matingPool.AddAll((Population)sel.Clone());
                }

                int missing = parents.GetPopulationSize() - matingPool.GetPopulationSize();
                if (missing > 0)
                {
                    Population sel = new Population();
                    matingSelectors[matingSelectors.Count - 1].Select(toSelect, parents, sel);
                    matingPool.AddAll((Population)sel.Clone());
                }
            }
            else
            {
                matingPool = (Population)parents.Clone();
                matingPool.Shuffle();
            }

            // operators are executed in the order of specification
            Population offspring = null;

            foreach (Operator o in operators)
            {
                offspring = new Population();
                o.Operate(matingPool, offspring);
                matingPool = offspring;
            }

            // update of operator properties (if desired)
            foreach (Operator o in operators)
            {
                o.Update();
            }

            fitness.Evaluate(offspring, false);

            // selection process
            Population selected = new Population();

            Population combined = replacement.Replace(parents, offspring);

            if (environmentalSelectors.Count < 1)
            {
                selected = (Population)combined.Clone();
                fitness.Evaluate(combined, false);
            }
            else
            {
                List <Individual> sortedOld = parents.GetSortedIndividuals();
                for (int i = 0; i < eliteSize * parents.GetPopulationSize(); i++)
                {
                    selected.Add(sortedOld[i]);
                }

                fitness.Evaluate(combined, false);

                int envSel   = environmentalSelectors.Count;
                int toSelect = (parents.GetPopulationSize() - selected.GetPopulationSize()) / envSel;
                for (int i = 0; i < environmentalSelectors.Count; i++)
                {
                    Population sel = new Population();
                    environmentalSelectors[i].Select(toSelect, combined, sel);
                    selected.AddAll((Population)sel.Clone());
                }

                int missing = parents.GetPopulationSize() - selected.GetPopulationSize();
                if (missing > 0)
                {
                    Population sel = new Population();
                    environmentalSelectors[environmentalSelectors.Count - 1].Select(toSelect, combined, sel);
                    selected.AddAll((Population)sel.Clone());
                }
            }
            pop.Clear();
            pop.AddAll(selected);

            fitness.Evaluate(pop, true);
        }
Ejemplo n.º 5
0
        protected void replaceText()
        {
            var fixedReplacement = Replacement.Replace("//", "~double");

            fixedReplacement = fixedReplacement.Replace("/", "$");
            fixedReplacement = fixedReplacement.Replace("~double", "/");

            if (File.If(out var file))
            {
                file.Must().Exist().OrThrow();

                if (Backup)
                {
                    var backupFile = FileName.UniqueFileName(file.Folder, file);
                    file.CopyTo(backupFile, true);
                }

                if (OutputFile.If(out var outputFile))
                {
                    var tempFile = FolderName.Temp + $"{uniqueID()}.{file.Extension}";

                    var newLines = new List <string>();

                    if (Delete)
                    {
                        foreach (var line in file.Lines)
                        {
                            if (!IsMatch(line))
                            {
                                newLines.Add(line);
                            }
                        }
                    }
                    else
                    {
                        foreach (var line in file.Lines.Where(IsMatch))
                        {
                            var replacement = line.Substitute(Pattern, fixedReplacement);
                            newLines.Add(replacement);
                        }
                    }

                    tempFile.SetText(newLines.ToString("\r\n"), file.Encoding);

                    tempFile.CopyTo(outputFile, true);
                    tempFile.Delete();
                }
                else
                {
                    foreach (var line in file.Lines.Where(IsMatch))
                    {
                        var replacement = line.Substitute(Pattern, fixedReplacement);
                        WriteLine(replacement);
                    }
                }
            }
            else
            {
                while (true)
                {
                    var line = ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    var replacement = line.Substitute(Pattern, fixedReplacement);
                    WriteLine(replacement);
                }
            }
        }
Ejemplo n.º 6
0
 public void Replace(object obj, Slicer slicer) => Replacement.Replace(obj, Getter, slicer);