1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/mc/explo/udpor/Configuration.hpp"
7 #include "src/mc/explo/udpor/History.hpp"
8 #include "src/mc/explo/udpor/UnfoldingEvent.hpp"
9 #include "xbt/asserts.h"
15 namespace simgrid::mc::udpor {
17 Configuration::Configuration(std::initializer_list<UnfoldingEvent*> events) : Configuration(EventSet(std::move(events)))
21 Configuration::Configuration(const EventSet& events) : events_(events)
23 if (!events_.is_valid_configuration()) {
24 throw std::invalid_argument("The events do not form a valid configuration");
28 void Configuration::add_event(UnfoldingEvent* e)
31 throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
34 if (this->events_.contains(e)) {
38 this->events_.insert(e);
39 this->newest_event = e;
41 // Preserves the property that the configuration is valid
43 if (!this->events_.contains(history)) {
44 throw std::invalid_argument("The newly added event has dependencies "
45 "which are missing from this configuration");
49 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
51 if (events_.empty()) {
52 return std::vector<UnfoldingEvent*>();
55 std::stack<UnfoldingEvent*> event_stack;
56 std::vector<UnfoldingEvent*> topological_ordering;
57 EventSet unknown_events = events_;
58 EventSet temporarily_marked_events;
59 EventSet permanently_marked_events;
61 while (not unknown_events.empty()) {
62 EventSet discovered_events;
63 event_stack.push(*unknown_events.begin());
65 while (not event_stack.empty()) {
66 UnfoldingEvent* evt = event_stack.top();
67 discovered_events.insert(evt);
69 if (not temporarily_marked_events.contains(evt)) {
70 // If this event hasn't yet been marked, do
71 // so now so that if we see it again in a child we can
72 // detect a cycle and if we see it again here
73 // we can detect that the node is re-processed
74 temporarily_marked_events.insert(evt);
76 EventSet immediate_causes = evt->get_immediate_causes();
77 if (!immediate_causes.empty() && immediate_causes.is_subset_of(temporarily_marked_events)) {
78 throw std::invalid_argument("Attempted to perform a topological sort on a configuration "
79 "whose contents contain a cycle. The configuration (and the graph "
80 "connecting all of the events) is an invalid event structure");
82 immediate_causes.subtract(discovered_events);
83 immediate_causes.subtract(permanently_marked_events);
84 const EventSet undiscovered_causes = std::move(immediate_causes);
86 for (const auto cause : undiscovered_causes) {
87 event_stack.push(cause);
90 // Mark this event as:
91 // 1. discovered across all DFSs performed
92 // 2. permanently marked
93 // 3. part of the topological search
94 unknown_events.remove(evt);
95 temporarily_marked_events.remove(evt);
96 permanently_marked_events.insert(evt);
98 // In moving this event to the end of the list,
99 // we are saying this events "happens before" other
100 // events that are added later.
101 topological_ordering.push_back(evt);
103 // Only now do we remove the event, i.e. once
104 // we've processed the same event again
109 return topological_ordering;
112 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
114 // The method exploits the property that
115 // a topological sorting S^R of the reverse graph G^R
116 // of some graph G is simply the reverse of any
117 // topological sorting S of G.
118 auto topological_events = get_topologically_sorted_events();
119 std::reverse(topological_events.begin(), topological_events.end());
120 return topological_events;
123 } // namespace simgrid::mc::udpor