Beispiel #1
0
 public Application(IMigrationLocator migrationLocator, IVersionLocator versionLocator,
                    IMigrationStrategy migrationStrategy)
 {
     _migrationLocator  = migrationLocator;
     _versionLocator    = versionLocator;
     _migrationStrategy = migrationStrategy;
 }
Beispiel #2
0
 public MigrateToLatestVersionCommandHandler(
     IMigrationStrategy migrationStrategy,
     ILogger <MigrateToLatestVersionCommandHandler> logger)
 {
     _migrationStrategy = migrationStrategy;
     _logger            = logger;
 }
Beispiel #3
0
        public static IslandEngine Create(MultiKey islandKey, PeaSettings settings, int seed)
        {
            if (seed == 0)
            {
                seed = islandKey.GetHashCode() + Environment.TickCount;
            }

            var random       = (IRandom)Activator.CreateInstance(settings.Random, seed);
            var parameterSet = CreateParameters(settings);

            var fitness         = (IFitnessFactory)Activator.CreateInstance(settings.Fitness);
            var fitnessComparer = fitness.GetFitnessComparer();

            var engine = new IslandEngine()
            {
                Random     = random,
                Settings   = settings,
                Parameters = parameterSet
            };

            var algorithm         = CreateAlgorithm(engine, settings);
            var conflictDetectors = CreateConflictDetectors(settings.SubProblemList);

            var chromosomeFactories = CreateChromosomeFactories(engine, settings, conflictDetectors, random);
            var defaultCreator      = new EntityCreator(settings.EntityType, chromosomeFactories, random);

            engine.EntityCreators = CreateEntityCreators(settings.SubProblemList, defaultCreator, random);

            IMigrationStrategy migrationStrategy = CreateMigrationStrategy(engine, random, fitnessComparer, parameterSet, settings);

            engine.Algorithm         = algorithm.GetAlgorithm(engine);
            engine.FitnessComparer   = fitnessComparer;
            engine.ConflictDetectors = conflictDetectors;
            engine.Selections        = CreateSelections(algorithm, settings, parameterSet, random, fitnessComparer);
            engine.Replacements      = CreateReinsertions(algorithm, settings, parameterSet, random, fitnessComparer);
            engine.MigrationStrategy = migrationStrategy;

            engine.Reduction = new Population.Reduction.CleanOutTournamentLosers(random, parameterSet);
            //engine.Reduction = new Population.Reduction.DoNothingReduction();

            engine.Parameters.SetValueRange(algorithm.GetParameters());

            engine.EntityMutation  = new EntityMutation(chromosomeFactories, random);
            engine.EntityCrossover = new EntityCrossover(chromosomeFactories, random);
            engine.StopCriteria    = settings.StopCriteria;

            return(engine);
        }
Beispiel #4
0
        public void Migrate()
        {
            var filesToDelete = new List <string>();

            if (File.Exists(BackupFilePath))
            {
                File.Delete(BackupFilePath);
            }
            File.Copy(SourceFilePath, BackupFilePath);
            filesToDelete.Add(BackupFilePath);
            int    currentVersion      = GetFileVersion();
            string sourceFilePath      = SourceFilePath;
            string destinationFilePath = "";

            while (currentVersion != ToVersion)
            {
                IMigrationStrategy strategy = _migrationStrategies.Find(s => s.FromVersion == currentVersion);
                if (strategy == null)
                {
                    throw new InvalidOperationException(
                              String.Format("No migration strategy could be found for version {0}", currentVersion)
                              );
                }
                destinationFilePath = String.Format("{0}.Migrate_{1}_{2}", SourceFilePath, strategy.FromVersion, strategy.ToVersion);
                strategy.Migrate(sourceFilePath, destinationFilePath);
                filesToDelete.Add(destinationFilePath);
                currentVersion = strategy.ToVersion;
                sourceFilePath = destinationFilePath;
            }
            File.Delete(SourceFilePath);
            File.Move(destinationFilePath, SourceFilePath);
            foreach (var filePath in filesToDelete)
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
        }
Beispiel #5
0
 public void AddMigrationStrategy(IMigrationStrategy strategy)
 {
     _migrationStrategies.Add(strategy);
 }
Beispiel #6
0
        ///<summary>
        /// Perform the migration
        ///</summary>
        ///<exception cref="InvalidOperationException"></exception>
        public virtual void Migrate()
        {
            var problems = new List <FolderMigratorProblem>();

            // Clean up root backup path
            if (Directory.Exists(MigrationPath))
            {
                if (!RobustIO.DeleteDirectoryAndContents(MigrationPath))
                {
                    //for user, ah well
                    //for programmer, tell us
                    Debug.Fail("(Debug mode only) Couldn't delete the migration folder");
                }
            }

            //check if there is anything to migrate. If not, don't do anything
            if (!Directory.Exists(SourcePath))
            {
                return;
            }


            if (GetLowestVersionInFolder(SourcePath) == ToVersion)
            {
                return;
            }

            // Backup current folder to backup path under backup root
            CopyDirectory(SourcePath, BackupPath, MigrationPath);

            string currentPath = BackupPath;
            int    lowestVersionInFolder;
            int    lowestVersoinInFolder1 = -1;

            while ((lowestVersionInFolder = GetLowestVersionInFolder(currentPath)) != ToVersion)
            {
                //This guards against an empty Folder
                if (lowestVersionInFolder == int.MaxValue)
                {
                    break;
                }
                if (lowestVersionInFolder == lowestVersoinInFolder1)
                {
                    var fileNotMigrated = _versionCache.First(info => info.Version == lowestVersionInFolder).FileName;
                    throw new ApplicationException(
                              String.Format("The migration strategy for {0} failed to migrate the file '{1} from version {2}",
                                            SearchPattern, fileNotMigrated,
                                            lowestVersoinInFolder1)
                              );
                }
                int currentVersion = lowestVersionInFolder;
                // Get all files in folder with this version
                var fileNamesToMigrate = GetFilesOfVersion(currentVersion, currentPath);

                // Find a strategy to migrate this version
                IMigrationStrategy strategy = MigrationStrategies.Find(s => s.FromVersion == currentVersion);
                if (strategy == null)
                {
                    throw new InvalidOperationException(
                              String.Format("No migration strategy could be found for version {0}", currentVersion)
                              );
                }
                string destinationPath = Path.Combine(
                    MigrationPath, String.Format("{0}_{1}", strategy.FromVersion, strategy.ToVersion)
                    );
                Directory.CreateDirectory(destinationPath);
                // Migrate all the files of the current version
                foreach (var filePath in Directory.GetFiles(currentPath, SearchPattern))
                {
                    string fileName = Path.GetFileName(filePath);
                    if (fileName == null)
                    {
                        continue;
                    }
                    string sourceFilePath = Path.Combine(currentPath, fileName);
                    string targetFilePath = Path.Combine(destinationPath, fileName);
                    if (fileNamesToMigrate.Contains(sourceFilePath))
                    {
                        strategy.Migrate(sourceFilePath, targetFilePath);
                    }
                    else
                    {
                        File.Copy(sourceFilePath, targetFilePath);
                    }
                }

                try
                {
                    strategy.PostMigrate(currentPath, destinationPath);
                }
                catch (Exception e)
                {
                    problems.Add(new FolderMigratorProblem {
                        Exception = e, FilePath = currentPath
                    });
                }

                // Setup for the next iteration
                currentPath            = destinationPath;
                lowestVersoinInFolder1 = lowestVersionInFolder;
            }

            // Delete all the files in SourcePath matching SearchPattern
            foreach (var filePath in Directory.GetFiles(SourcePath, SearchPattern))
            {
                File.Delete(filePath);
            }

            // Copy the migration results into SourcePath
            CopyDirectory(currentPath, SourcePath, "");
            if (!RobustIO.DeleteDirectoryAndContents(MigrationPath))
            {
                //for user, ah well
                //for programmer, tell us
                Debug.Fail("(Debug mode only) Couldn't delete the migration folder");
            }

            // Call the problem handler if we encountered problems during migration.
            if (problems.Count > 0)
            {
                _problemHandler(problems);
            }
        }
 public MigrationProvider(IMigrationStrategy migrationStrategy)
 {
     _migrationStrategy = migrationStrategy;
 }