001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.graph.io;
005    
006    import graphlab.graph.atributeset.EdgeAttrSet;
007    import graphlab.graph.atributeset.GraphAttrSet;
008    import graphlab.graph.atributeset.VertexAttrSet;
009    import graphlab.graph.graph.EdgeModel;
010    import graphlab.graph.graph.GraphModel;
011    import graphlab.graph.graph.VertexModel;
012    
013    import java.util.HashMap;
014    import java.util.Iterator;
015    import java.util.Map;
016    
017    /**
018     * Author: @Reza Mohammadi
019     */
020    public class GraphML {
021        public static HashMap<String, String> graphMLGraphKeys = new HashMap<String, String>();
022        public static HashMap<String, String> graphMLVertexKeys = new HashMap<String, String>();
023        public static HashMap<String, String> graphMLEdgeKeys = new HashMap<String, String>();
024    
025        public static String vertex2GraphML(VertexModel v) {
026            int id = v.getId();
027            String s = "    <node id=\"" + id + "\">\n";
028            for (String key : graphMLVertexKeys.keySet()) {
029                VertexAttrSet _ = new VertexAttrSet(v);
030                if (_.get(key) != null)
031                    s += "      <data key=\"n_" + key + "\">"
032                            + _.get(key).toString()
033                            + "</data>\n";
034            }
035            s += "    </node>\n";
036            return s;
037        }
038    
039        public static String edge2GraphML(EdgeModel e) {
040            String s = "";
041            String s2 = "";
042            if (e.getId() != null)
043                s = " id=\"" + e.getId() + "\"";
044            EdgeAttrSet _ = new EdgeAttrSet(e);
045    // edge direction now is setted only in graph
046    //        if ((_.get(EdgeAttrSet.DIRECTED) != null) && (e.model.getAttributes().get(EdgeModel.DIRECTED).toString().equalsIgnoreCase("true")))
047    //            s += " directed=\"true\"";
048            s2 = "    <edge source=\"" + e.source.getId()
049                    + "\" target=\"" + e.target.getId()
050                    + "\"" + s + ">\n";
051            for (String key : graphMLEdgeKeys.keySet()) {
052                if (_.get(key) != null)
053                    s2 += "      <data key=\"e_" + key + "\">"
054                            + _.get(key).toString()
055                            + "</data>\n";
056            }
057            s2 += "    </edge>\n";
058            return s2;
059        }
060    
061        public static String graph2GraphML(GraphModel g) {
062            initializeGMLKeys(g);
063            GraphAttrSet _ = new GraphAttrSet(g);
064            String graphML = graphMLKeys();
065            graphML +=
066                    "  <graph id=\"" + g.getLabel()
067                            + "\" edgedefault=\"" + (g.isDirected() ? GraphAttrSet.EDGEDEFAULT_DIRECTED : GraphAttrSet.EDGEDEFAULT_UNDIRECTED)
068                            + "\">\n";
069            for (String ss : graphMLGraphKeys.keySet()) {
070                if (!ss.equals(GraphAttrSet.EDGEDEFAULT))
071                    if (_.get(ss) != null)
072                        graphML += "    <data key=\"g_" + ss + "\">"
073                                + _.get(ss)
074                                + "</data>\n";
075            }
076            for (VertexModel v : g) {
077                graphML += GraphML.vertex2GraphML(v);
078            }
079            for (Iterator<EdgeModel> it = g.edgeIterator(); it.hasNext();) {
080                EdgeModel e = it.next();
081                graphML += GraphML.edge2GraphML(e);
082            }
083            graphML += "  </graph>\n";
084            return graphML;
085        }
086    //    public static String graphSelection2GraphML(GraphModel g, Collection<Vertex> v, Collection<Edge> e){
087    //        String graphML = graphMLKeys();
088    //        graphML +=
089    //                "  <graph id=\"" + g.getAttribute(Graph.ID)
090    //                + "\" edgedefault=\"" + (g.isDirected()?Graph.EDGEDEFAULT_DIRECTED:GraphModel.EDGEDEFAULT_UNDIRECTED)
091    //                + "\">\n";
092    //        for (String ss : Graph.graphMLGraphKeys.keySet()) {
093    //            if (ss!= Graph.EDGEDEFAULT)
094    //            if (g.getAttribute(ss) != null)
095    //                graphML += "    <data key=\"g_" + ss + "\">"
096    //                        + g.getAttribute(ss)
097    //                        + "</data>\n";
098    //        }
099    //        for (Vertex vv: v) {
100    //            graphML += GraphML.vertex2GraphML(vv);
101    //        }
102    //        for (Edge ee:e) {
103    //            graphML += GraphML.edge2GraphML(ee);
104    //        }
105    //        graphML += "  </graph>\n";
106    //        return graphML;
107    //
108    //    }
109    
110        public static String graphMLKeys() {
111            String s = "";
112            for (Map.Entry<String, String> e : graphMLGraphKeys.entrySet()) {
113                s += "  <key id=\"g_" + e.getKey() + "\" for=\"graph\" attr.name=\""
114                        + e.getKey() + "\" attr.type=\"" + e.getValue() + "\"/>\n";
115            }
116            for (Map.Entry<String, String> e : graphMLVertexKeys.entrySet()) {
117                s += "  <key id=\"n_" + e.getKey() + "\" for=\"node\" attr.name=\""
118                        + e.getKey() + "\" attr.type=\"" + e.getValue() + "\"/>\n";
119            }
120            for (Map.Entry<String, String> e : graphMLEdgeKeys.entrySet()) {
121                s += "  <key id=\"e_" + e.getKey() + "\" for=\"edge\" attr.name=\""
122                        + e.getKey() + "\" attr.type=\"" + e.getValue() + "\"/>\n";
123            }
124            return s;
125        }
126    
127        static void initializeGMLKeys(GraphModel g) {
128            graphMLEdgeKeys.clear();
129            graphMLGraphKeys.clear();
130            graphMLVertexKeys.clear();
131            for (VertexModel v : g) {
132                VertexAttrSet _ = new VertexAttrSet(v);
133                Map<String, Object> atr = _.getAttrs();
134                for (String name : atr.keySet()) {
135                    if (atr.get(name) != null)
136                        graphMLVertexKeys.put(name, atr.get(name).getClass().getName());
137                }
138            }
139            Iterator<EdgeModel> ie = g.edgeIterator();
140            while (ie.hasNext()) {
141                EdgeModel edge = ie.next();
142                Map<String, Object> atr = new EdgeAttrSet(edge).getAttrs();
143                for (String name : atr.keySet()) {
144                    if (atr.get(name) != null)
145                        graphMLEdgeKeys.put(name, atr.get(name).getClass().getName());
146                }
147            }
148            Map<String, Object> atr = new GraphAttrSet(g).getAttrs();
149            for (String name : atr.keySet()) {
150                if (atr.get(name) != null)
151                    graphMLGraphKeys.put(name, atr.get(name).getClass().getName());
152            }
153        }
154    }