1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
|
/**
*
* This file is part of Tulip (www.tulip-software.org)
*
* Authors: David Auber and the Tulip development Team
* from LaBRI, University of Bordeaux
*
* Tulip is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Tulip is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <tulip/ImportModule.h>
#include <tulip/TulipPluginHeaders.h>
#include <tulip/ForEach.h>
#include <tulip/TulipViewSettings.h>
#include <QXmlStreamReader>
#include <QFile>
using namespace tlp;
using namespace std;
/** \file
* \brief - Import GEXF format graph file.
* This plugin imports a graph from a file in GEXF format,<br/>
* as it is described in the XML Schema 1.2draft (http://gexf.net/format/schema.html). Dynamic mode is not yet supported.
* <b>HISTORY</b>
*
* - 04/06/2012 Version 1.0: Initial release
*
* \author Antoine Lambert & Patrick Mary of Tulip Team http://tulip-software.org/
*
* <b>LICENCE</b>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
static const char * paramHelp[] = {
// filename
HTML_HELP_OPEN() \
HTML_HELP_DEF( "type", "pathname" ) \
HTML_HELP_BODY() \
"This parameter defines the file pathname to import." \
HTML_HELP_CLOSE(),
// curved edges
HTML_HELP_OPEN() \
HTML_HELP_DEF( "type", "bool" ) \
HTML_HELP_DEF( "values", "true, false" ) \
HTML_HELP_DEF( "default", "false" ) \
HTML_HELP_BODY() \
"Indicates if Bezier curves will be used to draw the edges." \
HTML_HELP_CLOSE(),
};
class GEXFImport : public ImportModule {
public :
PLUGININFORMATION("GEXF","Antoine LAMBERT","05/05/2010","Imports a new graph from a file in the GEXF input format.","1.0","File")
GEXFImport(const PluginContext* context):ImportModule(context),
viewLayout(NULL), viewSize(NULL), viewColor(NULL), viewLabel(NULL),
viewShape(NULL), nodesHaveCoordinates(false) {
// add a file parameter for the plugin
addInParameter<string>("file::filename", paramHelp[0],"");
addInParameter<bool>("Curved edges",paramHelp[1], "false");
}
~GEXFImport() {}
std::string icon() const {
return ":/tulip/graphperspective/icons/32/import_gephi.png";
}
std::list<std::string> fileExtensions() const {
std::list<std::string> l;
l.push_back("gexf");
return l;
}
// import plugins must implement bool importGraph()
bool importGraph() {
string filename;
bool curvedEdges = false;
// get the filename choosed by the user
dataSet->get<string>("file::filename", filename);
dataSet->get<bool>("Curved edges", curvedEdges);
QString qfilename = QString::fromUtf8(filename.c_str());
// if wrong extension, abort
if (!qfilename.endsWith(".gexf")) {
return false;
}
// get Tulip visual attributes properties associated to the empty graph we want to fill
viewLayout = graph->getProperty<LayoutProperty>("viewLayout");
viewLabel = graph->getProperty<StringProperty>("viewLabel");
viewSize = graph->getProperty<SizeProperty>("viewSize");
viewColor = graph->getProperty<ColorProperty>("viewColor");
viewShape = graph->getProperty<IntegerProperty>("viewShape");
nodesHaveCoordinates = false;
// Open the GEXF file choosed by the user
QFile *xmlFile = new QFile(qfilename);
if (!xmlFile->open(QIODevice::ReadOnly | QIODevice::Text)) {
// get error
pluginProgress->setError(xmlFile->errorString().toUtf8().data());
return false;
}
// Instantiate a QXmlStreamReader to parse the file (GEXF is xml)
QXmlStreamReader xmlReader(xmlFile);
// Parse each line of the file
while (!xmlReader.atEnd()) {
if (xmlReader.readNextStartElement()) {
// only static graph are supported
if (xmlReader.name() == "graph") {
string mode =
xmlReader.attributes().value("mode").toString().toStdString();
if (mode == "dynamic") {
pluginProgress->setError("dynamic graph is not yet supported");
return false;
}
}
// create Tulip Properties from Gephi attributes
else if (xmlReader.name() == "attributes") {
createPropertiesFromAttributes(xmlReader);
}
// parse graph node data
else if (xmlReader.name() == "nodes") {
createNodes(xmlReader, graph);
}
// parse graph edge data
else if (xmlReader.name() == "edges") {
createEdges(xmlReader);
}
}
}
delete xmlFile;
// Special case : some GEXF files declare edges before nodes
// so we have to add edges once nodes have been parsed
for (size_t i = 0 ; i < edgesTmp.size() ; ++i) {
graph->addEdge(nodesMap[edgesTmp[i].first], nodesMap[edgesTmp[i].second]);
}
// nodes shape will be circle
viewShape->setAllNodeValue(NodeShape::Circle);
// add subgraph nodes, edges and meta nodes if needed
Graph*quotientGraph = addSubGraphsNodes();
if (quotientGraph) {
addSubGraphsEdges();
computeMetaNodes(quotientGraph);
}
// Set edges to be rendered as Cubic Bézier curves and
// compute curve control points for each edge
if (curvedEdges && nodesHaveCoordinates) {
viewShape->setAllEdgeValue(EdgeShape::BezierCurve);
curveGraphEdges();
}
return true;
}
// Create a set of Tulip Properties from the attributes declared in the GEXF file
// according to data types
void createPropertiesFromAttributes(QXmlStreamReader &xmlReader) {
bool nodeProperties = xmlReader.attributes().value("class") == "node";
map<string, PropertyInterface *> *propertiesMap = NULL;
if (nodeProperties) {
propertiesMap = &nodePropertiesMap;
}
else {
propertiesMap = &edgePropertiesMap;
}
while (!(xmlReader.isEndElement() && xmlReader.name() == "attributes")) {
xmlReader.readNext();
// create a Tulip property and store mapping between attribute id and property
if (xmlReader.isStartElement() && xmlReader.name() == "attribute") {
string attributeId = xmlReader.attributes().value("id").toString().toStdString();
string attributeName = xmlReader.attributes().value("title").toString().toUtf8().data();
string attributeType = xmlReader.attributes().value("type").toString().toStdString();
if (attributeType == "string") {
(*propertiesMap)[attributeId] = graph->getProperty<StringProperty>(attributeName);
}
else if (attributeType == "float" || attributeType == "double") {
(*propertiesMap)[attributeId] = graph->getProperty<DoubleProperty>(attributeName);
}
else if (attributeType == "integer") {
(*propertiesMap)[attributeId] = graph->getProperty<IntegerProperty>(attributeName);
}
else if (attributeType == "boolean") {
(*propertiesMap)[attributeId] = graph->getProperty<BooleanProperty>(attributeName);
}
}
}
}
// create nodes
void createNodes(QXmlStreamReader &xmlReader, Graph* g) {
while (!(xmlReader.isEndElement() && xmlReader.name() == "nodes")) {
xmlReader.readNext();
// must be a node
if (xmlReader.isStartElement() && xmlReader.name() == "node")
parseNode(xmlReader, g);
}
}
// create edges
void createEdges(QXmlStreamReader &xmlReader) {
while (!(xmlReader.isEndElement() && xmlReader.name() == "edges")) {
xmlReader.readNext();
// must be an edge
if (xmlReader.isStartElement() && xmlReader.name() == "edge")
parseEdge(xmlReader);
}
}
Graph* addInParent(node n, const string& pid) {
// get the parent node
// to find the corresponding sub graph
// in which the current node must be added
node pn;
if (nodesMap.find(pid) == nodesMap.end()) {
// create a fake meta node
pn = graph->addNode();
nodesMap[pid] = pn;
}
else
pn = nodesMap[pid];
Graph* sg = (Graph*) nodeToSubgraph.get(pn.id);
if (sg == NULL) {
// add a subgraph for the fake meta node
sg = graph->addSubGraph();
// record pn as its fake meta node
sg->setAttribute<node>("meta-node", pn);
// and vice-versa
nodeToSubgraph.set(pn.id, (size_t) sg);
}
// add n in the subgraph found
sg->addNode(n);
return sg;
}
// Parse node data
void parseNode(QXmlStreamReader &xmlReader, Graph* g) {
node n;
string nodeId = xmlReader.attributes().value("id").toString().toStdString();
if (nodesMap.find(nodeId) == nodesMap.end()) {
// if needed, add a node in the graph we are building
n = g->addNode();
// save mapping between gexf node id and created Tulip node
nodesMap[nodeId] = n;
}
else
n = nodesMap[nodeId];
// parse node label
if (xmlReader.attributes().hasAttribute("label")) {
string nodeLabel =
xmlReader.attributes().value("label").toString().toUtf8().data();
viewLabel->setNodeValue(n, nodeLabel);
}
// parse node pid
if (xmlReader.attributes().hasAttribute("pid")) {
string pid =
xmlReader.attributes().value("pid").toString().toStdString();
if (g == graph)
g = addInParent(n, pid);
else
cerr << "multiple parents are not supported: " << pid.c_str() << " will be not added as parent of " << nodeId.c_str() << endl;
}
xmlReader.readNext();
while (!(xmlReader.isEndElement() && xmlReader.name() == "node")) {
// parse node color
if (xmlReader.isStartElement() && xmlReader.qualifiedName() == "viz:color") {
unsigned int r = xmlReader.attributes().value("r").toString().toUInt();
unsigned int g = xmlReader.attributes().value("g").toString().toUInt();
unsigned int b = xmlReader.attributes().value("b").toString().toUInt();
float a = 1.0f;
if (xmlReader.attributes().hasAttribute("a")) {
a = xmlReader.attributes().value("a").toString().toFloat();
}
viewColor->setNodeValue(n, Color((unsigned char) r, (unsigned char) g,
(unsigned char) b,
(unsigned char) (a * 255)));
}
// parse node coordinates
else if (xmlReader.isStartElement() && xmlReader.qualifiedName() == "viz:position") {
nodesHaveCoordinates = true;
float x = xmlReader.attributes().value("x").toString().toFloat();
float y = xmlReader.attributes().value("y").toString().toFloat();
float z = xmlReader.attributes().value("z").toString().toFloat();
viewLayout->setNodeValue(n, Coord(x, y, z));
}
// parse node size
else if (xmlReader.isStartElement() && xmlReader.qualifiedName() == "viz:size") {
float size = xmlReader.attributes().value("value").toString().toFloat();
viewSize->setNodeValue(n, Size(size, size, size));
}
// parse node attributes
else if (xmlReader.isStartElement() && xmlReader.qualifiedName() == "attvalue") {
string attributeId = "";
if (xmlReader.attributes().hasAttribute("id")) {
attributeId = xmlReader.attributes().value("id").toString().toStdString();
}
else if (xmlReader.attributes().hasAttribute("for")) {
attributeId = xmlReader.attributes().value("for").toString().toStdString();
}
string attributeStrValue = xmlReader.attributes().value("value").toString().toUtf8().data();
if (nodePropertiesMap.find(attributeId) != nodePropertiesMap.end()) {
nodePropertiesMap[attributeId]->setNodeStringValue(n, attributeStrValue);
}
}
// check for subgraph
else if (xmlReader.isStartElement() && xmlReader.qualifiedName() == "nodes") {
Graph* sg = (Graph *) nodeToSubgraph.get(n.id);
if (sg == NULL) {
// add subgraph
sg = graph->addSubGraph();
// record the current node as its fake meta node
sg->setAttribute<node>("meta-node", n);
// and vice-versa
nodeToSubgraph.set(n.id, (size_t) sg);
}
// create its nodes
createNodes(xmlReader, sg);
}
else if (xmlReader.isStartElement() && xmlReader.qualifiedName() == "edges") {
// create its edges
createEdges(xmlReader);
}
else if (xmlReader.isStartElement() && xmlReader.qualifiedName() == "parents") {
while (!(xmlReader.isEndElement() && xmlReader.name() == "parents")) {
xmlReader.readNext();
// must be a parent
if (xmlReader.isStartElement() && xmlReader.name() == "parent") {
string pid =
xmlReader.attributes().value("for").toString().toStdString();
if (g == graph) {
g = addInParent(n, pid);
}
else
cerr << "multiple parents are not supported: " << pid.c_str() << " will be not added as parent of " << nodeId.c_str() << endl;
}
}
}
xmlReader.readNext();
}
}
// Parse edge data
void parseEdge(QXmlStreamReader &xmlReader) {
// parse the source node id and target node id
string srcId = xmlReader.attributes().value("source").toString().toStdString();
string tgtId = xmlReader.attributes().value("target").toString().toStdString();
// Check if nodes have been parsed
if (!nodesMap.empty()) {
// add an edge in the graph we are building
edge e = graph->addEdge(nodesMap[srcId], nodesMap[tgtId]);
if (xmlReader.attributes().hasAttribute("label")) {
string edgeLabel = xmlReader.attributes().value("label").toString().toUtf8().data();
viewLabel->setEdgeValue(e, edgeLabel);
}
xmlReader.readNext();
while (!(xmlReader.isEndElement() && xmlReader.name() == "edge")) {
// parse edge attribute
if (xmlReader.isStartElement() && xmlReader.qualifiedName() == "attvalue") {
string attributeId = "";
if (xmlReader.attributes().hasAttribute("id")) {
attributeId = xmlReader.attributes().value("id").toString().toStdString();
}
else if (xmlReader.attributes().hasAttribute("for")) {
attributeId = xmlReader.attributes().value("for").toString().toStdString();
}
string attributeStrValue = xmlReader.attributes().value("value").toString().toUtf8().data();
if (edgePropertiesMap.find(attributeId) != edgePropertiesMap.end()) {
edgePropertiesMap[attributeId]->setEdgeStringValue(e, attributeStrValue);
}
}
xmlReader.readNext();
}
// Store edge extremities information to add them to the graph once nodes will be parsed
}
else {
edgesTmp.push_back(make_pair(srcId, tgtId));
}
}
Graph* addSubGraphsNodes() {
// quotientGraph will be created
// as soon as there is a subgraph
Graph* quotientGraph = NULL;
// iterate on each subgraph of graph
// and add missing nodes
StableIterator<Graph*> itg(graph->getSubGraphs());
while(itg.hasNext()) {
if (!quotientGraph)
quotientGraph = graph->addCloneSubGraph("quotient graph");
Graph* sg = itg.next();
// iterate on nodes
StableIterator<node> itn(sg->getNodes());
while(itn.hasNext()) {
node n = itn.next();
Graph* msg = (Graph*) nodeToSubgraph.get(n.id);
if (msg) {
// if the current node is a fake meta node
// add the nodes of the pointed subgraph
node msn;
forEach(msn, msg->getNodes()) {
sg->addNode(msn);
// the nodes in a pointed subgraph have to be removed
// from quotientGraph
quotientGraph->delNode(msn);
}
}
}
}
return quotientGraph;
}
void addSubGraphsEdges() {
// iterate on each subgraph of graph
// and add needed edges
Iterator<Graph*>* itg = graph->getSubGraphs();
while(itg->hasNext()) {
Graph* sg = itg->next();
node n;
// iterate on nodes
forEach(n, sg->getNodes()) {
// add its out edges
edge e;
forEach(e, graph->getOutEdges(n)) {
if (sg->isElement(graph->target(e)))
sg->addEdge(e);
}
}
}
}
void computeMetaNodes(Graph* quotientGraph) {
// iterate on each subgraph of g
// and add needed meta nodes
Iterator<Graph*>* itg = graph->getSubGraphs();
while(itg->hasNext()) {
Graph* sg = itg->next();
// iterate on nodes
StableIterator<node> itn(sg->getNodes());
while(itn.hasNext()) {
node n = itn.next();
Graph* msg = (Graph*) nodeToSubgraph.get(n.id);
if (msg != NULL) {
// n is a fake meta node
// create the real meta node
// pointing to msg
node mn = sg->createMetaNode(msg);
// set its label as the name
// of the pointed subgraph
string label = viewLabel->getNodeValue(n);
if (!label.empty())
msg->setName(label);
// set meta node properties values to the ones
// of the fake meta node
PropertyInterface* prop;
forEach(prop, graph->getObjectProperties()) {
prop->copy(mn, n, prop, true);
}
// add it to quotient graph if needed
if (sg != quotientGraph)
quotientGraph->addNode(mn);
// replace n by mn
edge e;
forEach(e, graph->getInOutEdges(n)) {
pair<node, node> eEnds = graph->ends(e);
if (eEnds.first == n) {
graph->setEnds(e, mn, eEnds.second);
if ((sg != quotientGraph) &&
quotientGraph->isElement(eEnds.second))
quotientGraph->addEdge(e);
}
else {
graph->setEnds(e, eEnds.first, mn);
if ((sg != quotientGraph) &&
quotientGraph->isElement(eEnds.first))
quotientGraph->addEdge(e);
}
}
// remove fake meta node
graph->delNode(n);
msg->removeAttribute("meta-node");
nodeToSubgraph.set(n.id, 0);
}
}
}
}
// Methods which compute Cubic Bézier control points for each edge
void curveGraphEdges() {
edge e;
forEach(e, graph->getEdges()) {
node src = graph->source(e);
node tgt = graph->target(e);
Coord srcCoord = viewLayout->getNodeValue(src);
Coord tgtCoord = viewLayout->getNodeValue(tgt);
Coord dir = tgtCoord - srcCoord;
dir /= dir.norm();
float length = srcCoord.dist(tgtCoord);
float factor = 0.2f * length;
Coord normal = Coord(dir[1], -dir[0]);
normal *= factor;
Coord p1 = dir;
p1 *= factor;
p1 += srcCoord;
p1 += normal;
Coord p2 = dir;
p2 *= -factor;
p2 += tgtCoord;
p2 += normal;
// Set the second and third Cubic Bézier curve control points as edge bends
vector<Coord> bends;
bends.push_back(p1);
bends.push_back(p2);
viewLayout->setEdgeValue(e, bends);
}
}
private :
// maps associating attribute id to Tulip property
map<string, PropertyInterface *> nodePropertiesMap;
map<string, PropertyInterface *> edgePropertiesMap;
// map associating GEXF node id to Tulip node
map<string, node> nodesMap;
// vector to store edge information in case edges are declared before nodes in GEXF file
vector<pair<string, string> > edgesTmp;
// Visual attributes properties pointers to the graph we are building
LayoutProperty *viewLayout;
SizeProperty *viewSize;
ColorProperty *viewColor;
StringProperty *viewLabel;
IntegerProperty *viewShape;
// to register the subgraph corresponding
// to a fake meta node
MutableContainer<size_t> nodeToSubgraph;
bool nodesHaveCoordinates;
};
// Macro for declaring import plugin in Tulip, it will then be accessible since the File -> Import -> File menu entry in Tulip
PLUGIN(GEXFImport)
|