Skip to content
Snippets Groups Projects
ScrollDetails.cs 11.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    using TMPro;
    
    using UnityEngine.Networking;
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Xml.Linq;
    
    
    public class ScrollDetails : MonoBehaviour
    {
    
    Richard Marcus's avatar
    Richard Marcus committed
        public WorldCursor cursor;
    
        public GameObject parameterDisplayPrefab;
    
        public Scroll scroll;
    
    
        public int x_Start;
        public int y_Start;
        public int y_Paece_Between_Items;
    
        public GameObject[] ParameterDisplays;
    
    
        public string situationTheory = "http://BenniDoes.Stuff?SituationTheory";
    
    
        public Vector3 GetPosition(int i)
        {
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            return new Vector3(x_Start, y_Start + i * (-y_Paece_Between_Items), 0f);
    
        // Start is called before the first frame update
        void Start()
        {
    
    Richard Marcus's avatar
    Richard Marcus committed
            if (cursor == null) cursor = GameObject.FindObjectOfType<WorldCursor>();
    
        }
    
        // Update is called once per frame
        void Update()
        {
    
    BenniHome's avatar
    BenniHome committed
        public void setScroll(Scroll s)
        {
    
            Transform scrollView = gameObject.transform.GetChild(2);
            Transform viewport = scrollView.GetChild(0);
    
            this.scroll = s;
    
            //wipe out old Displays
    
    BenniHome's avatar
    BenniHome committed
            for (int i = 0; i < this.ParameterDisplays.Length; i++)
            {
    
                Destroy(ParameterDisplays[i]);
            }
            this.ParameterDisplays = new GameObject[s.declarations.Length];
    
    BenniHome's avatar
    BenniHome committed
            for (int i = 0; i < s.declarations.Length; i++)
            {
    
                var obj = Instantiate(parameterDisplayPrefab, Vector3.zero, Quaternion.identity, transform);
    
    John Schihada's avatar
    John Schihada committed
                //obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
    
                obj.transform.GetChild(1).gameObject.GetComponent<TextMeshProUGUI>().text = s.declarations[i].description;
    
    John Schihada's avatar
    John Schihada committed
                obj.transform.SetParent(viewport.GetChild(0));
    
                //TODO: Remvoe this reaaaaly bad hack
    
    John Schihada's avatar
    John Schihada committed
                //obj.transform.localScale = Vector3.one;
    
                this.ParameterDisplays[i] = obj;
            }
            gameObject.transform.GetChild(1).gameObject.GetComponent<TextMeshProUGUI>().text = s.description;
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
    
    
    BenniHome's avatar
    BenniHome committed
        public void magicButton()
        {
            string view = sendView2();
            if (view.Equals(FAIL))
            {
    
                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);
    
    
        string FAIL = "FAIL";
    
    BenniHome's avatar
    BenniHome committed
        class ViewResponse
        {
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            public string view;
    
    BenniHome's avatar
    BenniHome committed
        private string sendView()
        {
    
            string jsonRequest = @"{";
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            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>();
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
                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 + "}}";
    
            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;
            }
        }
    
    
    BenniHome's avatar
    BenniHome committed
        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)
                {
                    possibilities.Add(decl_i, new Fact[] { fact_i, ((DirectedFact)fact_i).flippedFact });
                }
                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 + @""",";
                }
                jsonRequest = jsonRequest + @" """ + decl_i.identifier + @""":""" + fact_i.backendURI + @""",";
            }
            //removing the last ','
            jsonRequest = jsonRequest.Substring(0, jsonRequest.Length - 1);
            jsonRequest = jsonRequest + "}}";
    
            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;
            }
        }
    
    
    BenniHome's avatar
    BenniHome committed
        class PushoutReturn
        {
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            public string newSituation;
            public PushoutFact[] outputs;
        }
    
    BenniHome's avatar
    BenniHome committed
    
        [System.Serializable]
    
    BenniHome's avatar
    BenniHome committed
        public class PushoutFact
        {
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            // 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;
    
    BenniHome's avatar
    BenniHome committed
    
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            public string left;
            public string middle;
            public string right;
    
    
    BenniHome's avatar
    BenniHome committed
            public bool isVector()
            {
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
                return a != null &&
                    b != null &&
                    c != null &&
                    pointA == null &&
                    pointB == null &&
                    value == null &&
                    left == null &&
                    middle == null &&
    
    BenniHome's avatar
    BenniHome committed
                    right == null;
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            }
            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;
            }
        }
    
    
    BenniHome's avatar
    BenniHome committed
        private string pushout(string view)
        {
    
            string path = "localhost:8081/pushout?";
            path = path + "problem=" + this.scroll.problemTheory + "&";
            path = path + "solution=" + this.scroll.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;
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            }
    
                string answer = www.downloadHandler.text;
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
                readPushout(answer);
    
                return "true";
                //return answer;
    
                //TODO Parse Anwser from JSON TO FACTS...
            }
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
        }
    
    BenniHome's avatar
    BenniHome committed
        private void readPushout(string txt)
        {
    
    BenniHome's avatar
    BenniHome committed
            Debug.Log(txt);
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            PushoutReturn ret = JsonUtility.FromJson<PushoutReturn>(txt);
            this.situationTheory = ret.newSituation;
            FactManager factManager = cursor.GetComponent<FactManager>();
    
    BenniHome's avatar
    BenniHome committed
            for (int i = 0; i < ret.outputs.Length; i++)
            {
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
                PushoutFact f = ret.outputs[i];
    
    BenniHome's avatar
    BenniHome committed
                if (f.isVector())
                {
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
                    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);
    
    BenniHome's avatar
    BenniHome committed
                if (f.isDistance())
                {
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
                    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);
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
                    CommunicationEvents.Facts.Insert(id, lf);
                    CommunicationEvents.AddFactEvent.Invoke(lf);
    
                    CommunicationEvents.PushoutFactEvent.Invoke(lf);
    
    BenniHome's avatar
    BenniHome committed
                if (f.isAngle())
                {
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
                    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);
    
    BenniHome's avatar
    BenniHome committed
        private int getIdforBackendURI(string uri)
        {
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            return CommunicationEvents.Facts.Find(x => x.backendURI.Equals(uri)).Id;
        }