-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathInstanceIO.cs
403 lines (362 loc) · 15 KB
/
InstanceIO.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
using SC.ObjectModel.Additionals;
using SC.ObjectModel.Elements;
using SC.ObjectModel.IO;
using SC.ObjectModel.IO.Json;
using SC.ObjectModel.Rules;
using SC.Toolbox;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Xml;
namespace SC.ObjectModel
{
public partial class Instance
{
#region Xml exportation
/// <summary>
/// Writes the instance to an XML-file without any solutions
/// </summary>
/// <param name="path">The path to write to</param>
public void WriteXMLWithoutSolutions(string path)
{
// Delete if existing
if (File.Exists(path))
{
File.Delete(path);
}
// Prepare
XmlTextWriter textWriter = new XmlTextWriter(path, Encoding.UTF8);
textWriter.WriteStartDocument();
textWriter.WriteStartElement(ExportationConstants.XML_INSTANCE_IDENT);
textWriter.WriteStartAttribute(ExportationConstants.XML_VERSION_IDENT);
textWriter.WriteValue(ExportationConstants.XML_VERSION);
textWriter.WriteEndAttribute();
textWriter.WriteEndElement();
textWriter.WriteEndDocument();
textWriter.Close();
// Load existing data
XmlDocument document = new XmlDocument();
document.Load(path);
// Get root
XmlNode root = document.SelectSingleNode("//" + ExportationConstants.XML_INSTANCE_IDENT);
// Create container root
XmlNode containerRoot = document.CreateElement(ExportationConstants.XML_CONTAINER_COLLECTION_IDENT);
root.AppendChild(containerRoot);
// Submit data
foreach (var container in Containers)
{
containerRoot.AppendChild(container.WriteXML(document));
}
// Create pieces root
XmlNode pieceRoot = document.CreateElement(ExportationConstants.XML_PIECE_COLLECTION_IDENT);
root.AppendChild(pieceRoot);
// Submit data
foreach (var piece in Pieces)
{
pieceRoot.AppendChild(piece.WriteXML(document));
}
// Save the document
document.Save(path);
}
/// <summary>
/// Writes the instance to an XML-file
/// </summary>
/// <param name="path">The path to write to</param>
public void WriteXML(string path)
{
// Delete if existing
if (File.Exists(path))
{
File.Delete(path);
}
// Prepare
XmlTextWriter textWriter = new XmlTextWriter(path, Encoding.UTF8);
textWriter.WriteStartDocument();
textWriter.WriteStartElement(ExportationConstants.XML_INSTANCE_IDENT);
textWriter.WriteStartAttribute(ExportationConstants.XML_VERSION_IDENT);
textWriter.WriteValue(ExportationConstants.XML_VERSION);
textWriter.WriteEndAttribute();
textWriter.WriteEndElement();
textWriter.WriteEndDocument();
textWriter.Close();
// Load existing data
XmlDocument document = new XmlDocument();
document.Load(path);
// Get root
XmlNode root = document.SelectSingleNode("//" + ExportationConstants.XML_INSTANCE_IDENT);
// Name
XmlAttribute attr = document.CreateAttribute(Helper.Check(() => Name));
attr.Value = Name;
root.Attributes.Append(attr);
// Create container root
XmlNode containerRoot = document.CreateElement(ExportationConstants.XML_CONTAINER_COLLECTION_IDENT);
root.AppendChild(containerRoot);
// Submit data
foreach (var container in Containers)
{
containerRoot.AppendChild(container.WriteXML(document));
}
// Create pieces root
XmlNode pieceRoot = document.CreateElement(ExportationConstants.XML_PIECE_COLLECTION_IDENT);
root.AppendChild(pieceRoot);
// Submit data
foreach (var piece in Pieces)
{
pieceRoot.AppendChild(piece.WriteXML(document));
}
// Create solutions root
XmlNode solutionRoot = document.CreateElement(ExportationConstants.XML_SOLUTION_COLLECTION_IDENT);
root.AppendChild(solutionRoot);
// Submit data
foreach (var solution in Solutions.Where(s => s.ContainerContent.Any(c => c.Any())))
{
solutionRoot.AppendChild(solution.WriteXML(document));
}
// Save the document
document.Save(path);
}
/// <summary>
/// Reads an instance file from the given path.
/// </summary>
/// <param name="path">The path to the instance file</param>
/// <returns>The read instance</returns>
public static Instance ReadXML(string path)
{
// Prepare XML data
XmlDocument document = new XmlDocument();
document.Load(path);
Instance instance = new Instance();
// Name
if (document.SelectSingleNode("//Instance").Attributes["Name"] != null)
instance.Name = document.SelectSingleNode("//Instance").Attributes["Name"].Value;
else
instance.Name = "Default";
// Read containers
XmlNode containersRoot = document.SelectSingleNode("//Instance/Containers");
List<Container> containers = new List<Container>();
if (containersRoot != null)
{
foreach (var childNode in containersRoot.ChildNodes.OfType<XmlNode>())
{
Container container = new Container();
container.LoadXML(childNode);
containers.Add(container);
}
}
// Read pieces
XmlNode piecesRoot = document.SelectSingleNode("//Instance/Pieces");
List<VariablePiece> pieces = new List<VariablePiece>();
if (piecesRoot != null)
{
foreach (var childNode in piecesRoot.ChildNodes.OfType<XmlNode>())
{
VariablePiece piece = new VariablePiece();
piece.LoadXML(childNode);
pieces.Add(piece);
}
}
// Add all items
instance.Containers.AddRange(containers);
instance.Pieces.AddRange(pieces);
// Read solutions
XmlNode solutionsRoot = document.SelectSingleNode("//Instance/Solutions");
List<COSolution> solutions = new List<COSolution>();
if (solutionsRoot != null)
{
foreach (var childNode in solutionsRoot.ChildNodes.OfType<XmlNode>())
{
COSolution solution = instance.CreateSolution(false, MeritFunctionType.None);
solution.LoadXML(childNode);
solutions.Add(solution);
}
}
// Return
return instance;
}
#endregion
#region JSON I/O
/// <summary>
/// Converts a simplified representation of an instance (used for JSON serialization) to a proper instance.
/// </summary>
/// <param name="jsonInstance">The instance in simplified presentation.</param>
/// <returns>The converted instance.</returns>
public static Instance FromJsonInstance(JsonInstance jsonInstance)
{
// Create instance with given parameters
Instance instance = new Instance
{
Name = jsonInstance.Name
};
instance.Containers.AddRange(jsonInstance.Containers.Select(c => new Container()
{
ID = c.ID,
Mesh = new MeshCube()
{
Length = c.Length,
Width = c.Width,
Height = c.Height
},
MaxWeight = c.MaxWeight
}));
instance.Pieces.AddRange(jsonInstance.Pieces.Select(p =>
{
var convp = new VariablePiece() { ID = p.ID, Weight = p.Weight };
if (p.Flags != null)
convp.SetFlags(p.Flags.Select(f => (f.FlagId, f.FlagValue)));
foreach (var comp in p.Cubes)
convp.AddComponent(comp.X, comp.Y, comp.Z, comp.Length, comp.Width, comp.Height);
return convp;
}));
if (jsonInstance.Rules != null && jsonInstance.Rules.FlagRules != null)
instance.Rules = new RuleSet()
{
FlagRules = jsonInstance.Rules.FlagRules.Select(r => new FlagRule()
{
FlagId = r.FlagId,
RuleType = r.RuleType,
Parameter = r.Parameter,
}).ToList(),
};
// Seal it
foreach (var container in instance.Containers)
container.Seal();
foreach (var piece in instance.Pieces)
piece.Seal();
// Return new instance
return instance;
}
/// <summary>
/// Convert this instance into a simplified type for JSON serialization.
/// </summary>
/// <returns>The instance as a simplified representation.</returns>
public JsonInstance ToJsonInstance()
{
// Convert to JSON representation
var jsonInstance = new JsonInstance()
{
Name = Name,
Containers = Containers.Select(c =>
new JsonContainer()
{
ID = c.ID,
Length = c.Mesh.Length,
Width = c.Mesh.Width,
Height = c.Mesh.Height
}).ToList(),
Pieces = Pieces.Select(p =>
new JsonPiece()
{
ID = p.ID,
Weight = p.Weight,
Flags = p.GetFlags().Select(f => new JsonFlag() { FlagId = f.flag, FlagValue = f.value }).ToList(),
Cubes = p.Original.Components.Select(c =>
new JsonCube()
{
X = c.RelPosition.X,
Y = c.RelPosition.Y,
Z = c.RelPosition.Z,
Length = c.Length,
Width = c.Width,
Height = c.Height
}).ToList()
}).ToList(),
Rules = new JsonRuleSet()
{
FlagRules = Rules.FlagRules.Select(r => new JsonFlagRule()
{
FlagId = r.FlagId,
RuleType = r.RuleType,
Parameter = r.Parameter
}).ToList(),
}
};
// Return it
return jsonInstance;
}
/// <summary>
/// Reads an instance file from the given path.
/// </summary>
/// <param name="path">The path to the instance file</param>
/// <returns>The read instance</returns>
public static Instance ReadJson(string json)
{
// Deserialize JSON
var jsonInstance = JsonIO.From<JsonInstance>(json);
// Convert and return it
return FromJsonInstance(jsonInstance);
}
/// <summary>
/// Converts the instance to a JSON string.
/// </summary>
/// <returns>The JSON string.</returns>
public string WriteJson()
{
// Serialize
string json = JsonIO.To(ToJsonInstance());
// Return JSON string
return json;
}
#endregion
//#region CSV I/O
///// <summary>
///// Reads an Excel based instance file from the given path.
///// </summary>
///// <param name="path">The path to the instance file</param>
///// <returns>The read instance</returns>
//public static Instance ReadExcel(string path)
//{
// // Init
// ExcelPackage excelFile = new ExcelPackage(new FileInfo(path));
// var containerSheet = excelFile.Workbook.Worksheets[1];
// var orderSheet = excelFile.Workbook.Worksheets[2];
// List<Container> containers = new List<Container>();
// for (int row = 2; containerSheet.Cells[row, 1].Value != null; row++)
// {
// Container container = new Container()
// {
// ID = row - 1,
// Mesh = new MeshCube()
// {
// Length = containerSheet.Cells[row, 4].GetValue<double>(),
// Width = containerSheet.Cells[row, 5].GetValue<double>(),
// Height = containerSheet.Cells[row, 9].GetValue<double>() - containerSheet.Cells[row, 6].GetValue<double>(),
// }
// };
// container.Seal();
// containers.Add(container);
// }
// List<VariablePiece> pieces = new List<VariablePiece>(); int pieceID = 0;
// for (int row = 2; orderSheet.Cells[row, 1].Value != null; row++)
// {
// int quantity = orderSheet.Cells[row, 3].GetValue<int>();
// for (int i = 0; i < quantity; i++)
// {
// VariablePiece piece = new VariablePiece() { ID = (++pieceID), };
// // Add the parallelepiped component
// piece.AddComponent(0, 0, 0,
// orderSheet.Cells[row, 4].GetValue<double>(),
// orderSheet.Cells[row, 5].GetValue<double>(),
// orderSheet.Cells[row, 6].GetValue<double>());
// // Assume only this side up pieces
// bool allowTurning = true;
// if (allowTurning) piece.ForbiddenOrientations = new HashSet<int>();
// else piece.ForbiddenOrientations = new HashSet<int>(MeshConstants.ORIENTATIONS.Except(MeshConstants.ORIENTATIONS_THIS_SIDE_UP));
// // Seal it
// piece.Seal();
// pieces.Add(piece);
// }
// }
// // Create instance
// Instance instance = new Instance();
// // Add all items
// instance.Containers.AddRange(containers);
// instance.Pieces.AddRange(pieces);
// // Return it
// return instance;
//}
//#endregion
}
}