ConnectivityChecker.cpp
Go to the documentation of this file.
1 
18 #include "ConnectivityChecker.hpp"
19 #include <iostream>
20 
21 namespace ISM
22 {
23 
24  ConnectivityChecker::ConnectivityChecker(std::map<std::string, unsigned int> numObjectsPerPattern)
25  {
26  mNumObjectsPerPattern = numObjectsPerPattern;
27  for (std::map<std::string, unsigned int>::iterator it = numObjectsPerPattern.begin(); it != numObjectsPerPattern.end(); ++it)
28  {
29  unsigned int numObjects = it->second;
30 
31  std::vector<Relation> relations;
32 
33  for (unsigned int i = 0; i < numObjects; ++i)
34  {
35  for (unsigned int j = i + 1; j < numObjects; ++j)
36  {
37  relations.push_back({i, j});
38  }
39  }
40 
41  mRelationsPerPattern[it->first] = relations;
42  }
43  }
44 
45  bool ConnectivityChecker::isConnected(std::vector<bool> bitvector, const std::string& patternName)
46  {
47  unsigned int numOnesInVector = 0;
48  for (bool bit : bitvector)
49  {
50  numOnesInVector += bit;
51  }
52 
53  if (numOnesInVector < mNumObjectsPerPattern[patternName] - 1)
54  {
55  //A topology needs at least #objects - 1 relations to be connected.
56  return false;
57  }
58 
59  std::stack<unsigned int>().swap(mToVisit);
60  mVisited.clear();
61 
62  visitNeighbours(patternName, 0, bitvector);
63 
64  while(!mToVisit.empty())
65  {
66  unsigned int toVisit = mToVisit.top();
67  mToVisit.pop();
68  visitNeighbours(patternName, toVisit, bitvector);
69  }
70 
71  return mVisited.size() == mNumObjectsPerPattern[patternName];
72  }
73 
74  void ConnectivityChecker::checkNeighbour(const::std::string &patternName, unsigned int from, unsigned int index, std::vector<bool> bitvector)
75  {
76  if (bitvector[index])
77  {
78  int neighbour = mRelationsPerPattern[patternName][index].getNeighbour(from);
79  if (mVisited.find(neighbour) == mVisited.end())
80  {
81  mToVisit.push(neighbour);
82  }
83  }
84  }
85 
86  void ConnectivityChecker::visitNeighbours(const std::string& patternName, unsigned int from, std::vector<bool> bitvector)
87  {
88  mVisited.insert(from);
89  unsigned int numObjects = mNumObjectsPerPattern[patternName];
90  int index = 0;
91  for (unsigned int i = 0; i < from; ++i)
92  {
93  checkNeighbour(patternName, from, index + from - (i + 1), bitvector);
94  index += numObjects - (i + 1);
95  }
96 
97  for (unsigned int i = index; i < index + numObjects - (from + 1); ++i)
98  {
99  checkNeighbour(patternName, from, i, bitvector);
100  }
101  }
102 }
bool isConnected(std::vector< bool > bitvector, const std::string &patternName)
std::stack< unsigned int > mToVisit
void visitNeighbours(const std::string &patternName, unsigned int from, std::vector< bool > bitvector)
std::string patternName
std::map< std::string, std::vector< Relation > > mRelationsPerPattern
void checkNeighbour(const std::string &patternName, unsigned int from, unsigned int index, std::vector< bool > bitvector)
ConnectivityChecker(std::map< std::string, unsigned int > numObjectsPerPattern)
std::map< std::string, unsigned int > mNumObjectsPerPattern
std::set< unsigned int > mVisited
this namespace contains all generally usable classes.


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40