001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.io;
025    
026    import java.io.File;
027    import java.io.IOException;
028    import java.io.OutputStream;
029    import java.io.OutputStreamWriter;
030    import java.io.Writer;
031    
032    import org.ujmp.core.Matrix;
033    import org.ujmp.core.util.io.IntelligentFileWriter;
034    
035    public class ExportMatrixGML {
036    
037            public static final void toFile(File file, Matrix m, Object... parameters) throws IOException {
038                    IntelligentFileWriter w = new IntelligentFileWriter(file);
039                    toWriter(w, m, parameters);
040                    w.close();
041            }
042    
043            public static final void toStream(OutputStream out, Matrix m, Object... parameters) throws IOException {
044                    OutputStreamWriter w = new OutputStreamWriter(out);
045                    toWriter(w, m, parameters);
046                    w.close();
047            }
048    
049            public static final void toWriter(Writer w, Matrix m, Object... parameters) throws IOException {
050                    w.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
051                    w.write("<graphml>\n");
052                    w.write("<key id=\"k0\" for=\"node\" attr.name=\"variableName\" attr.type=\"string\"></key>\n");
053                    w.write("<key id=\"k1\" for=\"node\" attr.name=\"description\" attr.type=\"string\"></key>\n");
054                    w
055                                    .write("<key id=\"k2\" for=\"edge\" attr.name=\"confidence\" attr.type=\"double\"><default>0.0</default></key>\n");
056                    w
057                                    .write("<key id=\"k3\" for=\"edge\" attr.name=\"directed\" attr.type=\"boolean\"><default>false</default></key>\n");
058                    w.write("<key id=\"k5\" for=\"node\" attr.name=\"xPos\" attr.type=\"string\"></key>\n");
059                    w.write("<key id=\"k6\" for=\"node\" attr.name=\"yPos\" attr.type=\"string\"></key>\n");
060    
061                    w.write("<graph id=\"" + m.getLabel() + "\" edgedefault=\"undirected\">\n");
062    
063                    for (int i = 0; i < m.getRowCount(); i++) {
064                            w.write("<node id=\"node" + i + "\">\n");
065                            w.write("<data key=\"k0\">" + m.getRowLabel(i).replaceAll("[<>&]", "") + "</data>\n");
066                            // w.write("<data key=\"k5\">" + n.getPosX() + "</data>\n");
067                            // w.write("<data key=\"k6\">" + n.getPosY() + "</data>\n");
068                            w.write("</node>\n");
069                    }
070    
071                    int id = 0;
072                    for (long[] c : m.allCoordinates()) {
073                            if (m.getAsDouble(c) > 0.0) {
074                                    w.write("<edge id=\"edge" + (id++) + "\" ");
075                                    w.write("source=\"node" + c[Matrix.ROW] + "\" ");
076                                    w.write("target=\"node" + c[Matrix.COLUMN] + "\">\n");
077                                    w.write("</edge>\n");
078                            }
079                    }
080                    w.write("</graph>\n");
081                    w.write("</graphml>");
082                    w.close();
083            }
084    
085    }