1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Parameters;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using System;
|
---|
28 | using System.Collections.Generic;
|
---|
29 | using System.Globalization;
|
---|
30 | using System.IO;
|
---|
31 | using System.Linq;
|
---|
32 | using System.Text;
|
---|
33 | using System.Text.RegularExpressions;
|
---|
34 | using YamlDotNet.RepresentationModel;
|
---|
35 | using HEAL.Attic;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.BioBoost.Data {
|
---|
38 |
|
---|
39 | [StorableType("ADCB9CD6-ECBD-4202-B945-0602E538D771")]
|
---|
40 | [Item("DataItem", "Generic Item for holding various pieces of information")]
|
---|
41 | public abstract class DataItem : ParameterizedNamedItem {
|
---|
42 |
|
---|
43 | #region Construction & Cloning
|
---|
44 | [StorableConstructor]
|
---|
45 | protected DataItem(StorableConstructorFlag _) : base(_) { }
|
---|
46 | protected DataItem(DataItem orig, Cloner cloner) : base(orig, cloner) {}
|
---|
47 | protected DataItem() {}
|
---|
48 | #endregion
|
---|
49 |
|
---|
50 | public static IEnumerable<DataItem> ParseFile(string filename) {
|
---|
51 | using (var reader = File.OpenText(filename)) {
|
---|
52 | return ParseStream(reader);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public abstract bool IsEquivalentTo(DataItem other);
|
---|
57 |
|
---|
58 | public static IEnumerable<DataItem> ParseStream(TextReader reader) {
|
---|
59 | var yaml = new YamlStream();
|
---|
60 | yaml.Load(reader);
|
---|
61 | if (yaml.Documents.Count == 0) return Enumerable.Empty<DataItem>();
|
---|
62 | var root = yaml.Documents[0].RootNode as YamlMappingNode;
|
---|
63 | if (root == null) return Enumerable.Empty<DataItem>();
|
---|
64 | return ParseItems(root);
|
---|
65 | }
|
---|
66 |
|
---|
67 | private static IEnumerable<DataItem> ParseItems(YamlMappingNode root) {
|
---|
68 | foreach (var child in root.Children) {
|
---|
69 | var type = child.Key as YamlScalarNode;
|
---|
70 | var value = child.Value as YamlSequenceNode;
|
---|
71 | if (type == null || value == null) {
|
---|
72 | // ignore or error?
|
---|
73 | } else {
|
---|
74 | switch (((string) type).ToLowerInvariant()) {
|
---|
75 | case "products":
|
---|
76 | foreach (var item in ParseSequence<Product>(value))
|
---|
77 | yield return item;
|
---|
78 | break;
|
---|
79 | case "logistics":
|
---|
80 | foreach (var item in ParseSequence<Logistic>(value))
|
---|
81 | yield return item;
|
---|
82 | break;
|
---|
83 | case "conversions":
|
---|
84 | foreach (var item in ParseSequence<Conversion>(value))
|
---|
85 | yield return item;
|
---|
86 | break;
|
---|
87 | case "cost-factors":
|
---|
88 | foreach (var item in ParseSequence<CostFactors>(value))
|
---|
89 | yield return item;
|
---|
90 | break;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public static IEnumerable<T> ParseSequence<T>(YamlSequenceNode seq) where T : DataItem, new() {
|
---|
97 | foreach (var node in seq.Children) {
|
---|
98 | var mappingNode = node as YamlMappingNode;
|
---|
99 | if (mappingNode == null) continue;
|
---|
100 | var item = new T();
|
---|
101 | item.Parse(mappingNode);
|
---|
102 | yield return item;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | protected virtual void Parse(YamlMappingNode node) {
|
---|
107 | foreach (var child in node) {
|
---|
108 | var parameterNode = child.Key as YamlScalarNode;
|
---|
109 | if (parameterNode == null) continue;
|
---|
110 | var parameterName = ConvertNameYAML2NET(parameterNode.ToString());
|
---|
111 | IParameter param;
|
---|
112 | if (Parameters.TryGetValue(parameterName, out param) && param is IValueParameter) {
|
---|
113 | SetParameter(param, child.Value);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | // TODO: check that all parameters have been filled
|
---|
117 | }
|
---|
118 |
|
---|
119 | private static void SetParameter(IParameter param, YamlNode node) {
|
---|
120 | if (node is YamlScalarNode) {
|
---|
121 | SetScalarParameter((IValueParameter) param, (YamlScalarNode) node);
|
---|
122 | } else if (node is YamlMappingNode) {
|
---|
123 | SetMappingParameter((IValueParameter) param, (YamlMappingNode) node);
|
---|
124 | } else {
|
---|
125 | // TODO: parse error/warning?
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | private static void SetScalarParameter(IValueParameter param, YamlScalarNode node) {
|
---|
130 | if (param.DataType == typeof(StringValue)) {
|
---|
131 | param.Value = new StringValue(node.ToString());
|
---|
132 | } else if (param.DataType == typeof(DoubleValue)) {
|
---|
133 | param.Value = new DoubleValue(Double.Parse(node.ToString(), CultureInfo.InvariantCulture));
|
---|
134 | } else {
|
---|
135 | // TODO: parse error/warning?
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | private static void SetMappingParameter(IValueParameter param, YamlMappingNode node) {
|
---|
140 | if (typeof (DataItem).IsAssignableFrom(param.DataType)) {
|
---|
141 | var constructorInfo = param.DataType.GetConstructor(Type.EmptyTypes);
|
---|
142 | if (constructorInfo != null) {
|
---|
143 | var item = (DataItem) constructorInfo.Invoke(new object[0]);
|
---|
144 | item.Parse(node);
|
---|
145 | param.Value = item;
|
---|
146 | return;
|
---|
147 | }
|
---|
148 | } else if (param.DataType.IsAssignableFrom(typeof(ValueParameterCollection))) {
|
---|
149 | var dict = new ValueParameterCollection();
|
---|
150 | foreach (var child in node.Children) {
|
---|
151 | var key = child.Key as YamlScalarNode;
|
---|
152 | if (key != null) {
|
---|
153 | var p = new ValueParameter<DoubleValue>(key.ToString());
|
---|
154 | SetParameter(p, child.Value);
|
---|
155 | dict.Add(p);
|
---|
156 | } else {
|
---|
157 | // TODO: parse error/warning?
|
---|
158 | }
|
---|
159 | }
|
---|
160 | param.Value = dict;
|
---|
161 | }
|
---|
162 | // TODO: parse error? warning?
|
---|
163 | }
|
---|
164 |
|
---|
165 | public static IItem CreateItem(YamlNode node) {
|
---|
166 | var n = node as YamlScalarNode;
|
---|
167 | if (n == null) return null;
|
---|
168 | double d;
|
---|
169 | string s = n.ToString();
|
---|
170 | if (Double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out d)) {
|
---|
171 | return new DoubleValue(d);
|
---|
172 | } else {
|
---|
173 | return new StringValue(s);
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | #region Auxiliary Methods
|
---|
178 | public static string ConvertNameYAML2NET(string s) {
|
---|
179 | var sb = new StringBuilder();
|
---|
180 | foreach (var word in s.Split('-')) {
|
---|
181 | sb.Append(CultureInfo.InvariantCulture.TextInfo.ToTitleCase(word.ToLower()));
|
---|
182 | }
|
---|
183 | return sb.ToString();
|
---|
184 | }
|
---|
185 |
|
---|
186 | public static string ConvertNameNET2YAML(string s) {
|
---|
187 | return Regex.Replace(s, "([A-Z])", "-$1", RegexOptions.Compiled).Trim().ToLower();
|
---|
188 | }
|
---|
189 | #endregion
|
---|
190 |
|
---|
191 | }
|
---|
192 | }
|
---|