Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.19 KB | None | 0 0
  1. /*
  2.  * TulipXmlGraphImport.cpp
  3.  */
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. #include <cctype>
  9. #include <tulip/TulipPlugin.h>
  10.  
  11. #include <QtXml/QXmlStreamReader>
  12. #include <QtCore/QFile>
  13. #include <QStringList>
  14. #include <QDebug>
  15. #include <QRegExp>
  16.  
  17. using namespace tlp;
  18. using namespace std;
  19.  
  20. namespace {
  21.     const char * paramHelp[] = {
  22.         // position x
  23.         HTML_HELP_OPEN()
  24.         HTML_HELP_DEF( "type", "string" )
  25.         HTML_HELP_DEF( "values", "x" )
  26.         HTML_HELP_DEF( "default", "x" )
  27.         HTML_HELP_BODY()
  28.         "Set the name of the GraphML property for x parameters layout"
  29.         HTML_HELP_CLOSE(),
  30.  
  31.         // position y
  32.         HTML_HELP_OPEN()
  33.         HTML_HELP_DEF( "type", "string" )
  34.         HTML_HELP_DEF( "values", "y" )
  35.         HTML_HELP_DEF( "default", "y" )
  36.         HTML_HELP_BODY()
  37.         "Set the name of the GraphML property for y parameters layout"
  38.         HTML_HELP_CLOSE(),
  39.  
  40.         // position z
  41.         HTML_HELP_OPEN()
  42.         HTML_HELP_DEF( "type", "string" )
  43.         HTML_HELP_DEF( "values", "z" )
  44.         HTML_HELP_DEF( "default", "z" )
  45.         HTML_HELP_BODY()
  46.         "Set the name of the GraphML property for z parameters layout"
  47.         HTML_HELP_CLOSE(),
  48.  
  49.         // Properties to ignore
  50.         HTML_HELP_OPEN()
  51.         HTML_HELP_DEF( "type", "string" )
  52.         HTML_HELP_BODY()
  53.         "By default Tulip will import all parameters contained in GraphML file. You can choose to ignore some of them by enter their name. Note: you can use regular expression and you need to separate each properties / regex by a comma."
  54.         HTML_HELP_CLOSE(),
  55.     };
  56. }
  57.  
  58. class GraphML : public ImportModule {
  59.  
  60. public :
  61.  
  62.     GraphML(AlgorithmContext context):ImportModule(context) {
  63.         addParameter<string>("file::filename","");
  64.         addParameter<string>("Map coordinate x",paramHelp[0], "x");
  65.         addParameter<string>("Map coordinate y",paramHelp[1], "y");
  66.         addParameter<string>("Map coordinate z",paramHelp[2], "z");
  67.         addParameter<string>("Properties to ignore",paramHelp[3], "");
  68.  
  69.     }
  70.  
  71.     ~GraphML() {}
  72.  
  73.     bool import(const string &name) {
  74.  
  75.         string filename;
  76.         dataSet->get<string>("file::filename", filename);
  77.  
  78.         dataSet->get<string>("Map coordinate x", xPropertyLabel);
  79.         dataSet->get<string>("Map coordinate y", yPropertyLabel);
  80.         dataSet->get<string>("Map coordinate z", zPropertyLabel);
  81.  
  82.         string tmp;
  83.         dataSet->get<string>("Properties to ignore", tmp);
  84.         propertiesToIgnore = QString::fromStdString(tmp).split(",");
  85.  
  86.         color = graph->getProperty<ColorProperty>("viewColor");
  87.         layout = graph->getProperty<LayoutProperty>("viewLayout");
  88.         labels = graph->getProperty<StringProperty>("viewLabel");
  89.         id = graph->getProperty<StringProperty>("GraphML ID");
  90.  
  91.         QFile *xmlFile = new QFile(filename.c_str());
  92.         xmlFile->open(QIODevice::ReadOnly | QIODevice::Text);
  93.         QXmlStreamReader xmlReader(xmlFile);
  94.         while (!xmlReader.atEnd()) {
  95.  
  96.             if (xmlReader.isStartElement() && xmlReader.name() == "key") {
  97.  
  98.                 QString attributeId = xmlReader.attributes().value("id").toString();
  99.                 QString attributeName = xmlReader.attributes().value("attr.name").toString();
  100.                 QString attributeType = xmlReader.attributes().value("attr.type").toString();
  101.                 QString attributeFor = xmlReader.attributes().value("attr.for").toString();
  102.  
  103.                 // Check if we ignore this properties
  104.                 if(checkProperty(attributeName) == true){
  105.              
  106.                     PropertyInterface* newProp;
  107.  
  108.                     if(attributeType == "double") {
  109.                         newProp = graph->getProperty<StringProperty>(attributeName.toStdString());
  110.                     }
  111.                     else if(attributeType == "int") {
  112.                         newProp = graph->getProperty<StringProperty>(attributeName.toStdString());
  113.                     }
  114.                     if(attributeType == "bool") {
  115.                         newProp = graph->getProperty<BooleanProperty>(attributeName.toStdString());
  116.                     }
  117.                     if(attributeType == "string") {
  118.                         newProp = graph->getProperty<StringProperty>(attributeName.toStdString());
  119.                     }
  120.                     if(attributeType == "float") {
  121.                         newProp = graph->getProperty<StringProperty>(attributeName.toStdString());
  122.                     }
  123.                     //TODO all other types
  124.  
  125.                     if(newProp) {
  126.                         graphAttributes[attributeId] = newProp;
  127.                     }
  128.                 }
  129.             }
  130.             if (xmlReader.isStartElement() && xmlReader.name() == "node") {
  131.                 parseNode(xmlReader);
  132.             }
  133.  
  134.             if (xmlReader.isStartElement() && xmlReader.name() == "edge") {
  135.                 parseEdge(xmlReader);
  136.             }
  137.  
  138.             xmlReader.readNext();
  139.         }
  140.  
  141.         node n;
  142.         forEach(n, graph->getNodes()) {
  143.             Coord nodeCoord(layout->getNodeValue(n));
  144.             layout->setNodeValue(n, nodeCoord);
  145.         }
  146.  
  147.         return true;
  148.     }
  149.  
  150.     void parseNode(QXmlStreamReader &xmlReader) {
  151.         Coord nodeCoord;
  152.         string nodeLabel;
  153.         QString nodeId = xmlReader.attributes().value("id").toString();
  154.         node n = graph->addNode();
  155.         id->setNodeValue(n, nodeId.toStdString());
  156.         labels->setNodeValue(n, xmlReader.attributes().value("id").toString().toStdString());
  157.         idToNode[nodeId] = n;
  158.  
  159.         QString viewLayoutValue = "(";
  160.  
  161.         while (!(xmlReader.isEndElement() && xmlReader.name() == "node")) {
  162.             xmlReader.readNext();
  163.            
  164.             if (xmlReader.name() == "data" && xmlReader.attributes().hasAttribute("key")) {
  165.  
  166.                 QString attributeName = xmlReader.attributes().value("key").toString();
  167.  
  168.                 // Check if we ignore this properties
  169.                 if(checkProperty(attributeName) == true){
  170.                     map<QString, PropertyInterface*>::const_iterator it = graphAttributes.find(xmlReader.attributes().value("key").toString());
  171.                     if(it != graphAttributes.end()) {
  172.                         PropertyInterface* property = it->second;
  173.                         xmlReader.readNext();
  174.  
  175.                         if (attributeName == QString::fromStdString(xPropertyLabel) ||
  176.                             attributeName == QString::fromStdString(yPropertyLabel) ||
  177.                             attributeName == QString::fromStdString(zPropertyLabel) )
  178.                         {
  179.                             viewLayoutValue += xmlReader.text().toString() + ",";
  180.                         }
  181.                         else
  182.                             property->setNodeStringValue(n, xmlReader.text().toString().toStdString());
  183.                     }
  184.                 }
  185.             }
  186.         }
  187.  
  188.         if(viewLayoutValue != "("){
  189.  
  190.             viewLayoutValue.remove(viewLayoutValue.size()-1, 1);
  191.             viewLayoutValue += ")";
  192.  
  193.             LayoutProperty *prop = graph->getProperty<LayoutProperty>("viewLayout");
  194.             prop->setNodeStringValue(n, viewLayoutValue.toStdString());
  195.         }
  196.  
  197.     }
  198.  
  199.     void parseEdge(QXmlStreamReader &xmlReader) {
  200.         QString sourceId = xmlReader.attributes().value("source").toString();
  201.         QString targetId = xmlReader.attributes().value("target").toString();
  202.        
  203.         edge e = graph->addEdge(idToNode[sourceId], idToNode[targetId]);
  204.  
  205.         while (!(xmlReader.isEndElement() && xmlReader.name() == "edge")) {
  206.             xmlReader.readNext();
  207.             if (xmlReader.name() == "data" && xmlReader.attributes().hasAttribute("key")) {
  208.  
  209.                 QString attributeName = xmlReader.attributes().value("key").toString();
  210.  
  211.                 // Check if we ignore this properties
  212.                 if(checkProperty(attributeName) == true){
  213.  
  214.                     map<QString, PropertyInterface*>::const_iterator it = graphAttributes.find(xmlReader.attributes().value("key").toString());
  215.                     if(it != graphAttributes.end()) {
  216.                         PropertyInterface* property = it->second;
  217.                         xmlReader.readNext();
  218.                         property->setEdgeStringValue(e, xmlReader.text().toString().toStdString());
  219.                     }
  220.                 }
  221.             }
  222.         }
  223.     }
  224.    
  225.     /*
  226.        Check if this property name is in propertiesToIgnore or match
  227.        with regex in propertiesToIgnore
  228.     */
  229.     bool checkProperty(QString propToValid) {
  230.  
  231.         foreach (const QString &str, propertiesToIgnore) {
  232.  
  233.             QString attrName = QString(str).replace(" ", "");
  234.            
  235.             // Test if string are identic
  236.             if(propToValid == attrName){
  237.                 return false;
  238.             }
  239.  
  240.             // Test with regex
  241.             QRegExp rx(attrName);
  242.             if(rx.exactMatch(propToValid) == true){
  243.                 return false;
  244.             }
  245.         }
  246.        
  247.         return true;
  248.     }
  249.    
  250.  
  251. private :
  252.     map<QString, PropertyInterface*> graphAttributes;
  253.     map<QString, node> idToNode;
  254.     ColorProperty *color;
  255.     LayoutProperty *layout;
  256.     StringProperty *labels;
  257.     StringProperty *id;
  258.  
  259.     string xPropertyLabel;
  260.     string yPropertyLabel;
  261.     string zPropertyLabel;
  262.  
  263.     QStringList propertiesToIgnore;
  264. };
  265.  
  266. IMPORTPLUGINOFGROUP(GraphML,"GraphML Import","Hidden author","14/07/2011","GraphML Import Plugin","1.2","File")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement