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
|
/**
*
* 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 <cerrno>
#include <fstream>
#include <sstream>
#include <vector>
#include <tulip/TulipPluginHeaders.h>
#include <tulip/TlpTools.h>
using namespace std;
using namespace tlp;
enum ValType { TLP_DOUBLE = 0, TLP_STRING = 1, TLP_NOVAL = 2, TLP_NOTHING = 3, TLP_AND = 4 };
namespace {
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(),
};
}
/** \addtogroup import */
/** This plugin enables to import a graph coded with a matrix
*
* File format:
*
* The input format of this plugin is an ascii file where each
* line represents a row of the matrix.
* In each row, cells must be separated by a space.
*
* Let M(i,j) be a cell of the matrix :
* - if i==j we define the value of a node.
* - if i!=j we define a directed edge between node[i] and node[j]
*
* If M(i,j) is real value (0, .0, -1, -1.0), it is stored in the
* viewMetric property of the graph. <br>
* If M(i,j) is a string, it is stored in the
* viewLabel property of the graph. <br>
* Use & to set the viewMetric and viewLabel properties of a node or edge in the same time.
* If M(i,j) == @ an edge will be created without value <br>
* If M(i,j) == # no edge will be created between node[i] and node[j]
*
* EXAMPLE 1 :
* <br>A
* <br># B
* <br># # C
* <br>Defines a graph with 3 nodes (with labels A B C) and without edge.
*
* EXAMPLE 2 :
* <br>A
* <br>@ B
* <br>@ @ C
* <br>Defines a simple complete graph with 3 nodes (with labels A B C) and no label (or value) on its edges
*
* EXAMPLE 3 :
* <br>A # E & 5
* <br>@ B
* <br># @ C
* <br>Defines a graph with 3 nodes and 3 edges, the edge between A and C is named E and has the value 5
*
*/
class AdjacencyMatrixImport:public ImportModule {
public:
PLUGININFORMATION("Adjacency Matrix", "Auber David", "05/09/2008",
"Imports a graph from a file coding an adjacency matrix.<br/>File format:<br/>\
The input format of this plugin is an ascii file where each line represents a row of the matrix.\
In each row, cells must be separated by a space.<br/>Let M(i,j) be a cell of the matrix :<br/>\
- if i==j we define the value of a node.<br/>\
- if i!=j we define a directed edge between node[i] and node[j]<br/>\
If M(i,j) is real value (0, .0, -1, -1.0), it is stored in the viewMetric property of the graph.<br/>\
If M(i,j) is a string, it is stored in the viewLabel property of the graph.<br/>\
Use & to set the viewMetric and viewLabel properties of a node or edge in the same time.<br/>\
If M(i,j) == @ an edge will be created without value<br/>\
If M(i,j) == # no edge will be created between node[i] and node[j]<br/>\
EXAMPLE 1 :<br/>\
A<br/>\
# B<br/>\
# # C<br/>\
Defines a graph with 3 nodes (with labels A B C) and without edge.<br/>\
EXAMPLE 2 :<br/>\
A<br/>\
@ B<br/>\
@ @ C<br/>\
Defines a simple complete graph with 3 nodes (with labels A B C) and no label (or value) on its edges<br/>\
EXAMPLE 3 :<br/>\
A # E & 5<br/>\
@ B<br/># @ C<br/>\
Defines a graph with 3 nodes and 3 edges, the edge between A and C is named E and has the value 5",
"1.2","File")
AdjacencyMatrixImport(tlp::PluginContext* context):ImportModule(context) {
addInParameter<string>("file::filename",paramHelp[0],"");
}
~AdjacencyMatrixImport() {}
vector<node> nodes;
std::string icon() const {
return ":/tulip/graphperspective/icons/32/import_adj_mat.png";
}
bool formatError(const char* s, int curLine) {
std::stringstream ess;
ess << "Error parsing '" << s << "' at line :" << curLine + 1;
pluginProgress->setError(ess.str());
tlp::warning() << pluginProgress->getError() << std::endl;
return false;
}
bool importGraph() {
string name2;
if (!(dataSet->get("file::filename", name2) ||
// ensure compatibility with old version
dataSet->get("file::name", name2)))
return false;
tlp_stat_t infoEntry;
int result = statPath(name2, &infoEntry);
if (result == -1) {
pluginProgress->setError(strerror(errno));
return false;
}
std::istream *in = tlp::getInputFileStream(name2.c_str());
unsigned int curLine = 0;
DoubleProperty *metric = graph->getProperty<DoubleProperty>("viewMetric");
StringProperty *stringP = graph->getProperty<StringProperty>("viewLabel");
std::string line;
while (!in->eof() && std::getline(*in, line)) {
stringstream lines(line);
unsigned int curNode = 0;
edge e;
bool itemFound = false;
bool andFound = false;
while (lines.good()) {
string valString;
if ( lines >> valString) {
const char *start= valString.c_str();
char *res;
double valDouble = strtod(start, &res);
ValType type;
if (res!=start) {
type = TLP_DOUBLE;
itemFound = true;
}
else if (valString == "&") {
if (!itemFound)
return formatError(valString.c_str(), curLine);
type = TLP_AND;
--curNode;
andFound = true;
itemFound = false;
continue;
}
else if (valString == "@") {
if (andFound)
return formatError(valString.c_str(), curLine);
type = TLP_NOVAL;
itemFound = false;
}
else if (valString == "#") {
if (andFound)
return formatError(valString.c_str(), curLine);
type = TLP_NOTHING;
itemFound = false;
}
else {
type = TLP_STRING;
itemFound = true;
}
if ( curNode >= nodes.size() || curLine >= nodes.size()) {
nodes.push_back(graph->addNode());
}
if (curNode == curLine) {
switch (type) {
case TLP_DOUBLE:
metric->setNodeValue(nodes[curNode],valDouble);
break;
case TLP_STRING:
stringP->setNodeValue(nodes[curNode],valString);
break;
default:
return formatError(valString.c_str(), curLine);
}
}
else {
switch (type) {
case TLP_DOUBLE:
if (!andFound)
e=graph->addEdge(nodes[curLine],nodes[curNode]);
metric->setEdgeValue(e,valDouble);
break;
case TLP_STRING:
if (!andFound)
e=graph->addEdge(nodes[curLine],nodes[curNode]);
stringP->setEdgeValue(e,valString);
break;
case TLP_NOVAL:
e=graph->addEdge(nodes[curLine],nodes[curNode]);
break;
case TLP_NOTHING:
break;
default:
return formatError(valString.c_str(), curLine);
}
}
++curNode;
}
andFound = false;
}
if (andFound)
return formatError("&", curLine);
++curLine;
}
delete in;
// final check:
// number of lines must be equal to number of nodes
if (curLine == nodes.size())
return true;
pluginProgress->setError(std::string("The number of lines in file ") + name2 + "\n is different from the number of found nodes.");
tlp::warning() << pluginProgress->getError() << std::endl;
return false;
}
};
PLUGIN(AdjacencyMatrixImport)
|