Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlTextWriterBranch/HeuristicLab.Scheduling.JSSP/Schedule.cs @ 165

Last change on this file since 165 was 119, checked in by gkronber, 17 years ago

created a branch that uses XmlTextWriter instead of XMLDocument to save documents. Investigating ticket #103.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using System.Xml;
26using HeuristicLab.Core;
27
28namespace HeuristicLab.Scheduling.JSSP {
29  public class Schedule : ItemBase {
30
31    private ScheduleTree[] schedule;
32
33    public Schedule(int nrOfMachines, int timeSpan)
34      : base() {
35      schedule = new ScheduleTree[nrOfMachines];
36      for(int i = 0; i < nrOfMachines; i++) {
37        schedule[i] = new ScheduleTree(timeSpan);
38      }
39    }
40
41    public Schedule() : this(0, 100) { }
42
43    public int Machines {
44      get { return schedule.Length; }
45    }
46
47    public ScheduleTree GetMachineSchedule(int machineIndex) {
48      return schedule[machineIndex];
49    }
50
51    // return value: operation completion time
52    public int ScheduleOperation(Operation op) {
53      int machine = op.Machines[0];
54      return schedule[machine].Insert(op);
55    }
56
57    public override string ToString() {
58      string s = "";
59      for(int i = 0; i < this.Machines; i++) {
60        s += schedule[i].ToString();
61        s += "\n";
62      }
63      return s;
64    }
65
66    #region IStorable Members
67
68    //public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
69    //  XmlNode node = base.GetXmlNode(name, document, persistedObjects);
70    //  XmlAttribute machineAttribute = document.CreateAttribute("Machines");
71    //  machineAttribute.Value = Machines.ToString();
72    //  node.Attributes.Append(machineAttribute);
73    //  for(int i = 0; i < Machines; i++) {
74    //    node.AppendChild(PersistenceManager.Persist("Machine" + i.ToString(), schedule[i], document, persistedObjects));
75    //  }
76    //  return node;
77    //}
78    public override void Persist(string name, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
79      base.Persist(name, writer, persistedObjects);
80      writer.WriteAttributeString("Machines", Machines.ToString());
81      for(int i = 0; i < Machines; i++)
82        PersistenceManager.Persist("Machine" + i.ToString(), schedule[i], writer, persistedObjects);
83    }
84
85    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
86      base.Populate(node, restoredObjects);
87      int machines = int.Parse(node.Attributes["Machines"].Value);
88      schedule = new ScheduleTree[machines];
89      for(int i = 0; i < machines; i++) {
90        schedule[i] = (ScheduleTree)PersistenceManager.Restore(node.SelectSingleNode("Machine" + i.ToString()), restoredObjects);
91      }
92    }
93
94    public override object Clone(IDictionary<Guid, object> clonedObjects) {
95      int timespan = 0;
96      if ((schedule != null) && (schedule.Length > 0)) {
97        timespan = schedule[0].Timespan;
98      }
99      Schedule clone = new Schedule(this.Machines, timespan);
100      clonedObjects.Add(Guid, clone);
101      clone.schedule = new ScheduleTree[this.Machines];
102      for(int i = 0; i < this.Machines; i++) {
103        clone.schedule[i] = (ScheduleTree)Auxiliary.Clone(schedule[i], clonedObjects);
104      }
105      return clone;
106    }
107
108    #endregion
109  }
110}
Note: See TracBrowser for help on using the repository browser.