Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add conflict-free invariant check to Configuration
[simgrid.git] / src / mc / explo / udpor / UnfoldingEvent.cpp
index ee6de7cba164fc300463b96671b5e94772e5f15a..83e1d7dba3e3b2197d284e306cfce2e6b2064d26 100644 (file)
@@ -4,10 +4,11 @@
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "src/mc/explo/udpor/UnfoldingEvent.hpp"
+#include "src/mc/explo/udpor/History.hpp"
 
 namespace simgrid::mc::udpor {
 
-UnfoldingEvent::UnfoldingEvent(std::initializer_list<UnfoldingEvent*> init_list)
+UnfoldingEvent::UnfoldingEvent(std::initializer_list<const UnfoldingEvent*> init_list)
     : UnfoldingEvent(EventSet(std::move(init_list)))
 {
 }
@@ -34,4 +35,60 @@ bool UnfoldingEvent::operator==(const UnfoldingEvent& other) const
   return this->immediate_causes == other.immediate_causes;
 }
 
+EventSet UnfoldingEvent::get_history() const
+{
+  return History(this).get_all_events();
+}
+
+bool UnfoldingEvent::related_to(const UnfoldingEvent* other) const
+{
+  return this->in_history_of(other) or other->in_history_of(this);
+}
+
+bool UnfoldingEvent::in_history_of(const UnfoldingEvent* other) const
+{
+  return History(other).contains(this);
+}
+
+bool UnfoldingEvent::conflicts_with(const UnfoldingEvent* other) const
+{
+  // Events that have a causal relation never are in conflict
+  // in an unfolding structure. Two events in conflict must
+  // not be contained in each other's histories
+  if (related_to(other)) {
+    return false;
+  }
+
+  const EventSet my_history      = get_history();
+  const EventSet other_history   = other->get_history();
+  const EventSet unique_to_me    = my_history.subtracting(other_history);
+  const EventSet unique_to_other = other_history.subtracting(my_history);
+
+  for (const auto e_me : unique_to_me) {
+    for (const auto e_other : unique_to_other) {
+      if (e_me->has_conflicting_transition_with(e_other)) {
+        return true;
+      }
+    }
+  }
+  return false;
+}
+
+bool UnfoldingEvent::conflicts_with(const Configuration& config) const
+{
+  // A configuration is itself already conflict-free. Thus, it is
+  // simply a matter of testing whether or not the transition associated
+  // with the event is dependent with any already in `config` that are
+  // OUTSIDE this event's history (in an unfolding, events only conflict
+  // if they are not related)
+  const EventSet potential_conflicts = config.get_events().subtracting(get_history());
+  return std::any_of(potential_conflicts.cbegin(), potential_conflicts.cend(),
+                     [&](const UnfoldingEvent* e) { return this->has_conflicting_transition_with(e); });
+}
+
+bool UnfoldingEvent::has_conflicting_transition_with(const UnfoldingEvent* other) const
+{
+  return associated_transition->depends(other->associated_transition.get());
+}
+
 } // namespace simgrid::mc::udpor