Skip to content

Commit

Permalink
#70 Passing properties not definitions, also filling missing calls fo…
Browse files Browse the repository at this point in the history
…r environment updating with default values
  • Loading branch information
angalbiati committed Aug 30, 2024
1 parent d38137f commit 18311d4
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 3 deletions.
4 changes: 2 additions & 2 deletions DbcParserLib/Dbc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class Dbc
public IEnumerable<Node> Nodes {get;}
public IEnumerable<Message> Messages {get;}
public IEnumerable<EnvironmentVariable> EnvironmentVariables { get; }
public IEnumerable<CustomPropertyDefinition> GlobalProperties { get; }
public IEnumerable<CustomProperty> GlobalProperties { get; }

public Dbc(IEnumerable<Node> nodes, IEnumerable<Message> messages, IEnumerable<EnvironmentVariable> environmentVariables,
IEnumerable<CustomPropertyDefinition> globalProperties)
IEnumerable<CustomProperty> globalProperties)
{
Nodes = nodes;
Messages = messages;
Expand Down
51 changes: 50 additions & 1 deletion DbcParserLib/DbcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class DbcBuilder : IDbcBuilder
private readonly IDictionary<uint, Message> m_messages = new Dictionary<uint, Message>();
private readonly IDictionary<uint, IDictionary<string, Signal>> m_signals = new Dictionary<uint, IDictionary<string, Signal>>();
private readonly IDictionary<string, EnvironmentVariable> m_environmentVariables = new Dictionary<string, EnvironmentVariable>();
private readonly IDictionary<string, CustomProperty> m_globalCustomProperties = new Dictionary<string, CustomProperty>();

private readonly IDictionary<string, ValuesTable> m_namedTablesMap = new Dictionary<string, ValuesTable>();
private readonly IDictionary<CustomPropertyObjectType, IDictionary<string, CustomPropertyDefinition>> m_customProperties = new Dictionary<CustomPropertyObjectType, IDictionary<string, CustomPropertyDefinition>>() {
Expand Down Expand Up @@ -117,6 +118,23 @@ public void AddNodeCustomProperty(string propertyName, string nodeName, string v
m_observer.PropertyNameNotFound(propertyName);
}

public void AddGlobalCustomProperty(string propertyName, string value, bool isNumeric)
{
if(m_customProperties[CustomPropertyObjectType.Global].TryGetValue(propertyName, out var customPropertyDefinition))
{
var property = new CustomProperty(customPropertyDefinition);
if(!property.SetCustomPropertyValue(value, isNumeric))
return;

if(m_globalCustomProperties.TryGetValue(propertyName, out _))
m_observer.DuplicatedGlobalProperty(propertyName);
else
m_globalCustomProperties[propertyName] = property;
}
else
m_observer.PropertyNameNotFound(propertyName);
}

public void AddEnvironmentVariableCustomProperty(string propertyName, string variableName, string value, bool isNumeric)
{
if (m_customProperties[CustomPropertyObjectType.Environment].TryGetValue(propertyName, out var customProperty))
Expand Down Expand Up @@ -332,6 +350,35 @@ private void FillNodesNotSetCustomPropertyWithDefault()
}
}

private void FillEnvironmentVariablesNotSetCustomPropertyWithDefault()
{
var environmentCustomProperties = m_customProperties[CustomPropertyObjectType.Environment];
foreach (var customProperty in environmentCustomProperties)
{
foreach (var envVariable in m_environmentVariables.Values)
{
if (!envVariable.CustomProperties.TryGetValue(customProperty.Key, out _))
{
envVariable.CustomProperties[customProperty.Key] = new CustomProperty(customProperty.Value);
envVariable.CustomProperties[customProperty.Key].SetCustomPropertyValueFromDefault();
}
}
}
}

private void FillGlobalCustomPropertiesNotSetCustomPropertyWithDefault()
{
var globalCustomProperties = m_customProperties[CustomPropertyObjectType.Global];
foreach (var customPropertyPair in globalCustomProperties)
{
if (!m_globalCustomProperties.TryGetValue(customPropertyPair.Key, out _))
{
m_globalCustomProperties[customPropertyPair.Key] = new CustomProperty(customPropertyPair.Value);
m_globalCustomProperties[customPropertyPair.Key].SetCustomPropertyValueFromDefault();
}
}
}

private void FillMessagesNotSetCustomPropertyWithDefault()
{
var messageCustomProperties = m_customProperties[CustomPropertyObjectType.Message];
Expand Down Expand Up @@ -369,6 +416,8 @@ public Dbc Build()
{
FillNodesNotSetCustomPropertyWithDefault();
FillMessagesNotSetCustomPropertyWithDefault();
FillEnvironmentVariablesNotSetCustomPropertyWithDefault();
FillGlobalCustomPropertiesNotSetCustomPropertyWithDefault();

foreach (var message in m_messages)
{
Expand Down Expand Up @@ -399,7 +448,7 @@ public Dbc Build()
//}
//return new Dbc(nodes, messages, environmentVariables);

return new Dbc(m_nodes.ToArray(), m_messages.Values.ToArray(), m_environmentVariables.Values.ToArray(), m_customProperties[CustomPropertyObjectType.Global].Values);
return new Dbc(m_nodes.ToArray(), m_messages.Values.ToArray(), m_environmentVariables.Values.ToArray(), m_globalCustomProperties.Values);
}
}

Expand Down
1 change: 1 addition & 0 deletions DbcParserLib/IDbcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal interface IDbcBuilder
void AddCustomProperty(CustomPropertyObjectType objectType, CustomPropertyDefinition customProperty);
void AddCustomPropertyDefaultValue(string propertyName, string value, bool isNumeric);
void AddNodeCustomProperty(string propertyName, string nodeName, string value, bool isNumeric);
void AddGlobalCustomProperty(string propertyName, string value, bool isNumeric);
void AddEnvironmentVariableCustomProperty(string propertyName, string variableName, string value, bool isNumeric);
void AddMessageCustomProperty(string propertyName, uint messageId, string value, bool isNumeric);
void AddSignalCustomProperty(string propertyName, uint messageId, string signalName, string value, bool isNumeric);
Expand Down
1 change: 1 addition & 0 deletions DbcParserLib/Observers/IParseFailureObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IParseFailureObserver
void DuplicatedProperty(string propertyName);
void DuplicatedPropertyInNode(string propertyName, string nodeName);
void DuplicatedPropertyInEnvironmentVariable(string propertyName, string environmentVariableName);
void DuplicatedGlobalProperty(string propertyName);
void DuplicatedPropertyInMessage(string propertyName, uint messageId);
void DuplicatedPropertyInSignal(string propertyName, string signalName);
void DuplicatedEnvironmentVariableInNode(string environmentVariableName, string nodeName);
Expand Down
4 changes: 4 additions & 0 deletions DbcParserLib/Observers/SilentFailureObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public void DuplicatedPropertyInEnvironmentVariable(string propertyName, string
{
}

public void DuplicatedGlobalProperty(string propertyName)
{
}

public void DuplicatedPropertyInMessage(string propertyName, uint messageId)
{
}
Expand Down
5 changes: 5 additions & 0 deletions DbcParserLib/Observers/SimpleFailureObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public void DuplicatedPropertyInEnvironmentVariable(string propertyName, string
AddError($"Duplicated custom property '{propertyName}' in environment variable '{environmentVariableName}'");
}

public void DuplicatedGlobalProperty(string propertyName)
{
AddError($"Duplicated custom property '{propertyName}' among global properties");
}

public void DuplicatedPropertyInMessage(string propertyName, uint messageId)
{
AddError($"Duplicated custom property '{propertyName}' in message (ID {messageId})");
Expand Down
4 changes: 4 additions & 0 deletions DbcParserLib/Parsers/PropertiesLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
builder.AddMessageCustomProperty(match.Groups[1].Value, uint.Parse(match.Groups[5].Value, CultureInfo.InvariantCulture), stringValue, isNumeric);
else if (match.Groups[6].Value == "SG_")
builder.AddSignalCustomProperty(match.Groups[1].Value, uint.Parse(match.Groups[7].Value, CultureInfo.InvariantCulture), match.Groups[8].Value, stringValue, isNumeric);
else
{
builder.AddGlobalCustomProperty(match.Groups[1].Value, match.Groups[9].Value, isNumeric);
}
}
else
m_observer.PropertySyntaxError();
Expand Down

0 comments on commit 18311d4

Please sign in to comment.