@@ -63,14 +63,16 @@ void Environment::register_input_action(BaseAction* action) {
63
63
}
64
64
65
65
void Environment::optimize () {
66
- #ifdef GRAPH_OPTIMIZATIONS
66
+ #if GRAPH_OPTIMIZATIONS
67
67
constexpr bool enable_optimizations = true ;
68
68
#else
69
69
constexpr bool enable_optimizations = false ;
70
70
#endif
71
+ log::Debug () << " Opimizations:" << enable_optimizations;
71
72
if constexpr (enable_optimizations) {
73
+ log::Debug () << graph_.to_mermaid ();
72
74
expand_and_merge ();
73
- strip_and_optimize ();
75
+ log::Debug () << optimized_graph_. to_mermaid ();
74
76
} else {
75
77
// no optimizations
76
78
optimized_graph_ = graph_;
@@ -149,70 +151,53 @@ void Environment::expand_and_merge() {
149
151
auto spanning_tree = graph_.naive_spanning_tree (source);
150
152
for (auto & path : spanning_tree) {
151
153
ConnectionProperties merged_properties{};
152
- auto * current_source = source;
153
154
154
- for (auto element : path) {
155
- auto property = element.first ;
155
+ std::reverse (path.begin (), path.end ());
156
156
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
+ }
159
165
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 ();
164
167
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 () ;
168
171
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);
172
179
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;
175
183
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;
178
187
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_ ;
185
190
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);
209
196
}
210
197
}
211
198
}
212
199
}
213
200
}
214
-
215
- optimized_graph_ = striped_graph;
216
201
}
217
202
218
203
void recursive_assemble (Reactor* container) { // NOLINT
@@ -233,11 +218,17 @@ void Environment::assemble() { // NOLINT
233
218
recursive_assemble (reactor);
234
219
}
235
220
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
+ }
238
225
239
- log::Debug () << " instantiating port graph declaration" ;
240
226
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
+
241
232
log::Debug () << " graph: " ;
242
233
log::Debug () << optimized_graph_;
243
234
@@ -291,11 +282,6 @@ void Environment::assemble() { // NOLINT
291
282
}
292
283
293
284
calculate_indexes ();
294
-
295
- // this assembles all the contained environments aka enclaves
296
- for (auto * env : contained_environments_) {
297
- env->assemble ();
298
- }
299
285
}
300
286
301
287
void Environment::build_dependency_graph (Reactor* reactor) { // NOLINT
0 commit comments