Example #1
0
    public static CleanSceneData.Edge ConvertGraphEdgeToSceneEdge(GraphsonToGremlin.Edge graphE)
    {
        CleanSceneData.Edge newE = new CleanSceneData.Edge();
        newE.label = graphE.label;
        newE.type  = graphE.type;

        newE.instanceID     = graphE.id;
        newE.fromInstanceID = graphE.outV;
        newE.toInstanceID   = graphE.inV;

        newE.properties = new Dictionary <string, string>();
        if (graphE.properties != null)
        {
            foreach (KeyValuePair <string, string> p in graphE.properties)
            {
                if (p.Key != null && p.Value != null)
                {
                    newE.properties.Add(p.Key, p.Value);
                }
            }
        }
        return(newE);
    }
    public static async Task UploadToCosmosDB(string hostname, int port, string authKey, string database, string collection, GraphsonToGremlin.CosmosDbStructure structure, bool dropData = false)
    {
        try
        {
            var gremlinServer = new GremlinServer(hostname, port, enableSsl: true,
                                                  username: "******" + database + "/colls/" + collection,
                                                  password: authKey);
            var gremlinClient = new GremlinClient(gremlinServer, new GraphSON2Reader(), new GraphSON2Writer(), GremlinClient.GraphSON2MimeType);


            Dictionary <string, string> instanceIDToDBID = new Dictionary <string, string>();

            if (dropData)
            {
                Debug.Log("Dropping data");
                var result = await gremlinClient.SubmitWithSingleResultAsync <dynamic>("g.V().drop()");
            }

            foreach (var v in structure.vertices)
            {
                // Removing Unitys id from all vertices in order for CosmosDB to create IDs for us instead
                Debug.Log(String.Format("Running this query: {0}", v.ToString()));
                GraphsonToGremlin.Vertex cloneV = v;
                cloneV.id = "";
                string query = GraphsonToGremlin.ObjectToGremlinConverter.VertexToQuery(cloneV);

                //var resultSet = SubmitRequest(gremlinClient, query).Result;
                try
                {
                    // Create async task to execute the Gremlin query.
                    var resultSet = await gremlinClient.SubmitWithSingleResultAsync <dynamic>(query);

                    string vID = resultSet["id"];

                    instanceIDToDBID.Add(v.id, vID);
                    //Dictionary<int, GameObject> instanceIDs = UnityToGenerealUtility.GetEntitiesInstanceIDs();
                }
                catch
                {
                    Debug.LogError("Could not submit request");
                    GameObject.Find("Canvas").GetComponent <UserInterface>().ShowMessage("Could not submit request", Color.red);
                    return;
                }
            }

            foreach (var e in structure.edges)
            {
                // Removing Unitys id from all edges in order for CosmosDB to create IDs for us instead
                Debug.Log(String.Format("Running this query: {0}", e.ToString()));
                GraphsonToGremlin.Edge cloneE = e;
                cloneE.id   = "";
                cloneE.inV  = instanceIDToDBID[cloneE.inV];
                cloneE.outV = instanceIDToDBID[cloneE.outV];
                string query = GraphsonToGremlin.ObjectToGremlinConverter.EdgeToQuery(cloneE);

                try
                {
                    // Create async task to execute the Gremlin query.
                    var resultSet = await gremlinClient.SubmitWithSingleResultAsync <dynamic>(query);

                    //string eID = resultSet["id"];
                }
                catch
                {
                    Debug.LogError("Could not submit request");
                    GameObject.Find("Canvas").GetComponent <UserInterface>().ShowMessage("Could not submit request", Color.red);
                    return;
                }
            }

            GameObject.Find("Canvas").GetComponent <UserInterface>().ShowMessage("Successfully updated database", new Color(0, 0.5f, 0.02f));
            return;
        }
        catch
        {
            GameObject.Find("Canvas").GetComponent <UserInterface>().ShowMessage("Could not submit request", Color.red);
            return;
        }
    }
Example #3
0
    public GraphsonToGremlin.CosmosDbStructure SceneDataToCosmosDBStructure()
    {
        List <GameObject> nodeGOs     = entityManager.entities;
        List <Node>       nodes       = new List <Node>();
        List <Connect>    connections = new List <Connect>();

        FillNodeAndEdgeData(nodeGOs, ref nodes, ref connections);

        GraphsonToGremlin.CosmosDbStructure structure = new GraphsonToGremlin.CosmosDbStructure();
        structure.vertices = new List <GraphsonToGremlin.Vertex>();
        structure.edges    = new List <GraphsonToGremlin.Edge>();

        foreach (var n in nodes)
        {
            GraphsonToGremlin.Vertex v = new GraphsonToGremlin.Vertex();
            v.id    = n.instanceID.ToString();
            v.label = n.label;
            v.type  = n.label;

            v.property            = new GraphsonToGremlin.Properties();
            v.property.properties = new List <GraphsonToGremlin.Property>();

            if (n.propertiesDict != null)
            {
                foreach (var entry in n.propertiesDict)
                {
                    GraphsonToGremlin.Property p = new GraphsonToGremlin.Property();
                    p.id    = entry.Key;
                    p.value = entry.Value;
                    v.property.properties.Add(p);
                }
            }

            structure.vertices.Add(v);
        }

        foreach (var c in connections)
        {
            GraphsonToGremlin.Edge e = new GraphsonToGremlin.Edge();
            e.id    = c.instanceID.ToString();
            e.label = c.label;
            e.type  = c.label;

            e.inV       = c.fromInstanceID.ToString();
            e.inVLabel  = c.from.GetComponent <Node>().label;
            e.outV      = c.toInstanceID.ToString();
            e.outVLabel = c.to.GetComponent <Node>().label;

            if (c.propertiesDict != null)
            {
                foreach (var entry in c.propertiesDict)
                {
                    e.properties.Add(entry.Key, entry.Value);
                }
            }

            structure.edges.Add(e);
        }

        return(structure);
    }
        public static CosmosDbStructure GraphsonToObject(string jsonContent)
        {
            CosmosDbStructure data = new CosmosDbStructure();

            data.vertices = new List <Vertex>();
            data.edges    = new List <Edge>();

            UserInterface ui = GameObject.Find("Canvas").GetComponent <UserInterface>();

            try
            {
                //dynamic dynJson = fastJSON.JSON.Parse(jsonContent.Trim());
                List <object> dynJson = fastJSON.JSON.Parse(jsonContent) as List <object>;

                foreach (var items in dynJson)
                {
                    Dictionary <string, object> parsedObject = items as Dictionary <string, object>;

                    foreach (var item in parsedObject)
                    {
                        var key = item.Key.ToString();
                        var val = item.Value.ToString();

                        //return data;
                        //Debug.Log("key: " + item.Key.ToString());
                        //Debug.Log("val: " + item.Value.ToString());

                        try
                        {
                            if (key == "type" && val == "vertex")
                            {
                                // Iterate through items and save data to vertex...
                                // Creating vertex and save to vertex list
                                try
                                {
                                    Vertex v = new Vertex();
                                    foreach (var vertexItem in parsedObject)
                                    {
                                        if (vertexItem.Key == "id")
                                        {
                                            v.id = vertexItem.Value.ToString();
                                        }

                                        if (vertexItem.Key == "label")
                                        {
                                            v.label = vertexItem.Value.ToString();
                                        }

                                        if (vertexItem.Key == "type")
                                        {
                                            v.type = vertexItem.Value.ToString();
                                        }


                                        v.property = new Properties();
                                        if (vertexItem.Key == "properties")
                                        {
                                            v.property.properties = new List <Property>();

                                            Dictionary <string, object> _properties = vertexItem.Value as Dictionary <string, object>;

                                            v.property.properties = ReadProperties(ref _properties);
                                        }
                                    }

                                    data.vertices.Add(v);
                                }
                                catch
                                {
                                    Debug.Log("Could not parse vertex");
                                    ui.ShowMessage("Could not parse vertex", Color.red);
                                }
                            }
                            else if (key == "type" && val == "edge")
                            {
                                // Create edge and save to edge list
                                try
                                {
                                    Edge e = new Edge();
                                    foreach (var edgeItem in parsedObject)
                                    {
                                        if (edgeItem.Key == "id")
                                        {
                                            e.id = edgeItem.Value.ToString();
                                        }

                                        else if (edgeItem.Key == "label")
                                        {
                                            e.label = edgeItem.Value.ToString();
                                        }

                                        else if (edgeItem.Key == "type")
                                        {
                                            e.type = edgeItem.Value.ToString();
                                        }

                                        else if (edgeItem.Key == "inVLabel")
                                        {
                                            e.inVLabel = edgeItem.Value.ToString();
                                        }

                                        else if (edgeItem.Key == "outVLabel")
                                        {
                                            e.outVLabel = edgeItem.Value.ToString();
                                        }

                                        else if (edgeItem.Key == "inV")
                                        {
                                            e.inV = edgeItem.Value.ToString();
                                        }

                                        else if (edgeItem.Key == "outV")
                                        {
                                            e.outV = edgeItem.Value.ToString();
                                        }

                                        else if (edgeItem.Key == "properties")
                                        {
                                            Dictionary <string, object> _properties = edgeItem.Value as Dictionary <string, object>;

                                            foreach (object pp in _properties)
                                            {
                                                KeyValuePair <string, object> currentProp = (KeyValuePair <string, object>)pp;

                                                e.properties.Add(currentProp.Key, currentProp.Value.ToString());
                                            }

                                            //e.property.properties = ReadProperties(ref _properties);
                                        }
                                    }
                                    data.edges.Add(e);
                                }
                                catch
                                {
                                    Debug.Log("Could not parse edge");
                                    ui.ShowMessage("Could not parse edge", Color.red);
                                }
                            }
                        }

                        catch
                        {
                            Debug.Log("Something went when converting the following item: ");
                            Debug.Log(item.ToString());
                            Debug.Log("The data exported will probably include errors, would you like to try to procceed with the loading?: ");

                            ui.ShowMessage("Something went when converting the following item:" + item.ToString(), Color.red);
                            break;
                        }
                    }
                }
            }
            catch
            {
                Debug.LogError("Something went wrong when parsing json");
                ui.ShowMessage("Something went wrong when parsing json", Color.red);
                return(new CosmosDbStructure());
            }

            ui.ShowMessage("Successfully parsed data", new Color(0, 0.5f, 0.02f));
            return(data);
        }