From 6d1cffe2dbb0eeb2b2a3b5738502f96ff2c3072a Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Thu, 25 Dec 2025 13:20:24 +0100 Subject: [PATCH] Added enums to JSONEntity and JSONDatapoint --- src/Server/Exceptions/GeneralExceptions.cs | 6 +++-- src/Server/Helper/DatabaseHelper.cs | 10 ++++----- src/Server/Helper/SearchdomainHelper.cs | 8 +++---- src/Server/Probmethods.cs | 25 +++++++++------------ src/Server/Program.cs | 9 +++++++- src/Server/Searchdomain.cs | 12 ++++++++-- src/Server/SimilarityMethods.cs | 16 +++++-------- src/Shared/Models/JSONModels.cs | 26 +++++++++++++++++++--- 8 files changed, 70 insertions(+), 42 deletions(-) diff --git a/src/Server/Exceptions/GeneralExceptions.cs b/src/Server/Exceptions/GeneralExceptions.cs index e5a37b1..0a558f0 100644 --- a/src/Server/Exceptions/GeneralExceptions.cs +++ b/src/Server/Exceptions/GeneralExceptions.cs @@ -1,7 +1,9 @@ +using Shared.Models; + namespace Server.Exceptions; -public class ProbMethodNotFoundException(string probMethod) : Exception($"Unknown probMethod name {probMethod}") { } +public class ProbMethodNotFoundException(ProbMethodEnum probMethod) : Exception($"Unknown probMethod name {probMethod}") { } -public class SimilarityMethodNotFoundException(string similarityMethod) : Exception($"Unknown similarityMethod name \"{similarityMethod}\"") { } +public class SimilarityMethodNotFoundException(SimilarityMethodEnum similarityMethod) : Exception($"Unknown similarityMethod name \"{similarityMethod}\"") { } public class JSONPathSelectionException(string path, string testedContent) : Exception($"Unable to select tokens using JSONPath {path} for string: {testedContent}.") { } \ No newline at end of file diff --git a/src/Server/Helper/DatabaseHelper.cs b/src/Server/Helper/DatabaseHelper.cs index 0a9c135..2904659 100644 --- a/src/Server/Helper/DatabaseHelper.cs +++ b/src/Server/Helper/DatabaseHelper.cs @@ -38,12 +38,12 @@ public class DatabaseHelper(ILogger logger) return helper.ExecuteSQLCommandGetInsertedID("INSERT INTO searchdomain (name, settings) VALUES (@name, @settings)", parameters); } - public static int DatabaseInsertEntity(SQLHelper helper, string name, string probmethod, int id_searchdomain) + public static int DatabaseInsertEntity(SQLHelper helper, string name, ProbMethodEnum probmethod, int id_searchdomain) { Dictionary parameters = new() { { "name", name }, - { "probmethod", probmethod }, + { "probmethod", probmethod.ToString() }, { "id_searchdomain", id_searchdomain } }; return helper.ExecuteSQLCommandGetInsertedID("INSERT INTO entity (name, probmethod, id_searchdomain) VALUES (@name, @probmethod, @id_searchdomain)", parameters); @@ -60,13 +60,13 @@ public class DatabaseHelper(ILogger logger) return helper.ExecuteSQLCommandGetInsertedID("INSERT INTO attribute (attribute, value, id_entity) VALUES (@attribute, @value, @id_entity)", parameters); } - public static int DatabaseInsertDatapoint(SQLHelper helper, string name, string probmethod_embedding, string similarityMethod, string hash, int id_entity) + public static int DatabaseInsertDatapoint(SQLHelper helper, string name, ProbMethodEnum probmethod_embedding, SimilarityMethodEnum similarityMethod, string hash, int id_entity) { Dictionary parameters = new() { { "name", name }, - { "probmethod_embedding", probmethod_embedding }, - { "similaritymethod", similarityMethod }, + { "probmethod_embedding", probmethod_embedding.ToString() }, + { "similaritymethod", similarityMethod.ToString() }, { "hash", hash }, { "id_entity", id_entity } }; diff --git a/src/Server/Helper/SearchdomainHelper.cs b/src/Server/Helper/SearchdomainHelper.cs index 2dfee5a..96727cf 100644 --- a/src/Server/Helper/SearchdomainHelper.cs +++ b/src/Server/Helper/SearchdomainHelper.cs @@ -179,13 +179,13 @@ public class SearchdomainHelper(ILogger logger, DatabaseHelp preexistingEntity.datapoints.Add(newDatapoint); } - if (newEntityDatapoint is not null && (newEntityDatapoint.Probmethod_embedding != datapoint.probMethod.name || newEntityDatapoint.SimilarityMethod != datapoint.similarityMethod.name)) + if (newEntityDatapoint is not null && (newEntityDatapoint.Probmethod_embedding != datapoint.probMethod.probMethodEnum || newEntityDatapoint.SimilarityMethod != datapoint.similarityMethod.similarityMethodEnum)) { // Datapoint - Updated (probmethod or similaritymethod) Dictionary parameters = new() { - { "probmethod", newEntityDatapoint.Probmethod_embedding }, - { "similaritymethod", newEntityDatapoint.SimilarityMethod }, + { "probmethod", newEntityDatapoint.Probmethod_embedding.ToString() }, + { "similaritymethod", newEntityDatapoint.SimilarityMethod.ToString() }, { "datapointName", datapoint.name }, { "entityId", preexistingEntityID} }; @@ -227,7 +227,7 @@ public class SearchdomainHelper(ILogger logger, DatabaseHelp } var probMethod = Probmethods.GetMethod(jsonEntity.Probmethod) ?? throw new ProbMethodNotFoundException(jsonEntity.Probmethod); - Entity entity = new(jsonEntity.Attributes, probMethod, jsonEntity.Probmethod, datapoints, jsonEntity.Name) + Entity entity = new(jsonEntity.Attributes, probMethod, jsonEntity.Probmethod.ToString(), datapoints, jsonEntity.Name) { id = id_entity }; diff --git a/src/Server/Probmethods.cs b/src/Server/Probmethods.cs index 0a17a86..439f9cd 100644 --- a/src/Server/Probmethods.cs +++ b/src/Server/Probmethods.cs @@ -1,37 +1,29 @@ using System.Text.Json; using Server.Exceptions; +using Shared.Models; namespace Server; public class ProbMethod { public Probmethods.probMethodDelegate method; + public ProbMethodEnum probMethodEnum; public string name; - public ProbMethod(string name, ILogger logger) + public ProbMethod(ProbMethodEnum probMethodEnum, ILogger logger) { - this.name = name; + this.probMethodEnum = probMethodEnum; + this.name = probMethodEnum.ToString(); Probmethods.probMethodDelegate? probMethod = Probmethods.GetMethod(name); if (probMethod is null) { logger.LogError("Unable to retrieve probMethod {name}", [name]); - throw new ProbMethodNotFoundException(name); + throw new ProbMethodNotFoundException(probMethodEnum); } method = probMethod; } } -public enum ProbMethodEnum -{ - Mean, - HarmonicMean, - QuadraticMean, - GeometricMean, - EVEWAvg, - HVEWAvg, - LVEWAvg, - DictionaryWeightedAverage -} public static class Probmethods { @@ -54,6 +46,11 @@ public static class Probmethods }; } + public static probMethodDelegate? GetMethod(ProbMethodEnum probMethodEnum) + { + return GetMethod(probMethodEnum.ToString()); + } + public static probMethodDelegate? GetMethod(string name) { string methodName = name; diff --git a/src/Server/Program.cs b/src/Server/Program.cs index df02526..77f701d 100644 --- a/src/Server/Program.cs +++ b/src/Server/Program.cs @@ -8,12 +8,19 @@ using Server.HealthChecks; using Server.Helper; using Server.Models; using Server.Services; +using System.Text.Json.Serialization; var builder = WebApplication.CreateBuilder(args); // Add services to the container. -builder.Services.AddControllersWithViews(); +builder.Services.AddControllersWithViews() + .AddJsonOptions(options => + { + options.JsonSerializerOptions.Converters.Add( + new JsonStringEnumConverter() + ); + }); // Add Localization builder.Services.AddLocalization(options => options.ResourcesPath = "Resources"); diff --git a/src/Server/Searchdomain.cs b/src/Server/Searchdomain.cs index 1b0e0c3..b48fcf0 100644 --- a/src/Server/Searchdomain.cs +++ b/src/Server/Searchdomain.cs @@ -96,8 +96,16 @@ public class Searchdomain string probmethodString = datapointReader.GetString(3); string similarityMethodString = datapointReader.GetString(4); string hash = datapointReader.GetString(5); - ProbMethod probmethod = new(probmethodString, _logger); - SimilarityMethod similarityMethod = new(similarityMethodString, _logger); + ProbMethodEnum probmethodEnum = (ProbMethodEnum)Enum.Parse( + typeof(ProbMethodEnum), + probmethodString + ); + SimilarityMethodEnum similairtyMethodEnum = (SimilarityMethodEnum)Enum.Parse( + typeof(SimilarityMethodEnum), + similarityMethodString + ); + ProbMethod probmethod = new(probmethodEnum, _logger); + SimilarityMethod similarityMethod = new(similairtyMethodEnum, _logger); if (embedding_unassigned.TryGetValue(id, out Dictionary? embeddings) && probmethod is not null) { embedding_unassigned.Remove(id); diff --git a/src/Server/SimilarityMethods.cs b/src/Server/SimilarityMethods.cs index 9d02c22..3d750ca 100644 --- a/src/Server/SimilarityMethods.cs +++ b/src/Server/SimilarityMethods.cs @@ -1,16 +1,18 @@ using System.Numerics.Tensors; -using System.Text.Json; +using Shared.Models; namespace Server; public class SimilarityMethod { public SimilarityMethods.similarityMethodDelegate method; + public SimilarityMethodEnum similarityMethodEnum; public string name; - public SimilarityMethod(string name, ILogger logger) + public SimilarityMethod(SimilarityMethodEnum similarityMethodEnum, ILogger logger) { - this.name = name; + this.similarityMethodEnum = similarityMethodEnum; + this.name = similarityMethodEnum.ToString(); SimilarityMethods.similarityMethodDelegate? probMethod = SimilarityMethods.GetMethod(name); if (probMethod is null) { @@ -21,14 +23,6 @@ public class SimilarityMethod } } -public enum SimilarityMethodEnum -{ - Cosine, - Euclidian, - Manhattan, - Pearson -} - public static class SimilarityMethods { public delegate float similarityMethodProtoDelegate(float[] vector1, float[] vector2); diff --git a/src/Shared/Models/JSONModels.cs b/src/Shared/Models/JSONModels.cs index 593ae96..29aa1fb 100644 --- a/src/Shared/Models/JSONModels.cs +++ b/src/Shared/Models/JSONModels.cs @@ -3,7 +3,7 @@ namespace Shared.Models; public class JSONEntity { public required string Name { get; set; } - public required string Probmethod { get; set; } + public required ProbMethodEnum Probmethod { get; set; } public required string Searchdomain { get; set; } public required Dictionary Attributes { get; set; } public required JSONDatapoint[] Datapoints { get; set; } @@ -13,7 +13,27 @@ public class JSONDatapoint { public required string Name { get; set; } public required string? Text { get; set; } - public required string Probmethod_embedding { get; set; } - public required string SimilarityMethod { get; set; } + public required ProbMethodEnum Probmethod_embedding { get; set; } + public required SimilarityMethodEnum SimilarityMethod { get; set; } public required string[] Model { get; set; } +} + +public enum ProbMethodEnum +{ + Mean, + HarmonicMean, + QuadraticMean, + GeometricMean, + EVEWAvg, + HVEWAvg, + LVEWAvg, + DictionaryWeightedAverage +} + +public enum SimilarityMethodEnum +{ + Cosine, + Euclidian, + Manhattan, + Pearson } \ No newline at end of file