SceneController.cs 4.87 KB
Newer Older
1
using UnityEngine;
2
using UnityEngine.SceneManagement;
3
using SimpleJSON;
4
using System.IO;
5
6
using System.Collections;
using System.Collections.Generic;
7
8
9
10
11
12

namespace dlu
{

public class SceneController : MonoBehaviour
{
13
14
    public string importInstructionFile = "Assets/DLU/Resources/instruction.json";

15
    [HideInInspector]
16
    public JSONArray instruction = null;
17
    private SemanticBehaviour[] _semanticBehaviours;
18
    private Dictionary<string, GameObject> sceneDict;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

    [HideInInspector]
    public SemanticBehaviour[] semanticBehaviours {
        get
        {
            return this._semanticBehaviours;
        }

        set
        {
            sceneDict = new Dictionary<string, GameObject>();
            foreach (SemanticBehaviour sb in value)
            {
                sceneDict[sb.GetComponent<InstanceIdentifier>().name] = sb.gameObject;
            }
            this._semanticBehaviours = value;
        }
    }
37

38
39
    public void Start()
    {
40
        if (instruction == null)
41
        {
42
            instruction = LoadInstruction(importInstructionFile);
43
        }
44
45
46
47
        if (semanticBehaviours == null)
        {
            semanticBehaviours = FindSemanticLabels();
        }
48
49
50
51
        if (!Application.isEditor)
        {
            SceneManager.LoadScene("UI", LoadSceneMode.Additive);
        }
52
        StartCoroutine(EvaluateInstruction());
53
    }
54

55
    public JSONArray LoadInstruction(string json)
56
    {
57
        return JSON.Parse(File.ReadAllText(json)) as JSONArray;
58
    }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

    public SemanticBehaviour[] FindSemanticLabels()
    {
        return GameObject.FindObjectsOfType<SemanticBehaviour>();
    }

    public IEnumerator EvaluateInstruction()
    {
        Dictionary<string, JSONObject> taskDict = new Dictionary<string, JSONObject>();
        JSONObject task = null;
        // Populate quick access dict and search for task
        foreach (JSONObject jo in instruction)
        {
            taskDict[jo["name"]] = jo;
            if (task == null && jo.HasKey("classifies"))
            {
                task = jo;
                Debug.Log($"Task identified: {task}");
            }
        }

        // If no task is found, stop
        if (task == null)
        {
            Debug.LogError("No task found!");
            yield return null;
        }
        else  // if task is found, execute actions
        {
            foreach (JSONNode action in task["classifies"])
            {
                yield return ExecuteAction(action, taskDict);
            }
        }
    }

    public IEnumerator ExecuteAction(string actionName, Dictionary<string, JSONObject> taskDict)
    {
        JSONObject action = taskDict[actionName];
        Debug.Log($"Performing action {actionName}: {action}");
        List<string> participants = new List<string>();
        foreach (JSONNode participant in action["hasParticipant"])
        {
            Debug.Log($"Participant: {participant}");
            foreach(JSONNode is_a in taskDict[participant]["is_a"])
            {
105
                // TODO(@shoeffner): This is not good: we should query the ontology and don't use is_a which are also PhysicalAgents etc., also just add participants once
106
107
108
                if (is_a != "http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#PhysicalAgent")
                {
                    participants.Add(participant);
109
                    break;
110
111
112
113
114
                }
            }
        }
        foreach (string participant in participants)
        {
115
            GameObject participantGO = sceneDict[participant];
116
            GameObject targetGO = new GameObject($"Target of {participant}");
117
118
119
120
121
122
123
124
125
126

            // Setup visuals
            GameObject renderer = new GameObject("RenderComponents");
            renderer.transform.SetParent(targetGO.transform);
            renderer.transform.localScale = participantGO.transform.lossyScale;
            MeshFilter mesh = renderer.AddComponent<MeshFilter>();
            mesh.mesh = participantGO.GetComponent<MeshFilter>().mesh;
            MeshRenderer mr = renderer.AddComponent<MeshRenderer>();
            mr.material = Resources.Load<Material>("Target");

127
128
129
130
131
132
133
134
135
136
            Vector3 position = taskDict[participant]["components"]["transform"]["position"];
            Quaternion orientation = taskDict[participant]["components"]["transform"]["orientation"];
            targetGO.transform.SetPositionAndRotation(position, orientation);
            double speed = taskDict[participant]["components"].HasKey("speed") ? taskDict[participant]["components"]["speed"].AsDouble : 1.0;

            Hashtable arguments = new Hashtable();
            arguments["name"] = actionName;
            arguments["position"] = targetGO.transform;
            arguments["speed"] = speed;

137
            iTween.MoveTo(participantGO, arguments);
138
139
140
        }
        yield return null;
    }
141
142
143
}

}