Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.Scheduling/3.3/JSSPORLIBInstanceProvider.cs @ 10988

Last change on this file since 10988 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 4.8 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;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Reflection;
27using System.Text.RegularExpressions;
28using ICSharpCode.SharpZipLib.Zip;
29
30namespace HeuristicLab.Problems.Instances.Scheduling {
31  public class JSSPORLIBInstanceProvider : ProblemInstanceProvider<JSSPData> {
32
33    public override string Name {
34      get { return "ORLIB JSSP"; }
35    }
36
37    public override string Description {
38      get { return "Job shop scheduling problems from the Operations Research Library."; }
39    }
40
41    public override Uri WebLink {
42      get { return new Uri("http://people.brunel.ac.uk/~mastjjb/jeb/orlib/jobshopinfo.html"); }
43    }
44
45    public override string ReferencePublication {
46      get { return String.Empty; }
47    }
48
49    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
50      var instanceArchiveName = GetResourceName("JSSPORLIB.zip");
51      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
52
53      using (var instanceStream = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
54        foreach (var entry in GetZipContents(instanceStream).OrderBy(x => x)) {
55          yield return new JSSPORLIBDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry, null);
56        }
57      }
58    }
59
60    public override JSSPData LoadData(IDataDescriptor id) {
61      var descriptor = (JSSPORLIBDataDescriptor)id;
62      var instanceArchiveName = GetResourceName("JSSPORLIB.zip");
63      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
64        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
65
66        using (var stream = instancesZipFile.GetInputStream(entry)) {
67          var parser = new JSSPORLIBParser();
68          parser.Parse(stream);
69          var instance = Load(parser);
70          instance.Name = id.Name;
71          instance.Description = id.Description;
72
73          return instance;
74        }
75      }
76    }
77
78    public override bool CanImportData {
79      get { return true; }
80    }
81    public override JSSPData ImportData(string path) {
82      var parser = new JSSPORLIBParser();
83      parser.Parse(path);
84      var instance = Load(parser);
85      instance.Name = Path.GetFileName(path);
86      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
87      return instance;
88    }
89
90    private JSSPData Load(JSSPORLIBParser parser) {
91      var instance = new JSSPData {
92        Jobs = parser.Jobs,
93        Resources = parser.Resources,
94        ProcessingTimes = parser.ProcessingTimes,
95        Demands = parser.Demands,
96        DueDates = parser.DueDates
97      };
98      return instance;
99    }
100
101    public override bool CanExportData {
102      get { return true; }
103    }
104
105    public override void ExportData(JSSPData instance, string path) {
106      var parser = new JSSPORLIBParser {
107        Name = instance.Name,
108        Description = instance.Description,
109        Jobs = instance.Jobs,
110        Resources = instance.Resources,
111        ProcessingTimes = instance.ProcessingTimes,
112        Demands = instance.Demands,
113        DueDates = instance.DueDates
114      };
115      parser.Export(path);
116    }
117
118    private string GetDescription() {
119      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
120    }
121
122    protected virtual string GetResourceName(string fileName) {
123      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
124        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
125    }
126
127    protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
128      ZipEntry entry;
129      while ((entry = zipFile.GetNextEntry()) != null) {
130        yield return entry.Name;
131      }
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.