Esempio n. 1
0
        /////////////////////////////////////////////////////////////////////////////////

        private IGraph ImportModels(List <String> ecoreFilenames, String grgFilename, IBackend backend, out IActions actions)
        {
            foreach (String ecoreFilename in ecoreFilenames)
            {
                String modelText = ParseModel(ecoreFilename);

                String modelfilename = ecoreFilename.Substring(0, ecoreFilename.LastIndexOf('.')) + "__ecore.gm";

                // Do we have to update the model file (.gm)?
                if (!File.Exists(modelfilename) || File.GetLastWriteTime(ecoreFilename) > File.GetLastWriteTime(modelfilename))
                {
                    Console.WriteLine("Writing model file \"" + modelfilename + "\"...");
                    using (StreamWriter writer = new StreamWriter(modelfilename))
                        writer.Write(modelText);
                }
            }

            if (grgFilename == null)
            {
                grgFilename = "";
                foreach (String ecoreFilename in ecoreFilenames)
                {
                    grgFilename += ecoreFilename.Substring(0, ecoreFilename.LastIndexOf('.')) + "_";
                }
                grgFilename += "_ecore.grg";

                StringBuilder sb = new StringBuilder();
                sb.Append("// Automatically generated\n// Do not change, changes will be lost!\n\nusing ");

                DateTime grgTime;
                if (!File.Exists(grgFilename))
                {
                    grgTime = DateTime.MinValue;
                }
                else
                {
                    grgTime = File.GetLastWriteTime(grgFilename);
                }

                bool mustWriteGrg = false;

                bool first = true;
                foreach (String ecore in ecoreFilenames)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(", ");
                    }
                    sb.Append(ecore.Substring(0, ecore.LastIndexOf('.')) + "__ecore");

                    if (File.GetLastWriteTime(ecore) > grgTime)
                    {
                        mustWriteGrg = true;
                    }
                }

                if (mustWriteGrg)
                {
                    sb.Append(";\n");
                    using (StreamWriter writer = new StreamWriter(grgFilename))
                        writer.Write(sb.ToString());
                }
            }

            IGraph graph;

            backend.CreateFromSpec(grgFilename, "defaultname", null,
                                   ProcessSpecFlags.UseNoExistingFiles, new List <String>(),
                                   out graph, out actions);
            return(graph);
        }
Esempio n. 2
0
        /////////////////////////////////////////////////////////////////////////////////

        private IGraph ImportModels(List<String> ecoreFilenames, String grgFilename, IBackend backend, out IActions actions)
        {
            foreach(String ecoreFilename in ecoreFilenames)
            {
                String modelText = ParseModel(ecoreFilename);

                String modelfilename = ecoreFilename.Substring(0, ecoreFilename.LastIndexOf('.')) + "__ecore.gm";

                // Do we have to update the model file (.gm)?
                if(!File.Exists(modelfilename) || File.GetLastWriteTime(ecoreFilename) > File.GetLastWriteTime(modelfilename))
                {
                    Console.WriteLine("Writing model file \"" + modelfilename + "\"...");
                    using(StreamWriter writer = new StreamWriter(modelfilename))
                        writer.Write(modelText);
                }
            }

            if(grgFilename == null)
            {
                grgFilename = "";
                foreach(String ecoreFilename in ecoreFilenames)
                    grgFilename += ecoreFilename.Substring(0, ecoreFilename.LastIndexOf('.')) + "_";
                grgFilename += "_ecore.grg";

                StringBuilder sb = new StringBuilder();
                sb.Append("// Automatically generated\n// Do not change, changes will be lost!\n\nusing ");

                DateTime grgTime;
                if(!File.Exists(grgFilename)) grgTime = DateTime.MinValue;
                else grgTime = File.GetLastWriteTime(grgFilename);

                bool mustWriteGrg = false;

                bool first = true;
                foreach(String ecore in ecoreFilenames)
                {
                    if(first) first = false;
                    else sb.Append(", ");
                    sb.Append(ecore.Substring(0, ecore.LastIndexOf('.')) + "__ecore");

                    if(File.GetLastWriteTime(ecore) > grgTime)
                        mustWriteGrg = true;
                }

                if(mustWriteGrg)
                {
                    sb.Append(";\n");
                    using(StreamWriter writer = new StreamWriter(grgFilename))
                        writer.Write(sb.ToString());
                }
            }

            IGraph graph;
            backend.CreateFromSpec(grgFilename, "defaultname", null,
                ProcessSpecFlags.UseNoExistingFiles, new List<String>(), 
                out graph, out actions);
            return graph;
        }
Esempio n. 3
0
        /// <summary>
        /// Imports the first graph not being of type "gxl-1.0" from the given text reader input stream.
        /// Any errors will be reported by exception.
        /// </summary>
        /// <param name="inStream">The text reader input stream import source.</param>
        /// <param name="modelOverride">If not null, overrides the filename of the graph model to be used.</param>
        /// <param name="backend">The backend to use to create the graph.</param>
        /// <param name="actions">Receives the actions object in case a .grg model is given.</param>
        public static IGraph Import(TextReader inStream, String modelOverride, IBackend backend, out IActions actions)
        {
            XmlDocument doc = new XmlDocument();

            doc.XmlResolver = null;
            doc.Load(inStream);

            XmlElement gxlelem = doc["gxl"];

            if (gxlelem == null)
            {
                throw new Exception("The document has no gxl element.");
            }

            XmlElement graphelem = null;
            String     graphtype = null;

            foreach (XmlElement curgraphnode in gxlelem.GetElementsByTagName("graph"))
            {
                graphtype = GetTypeName(curgraphnode);
                if (!graphtype.EndsWith("gxl-1.0.gxl#gxl-1.0"))
                {
                    if (graphelem != null)
                    {
                        throw new Exception("More than one instance graph included (not yet supported)!");
                    }
                    graphelem = curgraphnode;
                }
            }
            if (graphelem == null)
            {
                throw new Exception("No non-meta graph found!");
            }

            String modelfilename;

            if (modelOverride == null)
            {
                XmlDocument modeldoc;
                int         hashindex = graphtype.IndexOf('#');
                if (hashindex <= 0)
                {
                    modeldoc = doc;
                }
                else
                {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.ProhibitDtd = false;
                    XmlReader reader = XmlReader.Create(graphtype.Substring(0, hashindex), settings);
                    modeldoc = new XmlDocument();
                    modeldoc.Load(reader);
                }

                String     localname = graphtype.Substring(hashindex + 1);
                XmlElement modelnode = (XmlElement)modeldoc.SelectSingleNode(
                    "descendant::graph/node[@id='" + localname + "']");
                if (modelnode == null)
                {
                    throw new Exception("Graph schema \"" + graphtype + "\" not found.");
                }
                if (!GetTypeName(modelnode).EndsWith("gxl-1.0.gxl#GraphClass"))
                {
                    throw new Exception("Graph type \"" + graphtype + "\" must refer to a GraphClass node.");
                }

                String modelname = GetGXLAttr(modelnode, "name", "string");
                if (modelname.EndsWith("__gxl"))
                {
                    modelname = modelname.Substring(0, modelname.Length - 5);
                }
                XmlElement modelgraph = (XmlElement)modelnode.ParentNode;
                modelfilename = ImportModel(modelgraph, modelname);
            }
            else
            {
                modelfilename = modelOverride;
            }

            IGraph graph;
            String graphname = graphelem.GetAttribute("id");

            if (modelfilename.EndsWith(".grg"))
            {
                backend.CreateFromSpec(modelfilename, graphname, null,
                                       ProcessSpecFlags.UseNoExistingFiles, new List <String>(),
                                       out graph, out actions);
            }
            else
            {
                graph   = backend.CreateGraph(modelfilename, graphname);
                actions = null;
            }

            ImportGraph(graph, doc, graphelem);

            return(graph);
        }