Beispiel #1
0
        private static String ImportModel(XmlElement modelgraph, String modelname)
        {
            IDMap idmap       = new IDMap();
            int   nextenumval = 1000;

            foreach (XmlElement nodeelem in modelgraph.GetElementsByTagName("node"))
            {
                String nodetype = GetTypeName(nodeelem);

                // skip unknown elements
                int hashchar = nodetype.IndexOf('#');
                if (hashchar == -1 || !nodetype.Substring(0, hashchar).EndsWith("gxl-1.0.gxl"))
                {
                    continue;
                }

                String id = nodeelem.GetAttribute("id");
                nodetype = nodetype.Substring(hashchar + 1);
                switch (nodetype)
                {
                case "Bool":
                    idmap[id] = new Thing(id, ThingKind.Domain, "boolean");
                    break;

                case "Int":
                    idmap[id] = new Thing(id, ThingKind.Domain, "int");
                    break;

                case "Float":
                    idmap[id] = new Thing(id, ThingKind.Domain, "double");
                    break;

                case "String":
                    idmap[id] = new Thing(id, ThingKind.Domain, "string");
                    break;

                case "Enum":
                {
                    String name;
                    if (id.StartsWith("DM_enum_"))
                    {
                        name = id.Substring(8);
                    }
                    else
                    {
                        name = id;
                    }
                    idmap[id] = new Thing(id, ThingKind.EnumDomain, new EnumDomain(name));
                    break;
                }

                case "EnumVal":
                {
                    int val;
                    if (id.StartsWith("EV_"))
                    {
                        int ind = id.IndexOf('_', 4);
                        if (id[3] == '_')
                        {
                            val = -int.Parse(id.Substring(4, ind - 4));
                        }
                        else
                        {
                            val = int.Parse(id.Substring(3, ind - 3));
                        }
                    }
                    else
                    {
                        val = nextenumval++;
                    }

                    String name = GetGXLAttr(nodeelem, "value", "string");
                    idmap[id] = new Thing(id, ThingKind.EnumValue, new EnumMember(val, name));
                    break;
                }

                case "AttributeClass":
                {
                    String name = GetGXLAttr(nodeelem, "name", "string");
                    idmap[id] = new Thing(id, ThingKind.AttributeClass, new AttributeClass(name));
                    break;
                }

                case "NodeClass":
                {
                    String name       = GetGXLAttr(nodeelem, "name", "string");
                    bool   isabstract = GetGXLAttr(nodeelem, "isabstract", "bool") == "true";
                    idmap[id] = new Thing(id, ThingKind.NodeClass, new NodeClass(name, isabstract));
                    break;
                }

                case "EdgeClass":
                {
                    String name       = GetGXLAttr(nodeelem, "name", "string");
                    bool   isabstract = GetGXLAttr(nodeelem, "isabstract", "bool") == "true";
                    bool   isdirected = GetGXLAttr(nodeelem, "isdirected", "bool") == "true";
                    idmap[id] = new Thing(id, ThingKind.EdgeClass, new EdgeClass(name, isabstract, isdirected));
                    break;
                }
                }
            }

            foreach (XmlElement edgeelem in modelgraph.GetElementsByTagName("edge"))
            {
                String edgetype = GetTypeName(edgeelem);

                // skip unknown elements
                int hashchar = edgetype.IndexOf('#');
                if (hashchar == -1 || !edgetype.Substring(0, hashchar).EndsWith("gxl-1.0.gxl"))
                {
                    continue;
                }

                String fromid = edgeelem.GetAttribute("from");
                String toid   = edgeelem.GetAttribute("to");

                edgetype = edgetype.Substring(hashchar + 1);
                switch (edgetype)
                {
                case "hasDomain":
                {
                    AttributeClass attrClass = idmap[fromid].AttributeClass;
                    String         attrKind  = idmap[toid].AttributeKind;
                    attrClass.Type = attrKind;
                    break;
                }

                case "containsValue":
                {
                    EnumDomain enumDomain = idmap[fromid].EnumDomain;
                    EnumMember enumMember = idmap[toid].EnumValue;
                    enumDomain.Members.Add(enumMember);
                    break;
                }

                case "isA":
                {
                    NodeClass nodeClass = idmap[fromid].NodeOrEdgeClass;
                    nodeClass.SuperClasses.Add(toid);
                    break;
                }

                case "hasAttribute":
                {
                    NodeClass      nodeClass = idmap[fromid].NodeOrEdgeClass;
                    AttributeClass attrClass = idmap[toid].AttributeClass;
                    nodeClass.AttrList.Add(attrClass);
                    break;
                }
                }
            }

            String model         = BuildModel(idmap);
            String modelfilename = modelname + "__gxl.gm";

            using (StreamWriter writer = new StreamWriter(modelfilename))
                writer.Write(model);
            return(modelfilename);
        }
Beispiel #2
0
        /// <summary>
        /// Returns a string representation of the given scalar value
        /// </summary>
        /// <param name="value">The scalar of which to get the string representation</param>
        /// <param name="attrType">The attribute type</param>
        /// <param name="graph">The graph with the model and the element names</param>
        /// <returns>String representation of the scalar.</returns>
        private static string ToString(object value, AttributeType attrType, IGraph graph)
        {
            // enums are bitches, sometimes ToString gives the symbolic name, sometimes only the integer value
            // we always want the symbolic name, enforce this here
            if (attrType != null && attrType.Kind == AttributeKind.EnumAttr)
            {
                Debug.Assert(attrType.Kind != AttributeKind.SetAttr && attrType.Kind != AttributeKind.MapAttr);
                EnumAttributeType enumAttrType = attrType.EnumType;
                EnumMember        member       = enumAttrType[(int)value];
                if (member != null)
                {
                    return(member.Name);
                }
            }
            if (graph != null && value is Enum)
            {
                Debug.Assert(value.GetType().Name != "Dictionary`2" && value.GetType().Name != "List`1" && value.GetType().Name != "Deque`1");
                foreach (EnumAttributeType enumAttrType in graph.Model.EnumAttributeTypes)
                {
                    if (value.GetType() == enumAttrType.EnumType)
                    {
                        EnumMember member = enumAttrType[(int)value];
                        if (member != null)
                        {
                            return(member.Name);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            // for set/map entries of node/edge type return the persistent name
            if (attrType != null && (attrType.Kind == AttributeKind.NodeAttr || attrType.Kind == AttributeKind.EdgeAttr))
            {
                if (graph != null && value != null)
                {
                    return(((INamedGraph)graph).GetElementName((IGraphElement)value));
                }
            }
            if (value is IGraphElement)
            {
                if (graph != null)
                {
                    return(((INamedGraph)graph).GetElementName((IGraphElement)value));
                }
            }

            // we return "" for null as null is a valid string denoting the empty string in GrGen (dubious performance optimization)
            if (value == null)
            {
                return("");
            }
            else
            {
                if (value is double)
                {
                    return(((double)value).ToString(System.Globalization.CultureInfo.InvariantCulture));
                }
                else if (value is float)
                {
                    return(((float)value).ToString(System.Globalization.CultureInfo.InvariantCulture) + "f");
                }
                else
                {
                    return(value.ToString());
                }
            }
        }