From 5a758cddc9336bb3dfce4b25d4f525dcd3166916 Mon Sep 17 00:00:00 2001 From: unknown <john.schihada@hotmail.com> Date: Wed, 28 Oct 2020 19:45:23 +0100 Subject: [PATCH] Adjusted scroll/apply -> Tree-Example is working again --- Assets/Scripts/InteractionEngine/Fact.cs | 58 +++- Assets/Scripts/InventoryStuff/Scroll.cs | 58 ++-- .../Scripts/InventoryStuff/ScrollDetails.cs | 313 ++---------------- Assets/Scripts/JSONManager.cs | 10 + 4 files changed, 133 insertions(+), 306 deletions(-) diff --git a/Assets/Scripts/InteractionEngine/Fact.cs b/Assets/Scripts/InteractionEngine/Fact.cs index 568b33ee..98850a77 100644 --- a/Assets/Scripts/InteractionEngine/Fact.cs +++ b/Assets/Scripts/InteractionEngine/Fact.cs @@ -3,6 +3,17 @@ using UnityEngine; using UnityEngine.Networking; using static JSONManager; +using static FactManager; + +public class ParsingDictionary { + + public static Dictionary<string, Func<Scroll.ScrollFact, Fact>> parseFactDictionary = new Dictionary<string, Func<Scroll.ScrollFact, Fact>>() { + {MMTURIs.Point, PointFact.parseFact}, + {MMTURIs.Metric, LineFact.parseFact}, + {MMTURIs.Angle, AngleFact.parseFact} + }; + +} public abstract class Fact { @@ -103,13 +114,19 @@ public PointFact(int i, Vector3 P, Vector3 N) Debug.Log(this.backendURI); } - public PointFact(int i, float a, float b, float c, string uri) + public PointFact(float a, float b, float c, string uri) { - this.Id = i; this.Point = new Vector3(a, b, c); this.Normal = new Vector3(0, 1, 0); this.backendURI = uri; + } + public static PointFact parseFact(Scroll.ScrollFact fact) { + String uri = fact.@ref.uri; + float a = (float) ((OMF)((OMA)((Scroll.ScrollSymbolFact)fact).df).arguments[0]).f; + float b = (float) ((OMF)((OMA)((Scroll.ScrollSymbolFact)fact).df).arguments[1]).f; + float c = (float) ((OMF)((OMA)((Scroll.ScrollSymbolFact)fact).df).arguments[2]).f; + return new PointFact(a, b, c, uri); } } @@ -151,6 +168,23 @@ public LineFact(int i, int pid1, int pid2) this.backendURI = res.uri; Debug.Log(this.backendURI); } + + public LineFact(int Pid1, int Pid2, string backendURI) + { + this.Pid1 = Pid1; + this.Pid2 = Pid2; + this.backendURI = backendURI; + } + + public static LineFact parseFact(Scroll.ScrollFact fact) + { + String uri = fact.@ref.uri; + String pointAUri = ((OMS)((OMA)((Scroll.ScrollValueFact)fact).lhs).arguments[0]).uri; + String pointBUri = ((OMS)((OMA)((Scroll.ScrollValueFact)fact).lhs).arguments[1]).uri; + int pid1 = CommunicationEvents.Facts.Find(x => x.backendURI.Equals(pointAUri)).Id; + int pid2 = CommunicationEvents.Facts.Find(x => x.backendURI.Equals(pointBUri)).Id; + return new LineFact(pid1, pid2, uri); + } } public class OpenLineFact : Fact @@ -268,6 +302,26 @@ public AngleFact(int i, int pid1, int pid2, int pid3) this.backendURI = res.uri; Debug.Log(this.backendURI); } + + public AngleFact(int Pid1, int Pid2, int Pid3, string backendURI) + { + this.Pid1 = Pid1; + this.Pid2 = Pid2; + this.Pid3 = Pid3; + this.backendURI = backendURI; + } + + public static AngleFact parseFact(Scroll.ScrollFact fact) + { + String uri = fact.@ref.uri; + String pointAUri = ((OMS)((OMA)((Scroll.ScrollValueFact)fact).lhs).arguments[0]).uri; + String pointBUri = ((OMS)((OMA)((Scroll.ScrollValueFact)fact).lhs).arguments[1]).uri; + String pointCUri = ((OMS)((OMA)((Scroll.ScrollValueFact)fact).lhs).arguments[2]).uri; + int pid1 = CommunicationEvents.Facts.Find(x => x.backendURI.Equals(pointAUri)).Id; + int pid2 = CommunicationEvents.Facts.Find(x => x.backendURI.Equals(pointBUri)).Id; + int pid3 = CommunicationEvents.Facts.Find(x => x.backendURI.Equals(pointCUri)).Id; + return new AngleFact(pid1, pid2, pid3, uri); + } } diff --git a/Assets/Scripts/InventoryStuff/Scroll.cs b/Assets/Scripts/InventoryStuff/Scroll.cs index 5be3eb98..7239d379 100644 --- a/Assets/Scripts/InventoryStuff/Scroll.cs +++ b/Assets/Scripts/InventoryStuff/Scroll.cs @@ -7,8 +7,9 @@ using JsonSubTypes; using Newtonsoft.Json; -public class Scroll : LightScroll +public class Scroll { + public ScrollTheoryReference @ref; public string label; public string description; public List<ScrollFact> requiredFacts; @@ -33,32 +34,35 @@ public static void InitDynamicScroll(int n) FactOccurences = new List<KeyValuePair<int, int>>[n]; } - public class ScrollAssignment - { - public KeyValuePair<string, JSONManager.MMTTerm> assignment; - } - public class FilledScroll { - LightScroll scroll; - List<ScrollAssignment> assignments; + public ScrollTheoryReference scroll; + //public List<List<KeyValuePair<JSONManager.URI, JSONManager.MMTTerm>>> assignments; + public List<List<System.Object>> assignments; - public FilledScroll(LightScroll scroll, List<ScrollAssignment> assignments) + public FilledScroll(ScrollTheoryReference scroll, List<List<System.Object>> assignments) { this.scroll = scroll; this.assignments = assignments; } } + public class ScrollTheoryReference + { + public string problemTheory; + public string solutionTheory; + } [JsonConverter(typeof(JsonSubtypes), "kind")] [JsonSubtypes.KnownSubType(typeof(ScrollSymbolFact), "general")] [JsonSubtypes.KnownSubType(typeof(ScrollValueFact), "veq")] - public class ScrollFact + public abstract class ScrollFact { public string kind; public UriReference @ref; public string label; + + public abstract String getType(); } public class UriReference @@ -73,6 +77,13 @@ public class ScrollSymbolFact : ScrollFact { public MMTTerm tp; public MMTTerm df; + + public override String getType() { + if (this.tp is OMS) + return ((OMS)this.tp).uri; + else + return null; + } } /** @@ -80,23 +91,20 @@ public class ScrollSymbolFact : ScrollFact */ public class ScrollValueFact : ScrollFact { - MMTTerm lhs; - MMTTerm valueTp; - MMTTerm value; - MMTTerm proof; - } + public MMTTerm lhs; + public MMTTerm valueTp; + public MMTTerm value; + public MMTTerm proof; -} - -public class LightScroll -{ - public ScrollTheoryReference @ref; -} + public override String getType() + { + if (this.lhs is OMA & (((OMA)this.lhs).applicant is OMS)) + return ((OMS)((OMA)this.lhs).applicant).uri; + else + return null; + } + } -public class ScrollTheoryReference -{ - public string problemTheory; - public string solutionTheory; } diff --git a/Assets/Scripts/InventoryStuff/ScrollDetails.cs b/Assets/Scripts/InventoryStuff/ScrollDetails.cs index cb40cbbf..d3e1698e 100644 --- a/Assets/Scripts/InventoryStuff/ScrollDetails.cs +++ b/Assets/Scripts/InventoryStuff/ScrollDetails.cs @@ -3,6 +3,8 @@ using UnityEngine; using TMPro; using UnityEngine.Networking; +using Newtonsoft.Json; +using static ParsingDictionary; using System.Runtime.Serialization.Json; using System.Text; using System.Xml.Linq; @@ -30,12 +32,11 @@ public static string ScrollDescription } } - public string situationTheory = "http://BenniDoes.Stuff?SituationTheory"; - public Vector3 GetPosition(int i) { return new Vector3(x_Start, y_Start + i * (-y_Paece_Between_Items), 0f); } + // Start is called before the first frame update void Start() { @@ -45,12 +46,6 @@ void Start() } - // Update is called once per frame - void Update() - { - - } - public void setScroll(Scroll s) { Transform scrollView = gameObject.transform.GetChild(2); @@ -82,311 +77,71 @@ public void setScroll(Scroll s) public void magicButton() { - string view = sendView(); - if (view.Equals(FAIL)) + List<Scroll.ScrollFact> pushoutFacts = sendView(); + if (pushoutFacts == null) { Debug.Log("DAS HAT NICHT GEKLAPPT"); //TODO: hier ne Art PopUp, wo drin steht, dass das nicht geklappt hat CommunicationEvents.PushoutFactFailEvent.Invoke(null); return; } - string ret = pushout(view); - Debug.Log(ret); - + readPushout(pushoutFacts); } - string FAIL = "FAIL"; - class ViewResponse - { - public string view; - } - - - - private string sendView() - { - - List<Scroll.ScrollAssignment> assignments = new List<Scroll.ScrollAssignment>(); - Scroll.FilledScroll scroll = new Scroll.FilledScroll(this.scroll, assignments); - string body = Scroll.ToJSON(scroll); - - UnityWebRequest www = UnityWebRequest.Put(CommunicationEvents.ServerAdress+"/scroll/apply", body); - www.method = UnityWebRequest.kHttpVerbPOST; - www.SetRequestHeader("Content-Type", "application/json"); - var async = www.Send(); - while (!async.isDone) { } - - if (www.isNetworkError || www.isHttpError) - { - Debug.Log(www.error); - return FAIL; - } - else - { - string answer = www.downloadHandler.text; - return JsonUtility.FromJson<ViewResponse>(answer).view; - } - } - /* - * private string sendView() + private List<Scroll.ScrollFact> sendView() { - string jsonRequest = @"{"; - jsonRequest = jsonRequest + @" ""from"":""" + this.scroll.problemTheory + @""", "; - jsonRequest = jsonRequest + @" ""to"":""" + this.situationTheory + @""", "; - jsonRequest = jsonRequest + @" ""mappings"": { "; - - - for (int i = 0; i < ParameterDisplays.Length; i++) - { - Fact fact_i = ParameterDisplays[i].GetComponentInChildren<DropHandling>().currentFact; - var drophandler = ParameterDisplays[i].GetComponentInChildren<DropHandling>(); - Declaration decl_i = scroll.declarations[i]; - if (decl_i.value != null && fact_i.backendValueURI != null) - { - jsonRequest = jsonRequest + @" """ + decl_i.value + @""":""" + fact_i.backendValueURI + @""","; - } - jsonRequest = jsonRequest + @" """ + decl_i.identifier + @""":""" + fact_i.backendURI + @""","; - } - //removing the last ',' - jsonRequest = jsonRequest.Substring(0, jsonRequest.Length - 1); - jsonRequest = jsonRequest + "}}"; + Fact tempFact; + List<List<System.Object>> assignmentList = new List<List<System.Object>>(); - UnityWebRequest www = UnityWebRequest.Put("localhost:8081/view/add", jsonRequest); - www.method = UnityWebRequest.kHttpVerbPOST; - var async = www.Send(); - while (!async.isDone) { } - - if (www.isNetworkError || www.isHttpError) - { - Debug.Log(www.error); - return FAIL; - } - else - { - string answer = www.downloadHandler.text; - return JsonUtility.FromJson<ViewResponse>(answer).view; - } - } - private string sendView2() - { - Dictionary<Declaration, Fact[]> possibilities = new Dictionary<Declaration, Fact[]>(); - for (int i = 0; i < ParameterDisplays.Length; i++) - { - Fact fact_i = ParameterDisplays[i].GetComponentInChildren<DropHandling>().currentFact; - var drophandler = ParameterDisplays[i].GetComponentInChildren<DropHandling>(); - Declaration decl_i = scroll.declarations[i]; - if (fact_i is DirectedFact) + for(int i = 0; i < ParameterDisplays.Count; i++) { + List<System.Object> listEntry = new List<System.Object>(); + tempFact = ParameterDisplays[i].GetComponentInChildren<DropHandling>().currentFact; + if (tempFact != null) { - possibilities.Add(decl_i, new Fact[] { fact_i, ((DirectedFact)fact_i).flippedFact }); + listEntry.Add(new JSONManager.URI(this.scroll.requiredFacts[i].@ref.uri)); + listEntry.Add(new JSONManager.OMS(tempFact.backendURI)); } else { - possibilities.Add(decl_i, new Fact[] { fact_i }); - } - } - Dictionary<Declaration, Fact> selection = new Dictionary<Declaration, Fact>(); - for (int i = 0; i < possibilities.Count; i++) - { - Declaration decl_i = new List<Declaration>(possibilities.Keys)[i]; - selection[decl_i] = possibilities[decl_i][0]; - } - return testPossibilities(selection, possibilities, 0); - - return ""; - } - - private string testPossibilities(Dictionary<Declaration, Fact> selection, Dictionary<Declaration, Fact[]> possibilities, int i) - { - if (i == possibilities.Count) - { - return testVariant(selection); - } - else - { - Declaration decl_i = new List<Declaration>(possibilities.Keys)[i]; - for (int j = 0; j < possibilities[decl_i].Length; j++) - { - Fact fact_j = possibilities[decl_i][j]; - selection.Remove(decl_i); - selection.Add(decl_i, fact_j); - string ret = testPossibilities(selection, possibilities, i + 1); - if (ret != FAIL) return ret; - } - } - return FAIL; - } - - private string testVariant(Dictionary<Declaration, Fact> selection) - { - string jsonRequest = @"{"; - jsonRequest = jsonRequest + @" ""from"":""" + this.scroll.problemTheory + @""", "; - jsonRequest = jsonRequest + @" ""to"":""" + this.situationTheory + @""", "; - jsonRequest = jsonRequest + @" ""mappings"": { "; - for (int i = 0; i < selection.Count; i++) - { - Declaration decl_i = new List<Declaration>(selection.Keys)[i]; - Fact fact_i = selection[decl_i]; - if (decl_i.value != null && fact_i.backendValueURI != null) - { - jsonRequest = jsonRequest + @" """ + decl_i.value + @""":""" + fact_i.backendValueURI + @""","; + listEntry.Add(new JSONManager.URI(this.scroll.requiredFacts[i].@ref.uri)); + listEntry.Add(null); } - jsonRequest = jsonRequest + @" """ + decl_i.identifier + @""":""" + fact_i.backendURI + @""","; + assignmentList.Add(listEntry); } - //removing the last ',' - jsonRequest = jsonRequest.Substring(0, jsonRequest.Length - 1); - jsonRequest = jsonRequest + "}}"; + + Scroll.FilledScroll filledScroll = new Scroll.FilledScroll(this.scroll.@ref, assignmentList); + string body = Scroll.ToJSON(filledScroll); - UnityWebRequest www = UnityWebRequest.Put("localhost:8081/view/add", jsonRequest); + UnityWebRequest www = UnityWebRequest.Put(CommunicationEvents.ServerAdress+"/scroll/apply", body); www.method = UnityWebRequest.kHttpVerbPOST; + www.SetRequestHeader("Content-Type", "application/json"); var async = www.Send(); while (!async.isDone) { } if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); - return FAIL; - } - else - { - string answer = www.downloadHandler.text; - return JsonUtility.FromJson<ViewResponse>(answer).view; - } - }*/ - - - class PushoutReturn - { - public string newSituation; - public PushoutFact[] outputs; - } - - [System.Serializable] - public class PushoutFact - { - // generic class to make a common Supertype for all PushoutResponses - public string uri; - public string value; - public string a; - public string b; - public string c; - public string pointA; - public string pointB; - - public string left; - public string middle; - public string right; - - public bool isVector() - { - return a != null && - b != null && - c != null && - pointA == null && - pointB == null && - value == null && - left == null && - middle == null && - right == null; - } - public bool isDistance() - { - return a == null && - b == null && - c == null && - pointA != null && - pointB != null && - value != null && - left == null && - middle == null && - right == null; - } - public bool isAngle() - { - return a == null && - b == null && - c == null && - pointA == null && - pointB == null && - value != null && - left != null && - middle != null && - right != null; - } - } - - private string pushout(string view) - { - string path = "localhost:8081/pushout?"; - path = path + "problem=" + this.scroll.@ref.problemTheory + "&"; - path = path + "solution=" + this.scroll.@ref.solutionTheory + "&"; - path = path + "view=" + view; - UnityWebRequest www = UnityWebRequest.Get(path); - var async = www.Send(); - while (!async.isDone) { } - - if (www.isNetworkError || www.isHttpError) - { - Debug.Log(www.error); - //TODO: hier ne Art PopUp, wo drin steht, dass das nicht geklappt hat - return FAIL; + return null; } else { string answer = www.downloadHandler.text; - readPushout(answer); - - return "true"; - //return answer; - //TODO Parse Anwser from JSON TO FACTS... + Debug.Log(answer); + return JsonConvert.DeserializeObject<List<Scroll.ScrollFact>>(answer); } } - private void readPushout(string txt) + private void readPushout(List<Scroll.ScrollFact> pushoutFacts) { - Debug.Log(txt); - PushoutReturn ret = JsonUtility.FromJson<PushoutReturn>(txt); - this.situationTheory = ret.newSituation; FactManager factManager = cursor.GetComponent<FactManager>(); - for (int i = 0; i < ret.outputs.Length; i++) + for (int i = 0; i < pushoutFacts.Count; i++) { - PushoutFact f = ret.outputs[i]; - if (f.isVector()) - { - float a = float.Parse(f.a); - float b = float.Parse(f.b); - float c = float.Parse(f.c); - int id = factManager.GetFirstEmptyID(); - PointFact pf = new PointFact(id, a, b, c, f.uri); - CommunicationEvents.Facts.Insert(id, pf); - CommunicationEvents.AddFactEvent.Invoke(pf); - CommunicationEvents.PushoutFactEvent.Invoke(pf); - } - if (f.isDistance()) - { - int id = factManager.GetFirstEmptyID(); - int pid1 = getIdforBackendURI(f.pointA); - int pid2 = getIdforBackendURI(f.pointB); - LineFact lf = new LineFact();//id, pid1, pid2, f.uri, f.value); - CommunicationEvents.Facts.Insert(id, lf); - CommunicationEvents.AddFactEvent.Invoke(lf); - CommunicationEvents.PushoutFactEvent.Invoke(lf); - } - if (f.isAngle()) - { - int id = factManager.GetFirstEmptyID(); - int pid1 = getIdforBackendURI(f.left); - int pid2 = getIdforBackendURI(f.middle); - int pid3 = getIdforBackendURI(f.right); - AngleFact af = new AngleFact();//id, pid1, pid2, pid3, f.uri, f.value); - CommunicationEvents.Facts.Insert(id, af); - CommunicationEvents.AddFactEvent.Invoke(af); - CommunicationEvents.PushoutFactEvent.Invoke(af); - } + Fact newFact = ParsingDictionary.parseFactDictionary[pushoutFacts[i].getType()].Invoke(pushoutFacts[i]); + int id = factManager.GetFirstEmptyID(); + newFact.Id = id; + CommunicationEvents.Facts.Insert(id, newFact); + CommunicationEvents.AddFactEvent.Invoke(newFact); + CommunicationEvents.PushoutFactEvent.Invoke(newFact); } } - - private int getIdforBackendURI(string uri) - { - return CommunicationEvents.Facts.Find(x => x.backendURI.Equals(uri)).Id; - } } diff --git a/Assets/Scripts/JSONManager.cs b/Assets/Scripts/JSONManager.cs index 1cae7a2b..9677e16f 100644 --- a/Assets/Scripts/JSONManager.cs +++ b/Assets/Scripts/JSONManager.cs @@ -23,6 +23,16 @@ public static class JSONManager { //could init the strings of MMTURIs with JSON or other settings file instead public static MMTURICollection MMTURIs = new MMTURICollection(); + + public class URI + { + public string uri; + + public URI(string uri) + { + this.uri = uri; + } + } [JsonConverter(typeof(JsonSubtypes), "kind")] public class MMTTerm -- GitLab