Skip to content

Commit 3b27e9f

Browse files
committed
ISSUE-74 : Fix when parsing an XCApi containing different components with the same stateMachine name
1 parent 8826a68 commit 3b27e9f

3 files changed

Lines changed: 60 additions & 47 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ReactiveXComponent.Parser
2+
{
3+
public class StateMachineInfo
4+
{
5+
public string StateMachineName { get; set; }
6+
public int StateMachineCode { get; set; }
7+
}
8+
}

ReactiveXComponent/Parser/XCApiConfigParser.cs

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,61 @@ namespace ReactiveXComponent.Parser
1111
{
1212
public class XCApiConfigParser : IXCApiConfigParser
1313
{
14-
private XCApiDescription _xcApiDescription;
15-
16-
private Dictionary<string, int> _componentCodeByComponent;
17-
private Dictionary<string, int> _stateMachineCodeByStateMachine;
14+
private XCApiDescription _xcApiDescription;
15+
private XNamespace _xc;
1816
private Dictionary<string, int> _eventCodeByEvent;
1917
private Dictionary<TopicIdentifier, string> _publisherTopicByIdentifier;
2018
private Dictionary<TopicIdentifier, string> _subscriberTopicByIdentifier;
2119
private Dictionary<long, string> _snapshotTopicByComponent;
20+
private Dictionary<string, List<StateMachineInfo>> _stateMachineInfoByComponentRepository;
21+
private Dictionary<string, int> _componentCodeByComponentNameRepo;
2222

23-
private XNamespace _xc;
2423

2524
public void Parse(Stream xcApiStream)
2625
{
2726
var reader = XmlReader.Create(xcApiStream);
2827
_xcApiDescription = new XCApiDescription(reader);
2928
_xc = "http://xcomponent.com/DeploymentConfig.xsd";
30-
31-
_componentCodeByComponent = CreateComponentCodeByNameRepository(_xcApiDescription.GetComponentsNode());
32-
_stateMachineCodeByStateMachine = CreateStateMachineCodeByNameRepository(_xcApiDescription.GetStateMachinesNode());
29+
ParseComponentNodes(_xcApiDescription.GetComponentsNode(), out _componentCodeByComponentNameRepo, out _stateMachineInfoByComponentRepository);
3330
_eventCodeByEvent = CreateEventCodeByEventRepository(_xcApiDescription.GetPublishersNode());
3431
_publisherTopicByIdentifier = CreatePublisherTopicByComponentAndStateMachineRepository(_xcApiDescription.GetPublishersNode());
3532
_subscriberTopicByIdentifier = CreateConsumerTopicByComponentAndStateMachineAndRepository(_xcApiDescription.GetConsumersNode());
36-
_snapshotTopicByComponent = CreateSnapshotTopicByComponentRepository(_xcApiDescription.GetSnapshotsNode()); ;
33+
_snapshotTopicByComponent = CreateSnapshotTopicByComponentRepository(_xcApiDescription.GetSnapshotsNode());
3734
}
3835

39-
private Dictionary<string, int> CreateComponentCodeByNameRepository(IEnumerable<XElement> components)
36+
private void ParseComponentNodes(IEnumerable<XElement> components,
37+
out Dictionary<string, int> componentCodeByComponentNameRepo,
38+
out Dictionary<string, List<StateMachineInfo>> stateMachineInfoByComponentRepository)
4039
{
41-
Dictionary<string, int> componentCodeByNameRepo = new Dictionary<string, int>();
40+
componentCodeByComponentNameRepo = new Dictionary<string, int>();
41+
stateMachineInfoByComponentRepository = new Dictionary<string, List<StateMachineInfo>>();
4242

4343
foreach (XElement component in components)
4444
{
45-
AddComponentToRepository(componentCodeByNameRepo, component);
46-
}
47-
return componentCodeByNameRepo;
48-
}
45+
var componentName = component.Attribute(XCApiTags.Name)?.Value;
46+
if (string.IsNullOrEmpty(componentName))
47+
continue;
4948

50-
private void AddComponentToRepository(Dictionary<string, int> repository, XElement component)
51-
{
52-
repository.Add(component.Attribute(XCApiTags.Name).Value, Convert.ToInt32(component.Attribute(XCApiTags.Id).Value));
53-
}
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+
);
5462

55-
private Dictionary<string, int> CreateStateMachineCodeByNameRepository(IEnumerable<XElement> stateMachines)
56-
{
57-
Dictionary<string, int> stateMachineCodeRepo = new Dictionary<string, int>();
58-
foreach (XElement stateMachine in stateMachines)
59-
{
60-
stateMachineCodeRepo.Add(stateMachine.Attribute(XCApiTags.Name).Value, Convert.ToInt32(stateMachine.Attribute(XCApiTags.Id).Value));
61-
}
62-
return stateMachineCodeRepo;
63+
if (statemachineInfoList.Any())
64+
{
65+
_stateMachineInfoByComponentRepository.Add(componentName, statemachineInfoList);
66+
}
67+
}
68+
}
6369
}
6470

6571
private Dictionary<string, int> CreateEventCodeByEventRepository(IEnumerable<XElement> publishNodes)
@@ -78,7 +84,7 @@ private void AddEventCodeToRepository(Dictionary<string, int> repository, XEleme
7884
if (node?.Attribute(XCApiTags.EventName)?.Value != null && !repository.ContainsKey(node.Attribute(XCApiTags.EventName).Value))
7985
{
8086
repository.Add(node.Attribute(XCApiTags.EventName).Value,
81-
Convert.ToInt32(node.Attribute(XCApiTags.EventCode).Value));
87+
Convert.ToInt32(node.Attribute(XCApiTags.EventCode)?.Value));
8288
}
8389
}
8490

@@ -114,43 +120,40 @@ private void AddTopicToRepository(Dictionary<TopicIdentifier, string> repository
114120

115121
if (!repository.ContainsKey(topicIdentifier))
116122
{
117-
repository.Add(topicIdentifier, node?.Descendants(_xc + XCApiTags.Topic).FirstOrDefault().Value);
123+
repository.Add(topicIdentifier, node?.Descendants(_xc + XCApiTags.Topic).FirstOrDefault()?.Value);
118124
}
119125
}
120126

121127
private Dictionary<long, string> CreateSnapshotTopicByComponentRepository(IEnumerable<XElement> snapshotNodes)
122128
{
123-
Dictionary<long, string> SnapshotTopicByComponentRepo = new Dictionary<long, string>();
124-
foreach (XElement node in snapshotNodes)
129+
var snapshotTopicByComponentRepo = new Dictionary<long, string>();
130+
foreach (var node in snapshotNodes)
125131
{
126-
AddSnapshotTopicToRepository(SnapshotTopicByComponentRepo, node);
132+
AddSnapshotTopicToRepository(snapshotTopicByComponentRepo, node);
127133
}
128-
return SnapshotTopicByComponentRepo;
134+
return snapshotTopicByComponentRepo;
129135
}
130136

131-
private void AddSnapshotTopicToRepository(Dictionary<long, string> repository, XElement node)
137+
private void AddSnapshotTopicToRepository(IDictionary<long, string> repository, XElement node)
132138
{
133139
var componentCode = Convert.ToInt64(node?.Attribute(XCApiTags.ComponentCode)?.Value);
134140

135141
if (!repository.ContainsKey(componentCode))
136142
{
137-
repository.Add(componentCode, node?.Descendants(_xc + XCApiTags.Topic).FirstOrDefault().Value);
143+
repository.Add(componentCode, node?.Descendants(_xc + XCApiTags.Topic).FirstOrDefault()?.Value);
138144
}
139145
}
140146

141147
public string GetConnectionType()
142148
{
143149
var commmunicationNode = _xcApiDescription.GetCommunicationNode()?.FirstOrDefault();
144150

145-
if (commmunicationNode != null)
146-
{
147-
var communicationChildElements = commmunicationNode.Elements().ToList();
151+
var communicationChildElements = commmunicationNode?.Elements().ToList();
148152

149-
if (communicationChildElements != null && communicationChildElements.Count == 1)
150-
{
151-
var connection = communicationChildElements.FirstOrDefault();
152-
return connection.Name.LocalName;
153-
}
153+
if (communicationChildElements != null && communicationChildElements.Count == 1)
154+
{
155+
var connection = communicationChildElements.FirstOrDefault();
156+
return connection?.Name.LocalName;
154157
}
155158

156159
return string.Empty;
@@ -197,15 +200,16 @@ public WebSocketEndpoint GetWebSocketEndpoint()
197200
public int GetComponentCode(string component)
198201
{
199202
int componentCode;
200-
_componentCodeByComponent.TryGetValue(component, out componentCode);
203+
_componentCodeByComponentNameRepo.TryGetValue(component, out componentCode);
201204
return componentCode;
202205
}
203206

204207
public int GetStateMachineCode(string component, string stateMachine)
205208
{
206-
int stateMachineCode = 0;
207-
_stateMachineCodeByStateMachine?.TryGetValue(stateMachine, out stateMachineCode);
208-
return stateMachineCode;
209+
if (!_stateMachineInfoByComponentRepository.ContainsKey(component)) return 0;
210+
211+
var stateMachineInfo = _stateMachineInfoByComponentRepository[component]?.FirstOrDefault(stmInfo => stmInfo.StateMachineName.Equals(stateMachine));
212+
return stateMachineInfo?.StateMachineCode ?? 0;
209213
}
210214

211215
public int GetPublisherEventCode(string eventName)

ReactiveXComponent/ReactiveXComponent.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="Connection\IXCSubscriber.cs" />
9999
<Compile Include="Connection\IXCConnection.cs" />
100100
<Compile Include="Parser\IXCApiConfigParser.cs" />
101+
<Compile Include="Parser\StateMachineInfo.cs" />
101102
<Compile Include="Parser\XCApiDescription.cs" />
102103
<Compile Include="Connection\IXCPublisher.cs" />
103104
<Compile Include="Connection\IXCSession.cs" />

0 commit comments

Comments
 (0)