-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompositeModelConfig.cs
145 lines (131 loc) · 4.78 KB
/
CompositeModelConfig.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace EasyMLCore.MLP
{
/// <summary>
/// Configuration of the CompositeModel.
/// </summary>
[Serializable]
public class CompositeModelConfig : ConfigBase, IModelConfig
{
//Constants
/// <summary>
/// Name of an associated xsd type.
/// </summary>
public const string XsdTypeName = "CompositeModelConfig";
//Attribute properties
/// <summary>
/// The collection of submodel configurations.
/// </summary>
public List<IModelConfig> SubModelCfgCollection { get; }
//Constructors
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="subModelCfgs">The collection of submodel configurations.</param>
public CompositeModelConfig(IEnumerable<IModelConfig> subModelCfgs)
{
SubModelCfgCollection = new List<IModelConfig>();
foreach (IModelConfig modelCfg in subModelCfgs)
{
SubModelCfgCollection.Add((IModelConfig)modelCfg.DeepClone());
}
Check();
return;
}
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="subModelCfgs">Submodel configuration (params).</param>
public CompositeModelConfig(params IModelConfig[] subModelCfgs)
: this(subModelCfgs.AsEnumerable())
{
return;
}
/// <summary>
/// The deep copy constructor.
/// </summary>
/// <param name="source">The source instance.</param>
public CompositeModelConfig(CompositeModelConfig source)
: this(source.SubModelCfgCollection)
{
return;
}
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="elem">A xml element containing the configuration data.</param>
public CompositeModelConfig(XElement elem)
{
//Validation
XElement validatedElem = Validate(elem, XsdTypeName);
SubModelCfgCollection = new List<IModelConfig>();
//Parsing
//Models
foreach (XElement modelElem in validatedElem.Elements())
{
switch (modelElem.Name.LocalName)
{
case "networkModel":
SubModelCfgCollection.Add(new NetworkModelConfig(modelElem));
break;
case "crossValModel":
SubModelCfgCollection.Add(new CrossValModelConfig(modelElem));
break;
case "stackingModel":
SubModelCfgCollection.Add(new StackingModelConfig(modelElem));
break;
case "bhsModel":
SubModelCfgCollection.Add(new BHSModelConfig(modelElem));
break;
case "rvflModel":
SubModelCfgCollection.Add(new RVFLModelConfig(modelElem));
break;
case "compositeModel":
SubModelCfgCollection.Add(new CompositeModelConfig(modelElem));
break;
default:
throw new ArgumentException($"Unknown descendant element name {modelElem.Name.LocalName}.", nameof(elem));
}
}
Check();
return;
}
//Properties
/// <inheritdoc/>
public override bool ContainsOnlyDefaults { get { return false; } }
//Methods
/// <inheritdoc/>
protected override void Check()
{
if (SubModelCfgCollection.Count < 1)
{
throw new ArgumentException($"At least one model configuration has to be defined.", nameof(SubModelCfgCollection));
}
return;
}
/// <inheritdoc/>
public override ConfigBase DeepClone()
{
return new CompositeModelConfig(this);
}
/// <inheritdoc/>
public override XElement GetXml(string rootElemName, bool suppressDefaults)
{
XElement rootElem = new XElement(rootElemName);
foreach (IModelConfig subModelCfg in SubModelCfgCollection)
{
rootElem.Add(subModelCfg.GetXml(suppressDefaults));
}
Validate(rootElem, XsdTypeName);
return rootElem;
}
/// <inheritdoc/>
public override XElement GetXml(bool suppressDefaults)
{
return GetXml("compositeModel", suppressDefaults);
}
}//CompositeModelConfig
}//Namespace