Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Backend.Hl2ImporterFormat/Hl2ImporterCommand.cs @ 16567

Last change on this file since 16567 was 16567, checked in by gkronber, 5 years ago

#2520: changed StorableConstructors and added StorableType attributes in HeuristicLab.DataImporter addon

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.IO;
23using HeuristicLab.DataImporter.Data.CommandBase;
24using HeuristicLab.DataImporter.Data.Model;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HEAL.Attic;
27
28namespace HeuristicLab.DataImporter.Backend.Hl2ImporterFormat {
29  [StorableType("A13F1017-2798-475A-BAFE-211B2921B926")]
30  public class Hl2ImporterCommand : DataSetCommandBase {
31    private ColumnGroup newColumnGroup;
32
33    [StorableConstructor]
34    protected Hl2ImporterCommand(StorableConstructorFlag _) : base(_) { }
35
36    public Hl2ImporterCommand(DataSet dataSet)
37      : base(dataSet) {
38    }
39
40    public override string Description {
41      get { return "Import HL2 file"; }
42    }
43
44    [Storable]
45    private string fileName;
46    public string FileName {
47      get { return fileName; }
48      set { this.fileName = value; }
49    }
50
51    public override void Execute() {
52      base.Execute();
53      newColumnGroup = new ColumnGroup(fileName.Substring(fileName.LastIndexOf('\\') + 1));
54      StreamReader reader = new StreamReader(fileName);
55      string firstLine = reader.ReadLine();
56      string[] variableNames = new string[0];
57
58      //skip meta information && extract variable/column names
59      while (firstLine.Trim()[0] == Hl2Exporter.METATAG[0]) {
60        if (firstLine.Contains(Hl2Exporter.VARIABLENAMES)) {
61          firstLine = firstLine.Replace(Hl2Exporter.METATAG + Hl2Exporter.VARIABLENAMES + "=", "");
62          variableNames = firstLine.Split(new char[] { ';' });
63        }
64        firstLine = reader.ReadLine();
65      }
66      string[] values = firstLine.Split(new char[] { ';' });
67      for (int i = 0; i < values.Length; i++) {
68        if (i < variableNames.Length)
69          newColumnGroup.AddColumn(new StringColumn(variableNames[i]));
70        else
71          newColumnGroup.AddColumn(new StringColumn("Column " + i));
72      }
73
74      while (!reader.EndOfStream) {
75        newColumnGroup.AddRow(reader.ReadLine().Split(new char[] { ';' }));
76      }
77      reader.Close();
78      DataSet.AddColumnGroup(newColumnGroup);
79      DataSet.FireChanged();
80    }
81
82    public override void UndoExecute() {
83      base.UndoExecute();
84      DataSet.RemoveColumnGroup(newColumnGroup);
85      newColumnGroup = null;
86      DataSet.FireChanged();
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.