Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix logic for mutual conflict between two events
[simgrid.git] / src / mc / explo / udpor / UnfoldingEvent.cpp
1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/mc/explo/udpor/UnfoldingEvent.hpp"
7 #include "src/mc/explo/udpor/History.hpp"
8
9 namespace simgrid::mc::udpor {
10
11 UnfoldingEvent::UnfoldingEvent(std::initializer_list<const UnfoldingEvent*> init_list)
12     : UnfoldingEvent(EventSet(std::move(init_list)))
13 {
14 }
15
16 UnfoldingEvent::UnfoldingEvent(EventSet immediate_causes, std::shared_ptr<Transition> transition)
17     : associated_transition(std::move(transition)), immediate_causes(std::move(immediate_causes))
18 {
19 }
20
21 bool UnfoldingEvent::operator==(const UnfoldingEvent& other) const
22 {
23   const bool same_actor = associated_transition->aid_ == other.associated_transition->aid_;
24   if (!same_actor)
25     return false;
26
27   // TODO: Add in information to determine which step in the sequence this actor was executed
28
29   // All unfolding event objects are created in reference to
30   // an Unfolding object which owns them. Hence, the references
31   // they contain to other events in the unfolding can
32   // be used as intrinsic identities (i.e. we don't need to
33   // recursively check if each of our causes has a `==` in
34   // the other event's causes)
35   return this->immediate_causes == other.immediate_causes;
36 }
37
38 EventSet UnfoldingEvent::get_history() const
39 {
40   return History(this).get_all_events();
41 }
42
43 bool UnfoldingEvent::related_to(const UnfoldingEvent* other) const
44 {
45   return this->in_history_of(other) or other->in_history_of(this);
46 }
47
48 bool UnfoldingEvent::in_history_of(const UnfoldingEvent* other) const
49 {
50   return History(other).contains(this);
51 }
52
53 bool UnfoldingEvent::conflicts_with(const UnfoldingEvent* other) const
54 {
55   // Events that have a causal relation never are in conflict
56   // in an unfolding structure. Two events in conflict must
57   // not be contained in each other's histories
58   if (related_to(other)) {
59     return false;
60   }
61
62   const EventSet my_history      = get_history();
63   const EventSet other_history   = other->get_history();
64   const EventSet unique_to_me    = my_history.subtracting(other_history);
65   const EventSet unique_to_other = other_history.subtracting(my_history);
66
67   const bool conflicts_with_me = std::any_of(unique_to_me.begin(), unique_to_me.end(), [&](const UnfoldingEvent* e) {
68     return e->has_dependent_transition_with(other);
69   });
70   const bool conflicts_with_other =
71       std::any_of(unique_to_other.begin(), unique_to_other.end(),
72                   [&](const UnfoldingEvent* e) { return e->has_dependent_transition_with(this); });
73   return conflicts_with_me or conflicts_with_other;
74 }
75
76 bool UnfoldingEvent::conflicts_with(const Configuration& config) const
77 {
78   // A configuration is itself already conflict-free. Thus, it is
79   // simply a matter of testing whether or not the transition associated
80   // with the event is dependent with any already in `config` that are
81   // OUTSIDE this event's history (in an unfolding, events only conflict
82   // if they are not related)
83   const EventSet potential_conflicts = config.get_events().subtracting(get_history());
84   return std::any_of(potential_conflicts.cbegin(), potential_conflicts.cend(),
85                      [&](const UnfoldingEvent* e) { return this->has_dependent_transition_with(e); });
86 }
87
88 bool UnfoldingEvent::has_dependent_transition_with(const UnfoldingEvent* other) const
89 {
90   return associated_transition->depends(other->associated_transition.get());
91 }
92
93 } // namespace simgrid::mc::udpor