File: QuotientClustering.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 (426 lines) | stat: -rwxr-xr-x 14,950 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
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
/**
 *
 * 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 "QuotientClustering.h"

using namespace std;
using namespace tlp;

ALGORITHMPLUGIN(QuotientClustering,"Quotient Clustering","David Auber","13/06/2001","OK","1.3");

//==============================================================================
namespace {
const char * paramHelp[] = {
  // oriented
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "bool" ) \
  HTML_HELP_DEF( "values", "[true, false]" ) \
  HTML_HELP_DEF( "default", "true" ) \
  HTML_HELP_BODY() \
  "This parameter indicates whether the graph has to be considered as oriented or not." \
  HTML_HELP_CLOSE(),
  // recursive
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "bool" ) \
  HTML_HELP_DEF( "values", "[true, false]" ) \
  HTML_HELP_DEF( "default", "false" ) \
  HTML_HELP_BODY() \
  "This parameter indicates whether the algorithm has to be applied along the entire hierarchy of subgraphs." \
  HTML_HELP_CLOSE(),
  // node aggregation function
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "String Collection" ) \
  HTML_HELP_DEF( "default", "none" )   \
  HTML_HELP_BODY() \
  "This parameter indicates the function used to compute a measure value for a meta-node using the values of its underlying nodes. If 'none' is choosen no value will be computed" \
  HTML_HELP_CLOSE(),
  // edge aggregation function
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "String Collection" ) \
  HTML_HELP_DEF( "default", "none" )   \
  HTML_HELP_BODY() \
  "This parameter indicates the function used to compute a measure value for a meta-edge using the values of its underlying edges. If 'none' is choosen no value will be computed." \
  HTML_HELP_CLOSE(),
  // meta-node label
  HTML_HELP_OPEN()              \
  HTML_HELP_DEF( "type", "StringProperty" )       \
  HTML_HELP_DEF( "value", "An existing string property" )   \
  HTML_HELP_BODY()              \
  "This parameter defines the property used to compute the label of the meta-nodes. An arbitrary underlying node is choosen and its associated value for the given property becomes the meta-node label."\
  HTML_HELP_CLOSE(),
  // use name of subgraphs
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "bool" ) \
  HTML_HELP_DEF( "values", "[true, false]" ) \
  HTML_HELP_DEF( "default", "false" ) \
  HTML_HELP_BODY() \
  "This parameter indicates whether the meta-node label has to be the same as the name of the subgraph it represents." \
  HTML_HELP_CLOSE(),
  // edge cardinality
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "bool" ) \
  HTML_HELP_DEF( "values", "[true, false]" ) \
  HTML_HELP_DEF( "default", "false" ) \
  HTML_HELP_BODY() \
  "This parameter indicates whether the cardinality of the underlying edges of the meta-edges has to be computed or not. If yes, the property edgeCardinality will be created for the quotient graph." \
  HTML_HELP_CLOSE(),
  // layout quotient graph
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "bool" ) \
  HTML_HELP_DEF( "values", "[true, false]" ) \
  HTML_HELP_DEF( "default", "false" ) \
  HTML_HELP_BODY() \
  "This parameter indicates whether the layout of the quotient graph(s) has to be computed or not." \
  HTML_HELP_CLOSE(),
};
}
#define AGGREGATION_FUNCTIONS "none;average;sum;max;min"
//================================================================================
QuotientClustering::QuotientClustering(AlgorithmContext context):Algorithm(context) {
  addDependency<LayoutAlgorithm>("Circular", "1.1");
  addDependency<LayoutAlgorithm>("GEM (Frick)", "1.2");
  addDependency<SizeAlgorithm>("Auto Sizing", "1.0");
  addParameter<bool>("oriented", paramHelp[0], "true");
  addParameter<StringCollection>("node function", paramHelp[2], AGGREGATION_FUNCTIONS);
  addParameter<StringCollection>("edge function", paramHelp[3], AGGREGATION_FUNCTIONS);
  addParameter<StringProperty>("meta-node label", paramHelp[4], 0, false);
  addParameter<bool>("use name of subgraph", paramHelp[5], "false");
  addParameter<bool>("recursive", paramHelp[1], "false");
  addParameter<bool>("layout quotient graph(s)", paramHelp[7], "false");
  addParameter<bool>("edge cardinality", paramHelp[6], "false");
}
//================================================================================
QuotientClustering::~QuotientClustering() {}
//===============================================================================
// define a specific calculator for viewLabel
class QuotientLabelCalculator :public AbstractStringProperty::MetaValueCalculator {
  StringProperty* sgLabel;
  bool useSubGraphName;
public:
  QuotientLabelCalculator(StringProperty* label, bool useSgName)
    : AbstractStringProperty::MetaValueCalculator(),
      sgLabel(label), useSubGraphName(useSgName) {}

  void computeMetaValue(AbstractStringProperty* label, node mN, Graph* sg,
                        Graph*) {
    if (sgLabel)
      label->setNodeValue(mN, sgLabel->getNodeValue(sg->getOneNode()));
    else if (useSubGraphName) {
      string name;
      sg->getAttribute<string>("name", name);
      label->setNodeValue(mN, name);
    }
  }
};

// define a specific calculator for edgeCardinality
class EdgeCardinalityCalculator :public AbstractIntegerProperty::MetaValueCalculator {
public:
  void computeMetaValue(AbstractIntegerProperty* card, edge mE,
                        Iterator<edge>* itE, Graph*) {
    unsigned int nbEdges = 0;

    while(itE->hasNext()) {
      itE->next();
      ++nbEdges;
    }

    card->setEdgeValue(mE, nbEdges);
  }
};

//===============================================================================
bool QuotientClustering::run() {
  bool oriented = true, edgeCardinality = true;
  bool recursive = false, quotientLayout = true, useSubGraphName = false;
  StringProperty *metaLabel = NULL;
  StringCollection nodeFunctions(AGGREGATION_FUNCTIONS);
  nodeFunctions.setCurrent(0);
  StringCollection edgeFunctions(AGGREGATION_FUNCTIONS);
  edgeFunctions.setCurrent(0);

  if (dataSet!=0) {
    dataSet->get("oriented", oriented);
    dataSet->get("node function", nodeFunctions);
    dataSet->get("edge function", edgeFunctions);
    dataSet->get("edge cardinality", edgeCardinality);
    dataSet->get("recursive", recursive);
    dataSet->get("meta-node label", metaLabel);
    dataSet->get("use name of subgraph", useSubGraphName);
    dataSet->get("layout quotient graph(s)", quotientLayout);
  }

  Iterator<Graph *> *itS= graph->getSubGraphs();

  // do nothing if there is no subgraph
  if (!itS->hasNext()) {
    delete itS;
    return true;
  }

  delete itS;

  IntegerProperty *opProp = 0, *cardProp = 0;
  Graph *quotientGraph = graph->getRoot()->addSubGraph();
  stringstream sstr;
  sstr << "quotient of ";
  string graphName;
  graph->getAttribute<string>("name", graphName);

  if (graphName.size() == 0)
    sstr << graph->getId();
  else {
    sstr << graphName;

    if (graphName == "unnamed")
      sstr << " " << graph->getId();
  }

  quotientGraph->setAttribute(string("name"), sstr.str());

  if (!oriented) {
    opProp = new IntegerProperty(quotientGraph);
    opProp->setAllEdgeValue(edge().id);
  }

  EdgeCardinalityCalculator cardCalc;

  if (edgeCardinality) {
    cardProp = quotientGraph->getLocalProperty<IntegerProperty>("edgeCardinality");
    cardProp->setMetaValueCalculator(&cardCalc);
  }

  // set specific meta value calculators
  // for most properties
  DoubleProperty::PredefinedMetaValueCalculator nodeFn =
    (DoubleProperty::PredefinedMetaValueCalculator ) nodeFunctions.getCurrent();
  DoubleProperty::PredefinedMetaValueCalculator edgeFn =
    (DoubleProperty::PredefinedMetaValueCalculator) edgeFunctions.getCurrent();
  QuotientLabelCalculator viewLabelCalc(metaLabel, useSubGraphName);
  TLP_HASH_MAP<unsigned long, PropertyInterface::MetaValueCalculator *> prevCalcs;
  string pName;
  forEach(pName, quotientGraph->getProperties()) {
    PropertyInterface *prop = quotientGraph->getProperty(pName);

    if (dynamic_cast<DoubleProperty *>(prop)) {
      prevCalcs[(unsigned long) prop] = prop->getMetaValueCalculator();
      ((DoubleProperty *)prop)->setMetaValueCalculator(nodeFn, edgeFn);
    }

    if (pName == "viewLabel") {
      prevCalcs[(unsigned long) prop] = prop->getMetaValueCalculator();
      ((StringProperty*) prop)->setMetaValueCalculator(&viewLabelCalc);
    }
  }
  // compute meta nodes, edges and associated meta values
  itS = graph->getSubGraphs();
  vector<node> mNodes;
  graph->createMetaNodes(itS, quotientGraph, mNodes);
  delete itS;

  // restore previous calculators
  TLP_HASH_MAP<unsigned long, PropertyInterface::MetaValueCalculator *>::iterator itC =
    prevCalcs.begin();

  while(itC != prevCalcs.end()) {
    ((PropertyInterface*) (*itC).first)->setMetaValueCalculator((*itC).second);
    itC++;
  }

  GraphProperty *metaInfo =
    graph->getRoot()->getProperty<GraphProperty>("viewMetaGraph");

  // orientation
  if (!oriented) {
    // for each edge
    // store opposite edge in opProp
    edge mE;
    forEach(mE, quotientGraph->getEdges()) {
      edge op = quotientGraph->existEdge(quotientGraph->target(mE),
                                         quotientGraph->source(mE));

      if (op.isValid()) {
        opProp->setEdgeValue(op, mE.id);
        opProp->setEdgeValue(mE, op.id);
      }
    }
    set<edge> edgesToDel;
    DoubleProperty* viewMetric =
      quotientGraph->getProperty<DoubleProperty>("viewMetric");
    Iterator<edge>* itE = quotientGraph->getEdges();

    while (itE->hasNext()) {
      edge mE = itE->next();
      edge op(opProp->getEdgeValue(mE));

      if (op.isValid() &&
          edgesToDel.find(mE) == edgesToDel.end() &&
          edgesToDel.find(op) == edgesToDel.end()) {
        // if the opposite edge viewMetric associated value is greater
        // than the mE associated value than we will keep it instead of mE
        bool opOK =
          viewMetric->getEdgeValue(mE) < viewMetric->getEdgeValue(op);

        if (edgeFn != DoubleProperty::NO_CALC) {
          forEach(pName, graph->getProperties()) {
            PropertyInterface *property = graph->getProperty(pName);

            if (dynamic_cast<DoubleProperty *>(property) &&
                // try to avoid view... properties
                (pName.find("view") != 0 || pName == "viewMetric")) {
              DoubleProperty *metric = graph->getProperty<DoubleProperty>(pName);
              double value = metric->getEdgeValue(mE);

              switch(edgeFn) {
              case DoubleProperty::AVG_CALC:
                value = (value + metric->getEdgeValue(op))/2;
                break;

              case DoubleProperty::SUM_CALC:
                value += metric->getEdgeValue(op);
                break;

              case DoubleProperty::MAX_CALC:

                if (value < metric->getEdgeValue(op))
                  value = metric->getEdgeValue(op);

                break;

              case DoubleProperty::MIN_CALC:

                if (value > metric->getEdgeValue(op))
                  value = metric->getEdgeValue(op);

                break;

              case DoubleProperty::NO_CALC:
                break;
              }

              if (opOK)
                metric->setEdgeValue(op, value);
              else
                metric->setEdgeValue(mE, value);
            }
          }
        }

        // compute cardinaly if needed
        if (cardProp) {
          unsigned int card =
            cardProp->getEdgeValue(mE) + cardProp->getEdgeValue(op);

          if (opOK)
            cardProp->setEdgeValue(op, card);
          else
            cardProp->setEdgeValue(mE, card);
        }

        // insert one of the opposite meta edges in edgesToDel
        // and insert its undelying edges in the set of the remaining one
        edge meToKeep(mE.id), meToDel(op.id);

        if (opOK)
          meToKeep = op, meToDel = mE;

        edgesToDel.insert(meToDel);
        set<edge> se = metaInfo->getEdgeValue(meToKeep);
        const set<edge>& nse = metaInfo->getEdgeValue(meToDel);
        set<edge>::const_iterator itnse;

        for(itnse = nse.begin(); itnse != nse.end(); ++itnse)
          se.insert(*itnse);

        metaInfo->setEdgeValue(meToKeep, se);
      }
    }

    delete itE;
    set<edge>::const_iterator it;

    for ( it = edgesToDel.begin(); it!=edgesToDel.end(); ++it)
      quotientGraph->delEdge(*it);
  }

  if (opProp)
    delete opProp;

  if (dataSet!=0) {
    dataSet->set("quotientGraph", quotientGraph);
  }

  // layouting if needed
  if (quotientLayout) {
    string errMsg;
    string layoutName;

    if (mNodes.size() > 300  ||
        quotientGraph->numberOfEdges() == 0)
      layoutName = "Circular";
    else
      layoutName = "GEM (Frick)";

    string sizesName="Auto Sizing";
    quotientGraph->computeProperty(layoutName, quotientGraph->getLocalProperty<LayoutProperty>("viewLayout"), errMsg);

    if (mNodes.size() < 300)
      quotientGraph->computeProperty(sizesName, quotientGraph->getLocalProperty<SizeProperty>("viewSize"),errMsg);
  }

  // recursive call if needed
  if (recursive) {
    DataSet dSet;
    dSet.set("oriented", oriented);
    dSet.set("node function", nodeFunctions);
    dSet.set("edge function", edgeFunctions);
    dSet.set("edge cardinality", edgeCardinality);
    dSet.set("recursive", recursive);
    dSet.set("meta-node label", metaLabel);
    dSet.set("use name of subgraph", useSubGraphName);
    dSet.set("layout quotient graph(s)", quotientLayout);
    vector<node>::iterator itn = mNodes.begin();

    while(itn != mNodes.end()) {
      node mn = *itn;
      Graph* sg = quotientGraph->getNodeMetaInfo(mn);
      string eMsg;
      sg->applyAlgorithm("Quotient Clustering", eMsg, &dSet,
                         pluginProgress);

      // if a quotient graph has been computed
      // update metaInfo of current meta node
      if (dSet.getAndFree("quotientGraph", sg))
        metaInfo->setNodeValue(mn, sg);

      itn++;
    }
  }

  return true;
}

//================================================================================
bool QuotientClustering::check(string &erreurMsg) {
  erreurMsg="";
  return true;
}
//================================================================================
void QuotientClustering::reset() {}
//================================================================================