sensornavigator.cpp 15.9 KB
Newer Older
Alessio Netti's avatar
Alessio Netti committed
1
2
3
4
//
// Created by Netti, Alessio on 11.12.18.
//

5
#include "sensornavigator.h"
Alessio Netti's avatar
Alessio Netti committed
6

Alessio Netti's avatar
Alessio Netti committed
7
8
const string SensorNavigator::rootKey = "__root__";
const string SensorNavigator::templateKey = "__template__";
Alessio Netti's avatar
Alessio Netti committed
9
const char SensorNavigator::pathSeparator = '/';
10

Alessio Netti's avatar
Alessio Netti committed
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
void SensorNavigator::clearTree() {
    if(_sensorTree) {
        _sensorTree->clear();
        delete _sensorTree;
        _sensorTree = NULL;
    }
    if(_hierarchy) {
        _hierarchy->clear();
        delete _hierarchy;
        _hierarchy = NULL;
    }
    _treeDepth = -1;
    _usingTopics = false;
}

bool SensorNavigator::nodeExists(const string& node) {
    return _sensorTree && _sensorTree->count(node) && !isSensorNode(node);
}

bool SensorNavigator::sensorExists(const string& node) {
    return _sensorTree && _sensorTree->count(node) && isSensorNode(node);
}

string SensorNavigator::buildTopicForNode(const string& node, const string& suffix, int len) {
    if(!_sensorTree || !_sensorTree->count(node) || isSensorNode(node))
        throw domain_error("SensorNavigator: node not found in tree!");
Alessio Netti's avatar
Alessio Netti committed
37
38
    
    return MQTTChecker::formatTopic(getNodeTopic(node)) + MQTTChecker::formatTopic(suffix);
Alessio Netti's avatar
Alessio Netti committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
}

bool SensorNavigator::isSensorNode(const string& node) {
    if(!_sensorTree || !_sensorTree->count(node))
        throw domain_error("SensorNavigator: node not found in tree!");

    return _isSensorNode(_sensorTree->at(node));
}

int SensorNavigator::getNodeDepth(const string& node) {
    if(!_sensorTree || !_sensorTree->count(node))
        throw domain_error("SensorNavigator: node not found in tree!");

    return _sensorTree->at(node).depth;
}

string SensorNavigator::getNodeTopic(const string& node) {
    if(!_sensorTree || !_sensorTree->count(node))
        throw domain_error("SensorNavigator: node not found in tree!");

    return _usingTopics ? _sensorTree->at(node).topic : node;
}

void SensorNavigator::buildTree(const string& hierarchy, const vector<string> *sensors, const vector<string> *topics, const string& delimiter) {
63
64
    //if( hierarchy == "" )
    //    throw invalid_argument("SensorNavigator: cannot build sensor hierarchy!");
Alessio Netti's avatar
Alessio Netti committed
65
66
67
68

    vector<string> hierarchyVec;
    string hBuf = hierarchy;
    size_t pos;
69
    if(!hierarchy.empty())
70
71
72
73
74
75
76
77
        while( !hBuf.empty() ) {
            pos = hBuf.find(delimiter);
            hierarchyVec.push_back(hBuf.substr(0, pos));
            if(pos == std::string::npos)
                hBuf.clear();
            else
                hBuf.erase(0, pos + delimiter.length());
        }
Alessio Netti's avatar
Alessio Netti committed
78
79
80
81
    buildTree(&hierarchyVec, sensors, topics);
}

void SensorNavigator::buildTree(const vector<string> *hierarchy, const vector<string> *sensors, const vector<string> *topics) {
82
    if(sensors && sensors->size() > 0)
Alessio Netti's avatar
Alessio Netti committed
83
84
85
86
        clearTree();
    else
        throw invalid_argument("SensorNavigator: cannot build sensor hierarchy!");

87
    bool autoMode = false;
88
    boost::regex filterReg(_filter);
89
90
91
92
93
94
95
96
97
98
99
100
101
    if(!hierarchy || hierarchy->empty()) {
        _hierarchy = NULL;
        autoMode = true;
    }
    else {
        _hierarchy = new vector<boost::regex>();
        string s = "";
        //Each level in the hierarchy includes the regular expressions at the upper levels
        for (const auto &l: *hierarchy) {
            s += l;
            _hierarchy->push_back(boost::regex(s));
        }
        autoMode = false;
Alessio Netti's avatar
Alessio Netti committed
102
103
104
105
106
107
108
109
110
    }

    _usingTopics = topics != NULL;
    _sensorTree = new map<string, Node>();

    //We initialize the sensor tree by pushing the root supernode
    Node rootNode = {-1, set<string>(), set<string>(), "", ""};
    _sensorTree->insert(make_pair(rootKey, rootNode));
    //We iterate over all sensor names that were supplied and build up the tree
Alessio Netti's avatar
Alessio Netti committed
111
    for(int i=0; i < (int)sensors->size(); i++)
112
113
114
115
116
117
        if(_filter=="" || boost::regex_search(sensors->at(i).c_str(), _match, filterReg)) {
            if (autoMode)
                _addAutoSensor(sensors->at(i), topics ? topics->at(i) : "");
            else
                _addSensor(sensors->at(i), topics ? topics->at(i) : "");
        }
Alessio Netti's avatar
Alessio Netti committed
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
}

void SensorNavigator::buildCassandraTree(const map<string, vector<string> > *table, const string& root, const string& ignore) {
    if(table->size() > 0 && table->count(root))
        clearTree();
    else
        throw invalid_argument("SensorNavigator: cannot build sensor hierarchy!");

    _hierarchy = NULL;
    _usingTopics = false;
    _sensorTree = new map<string, Node>();

    boost::regex ignoreReg(ignore);

    //We initialize the sensor tree by pushing the root supernode
    Node rootNode = {-1, set<string>(), set<string>(), "", ""};
    _sensorTree->insert(make_pair(rootKey, rootNode));
    //We manually add the root's children to the tree, so as to map the Cassandra root key to the one we use
    for(const auto& s : table->at(root)) {
        if(!boost::regex_search(s.c_str(), _match, ignoreReg)) {
            Node newNode = {0, set<string>(), set<string>(), rootKey, ""};
            _sensorTree->insert(make_pair(s, newNode));
            if (table->count(s)) {
                _sensorTree->at(rootKey).children.insert(s);
                _addCassandraSensor(s, table, 1, ignoreReg);
            } else {
                //If the entry is a sensor it will share the same depth as its father, hence the -1 decrease
                _sensorTree->at(s).depth -= 1;
                _sensorTree->at(rootKey).sensors.insert(s);
            }
        }
    }
}

map<string, SensorNavigator::Node> *SensorNavigator::getSubTree(const string& node, int depth) {
    if(!_sensorTree || !_sensorTree->count(node))
        throw domain_error("SensorNavigator: node not found in tree!");

    if( depth < -1 )
        throw out_of_range("SensorNavigator: depth not valid for subtree query!");

    //We start by picking the start node, then we call the recursive routine
    map<string, SensorNavigator::Node> *m = new map<string, Node>();
    m->insert(make_pair(node,_sensorTree->at(node)));
    _getSubTree(node, m, depth);
    return m;
}

set<string> *SensorNavigator::getNodes(int depth, bool recursive) {
    if( depth < -1 || depth > _treeDepth )
        throw out_of_range("SensorNavigator: depth not valid for node query!");

    if(!_sensorTree)
        throw runtime_error("SensorNavigator: sensor tree not initialized!");

    //We pick all nodes in the tree whose depth is consistent with the input
    //Iterating over the node map is sufficient for this purpose
    set<string> *vec = new set<string>();
    for(const auto& n : *_sensorTree)
        if(!_isSensorNode(n.second) && (n.second.depth == depth || (recursive && n.second.depth >= depth)))
            vec->insert(n.first);

    return vec;
}

set<string> *SensorNavigator::getNodes(const string& node, bool recursive) {
    if(!_sensorTree || !_sensorTree->count(node))
        throw domain_error("SensorNavigator: node not found in tree!");

    if(isSensorNode(node))
        throw invalid_argument("SensorNavigator: input must be a node, not a sensor!");

    //We start by picking all children of the start node, then we proceed in the subtree (if recursive)
    set<string> *vec = new set<string>();
    _getNodes(node, vec, recursive ? 0 : 1);
    return vec;
}

set<string> *SensorNavigator::getSensors(int depth, bool recursive) {
    if( depth < -1 || depth > _treeDepth )
        throw out_of_range("SensorNavigator: depth not valid for sensor query!");

    if(!_sensorTree)
        throw runtime_error("SensorNavigator: sensor tree not initialized!");

    //Like in getNodes, we iterate over the node map and pick all sensors that are at least at the given input depth
    set<string> *vec = new set<string>();
    for(const auto& n : *_sensorTree)
        if(!_isSensorNode(n.second) && (n.second.depth == depth || (recursive && n.second.depth >= depth)))
            vec->insert(n.second.sensors.begin(), n.second.sensors.end());

    return vec;
}

set<string> *SensorNavigator::getSensors(const string& node, bool recursive) {
    if(!_sensorTree || !_sensorTree->count(node))
        throw domain_error("SensorNavigator: node not found in tree!");

    if(isSensorNode(node))
        throw invalid_argument("SensorNavigator: input must be a node, not a sensor!");

    //We start by picking all sensors of the start node, then we call the recursive routine
    set<string> *vec = new set<string>();
    _getSensors(node, vec, recursive ? 0 : 1);
    return vec;
}

set<string> *SensorNavigator::navigate(const string& node, int direction) {
    if(!_sensorTree || !_sensorTree->count(node))
        throw domain_error("SensorNavigator: node not found in tree!");

    set<string> *vec = new set<string>();
230
231
232
233
    if(direction == 0)
        vec->insert(node);
        //if direction is negative, we go up in the tree
    else if(direction < 0) {
Alessio Netti's avatar
Alessio Netti committed
234
235
236
237
238
239
240
241
        int ctr = -direction;
        string currNode = node;
        //We go up the sensor tree in iterative fashion
        while (ctr-- > 1 && currNode != rootKey) {
            currNode = _sensorTree->at(currNode).parent;
        }
        vec->insert(_sensorTree->at(currNode).parent);
    }
242
        //Else, we go down to the children
Alessio Netti's avatar
Alessio Netti committed
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
    else {
        _getNodes(node, vec, direction);
    }
    return vec;
}

void SensorNavigator::_getNodes(const string& node, set<string> *vec, int depth) {
    if(!_sensorTree || !_sensorTree->count(node))
        return;

    if(depth<=1)
        vec->insert(_sensorTree->at(node).children.begin(), _sensorTree->at(node).children.end());
    //We iterate over all children of the input node, and call the recursive routine over them
    //depth=1 implies that we are at the last level of the search; in this case, we do not continue
    //an input value of depth<1 implies that the search must proceed for the entire subtree
    if(depth!=1)
        for(const auto& n : _sensorTree->at(node).children)
            _getNodes(n, vec, depth-1);
}

void SensorNavigator::_getSensors(const string& node, set<string> *vec, int depth) {
    if(!_sensorTree || !_sensorTree->count(node))
        return;

    if(depth<=1)
        vec->insert(_sensorTree->at(node).sensors.begin(), _sensorTree->at(node).sensors.end());
    //We iterate over all children of the input node, and call the recursive routine over them
    //Works the same as _getNodes; here, depth does not really refer to the depth level in the tree (as sensors share
    //the depth level of the node they belong to) but rather to the search steps performed. A depth of 1 means that
    //only the sensors of the original node will be added, 2 means that the sensors of its children will be added, etc.
    if(depth!=1)
        for(const auto& n : _sensorTree->at(node).children)
            _getSensors(n, vec, depth-1);
}

void SensorNavigator::_getSubTree(const string& node, map<string, SensorNavigator::Node> *m, int depth) {
    if(!_sensorTree || !_sensorTree->count(node))
        return;

    for(const auto& s : _sensorTree->at(node).sensors)
        m->insert(make_pair(s,_sensorTree->at(s)));

    //We iterate over all children of the input node, and add them to the subtree
    if(_sensorTree->at(node).depth < depth)
        for(const auto& n : _sensorTree->at(node).children) {
            m->insert(make_pair(n,_sensorTree->at(n)));
            _getSubTree(n, m, depth);
        }
}

void SensorNavigator::_addSensor(const string& name, const string& topic) {
    string last=rootKey, prev="";

    int d=0;
    //We verify how deep in the hierarchy the input sensor is by progressively matching it with the hierarchical regexes
Alessio Netti's avatar
Alessio Netti committed
298
    for(d=0; d < (int)_hierarchy->size(); d++)
Alessio Netti's avatar
Alessio Netti committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
        //If we have a match with regex at level d, the sensor belongs to its subtree
        if( boost::regex_search(name.c_str(), _match, _hierarchy->at(d)) ) {
            //prev contains the name of the parent for the current node in the subtree
            prev = last;
            //last is the name of the node we are currently at; the sensor lies in this subtree
            last = _match.str(0);

            //If this node has never been instantiated, we create it now
            if( !_sensorTree->count(last) ) {
                Node newNode = {d, set<string>(), set<string>(), prev, topic};
                _sensorTree->insert(make_pair(last, newNode));
                //we add the current node to the list of children of its parent
                _sensorTree->at(prev).children.insert(last);
            }
            else if(_usingTopics) {
                // Intersecting the topics from the new sensor and the existing hierarchical node
                int pCtr = 0;
                Node& target = _sensorTree->at(last);
Alessio Netti's avatar
Alessio Netti committed
317
                while( pCtr < (int)topic.length() && pCtr < (int)target.topic.length() && target.topic[pCtr] == topic[pCtr])
Alessio Netti's avatar
Alessio Netti committed
318
319
320
321
322
323
324
325
326
327
328
329
330
                    pCtr++;
                target.topic = target.topic.substr(0, pCtr);
            }

            //Refreshing tree depth
            if( d > _treeDepth )
                _treeDepth = d;
        } else
            break;
    //Once out of the loop, we know to which node the sensor actually belongs
    _sensorTree->at(last).sensors.insert(name);
    //We add the sensor node corresponding to the current sensor
    Node sNode = {d-1, set<string>(), set<string>(), last, topic};
331
332
333
334
335
    _sensorTree->insert(make_pair(name, sNode));
}

void SensorNavigator::_addAutoSensor(const string& name, const string& topic) {
    string last=rootKey, prev="";
Alessio Netti's avatar
Alessio Netti committed
336
337
    //sepPos=0 implies that the first character is skipped when searching for separators
    size_t sepPos = 0;
338
339
    int d=0;

Alessio Netti's avatar
Alessio Netti committed
340
    if(name.empty() || name[name.size()-1] == pathSeparator) {
Alessio Netti's avatar
Alessio Netti committed
341
342
343
344
        clearTree();
        throw invalid_argument("SensorNavigator: sensor " + name + " does not describe a valid tree path!");
    }

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
    //We verify how deep in the hierarchy the input sensor is by searching for the appropriate path separators
    while((sepPos = name.find(pathSeparator, sepPos+1)) != std::string::npos) {
        //prev contains the name of the parent for the current node in the subtree
        prev = last;
        //last is the name of the node we are currently at; the sensor lies in this subtree
        last = name.substr(0, sepPos+1);

        //If this node has never been instantiated, we create it now
        if( !_sensorTree->count(last) ) {
            Node newNode = {d, set<string>(), set<string>(), prev, topic};
            _sensorTree->insert(make_pair(last, newNode));
            //we add the current node to the list of children of its parent
            _sensorTree->at(prev).children.insert(last);
        }
        else if(_usingTopics) {
            // Intersecting the topics from the new sensor and the existing hierarchical node
            int pCtr = 0;
            Node& target = _sensorTree->at(last);
            while( pCtr < (int)topic.length() && pCtr < (int)target.topic.length() && target.topic[pCtr] == topic[pCtr])
                pCtr++;
            target.topic = target.topic.substr(0, pCtr);
        }

        //Refreshing tree depth
        if( d > _treeDepth )
            _treeDepth = d;
        d++;
    }

    //Once out of the loop, we know to which node the sensor actually belongs
    _sensorTree->at(last).sensors.insert(name);
    //We add the sensor node corresponding to the current sensor
    Node sNode = {d-1, set<string>(), set<string>(), last, topic};
Alessio Netti's avatar
Alessio Netti committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
    _sensorTree->insert(make_pair(name, sNode));
}

void SensorNavigator::_addCassandraSensor(const string& name, const map<string, vector<string> > *table, int depth, boost::regex ignore) {
    for(const auto& s : table->at(name)) {
        if (!boost::regex_search(s.c_str(), _match, ignore)) {
            Node newNode = {depth, set<string>(), set<string>(), name, ""};
            if (!_sensorTree->count(s))
                _sensorTree->insert(make_pair(s, newNode));
            //else
            //    LOG(warning) << "Node " << s << " has multiple parents!";
            //If s is in the table, then it must be a group, otherwise it is a sensor
            if (table->count(s)) {
                _sensorTree->at(name).children.insert(s);
                _addCassandraSensor(s, table, depth + 1, ignore);
            } else {
                _sensorTree->at(s).depth -= 1;
                _sensorTree->at(name).sensors.insert(s);
            }
        }
    }
}