IVertex InstanciateNode(Type nodeConcreteType, Guid nodeId, GraphObjectTypeInfo nodeType, string nodeContent, string lang)
        {
            List <object>       constructorParams = new List <object>();
            GraphObjectTypeInfo got = nodeType;
            string contentString    = nodeContent;

            Type    genericType   = nodeConcreteType;
            IVertex v             = null;
            Type    vertexContent = typeof(SerializableString);

            if (vertexContentTypeMap.ContainsKey(got.Id))
            {
                vertexContent = vertexContentTypeMap[got.Id];
            }

            IStringSerializable content = Activator.CreateInstance(vertexContent) as IStringSerializable;

            content.Deserialize(contentString);

            Type instanceType = nodeConcreteType;

            if (nodeConcreteType.IsGenericTypeDefinition)
            {
                instanceType = genericType.MakeGenericType(vertexContent);
            }
            v = Activator.CreateInstance(instanceType, new object[] { got, content, lang, nodeId }) as IVertex;

            return(v);
        }
Esempio n. 2
0
        public static string SerializeAsString(this IStringSerializable obj)
        {
            StringBuilder sb = new StringBuilder();

            obj.Serialize(sb);
            return(sb.ToString());
        }
    public static void WriteToTextSource(IStringSerializable s, TextAsset source) {
      if (source == null) {
        Debug.LogWarning("WriteToTextSource: failed to write because source is null!");
        return;
      }

#if UNITY_EDITOR
      string assetPath = AssetDatabase.GetAssetPath(source);
      File.WriteAllText(Application.dataPath +  assetPath.Replace("Assets", ""), s.SerializeToString());
      AssetDatabase.SaveAssets();
      AssetDatabase.Refresh();
#endif
    }
Esempio n. 4
0
        public Graph ReadGraph(StreamReader sr)
        {
            Graph graph = new Graph();

            GraphObjectTypeInfo[] aliasTable = new GraphObjectTypeInfo[0];
            IVertex[]             nodesTable = new IVertex[0];
            lineNumber = 0;

            while (!sr.EndOfStream)
            {
                lineNumber++;
                string   line  = sr.ReadLine();
                string[] parts = line.Split('|');
                switch (parts[0])
                {
                case "H":
                    HeaderInfo hInfo = ParseHeader(parts[1]);
                    aliasTable = new GraphObjectTypeInfo[hInfo.DeclaredTypes];
                    nodesTable = new IVertex[hInfo.Vertices];
                    break;

                case "A":
                    TypeInfo typeInfo = ParseTypeDeclaration(line);
                    aliasTable[typeInfo.LocalId] = new GraphObjectTypeInfo(typeInfo.ExternalId, typeInfo.Name, typeInfo.ObjectType);
                    break;

                case "N":
                    List <object>       constructorParams = new List <object>();
                    GraphObjectTypeInfo got = aliasTable[Int32.Parse(parts[2])];
                    string contentString    = parts[3];

                    // si un type deserialisable concret est enregistré
                    // on crée une instance 'i' de IStringSerializable à partir du type enregistré
                    // on désérialise la chaîne depuis 'i'
                    // on crée une instance de StringSerializableVertex dont on fixe le contenu à 'i'
                    // sinon
                    // on crée une instance de Vertex<string> et on y la chaîne 'content'

                    Type    genericType   = typeof(Vertex <>);
                    IVertex v             = null;
                    Type    vertexContent = typeof(SerializableString);
                    if (vertexContentTypeMap.ContainsKey(got.Id))
                    {
                        vertexContent = vertexContentTypeMap[got.Id];
                    }

                    IStringSerializable content = Activator.CreateInstance(vertexContent) as IStringSerializable;
                    content.Deserialize(contentString);
                    Type specializedGenericType = genericType.MakeGenericType(vertexContent);
                    v = Activator.CreateInstance(specializedGenericType, new object[] { got, content }) as IVertex;

                    nodesTable[Int32.Parse(parts[1])] = v;
                    break;

                case "E":
                    List <IVertex>      vertices = new List <IVertex>();
                    GraphObjectTypeInfo type     = aliasTable[Int32.Parse(parts[1])];
                    foreach (string v_str in parts[2].Split(','))
                    {
                        vertices.Add(nodesTable[long.Parse(v_str)]);
                    }
                    InstanciateEdge(type, typeof(HyperEdge), vertices);
                    break;
                }
            }

            foreach (IVertex node in nodesTable)
            {
                graph.AddVertex(node as IVertex);
            }
            return(graph);
        }