-
Notifications
You must be signed in to change notification settings - Fork 0
/
readXML.cs
206 lines (178 loc) · 12.8 KB
/
readXML.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
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml.Linq;
namespace systemEval
{
class readXML
{
public static List<Constraint> XMLCon(OpenFileDialog ofd)
{
XDocument xDoc = XDocument.Load(ofd.FileName); // Opens XML file within framework and loads chosen XML file
//LINQ query to read the xml file details
var modelDetails = from readInfo in xDoc.Descendants("MODEL") // Find the Model and read-in its descendant elements
select new
{
conInfo = readInfo.Descendants("CONSTRAINT").DescendantsAndSelf(), // Find all constraints and read-in their descendant elements
};
// Asset details retrieval
Constraint constraint = new Constraint(null,0,null); // New constraint class
List<Constraint> constraintsList = new List<Constraint>(); // New list of information for each constraint
int i = 0; // Declare counter
string ConName = null;
List<string> StateVars = new List<string>();
double Value = 0;
// Using foreach loop to retrieve all details by iterating through each element with the MODEL element
foreach (var modelElem in modelDetails)
{
// Using foreach loop to retrieve each constraint's respective details by iterating through each element with the CONSTRAINT element
foreach (XElement ce in modelElem.conInfo)
{
double conValue = Convert.ToDouble(ce.Attribute("value") != null ? ce.Attribute("value").Value : "0"); // Extract constraint value data
string stateID = ce.Attribute("key") != null ? ce.Attribute("key").Value : String.Empty; // Extract state variable data
string conName = ce.Attribute("constraintName") != null ? ce.Attribute("constraintName").Value : String.Empty; // Extract constraint name data
if (conValue > 0) // If the element is the constraint value
{
if (i > 0) // If a new constraint
{
constraint = new Constraint(ConName, Value, StateVars); // Populate Constraint class
StateVars = new List<string>();
constraintsList.Add(constraint); // Add constraint to constraints list
}
Value = conValue; // Record constraint value in subsystem constraint class
}
if (conName != String.Empty) // If the element is a constraint name
{
ConName = conName; // Record constraint name in the constraint class
}
if (stateID != String.Empty) // If the element is a state variable
{
StateVars.Add(stateID); // Add state ID associated with constraint
i++;
}
}
constraint = new Constraint(ConName, Value, StateVars); // Populate Constraint class
constraintsList.Add(constraint); // Add constraint to constraints list
}
return constraintsList;
}
public static List<SubsystemClass> XMLSub(OpenFileDialog ofd) // Read in subsystem information within XML file
{
XDocument xDoc = XDocument.Load(ofd.FileName); // Opens XML file within framework and loads chosen XML file
//LINQ query to read the xml file details
var modelDetails = from readInfo in xDoc.Descendants("MODEL") // Find the Model and read-in its descendant elements
select new
{
subInfo = readInfo.Descendants("SUBSYSTEM").DescendantsAndSelf(), // Find all subsystems and read-in their descendant elements
assetInfo = readInfo.DescendantsAndSelf("ASSET").DescendantsAndSelf() // Find all assets and read-in in their descendant elements
};
// Asset details retrieval
Asset asset = new Asset(null, null); // New asset class
SubsystemClass subSys = new SubsystemClass(null,null,null); // New subsystem class
List<Asset> assetsList = new List<Asset>(); // New list of information for each asset
List<SubsystemClass> subsysList = new List<SubsystemClass>(); // New list of information for each subsystem
int i = 0; // Declare counter
string SubName = null;
string StateVar = null;
string AssetName = null;
List<string> StateVars = new List<string>();
List<string> SubNames = new List<string>();
// Using foreach loop to retrieve all details by iterating through each element with the MODEL element
foreach (var modelElem in modelDetails)
{
// Using foreach loop to retrieve each asset's details by iterating through each element with the ASSET element
foreach (XElement ae in modelElem.assetInfo)
{
string assetName = ae.Attribute("AssetName") != null ? ae.Attribute("AssetName").Value : String.Empty; // Extract asset name data
string subName = ae.Attribute("SubsystemName") != null ? ae.Attribute("SubsystemName").Value : String.Empty; // Extract subsystem name data
if (assetName != String.Empty) // If the element is an asset name
{
if (i > 0) // If there is a new asset
{
asset = new Asset(AssetName, SubNames);
assetsList.Add(asset); // Add asset to asset list
asset = new Asset(AssetName,SubNames); // Populate asset class
SubNames = new List<string>();
}
AssetName = assetName; // Record asset name
}
if (subName != String.Empty) // If the element is a subsystem name
{
SubNames.Add(subName); // Record subsystem name in subsystem names list
i++;
}
}
asset = new Asset(AssetName, SubNames); // Populate asset class
assetsList.Add(asset); // Add asset to asset list
i = 0;
// Using foreach loop to retrieve each subsystem and dependecies respective details by iterating through each element with the SUBSYSTEM element
foreach (XElement se in modelElem.subInfo)
{
string stateName = se.Attribute("key") != null ? se.Attribute("key").Value : String.Empty; // Extract state variable data
string subName = se.Attribute("SubsystemName") != null ? se.Attribute("SubsystemName").Value : String.Empty; // Extract subsystem name data
if (subName != String.Empty) // If the element is a subsystem name
{
if (i > 0) // If there is a new subsystem
{
foreach (Asset item in assetsList) // Check each asset
{
if (item.SubNames.Contains(SubName)) // Find subsystem name within asset
{
AssetName = item.AssetName; // Record asset name for subsystem class
}
}
subSys = new SubsystemClass(SubName, AssetName, StateVars); // Populate subsystem class
subsysList.Add(subSys); // Add subsystem class to subsystem list
StateVars = new List<string>();
}
SubName = subName; // Record subsystem name for subsystem class
i++;
}
if (stateName != String.Empty) // If the element is a state variable
{
StateVar = stateName;
StateVars.Add(stateName); // Record state variable requirement for subsystem class
}
}
subSys = new SubsystemClass(SubName, AssetName, StateVars); // Populate subsystem class
subsysList.Add(subSys); // Add subsystem class to subsystem list
}
return subsysList;
}
public static List<StateVar> XMLState(List<Constraint> constraintsList, List<SubsystemClass> subsysList) // Using previously read-in XML data, create a state variable list
{
StateVar stateVar = new StateVar(null, null, null);
List<StateVar> stateVarList = new List<StateVar>(); // New list of information for each state variable
string StateName = null;
string SubName = null;
string ConName = null;
List<string> ConNames = new List<string>();
List<Constraint> conNames = new List<Constraint>();
foreach (SubsystemClass item in subsysList)
{
foreach (string item2 in item.StateVars)
{
StateName = item2;
foreach (Constraint item6 in constraintsList)
{
foreach (string item7 in item6.StateVars)
{
if (item2 == item7)
{
ConName = item6.ConName;
ConNames.Add(ConName);
}
}
}
SubName = item.SubName;
stateVar = new StateVar(StateName, SubName, ConNames);
stateVarList.Add(stateVar);
ConNames = new List<string>();
}
}
return stateVarList;
}
}
}