melodium-engine 0.10.0

Mélodium core engine and executor implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use crate::building::{
    BuildId, CheckBuild, CheckBuildResult, CheckEnvironment, CheckStep, ContextualEnvironment,
    DynamicBuildResult, FeedingInputs, GenesisEnvironment, StaticBuildResult,
};
use crate::building::{Builder as BuilderTrait, HostTreatment};
use crate::debug::{Event, EventKind, TransmissionDetails};
use crate::error::{LogicError, LogicResult};
use crate::world::World;
use core::fmt::Debug;
use melodium_common::descriptor::{Status, Treatment as TreatmentDescriptor};
use melodium_common::executive::{TrackId, Treatment};
use std::collections::HashMap;
use std::sync::{Arc, RwLock, Weak};

#[derive(Debug)]
struct BuildSample {
    genesis_environment: GenesisEnvironment,
    host_treatment: HostTreatment,
    host_build_id: Option<BuildId>,
    #[allow(unused)]
    check: Arc<RwLock<CheckBuild>>,
    label: String,
}

impl BuildSample {
    pub fn new(
        host_treatment: &HostTreatment,
        host_build: &Option<BuildId>,
        label: &str,
        environment: &GenesisEnvironment,
    ) -> Self {
        Self {
            genesis_environment: environment.clone(),
            host_treatment: host_treatment.clone(),
            host_build_id: host_build.clone(),
            check: Arc::new(RwLock::new(CheckBuild::new(
                host_treatment.host_id().cloned(),
                label,
            ))),
            label: label.to_string(),
        }
    }
}

#[derive(Debug)]
pub struct Builder {
    world: Weak<World>,

    build_fn: fn() -> Arc<dyn Treatment>,
    descriptor: Weak<dyn TreatmentDescriptor>,

    builds: RwLock<Vec<BuildSample>>,
    building_inputs: RwLock<HashMap<(BuildId, TrackId), FeedingInputs>>,
}

impl Builder {
    pub fn new(
        world: Weak<World>,
        descriptor: Weak<dyn TreatmentDescriptor>,
        build_fn: fn() -> Arc<dyn Treatment>,
    ) -> Self {
        Self {
            world,
            build_fn,
            descriptor,
            builds: RwLock::new(Vec::new()),
            building_inputs: RwLock::new(HashMap::new()),
        }
    }
}

impl BuilderTrait for Builder {
    fn static_build(
        &self,
        host_treatment: HostTreatment,
        host_build: Option<BuildId>,
        label: String,
        environment: &GenesisEnvironment,
    ) -> LogicResult<StaticBuildResult> {
        let world = self.world.upgrade().unwrap();
        // Make a BuildSample with matching informations
        let build_sample = BuildSample::new(&host_treatment, &host_build, &label, environment);

        let mut builds_writer = self.builds.write().unwrap();
        let idx = builds_writer.len() as BuildId;

        let rc_descriptor = self.descriptor.upgrade().unwrap();
        for (model_name, sources) in rc_descriptor.source_from() {
            let (_, matching_model) = environment
                .models()
                .iter()
                .find(|(name, _)| name == &model_name)
                .unwrap();

            for source in sources {
                world.add_source(
                    matching_model.id().unwrap(),
                    source,
                    self.descriptor.upgrade().unwrap(),
                    environment.variables().clone(),
                    idx,
                );
            }
        }

        builds_writer.push(build_sample);

        Status::new_success(StaticBuildResult::Build(idx))
    }

    fn dynamic_build(
        &self,
        build: BuildId,
        _with_inputs: Vec<String>,
        environment: &ContextualEnvironment,
    ) -> Option<DynamicBuildResult> {
        let world = self.world.upgrade().unwrap();

        // Look for existing build
        {
            let borrowed_building_inputs = self.building_inputs.read().unwrap();

            if let Some(existing_building_inputs) =
                borrowed_building_inputs.get(&(build, environment.track_id()))
            {
                let mut dynamic_result = DynamicBuildResult::new();
                dynamic_result
                    .feeding_inputs
                    .extend(existing_building_inputs.clone());

                return Some(dynamic_result);
            }
        }

        // Get build
        let borrowed_builds = self.builds.read().unwrap();
        let build_sample = borrowed_builds.get(build as usize).unwrap();
        let descriptor = self.descriptor.upgrade().unwrap();

        let mut result = DynamicBuildResult::new();

        let treatment = (self.build_fn)();

        for (name, generic) in build_sample.genesis_environment.generics() {
            treatment.set_generic(name, generic.clone());
        }

        for (name, model) in build_sample.genesis_environment.models() {
            treatment.set_model(name, Arc::clone(model));
        }

        for (name, value) in environment.variables() {
            treatment.set_parameter(name, value.clone());
        }

        world.send_debug(Event::new(EventKind::TreatmentBuilt {
            treatment: treatment.descriptor(),
            environment: environment.clone(),
            host_treatment: build_sample.host_treatment.clone(),
            host_build: build_sample.host_build_id,
            build_id: build,
            label: build_sample.label.clone(),
        }));

        let start = Box::pin({
            let world = world.clone();
            let treatment = treatment.descriptor();
            let host_treatment = build_sample.host_treatment.clone();
            let host_build = build_sample.host_build_id;
            let build_id = build;
            let track_id = environment.track_id();
            let label = build_sample.label.clone();
            async move {
                world
                    .send_debug_async(Event::new(EventKind::TreatmentStarted {
                        treatment,
                        host_treatment,
                        host_build,
                        build_id,
                        track_id,
                        label,
                    }))
                    .await;
            }
        });
        let finish = Box::pin({
            let world = world.clone();
            let treatment = treatment.descriptor();
            let host_treatment = build_sample.host_treatment.clone();
            let host_build = build_sample.host_build_id;
            let build_id = build;
            let track_id = environment.track_id();
            let label = build_sample.label.clone();
            async move {
                world
                    .send_debug_async(Event::new(EventKind::TreatmentFinished {
                        treatment,
                        host_treatment,
                        host_build,
                        build_id,
                        track_id,
                        label,
                    }))
                    .await;
            }
        });

        match &build_sample.host_treatment {
            HostTreatment::Treatment(host_descriptor) => {
                let host_build = world
                    .builder(host_descriptor.identifier())
                    .success()
                    .unwrap()
                    .give_next(
                        build_sample.host_build_id.unwrap(),
                        build_sample.label.to_string(),
                        descriptor.outputs().keys().cloned().collect(),
                        &environment.base_on(),
                    )
                    .unwrap();

                let mut inputs = HashMap::new();
                for (name, descriptor) in descriptor.inputs() {
                    let input = world.new_input(
                        *descriptor.flow(),
                        environment.track_id(),
                        TransmissionDetails {
                            treatment: treatment.descriptor(),
                            host_treatment: build_sample.host_treatment.clone(),
                            host_build: build_sample.host_build_id,
                            build_id: build,
                            label: build_sample.label.clone(),
                            name: name.clone(),
                        },
                    );
                    treatment.assign_input(name, Box::new(input.clone()));
                    inputs.insert(name.clone(), input);
                }
                for (name, descriptor) in descriptor.outputs() {
                    let output = world.new_output(
                        *descriptor.flow(),
                        environment.track_id(),
                        TransmissionDetails {
                            treatment: treatment.descriptor(),
                            host_treatment: build_sample.host_treatment.clone(),
                            host_build: build_sample.host_build_id,
                            build_id: build,
                            label: build_sample.label.clone(),
                            name: name.clone(),
                        },
                    );
                    if let Some(inputs) = host_build.feeding_inputs.get(name) {
                        output.add_transmission(inputs);
                    }
                    treatment.assign_output(name, Box::new(output));
                }

                result.feeding_inputs = inputs
                    .iter()
                    .map(|(name, input)| (name.to_string(), vec![input.clone()]))
                    .collect();

                let prepared_futures = treatment.prepare(environment.track_id(), start, finish);
                result.prepared_futures.extend(prepared_futures);
                result.prepared_futures.extend(host_build.prepared_futures);
            }
            HostTreatment::Direct => {
                let direct_inputs = world.direct(&environment.track_id()).to_success().unwrap();

                let mut inputs = HashMap::new();
                for (name, descriptor) in descriptor.inputs() {
                    let input = world.new_input(
                        *descriptor.flow(),
                        environment.track_id(),
                        TransmissionDetails {
                            treatment: treatment.descriptor(),
                            host_treatment: build_sample.host_treatment.clone(),
                            host_build: build_sample.host_build_id,
                            build_id: build,
                            label: build_sample.label.clone(),
                            name: name.clone(),
                        },
                    );
                    treatment.assign_input(name, Box::new(input.clone()));
                    inputs.insert(name.clone(), input);
                }
                for (name, descriptor) in descriptor.outputs() {
                    let output = world.new_output(
                        *descriptor.flow(),
                        environment.track_id(),
                        TransmissionDetails {
                            treatment: treatment.descriptor(),
                            host_treatment: build_sample.host_treatment.clone(),
                            host_build: build_sample.host_build_id,
                            build_id: build,
                            label: build_sample.label.clone(),
                            name: name.clone(),
                        },
                    );
                    if let Some(inputs) = direct_inputs.get(name) {
                        output.add_transmission(inputs);
                    }
                    treatment.assign_output(name, Box::new(output));
                }

                result.feeding_inputs = inputs
                    .iter()
                    .map(|(name, input)| (name.to_string(), vec![input.clone()]))
                    .collect();

                result.prepared_futures.extend(treatment.prepare(
                    environment.track_id(),
                    start,
                    finish,
                ));
            }
        }

        self.building_inputs.write().unwrap().insert(
            (build, environment.track_id()),
            result.feeding_inputs.clone(),
        );

        Some(result)
    }

    fn give_next(
        &self,
        _within_build: BuildId,
        _for_label: String,
        _for_outputs: Vec<String>,
        _environment: &ContextualEnvironment,
    ) -> Option<DynamicBuildResult> {
        // A core treatment cannot have sub-treatments (it is not a sequence), so nothing to ever return.
        None
    }

    fn check_dynamic_build(
        &self,
        build: BuildId,
        environment: CheckEnvironment,
        previous_steps: Vec<CheckStep>,
    ) -> Option<CheckBuildResult> {
        let world = self.world.upgrade().unwrap();
        let descriptor = self.descriptor.upgrade().unwrap();

        let mut errors = Vec::new();
        // Check if we're not in our own previous steps
        let check_step = CheckStep {
            identifier: descriptor.identifier().clone(),
            build_id: build,
        };
        if let Some(_existing_check_step) = previous_steps.iter().find(|&cs| cs == &check_step) {
            errors.push(LogicError::already_included_build_step(
                56,
                descriptor.identifier().clone(),
                check_step.clone(),
                previous_steps.clone(),
                None,
            ));
        }
        let mut current_previous_steps = previous_steps.clone();
        current_previous_steps.push(check_step);

        // Get build
        let borrowed_builds = self.builds.read().unwrap();
        let build_sample = borrowed_builds.get(build as usize).unwrap();

        let mut all_builds = Vec::new();
        if errors.is_empty() {
            match &build_sample.host_treatment {
                HostTreatment::Treatment(host_descriptor) => {
                    let build_result = world
                        .builder(host_descriptor.identifier())
                        .success()
                        .unwrap()
                        .check_give_next(
                            build_sample.host_build_id.unwrap(),
                            build_sample.label.to_string(),
                            environment.clone(),
                            current_previous_steps,
                        )
                        .unwrap();

                    all_builds.extend(build_result.checked_builds);

                    errors.extend(build_result.errors);
                }
                HostTreatment::Direct => {}
            }
            all_builds.push(Arc::clone(&build_sample.check));
        }

        // Return checked build result
        let own_checked_build_result = CheckBuildResult {
            checked_builds: all_builds,
            build: Arc::clone(&build_sample.check),
            errors,
        };

        Some(own_checked_build_result)
    }

    fn check_give_next(
        &self,
        _within_build: BuildId,
        _for_label: String,
        _environment: CheckEnvironment,
        _previous_steps: Vec<CheckStep>,
    ) -> Option<CheckBuildResult> {
        // A core treatment cannot have sub-treatments (its not a sequence), so nothing to ever return.
        None
    }
}