feat: Add function defs to DataStream DSL
This commit is contained in:
1 parent
0550078de0
commit
03294ffe37
55 files changed
+3332
-259
No files matched your search
@@ -94,6 +94,47 @@ namespace BCG {
|
||||
return BlueDataUtility.get_row_datetime_value(r, col_no, err_value);
|
||||
}
|
||||
|
||||
public static bool? GetBool(this IBlueRow r, int colNo) =>
|
||||
GetBool(r[colNo]);
|
||||
|
||||
public static bool? GetBool(this IBlueRow r, string colName) =>
|
||||
GetBool(r[colName]);
|
||||
|
||||
public static bool? GetBool(object obj) {
|
||||
if (obj is bool b) {
|
||||
return b;
|
||||
}
|
||||
if (obj is string s) {
|
||||
if (bool.TryParse(s, out b)) {
|
||||
return b;
|
||||
}
|
||||
return s.ToLower() switch {
|
||||
"y" or "yes" or "on" or "1" => true,
|
||||
"n" or "no" or "off" or "0" => false,
|
||||
_ => throw new ArgumentException("", nameof(obj)),
|
||||
};
|
||||
}
|
||||
if (obj is int i32) {
|
||||
return i32 > 0;
|
||||
}
|
||||
if (obj is long i64) {
|
||||
return i64 > 0;
|
||||
}
|
||||
if (obj is uint u32) {
|
||||
return u32 > 0;
|
||||
}
|
||||
if (obj is ulong u64) {
|
||||
return u64 > 0;
|
||||
}
|
||||
if (obj is short i16) {
|
||||
return i16 > 0;
|
||||
}
|
||||
if (obj is ushort u16) {
|
||||
return u16 > 0;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static double? GetFloat64(this IBlueRow r, int colNo) =>
|
||||
GetFloat64(r[colNo]);
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class FunctionEvaluator {
|
||||
public static IFunctionEvaluator Create<T, TResult>(Func<T, TResult> evaluate) =>
|
||||
new Impl<T, TResult>(evaluate);
|
||||
|
||||
class Impl<T, TResult> : IFunctionEvaluator {
|
||||
readonly Func<T, TResult> evaluate;
|
||||
|
||||
public Impl(Func<T, TResult> evaluate) {
|
||||
this.evaluate = evaluate;
|
||||
}
|
||||
|
||||
public object Evaluate(string functionId, IEnumerable<(string Name, int? Index, object Value)> args) {
|
||||
foreach (var a in args) {
|
||||
if (a is T ta) {
|
||||
return evaluate(ta);
|
||||
}
|
||||
}
|
||||
throw new ArgumentException($"Arguments does not contain item of expected input type {typeof(T)}", nameof(args));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,16 @@ using BCG;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public interface IDataStreamFactory {
|
||||
public interface IDataStreamFactory : IFunctionEvaluator {
|
||||
IEnumerable<IDataStreamMetadata> GetSupportedStreams();
|
||||
|
||||
ViewStream? Create(string streamId, IEnumerable<(string? Name, int? Index, object? Value)> args);
|
||||
|
||||
bool CanCreate(string streamId) =>
|
||||
GetSupportedStreams().Any(s => s.IsMatch(streamId));
|
||||
|
||||
object? IFunctionEvaluator.Evaluate(string functionId, IEnumerable<(string? Name, int? Index, object? Value)> args) =>
|
||||
Create(functionId, args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#nullable enable
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public interface IFunctionEvaluator {
|
||||
bool CanCompile => false;
|
||||
object? Evaluate(string functionId, IEnumerable<(string? Name, int? Index, object? Value)> args);
|
||||
void Compile() =>
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public interface IExtensionFunctionEvaluator : IFunctionEvaluator {
|
||||
Type TargetType { get; }
|
||||
|
||||
public IEnumerable<string> SupportedFunctions { get; }
|
||||
|
||||
object? Evaluate(string functionId, object target, IEnumerable<(string? Name, int? Index, object? Value)> args);
|
||||
|
||||
object? IFunctionEvaluator.Evaluate(string functionId, IEnumerable<(string? Name, int? Index, object? Value)> args) {
|
||||
var en = args.GetEnumerator();
|
||||
if (!en.MoveNext()) {
|
||||
throw new ArgumentException("Expected at least 1 arg (first arg is the target obj)", nameof(args));
|
||||
}
|
||||
var targetObj = en.Current.Value;
|
||||
if (targetObj is null) {
|
||||
return null;
|
||||
}
|
||||
return Evaluate(functionId, targetObj, RemainingArgs());
|
||||
|
||||
IEnumerable<(string? Name, int? Index, object? Value)> RemainingArgs() {
|
||||
while (en.MoveNext()) {
|
||||
yield return en.Current;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface IExtensionFunctionEvaluator<T> : IExtensionFunctionEvaluator {
|
||||
Type IExtensionFunctionEvaluator.TargetType => typeof(T);
|
||||
|
||||
object? Evaluate(string functionId, T target, IEnumerable<(string? Name, int? Index, object? Value)> args);
|
||||
|
||||
object? IExtensionFunctionEvaluator.Evaluate(string functionId, object target, IEnumerable<(string? Name, int? Index, object? Value)> args) =>
|
||||
Evaluate(functionId, ExceptionAssert.Argument.Is<T>(target), args);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#nullable enable
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using BCG;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class SingleValueStream : ViewStream {
|
||||
internal readonly IBlueTableSchema Schema;
|
||||
|
||||
readonly Row row;
|
||||
|
||||
public SingleValueStream(object value)
|
||||
: this(value, value.GetType()) { }
|
||||
|
||||
public SingleValueStream(object? value, Type type) {
|
||||
Schema = new BlueTableSchema(
|
||||
new ColumnInfo[] { new("id", 0, typeof(int)), new("Value", 1, type) },
|
||||
"SingleValue");
|
||||
row = new(0, value, Schema);
|
||||
}
|
||||
|
||||
protected override void myOnFirstSubscriber() {
|
||||
var rows = new RowCollection(row);
|
||||
myFireEvent(new DataPacket(rows, rows, new BlueTable(Schema)));
|
||||
}
|
||||
|
||||
class Row : IBlueRow {
|
||||
internal readonly int Id;
|
||||
internal readonly object? Value;
|
||||
internal readonly IBlueTableSchema Schema;
|
||||
|
||||
public Row(int id, object? value, IBlueTableSchema schema) {
|
||||
Id = id;
|
||||
Value = value;
|
||||
Schema = schema;
|
||||
}
|
||||
|
||||
public object? this[int index] =>
|
||||
index switch {
|
||||
0 => Id,
|
||||
1 => Value,
|
||||
_ => throw new IndexOutOfRangeException(),
|
||||
};
|
||||
|
||||
public object? this[string column] =>
|
||||
column switch {
|
||||
"id" => Id,
|
||||
"Value" => Value,
|
||||
_ => throw new KeyNotFoundException(),
|
||||
};
|
||||
|
||||
IBlueTableSchema IBlueRow.schema => Schema;
|
||||
}
|
||||
|
||||
class RowCollection : IBlueRowCollection {
|
||||
readonly Row row;
|
||||
IBlueRow? blueRow;
|
||||
|
||||
public RowCollection(Row row) =>
|
||||
this.row = row;
|
||||
|
||||
public IBlueRow this[int id] =>
|
||||
id == row.Id ? blueRow ??= row : throw new KeyNotFoundException();
|
||||
|
||||
public int Count => 1;
|
||||
|
||||
IBlueTableSchema IBlueRowCollection.schema => row.Schema;
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
index == 0 ? row : throw new IndexOutOfRangeException();
|
||||
|
||||
public int IndexOf(IBlueRow item) =>
|
||||
item.schema.columnInfo.Select(c => item[c.colNo]).SequenceEqual(row.Schema.columnInfo.Select(c => row[c.colNo]))
|
||||
? 0
|
||||
: -1;
|
||||
|
||||
public bool containsID(int id) =>
|
||||
id == row.Id;
|
||||
|
||||
public IList<object?> GetColumn(int index) =>
|
||||
new[] { row[0] };
|
||||
|
||||
public IEnumerator<IBlueRow> GetEnumerator() {
|
||||
yield return row;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user