Event.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using Comal.Classes;
  2. using Expressive;
  3. using InABox.Core;
  4. using InABox.Database;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace PRS.Shared.Events;
  12. public class EventData<T, TDataModel>
  13. where T : IEvent<TDataModel>
  14. where TDataModel : IEventDataModel
  15. {
  16. public T Event { get; set; }
  17. /// <summary>
  18. /// A list of triggers for this event. If any of the triggers match, the event runs.
  19. /// </summary>
  20. public List<ITrigger<T, TDataModel>> Triggers { get; set; }
  21. public List<IEventAction<T>> Actions { get; set; }
  22. public EventData(T eventData)
  23. {
  24. Event = eventData;
  25. Triggers = new List<ITrigger<T, TDataModel>>();
  26. Actions = new List<IEventAction<T>>();
  27. }
  28. public Notification GenerateNotification(TDataModel model) => Event.GenerateNotification(model);
  29. }
  30. public static class EventUtils
  31. {
  32. private static bool Check<T, TDataModel>(EventData<T, TDataModel> ev, TDataModel dataModel)
  33. where T : IEvent<TDataModel>
  34. where TDataModel : IEventDataModel
  35. {
  36. return ev.Triggers.Any(x => x.Check(dataModel));
  37. }
  38. public static void Run<T, TDataModel>(IStore store, Event ev, EventData<T, TDataModel> evData, TDataModel dataModel)
  39. where T : IEvent<TDataModel>
  40. where TDataModel : IEventDataModel
  41. {
  42. if (!Check(evData, dataModel)) return;
  43. foreach(var action in evData.Actions)
  44. {
  45. IEvent.RunAction(evData.Event, dataModel, action);
  46. }
  47. NotifySubscribers(store, ev, evData, dataModel);
  48. }
  49. private static void NotifySubscribers<T, TDataModel>(IStore store, Event ev, EventData<T, TDataModel> evData, TDataModel dataModel)
  50. where T : IEvent<TDataModel>
  51. where TDataModel : IEventDataModel
  52. {
  53. string? description;
  54. if (ev.NotificationExpression.IsNullOrWhiteSpace())
  55. {
  56. description = null;
  57. }
  58. else
  59. {
  60. var descriptionExpr = new CoreExpression(ev.NotificationExpression, typeof(string));
  61. if(!descriptionExpr.TryEvaluate<string>(dataModel).Get(out description, out var error))
  62. {
  63. CoreUtils.LogException(store.UserID, error, extra: "Error notifying subscribers", store.Logger);
  64. return;
  65. }
  66. }
  67. var subscribers = store.Provider.Query(
  68. new Filter<EventSubscriber>(x => x.Event.ID).IsEqualTo(ev.ID),
  69. Columns.None<EventSubscriber>().Add(x => x.Employee.ID))
  70. .ToArray<EventSubscriber>();
  71. var notifications = new List<Notification>();
  72. foreach(var subscriber in subscribers)
  73. {
  74. var notification = evData.GenerateNotification(dataModel);
  75. notification.Employee.CopyFrom(subscriber.Employee);
  76. if(description is not null)
  77. {
  78. notification.Description = description;
  79. }
  80. notifications.Add(notification);
  81. }
  82. store.Provider.Save(notifications);
  83. }
  84. }
  85. #region DataModel Definition
  86. public interface IEventVariable
  87. {
  88. string Name { get; set; }
  89. Type Type { get; set; }
  90. }
  91. public class StandardEventVariable : IEventVariable
  92. {
  93. public string Name { get; set; }
  94. public Type Type { get; set; }
  95. public StandardEventVariable(string name, Type type)
  96. {
  97. Name = name;
  98. Type = type;
  99. }
  100. }
  101. public class ListEventVariable : IEventVariable
  102. {
  103. public string Name { get; set; }
  104. public Type Type { get; set; }
  105. public ListEventVariable(string name, Type type)
  106. {
  107. Name = name;
  108. Type = type;
  109. }
  110. }
  111. public interface IEventDataModelDefinition
  112. {
  113. IEnumerable<IEventVariable> GetVariables();
  114. IEventVariable? GetVariable(string name);
  115. }
  116. #endregion
  117. #region DataModel
  118. public interface IEventDataModel : IVariableProvider
  119. {
  120. bool TryGetVariable(string name, out object? value);
  121. bool IVariableProvider.TryGetValue(string variableName, out object? value)
  122. {
  123. return TryGetVariable(variableName, out value);
  124. }
  125. T RootModel<T>()
  126. where T : IEventDataModel
  127. {
  128. if(this is IChildEventDataModel child)
  129. {
  130. return child.Parent.RootModel<T>();
  131. }
  132. else if(this is T root)
  133. {
  134. return root;
  135. }
  136. else
  137. {
  138. throw new Exception($"Root model of wrong type; expected {typeof(T).FullName}; got {GetType().FullName}");
  139. }
  140. }
  141. }
  142. public interface ITypedEventDataModel : IEventDataModel
  143. {
  144. public Type EntityType { get; }
  145. }
  146. public interface IChildEventDataModel : IEventDataModel
  147. {
  148. IEventDataModel Parent { get; }
  149. }
  150. public class ChildEventDataModel : IChildEventDataModel
  151. {
  152. public IEventDataModel Parent { get; set; }
  153. public Dictionary<string, object?> Values { get; set; } = new Dictionary<string, object?>();
  154. public ChildEventDataModel(IEventDataModel parent)
  155. {
  156. Parent = parent;
  157. }
  158. public bool TryGetVariable(string name, out object? value)
  159. {
  160. return Values.TryGetValue(name, out value)
  161. || Parent.TryGetVariable(name, out value);
  162. }
  163. }
  164. #endregion
  165. public interface IEvent
  166. {
  167. IEventDataModelDefinition DataModelDefinition();
  168. public static void RunAction(IEvent ev, IEventDataModel model, IEventAction action)
  169. {
  170. var dataModelDef = ev.DataModelDefinition();
  171. var values = new List<(string name, object? value)[]>() { Array.Empty<(string, object?)>() };
  172. var vars = action.ReferencedVariables();
  173. foreach(var variable in vars)
  174. {
  175. var varDef = dataModelDef.GetVariable(variable);
  176. if(varDef is ListEventVariable)
  177. {
  178. if (model.TryGetVariable(varDef.Name, out var list) && list is IEnumerable enumerable)
  179. {
  180. var oldValues = values;
  181. values = new List<(string name, object? value)[]>();
  182. foreach(var item in enumerable)
  183. {
  184. foreach(var valueList in oldValues)
  185. {
  186. values.Add(valueList.Concatenate(new (string name, object? value)[]
  187. {
  188. (varDef.Name, item)
  189. }));
  190. }
  191. }
  192. }
  193. else
  194. {
  195. values.Clear();
  196. break;
  197. }
  198. }
  199. }
  200. if(values.Count > 0 && (values.Count > 1 || values[0].Length > 0))
  201. {
  202. var subModel = new ChildEventDataModel(model);
  203. foreach(var valueSet in values)
  204. {
  205. subModel.Values.Clear();
  206. foreach(var (name, value) in valueSet)
  207. {
  208. subModel.Values[name] = value;
  209. }
  210. action.Execute(subModel);
  211. }
  212. }
  213. else
  214. {
  215. action.Execute(model);
  216. }
  217. }
  218. }
  219. public interface IEvent<TDataModel> : IEvent
  220. {
  221. Notification GenerateNotification(TDataModel model);
  222. }
  223. public interface ITrigger<TEvent, TDataModel>
  224. where TEvent : IEvent<TDataModel>
  225. where TDataModel : IEventDataModel
  226. {
  227. bool Check(TDataModel dataModel);
  228. }
  229. public interface IEventAction
  230. {
  231. IEnumerable<string> ReferencedVariables();
  232. object? Execute(IEventDataModel dataModel);
  233. }
  234. public interface IEventAction<TEvent> : IEventAction
  235. where TEvent : IEvent
  236. {
  237. }