Skip to content

Commit

Permalink
Merge pull request #53 from Whitehouse112/default_custom_property_ref…
Browse files Browse the repository at this point in the history
…actoring

Custom property parsing refactoring
  • Loading branch information
Adhara3 authored Oct 19, 2023
2 parents ff21747 + 828aeaf commit 310dcf2
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 57 deletions.
29 changes: 27 additions & 2 deletions DbcParserLib.Tests/PropertiesLineParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void EnumDefinitionCustomPropertyMoreWhiteSpaceIsParsedTest()
}

[Test]
public void MsgCustomPropertyIsParsedTest()
public void MsgCycleTimePropertyIsParsedTest()
{
var builder = new DbcBuilder(new SilentFailureObserver());
var message = new Message { ID = 2394947585 };
Expand All @@ -170,11 +170,13 @@ public void MsgCustomPropertyIsParsedTest()
Assert.IsTrue(ParseLine(@"BA_ ""GenMsgCycleTime"" BO_ 2394947585 100;", msgCycleTimeLineParser, builder, nextLineProvider));

var dbc = builder.Build();
Assert.AreEqual(true, dbc.Messages.First().CycleTime(out var cycleTime));
Assert.AreEqual(100, dbc.Messages.First().CycleTime);
Assert.AreEqual(100, cycleTime);
}

[Test]
public void SigCustomPropertyIsParsedTest()
public void SigInitialValueIntegerPropertyIsParsedTest()
{
var builder = new DbcBuilder(new SilentFailureObserver());
var message = new Message { ID = 2394947585 };
Expand All @@ -189,7 +191,30 @@ public void SigCustomPropertyIsParsedTest()
Assert.IsTrue(ParseLine(@"BA_ ""GenSigStartValue"" SG_ 2394947585 sig_name 40;", sigInitialValueLineParser, builder, nextLineProvider));

var dbc = builder.Build();
Assert.AreEqual(true, dbc.Messages.First().Signals.First().InitialValue(out var initialValue));
Assert.AreEqual(40, dbc.Messages.First().Signals.First().InitialValue);
Assert.AreEqual(40, initialValue);
}

[Test]
public void SigInitialValueHexPropertyIsParsedTest()
{
var builder = new DbcBuilder(new SilentFailureObserver());
var message = new Message { ID = 2394947585 };
builder.AddMessage(message);
var signal = new Signal { Name = "sig_name" };
builder.AddSignal(signal);

var sigInitialValueLineParser = CreateParser();
var nextLineProvider = new NextLineProvider(new StringReader(string.Empty));
Assert.IsTrue(ParseLine(@"BA_DEF_ SG_ ""GenSigStartValue"" HEX 0 200;", sigInitialValueLineParser, builder, nextLineProvider));
Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""GenSigStartValue"" 150;", sigInitialValueLineParser, builder, nextLineProvider));
Assert.IsTrue(ParseLine(@"BA_ ""GenSigStartValue"" SG_ 2394947585 sig_name 40;", sigInitialValueLineParser, builder, nextLineProvider));

var dbc = builder.Build();
Assert.AreEqual(true, dbc.Messages.First().Signals.First().InitialValue(out var initialValue));
Assert.AreEqual(40, dbc.Messages.First().Signals.First().InitialValue);
Assert.AreEqual(40, initialValue);
}

[Test]
Expand Down
34 changes: 12 additions & 22 deletions DbcParserLib/DbcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public void AddNodeCustomProperty(string propertyName, string nodeName, string v
if (node != null)
{
var property = new CustomProperty(customProperty);
property.SetCustomPropertyValue(value, isNumeric);
if(!property.SetCustomPropertyValue(value, isNumeric))
return;

if(node.CustomProperties.TryGetValue(propertyName, out _))
m_observer.DuplicatedPropertyInNode(propertyName, node.Name);
else
Expand All @@ -123,7 +125,9 @@ public void AddEnvironmentVariableCustomProperty(string propertyName, string var
if (m_environmentVariables.TryGetValue(variableName, out var envVariable))
{
var property = new CustomProperty(customProperty);
property.SetCustomPropertyValue(value, isNumeric);
if(!property.SetCustomPropertyValue(value, isNumeric))
return;

if(envVariable.CustomProperties.TryGetValue(propertyName, out _))
m_observer.DuplicatedPropertyInEnvironmentVariable(propertyName, envVariable.Name);
else
Expand All @@ -143,7 +147,9 @@ public void AddMessageCustomProperty(string propertyName, uint messageId, string
if (m_messages.TryGetValue(messageId, out var message))
{
var property = new CustomProperty(customProperty);
property.SetCustomPropertyValue(value, isNumeric);
if(!property.SetCustomPropertyValue(value, isNumeric))
return;

if(message.CustomProperties.TryGetValue(propertyName, out _))
m_observer.DuplicatedPropertyInMessage(propertyName, message.ID);
else
Expand All @@ -163,7 +169,9 @@ public void AddSignalCustomProperty(string propertyName, uint messageId, string
if (TryGetValueMessageSignal(messageId, signalName, out var signal))
{
var property = new CustomProperty(customProperty);
property.SetCustomPropertyValue(value, isNumeric);
if(!property.SetCustomPropertyValue(value, isNumeric))
return;

if(signal.CustomProperties.TryGetValue(propertyName, out _))
m_observer.DuplicatedPropertyInSignal(propertyName, signal.Name);
else
Expand All @@ -184,14 +192,6 @@ public void AddSignalComment(uint messageId, string signalName, string comment)
m_observer.SignalNameNotFound(messageId, signalName);
}

public void AddSignalInitialValue(uint messageId, string signalName, double initialValue)
{
if (TryGetValueMessageSignal(messageId, signalName, out var signal))
signal.InitialValue = initialValue * signal.Factor + signal.Offset;
else
m_observer.SignalNameNotFound(messageId, signalName);
}

public void AddSignalValueType(uint messageId, string signalName, DbcValueType valueType)
{
if (TryGetValueMessageSignal(messageId, signalName, out var signal))
Expand Down Expand Up @@ -263,16 +263,6 @@ public void AddNodeEnvironmentVariable(string nodeName, string variableName)
m_observer.NodeNameNotFound(nodeName);
}

public void AddMessageCycleTime(uint messageId, int cycleTime)
{
if (m_messages.TryGetValue(messageId, out var message))
{
message.CycleTime = cycleTime;
}
else
m_observer.MessageIdNotFound(messageId);
}

public void AddNamedValueTable(string name, IReadOnlyDictionary<int, string> dictValues, string stringValues)
{
if(m_namedTablesMap.TryGetValue(name, out _))
Expand Down
49 changes: 42 additions & 7 deletions DbcParserLib/ExtensionsAndHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using System.Collections.Generic;
using DbcParserLib.Model;
using System;

namespace DbcParserLib
{
Expand Down Expand Up @@ -33,12 +32,6 @@ internal static ulong BitMask(this Signal signal)
return (ulong.MaxValue >> (64 - signal.Length));
}

[Obsolete("Please use ValueTableMap instead. ToPairs() and ValueTable will be removed in future releases")]
public static IEnumerable<KeyValuePair<int, string>> ToPairs(this Signal signal)
{
return signal.ValueTableMap;
}

private const string MultiplexorLabel = "M";
private const string MultiplexedLabel = "m";

Expand Down Expand Up @@ -99,5 +92,47 @@ internal static IReadOnlyDictionary<int, string> ToDictionary(this string record
}
return dict;
}

public static bool CycleTime(this Message message, out int cycleTime)
{
cycleTime = 0;

if (message.CustomProperties.TryGetValue("GenMsgCycleTime", out var property))
{
cycleTime = property.IntegerCustomProperty.Value;
return true;
}
else
return false;
}

internal static bool InitialValue(this Signal signal, out double initialValue)
{
initialValue = 0;

if (signal.CustomProperties.TryGetValue("GenSigStartValue", out var property))
{
double value = 0;
switch (property.CustomPropertyDefinition.DataType)
{
case CustomPropertyDataType.Float:
value = property.FloatCustomProperty.Value;
break;
case CustomPropertyDataType.Hex:
value = property.HexCustomProperty.Value;
break;
case CustomPropertyDataType.Integer:
value = property.IntegerCustomProperty.Value;
break;
default:
return false;
}

initialValue = value * signal.Factor + signal.Offset;
return true;
}
else
return false;
}
}
}
2 changes: 0 additions & 2 deletions DbcParserLib/IDbcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ internal interface IDbcBuilder
{
void AddMessage(Message message);
void AddMessageComment(uint messageId, string comment);
void AddMessageCycleTime(uint messageId, int cycleTime);
void AddNamedValueTable(string name, IReadOnlyDictionary<int, string> dictValues, string stringValues);
void AddNode(Node node);
void AddNodeComment(string nodeName, string comment);
void AddSignal(Signal signal);
void AddSignalComment(uint messageId, string signalName, string comment);
void AddSignalInitialValue(uint messageId, string signalName, double initialValue);
void AddSignalValueType(uint messageId, string signalName, DbcValueType valueType);
void LinkNamedTableToSignal(uint messageId, string signalName, string tableName);
void LinkTableValuesToSignal(uint messageId, string signalName, IReadOnlyDictionary<int, string> dictValues, string stringValues);
Expand Down
13 changes: 7 additions & 6 deletions DbcParserLib/Model/CustomProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public CustomProperty(CustomPropertyDefinition customPropertyDefinition)
CustomPropertyDefinition = customPropertyDefinition;
}

public void SetCustomPropertyValue(string value, bool isNumeric)
public bool SetCustomPropertyValue(string value, bool isNumeric)
{
switch (CustomPropertyDefinition.DataType)
{
case CustomPropertyDataType.Integer:
if(!CustomPropertyDefinition.TryGetIntegerValue(value, isNumeric, out var integerValue))
return;
return false;

IntegerCustomProperty = new CustomPropertyValue<int>()
{
Expand All @@ -30,7 +30,7 @@ public void SetCustomPropertyValue(string value, bool isNumeric)

case CustomPropertyDataType.Hex:
if(!CustomPropertyDefinition.TryGetHexValue(value, isNumeric, out var hexValue))
return;
return false;

HexCustomProperty = new CustomPropertyValue<int>()
{
Expand All @@ -40,7 +40,7 @@ public void SetCustomPropertyValue(string value, bool isNumeric)

case CustomPropertyDataType.Float:
if(!CustomPropertyDefinition.TryGetFloatValue(value, isNumeric, out var floatValue))
return;
return false;
FloatCustomProperty = new CustomPropertyValue<double>()
{
Value = floatValue
Expand All @@ -49,7 +49,7 @@ public void SetCustomPropertyValue(string value, bool isNumeric)

case CustomPropertyDataType.String:
if(!CustomPropertyDefinition.IsString(isNumeric))
return;
return false;
StringCustomProperty = new CustomPropertyValue<string>()
{
Value = value
Expand All @@ -58,14 +58,15 @@ public void SetCustomPropertyValue(string value, bool isNumeric)

case CustomPropertyDataType.Enum:
if(!CustomPropertyDefinition.TryGetEnumValue(value, isNumeric, out var enumValue))
return;
return false;

EnumCustomProperty = new CustomPropertyValue<string>()
{
Value = enumValue
};
break;
}
return true;
}

public void SetCustomPropertyValueFromDefault()
Expand Down
1 change: 0 additions & 1 deletion DbcParserLib/Model/CustomPropertyDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public CustomPropertyDefinition(IParseFailureObserver observer)
m_observer = observer;
}


public void SetCustomPropertyDefaultValue(string value, bool isNumeric)
{
switch (DataType)
Expand Down
14 changes: 12 additions & 2 deletions DbcParserLib/Model/Message.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace DbcParserLib.Model
{
Expand Down Expand Up @@ -37,7 +38,16 @@ public class Message
public ushort DLC;
public string Transmitter;
public string Comment;
public int CycleTime;
[Obsolete("Please use CycleTime(out int cycleTime) instead. CycleTime property will be removed and will be accessible only through extension method")]
public int CycleTime
{
get
{
this.CycleTime(out var cycleTime);
return cycleTime;
}
}

public List<Signal> Signals = new List<Signal>();
public IDictionary<string, CustomProperty> CustomProperties = new Dictionary<string, CustomProperty>();

Expand Down
20 changes: 13 additions & 7 deletions DbcParserLib/Model/Signal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal ImmutableSignal(Signal signal)

public class Signal
{
private DbcValueType m_ValueType = DbcValueType.Signed;
private DbcValueType m_valueType = DbcValueType.Signed;

public uint ID;
public string Name;
Expand All @@ -68,17 +68,23 @@ public class Signal
public byte IsSigned { get; private set; } = 1;
public DbcValueType ValueType
{
get
{
return m_ValueType;
}
get => m_valueType;
set
{
m_ValueType = value;
m_valueType = value;
IsSigned = (byte)(value == DbcValueType.Unsigned ? 0 : 1);

Check warning on line 75 in DbcParserLib/Model/Signal.cs

View workflow job for this annotation

GitHub Actions / test

'Signal.IsSigned' is obsolete: 'Please use ValueType instead. IsSigned will be removed in future releases'

Check warning on line 75 in DbcParserLib/Model/Signal.cs

View workflow job for this annotation

GitHub Actions / test

'Signal.IsSigned' is obsolete: 'Please use ValueType instead. IsSigned will be removed in future releases'

Check warning on line 75 in DbcParserLib/Model/Signal.cs

View workflow job for this annotation

GitHub Actions / test

'Signal.IsSigned' is obsolete: 'Please use ValueType instead. IsSigned will be removed in future releases'

Check warning on line 75 in DbcParserLib/Model/Signal.cs

View workflow job for this annotation

GitHub Actions / test

'Signal.IsSigned' is obsolete: 'Please use ValueType instead. IsSigned will be removed in future releases'
}
}
public double InitialValue;

public double InitialValue
{
get
{
this.InitialValue(out var initialValue);
return initialValue;
}
}

public double Factor = 1;
public bool IsInteger = false;
public double Offset;
Expand Down
8 changes: 0 additions & 8 deletions DbcParserLib/Parsers/PropertiesLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,9 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
else if (match.Groups[2].Value == "EV_")
builder.AddEnvironmentVariableCustomProperty(match.Groups[1].Value, match.Groups[3].Value, stringValue, isNumeric);
else if (match.Groups[4].Value == "BO_")
{
builder.AddMessageCustomProperty(match.Groups[1].Value, uint.Parse(match.Groups[5].Value, CultureInfo.InvariantCulture), stringValue, isNumeric);
if (match.Groups[1].Value == "GenMsgCycleTime")
builder.AddMessageCycleTime(uint.Parse(match.Groups[5].Value, CultureInfo.InvariantCulture), int.Parse(match.Groups[9].Value, CultureInfo.InvariantCulture));
}
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);
if (match.Groups[1].Value == "GenSigStartValue")
builder.AddSignalInitialValue(uint.Parse(match.Groups[7].Value, CultureInfo.InvariantCulture), match.Groups[8].Value, double.Parse(match.Groups[9].Value, CultureInfo.InvariantCulture));
}
}
else
m_observer.PropertySyntaxError();
Expand Down
Loading

0 comments on commit 310dcf2

Please sign in to comment.