Skip to content

Commit d1ee2de

Browse files
committed
ISSUE-74 : code refactoring in the parser's config file
1 parent 3b27e9f commit d1ee2de

3 files changed

Lines changed: 97 additions & 75 deletions

File tree

ReactiveXComponent/Configuration/XCApiTags.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static class XCApiTags
1515
public const string EventCode = "eventCode";
1616
public const string ComponentCode = "componentCode";
1717
public const string StateMachineCode = "stateMachineCode";
18+
public const string StateMachine = "stateMachine";
1819
public const string Topic = "topic";
1920
public const string Subscribe = "subscribe";
2021
public const string EventType = "eventType";

ReactiveXComponent/Parser/StateMachineInfo.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
{
33
public class StateMachineInfo
44
{
5-
public string StateMachineName { get; set; }
6-
public int StateMachineCode { get; set; }
5+
public string StateMachineName { get; }
6+
public int StateMachineCode { get; }
7+
8+
public StateMachineInfo(string stateMachineName, int stateMachineCode)
9+
{
10+
StateMachineName = stateMachineName;
11+
StateMachineCode = stateMachineCode;
12+
}
713
}
814
}

ReactiveXComponent/Parser/XCApiConfigParser.cs

Lines changed: 88 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,67 @@ public class XCApiConfigParser : IXCApiConfigParser
1313
{
1414
private XCApiDescription _xcApiDescription;
1515
private XNamespace _xc;
16-
private Dictionary<string, int> _eventCodeByEvent;
17-
private Dictionary<TopicIdentifier, string> _publisherTopicByIdentifier;
18-
private Dictionary<TopicIdentifier, string> _subscriberTopicByIdentifier;
19-
private Dictionary<long, string> _snapshotTopicByComponent;
20-
private Dictionary<string, List<StateMachineInfo>> _stateMachineInfoByComponentRepository;
21-
private Dictionary<string, int> _componentCodeByComponentNameRepo;
16+
private Dictionary<string, int> _eventCodeByEventRepository;
17+
private Dictionary<TopicIdentifier, string> _publisherTopicByIdentifierRepository;
18+
private Dictionary<TopicIdentifier, string> _subscriberTopicByIdentifierRepository;
19+
private Dictionary<long, string> _snapshotTopicByComponentRepository;
20+
private readonly Dictionary<string, List<StateMachineInfo>> _stateMachineInfoByComponentRepository =
21+
new Dictionary<string, List<StateMachineInfo>>();
22+
private readonly Dictionary<string, int> _componentCodeByComponentNameRepository = new Dictionary<string, int>();
2223

2324

2425
public void Parse(Stream xcApiStream)
2526
{
2627
var reader = XmlReader.Create(xcApiStream);
2728
_xcApiDescription = new XCApiDescription(reader);
2829
_xc = "http://xcomponent.com/DeploymentConfig.xsd";
29-
ParseComponentNodes(_xcApiDescription.GetComponentsNode(), out _componentCodeByComponentNameRepo, out _stateMachineInfoByComponentRepository);
30-
_eventCodeByEvent = CreateEventCodeByEventRepository(_xcApiDescription.GetPublishersNode());
31-
_publisherTopicByIdentifier = CreatePublisherTopicByComponentAndStateMachineRepository(_xcApiDescription.GetPublishersNode());
32-
_subscriberTopicByIdentifier = CreateConsumerTopicByComponentAndStateMachineAndRepository(_xcApiDescription.GetConsumersNode());
33-
_snapshotTopicByComponent = CreateSnapshotTopicByComponentRepository(_xcApiDescription.GetSnapshotsNode());
30+
31+
ParseComponentNodes(_xcApiDescription.GetComponentsNode());
32+
_eventCodeByEventRepository = CreateEventCodeByEventRepository(_xcApiDescription.GetPublishersNode());
33+
_publisherTopicByIdentifierRepository =
34+
CreatePublisherTopicByComponentAndStateMachineRepository(_xcApiDescription.GetPublishersNode());
35+
_subscriberTopicByIdentifierRepository =
36+
CreateConsumerTopicByComponentAndStateMachineRepository(_xcApiDescription.GetConsumersNode());
37+
_snapshotTopicByComponentRepository = CreateSnapshotTopicByComponentRepository(_xcApiDescription.GetSnapshotsNode());
3438
}
3539

36-
private void ParseComponentNodes(IEnumerable<XElement> components,
37-
out Dictionary<string, int> componentCodeByComponentNameRepo,
38-
out Dictionary<string, List<StateMachineInfo>> stateMachineInfoByComponentRepository)
40+
private void ParseComponentNodes(IEnumerable<XElement> components)
3941
{
40-
componentCodeByComponentNameRepo = new Dictionary<string, int>();
41-
stateMachineInfoByComponentRepository = new Dictionary<string, List<StateMachineInfo>>();
42-
4342
foreach (XElement component in components)
4443
{
4544
var componentName = component.Attribute(XCApiTags.Name)?.Value;
46-
if (string.IsNullOrEmpty(componentName))
47-
continue;
48-
49-
componentCodeByComponentNameRepo.Add(componentName, Convert.ToInt32(component.Attribute(XCApiTags.Id)?.Value));
50-
51-
if (!_stateMachineInfoByComponentRepository.ContainsKey(componentName))
52-
{
53-
var stateMachines = component.Descendants(_xc + "stateMachine");
54-
var statemachineInfoList = new List<StateMachineInfo>(
55-
stateMachines.Select(stateMachine => new StateMachineInfo
56-
{
57-
StateMachineName = stateMachine?.Attribute(XCApiTags.Name)?.Value,
58-
StateMachineCode = Convert.ToInt32(stateMachine?.Attribute(XCApiTags.Id)?.Value)
59-
})
60-
.ToList()
61-
);
45+
var componentCode = component.Attribute(XCApiTags.Id)?.Value;
6246

63-
if (statemachineInfoList.Any())
64-
{
65-
_stateMachineInfoByComponentRepository.Add(componentName, statemachineInfoList);
66-
}
47+
if (!string.IsNullOrEmpty(componentName) && !string.IsNullOrEmpty(componentCode))
48+
{
49+
_componentCodeByComponentNameRepository.Add(componentName, Convert.ToInt32(componentCode));
50+
51+
ParseStateMachineNodes(component, componentName);
6752
}
68-
}
53+
}
54+
}
55+
56+
private void ParseStateMachineNodes(XElement component, string componentName)
57+
{
58+
if (_stateMachineInfoByComponentRepository.ContainsKey(componentName)) return;
59+
60+
var stateMachines = component.Descendants(_xc + XCApiTags.StateMachine);
61+
var statemachineInfoList = new List<StateMachineInfo>
62+
(
63+
stateMachines
64+
.Where(stateMachine =>
65+
!string.IsNullOrEmpty(stateMachine?.Attribute(XCApiTags.Name)?.Value)
66+
&& !string.IsNullOrEmpty(stateMachine.Attribute(XCApiTags.Id)?.Value))
67+
.Select(stateMachine =>
68+
new StateMachineInfo(stateMachine.Attribute(XCApiTags.Name)?.Value,
69+
Convert.ToInt32(stateMachine.Attribute(XCApiTags.Id)?.Value))
70+
).ToList()
71+
);
72+
73+
if (statemachineInfoList.Any())
74+
{
75+
_stateMachineInfoByComponentRepository.Add(componentName, statemachineInfoList);
76+
}
6977
}
7078

7179
private Dictionary<string, int> CreateEventCodeByEventRepository(IEnumerable<XElement> publishNodes)
@@ -81,14 +89,19 @@ private Dictionary<string, int> CreateEventCodeByEventRepository(IEnumerable<XEl
8189

8290
private void AddEventCodeToRepository(Dictionary<string, int> repository, XElement node)
8391
{
84-
if (node?.Attribute(XCApiTags.EventName)?.Value != null && !repository.ContainsKey(node.Attribute(XCApiTags.EventName).Value))
92+
var eventName = node?.Attribute(XCApiTags.EventName)?.Value;
93+
var eventCode = node?.Attribute(XCApiTags.EventCode)?.Value;
94+
95+
if (!string.IsNullOrEmpty(eventName)
96+
&& !repository.ContainsKey(eventName)
97+
&& !string.IsNullOrEmpty(eventCode))
8598
{
86-
repository.Add(node.Attribute(XCApiTags.EventName).Value,
87-
Convert.ToInt32(node.Attribute(XCApiTags.EventCode)?.Value));
99+
repository.Add(eventName, Convert.ToInt32(eventCode));
88100
}
89101
}
90102

91-
private Dictionary<TopicIdentifier, string> CreatePublisherTopicByComponentAndStateMachineRepository(IEnumerable<XElement> publishNodes)
103+
private Dictionary<TopicIdentifier, string> CreatePublisherTopicByComponentAndStateMachineRepository(
104+
IEnumerable<XElement> publishNodes)
92105
{
93106
Dictionary<TopicIdentifier, string> topicByIdentifierRepo = new Dictionary<TopicIdentifier, string>();
94107

@@ -99,7 +112,8 @@ private Dictionary<TopicIdentifier, string> CreatePublisherTopicByComponentAndSt
99112
return topicByIdentifierRepo;
100113
}
101114

102-
private Dictionary<TopicIdentifier, string> CreateConsumerTopicByComponentAndStateMachineAndRepository(IEnumerable<XElement> subscribeNodes)
115+
private Dictionary<TopicIdentifier, string> CreateConsumerTopicByComponentAndStateMachineRepository(
116+
IEnumerable<XElement> subscribeNodes)
103117
{
104118
Dictionary<TopicIdentifier, string> topicByIdentifierRepo = new Dictionary<TopicIdentifier, string>();
105119

@@ -115,15 +129,15 @@ private void AddTopicToRepository(Dictionary<TopicIdentifier, string> repository
115129
var componentCode = Convert.ToInt64(node?.Attribute(XCApiTags.ComponentCode)?.Value);
116130
var stateMachineCode = Convert.ToInt64(node?.Attribute(XCApiTags.StateMachineCode)?.Value);
117131
var topicType = node?.Attribute(XCApiTags.TopicType)?.Value;
118-
132+
var topicValue = node?.Descendants(_xc + XCApiTags.Topic).FirstOrDefault()?.Value;
119133
var topicIdentifier = new TopicIdentifier(componentCode, stateMachineCode, topicType);
120-
121-
if (!repository.ContainsKey(topicIdentifier))
134+
135+
if (!repository.ContainsKey(topicIdentifier) && !string.IsNullOrEmpty(topicValue))
122136
{
123-
repository.Add(topicIdentifier, node?.Descendants(_xc + XCApiTags.Topic).FirstOrDefault()?.Value);
137+
repository.Add(topicIdentifier, topicValue);
124138
}
125-
}
126-
139+
}
140+
127141
private Dictionary<long, string> CreateSnapshotTopicByComponentRepository(IEnumerable<XElement> snapshotNodes)
128142
{
129143
var snapshotTopicByComponentRepo = new Dictionary<long, string>();
@@ -137,10 +151,11 @@ private Dictionary<long, string> CreateSnapshotTopicByComponentRepository(IEnume
137151
private void AddSnapshotTopicToRepository(IDictionary<long, string> repository, XElement node)
138152
{
139153
var componentCode = Convert.ToInt64(node?.Attribute(XCApiTags.ComponentCode)?.Value);
140-
141-
if (!repository.ContainsKey(componentCode))
154+
var snapshotTopic = node?.Descendants(_xc + XCApiTags.Topic).FirstOrDefault()?.Value;
155+
156+
if (!repository.ContainsKey(componentCode) && !string.IsNullOrEmpty(snapshotTopic))
142157
{
143-
repository.Add(componentCode, node?.Descendants(_xc + XCApiTags.Topic).FirstOrDefault()?.Value);
158+
repository.Add(componentCode, snapshotTopic);
144159
}
145160
}
146161

@@ -161,18 +176,17 @@ public string GetConnectionType()
161176

162177
public string GetSerializationType()
163178
{
164-
var serialisation = _xcApiDescription.GetSerializationNode()?.FirstOrDefault()?.Value;
165-
return serialisation;
179+
return _xcApiDescription.GetSerializationNode()?.FirstOrDefault()?.Value;
166180
}
167181

168182
public BusDetails GetBusDetails()
169183
{
170184
XElement busInfos = _xcApiDescription.GetBusNode()?.FirstOrDefault();
171185
var busDetails = new BusDetails(
172-
busInfos?.Attribute("user")?.Value,
173-
busInfos?.Attribute("password")?.Value,
174-
busInfos?.Attribute("host")?.Value,
175-
Convert.ToInt32(busInfos?.Attribute("port")?.Value));
186+
busInfos?.Attribute("user")?.Value,
187+
busInfos?.Attribute("password")?.Value,
188+
busInfos?.Attribute("host")?.Value,
189+
Convert.ToInt32(busInfos?.Attribute("port")?.Value));
176190

177191
return busDetails;
178192
}
@@ -189,33 +203,34 @@ public WebSocketEndpoint GetWebSocketEndpoint()
189203
}
190204

191205
var webSocketEndpoint = new WebSocketEndpoint(
192-
websocketInfos?.Attribute("name")?.Value,
193-
websocketInfos?.Attribute("host")?.Value,
194-
websocketInfos?.Attribute("port")?.Value,
195-
webSocketType);
206+
websocketInfos?.Attribute("name")?.Value,
207+
websocketInfos?.Attribute("host")?.Value,
208+
websocketInfos?.Attribute("port")?.Value,
209+
webSocketType);
196210

197211
return webSocketEndpoint;
198212
}
199213

200214
public int GetComponentCode(string component)
201215
{
202216
int componentCode;
203-
_componentCodeByComponentNameRepo.TryGetValue(component, out componentCode);
217+
_componentCodeByComponentNameRepository.TryGetValue(component, out componentCode);
204218
return componentCode;
205219
}
206220

207221
public int GetStateMachineCode(string component, string stateMachine)
208222
{
209223
if (!_stateMachineInfoByComponentRepository.ContainsKey(component)) return 0;
210224

211-
var stateMachineInfo = _stateMachineInfoByComponentRepository[component]?.FirstOrDefault(stmInfo => stmInfo.StateMachineName.Equals(stateMachine));
225+
var stateMachineInfo = _stateMachineInfoByComponentRepository[component]
226+
?.FirstOrDefault(stmInfo => stmInfo.StateMachineName.Equals(stateMachine));
212227
return stateMachineInfo?.StateMachineCode ?? 0;
213228
}
214229

215230
public int GetPublisherEventCode(string eventName)
216231
{
217232
int eventCode;
218-
_eventCodeByEvent.TryGetValue(eventName, out eventCode);
233+
_eventCodeByEventRepository.TryGetValue(eventName, out eventCode);
219234
return eventCode;
220235
}
221236

@@ -226,17 +241,17 @@ public string GetPublisherTopic(string component, string stateMachine)
226241
var stateMachineCode = GetStateMachineCode(component, stateMachine);
227242
var topicId = new TopicIdentifier(componentCode, stateMachineCode, XCApiTags.Output);
228243

229-
_publisherTopicByIdentifier.TryGetValue(topicId, out publisherTopic);
244+
_publisherTopicByIdentifierRepository.TryGetValue(topicId, out publisherTopic);
230245
return publisherTopic;
231246
}
232247

233248
public string GetPublisherTopic(long componentCode, long stateMachineCode)
234249
{
235250
string publisherTopic;
236-
var topicId = new TopicIdentifier(componentCode, stateMachineCode, XCApiTags.Output);
237-
238-
_publisherTopicByIdentifier.TryGetValue(topicId, out publisherTopic);
239-
251+
var topicId = new TopicIdentifier(componentCode, stateMachineCode, XCApiTags.Output);
252+
253+
_publisherTopicByIdentifierRepository.TryGetValue(topicId, out publisherTopic);
254+
240255
return publisherTopic;
241256
}
242257

@@ -247,16 +262,16 @@ public string GetSubscriberTopic(string component, string stateMachine)
247262
var stateMachineCode = GetStateMachineCode(component, stateMachine);
248263
var topicId = new TopicIdentifier(componentCode, stateMachineCode, XCApiTags.Input);
249264

250-
_subscriberTopicByIdentifier.TryGetValue(topicId, out subscriberTopic);
265+
_subscriberTopicByIdentifierRepository.TryGetValue(topicId, out subscriberTopic);
251266
return subscriberTopic;
252267
}
253268

254269
public string GetSnapshotTopic(string component)
255270
{
256271
string snapshotTopic;
257272
var componentCode = GetComponentCode(component);
258-
_snapshotTopicByComponent.TryGetValue(componentCode, out snapshotTopic);
273+
_snapshotTopicByComponentRepository.TryGetValue(componentCode, out snapshotTopic);
259274
return snapshotTopic;
260275
}
261276
}
262-
}
277+
}

0 commit comments

Comments
 (0)