File: AdjacencyMatrixImport.cpp

package info (click to toggle)
tulip 3.7.0dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 39,428 kB
  • sloc: cpp: 231,403; php: 11,023; python: 1,128; sh: 671; yacc: 522; makefile: 315; xml: 63; lex: 55
file content (242 lines) | stat: -rwxr-xr-x 6,623 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
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
 *
 * 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 <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fstream>
#include <sstream>
#include <cmath>
#include <tulip/TulipPlugin.h>
#include <vector>
#include <string.h>

using namespace std;
using namespace tlp;

const unsigned int MAX_SIZE = 10000;

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]
 *
 *  EXEMPLE 1 :
 *  <br>A
 *  <br># B
 *  <br># # C
 *  <br>Define  a graph with 3 nodes (with labels A B C) and without edge.
 *
 *  EXEMPLE 2 :
 *  <br>A
 *  <br>@ B
 *  <br>@ @ C
 *  <br>Define a simple complete graph with 3 nodes (with labels A B C) and no label (or value) on its edges
 *
 *  EXEMPLE 3 :
 *  <br>A # E & 5
 *  <br>@ B
 *  <br># @ C
 *  <br>Define 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:
  AdjacencyMatrixImport(AlgorithmContext context):ImportModule(context) {
    addParameter<string>("file::name",paramHelp[0]);
  }
  ~AdjacencyMatrixImport() {}
  vector<node> nodes;

  bool formatError(const char* s, int curLine) {
    std::stringstream ess;
    ess <<  "Error parsing '" << s << "' at line :" << curLine;
    pluginProgress->setError(ess.str());
    std::cerr << pluginProgress->getError() << std::endl;
    return false;
  }

  bool importGraph() {
    string name2;

    if (!dataSet->get("file::name", name2))
      return false;

    struct stat infoEntry;
    int result;
#ifdef _WIN32
    result = stat(name2.c_str(), &infoEntry);
#else
    result = lstat(name2.c_str(), &infoEntry);
#endif

    if (result == -1) {
      pluginProgress->setError(strerror(errno));
      return false;
    }

    std::ifstream in(name2.c_str());
    unsigned int curLine = 0;
    DoubleProperty *metric = graph->getProperty<DoubleProperty>("viewMetric");
    StringProperty *stringP = graph->getProperty<StringProperty>("viewLabel");

    while (!in.eof()) {
      char line[MAX_SIZE];
      in.getline(line,MAX_SIZE);
      stringstream lines(line);
      unsigned int curNode = 0;
      edge e;
      bool itemFound = false;
      bool andFound = false;

      while (lines.good()) {
        string valString;
        ValType type;

        if ( lines >> valString) {
          const char *start= valString.c_str();
          char *res;
          double valDouble = strtod(start, &res);

          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++;
    }

    return true;
  }
};
/*@}*/
IMPORTPLUGINOFGROUP(AdjacencyMatrixImport, "Adjacency Matrix", "Auber David", "05/09/2008","","1.2","File")