CoreExpression.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. using Expressive.Exceptions;
  12. namespace InABox.Core
  13. {
  14. public interface IExpressionModel { }
  15. public interface IExpressionModel<TReturn> : IExpressionModel { }
  16. public abstract class CoreExpressionFunction : FunctionBase
  17. {
  18. public abstract string Group { get; }
  19. public abstract string Description { get; }
  20. public abstract string[] Parameters { get; }
  21. }
  22. internal class FormatFunction : CoreExpressionFunction
  23. {
  24. #region IFunction Members
  25. public override object Evaluate(IExpression[] parameters, Context context)
  26. {
  27. ValidateParameterCount(parameters,-1, 2);
  28. string fmt = parameters.First()?.Evaluate(Variables).ToString() ?? "";
  29. object[] objects = parameters.Skip(1).Select(x => x.Evaluate(Variables)).ToArray();
  30. return String.Format(fmt, objects);
  31. }
  32. public override string Group => "String";
  33. public override string Name => "Format";
  34. public override string Description => "Formats a list of objects using the specified format string";
  35. public override string[] Parameters => new[] { "string format", "object[] parameters" };
  36. #endregion
  37. }
  38. internal class Client_LoadDocumentFunction : CoreExpressionFunction
  39. {
  40. public override object? Evaluate(IExpression[] parameters, Context context)
  41. {
  42. ValidateParameterCount(parameters, 1, 1);
  43. var id = parameters[0].Evaluate(Variables);
  44. if (id is null)
  45. return null;
  46. if (!(id is Guid docID))
  47. return null;
  48. return new Client<Document>()
  49. .Query(
  50. new Filter<Document>(x => x.ID).IsEqualTo(docID),
  51. Columns.None<Document>().Add(x => x.Data))
  52. .Rows.FirstOrDefault()
  53. ?.Get<Document, byte[]>(x => x.Data);
  54. }
  55. public override string Group => "Other";
  56. public override string Name => "Client_LoadDocument";
  57. public override string Description => "Retrieves a database document with the specified id";
  58. public override string[] Parameters => new[] { "Guid id" };
  59. }
  60. public class CoreExpression
  61. {
  62. private Expression Expression;
  63. public bool IsValid {
  64. get
  65. {
  66. try
  67. {
  68. return Expression.ReferencedVariables != null;
  69. }
  70. catch
  71. {
  72. return false;
  73. }
  74. }
  75. }
  76. public IReadOnlyCollection<string> ReferencedVariables => Expression.ReferencedVariables;
  77. protected virtual Type? ReturnType { get; }
  78. public CoreExpression(string expressionString)
  79. {
  80. Expression = new Expression(expressionString, _context);
  81. if (!IsValid)
  82. {
  83. var expr = "\"" + expressionString + "\"";
  84. var tags = new Regex(@"\[(.*?)\]").Matches(expressionString);
  85. foreach (var tag in tags)
  86. expr = expr.Replace($"{tag}", $"\"+{tag}+\"");
  87. expr = expr.Replace("+\"\"", "");
  88. Expression = new Expression(expr);
  89. }
  90. }
  91. public object? Evaluate(Dictionary<string, object?>? variables)
  92. {
  93. var result = Expression.Evaluate(variables);
  94. if(ReturnType != null)
  95. {
  96. return CoreUtils.ChangeType(result, ReturnType);
  97. }
  98. return result;
  99. }
  100. public static List<string> GetModelVariables(Type modelType)
  101. {
  102. var props = DatabaseSchema.Properties(modelType).Select(x => x.Name).ToList();
  103. props.Sort();
  104. return props;
  105. }
  106. public static List<string> GetModelVariables<TModel>() where TModel : IExpressionModel
  107. => GetModelVariables(typeof(TModel));
  108. #region Static
  109. public static List<CoreExpressionFunction> Functions = new List<CoreExpressionFunction>();
  110. private static Context _context = new Context(ExpressiveOptions.None);
  111. static void RegisterFunction<T>() where T : CoreExpressionFunction, new ()
  112. {
  113. var function = new T();
  114. Functions.Add(function);
  115. _context.RegisterFunction(function);
  116. }
  117. static CoreExpression()
  118. {
  119. RegisterFunction<FormatFunction>();
  120. RegisterFunction<Client_LoadDocumentFunction>();
  121. }
  122. #endregion
  123. }
  124. public class CoreExpression<TModel, TReturn> : CoreExpression
  125. where TModel : IExpressionModel<TReturn>
  126. {
  127. protected override Type? ReturnType => typeof(TReturn);
  128. public CoreExpression(string expressionString): base(expressionString) { }
  129. [return: MaybeNull]
  130. public new TReturn Evaluate(Dictionary<string, object?>? variables)
  131. {
  132. var result = base.Evaluate(variables);
  133. if(result is TReturn ret)
  134. {
  135. return ret;
  136. }
  137. return default;
  138. }
  139. public Result<TReturn, Exception> Evaluate(TModel model)
  140. {
  141. var values = new Dictionary<string, object?>();
  142. foreach(var variable in ReferencedVariables)
  143. {
  144. values[variable] = CoreUtils.GetPropertyValue(model, variable);
  145. }
  146. try
  147. {
  148. var result = base.Evaluate(values);
  149. if(result is TReturn ret)
  150. {
  151. return Result.Ok(ret);
  152. }
  153. return Result.Ok<TReturn>(default);
  154. }
  155. catch (Exception e)
  156. {
  157. return Result.Error(e);
  158. }
  159. }
  160. }
  161. }