~ubuntu-branches/ubuntu/saucy/rocs/saucy-proposed

« back to all changes in this revision

Viewing changes to RocsCore/DataStructures/Graph/GraphStructure.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg, Rohan Garg, Philip Muškovac
  • Date: 2013-06-21 02:04:20 UTC
  • mfrom: (1.1.27)
  • Revision ID: package-import@ubuntu.com-20130621020420-lzlui9y7qc6w3xog
Tags: 4:4.10.80-0ubuntu1
[ Rohan Garg ]
* New upstream release

[ Philip Muškovac ]
* Build-depend on libgrantlee-dev and libx11-dev
* Update rocs.install and not-installed 

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
#include <boost/graph/adjacency_list.hpp>
36
36
#include <boost/graph/dijkstra_shortest_paths.hpp>
37
37
#include <boost/graph/graph_concepts.hpp>
38
 
#include <qstring.h>
 
38
 
 
39
#include <KLocale>
 
40
#include <QString>
 
41
 
 
42
#include <cmath>
39
43
 
40
44
DataStructurePtr Rocs::GraphStructure::create(Document *parent)
41
45
{
61
65
    //FIXME this import does not correctly import different types
62
66
    setGraphType(Graph);
63
67
    QHash <Data*, DataPtr> dataTodata;
64
 
    foreach(DataPtr n, other->dataList()) {
65
 
        DataPtr newdata = addData(""); //n->name());
 
68
    //FIXME only default data type considered
 
69
    foreach(DataPtr n, other->dataList(0)) {
 
70
        DataPtr newdata = createData("", 0); //n->name());
66
71
        newdata->setColor(n->color());
67
72
        newdata->setProperty("value", n->property("value").toString());
68
73
        newdata->setX(n->x());
70
75
        newdata->setWidth(n->width());
71
76
        dataTodata.insert(n.get(), newdata);
72
77
    }
73
 
    foreach(PointerPtr e, other->pointers()) {
 
78
    //FIXME only default pointer type considered
 
79
    foreach(PointerPtr e, other->pointers(0)) {
74
80
        DataPtr from =  dataTodata.value(e->from().get());
75
81
        DataPtr to =  dataTodata.value(e->to().get());
76
82
 
77
 
        PointerPtr newPointer = addPointer(from, to);
 
83
        PointerPtr newPointer = createPointer(from, to, 0);
78
84
        if (newPointer.get()){
79
85
            newPointer->setColor(e->color());
80
86
            newPointer->setProperty("value", e->property("value").toString());
86
92
{
87
93
}
88
94
 
 
95
QScriptValue Rocs::GraphStructure::nodes()
 
96
{
 
97
    QScriptValue array = engine()->newArray();
 
98
    foreach(int type, document()->dataTypeList()) {
 
99
    foreach(DataPtr n, dataList(type)) {
 
100
        array.property("push").call(array, QScriptValueList() << n->scriptValue());
 
101
    }
 
102
    }
 
103
    return array;
 
104
}
 
105
 
 
106
QScriptValue Rocs::GraphStructure::nodes(int type)
 
107
{
 
108
    QScriptValue array = engine()->newArray();
 
109
    foreach(DataPtr n, dataList(type)) {
 
110
        array.property("push").call(array, QScriptValueList() << n->scriptValue());
 
111
    }
 
112
    return array;
 
113
}
 
114
 
 
115
QScriptValue Rocs::GraphStructure::edges()
 
116
{
 
117
    QScriptValue array = engine()->newArray();
 
118
    foreach(int type, document()->pointerTypeList()) {
 
119
    foreach(PointerPtr n, pointers(type)) {
 
120
        array.property("push").call(array, QScriptValueList() << n->scriptValue());
 
121
    }
 
122
    }
 
123
    return array;
 
124
}
 
125
 
 
126
QScriptValue Rocs::GraphStructure::edges(int type)
 
127
{
 
128
    QScriptValue array = engine()->newArray();
 
129
    foreach(PointerPtr n, pointers(type)) {
 
130
        array.property("push").call(array, QScriptValueList() << n->scriptValue());
 
131
    }
 
132
    return array;
 
133
}
 
134
 
 
135
QScriptValue Rocs::GraphStructure::createNode()
 
136
{
 
137
    return createNode(0);
 
138
}
 
139
 
 
140
QScriptValue Rocs::GraphStructure::createNode(int type)
 
141
{
 
142
    DataPtr n = createData("", type);
 
143
    n->setEngine(engine());
 
144
    return n->scriptValue();
 
145
}
 
146
 
 
147
QScriptValue Rocs::GraphStructure::createEdge(Data* fromRaw, Data* toRaw)
 
148
{
 
149
    return createEdge(fromRaw, toRaw, 0);
 
150
}
 
151
 
 
152
QScriptValue Rocs::GraphStructure::createEdge(Data* fromRaw, Data* toRaw, int type)
 
153
{
 
154
    if (fromRaw == 0 || toRaw == 0) {
 
155
        kError() << "No edge added: data does not exist";
 
156
        emit scriptError(i18n("Cannot create edge: nodes are not defined."));
 
157
        return QScriptValue();
 
158
    }
 
159
    if (!document()->pointerTypeList().contains(type)) {
 
160
        emit scriptError(i18n("Cannot create edge: pointer type %1 not defined", type));
 
161
        return QScriptValue();
 
162
    }
 
163
 
 
164
    DataPtr from = fromRaw->getData();
 
165
    DataPtr to = toRaw->getData();
 
166
 
 
167
    PointerPtr edge = createPointer(from, to, type);
 
168
    if (edge) {
 
169
        edge->setEngine(engine());
 
170
        return edge->scriptValue();
 
171
    }
 
172
    kError() << "Could not add pointer to data structure";
 
173
 
 
174
    return QScriptValue();
 
175
}
 
176
 
89
177
QScriptValue Rocs::GraphStructure::overlay_edges(int overlay)
90
178
{
91
 
    //FIXME deprecate this method
92
 
    QScriptValue array = engine()->newArray();
93
 
    foreach(PointerPtr n, pointers(overlay)) {
94
 
        array.property("push").call(array, QScriptValueList() << n->scriptValue());
95
 
    }
96
 
    return array;
 
179
    emit scriptError(i18n("The global method \"%1\" is deprecated, please use \"%2\" instead.",
 
180
        QString("overlay_edges(int type)"),
 
181
        QString("edges(int type)")));
 
182
    return edges(overlay);
97
183
}
98
184
 
99
185
QScriptValue Rocs::GraphStructure::list_nodes()
100
186
{
101
 
    QScriptValue array = engine()->newArray();
102
 
    foreach(int type, document()->dataTypeList()) {
103
 
    foreach(DataPtr n, dataList(type)) {
104
 
        array.property("push").call(array, QScriptValueList() << n->scriptValue());
105
 
    }
106
 
    }
107
 
    return array;
 
187
    emit scriptError(i18n("The global method \"%1\" is deprecated, please use \"%2\" instead.",
 
188
        QString("list_nodes()"),
 
189
        QString("nodes()")));
 
190
    return nodes();
108
191
}
109
192
 
110
193
QScriptValue Rocs::GraphStructure::list_nodes(int type)
111
194
{
112
 
    QScriptValue array = engine()->newArray();
113
 
    foreach(DataPtr n, dataList(type)) {
114
 
        array.property("push").call(array, QScriptValueList() << n->scriptValue());
115
 
    }
116
 
    return array;
 
195
    emit scriptError(i18n("The global method \"%1\" is deprecated, please use \"%2\" instead.",
 
196
        QString("list_nodes(int type)"),
 
197
        QString("nodes(int type)")));
 
198
    return nodes(type);
117
199
}
118
200
 
119
201
QScriptValue Rocs::GraphStructure::list_edges()
120
202
{
121
 
    QScriptValue array = engine()->newArray();
122
 
    foreach(int type, document()->pointerTypeList()) {
123
 
    foreach(PointerPtr n, pointers(type)) {
124
 
        array.property("push").call(array, QScriptValueList() << n->scriptValue());
125
 
    }
126
 
    }
127
 
    return array;
 
203
    emit scriptError(i18n("The global method \"%1\" is deprecated, please use \"%2\" instead.",
 
204
        QString("list_edges()"),
 
205
        QString("edges()")));
 
206
    return edges();
128
207
}
129
208
 
130
209
QScriptValue Rocs::GraphStructure::list_edges(int type)
131
210
{
132
 
    QScriptValue array = engine()->newArray();
133
 
    foreach(PointerPtr n, pointers(type)) {
134
 
        array.property("push").call(array, QScriptValueList() << n->scriptValue());
135
 
    }
136
 
    return array;
 
211
    emit scriptError(i18n("The global method \"%1\" is deprecated, please use \"%2\" instead.",
 
212
        QString("list_edges(int type)"),
 
213
        QString("edges(int type)")));
 
214
    return edges(type);
137
215
}
138
216
 
139
217
QScriptValue Rocs::GraphStructure::add_node(const QString& name)
140
218
{
141
 
    DataPtr n = addData(name);
 
219
    emit scriptError(i18n("The global method \"%1\" is deprecated, please use \"%2\" instead.",
 
220
        QString("add_node(string name)"),
 
221
        QString("createNode()")));
 
222
    DataPtr n = createData(name, 0);
142
223
    n->setEngine(engine());
143
224
    return n->scriptValue();
144
225
}
145
226
 
146
227
QScriptValue Rocs::GraphStructure::add_edge(Data* fromRaw, Data* toRaw)
147
228
{
 
229
    emit scriptError(i18n("The global method \"%1\" is deprecated, please use \"%2\" instead.",
 
230
        QString("add_edge(from, to)"),
 
231
        QString("createEdge(node from, node to)")));
148
232
    return add_overlay_edge(fromRaw, toRaw, 0);
149
233
}
150
234
 
151
235
QScriptValue Rocs::GraphStructure::add_overlay_edge(Data* fromRaw, Data* toRaw, int overlay)
152
236
{
153
 
    if (fromRaw == 0 || toRaw == 0) {
154
 
        kError() << "No edge added: data does not exist";
155
 
        return QScriptValue();
156
 
    }
157
 
    if (!document()->pointerTypeList().contains(overlay)) {
158
 
        kError() << "No edge added: pointer type does not exist";
159
 
        return QScriptValue();
160
 
    }
161
 
 
162
 
    DataPtr from = fromRaw->getData();
163
 
    DataPtr to = toRaw->getData();
164
 
 
165
 
    PointerPtr edge = addPointer(from, to, overlay);
166
 
    if (edge) {
167
 
        edge->setEngine(engine());
168
 
        return edge->scriptValue();
169
 
    }
170
 
    kError() << "Could not at this pointer to the data structure";
171
 
 
172
 
    return QScriptValue();
 
237
    emit scriptError(i18n("The global method \"%1\" is deprecated, please use \"%2\" instead.",
 
238
        QString("add_overlay_edge(from, to, overlay)"),
 
239
        QString("createEdge(node from, node to, int type)")));
 
240
    return createEdge(fromRaw, toRaw, overlay);
173
241
}
174
242
 
175
243
QScriptValue Rocs::GraphStructure::dijkstra_shortest_path(Data* fromRaw, Data* toRaw)
202
270
    QScriptValue distances = engine()->newArray();
203
271
    foreach (DataPtr target, dataListAll()) {
204
272
        qreal length = 0;
205
 
        foreach (PointerPtr edge, shortestPaths[target]) {
206
 
            if (!edge->property("value").toString().isEmpty()) {
207
 
                length += edge->property("value").toDouble();
208
 
            } else {
209
 
                length += 1;
 
273
 
 
274
        if (shortestPaths[target].isEmpty() && from != target) {
 
275
            length = INFINITY;
 
276
        } else {
 
277
            foreach (PointerPtr edge, shortestPaths[target]) {
 
278
                if (!edge->property("value").toString().isEmpty()) {
 
279
                    length += edge->property("value").toDouble();
 
280
                } else {
 
281
                    length += 1;
 
282
                }
210
283
            }
211
284
        }
 
285
 
212
286
        distances.property("push").call(
213
287
            distances,
214
288
            QScriptValueList() << length
322
396
    }
323
397
 
324
398
    // need to convert multigraph to graph
325
 
    foreach(DataPtr data, dataList()) {
 
399
    //FIXME only default data type considered
 
400
    foreach(DataPtr data, dataList(0)) {
326
401
        // Clear the rest. there should be only one edge between two nodes.
327
402
        foreach(DataPtr neighbor, data->adjacentDataList()) {
328
403
            if (data == neighbor) {
345
420
    return (_type == Multigraph);
346
421
}
347
422
 
348
 
PointerPtr Rocs::GraphStructure::addPointer(DataPtr from, DataPtr to, int pointerType)
 
423
PointerPtr Rocs::GraphStructure::createPointer(DataPtr from, DataPtr to, int pointerType)
349
424
{
350
425
    bool directed = document()->pointerType(pointerType)->direction() == PointerType::Unidirectional;
351
426
    if (!directed && !multigraph()) {
352
427
        // do not add back-edges if graph is undirected
353
428
        foreach(PointerPtr pointer, from->pointerList(to)) {
354
429
            if (pointer->pointerType() == pointerType) {
355
 
                document()->engineBackend()->debug(
356
 
                    i18n("Could not add back-edge (%1->%2) to undirected graph.", from->identifier(), to->identifier()));
 
430
                emit scriptError(i18n("Could not add back-edge (%1->%2) to undirected graph.", from->identifier(), to->identifier()));
357
431
                return PointerPtr();
358
432
            }
359
433
        }
363
437
        PointerList list = from->outPointerList();
364
438
        foreach(PointerPtr tmp, list) {
365
439
            if (tmp->to() == to && tmp->pointerType() == pointerType) {
366
 
                document()->engineBackend()->debug(
 
440
                emit scriptError(
367
441
                    i18n("Could not add existing edge (%1->%2): this graph is no multigraph.", from->identifier(), to->identifier()));
368
442
                return PointerPtr();
369
443
            }
370
444
        }
371
445
    }
372
446
 
373
 
    return DataStructure::addPointer(from, to, pointerType);
 
447
    return DataStructure::createPointer(from, to, pointerType);
374
448
}
375
449
 
376
 
DataPtr Rocs::GraphStructure::addData(const QString& name, int dataType)
 
450
DataPtr Rocs::GraphStructure::createData(const QString& name, int dataType)
377
451
{
378
452
    if (readOnly()) {
379
453
        return DataPtr();
382
456
                                         GraphNode::create(getDataStructure(), generateUniqueIdentifier(), dataType)
383
457
                                     );
384
458
    n->setProperty("name", name);
385
 
    return addData(n, dataType);
 
459
    return addData(n);
386
460
}
387
461
 
388
462
QMap<QString, QString> Rocs::GraphStructure::pluginProperties() const