TopologyManager.cpp
Go to the documentation of this file.
1 
18 #include "TopologyManager.hpp"
19 
20 namespace ISM {
21 
23  {
25  }
26 
28  {
29 
30  std::ostringstream oss;
31  oss << "Generating random Topologies until a valid one is found:" << std::endl;
32  LogHelper::logMessage(oss.str(), LOG_INFO);
33 
34  TopologyPtr randomTopology = mTopologyGenerator->generateRandomTopology(mCurrentPatternName);
35  oss.str("");
36  oss << std::endl << std::endl << randomTopology->objectRelations;
37  LogHelper::logMessage(oss.str(), LOG_INFO);
38  evaluateTopology(randomTopology);
39  bool foundValidTopology = randomTopology->isValid;
40 
41  while(!foundValidTopology)
42  {
43  oss.str("");
44  oss << "Random Topology was invalid, generating a new one." << std::endl;
45  LogHelper::logMessage(oss.str(), LOG_INFO);
46  randomTopology = mTopologyGenerator->generateRandomTopology(mCurrentPatternName);
47  oss.str("");
48  oss << std::endl << std::endl << randomTopology->objectRelations;
49  LogHelper::logMessage(oss.str(), LOG_INFO);
50 
51  evaluateTopology(randomTopology);
52 
53  foundValidTopology = randomTopology->isValid;
54  }
55 
57 
58  return randomTopology;
59  }
60 
62  {
63  while (mNeighbourIndex < mNeighbourTopologies.size())
64  {
66  prepareTopology(topology);
67 
68  if (topology->isValid)
69  {
70  return true;
71  }
73  }
74 
75  return false;
76  }
77 
79  {
80  markSelectedTopology(instance);
81  logSelectedTopology(instance);
82  mDocumentationHelper->setReferenceTopology(instance, mCurrentPatternName);
83  mHistory.push_back(std::vector<std::pair<TopologyPtr, unsigned int>>());
84  mHistoryIndex++;
85 
86  mNeighbourIndex = 0;
87  mNeighbourTopologies = mTopologyGenerator->generateNeighbours(mCurrentPatternName, instance);
88  }
89 
90  std::vector<TopologyPtr> TopologyManager::prepareStartTopologies(std::vector<TopologyPtr>& startTopologies)
91  {
92  std::vector<TopologyPtr> validStartTopologies;
93  for (TopologyPtr topology : startTopologies)
94  {
95  prepareTopology(topology);
96  if (topology->isValid)
97  {
98  validStartTopologies.push_back(topology);
99  }
100  }
101 
102  return validStartTopologies;
103  }
104 
105  void TopologyManager::setUp(const std::string & patternName)
106  {
108  mNumTopology = 1;
109 
110  mHistory.clear();
111  mHistory.push_back(std::vector<std::pair<TopologyPtr, unsigned int>>());
112  mHistoryIndex = 0;
113 
115  {
116  mPatternToTopologyIdToTopolgy[mCurrentPatternName] = std::map<std::string, TopologyPtr>();
117  }
118  }
119 
120  std::vector<std::vector<std::pair<TopologyPtr, unsigned int>>> TopologyManager::getHistory()
121  {
122  return mHistory;
123  }
124 
126  {
127  TopologyPtr fullyMeshed = mTopologyGenerator->generateFullyMeshedTopology(mCurrentPatternName);
128  std::stringstream oss;
129  oss << "Analysing the fully meshed topology: " << std::endl << std::endl
130  << fullyMeshed->objectRelations;
131  LogHelper::logMessage(oss.str(), LOG_INFO);
132  TreePtr tree = evaluateTopology(fullyMeshed, "fully_meshed_topology");
134 
135  if (storeFullyMeshedISM)
136  {
137  mDocumentationHelper->storeIsm((mCurrentPatternName + "_fully_meshed"), tree->getISM());
138  }
139 
140  return fullyMeshed;
141  }
142 
143  void TopologyManager::addStartTopologiesToHistory(std::vector<TopologyPtr> startTopologies)
144  {
145  for (TopologyPtr startTopology : startTopologies)
146  {
147  startTopology->index = mPatternToTopologyIndexCounter[mCurrentPatternName]++;
148  mHistory[mHistoryIndex].push_back(std::make_pair(startTopology, 0));
149 
150  }
151  }
152 
153  std::vector<TopologyPtr> TopologyManager::getStarTopologies()
154  {
155  std::ostringstream oss;
156  std::vector<TopologyPtr> starTopologies = mTopologyGenerator->generateStarTopologies(mCurrentPatternName);
157 
158  for (unsigned int i = 0; i < starTopologies.size(); ++i)
159  {
160  TopologyPtr starTopology = starTopologies[i];
161 
162  oss << "Analysing star topology " << (i + 1) << ": " << std::endl << std::endl
163  << starTopology->objectRelations;
164  LogHelper::logMessage(oss.str(), LOG_INFO);
165 
166  evaluateTopology(starTopology, "star_topology_" + std::to_string(i));
167 
168  oss.str("");
170  }
171 
172  return starTopologies;
173  }
174 
176  {
177  for (unsigned int i = 0; i < mHistory[mHistoryIndex].size(); ++i)
178  {
179  if (mHistory[mHistoryIndex][i].first->identifier == selectedTopology->identifier)
180  {
181  mHistory[mHistoryIndex][i].second = 1;
182  break;
183  }
184  }
185  }
186 
187  TreePtr TopologyManager::evaluateTopology(TopologyPtr& topology, const std::string filename)
188  {
189  TreePtr tree = TreePtr(new Tree(mCurrentPatternName, topology->objectRelations));
190 
191  if (mTreeValidator->isTreeValid(tree))
192  {
193  EvaluationResult er = mEvaluator->evaluate(mCurrentPatternName, tree->getISM());
194 
195  topology->evaluationResult = er;
196  topology->isValid = true;
197 
198  if (!filename.empty())
199  {
200  mDocumentationHelper->storeTopology(topology, mCurrentPatternName, filename,
201  tree->getISM()->voteSpecifiersPerObject);
202  }
203  }
204  else
205  {
206  topology->isValid = false;
207  }
208 
209  mPatternToTopologyIdToTopolgy[mCurrentPatternName][topology->identifier] = topology;
210  return tree;
211  }
212 
214  {
215  std::ostringstream oss;
216  oss << "Now analysing the following relation topology (Topology number "
217  << mNumTopology << ") : " << std::endl << std::endl << topology->objectRelations;
218  LogHelper::logMessage(oss.str(), LOG_INFO);
219 
220  const std::string identifier = topology->identifier;
221 
222  if (mPatternToTopologyIdToTopolgy[mCurrentPatternName].find(identifier) ==
224  {
225  evaluateTopology(topology, "topology_" + std::to_string(mPatternToTopologyIndexCounter[mCurrentPatternName]));
226 
227  if (topology->isValid)
228  {
230  }
231  mNumTopology++;
232  }
233  else
234  {
235  topology = mPatternToTopologyIdToTopolgy[mCurrentPatternName][identifier];
236  logAlreadyAnalysed(topology);
237  }
238 
239  if (topology->isValid)
240  {
241  mHistory[mHistoryIndex].push_back(std::make_pair(topology, 0));
242  }
243 
245  }
246 
248  {
249  std::ostringstream oss;
250  oss << "Already analyzed this topology before!" << std::endl << std::endl;
251  if (topology->isValid)
252  {
253  oss << "The corresponding tree was valid and the evaluation result of its ISM was: " << std::endl
254  << topology->evaluationResult.getDescription();
255  }
256  else
257  {
258  oss << " The corresponding tree was invalid!";
259  oss << std::endl;
260  }
261  LogHelper::logMessage(oss.str(), LOG_INFO);
262  }
263 
265  {
266  std::ostringstream oss;
267  oss << "Selected the following topology : "
268  << std::endl << std::endl << selectedTopology->objectRelations
269  << "with the evaluation result of its ISM beeing: " << std::endl
270  << selectedTopology->evaluationResult.getDescription() << std::endl << std::endl
271  << "The cost of this topology is: " << std::endl
272  << selectedTopology->cost;
273  LogHelper::logMessage(oss.str(), LOG_INFO);
275  }
276 }
277 
278 
TopologyPtr getFullyMeshedTopology(bool storeFullyMeshedISM=false)
std::map< std::string, std::map< std::string, TopologyPtr > > mPatternToTopologyIdToTopolgy
void markSelectedTopology(TopologyPtr selectedTopology)
TopologyPtr getNextNeighbour()
static void logLine(LogLevel logLevel=LOG_INFO)
Definition: LogHelper.cpp:101
std::vector< std::vector< std::pair< TopologyPtr, unsigned int > > > mHistory
TopologyGeneratorPtr mTopologyGenerator
std::vector< TopologyPtr > mNeighbourTopologies
std::string patternName
void logAlreadyAnalysed(TopologyPtr topology)
void logSelectedTopology(TopologyPtr selectedTopology)
boost::shared_ptr< Topology > TopologyPtr
Definition: Topology.hpp:51
TreePtr evaluateTopology(TopologyPtr &topology, const std::string filename="")
static void logMessage(const std::string &message, LogLevel logLevel=LOG_INFO, const char *logColor=LOG_COLOR_DEFAULT)
Definition: LogHelper.cpp:96
void setUp(const std::string &patternName)
std::map< std::string, unsigned int > mPatternToTopologyIndexCounter
std::vector< TopologyPtr > prepareStartTopologies(std::vector< TopologyPtr > &startTopologies)
boost::shared_ptr< Tree > TreePtr
Definition: typedef.hpp:46
void prepareTopology(TopologyPtr &topology)
DocumentationHelperPtr mDocumentationHelper
TopologyPtr getRandomTopology()
std::string mCurrentPatternName
std::vector< std::vector< std::pair< TopologyPtr, unsigned int > > > getHistory()
this namespace contains all generally usable classes.
std::vector< TopologyPtr > getStarTopologies()
void addStartTopologiesToHistory(std::vector< TopologyPtr > startTopologies)
void setReferenceInstance(TopologyPtr instance)
TreeValidatorPtr mTreeValidator


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:41