@@ -63,14 +63,16 @@ void Environment::register_input_action(BaseAction* action) {
6363}
6464
6565void Environment::optimize () {
66- #ifdef GRAPH_OPTIMIZATIONS
66+ #if GRAPH_OPTIMIZATIONS
6767 constexpr bool enable_optimizations = true ;
6868#else
6969 constexpr bool enable_optimizations = false ;
7070#endif
71+ log::Debug () << " Opimizations:" << enable_optimizations;
7172 if constexpr (enable_optimizations) {
73+ log::Debug () << graph_.to_mermaid ();
7274 expand_and_merge ();
73- strip_and_optimize ();
75+ log::Debug () << optimized_graph_. to_mermaid ();
7476 } else {
7577 // no optimizations
7678 optimized_graph_ = graph_;
@@ -149,70 +151,53 @@ void Environment::expand_and_merge() {
149151 auto spanning_tree = graph_.naive_spanning_tree (source);
150152 for (auto & path : spanning_tree) {
151153 ConnectionProperties merged_properties{};
152- auto * current_source = source;
153154
154- for (auto element : path) {
155- auto property = element.first ;
155+ std::reverse (path.begin (), path.end ());
156156
157- auto return_type =
158- construction_table[std::pair<ConnectionType, ConnectionType>(merged_properties.type_ , property.type_ )];
157+ auto * previous_element = std::begin (path)->second ;
158+ for (auto it = std::begin (path); it != std::end (path); ++it) {
159+ if (std::next (it) == std::end (path)) {
160+ it->second = source;
161+ } else {
162+ it->second = std::next (it)->second ;
163+ }
164+ }
159165
160- // invalid will split the connections
161- if (return_type == Invalid) {
162- // first add connection until this point
163- optimized_graph_.add_edge (current_source, element.second , merged_properties);
166+ auto current_rating = previous_element->rating ();
164167
165- // updating the source of the connection and resetting the properties
166- current_source = element.second ;
167- merged_properties = property ;
168+ for ( auto element : path) {
169+ auto property = element.first ;
170+ current_rating += element. second -> rating () ;
168171
169- } else {
170- // merging the connections
171- merged_properties.type_ = return_type;
172+ if (current_rating > 0 ) {
173+ auto return_type =
174+ construction_table[std::pair<ConnectionType, ConnectionType>(merged_properties.type_ , property.type_ )];
175+ // invalid will split the connections
176+ if (return_type == Invalid) {
177+ // first add connection until this point
178+ optimized_graph_.add_edge (element.second , previous_element, merged_properties);
172179
173- // adding up delays
174- merged_properties.delay_ += property.delay_ ;
180+ // updating the source of the connection and resetting the properties
181+ previous_element = element.second ;
182+ merged_properties = property;
175183
176- // updating target enclave if not nullptr
177- merged_properties.enclave_ = (property.enclave_ != nullptr ) ? property.enclave_ : merged_properties.enclave_ ;
184+ } else {
185+ // merging the connections
186+ merged_properties.type_ = return_type;
178187
179- optimized_graph_.add_edge (current_source, element.second , merged_properties);
180- }
181- }
182- }
183- }
184- }
188+ // adding up delays
189+ merged_properties.delay_ += property.delay_ ;
185190
186- void Environment::strip_and_optimize () {
187- Graph<BasePort*, ConnectionProperties> striped_graph{};
188-
189- auto nodes = optimized_graph_.get_nodes ();
190- std::vector<BasePort*> has_downstream_reactions{};
191- std::copy_if (nodes.begin (), nodes.end (), std::back_inserter (has_downstream_reactions),
192- [](BasePort* port) { return port->has_anti_dependencies (); });
193-
194- std::vector<BasePort*> has_triggers{};
195- std::copy_if (nodes.begin (), nodes.end (), std::back_inserter (has_triggers),
196- [](BasePort* port) { return port->has_dependencies (); });
197-
198- for (auto downstream : has_downstream_reactions) {
199- for (auto upstream : has_triggers) {
200- if (upstream != downstream) {
201- auto optional_path = optimized_graph_.shortest_path (downstream, upstream);
202-
203- if (optional_path.has_value ()) {
204- auto best_path = optional_path.value ();
205- auto source = downstream;
206- for (auto hop : best_path) {
207- striped_graph.add_edge (source, hop.second , hop.first );
208- source = hop.second ;
191+ // updating target enclave if not nullptr
192+ merged_properties.enclave_ =
193+ (property.enclave_ != nullptr ) ? property.enclave_ : merged_properties.enclave_ ;
194+
195+ optimized_graph_.add_edge (element.second , previous_element, merged_properties);
209196 }
210197 }
211198 }
212199 }
213200 }
214-
215- optimized_graph_ = striped_graph;
216201}
217202
218203void recursive_assemble (Reactor* container) { // NOLINT
@@ -233,11 +218,17 @@ void Environment::assemble() { // NOLINT
233218 recursive_assemble (reactor);
234219 }
235220
236- log::Debug () << " start optimization on port graph" ;
237- this ->optimize ();
221+ // this assembles all the contained environments aka enclaves
222+ for (auto * env : contained_environments_) {
223+ env->assemble ();
224+ }
238225
239- log::Debug () << " instantiating port graph declaration" ;
240226 if (top_environment_ == nullptr || top_environment_ == this ) {
227+ log::Debug () << " start optimization on port graph" ;
228+ this ->optimize ();
229+
230+ log::Debug () << " instantiating port graph declaration" ;
231+
241232 log::Debug () << " graph: " ;
242233 log::Debug () << optimized_graph_;
243234
@@ -291,11 +282,6 @@ void Environment::assemble() { // NOLINT
291282 }
292283
293284 calculate_indexes ();
294-
295- // this assembles all the contained environments aka enclaves
296- for (auto * env : contained_environments_) {
297- env->assemble ();
298- }
299285}
300286
301287void Environment::build_dependency_graph (Reactor* reactor) { // NOLINT
0 commit comments