Skip to content

Commit

Permalink
Merge branch 'feature/native_functions_for_expressions_(#468)' into d…
Browse files Browse the repository at this point in the history
…evelop
  • Loading branch information
Cédric L. Charlier committed Apr 13, 2019
2 parents 41780d1 + ff7a29b commit 6fc9ce6
Show file tree
Hide file tree
Showing 38 changed files with 866 additions and 643 deletions.
41 changes: 35 additions & 6 deletions NBi.Core/Calculation/BasePredicateFilter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using NBi.Core.Calculation.Predicate;
using NBi.Core.Evaluate;
using NBi.Core.ResultSet;
using NBi.Core.Transformation;
using NBi.Core.Transformation.Transformer;
using System;
using System.Collections.Generic;
using System.Data;
Expand Down Expand Up @@ -106,17 +108,44 @@ protected object GetValueFromRow(DataRow row, IColumnIdentifier identifier)

protected object EvaluateExpression(IColumnExpression expression, DataRow row)
{
var exp = new NCalc.Expression(expression.Value);
var factory = new ColumnIdentifierFactory();
if (expression.Language == LanguageType.NCalc)
{
var exp = new NCalc.Expression(expression.Value);
var factory = new ColumnIdentifierFactory();

exp.EvaluateParameter += delegate (string name, NCalc.ParameterArgs args)
{
args.Result = GetValueFromRow(row, factory.Instantiate(name));
};

exp.EvaluateParameter += delegate (string name, NCalc.ParameterArgs args)
return exp.Evaluate();
}
else if (expression.Language == LanguageType.Native)
{
args.Result = GetValueFromRow(row, factory.Instantiate(name));
};
var parse = expression.Value.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
var variable = new ColumnIdentifierFactory().Instantiate(parse.ElementAt(0));
var value = GetValueFromRow(row, variable);

return exp.Evaluate();
foreach (var nativeFunction in parse.Skip(1))
{
var factory = new NativeTransformationFactory();
var transformer = factory.Instantiate(nativeFunction);
value = transformer.Evaluate(value);
}

return value;
}
else
throw new ArgumentOutOfRangeException($"The language {expression.Language} is not supported during the evaluation of an expression.");
}

public abstract string Describe();

private class TransformationInfo : ITransformationInfo
{
public ColumnType OriginalType { get; set; }
public LanguageType Language { get; set; }
public string Code { get; set; }
}
}
}
2 changes: 2 additions & 0 deletions NBi.Core/Evaluate/IColumnExpression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using NBi.Core.ResultSet;
using NBi.Core.Transformation;

namespace NBi.Core.Evaluate
{
Expand All @@ -9,6 +10,7 @@ public interface IColumnExpression
int Column { get; set; }
string Name { get; set; }
string Value { get; set; }
LanguageType Language { get; }
ColumnType Type { get; set; }
string Tolerance { get; set; }
}
Expand Down
30 changes: 9 additions & 21 deletions NBi.Core/NBi.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -676,29 +676,17 @@
<Compile Include="Transformation\Transformer\FormatTransformer.cs" />
<Compile Include="Transformation\Transformer\NativeTransformationFactory.cs" />
<Compile Include="Transformation\Transformer\NativeTransformer.cs" />
<Compile Include="Transformation\Transformer\Native\DateTimeToPointInTime.cs" />
<Compile Include="Transformation\Transformer\Native\NumericToTruncation.cs" />
<Compile Include="Transformation\Transformer\Native\NullToZero.cs" />
<Compile Include="Transformation\Transformer\Native\NullToDate.cs" />
<Compile Include="Transformation\Transformer\Native\TextToText.cs" />
<Compile Include="Transformation\Transformer\Native\DateTime\DateTimeTransformations.cs" />
<Compile Include="Transformation\Transformer\Native\Numeric\NumericTransformations.cs" />
<Compile Include="Transformation\Transformer\Native\Text\TextTransformations.cs" />
<Compile Include="Transformation\Transformer\Native\INativeTransformation.cs" />
<Compile Include="Transformation\Transformer\Native\DateToAge.cs" />
<Compile Include="Transformation\Transformer\Native\NotImplementedTransformationException.cs" />
<Compile Include="Transformation\Transformer\Native\NullToValue.cs" />
<Compile Include="Transformation\Transformer\Native\TextToLength.cs" />
<Compile Include="Transformation\Transformer\Native\HtmlToText.cs" />
<Compile Include="Transformation\Transformer\Native\TextToTokenCount.cs" />
<Compile Include="Transformation\Transformer\Native\TextToWithoutWhitespaces.cs" />
<Compile Include="Transformation\Transformer\Native\TextToWithoutDiacritics.cs" />
<Compile Include="Transformation\Transformer\Native\LocalToUtc.cs" />
<Compile Include="Transformation\Transformer\Native\DateTimeToDate.cs" />
<Compile Include="Transformation\Transformer\Native\UtcToLocal.cs" />
<Compile Include="Transformation\Transformer\Native\ValueToValue.cs" />
<Compile Include="Transformation\Transformer\Native\AnyToAny.cs" />
<Compile Include="Transformation\Transformer\Native\NullToEmpty.cs" />
<Compile Include="Transformation\Transformer\Native\EmptyToNull.cs" />
<Compile Include="Transformation\Transformer\Native\BlankToNull.cs" />
<Compile Include="Transformation\Transformer\Native\BlankToEmpty.cs" />
<Compile Include="Transformation\Transformer\Native\Special\NullToValue.cs" />
<Compile Include="Transformation\Transformer\Native\Text\TextToWithoutWhitespaces.cs" />
<Compile Include="Transformation\Transformer\Native\Text\TextToWithoutDiacritics.cs" />
<Compile Include="Transformation\Transformer\Native\DateTime\TimeZoneTransformations.cs" />
<Compile Include="Transformation\Transformer\Native\Special\ValueToValue.cs" />
<Compile Include="Transformation\Transformer\Native\Special\AnyToAny.cs" />
<Compile Include="Transformation\Transformer\NCalcTransformer.cs" />
<Compile Include="Transformation\Transformer\CSharpTransformer.cs" />
<Compile Include="Transformation\ITransformationInfo.cs" />
Expand Down
29 changes: 0 additions & 29 deletions NBi.Core/Transformation/Transformer/Native/BlankToEmpty.cs

This file was deleted.

27 changes: 0 additions & 27 deletions NBi.Core/Transformation/Transformer/Native/BlankToNull.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using NBi.Core.Scalar.Casting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBi.Core.Transformation.Transformer.Native
{
abstract class AbstractDateTimeTransformation : INativeTransformation
{

public object Evaluate(object value)
{
switch (value)
{
case null: return EvaluateNull();
case DBNull dbnull: return EvaluateNull();
case DateTime dt: return EvaluateDateTime(dt);
default: return EvaluateUncasted(value);
}
}

private object EvaluateUncasted(object value)
{
if (value as string == "null")
EvaluateNull();

var caster = new DateTimeCaster();
var dateTime = caster.Execute(value);
return EvaluateDateTime(dateTime);
}

protected virtual object EvaluateNull() => null;
protected abstract object EvaluateDateTime(DateTime value);
}

class DateTimeToDate : AbstractDateTimeTransformation
{
protected override object EvaluateDateTime(DateTime value) => value.Date;
}

class DateToAge : AbstractDateTimeTransformation
{
protected override object EvaluateNull() => 0;
protected override object EvaluateDateTime(DateTime value)
{
// Save today's date.
var today = DateTime.Today;
// Calculate the age.
var age = today.Year - value.Year;
// Go back to the year the person was born in case of a leap year
return value.AddYears(age) > today ? age-- : age;
}
}

class DateTimeToFirstOfMonth : AbstractDateTimeTransformation
{
protected override object EvaluateDateTime(DateTime value) => new DateTime(value.Year, value.Month, 1);
}

class DateTimeToFirstOfYear : AbstractDateTimeTransformation
{
protected override object EvaluateDateTime(DateTime value) => new DateTime(value.Year, 1, 1);
}

class DateTimeToLastOfMonth : AbstractDateTimeTransformation
{
protected override object EvaluateDateTime(DateTime value) => new DateTime(value.Year, value.Month, 1).AddMonths(1).AddDays(-1);
}

class DateTimeToLastOfYear : AbstractDateTimeTransformation
{
protected override object EvaluateDateTime(DateTime value) => new DateTime(value.Year, 12, 31);
}

class DateTimeToNextDay : AbstractDateTimeTransformation
{
protected override object EvaluateDateTime(DateTime value) => value.AddDays(1);
}

class DateTimeToNextMonth : AbstractDateTimeTransformation
{
protected override object EvaluateDateTime(DateTime value) => value.AddMonths(1);
}

class DateTimeToNextYear : AbstractDateTimeTransformation
{
protected override object EvaluateDateTime(DateTime value) => value.AddYears(1);
}

class DateTimeToPreviousDay : AbstractDateTimeTransformation
{
protected override object EvaluateDateTime(DateTime value) => value.AddDays(-1);
}

class DateTimeToPreviousMonth : AbstractDateTimeTransformation
{
protected override object EvaluateDateTime(DateTime value) => value.AddMonths(-1);
}

class DateTimeToPreviousYear : AbstractDateTimeTransformation
{
protected override object EvaluateDateTime(DateTime value) => value.AddYears(-1);
}

class DateTimeToClip : AbstractDateTimeTransformation
{
public DateTime Min { get; }
public DateTime Max { get; }

public DateTimeToClip(string min, string max)
{
var caster = new DateTimeCaster();
Min = caster.Execute(min);
Max = caster.Execute(max);
}

protected override object EvaluateDateTime(DateTime value) => (value < Min) ? Min : (value > Max) ? Max : value;
}

class DateTimeToSetTime : AbstractDateTimeTransformation
{
public TimeSpan Instant { get; }

public DateTimeToSetTime(string instant)
{
Instant = TimeSpan.Parse(instant);
}

protected override object EvaluateDateTime(DateTime value)
=> new DateTime(value.Year, value.Month, value.Day, Instant.Hours, Instant.Minutes, Instant.Seconds);
}

class NullToDate : AbstractDateTimeTransformation
{
public DateTime Default { get; }

public NullToDate(string dt)
{
var caster = new DateTimeCaster();
Default = caster.Execute(dt);
}

protected override object EvaluateNull() => Default;
protected override object EvaluateDateTime(DateTime value) => value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace NBi.Core.Transformation.Transformer.Native
{
class UtcToLocal : INativeTransformation
class UtcToLocal : AbstractDateTimeTransformation
{
public string TimeZoneLabel { get; }

Expand All @@ -15,17 +15,7 @@ public UtcToLocal(string timeZoneLabel)
TimeZoneLabel = timeZoneLabel;
}

public object Evaluate(object value)
{
if (value == null)
return null;
else if (value is DateTime)
return EvaluateDateTime((DateTime)value);
else
throw new NotImplementedException();
}

protected virtual object EvaluateDateTime(DateTime value) =>
protected override object EvaluateDateTime(DateTime value) =>
TimeZoneInfo.ConvertTimeFromUtc(value, InstantiateTimeZoneInfo(TimeZoneLabel));

protected TimeZoneInfo InstantiateTimeZoneInfo(string label)
Expand All @@ -43,7 +33,15 @@ private string[] Tokenize(string label) =>
.Replace(":", ",")
.Replace(" ", "")
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}

class LocalToUtc : UtcToLocal
{
public LocalToUtc(string timeZoneLabel)
: base(timeZoneLabel)
{ }


protected override object EvaluateDateTime(DateTime value) =>
TimeZoneInfo.ConvertTimeToUtc(value, InstantiateTimeZoneInfo(TimeZoneLabel));
}
}
24 changes: 0 additions & 24 deletions NBi.Core/Transformation/Transformer/Native/DateTimeToDate.cs

This file was deleted.

Loading

0 comments on commit 6fc9ce6

Please sign in to comment.