Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/tools/CedmaExporter/CedmaExporterForm.cs @ 13397

Last change on this file since 13397 was 2312, checked in by gkronber, 15 years ago

Added CedmaExporter from #715 branch.

File size: 3.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using System.IO;
10using HeuristicLab.Core;
11using HeuristicLab.DataAnalysis;
12using HeuristicLab.Modeling.Database.SQLServerCompact;
13
14namespace CedmaExporter {
15  public partial class CedmaExporterForm : Form {
16    private BackgroundWorker worker;
17    public CedmaExporterForm() {
18      InitializeComponent();
19    }
20
21    private void exportButton_Click(object sender, EventArgs e) {
22      var dialog = new OpenFileDialog();
23      DialogResult result;
24      result = dialog.ShowDialog();
25      if (result == DialogResult.OK) {
26        string fileName = dialog.FileName;
27        exportButton.Enabled = false;
28        cancelButton.Enabled = true;
29        worker = new BackgroundWorker();
30        worker.WorkerReportsProgress = true;
31        worker.WorkerSupportsCancellation = true;
32        worker.DoWork += CreateWorkerDelegate(worker, fileName);
33        worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
34        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
35        worker.RunWorkerAsync();
36      }
37    }
38
39    private DoWorkEventHandler CreateWorkerDelegate(BackgroundWorker worker, string fileName) {
40      return (sender, args) => {
41        string inputFileName = fileName;
42        string outputFileName = fileName.Replace(".sdf", "") + ".txt";
43        string sqlCompactConnectionString = "sqlite:rdf:Data Source=\"" + inputFileName + "\"";
44        using (StreamWriter writer = File.CreateText(outputFileName)) {
45          DatabaseService database = new DatabaseService("Data Source=" + fileName);
46          database.Connect();
47          var models = database.GetAllModels();
48          var dataset = database.GetDataset();
49          CedmaExporter.WriteColumnHeaders(writer);
50          List<string> inputVariables = CedmaExporter.WriteVariableImpactHeaders(database, writer);
51          writer.WriteLine();
52          int i = 0;
53          var exporter = new ModelExporter(dataset, Path.GetDirectoryName(outputFileName), true);
54          var allModels = database.GetAllModels();
55          foreach (HeuristicLab.Modeling.Database.IModel m in allModels) {
56            CedmaExporter.WriteModel(m, ++i, database, writer, inputVariables, exporter);
57            worker.ReportProgress((i * 100) / allModels.Count());
58            if (worker.CancellationPending) return;
59          }
60        }
61      };
62    }
63
64    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
65      exportButton.Enabled = true;
66      cancelButton.Enabled = false;
67      progressBar.Value = 0;
68    }
69
70    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
71      progressBar.Value = e.ProgressPercentage;
72    }
73
74    private void cancelButton_Click(object sender, EventArgs e) {
75      worker.CancelAsync();
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.