| | | 1 | | using pva.Helpers.Extensions; |
| | | 2 | | using pva.SuperV.Engine.Exceptions; |
| | | 3 | | using pva.SuperV.Engine.FieldFormatters; |
| | | 4 | | using pva.SuperV.Engine.HistoryStorage; |
| | | 5 | | using pva.SuperV.Engine.Processing; |
| | | 6 | | using System.Collections.Concurrent; |
| | | 7 | | using System.Text.Json.Serialization; |
| | | 8 | | using System.Threading.Channels; |
| | | 9 | | |
| | | 10 | | namespace pva.SuperV.Engine |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// SuperV Project class. It contains all the information required (<see cref="Class"/>, <see cref="Instance"/>, pro |
| | | 14 | | /// </summary> |
| | | 15 | | public abstract class Project : IDisposable |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets the projects path where all SuperV stuff is stored (generated assemblies, project definitions and snaps |
| | | 19 | | /// </summary> |
| | | 20 | | /// <value> |
| | | 21 | | /// The projects path. |
| | | 22 | | /// </value> |
| | 16393 | 23 | | public static string ProjectsPath { get; } = Path.Combine(Path.GetTempPath(), "pva.SuperV"); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets or sets the current project. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <value> |
| | | 29 | | /// The current project. |
| | | 30 | | /// </value> |
| | 0 | 31 | | public static Project? CurrentProject { get; set; } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The name of project. Use <see cref="Name"/> to access its content. |
| | | 35 | | /// </summary> |
| | | 36 | | private string? _name; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets or sets the name of the project. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <value> |
| | | 42 | | /// The project name. |
| | | 43 | | /// </value> |
| | | 44 | | public string? Name |
| | | 45 | | { |
| | 5327 | 46 | | get => _name; |
| | | 47 | | set |
| | 438 | 48 | | { |
| | 438 | 49 | | IdentifierValidation.ValidateIdentifier("project", value); |
| | 435 | 50 | | _name = value; |
| | 435 | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets or sets the description. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <value> |
| | | 58 | | /// The description. |
| | | 59 | | /// </value> |
| | 379 | 60 | | public string? Description { get; set; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets or sets the version. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <value> |
| | | 66 | | /// The version. |
| | | 67 | | /// </value> |
| | 2151 | 68 | | public int Version { get; set; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the classes of project. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <value> |
| | | 74 | | /// The classes. |
| | | 75 | | /// </value> |
| | 9334 | 76 | | public Dictionary<string, Class> Classes { get; init; } = new(StringComparer.OrdinalIgnoreCase); |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets the field formatters. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <value> |
| | | 82 | | /// The field formatters. |
| | | 83 | | /// </value> |
| | 2034 | 84 | | public Dictionary<string, FieldFormatter> FieldFormatters { get; init; } = new(StringComparer.OrdinalIgnoreCase) |
| | | 85 | | |
| | | 86 | | private IHistoryStorageEngine? historyStorageEngine; |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// The history storage engin connection string. |
| | | 90 | | /// </summary> |
| | 718 | 91 | | public string? HistoryStorageEngineConnectionString { get; set; } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the history repositories. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <value> |
| | | 97 | | /// The history repositories. |
| | | 98 | | /// </value> |
| | | 99 | | [JsonIgnore] |
| | | 100 | | public IHistoryStorageEngine? HistoryStorageEngine |
| | | 101 | | { |
| | 671 | 102 | | get => historyStorageEngine; |
| | | 103 | | set |
| | 388 | 104 | | { |
| | 388 | 105 | | historyStorageEngine = value; |
| | 389 | 106 | | HistoryRepositories.Values.ForEach(historyRepository => historyRepository.HistoryStorageEngine = history |
| | 388 | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Gets the history repositories. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <value> |
| | | 114 | | /// The history repositories. |
| | | 115 | | /// </value> |
| | 4066 | 116 | | public Dictionary<string, HistoryRepository> HistoryRepositories { get; init; } = new(StringComparer.OrdinalIgno |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets the channels associated to topics. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <value> |
| | | 122 | | /// The channels. |
| | | 123 | | /// </value> |
| | | 124 | | [JsonIgnore] |
| | 1819 | 125 | | public Dictionary<string, Channel<FieldValueChangedEvent>> TopicsChannels { get; init; } = new(StringComparer.Or |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Gets the script definitions. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <value> |
| | | 131 | | /// The script definitions. |
| | | 132 | | /// </value> |
| | 1325 | 133 | | public Dictionary<string, ScriptDefinition> ScriptDefinitions { get; init; } = new(StringComparer.OrdinalIgnoreC |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// List of projects in use. |
| | | 137 | | /// </summary> |
| | 1929 | 138 | | public static ConcurrentDictionary<string, Project> Projects { get; } = new(StringComparer.OrdinalIgnoreCase); |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Creates an empty <see cref="WipProject"/>. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="projectName">Name of the project.</param> |
| | | 144 | | /// <returns>The created <see cref="WipProject"/></returns> |
| | | 145 | | public static WipProject CreateProject(string projectName) |
| | 92 | 146 | | { |
| | 92 | 147 | | return CreateProject(projectName, null); |
| | 89 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Creates an empty <see cref="WipProject"/>. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <param name="projectName">Name of the project.</param> |
| | | 154 | | /// <param name="historyStorageEngineConnectionString">History storage connection string.</param> |
| | | 155 | | /// <returns>The created <see cref="WipProject"/></returns> |
| | | 156 | | public static WipProject CreateProject(string projectName, string? historyStorageEngineConnectionString) |
| | 252 | 157 | | { |
| | 252 | 158 | | WipProject project = new(projectName); |
| | 249 | 159 | | AddProjectToCollection(project); |
| | 249 | 160 | | if (string.IsNullOrEmpty(historyStorageEngineConnectionString)) |
| | 91 | 161 | | { |
| | 91 | 162 | | return project; |
| | | 163 | | } |
| | | 164 | | |
| | 158 | 165 | | project.HistoryStorageEngineConnectionString = historyStorageEngineConnectionString; |
| | 158 | 166 | | project.HistoryStorageEngine = HistoryStorageEngineFactory.CreateHistoryStorageEngine(historyStorageEngineCo |
| | 158 | 167 | | return project; |
| | 249 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Creates a <see cref="WipProject"/> from a <see cref="RunnableProject"/> for modification. |
| | | 172 | | /// </summary> |
| | | 173 | | /// <param name="runnableProject">The runnable project from which to create the new <see cref="WipProject"/>.</p |
| | | 174 | | /// <returns>The new <see cref="WipProject"/></returns> |
| | | 175 | | public static WipProject CreateProject(RunnableProject runnableProject) |
| | 38 | 176 | | { |
| | 38 | 177 | | WipProject wipProject = new(runnableProject); |
| | 38 | 178 | | AddProjectToCollection(wipProject); |
| | 38 | 179 | | return wipProject; |
| | 38 | 180 | | } |
| | | 181 | | |
| | | 182 | | internal static void AddProjectToCollection(Project project) |
| | 433 | 183 | | { |
| | 433 | 184 | | if (Projects.TryGetValue(project.GetId(), out Project? previousProject)) |
| | 48 | 185 | | { |
| | 48 | 186 | | previousProject?.Unload(); |
| | 48 | 187 | | } |
| | 433 | 188 | | Projects[project.GetId()] = project; |
| | 433 | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Builds the specified <see cref="WipProject"/>. |
| | | 193 | | /// </summary> |
| | | 194 | | /// <param name="wipProject">The WIP project.</param> |
| | | 195 | | /// <returns>a <see cref="RunnableProject"/></returns> |
| | | 196 | | public static async Task<RunnableProject> BuildAsync(WipProject wipProject) |
| | 134 | 197 | | { |
| | 134 | 198 | | RunnableProject runnableProject = await ProjectBuilder.BuildAsync(wipProject); |
| | 132 | 199 | | AddProjectToCollection(runnableProject); |
| | 132 | 200 | | return runnableProject; |
| | 132 | 201 | | } |
| | | 202 | | |
| | | 203 | | /// <summary> |
| | | 204 | | /// Gets a class for a name. |
| | | 205 | | /// </summary> |
| | | 206 | | /// <param name="className">Name of the class.</param> |
| | | 207 | | /// <returns>Found class</returns> |
| | | 208 | | /// <exception cref="UnknownEntityException">Class wasn't found.</exception> |
| | | 209 | | public Class GetClass(string className) |
| | 6113 | 210 | | { |
| | 6113 | 211 | | if (Classes.TryGetValue(className, out Class? value)) |
| | 6111 | 212 | | { |
| | 6111 | 213 | | return value; |
| | | 214 | | } |
| | | 215 | | |
| | 2 | 216 | | throw new UnknownEntityException("Class", className); |
| | 6111 | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Finds a class for a name. |
| | | 221 | | /// </summary> |
| | | 222 | | /// <param name="className">Name of the class.</param> |
| | | 223 | | /// <returns><see cref="Class"/> if found or null otherwise.</returns> |
| | | 224 | | public Class? FindClass(string className) |
| | 2 | 225 | | { |
| | | 226 | | try |
| | 2 | 227 | | { |
| | 2 | 228 | | return GetClass(className); |
| | | 229 | | } |
| | 3 | 230 | | catch { return null; } |
| | 2 | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// Gets a formatter for name. |
| | | 235 | | /// </summary> |
| | | 236 | | /// <param name="formatterName">Name of the formatter.</param> |
| | | 237 | | /// <returns><see cref="FieldFormatter"/></returns> |
| | | 238 | | /// <exception cref="UnknownEntityException">The field formatter doesn't exist</exception> |
| | | 239 | | public FieldFormatter? GetFormatter(string? formatterName) |
| | 2232 | 240 | | { |
| | 2232 | 241 | | if (String.IsNullOrEmpty(formatterName)) |
| | 1888 | 242 | | { |
| | 1888 | 243 | | return null; |
| | | 244 | | } |
| | 344 | 245 | | if (FieldFormatters.TryGetValue(formatterName, out FieldFormatter? value)) |
| | 339 | 246 | | { |
| | 339 | 247 | | return value; |
| | | 248 | | } |
| | | 249 | | |
| | 5 | 250 | | throw new UnknownEntityException("Field formatter", formatterName); |
| | 2227 | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Finds a formatter for name. |
| | | 255 | | /// </summary> |
| | | 256 | | /// <param name="formatterName">Name of the formatter.</param> |
| | | 257 | | /// <returns><see cref="FieldFormatter"/> if found or null otherwise.</returns> |
| | | 258 | | public FieldFormatter? FindFormatter(string formatterName) |
| | 2 | 259 | | { |
| | | 260 | | try |
| | 2 | 261 | | { |
| | 2 | 262 | | return GetFormatter(formatterName); |
| | | 263 | | } |
| | 3 | 264 | | catch { return null; } |
| | 2 | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Gets the name of the generated assembly file for project. |
| | | 269 | | /// </summary> |
| | | 270 | | /// <returns>Assembly file name</returns> |
| | | 271 | | public string GetAssemblyFileName() |
| | 552 | 272 | | { |
| | 552 | 273 | | if (!Directory.Exists(ProjectsPath)) |
| | 1 | 274 | | { |
| | 1 | 275 | | Directory.CreateDirectory(ProjectsPath); |
| | 1 | 276 | | } |
| | 552 | 277 | | return Path.Combine(ProjectsPath, $"{Name}-V{Version}.dll"); |
| | 552 | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Gets the project highest version from generated assemblies of project. |
| | | 282 | | /// </summary> |
| | | 283 | | /// <param name="projectName">Name of the project.</param> |
| | | 284 | | /// <returns>The highest version or 0 if first version.</returns> |
| | | 285 | | protected static int GetProjectHighestVersion(string projectName) |
| | 287 | 286 | | { |
| | 287 | 287 | | return Directory.Exists(ProjectsPath) |
| | 287 | 288 | | ? Directory.EnumerateFiles(ProjectsPath, $"{projectName}-V*.dll") |
| | 287 | 289 | | .Select(fileName => |
| | 14705 | 290 | | Convert.ToInt32(fileName |
| | 14705 | 291 | | .Replace(ProjectsPath, "") |
| | 14705 | 292 | | .Replace(Path.DirectorySeparatorChar.ToString(), "") |
| | 14705 | 293 | | .Replace($"{projectName}-V", "") |
| | 14705 | 294 | | .Replace(".dll", ""))) |
| | 287 | 295 | | .Order() |
| | 287 | 296 | | .LastOrDefault() |
| | 287 | 297 | | : 0; |
| | 287 | 298 | | } |
| | | 299 | | |
| | | 300 | | /// <summary> |
| | | 301 | | /// Gets the next version for project. |
| | | 302 | | /// </summary> |
| | | 303 | | /// <returns>Next project version</returns> |
| | | 304 | | protected int GetNextVersion() |
| | 287 | 305 | | { |
| | 287 | 306 | | return GetProjectHighestVersion(Name!) + 1; |
| | 287 | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary> |
| | | 310 | | /// Unloads a project. |
| | | 311 | | /// </summary> |
| | | 312 | | /// <param name="project">The project to be unloaded.</param> |
| | | 313 | | public static void Unload(Project project) |
| | 15 | 314 | | { |
| | 15 | 315 | | WeakReference? projectAssemblyLoaderWeakRef = null; |
| | 15 | 316 | | if (project is RunnableProject runnableProject) |
| | 15 | 317 | | { |
| | 15 | 318 | | projectAssemblyLoaderWeakRef = runnableProject.ProjectAssemblyLoaderWeakRef; |
| | 15 | 319 | | } |
| | 15 | 320 | | project.Dispose(); |
| | 15 | 321 | | if (projectAssemblyLoaderWeakRef is not null) |
| | 15 | 322 | | { |
| | 15 | 323 | | CallGcCleanup(projectAssemblyLoaderWeakRef); |
| | 15 | 324 | | } |
| | 15 | 325 | | } |
| | | 326 | | |
| | | 327 | | |
| | | 328 | | /// <summary> |
| | | 329 | | /// Unloads the project. |
| | | 330 | | /// </summary> |
| | | 331 | | public virtual void Unload() |
| | 430 | 332 | | { |
| | 430 | 333 | | FieldFormatters.Clear(); |
| | 430 | 334 | | Classes.Clear(); |
| | 430 | 335 | | Projects.Remove(GetId(), out _); |
| | 430 | 336 | | } |
| | | 337 | | |
| | | 338 | | [System.Diagnostics.CodeAnalysis.SuppressMessage("Critical Code Smell", "S1215:\"GC.Collect\" should not be call |
| | | 339 | | Justification = "Need to call GC to remove references to assembly")] |
| | | 340 | | public static void CallGcCleanup(WeakReference palWeakRef) |
| | 15 | 341 | | { |
| | 314 | 342 | | for (int i = 0; palWeakRef.IsAlive && i < 10; i++) |
| | 142 | 343 | | { |
| | 142 | 344 | | GC.Collect(); |
| | 142 | 345 | | GC.WaitForPendingFinalizers(); |
| | 142 | 346 | | } |
| | 15 | 347 | | } |
| | | 348 | | |
| | | 349 | | /// <summary> |
| | | 350 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 351 | | /// </summary> |
| | | 352 | | public void Dispose() |
| | 381 | 353 | | { |
| | 381 | 354 | | Dispose(true); |
| | 381 | 355 | | GC.SuppressFinalize(this); |
| | 381 | 356 | | } |
| | | 357 | | |
| | | 358 | | /// <summary> |
| | | 359 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 360 | | /// </summary> |
| | | 361 | | /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release |
| | | 362 | | protected virtual void Dispose(bool disposing) |
| | 381 | 363 | | { |
| | 381 | 364 | | Unload(); |
| | 381 | 365 | | } |
| | | 366 | | |
| | | 367 | | /// <summary> |
| | | 368 | | /// Gets the identifier of project. |
| | | 369 | | /// </summary> |
| | | 370 | | /// <returns>Project ID</returns> |
| | | 371 | | public abstract string GetId(); |
| | | 372 | | |
| | | 373 | | /// <summary> |
| | | 374 | | /// Sets the field value change channel. |
| | | 375 | | /// </summary> |
| | | 376 | | /// <param name="fieldDefinition">The field definition.</param> |
| | | 377 | | public void SetFieldValueChangeChannel(IFieldDefinition fieldDefinition) |
| | 3514 | 378 | | { |
| | 3514 | 379 | | if (!String.IsNullOrEmpty(fieldDefinition.TopicName)) |
| | 318 | 380 | | { |
| | 318 | 381 | | Channel<FieldValueChangedEvent>? topicChannel = null; |
| | 318 | 382 | | if (!TopicsChannels.TryGetValue(fieldDefinition.TopicName, out topicChannel)) |
| | 318 | 383 | | { |
| | 318 | 384 | | topicChannel = Channel.CreateUnbounded<FieldValueChangedEvent>( |
| | 318 | 385 | | new UnboundedChannelOptions |
| | 318 | 386 | | { |
| | 318 | 387 | | SingleWriter = true, |
| | 318 | 388 | | SingleReader = false, |
| | 318 | 389 | | AllowSynchronousContinuations = false |
| | 318 | 390 | | }); |
| | 318 | 391 | | TopicsChannels.Add(fieldDefinition.TopicName, topicChannel); |
| | 318 | 392 | | } |
| | 318 | 393 | | fieldDefinition.FieldValueChangedEventChannel = topicChannel; |
| | 318 | 394 | | } |
| | 3514 | 395 | | } |
| | | 396 | | |
| | | 397 | | /// <summary> |
| | | 398 | | /// Gets the topic names. |
| | | 399 | | /// </summary> |
| | | 400 | | /// <returns>List of topic names.</returns> |
| | | 401 | | public HashSet<string> GetTopicNames() => |
| | 0 | 402 | | TopicsChannels.Keys.ToHashSet(); |
| | | 403 | | } |
| | | 404 | | } |