Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 73f9fecf authored by Markus Quaritsch's avatar Markus Quaritsch
Browse files

refactoring cycle tests, refactoring cycle reader

parent f9f7110e
No related branches found
No related tags found
No related merge requests found
......@@ -53,7 +53,7 @@ namespace TUGraz.VectoCore.InputData.Reader
/// <exception cref="VectoException">CycleFile Format is unknown.</exception>
public static CycleType DetectCycleType(DataTable cycleData)
{
var cols = cycleData.Columns.Cast<DataColumn>().Select(c => c.ColumnName).ToArray();
var cols = cycleData.Columns;
if (PTOCycleDataParser.ValidateHeader(cols, false)) {
return CycleType.PTO;
......@@ -336,10 +336,13 @@ namespace TUGraz.VectoCore.InputData.Reader
private abstract class AbstractCycleDataParser : LoggingObject, ICycleDataParser
{
protected static bool CheckColumns(string[] header, IEnumerable<string> allowedCols, IEnumerable<string> requiredCols,
protected static bool CheckColumns(DataColumnCollection header, IEnumerable<string> allowedCols,
IEnumerable<string> requiredCols,
bool throwExceptions, bool allowAux)
{
var diff = header.GroupBy(c => c).Where(g => g.Count() > 2).SelectMany(g => g).ToList();
var headerStr = header.Cast<DataColumn>().Select(col => col.ColumnName).ToArray();
var diff = headerStr.GroupBy(c => c).Where(g => g.Count() > 2).SelectMany(g => g).ToList();
if (diff.Any()) {
if (throwExceptions) {
throw new VectoException("Column(s) defined more than once: " + string.Join(", ", diff.OrderBy(x => x)));
......@@ -348,10 +351,10 @@ namespace TUGraz.VectoCore.InputData.Reader
}
if (allowAux) {
header = header.Where(c => !c.ToUpper().StartsWith(Constants.Auxiliaries.Prefix)).ToArray();
headerStr = headerStr.Where(c => !c.ToUpper().StartsWith(Constants.Auxiliaries.Prefix)).ToArray();
}
diff = header.Except(allowedCols).ToList();
diff = headerStr.Except(allowedCols.Select(x => x)).ToList();
if (diff.Any()) {
if (throwExceptions) {
throw new VectoException("Column(s) not allowed: " + string.Join(", ", diff));
......@@ -359,7 +362,7 @@ namespace TUGraz.VectoCore.InputData.Reader
return false;
}
diff = requiredCols.Except(header).ToList();
diff = requiredCols.Select(x => x).Except(headerStr).ToList();
if (diff.Any()) {
if (throwExceptions) {
throw new VectoException("Column(s) required: " + string.Join(", ", diff));
......@@ -369,9 +372,12 @@ namespace TUGraz.VectoCore.InputData.Reader
return true;
}
protected static bool CheckComboColumns(string[] header, string[] cols, bool throwExceptions)
protected static bool CheckComboColumns(DataColumnCollection header, string[] cols, bool throwExceptions)
{
var colCount = header.Intersect(cols).Count();
var colCount = header.Cast<DataColumn>()
.Select(col => col.ColumnName)
.Intersect(cols.Select(x => x)).Count();
if (colCount != 0 && colCount != cols.Length) {
if (throwExceptions) {
throw new VectoException("Either all columns have to be defined or none of them: {0}", string.Join(", ", cols));
......@@ -389,7 +395,7 @@ namespace TUGraz.VectoCore.InputData.Reader
{
public override IEnumerable<DrivingCycleData.DrivingCycleEntry> Parse(DataTable table, bool crossWindRequired)
{
ValidateHeader(table.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray());
ValidateHeader(table.Columns);
return table.Rows.Cast<DataRow>().Select(row => new DrivingCycleData.DrivingCycleEntry {
Distance = row.ParseDouble(Fields.Distance).SI<Meter>(),
......@@ -407,7 +413,7 @@ namespace TUGraz.VectoCore.InputData.Reader
});
}
public static bool ValidateHeader(string[] header, bool throwExceptions = true)
public static bool ValidateHeader(DataColumnCollection header, bool throwExceptions = true)
{
var requiredCols = new[] {
Fields.Distance,
......@@ -441,7 +447,7 @@ namespace TUGraz.VectoCore.InputData.Reader
{
public override IEnumerable<DrivingCycleData.DrivingCycleEntry> Parse(DataTable table, bool crossWindRequired)
{
ValidateHeader(table.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray());
ValidateHeader(table.Columns);
var absTime = 0;
foreach (DataRow row in table.Rows) {
......@@ -472,7 +478,7 @@ namespace TUGraz.VectoCore.InputData.Reader
}
}
public static bool ValidateHeader(string[] header, bool throwExceptions = true)
public static bool ValidateHeader(DataColumnCollection header, bool throwExceptions = true)
{
var requiredCols = new[] {
//Fields.Time not needed --> if missing 1 second resolution is assumed
......@@ -520,7 +526,7 @@ namespace TUGraz.VectoCore.InputData.Reader
{
public override IEnumerable<DrivingCycleData.DrivingCycleEntry> Parse(DataTable table, bool crossWindRequired)
{
ValidateHeader(table.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray());
ValidateHeader(table.Columns);
var entries = table.Rows.Cast<DataRow>().Select(row => new DrivingCycleData.DrivingCycleEntry {
Time = row.ParseDouble(Fields.Time).SI<Second>(),
......@@ -533,7 +539,7 @@ namespace TUGraz.VectoCore.InputData.Reader
return entries;
}
public static bool ValidateHeader(string[] header, bool throwExceptions = true)
public static bool ValidateHeader(DataColumnCollection header, bool throwExceptions = true)
{
var requiredCols = new[] {
Fields.Time,
......@@ -561,7 +567,7 @@ namespace TUGraz.VectoCore.InputData.Reader
{
public override IEnumerable<DrivingCycleData.DrivingCycleEntry> Parse(DataTable table, bool crossWindRequired)
{
ValidateHeader(table.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray());
ValidateHeader(table.Columns);
var entries = table.Rows.Cast<DataRow>().Select(row => new DrivingCycleData.DrivingCycleEntry {
Time = row.ParseDouble(Fields.Time).SI<Second>(),
......@@ -577,7 +583,7 @@ namespace TUGraz.VectoCore.InputData.Reader
return entries;
}
public static bool ValidateHeader(string[] header, bool throwExceptions = true)
public static bool ValidateHeader(DataColumnCollection header, bool throwExceptions = true)
{
var requiredCols = new[] {
Fields.Time,
......@@ -608,7 +614,7 @@ namespace TUGraz.VectoCore.InputData.Reader
{
public override IEnumerable<DrivingCycleData.DrivingCycleEntry> Parse(DataTable table, bool crossWindRequired)
{
ValidateHeader(table.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray());
ValidateHeader(table.Columns);
var entries = table.Rows.Cast<DataRow>().Select(row => new DrivingCycleData.DrivingCycleEntry {
Time = row.ParseDouble(Fields.Time).SI<Second>(),
......@@ -630,7 +636,7 @@ namespace TUGraz.VectoCore.InputData.Reader
return entries;
}
public static bool ValidateHeader(string[] header, bool throwExceptions = true)
public static bool ValidateHeader(DataColumnCollection header, bool throwExceptions = true)
{
var requiredCols = new[] {
Fields.Time,
......@@ -664,7 +670,7 @@ namespace TUGraz.VectoCore.InputData.Reader
{
public override IEnumerable<DrivingCycleData.DrivingCycleEntry> Parse(DataTable table, bool crossWindRequired)
{
ValidateHeader(table.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray());
ValidateHeader(table.Columns);
var entries = table.Rows.Cast<DataRow>().Select(row => new DrivingCycleData.DrivingCycleEntry {
Time = row.ParseDouble(Fields.Time).SI<Second>(),
......@@ -675,7 +681,7 @@ namespace TUGraz.VectoCore.InputData.Reader
return entries;
}
public static bool ValidateHeader(string[] header, bool throwExceptions = true)
public static bool ValidateHeader(DataColumnCollection header, bool throwExceptions = true)
{
var requiredCols = new[] {
Fields.Time,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment