Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporterForm.cs @ 2286

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

Fixed a bug in the s-expression exporter. Improved exporter UI. #715 & #719

File size: 3.2 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 SemWeb;
11using HeuristicLab.Core;
12using HeuristicLab.DataAnalysis;
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(".db3", "") + ".txt";
43        string rdfConnectionString = "sqlite:rdf:Data Source=\"" + inputFileName + "\"";
44        using (StreamWriter writer = File.CreateText(outputFileName)) {
45          using (Store store = Store.Create(rdfConnectionString)) {
46            var dsEntities = store.SelectSubjects(Ontology.InstanceOf, Ontology.TypeDataSet);
47            HeuristicLab.CEDMA.Core.Problem prob = (HeuristicLab.CEDMA.Core.Problem)PersistenceManager.RestoreFromGZip(Convert.FromBase64String(((Literal)store.SelectObjects(dsEntities[0], Ontology.SerializedData)[0]).Value));
48            Dataset ds = prob.Dataset;
49            CedmaExporter.WriteColumnHeaders(writer);
50            List<string> inputVariables = CedmaExporter.WriteVariableImpactHeaders(store, writer);
51            writer.WriteLine();
52            var subjects = store.SelectSubjects(new Entity(Ontology.InstanceOf.Uri), new Entity(Ontology.TypeModel.Uri));
53            int i = 0;
54            var exporter = new ModelExporter(ds, Path.GetDirectoryName(outputFileName), false);
55            foreach (var model in subjects) {
56              CedmaExporter.WriteModel(model, ++i, store, writer, inputVariables, exporter);
57              worker.ReportProgress((i * 100) / subjects.Count());
58              if (worker.CancellationPending) return;
59            }
60          }
61        }
62      };
63    }
64
65    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
66      exportButton.Enabled = true;
67      cancelButton.Enabled = false;
68      progressBar.Value = 0;
69    }
70
71    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
72      progressBar.Value = e.ProgressPercentage;
73    }
74
75    private void cancelButton_Click(object sender, EventArgs e) {
76      worker.CancelAsync();
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.