public bool Load() {
      if (loaded) {
        printToConsole("A file has already been loaded.");
        return false;
      }
      content = ContentManager.Load(filePath);

      printToConsole("Loading completed!");
      printToConsole("Content loaded: " + content.ToString());

      optimizer = content as IOptimizer;
      if (optimizer != null) {
        numberOfRuns = NumberOfRuns(optimizer);
        initialNumberOfRuns = optimizer.Runs.Count;
        printToConsole(String.Format("Initial number of runs: {0}", initialNumberOfRuns));
        PrintRuns();

        optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Optimizer_Exception);
        optimizer.ExecutionStateChanged += new EventHandler(Optimizer_ExecutionStateChanged);
        optimizer.Stopped += new EventHandler(Optimizer_Stopped);
        optimizer.ExecutionTimeChanged += new EventHandler(Optimizer_ExecutionTimeChanged);
        optimizer.Runs.RowsChanged += new EventHandler(Optimizer_Runs_RowsChanged);
      }
      loaded = optimizer != null;
      return loaded;
    }
        public bool Load()
        {
            if (loaded)
            {
                printToConsole("A file has already been loaded.");
                return(false);
            }
            content = ContentManager.Load(filePath);

            printToConsole("Loading completed!");
            printToConsole("Content loaded: " + content.ToString());

            optimizer = content as IOptimizer;
            if (optimizer != null)
            {
                numberOfRuns        = NumberOfRuns(optimizer);
                initialNumberOfRuns = optimizer.Runs.Count;
                printToConsole(String.Format("Initial number of runs: {0}", initialNumberOfRuns));
                PrintRuns();

                optimizer.ExceptionOccurred     += new EventHandler <EventArgs <Exception> >(Optimizer_Exception);
                optimizer.ExecutionStateChanged += new EventHandler(Optimizer_ExecutionStateChanged);
                optimizer.Stopped += new EventHandler(Optimizer_Stopped);
                optimizer.ExecutionTimeChanged += new EventHandler(Optimizer_ExecutionTimeChanged);
                optimizer.Runs.RowsChanged     += new EventHandler(Optimizer_Runs_RowsChanged);
            }
            loaded = optimizer != null;
            return(loaded);
        }
Beispiel #3
0
        public static async Task LoadAsync(string filename, Action <IStorableContent, Exception, Info> loadingCompletedCallback)
        {
            if (instance == null)
            {
                throw new InvalidOperationException("ContentManager is not initialized.");
            }

            Exception        error             = null;
            IStorableContent result            = null;
            Info             serializationInfo = null;

            try {
                result = await Task.Run(() => {
                    var content = instance.LoadContent(filename, out serializationInfo);
                    if (content != null)
                    {
                        content.Filename = filename;
                    }
                    return(content);
                });
            } catch (Exception ex) {
                error = ex;
            }
            loadingCompletedCallback(result, error, serializationInfo);
        }
        protected override IStorableContent LoadContent(string filename, out Info info)
        {
            bool             useOldPersistence = XmlParser.CanOpen(filename);
            IStorableContent content           = null;

            if (useOldPersistence)
            {
                var sw = new Stopwatch();
                sw.Start();
                content = XmlParser.Deserialize <IStorableContent>(filename);
                sw.Stop();
                info = new Info(filename, sw.Elapsed);
            }
            else
            {
                var ser = new ProtoBufSerializer();
                content = (IStorableContent)ser.Deserialize(filename, out SerializationInfo serInfo);
                info    = new Info(filename, serInfo);
            }
            if (content == null)
            {
                throw new PersistenceException($"Cannot deserialize root element of {filename}");
            }
            return(content);
        }
 public static void Save(IStorableContent content, string filename, bool compressed)
 {
     if (instance == null)
     {
         throw new InvalidOperationException("ContentManager is not initialized.");
     }
     instance.SaveContent(content, filename, compressed);
     content.Filename = filename;
 }
        public static IStorableContent Load(string filename)
        {
            if (instance == null)
            {
                throw new InvalidOperationException("ContentManager is not initialized.");
            }
            IStorableContent content = instance.LoadContent(filename);

            content.Filename = filename;
            return(content);
        }
Beispiel #7
0
 private static void LoadingCompleted(IStorableContent content, Exception error) {
   try {
     if (error != null) throw error;
     IView view = MainFormManager.MainForm.ShowContent(content);
     if (view == null)
       ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
   }
   catch (Exception ex) {
     ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
   }
   finally {
     ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
   }
 }
    public static void SaveAsync(IStorableContent content, string filename, bool compressed, Action<IStorableContent, Exception> savingCompletedCallback) {
      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
      var action = new Action<IStorableContent, string, bool>(instance.SaveContent);
      action.BeginInvoke(content, filename, compressed, delegate(IAsyncResult result) {
        Exception error = null;
        try {
          action.EndInvoke(result);
          content.Filename = filename;
        }
        catch (Exception ex) {
          error = ex;
        }
        savingCompletedCallback(content, error);
      }, null);

    }
Beispiel #9
0
 private static void SavingCompleted(IStorableContent content, Exception error)
 {
     try {
         if (error != null)
         {
             throw error;
         }
         MainFormManager.GetMainForm <HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
     } catch (OperationCanceledException) { // do nothing if canceled
     } catch (Exception ex) {
         ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
     } finally {
         Progress.Hide(content);
         MainFormManager.GetMainForm <HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
     }
 }
Beispiel #10
0
        public static void SaveAs(IContentView view)
        {
            IStorableContent content = view.Content as IStorableContent;

            if (!view.Locked && content != null)
            {
                if (saveFileDialog == null)
                {
                    saveFileDialog             = new SaveFileDialog();
                    saveFileDialog.Title       = "Save Item";
                    saveFileDialog.DefaultExt  = "hl";
                    saveFileDialog.Filter      = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
                    saveFileDialog.FilterIndex = 2;
                }

                INamedItem namedItem         = content as INamedItem;
                string     suggestedFileName = string.Empty;
                if (!string.IsNullOrEmpty(content.Filename))
                {
                    suggestedFileName = content.Filename;
                }
                else if (namedItem != null)
                {
                    suggestedFileName = namedItem.Name;
                }
                else
                {
                    suggestedFileName = "Item";
                }

                saveFileDialog.FileName = suggestedFileName;

                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    MainFormManager.GetMainForm <HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
                    SetSaveOperationProgressInContentViews(content, true, saveFileDialog.FileName);
                    if (saveFileDialog.FilterIndex == 1)
                    {
                        ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
                    }
                    else
                    {
                        ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
                    }
                }
            }
        }
Beispiel #11
0
 private static void SetSaveOperationProgressInContentViews(IStorableContent content, bool showProgress, string fileName = null)
 {
     HeuristicLab.MainForm.WindowsForms.MainForm mainForm = MainFormManager.GetMainForm <HeuristicLab.MainForm.WindowsForms.MainForm>();
     #region Mono Compatibility
     // removed the InvokeRequired check because of Mono
     mainForm.Invoke((Action) delegate {
         if (showProgress)
         {
             mainForm.AddOperationProgressToContent(content, string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename)));
         }
         else
         {
             mainForm.RemoveOperationProgressFromContent(content);
         }
     });
     #endregion
 }
Beispiel #12
0
        private static void Save(IContentView view)
        {
            IStorableContent content = view.Content as IStorableContent;

            if (!view.Locked && content != null)
            {
                if (string.IsNullOrEmpty(content.Filename))
                {
                    SaveAs(view);
                }
                else
                {
                    MainFormManager.GetMainForm <HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
                    SetSaveOperationProgressInContentViews(content, true);
                    ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
                }
            }
        }
Beispiel #13
0
        public static void SaveAsync(IStorableContent content, string filename, bool compressed, Action <IStorableContent, Exception> savingCompletedCallback, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (instance == null)
            {
                throw new InvalidOperationException("ContentManager is not initialized.");
            }
            var action = new Action <IStorableContent, string, bool, CancellationToken>(instance.SaveContent);

            action.BeginInvoke(content, filename, compressed, cancellationToken, delegate(IAsyncResult result) {
                Exception error = null;
                try {
                    action.EndInvoke(result);
                    content.Filename = filename;
                } catch (Exception ex) {
                    error = ex;
                }
                savingCompletedCallback(content, error);
            }, null);
        }
 public override void UpdateTitle()
 {
     if (InvokeRequired)
     {
         Invoke(new Action(UpdateTitle));
     }
     else
     {
         IContentView activeView = ActiveView as IContentView;
         if ((activeView != null) && (activeView.Content is IStorableContent))
         {
             IStorableContent content = (IStorableContent)activeView.Content;
             Title = title + " [" + (string.IsNullOrEmpty(content.Filename) ? "Unsaved" : content.Filename) + "]";
         }
         else
         {
             Title = title;
         }
     }
 }
Beispiel #15
0
 private static void LoadingCompleted(IStorableContent content, Exception error)
 {
     try {
         if (error != null)
         {
             throw error;
         }
         IView view = MainFormManager.MainForm.ShowContent(content);
         if (view == null)
         {
             ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
         }
     }
     catch (Exception ex) {
         ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
     }
     finally {
         ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
     }
 }
        public static void LoadAsync(string filename, Action <IStorableContent, Exception> loadingCompletedCallback)
        {
            if (instance == null)
            {
                throw new InvalidOperationException("ContentManager is not initialized.");
            }
            var func = new Func <string, IStorableContent>(instance.LoadContent);

            func.BeginInvoke(filename, delegate(IAsyncResult result) {
                Exception error          = null;
                IStorableContent content = null;
                try {
                    content          = func.EndInvoke(result);
                    content.Filename = filename;
                }
                catch (Exception ex) {
                    error = ex;
                }
                loadingCompletedCallback(content, error);
            }, null);
        }
Beispiel #17
0
        private void LoadingCompleted(IStorableContent content)
        {
            //if (error != null) throw error;
            UpdateRun("Algorithm loading complete", StrategyState.Running);

            if (content is HeuristicLab.Algorithms.EvolutionStrategy.EvolutionStrategy)
            {
                algorithm = content as HeuristicLab.Algorithms.EvolutionStrategy.EvolutionStrategy;
                VehicleRoutingProblem problem = algorithm.Problem as VehicleRoutingProblem;

                if (problem == null)
                {
                    Console.WriteLine("Unsupported problem type detected.");
                    return;
                }

                SetProblemInstance(problem);

                ParameterizeProblem(problem);
                ParameterizeAlgorithm(algorithm);

                RegisterAlgorithmEventHandlers(algorithm);

                try
                {
                    algorithm.Start();
                }
                catch (Exception exc)
                {
                    Console.WriteLine("Error Loading: " + exc.Message);
                }
            }
            else
            {
                // TODO: update the run status to error...
                //_strategyState = StrategyState.Stopped;
            }
        }
Beispiel #18
0
 private static void LoadingCompleted(IStorableContent content, Exception error, ContentManager.Info info)
 {
     try {
         if (error != null)
         {
             throw error;
         }
         if (info != null && info.UnknownTypeGuids.Any())
         {
             var message = "Unknown type guids: " + string.Join(Environment.NewLine, info.UnknownTypeGuids);
             MessageBox.Show((Control)MainFormManager.MainForm, message, $"File {info.Filename} not restored completely", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         IView view = MainFormManager.MainForm.ShowContent(content);
         if (view == null)
         {
             ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
         }
     } catch (Exception ex) {
         ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
     } finally {
         ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
     }
 }
 protected abstract void SaveContent(IStorableContent content, string filename, bool compressed);
 protected abstract void SaveContent(IStorableContent content, string filename, bool compressed);
Beispiel #21
0
 private static void SavingCompleted(IStorableContent content, Exception error) {
   try {
     if (error != null) throw error;
     MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
   }
   catch (Exception ex) {
     ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
   }
   finally {
     SetSaveOperationProgressInContentViews(content, false);
     MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
   }
 }
Beispiel #22
0
 private static void SetSaveOperationProgressInContentViews(IStorableContent content, bool showProgress, string fileName = null) {
   HeuristicLab.MainForm.WindowsForms.MainForm mainForm = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>();
   #region Mono Compatibility
   // removed the InvokeRequired check because of Mono
   mainForm.Invoke((Action)delegate {
     if (showProgress) {
       mainForm.AddOperationProgressToContent(content, string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename)));
     } else
       mainForm.RemoveOperationProgressFromContent(content);
   });
   #endregion
 }
 protected override void SaveContent(IStorableContent content, string filename, bool compressed) {
   XmlGenerator.Serialize(content, filename, compressed ? CompressionLevel.Optimal : CompressionLevel.NoCompression);
 }
Beispiel #24
0
 protected abstract void SaveContent(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken);
 public static void Save(IStorableContent content, string filename, bool compressed) {
   if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
   instance.SaveContent(content, filename, compressed);
   content.Filename = filename;
 }
        protected override void SaveContent(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken)
        {
            var ser = new ProtoBufSerializer();

            ser.Serialize(content, filename, cancellationToken);
        }
Beispiel #27
0
 protected override void SaveContent(IStorableContent content, string filename, bool compressed)
 {
     XmlGenerator.Serialize(content, filename, compressed ? CompressionLevel.Optimal : CompressionLevel.NoCompression);
 }
Beispiel #28
0
        private static void AddProgressInContentViews(IStorableContent content, CancellationTokenSource cancellationTokenSource, string fileName = null)
        {
            string message = string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename));

            Progress.Show(content, message, ProgressMode.Indeterminate, cancelRequestHandler: () => cancellationTokenSource.Cancel());
        }