| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- using Expressive;
- using Expressive.Expressions;
- using Expressive.Functions;
- using InABox.Clients;
- using System.Diagnostics.CodeAnalysis;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace InABox.Core
- {
- //using FncType = Func<IExpression[], IDictionary<string, object>, object?>;
- // public class CoreExpressionFunction
- // {
- // public string Group { get; set; }
- //
- // public string Name { get; set; }
- //
- // public string? Description { get; set; }
- //
- // public List<Parameter> Parameters { get; set; }
- //
- // public FncType Function { get; set; }
- //
- // public class Parameter
- // {
- // public string Name { get; set; }
- //
- // public string Type { get; set; }
- //
- // public Parameter(string name, string type)
- // {
- // Name = name;
- // Type = type;
- // }
- // }
- //
- // public CoreExpressionFunction(string group, string name, FncType function, List<Parameter> parameters, string? description = null)
- // {
- // Group = group;
- // Name = name;
- // Parameters = parameters;
- // Function = function;
- // Description = description;
- // }
- // }
- public interface IExpressionModel { }
- public interface IExpressionModel<TReturn> : IExpressionModel { }
- public abstract class CoreExpressionFunction : FunctionBase
- {
- public abstract string Group { get; }
- public abstract string Description { get; }
- public abstract string[] Parameters { get; }
- }
-
- internal class FormatFunction : CoreExpressionFunction
- {
- #region IFunction Members
- public override object Evaluate(IExpression[] parameters, Context context)
- {
- ValidateParameterCount(parameters,-1, 2);
- string fmt = parameters.First()?.Evaluate(Variables).ToString() ?? "";
- object[] objects = parameters.Skip(1).Select(x => x.Evaluate(Variables)).ToArray();
- return String.Format(fmt, objects);
- }
- public override string Group => "String";
- public override string Name => "Format";
- public override string Description => "Formats a list of objects using the specified format string";
- public override string[] Parameters => new[] { "string format", "object[] parameters" };
- #endregion
- }
- internal class Client_LoadDocumentFunction : CoreExpressionFunction
- {
- public override object? Evaluate(IExpression[] parameters, Context context)
- {
- ValidateParameterCount(parameters, 1, 1);
-
- var id = parameters[0].Evaluate(Variables);
-
- if (id is null)
- return null;
-
- if (!(id is Guid docID))
- return null;
- return new Client<Document>()
- .Query(
- new Filter<Document>(x => x.ID).IsEqualTo(docID),
- new Columns<Document>(x => x.Data))
- .Rows.FirstOrDefault()
- ?.Get<Document, byte[]>(x => x.Data);
- }
- public override string Group => "Other";
- public override string Name => "Client_LoadDocument";
- public override string Description => "Retrieves a database document with the specified id";
- public override string[] Parameters => new[] { "Guid id" };
- }
-
- public class CoreExpression
- {
- private Expression Expression;
-
- public bool IsValid {
- get
- {
- try
- {
- return Expression.ReferencedVariables != null;
- }
- catch (Exception e)
- {
- return false;
- }
- }
- }
- public IReadOnlyCollection<string> ReferencedVariables => Expression.ReferencedVariables;
- protected virtual Type? ReturnType { get; }
- public CoreExpression(string expressionString)
- {
- Expression = new Expression(expressionString, _context);
- if (!IsValid)
- {
- var expr = "\"" + expressionString + "\"";
- var tags = new Regex(@"\[(.*?)\]").Matches(expressionString);
- foreach (var tag in tags)
- expr = expr.Replace($"{tag}", $"\"+{tag}+\"");
- expr = expr.Replace("+\"\"", "");
- Expression = new Expression(expr);
- }
-
- // foreach (var function in Functions)
- // {
- // Expression.RegisterFunction(function.Name, function.Function);
- // }
- }
- public object? Evaluate(Dictionary<string, object?>? variables)
- {
- var result = Expression.Evaluate(variables);
- if(ReturnType != null)
- {
- return CoreUtils.ChangeType(result, ReturnType);
- }
- return result;
- }
- public static List<string> GetModelVariables(Type modelType)
- {
- return CoreUtils.PropertyList(modelType, x => true).Select(x => x.Name).ToList();
- }
- public static List<string> GetModelVariables<TModel>() where TModel : IExpressionModel
- => GetModelVariables(typeof(TModel));
- #region Static
- public static List<CoreExpressionFunction> Functions = new List<CoreExpressionFunction>();
-
- private static Context _context = new Context(ExpressiveOptions.None);
-
- static void RegisterFunction<T>() where T : CoreExpressionFunction, new ()
- {
- var function = new T();
- Functions.Add(function);
- _context.RegisterFunction(function);
- }
-
- static CoreExpression()
- {
-
- RegisterFunction<FormatFunction>();
- RegisterFunction<Client_LoadDocumentFunction>();
-
- // RegisterFunction("String", "Format", (p, v) =>
- // {
- // if (!(p[0].Evaluate(v) is string format)) throw new Exception("No format string given for Format()");
- // return string.Format(format, p.Skip(1).Select(x => x.Evaluate(v)).ToArray());
- // }, "string fmt", "...");
- //RegisterFunction("Other", "Client_LoadDocument", "Loads a byte array from the database Document table, with the ID {docID}.", Fnc_LoadDocument, "Guid docID");
- }
- // private static object? Fnc_LoadDocument(IExpression[] p, IDictionary<string, object> v)
- // {
- // var id = p[0].Evaluate(v);
- // if (id is null)
- // return null;
- // if (!(id is Guid docID))
- // return null;
- //
- // return new Client<Document>()
- // .Query(
- // new Filter<Document>(x => x.ID).IsEqualTo(docID),
- // new Columns<Document>(x => x.Data))
- // .Rows.FirstOrDefault()
- // ?.Get<Document, byte[]>(x => x.Data);
- // }
- // public static void RegisterFunction(string group, string name, FncType function, params string[] parameters)
- // => RegisterFunction(group, name, null, function, parameters);
- // public static void RegisterFunction(string group, string name, string? description, FncType function, params string[] parameters) =>
- // Functions.Add(new CoreExpressionFunction(group, name, function, parameters.Select(x =>
- // {
- // var parts = x.Split(' ');
- // if (parts.Length == 1)
- // return new CoreExpressionFunction.Parameter(parts[0], "");
- // return new CoreExpressionFunction.Parameter(parts[1], parts[0]);
- // }).ToList(), description));
- #endregion
- }
- public class CoreExpression<TModel, TReturn> : CoreExpression
- where TModel : IExpressionModel<TReturn>
- {
- protected override Type? ReturnType => typeof(TReturn);
- public CoreExpression(string expressionString): base(expressionString) { }
- [return: MaybeNull]
- public new TReturn Evaluate(Dictionary<string, object?>? variables)
- {
- var result = base.Evaluate(variables);
- if(result is TReturn ret)
- {
- return ret;
- }
- return default;
- }
- [return: MaybeNull]
- public TReturn Evaluate(TModel model)
- {
- var values = new Dictionary<string, object?>();
- foreach(var variable in ReferencedVariables)
- {
- values[variable] = CoreUtils.GetPropertyValue(model, variable);
- }
- var result = base.Evaluate(values);
- if(result is TReturn ret)
- {
- return ret;
- }
- return default;
- }
- }
- }
|