Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactorBranch/HeuristicLab.Scheduling.JSSP/Operation.cs @ 888

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

Refactored cloning in plugins HL.Routing and HL.Scheduling

#285 (Cloning could be improved by creating objects at the bottom of the cloning chain with 'new' instead of the top with Activator.CreateInstance())

File size: 5.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 System.Collections;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29
30namespace HeuristicLab.Scheduling.JSSP {
31  public class Operation : ItemBase, IComparable {
32
33    #region Data
34    private IntData start;
35    public int Start {
36      get { return start.Data; }
37      set {
38        start.Data = value;
39        OnChanged();
40      }
41    }
42
43    private IntData duration;
44    public int Duration {
45      get { return duration.Data; }
46      set {
47        duration.Data = value;
48        OnChanged();
49      }
50    }
51
52    private IntData job;
53    public int Job {
54      get { return job.Data; }
55      set {
56        job.Data = value;
57        OnChanged();
58      }
59    }
60
61    private IntData operationIndex;
62    public int OperationIndex {
63      get { return operationIndex.Data; }
64      set { operationIndex.Data = value; }
65    }
66
67    private IntArrayData machines;
68    public int[] Machines {
69      get { return machines.Data; }
70      set {
71        machines.Data = value;
72        OnChanged();
73      }
74    }
75
76    private ItemList predecessors;
77    public ItemList Predecessors {
78      get { return predecessors; }
79      set { predecessors = value; }
80    }
81    #endregion
82
83    public Operation()
84      : this(-1, 0, 0, -1, new int[0], null) {
85    }
86
87    public Operation(int job, int earliestStart, int duration, int operationIndex, int[] machines, int[] predecessors) {
88      this.start = new IntData(earliestStart);
89      this.duration = new IntData(duration);
90      this.job = new IntData(job);
91      this.machines = new IntArrayData(machines);
92      this.predecessors = new ItemList();
93      if(predecessors != null) {
94        for(int i = 0; i < predecessors.Length; i++) {
95          this.predecessors.Add(new IntData(predecessors[i]));
96        }
97      }
98      this.operationIndex = new IntData(operationIndex);
99    }
100
101    public Operation(Operation original) : this(original, new Dictionary<Guid, object>()) { }
102    protected Operation(Operation original, IDictionary<Guid, object> clonedObjects)
103      : base(original, clonedObjects) {
104      this.duration = (IntData)Auxiliary.Clone(original.duration, clonedObjects);
105      this.start = (IntData)Auxiliary.Clone(original.start, clonedObjects);
106      this.operationIndex = (IntData)Auxiliary.Clone(original.operationIndex, clonedObjects);
107      this.job = (IntData)Auxiliary.Clone(original.job, clonedObjects);
108      this.machines = (IntArrayData)Auxiliary.Clone(original.machines, clonedObjects);
109      this.predecessors = (ItemList)Auxiliary.Clone(original.predecessors, clonedObjects);
110    }
111
112    public override string ToString() {
113      if(this.Job == -1) {
114        return "";
115      }
116      return (String.Format("|{2},{3}:[m:{0} d:{1}]", machines.Data[0], duration, job, operationIndex));
117    }
118
119    #region IComparable Members
120
121    public int CompareTo(object obj) {
122      if(obj is Operation) {
123        Operation bin = (Operation)obj;
124        return (start.CompareTo(bin.start));
125      } else { throw new ArgumentException("Object is not an Operation"); }
126    }
127
128    #endregion
129
130    #region IViewable Members
131
132    public override IView CreateView() {
133      return new OperationView(this);
134    }
135
136    #endregion
137
138    #region IStorable Members
139
140    public override object Clone(IDictionary<Guid, object> clonedObjects) {
141      return new Operation(this, clonedObjects);
142    }
143
144    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
145      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
146      node.AppendChild(PersistenceManager.Persist("Job", job, document, persistedObjects));
147      node.AppendChild(PersistenceManager.Persist("OperationIndex", operationIndex, document, persistedObjects));
148      node.AppendChild(PersistenceManager.Persist("Start", start, document, persistedObjects));
149      node.AppendChild(PersistenceManager.Persist("Duration", duration, document, persistedObjects));
150      node.AppendChild(PersistenceManager.Persist("Predecessors", predecessors, document, persistedObjects));
151      node.AppendChild(PersistenceManager.Persist("Machines", machines, document, persistedObjects));
152      return node;
153    }
154
155    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
156      base.Populate(node, restoredObjects);
157      job = (IntData)PersistenceManager.Restore(node.SelectSingleNode("Job"), restoredObjects);
158      operationIndex = (IntData)PersistenceManager.Restore(node.SelectSingleNode("OperationIndex"), restoredObjects);
159      start = (IntData)PersistenceManager.Restore(node.SelectSingleNode("Start"), restoredObjects);
160      duration = (IntData)PersistenceManager.Restore(node.SelectSingleNode("Duration"), restoredObjects);
161      predecessors = (ItemList)PersistenceManager.Restore(node.SelectSingleNode("Predecessors"), restoredObjects);
162      machines = (IntArrayData)PersistenceManager.Restore(node.SelectSingleNode("Machines"), restoredObjects);
163    }
164
165    #endregion
166  }
167}
Note: See TracBrowser for help on using the repository browser.