Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Slay some nested-if-statements-from-hell
[simgrid.git] / src / mc / LivenessChecker.cpp
index a42f5c4..552609e 100644 (file)
@@ -57,7 +57,6 @@ VisitedPair::VisitedPair(
   this->automaton_state = automaton_state;
   this->num = pair_num;
   this->other_num = -1;
-  this->acceptance_pair = 0;
   this->atomic_propositions = atomic_propositions;
 }
 
@@ -135,26 +134,26 @@ std::shared_ptr<VisitedPair> LivenessChecker::insertAcceptancePair(simgrid::mc::
   std::shared_ptr<VisitedPair> new_pair = std::make_shared<VisitedPair>(
     pair->num, pair->automaton_state, pair->atomic_propositions,
     pair->graph_state);
-  new_pair->acceptance_pair = 1;
 
   auto res = std::equal_range(acceptancePairs_.begin(), acceptancePairs_.end(),
     new_pair.get(), simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
 
-  if (pair->search_cycle == 1)
-    for (auto i = res.first; i != res.second; ++i) {
-      std::shared_ptr<simgrid::mc::VisitedPair> const& pair_test = *i;
-      if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
-        if (pair_test->atomic_propositions == new_pair->atomic_propositions) {
-          if (this->compare(pair_test.get(), new_pair.get()) == 0) {
-            XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
-            livenessStack_.pop_back();
-            if (dot_output != nullptr)
-              fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
-            return nullptr;
-          }
-        }
-      }
-    }
+  if (pair->search_cycle) for (auto i = res.first; i != res.second; ++i) {
+    std::shared_ptr<simgrid::mc::VisitedPair> const& pair_test = *i;
+    if (xbt_automaton_state_compare(
+          pair_test->automaton_state, new_pair->automaton_state) != 0
+        || pair_test->atomic_propositions != new_pair->atomic_propositions
+        || this->compare(pair_test.get(), new_pair.get()) != 0)
+      continue;
+    XBT_INFO("Pair %d already reached (equal to pair %d) !",
+      new_pair->num, pair_test->num);
+    livenessStack_.pop_back();
+    if (dot_output != nullptr)
+      fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
+        initial_global_state->prev_pair, pair_test->num,
+        initial_global_state->prev_req);
+    return nullptr;
+  }
 
   acceptancePairs_.insert(res.first, new_pair);
   return new_pair;
@@ -195,7 +194,7 @@ void LivenessChecker::prepare(void)
           MC_state_interleave_process(initial_pair->graph_state.get(), &p.copy);
 
       initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state.get());
-      initial_pair->search_cycle = 0;
+      initial_pair->search_cycle = false;
 
       livenessStack_.push_back(initial_pair);
     }
@@ -282,24 +281,23 @@ int LivenessChecker::insertVisitedPair(std::shared_ptr<VisitedPair> visited_pair
 
   for (auto i = range.first; i != range.second; ++i) {
     VisitedPair* pair_test = i->get();
-    if (xbt_automaton_state_compare(pair_test->automaton_state, visited_pair->automaton_state) == 0) {
-      if (pair_test->atomic_propositions == visited_pair->atomic_propositions) {
-        if (this->compare(pair_test, visited_pair.get()) == 0) {
-          if (pair_test->other_num == -1)
-            visited_pair->other_num = pair_test->num;
-          else
-            visited_pair->other_num = pair_test->other_num;
-          if (dot_output == nullptr)
-            XBT_DEBUG("Pair %d already visited ! (equal to pair %d)",
-            visited_pair->num, pair_test->num);
-          else
-            XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))",
-              visited_pair->num, pair_test->num, visited_pair->other_num);
-          (*i) = std::move(visited_pair);
-          return (*i)->other_num;
-        }
-      }
-    }
+    if (xbt_automaton_state_compare(
+          pair_test->automaton_state, visited_pair->automaton_state) != 0
+        || pair_test->atomic_propositions != visited_pair->atomic_propositions
+        || this->compare(pair_test, visited_pair.get()) != 0)
+        continue;
+    if (pair_test->other_num == -1)
+      visited_pair->other_num = pair_test->num;
+    else
+      visited_pair->other_num = pair_test->other_num;
+    if (dot_output == nullptr)
+      XBT_DEBUG("Pair %d already visited ! (equal to pair %d)",
+      visited_pair->num, pair_test->num);
+    else
+      XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))",
+        visited_pair->num, pair_test->num, visited_pair->other_num);
+    (*i) = std::move(visited_pair);
+    return (*i)->other_num;
   }
 
   visitedPairs_.insert(range.first, std::move(visited_pair));
@@ -398,16 +396,14 @@ int LivenessChecker::main(void)
 
     if (current_pair->requests > 0) {
 
-      if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
-        /* If new acceptance pair, return new pair */
-        if ((reached_pair = this->insertAcceptancePair(current_pair)) == nullptr) {
-          this->showAcceptanceCycle(current_pair->depth);
-          return SIMGRID_MC_EXIT_LIVENESS;
-        }
+      if (current_pair->automaton_state->type == 1 && !current_pair->exploration_started
+          && (reached_pair = this->insertAcceptancePair(current_pair)) == nullptr) {
+        this->showAcceptanceCycle(current_pair->depth);
+        return SIMGRID_MC_EXIT_LIVENESS;
       }
 
       /* Pair already visited ? stop the exploration on the current path */
-      if ((current_pair->exploration_started == 0)
+      if ((!current_pair->exploration_started)
         && (visited_num = this->insertVisitedPair(
           reached_pair, current_pair)) != -1) {
 
@@ -445,7 +441,7 @@ int LivenessChecker::main(void)
 
          /* Update mc_stats */
          mc_stats->executed_transitions++;
-         if(current_pair->exploration_started == 0)
+         if (!current_pair->exploration_started)
            mc_stats->visited_pairs++;
 
          /* Answer the request */
@@ -455,7 +451,7 @@ int LivenessChecker::main(void)
          mc_model_checker->wait_for_requests();
 
          current_pair->requests--;
-         current_pair->exploration_started = 1;
+         current_pair->exploration_started = true;
 
          /* Get values of atomic propositions (variables used in the property formula) */
          std::vector<int> prop_values = this->getPropositionValues();
@@ -480,7 +476,7 @@ int LivenessChecker::main(void)
 
               /* FIXME : get search_cycle value for each acceptant state */
               if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
-                next_pair->search_cycle = 1;
+                next_pair->search_cycle = true;
 
               /* Add new pair to the exploration stack */
               livenessStack_.push_back(next_pair);
@@ -535,7 +531,6 @@ int LivenessChecker::run()
 
   XBT_DEBUG("Starting the liveness algorithm");
 
-
   /* Create the initial state */
   simgrid::mc::initial_global_state = std::unique_ptr<s_mc_global_t>(new s_mc_global_t());