From e7c693e578fb4e88b2e4a9933e7335ea1aa5d9bc Mon Sep 17 00:00:00 2001 From: MaZiFAU <63099053+MaZiFAU@users.noreply.github.com> Date: Fri, 7 Jul 2023 18:44:51 +0200 Subject: [PATCH] Moved ListFact and TupleFact; BugFix; BugFix: +no level reload after GenerateDemoFile --- Assets/Scripts/GenerateDemoFiles.cs | 3 +- .../FactHandling/Facts/Fact.cs | 213 ----------------- .../FactHandling/Facts/MMTTypes.cs | 218 ++++++++++++++++++ .../FactHandling/Facts/MMTTypes.cs.meta | 11 + Assets/Scripts/StageBehaviour.cs | 2 +- Assets/Scripts/StageStatic.cs | 2 +- Assets/Scripts/UI/MainMenue/ListLoader.cs | 12 +- 7 files changed, 238 insertions(+), 223 deletions(-) create mode 100644 Assets/Scripts/InteractionEngine/FactHandling/Facts/MMTTypes.cs create mode 100644 Assets/Scripts/InteractionEngine/FactHandling/Facts/MMTTypes.cs.meta diff --git a/Assets/Scripts/GenerateDemoFiles.cs b/Assets/Scripts/GenerateDemoFiles.cs index 6a3d69ff..bd2c24d0 100644 --- a/Assets/Scripts/GenerateDemoFiles.cs +++ b/Assets/Scripts/GenerateDemoFiles.cs @@ -10,10 +10,9 @@ public class GenerateDemoFiles { - public static void GenerateAll() { - if (GameObject.FindObjectOfType<GadgetBehaviour>(true) == null) + if (UnityEngine.Object.FindObjectOfType<GadgetBehaviour>(true) == null) { Debug.LogError("Cannot GenerateDemoFiles without populated GadgetManager"); return; diff --git a/Assets/Scripts/InteractionEngine/FactHandling/Facts/Fact.cs b/Assets/Scripts/InteractionEngine/FactHandling/Facts/Fact.cs index 16a6d1fe..45bc11f5 100644 --- a/Assets/Scripts/InteractionEngine/FactHandling/Facts/Fact.cs +++ b/Assets/Scripts/InteractionEngine/FactHandling/Facts/Fact.cs @@ -2297,219 +2297,6 @@ protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, F => new UnEqualCirclesFact(old_to_new[this.Cid1], old_to_new[this.Cid2], organizer); } -/// <summary> -/// -/// </summary> -/// <seealso cref="SOMDoc.MakeList(string[], string)"/> -/// <remarks>Needed to refere to lists serverside</remarks> -public class ListFact : FactWrappedCRTP<ListFact> -{ - public string[] lids; - - public string typeURI; - [JsonIgnore] - public Type ListType - { - get => MMT_OMS_URI.OMS_TO_TYPE[typeURI]; - private set => typeURI = MMT_OMS_URI.TYPE_TO_OMS[value]; - } - - - public ListFact() : base() - { - lids = new string[0]; - } - - public ListFact(string[] lids, Type ListType, FactOrganizer organizer) : base(organizer) - { - this.lids = lids; - this.ListType = ListType; - SendToMMT(); - } - - public ListFact(string[] lids, string typeURI, string backendURI, FactOrganizer organizer) : base(organizer) - { - this.lids = lids; - this.typeURI = typeURI; - _URI = backendURI; - } - - - protected override bool EquivalentWrapped(ListFact f1, ListFact f2) - => f1.typeURI == f2.typeURI - && DependentFactsEquivalent(f1, f2); - - public override bool HasDependentFacts - => true; - - protected override string[] GetGetDependentFactIds() - => lids; - - public override MMTDeclaration MakeMMTDeclaration() - { - OMA List = SOMDoc.MakeList(lids, typeURI); - return new MMTSymbolDeclaration(Label, List.applicant, List.arguments[0]); - } - - public static new ListFact parseFact(MMTDeclaration fact) - => new(parseFactList(fact, out string typeURI).ToArray(), typeURI, fact.@ref.uri, StageStatic.stage.factState); - - public static List<string> parseFactList(MMTDeclaration decl, out string typeURI) - { - if (decl is not MMTSymbolDeclaration MMTSymbol - || MMTSymbol.type is not OMA listOMA - || listOMA.arguments[0] is not OMS typeOMS) - { - typeURI = null; - return null; - } - - typeURI = typeOMS.uri; - - List<string> ret = new(); - SOMDoc next_element = MMTSymbol.defines; - while (true) - { - if (next_element is not OMA current_element) - return ret; - - switch (current_element.arguments.Length) - { - case 2: - if (current_element.arguments[1] is not OMS oMS) - return ret; - - ret.Add(oMS.uri); - next_element = current_element.arguments[0]; - break; - - case 0: - case 1: - default: - return ret; - } - } - } - - public static List<T> parseFactList<T>(MMTDeclaration decl) - { - if (decl is not MMTSymbolDeclaration mMTSymbol) - return null; - - List<T> ret = new(); - SOMDoc next_element = mMTSymbol.defines; - while (true) - { - if (next_element is not OMA current_element) - return ret; - - switch (current_element.arguments.Length) - { - case 2: - ret.Add((current_element.arguments[1].GetLambdaExpression().Compile() as Func<T>)()); - next_element = current_element.arguments[0]; - break; - - case 0: - case 1: - default: - return ret; - } - } - } - - protected override void RecalculateTransform() { } - - protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactOrganizer organizer) - => new ListFact(lids.Select(id => old_to_new[id]).ToArray(), ListType, organizer); -} - -/// <summary> -/// -/// </summary> -/// <seealso cref="SOMDoc.MakeTupel(SOMDoc[])"/> -/// <remarks>Needed to refere to tupels serverside</remarks> -public class TupelFact : FactWrappedCRTP<TupelFact> -{ - public string[] lids = new string[0]; - - public SOMDoc[] payload = new SOMDoc[0]; - - public TupelFact() : base() { } - - public TupelFact(string[] lids, SOMDoc[] payload, FactOrganizer organizer) : base(organizer) - { - Init(lids, payload); - SendToMMT(); - } - - public TupelFact(string[] lids, SOMDoc[] payload, string backendURI, FactOrganizer organizer) : base(organizer) - { - Init(lids, payload); - _URI = backendURI; - } - - private void Init(string[] lids, SOMDoc[] payload) - { - this.lids = lids ?? new string[0]; - if (!HasDependentFacts) - { - if (payload.Any(p => p == null)) - throw new ArgumentException(nameof(payload) + "must not include null elements iff " + nameof(lids) + " is all nulls or empty!"); - - this.payload = payload; - } - else - { - this.payload = new SOMDoc[math.max(lids.Length, payload.Length)]; - - for (int i = 0; i < this.payload.Length; i++) - if (lids[i] != null) - payload[i] = new OMS(lids[i]); - else - if (lids[i] == null && payload[i] == null) - throw new ArgumentException(nameof(lids) + " and " + nameof(payload) + " have to be complementary not null!"); - } - } - - protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactOrganizer organizer) - => new TupelFact(lids.Select(id => old_to_new[id]).ToArray(), payload, organizer); - - public static new TupelFact parseFact(MMTDeclaration fact) - { - if (fact is not MMTSymbolDeclaration MMTSymbol - || MMTSymbol.defines is not OMA defineOMA) - { - return null; - } - - string[] lids = defineOMA.arguments - .Select(oms => (oms as OMS)?.uri) - .ToArray(); - - return new(lids, defineOMA.arguments, fact.@ref.uri, StageStatic.stage.factState); - } - - public override MMTDeclaration MakeMMTDeclaration() - { - OMA List = SOMDoc.MakeTupel(payload); - return new MMTSymbolDeclaration(Label, List.applicant, List); - } - - protected override string[] GetGetDependentFactIds() - => lids.Where(lid => lid != null).ToArray(); - - protected override void RecalculateTransform() { } - - protected override bool EquivalentWrapped(TupelFact f1, TupelFact f2) - //=> DependentFactsEquivalent(f1, f2) - => f1.payload.Length == f2.payload.Length - && f1.payload - .Zip(f2.payload, (p1, p2) => SOMDoc.Equivalent(p1, p2)) - .All(b => b); -} - - #pragma warning disable // Testing... /// TEST FACT diff --git a/Assets/Scripts/InteractionEngine/FactHandling/Facts/MMTTypes.cs b/Assets/Scripts/InteractionEngine/FactHandling/Facts/MMTTypes.cs new file mode 100644 index 00000000..f1a57a51 --- /dev/null +++ b/Assets/Scripts/InteractionEngine/FactHandling/Facts/MMTTypes.cs @@ -0,0 +1,218 @@ +using Newtonsoft.Json; +using System.Collections.Generic; +using System.Linq; +using System; +using static SOMDocManager; + +/// <summary> +/// +/// </summary> +/// <seealso cref="SOMDoc.MakeList(string[], string)"/> +/// <remarks>Needed to refere to lists serverside</remarks> +public class ListFact : FactWrappedCRTP<ListFact> +{ + public string[] lids; + + public string typeURI; + [JsonIgnore] + public Type ListType + { + get => MMT_OMS_URI.OMS_TO_TYPE[typeURI]; + private set => typeURI = MMT_OMS_URI.TYPE_TO_OMS[value]; + } + + + public ListFact() : base() + { + lids = new string[0]; + } + + public ListFact(string[] lids, Type ListType, FactOrganizer organizer) : base(organizer) + { + this.lids = lids; + this.ListType = ListType; + SendToMMT(); + } + + public ListFact(string[] lids, string typeURI, string backendURI, FactOrganizer organizer) : base(organizer) + { + this.lids = lids; + this.typeURI = typeURI; + _URI = backendURI; + } + + + protected override bool EquivalentWrapped(ListFact f1, ListFact f2) + => f1.typeURI == f2.typeURI + && DependentFactsEquivalent(f1, f2); + + public override bool HasDependentFacts + => true; + + protected override string[] GetGetDependentFactIds() + => lids; + + public override MMTDeclaration MakeMMTDeclaration() + { + OMA List = SOMDoc.MakeList(lids, typeURI); + return new MMTSymbolDeclaration(Label, List.applicant, List.arguments[0]); + } + + public static new ListFact parseFact(MMTDeclaration fact) + => new(parseFactList(fact, out string typeURI).ToArray(), typeURI, fact.@ref.uri, StageStatic.stage.factState); + + public static List<string> parseFactList(MMTDeclaration decl, out string typeURI) + { + if (decl is not MMTSymbolDeclaration MMTSymbol + || MMTSymbol.type is not OMA listOMA + || listOMA.arguments[0] is not OMS typeOMS) + { + typeURI = null; + return null; + } + + typeURI = typeOMS.uri; + + List<string> ret = new(); + SOMDoc next_element = MMTSymbol.defines; + while (true) + { + if (next_element is not OMA current_element) + return ret; + + switch (current_element.arguments.Length) + { + case 2: + if (current_element.arguments[1] is not OMS oMS) + return ret; + + ret.Add(oMS.uri); + next_element = current_element.arguments[0]; + break; + + case 0: + case 1: + default: + return ret; + } + } + } + + public static List<T> parseFactList<T>(MMTDeclaration decl) + { + if (decl is not MMTSymbolDeclaration mMTSymbol) + return null; + + List<T> ret = new(); + SOMDoc next_element = mMTSymbol.defines; + while (true) + { + if (next_element is not OMA current_element) + return ret; + + switch (current_element.arguments.Length) + { + case 2: + ret.Add((current_element.arguments[1].GetLambdaExpression().Compile() as Func<T>)()); + next_element = current_element.arguments[0]; + break; + + case 0: + case 1: + default: + return ret; + } + } + } + + protected override void RecalculateTransform() { } + + protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactOrganizer organizer) + => new ListFact(lids.Select(id => old_to_new[id]).ToArray(), ListType, organizer); +} + +/// <summary> +/// +/// </summary> +/// <seealso cref="SOMDoc.MakeTupel(SOMDoc[])"/> +/// <remarks>Needed to refere to tupels serverside</remarks> +public class TupelFact : FactWrappedCRTP<TupelFact> +{ + public string[] lids = new string[0]; + + public SOMDoc[] payload = new SOMDoc[0]; + + + public TupelFact() : base() { } + + public TupelFact(string[] lids, SOMDoc[] payload, FactOrganizer organizer) : base(organizer) + { + Init(lids, payload); + SendToMMT(); + } + + public TupelFact(string[] lids, SOMDoc[] payload, string backendURI, FactOrganizer organizer) : base(organizer) + { + Init(lids, payload); + _URI = backendURI; + } + + private void Init(string[] lids, SOMDoc[] payload) + { + this.lids = lids ?? new string[0]; + if (!HasDependentFacts) + { + if (payload.Any(p => p == null)) + throw new ArgumentException(nameof(payload) + "must not include null elements iff " + nameof(lids) + " is all nulls or empty!"); + + this.payload = payload; + } + else + { + this.payload = new SOMDoc[Unity.Mathematics.math.max(lids.Length, payload.Length)]; + + for (int i = 0; i < this.payload.Length; i++) + if (lids[i] != null) + payload[i] = new OMS(lids[i]); + else + if (lids[i] == null && payload[i] == null) + throw new ArgumentException(nameof(lids) + " and " + nameof(payload) + " have to be complementary not null!"); + } + } + + protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactOrganizer organizer) + => new TupelFact(lids.Select(id => old_to_new[id]).ToArray(), payload, organizer); + + public static new TupelFact parseFact(MMTDeclaration fact) + { + if (fact is not MMTSymbolDeclaration MMTSymbol + || MMTSymbol.defines is not OMA defineOMA) + { + return null; + } + + string[] lids = defineOMA.arguments + .Select(oms => (oms as OMS)?.uri) + .ToArray(); + + return new(lids, defineOMA.arguments, fact.@ref.uri, StageStatic.stage.factState); + } + + public override MMTDeclaration MakeMMTDeclaration() + { + OMA List = SOMDoc.MakeTupel(payload); + return new MMTSymbolDeclaration(Label, List.applicant, List); + } + + protected override string[] GetGetDependentFactIds() + => lids.Where(lid => lid != null).ToArray(); + + protected override void RecalculateTransform() { } + + protected override bool EquivalentWrapped(TupelFact f1, TupelFact f2) + //=> DependentFactsEquivalent(f1, f2) + => f1.payload.Length == f2.payload.Length + && f1.payload + .Zip(f2.payload, (p1, p2) => SOMDoc.Equivalent(p1, p2)) + .All(b => b); +} \ No newline at end of file diff --git a/Assets/Scripts/InteractionEngine/FactHandling/Facts/MMTTypes.cs.meta b/Assets/Scripts/InteractionEngine/FactHandling/Facts/MMTTypes.cs.meta new file mode 100644 index 00000000..f17e280e --- /dev/null +++ b/Assets/Scripts/InteractionEngine/FactHandling/Facts/MMTTypes.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1310c9354caf7604dbd787fc4e90899f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/StageBehaviour.cs b/Assets/Scripts/StageBehaviour.cs index dca99dc7..f5c4d1ce 100644 --- a/Assets/Scripts/StageBehaviour.cs +++ b/Assets/Scripts/StageBehaviour.cs @@ -23,7 +23,7 @@ private void OnDestroy() { StageStatic.SetMode(Mode.Play); // no Mode.Create // StageStatic.stage.solution.hardreset(invoke_event: false); NO! keep in memory! - StageStatic.stage.factState.hardreset(invoke_event: false); + StageStatic.stage?.factState.hardreset(invoke_event: false); } /// <summary> diff --git a/Assets/Scripts/StageStatic.cs b/Assets/Scripts/StageStatic.cs index a7795875..7da9af0f 100644 --- a/Assets/Scripts/StageStatic.cs +++ b/Assets/Scripts/StageStatic.cs @@ -56,7 +56,7 @@ public static class StageStatic /// </summary> public static Stage stage { get { - return (local_stage ? StageLocal : StageOfficial)[current_name]; + return (local_stage ? StageLocal : StageOfficial)?[current_name]; } set { current_name = value.name; diff --git a/Assets/Scripts/UI/MainMenue/ListLoader.cs b/Assets/Scripts/UI/MainMenue/ListLoader.cs index c78b473f..edfab7d7 100644 --- a/Assets/Scripts/UI/MainMenue/ListLoader.cs +++ b/Assets/Scripts/UI/MainMenue/ListLoader.cs @@ -15,16 +15,16 @@ public abstract class ListLoader<T> : MenueLoader protected void OnEnable() { + // To measure correct timings, uncomment + //StartCoroutine(_Init()); - StartCoroutine(_Init()); - - System.Collections.IEnumerator _Init() - { - yield return new WaitForFixedUpdate(); + //System.Collections.IEnumerator _Init() + //{ + // yield return new WaitForFixedUpdate(); Clear(); Init(); - } + //} } protected void OnDisable() -- GitLab