Skip to content

Commit

Permalink
Merge pull request #54 from EFeru/revert-53-default_custom_property_r…
Browse files Browse the repository at this point in the history
…efactoring

Revert "Custom property parsing refactoring"
  • Loading branch information
Adhara3 authored Oct 19, 2023
2 parents 310dcf2 + 90b64b7 commit 49ac4c8
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 153 deletions.
29 changes: 2 additions & 27 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 MsgCycleTimePropertyIsParsedTest()
public void MsgCustomPropertyIsParsedTest()
{
var builder = new DbcBuilder(new SilentFailureObserver());
var message = new Message { ID = 2394947585 };
Expand All @@ -170,13 +170,11 @@ public void MsgCycleTimePropertyIsParsedTest()
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 SigInitialValueIntegerPropertyIsParsedTest()
public void SigCustomPropertyIsParsedTest()
{
var builder = new DbcBuilder(new SilentFailureObserver());
var message = new Message { ID = 2394947585 };
Expand All @@ -191,30 +189,7 @@ public void SigInitialValueIntegerPropertyIsParsedTest()
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: 22 additions & 12 deletions DbcParserLib/DbcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ public void AddNodeCustomProperty(string propertyName, string nodeName, string v
if (node != null)
{
var property = new CustomProperty(customProperty);
if(!property.SetCustomPropertyValue(value, isNumeric))
return;

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

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

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

property.SetCustomPropertyValue(value, isNumeric);
if(signal.CustomProperties.TryGetValue(propertyName, out _))
m_observer.DuplicatedPropertyInSignal(propertyName, signal.Name);
else
Expand All @@ -192,6 +184,14 @@ 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,6 +263,16 @@ 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: 7 additions & 42 deletions DbcParserLib/ExtensionsAndHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Collections.Generic;
using DbcParserLib.Model;
using System;

namespace DbcParserLib
{
Expand Down Expand Up @@ -32,6 +33,12 @@ 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 @@ -92,47 +99,5 @@ 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: 2 additions & 0 deletions DbcParserLib/IDbcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ 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: 6 additions & 7 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 bool SetCustomPropertyValue(string value, bool isNumeric)
public void SetCustomPropertyValue(string value, bool isNumeric)
{
switch (CustomPropertyDefinition.DataType)
{
case CustomPropertyDataType.Integer:
if(!CustomPropertyDefinition.TryGetIntegerValue(value, isNumeric, out var integerValue))
return false;
return;

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

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

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

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

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

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

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

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


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

namespace DbcParserLib.Model
{
Expand Down Expand Up @@ -38,16 +37,7 @@ public class Message
public ushort DLC;
public string Transmitter;
public string Comment;
[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 int CycleTime;
public List<Signal> Signals = new List<Signal>();
public IDictionary<string, CustomProperty> CustomProperties = new Dictionary<string, CustomProperty>();

Expand Down
20 changes: 7 additions & 13 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,23 +68,17 @@ public class Signal
public byte IsSigned { get; private set; } = 1;
public DbcValueType ValueType
{
get => m_valueType;
set
get
{
m_valueType = value;
IsSigned = (byte)(value == DbcValueType.Unsigned ? 0 : 1);
return m_ValueType;
}
}

public double InitialValue
{
get
set
{
this.InitialValue(out var initialValue);
return initialValue;
m_ValueType = value;
IsSigned = (byte)(value == DbcValueType.Unsigned ? 0 : 1);

Check warning on line 78 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 78 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 Factor = 1;
public bool IsInteger = false;
public double Offset;
Expand Down
8 changes: 8 additions & 0 deletions DbcParserLib/Parsers/PropertiesLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ 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 49ac4c8

Please sign in to comment.