File: AdjacencyMatrixImport.cpp

package info (click to toggle)
tulip 6.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196,224 kB
  • sloc: cpp: 571,851; ansic: 13,983; python: 4,105; sh: 1,555; yacc: 522; xml: 484; makefile: 168; pascal: 148; lex: 55
file content (251 lines) | stat: -rw-r--r-- 7,718 bytes parent folder | download
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
/**
 *
 * This file is part of Tulip (https://tulip.labri.fr)
 *
 * 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 <fstream>
#include <sstream>
#include <vector>

#include <tulip/TulipPluginHeaders.h>

using namespace std;
using namespace tlp;

enum ValType { TLP_DOUBLE = 0, TLP_STRING = 1, TLP_NOVAL = 2, TLP_NOTHING = 3, TLP_AND = 4 };

/** \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 ImportFileModule {
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) : ImportFileModule(context) {}
  ~AdjacencyMatrixImport() override {}

  std::string icon() const override {
    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());
    return false;
  }

  bool importFile() override {
    std::istream *in = tlp::getInputFileStream(filename);
    // check for open stream failure
    if (in->fail()) {
      std::stringstream ess;
      ess << "Unable to open " << filename << ": " << tlp::getStrError();
      pluginProgress->setError(ess.str());
      delete in;
      return false;
    }

    unsigned int curLine = 0;
    DoubleProperty *metric = graph->getProperty<DoubleProperty>("viewMetric");
    StringProperty *stringP = graph->getProperty<StringProperty>("viewLabel");

    std::string line;
    const vector<node> &nodes = graph->nodes();

    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()) {
            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 ") + filename +
                             "\n is different from the number of found nodes.");
    return false;
  }
};

PLUGIN(AdjacencyMatrixImport)