Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * TulipXmlGraphImport.cpp
- */
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cctype>
- #include <tulip/TulipPlugin.h>
- #include <QtXml/QXmlStreamReader>
- #include <QtCore/QFile>
- #include <QStringList>
- #include <QDebug>
- #include <QRegExp>
- using namespace tlp;
- using namespace std;
- namespace {
- const char * paramHelp[] = {
- // position x
- HTML_HELP_OPEN()
- HTML_HELP_DEF( "type", "string" )
- HTML_HELP_DEF( "values", "x" )
- HTML_HELP_DEF( "default", "x" )
- HTML_HELP_BODY()
- "Set the name of the GraphML property for x parameters layout"
- HTML_HELP_CLOSE(),
- // position y
- HTML_HELP_OPEN()
- HTML_HELP_DEF( "type", "string" )
- HTML_HELP_DEF( "values", "y" )
- HTML_HELP_DEF( "default", "y" )
- HTML_HELP_BODY()
- "Set the name of the GraphML property for y parameters layout"
- HTML_HELP_CLOSE(),
- // position z
- HTML_HELP_OPEN()
- HTML_HELP_DEF( "type", "string" )
- HTML_HELP_DEF( "values", "z" )
- HTML_HELP_DEF( "default", "z" )
- HTML_HELP_BODY()
- "Set the name of the GraphML property for z parameters layout"
- HTML_HELP_CLOSE(),
- // Properties to ignore
- HTML_HELP_OPEN()
- HTML_HELP_DEF( "type", "string" )
- HTML_HELP_BODY()
- "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."
- HTML_HELP_CLOSE(),
- };
- }
- class GraphML : public ImportModule {
- public :
- GraphML(AlgorithmContext context):ImportModule(context) {
- addParameter<string>("file::filename","");
- addParameter<string>("Map coordinate x",paramHelp[0], "x");
- addParameter<string>("Map coordinate y",paramHelp[1], "y");
- addParameter<string>("Map coordinate z",paramHelp[2], "z");
- addParameter<string>("Properties to ignore",paramHelp[3], "");
- }
- ~GraphML() {}
- bool import(const string &name) {
- string filename;
- dataSet->get<string>("file::filename", filename);
- dataSet->get<string>("Map coordinate x", xPropertyLabel);
- dataSet->get<string>("Map coordinate y", yPropertyLabel);
- dataSet->get<string>("Map coordinate z", zPropertyLabel);
- string tmp;
- dataSet->get<string>("Properties to ignore", tmp);
- propertiesToIgnore = QString::fromStdString(tmp).split(",");
- color = graph->getProperty<ColorProperty>("viewColor");
- layout = graph->getProperty<LayoutProperty>("viewLayout");
- labels = graph->getProperty<StringProperty>("viewLabel");
- id = graph->getProperty<StringProperty>("GraphML ID");
- QFile *xmlFile = new QFile(filename.c_str());
- xmlFile->open(QIODevice::ReadOnly | QIODevice::Text);
- QXmlStreamReader xmlReader(xmlFile);
- while (!xmlReader.atEnd()) {
- if (xmlReader.isStartElement() && xmlReader.name() == "key") {
- QString attributeId = xmlReader.attributes().value("id").toString();
- QString attributeName = xmlReader.attributes().value("attr.name").toString();
- QString attributeType = xmlReader.attributes().value("attr.type").toString();
- QString attributeFor = xmlReader.attributes().value("attr.for").toString();
- // Check if we ignore this properties
- if(checkProperty(attributeName) == true){
- PropertyInterface* newProp;
- if(attributeType == "double") {
- newProp = graph->getProperty<StringProperty>(attributeName.toStdString());
- }
- else if(attributeType == "int") {
- newProp = graph->getProperty<StringProperty>(attributeName.toStdString());
- }
- if(attributeType == "bool") {
- newProp = graph->getProperty<BooleanProperty>(attributeName.toStdString());
- }
- if(attributeType == "string") {
- newProp = graph->getProperty<StringProperty>(attributeName.toStdString());
- }
- if(attributeType == "float") {
- newProp = graph->getProperty<StringProperty>(attributeName.toStdString());
- }
- //TODO all other types
- if(newProp) {
- graphAttributes[attributeId] = newProp;
- }
- }
- }
- if (xmlReader.isStartElement() && xmlReader.name() == "node") {
- parseNode(xmlReader);
- }
- if (xmlReader.isStartElement() && xmlReader.name() == "edge") {
- parseEdge(xmlReader);
- }
- xmlReader.readNext();
- }
- node n;
- forEach(n, graph->getNodes()) {
- Coord nodeCoord(layout->getNodeValue(n));
- layout->setNodeValue(n, nodeCoord);
- }
- return true;
- }
- void parseNode(QXmlStreamReader &xmlReader) {
- Coord nodeCoord;
- string nodeLabel;
- QString nodeId = xmlReader.attributes().value("id").toString();
- node n = graph->addNode();
- id->setNodeValue(n, nodeId.toStdString());
- labels->setNodeValue(n, xmlReader.attributes().value("id").toString().toStdString());
- idToNode[nodeId] = n;
- QString viewLayoutValue = "(";
- while (!(xmlReader.isEndElement() && xmlReader.name() == "node")) {
- xmlReader.readNext();
- if (xmlReader.name() == "data" && xmlReader.attributes().hasAttribute("key")) {
- QString attributeName = xmlReader.attributes().value("key").toString();
- // Check if we ignore this properties
- if(checkProperty(attributeName) == true){
- map<QString, PropertyInterface*>::const_iterator it = graphAttributes.find(xmlReader.attributes().value("key").toString());
- if(it != graphAttributes.end()) {
- PropertyInterface* property = it->second;
- xmlReader.readNext();
- if (attributeName == QString::fromStdString(xPropertyLabel) ||
- attributeName == QString::fromStdString(yPropertyLabel) ||
- attributeName == QString::fromStdString(zPropertyLabel) )
- {
- viewLayoutValue += xmlReader.text().toString() + ",";
- }
- else
- property->setNodeStringValue(n, xmlReader.text().toString().toStdString());
- }
- }
- }
- }
- if(viewLayoutValue != "("){
- viewLayoutValue.remove(viewLayoutValue.size()-1, 1);
- viewLayoutValue += ")";
- LayoutProperty *prop = graph->getProperty<LayoutProperty>("viewLayout");
- prop->setNodeStringValue(n, viewLayoutValue.toStdString());
- }
- }
- void parseEdge(QXmlStreamReader &xmlReader) {
- QString sourceId = xmlReader.attributes().value("source").toString();
- QString targetId = xmlReader.attributes().value("target").toString();
- edge e = graph->addEdge(idToNode[sourceId], idToNode[targetId]);
- while (!(xmlReader.isEndElement() && xmlReader.name() == "edge")) {
- xmlReader.readNext();
- if (xmlReader.name() == "data" && xmlReader.attributes().hasAttribute("key")) {
- QString attributeName = xmlReader.attributes().value("key").toString();
- // Check if we ignore this properties
- if(checkProperty(attributeName) == true){
- map<QString, PropertyInterface*>::const_iterator it = graphAttributes.find(xmlReader.attributes().value("key").toString());
- if(it != graphAttributes.end()) {
- PropertyInterface* property = it->second;
- xmlReader.readNext();
- property->setEdgeStringValue(e, xmlReader.text().toString().toStdString());
- }
- }
- }
- }
- }
- /*
- Check if this property name is in propertiesToIgnore or match
- with regex in propertiesToIgnore
- */
- bool checkProperty(QString propToValid) {
- foreach (const QString &str, propertiesToIgnore) {
- QString attrName = QString(str).replace(" ", "");
- // Test if string are identic
- if(propToValid == attrName){
- return false;
- }
- // Test with regex
- QRegExp rx(attrName);
- if(rx.exactMatch(propToValid) == true){
- return false;
- }
- }
- return true;
- }
- private :
- map<QString, PropertyInterface*> graphAttributes;
- map<QString, node> idToNode;
- ColorProperty *color;
- LayoutProperty *layout;
- StringProperty *labels;
- StringProperty *id;
- string xPropertyLabel;
- string yPropertyLabel;
- string zPropertyLabel;
- QStringList propertiesToIgnore;
- };
- 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