Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

graphml.cpp

Go to the documentation of this file.
00001 /*
00002  Modular Psi - A graph tracker of the Parapsychologcal scientific development
00003  Copyright (C) 2005 Leonardo Stern
00004 
00005  This program is free software; you can redistribute it and/or modify it under
00006  the terms of the Common Public License Version 1.0 or (at your option) any later version.
00007 
00008  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
00009  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010  See the Common Public License for more details.
00011 
00012  You should have received a copy of the Common Public License along with this program;
00013 */
00014 
00015 
00016 #include "graphml.hpp"
00017 #include "mpsi.hpp"
00018 #include "core/category.hpp"
00019 #include "../edgelist.hpp"
00020 #include "core/utils.hpp"
00021 #include <qdict.h>
00022 #include <qfile.h>
00023 #include <qregexp.h>
00024 #include <qiodevice.h>
00025 #include <qxml.h>
00026 
00027 GraphML::GraphML(PropositionGraph* cl)
00028 {
00029    caller = cl;
00030 }
00031 
00032 GraphML::~GraphML()
00033 {
00034 }
00035 
00036 
00037 bool GraphML::startElement(const QString &, const QString &,
00038                               const QString &qName,
00039                               const QXmlAttributes &attribs)
00040 {
00041         if (qName == "node")  {
00042                 currentItem = attribs.value("id");
00043                 caller->newProposition(currentItem);
00044 //              loadProp(currentItem);
00045         }
00046     return true;
00047 }
00048 
00049 bool GraphML::characters(const QString &str)
00050 {
00051     currentText += str;
00052     return true;
00053 }
00054 
00055 bool GraphML::endElement(const QString &, const QString &,
00056                             const QString &qName)
00057 {
00058         if (qName == "graphml")
00059         {
00060                 QDictIterator<Proposition> it ( *caller );
00061                 for( ; it.current(); ++it )      {
00062                         it.current()->setPublicName(it.current()->name());
00063                         loadProp(it.current()->name());
00064                 }
00065         }
00066    return true;
00067 }
00068 
00069 bool GraphML::fatalError(const QXmlParseException &exception)
00070 {
00071     qWarning("Line %d, column %d: %s", exception.lineNumber(),
00072              exception.columnNumber(), exception.message().ascii());
00073 
00074     return false;
00075 
00076 }
00077 
00078 void GraphML::ioError(const QFile file, const QString message)
00079 {
00080     caller->error(file.name() + message + ": " + file.errorString());
00081 }
00082 
00083 void GraphML::loadProp(QString pname)
00084 {
00085         Mpsi handler(caller,pname);
00086     QFile file("data/" + pname + ".mpsi");
00087     QXmlSimpleReader reader;
00088     reader.setContentHandler(&handler);
00089     reader.setErrorHandler(&handler);
00090     reader.parse(&file);
00091     file.close();
00092 }
00093 
00094 void GraphML::loadCat(QString dir)
00095 {
00096         QFile pfile(dir + "/cats.mpsi");
00097     if ( pfile.open( IO_ReadOnly ) ) {
00098         QRegExp rx("<(.*)>(.*)</.*>");
00099         QString line;
00100         Category *current;
00101         QTextStream stream( &pfile );
00102         while ( !stream.atEnd() ) {
00103                 line = stream.readLine();
00104                 rx.search(line);
00105                 if (rx.cap(1) == "id") {
00106                         current = new Category();
00107                         current->setId(rx.cap(2));
00108                 } else if (rx.cap(1) == "name") {
00109                         current->setName(rx.cap(2));
00110                 } else if (rx.cap(1) == "color") {
00111                         // FIXME Multiple Lines
00112                         QRegExp rz("(\\d*),(\\d*),(\\d*)");
00113                         rz.search(rx.cap(2));
00114                         current->setColor(QColor(rz.cap(1).toInt(),rz.cap(2).toInt(),rz.cap(3).toInt()));
00115                 } else if (rx.cap(1) == "description") {
00116                         current->setDescription(rx.cap(2));
00117                         caller->categories->insert(current->name(), current);
00118                 }
00119         }
00120         }
00121 }
00122 
00123 void GraphML::save(QString dir)
00124 {
00125     QFile wfile( dir + "/prop.graphml" );
00126     if ( wfile.open( IO_WriteOnly | IO_Translate ) ) {
00127         QTextStream stream( &wfile );
00128          QDictIterator<Proposition> it ( *caller );
00129          stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
00130                  << "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">\r\n"
00131                  << "  <graph id=\"G\" edgedefault=\"directed\">\r\n"
00132                  << "    <numnodes>" << caller->lastName() << "</numnodes>\r\n";
00133 
00134          for( ; it.current(); ++it )     {
00135              stream << "    <node id=\"" << it.current()->name() << "\"/>\r\n";
00136              saveProp(dir, it.current()->name());
00137          }
00138           stream << "  </graph>\r\n"
00139                   << "</graphml>";   
00140         
00141         wfile.close();
00142     }   
00143 }
00144 
00145 /*void GraphML::save(QString dir)
00146 {
00147     QFile wfile( dir + "/prop.graphml" );
00148     if ( wfile.open( IO_WriteOnly | IO_Translate ) ) {
00149         QTextStream stream( &wfile );
00150          QDictIterator<Proposition> it ( *caller );
00151          stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
00152                  << "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">\r\n"
00153                  << "  <key id=\"d0\" for=\"node\" attr.name=\"name\" attr.type=\"string\">\r\n"
00154                  << "    <default>\" \"</default>\r\n"
00155                  << "  </key>\r\n"
00156                  << "  <key id=\"d1\" for=\"edge\" attr.name=\"trust\" attr.type=\"float\">\r\n"
00157                  << "    <default>\"0\"</default>\r\n"
00158                  << "  </key>\r\n"
00159                  << "  <graph id=\"G\" edgedefault=\"directed\">\r\n"
00160                  << "    <numnodes>" << caller->lastName() << "</numnodes>\r\n";
00161 
00162          for( ; it.current(); ++it )     {
00163              stream << "    <node id=\"" << it.current()->name() << "\"";
00164              saveProp(dir, it.current()->name());
00165              if (it.current()->publicName().compare(" ") != 0) {
00166                  stream << ">\r\n      <data key=\"d0\">" << escapeXml(it.current()->publicName()) << "</data>\r\n    </node>\r\n";
00167              } else stream << "/>\r\n";
00168          }
00169              EdgeList::iterator ix = caller->edges->begin();
00170              while ( ix != caller->edges->end()) {
00171                  stream <<"    <edge source=\"" << (*ix)->sourceName()  << "\" target=\"" << (*ix)->targetName() <<  "\"";
00172                  if ( (*ix)->trust() != NULL) {
00173                      stream << ">\r\n"
00174                              << "      <data key=\"d1\">" << (*ix)->trust() << "</data>\r\n"
00175                              << "    </edge>\r\n";
00176                  } else stream << "/>\r\n";
00177                  ++ix;
00178              }
00179           stream << "  </graph>\r\n"
00180                   << "</graphml>";   
00181         
00182         wfile.close();
00183     }   
00184 }
00185 */
00186 
00187 void GraphML::saveProp(QString dir, QString prop)
00188 {
00189     Proposition* p = caller->operator[](prop);
00190     QFile wfile( dir + "/"+ prop + ".mpsi" );
00191     if ( wfile.open( IO_WriteOnly | IO_Translate ) ) {
00192         QTextStream stream( &wfile );
00193         stream 
00194                 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
00195                 << "<mpsi>\r\n"
00196                 << "    <id>" <<  p->name() << "</id>\r\n"
00197                 << "    <name>"  << escapeXml(p->publicName())  << "</name>\r\n"
00198                 << "    <description>" << escapeXml(p->description()) << "</description>\r\n"
00199                 << "    <category>" << p->category() <<  "</category>\r\n"
00200                 << "    <references>" << escapeXml(p->references()) << "</references>\r\n";
00201                 
00202                 EdgeList::iterator ix = p->outgoing->begin();
00203             while ( ix != p->outgoing->end()) {
00204                 stream
00205                         << "    <edge id=\"" << (*ix)->name() << "\" target=\"" << (*ix)->targetName() << "\">\r\n"
00206                 << "            <trust>" << (*ix)->trust() <<  "</trust>\r\n"
00207                 << "    </edge>\r\n";
00208                 ++ix;
00209             }
00210                 stream << "</mpsi>";
00211                 // TODO Keywords e ChangeLog
00212         caller->operator[](prop)->setChanged(false);
00213     }
00214 }
Untitled Document Pesquisa Psi SourceForge.net Logo