CoreExpression.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using Expressive;
  2. using Expressive.Expressions;
  3. using Expressive.Functions;
  4. using InABox.Clients;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. namespace InABox.Core
  12. {
  13. //using FncType = Func<IExpression[], IDictionary<string, object>, object?>;
  14. // public class CoreExpressionFunction
  15. // {
  16. // public string Group { get; set; }
  17. //
  18. // public string Name { get; set; }
  19. //
  20. // public string? Description { get; set; }
  21. //
  22. // public List<Parameter> Parameters { get; set; }
  23. //
  24. // public FncType Function { get; set; }
  25. //
  26. // public class Parameter
  27. // {
  28. // public string Name { get; set; }
  29. //
  30. // public string Type { get; set; }
  31. //
  32. // public Parameter(string name, string type)
  33. // {
  34. // Name = name;
  35. // Type = type;
  36. // }
  37. // }
  38. //
  39. // public CoreExpressionFunction(string group, string name, FncType function, List<Parameter> parameters, string? description = null)
  40. // {
  41. // Group = group;
  42. // Name = name;
  43. // Parameters = parameters;
  44. // Function = function;
  45. // Description = description;
  46. // }
  47. // }
  48. public interface IExpressionModel { }
  49. public interface IExpressionModel<TReturn> : IExpressionModel { }
  50. public abstract class CoreExpressionFunction : FunctionBase
  51. {
  52. public abstract string Group { get; }
  53. public abstract string Description { get; }
  54. public abstract string[] Parameters { get; }
  55. }
  56. internal class FormatFunction : CoreExpressionFunction
  57. {
  58. #region IFunction Members
  59. public override object Evaluate(IExpression[] parameters, Context context)
  60. {
  61. ValidateParameterCount(parameters,-1, 2);
  62. string fmt = parameters.First()?.Evaluate(Variables).ToString() ?? "";
  63. object[] objects = parameters.Skip(1).Select(x => x.Evaluate(Variables)).ToArray();
  64. return String.Format(fmt, objects);
  65. }
  66. public override string Group => "String";
  67. public override string Name => "Format";
  68. public override string Description => "Formats a list of objects using the specified format string";
  69. public override string[] Parameters => new[] { "string format", "object[] parameters" };
  70. #endregion
  71. }
  72. internal class Client_LoadDocumentFunction : CoreExpressionFunction
  73. {
  74. public override object? Evaluate(IExpression[] parameters, Context context)
  75. {
  76. ValidateParameterCount(parameters, 1, 1);
  77. var id = parameters[0].Evaluate(Variables);
  78. if (id is null)
  79. return null;
  80. if (!(id is Guid docID))
  81. return null;
  82. return new Client<Document>()
  83. .Query(
  84. new Filter<Document>(x => x.ID).IsEqualTo(docID),
  85. new Columns<Document>(x => x.Data))
  86. .Rows.FirstOrDefault()
  87. ?.Get<Document, byte[]>(x => x.Data);
  88. }
  89. public override string Group => "Other";
  90. public override string Name => "Client_LoadDocument";
  91. public override string Description => "Retrieves a database document with the specified id";
  92. public override string[] Parameters => new[] { "Guid id" };
  93. }
  94. public class CoreExpression
  95. {
  96. private Expression Expression;
  97. public bool IsValid {
  98. get
  99. {
  100. try
  101. {
  102. return Expression.ReferencedVariables != null;
  103. }
  104. catch (Exception e)
  105. {
  106. return false;
  107. }
  108. }
  109. }
  110. public IReadOnlyCollection<string> ReferencedVariables => Expression.ReferencedVariables;
  111. protected virtual Type? ReturnType { get; }
  112. public CoreExpression(string expressionString)
  113. {
  114. Expression = new Expression(expressionString, _context);
  115. if (!IsValid)
  116. {
  117. var expr = "\"" + expressionString + "\"";
  118. var tags = new Regex(@"\[(.*?)\]").Matches(expressionString);
  119. foreach (var tag in tags)
  120. expr = expr.Replace($"{tag}", $"\"+{tag}+\"");
  121. expr = expr.Replace("+\"\"", "");
  122. Expression = new Expression(expr);
  123. }
  124. // foreach (var function in Functions)
  125. // {
  126. // Expression.RegisterFunction(function.Name, function.Function);
  127. // }
  128. }
  129. public object? Evaluate(Dictionary<string, object?>? variables)
  130. {
  131. var result = Expression.Evaluate(variables);
  132. if(ReturnType != null)
  133. {
  134. return CoreUtils.ChangeType(result, ReturnType);
  135. }
  136. return result;
  137. }
  138. public static List<string> GetModelVariables(Type modelType)
  139. {
  140. return CoreUtils.PropertyList(modelType, x => true).Select(x => x.Name).ToList();
  141. }
  142. public static List<string> GetModelVariables<TModel>() where TModel : IExpressionModel
  143. => GetModelVariables(typeof(TModel));
  144. #region Static
  145. public static List<CoreExpressionFunction> Functions = new List<CoreExpressionFunction>();
  146. private static Context _context = new Context(ExpressiveOptions.None);
  147. static void RegisterFunction<T>() where T : CoreExpressionFunction, new ()
  148. {
  149. var function = new T();
  150. Functions.Add(function);
  151. _context.RegisterFunction(function);
  152. }
  153. static CoreExpression()
  154. {
  155. RegisterFunction<FormatFunction>();
  156. RegisterFunction<Client_LoadDocumentFunction>();
  157. // RegisterFunction("String", "Format", (p, v) =>
  158. // {
  159. // if (!(p[0].Evaluate(v) is string format)) throw new Exception("No format string given for Format()");
  160. // return string.Format(format, p.Skip(1).Select(x => x.Evaluate(v)).ToArray());
  161. // }, "string fmt", "...");
  162. //RegisterFunction("Other", "Client_LoadDocument", "Loads a byte array from the database Document table, with the ID {docID}.", Fnc_LoadDocument, "Guid docID");
  163. }
  164. // private static object? Fnc_LoadDocument(IExpression[] p, IDictionary<string, object> v)
  165. // {
  166. // var id = p[0].Evaluate(v);
  167. // if (id is null)
  168. // return null;
  169. // if (!(id is Guid docID))
  170. // return null;
  171. //
  172. // return new Client<Document>()
  173. // .Query(
  174. // new Filter<Document>(x => x.ID).IsEqualTo(docID),
  175. // new Columns<Document>(x => x.Data))
  176. // .Rows.FirstOrDefault()
  177. // ?.Get<Document, byte[]>(x => x.Data);
  178. // }
  179. // public static void RegisterFunction(string group, string name, FncType function, params string[] parameters)
  180. // => RegisterFunction(group, name, null, function, parameters);
  181. // public static void RegisterFunction(string group, string name, string? description, FncType function, params string[] parameters) =>
  182. // Functions.Add(new CoreExpressionFunction(group, name, function, parameters.Select(x =>
  183. // {
  184. // var parts = x.Split(' ');
  185. // if (parts.Length == 1)
  186. // return new CoreExpressionFunction.Parameter(parts[0], "");
  187. // return new CoreExpressionFunction.Parameter(parts[1], parts[0]);
  188. // }).ToList(), description));
  189. #endregion
  190. }
  191. public class CoreExpression<TModel, TReturn> : CoreExpression
  192. where TModel : IExpressionModel<TReturn>
  193. {
  194. protected override Type? ReturnType => typeof(TReturn);
  195. public CoreExpression(string expressionString): base(expressionString) { }
  196. [return: MaybeNull]
  197. public new TReturn Evaluate(Dictionary<string, object?>? variables)
  198. {
  199. var result = base.Evaluate(variables);
  200. if(result is TReturn ret)
  201. {
  202. return ret;
  203. }
  204. return default;
  205. }
  206. [return: MaybeNull]
  207. public TReturn Evaluate(TModel model)
  208. {
  209. var values = new Dictionary<string, object?>();
  210. foreach(var variable in ReferencedVariables)
  211. {
  212. values[variable] = CoreUtils.GetPropertyValue(model, variable);
  213. }
  214. var result = base.Evaluate(values);
  215. if(result is TReturn ret)
  216. {
  217. return ret;
  218. }
  219. return default;
  220. }
  221. }
  222. }