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
use crate::Result;
use indexmap::{IndexMap, IndexSet};

pub mod filesystem;

mod loadable_device;

pub use loadable_device::LoadableDevice;

#[derive(Clone, Debug)]
pub struct Sampling {
    pub bins: IndexMap<String, Bin>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "lowercase", tag = "type", content = "details")]
pub enum Bin {
    Add,
    Blocker,
    CalibrationLookup(CalibrationLookupDetails),
    CalmingDetection,
    Cosinus,
    Cumulate,
    Derivation,
    Difference,
    Divide,
    Fifo,
    FirstValue,
    FixedValues(FixedValuesDetails),
    GreaterThan,
    Invert,
    LinearInterpolatedLookup(LinearInterpolatedLookupDetails),
    LinearRegression,
    Maximum,
    MeanValue,
    Minimum,
    Multiplex(MultiplexDetails),
    Multiply,
    Not,
    Pipeline(PipelineDetails),
    SingleNotNull(SingleNotNullDetails),
    Sinus,
    Storage,
    Subtract,
}

impl PipelineDetails {
    pub fn to_arnalisa(
        &self,
    ) -> Result<arnalisa::bins::pipeline::Description> {
        let inputs = self
            .pipes
            .iter()
            .filter_map(|pipe| {
                if pipe.from.bin.is_empty() {
                    Some(pipe.from.source.to_string())
                } else {
                    None
                }
            })
            .collect::<IndexSet<_>>();

        let bins = self
            .bins
            .iter()
            .map(|(k, v)| {
                use self::Bin as B;
                use arnalisa::bins::SourceSinkDescription as D;
                use arnalisa::bins::*;
                use arnalisa::CalibrationSource;
                let description = match v {
                    B::Add => D::Add(add::Description),
                    B::Blocker => D::Blocker(blocker::Description),
                    B::CalibrationLookup(details) => {
                        D::Calibration(calibration::Description {
                            details: CalibrationSource::Identifier {
                                id: details.calibration_id.to_string(),
                            },
                            reversed: details.reversed,
                        })
                    }
                    B::CalmingDetection => {
                        D::LastCalmPoint(last_calm_point::Description)
                    }
                    B::Cosinus => D::Cosinus(cosinus::Description),
                    B::Cumulate => D::Cumulation(cumulation::Description),
                    B::Derivation => {
                        D::Derivation(derivation::Description)
                    }
                    B::Difference => {
                        D::Difference(difference::Description)
                    }
                    B::Divide => D::Divide(divide::Description),
                    B::Fifo => D::Fifo(fifo::Description),
                    B::FirstValue => {
                        D::FirstValue(first_value::Description)
                    }
                    B::FixedValues(details) => {
                        D::FixedValues(fixedvalues::Description {
                            values: details.fixed_values.clone(),
                        })
                    }
                    B::GreaterThan => {
                        D::GreaterThan(greater_than::Description)
                    }
                    B::Invert => D::Invert(invert::Description),
                    B::LinearInterpolatedLookup(details) => {
                        D::Calibration(calibration::Description {
                            details: CalibrationSource::Embedded {
                                curve: details.points.clone(),
                            },
                            reversed: false,
                        })
                    }
                    B::LinearRegression => {
                        D::LinearRegression(linear_regression::Description)
                    }
                    B::Maximum => D::Maximum(maximum::Description),
                    B::MeanValue => D::Mean(mean::Description),
                    B::Minimum => D::Minimum(minimum::Description),
                    B::Multiplex(details) => {
                        D::Multiplex(multiplex::Description {
                            outputs: details.outputs.clone(),
                        })
                    }
                    B::Multiply => D::Multiply(multiply::Description),
                    B::Not => D::Not(not::Description),
                    B::Pipeline(details) => {
                        D::Pipeline(details.to_arnalisa()?)
                    }
                    B::SingleNotNull(details) => D::SingleNotNull({
                        single_not_null::Description {
                            inputs: details.inputs.clone(),
                        }
                    }),
                    B::Sinus => D::Sinus(sinus::Description),
                    B::Storage => D::Storage(storage::Description),
                    B::Subtract => D::Subtract(subtract::Description),
                };
                Ok((k.to_string(), description))
            })
            .collect::<Result<IndexMap<_, _>>>()?;

        fn pipes_for_bin(
            raw_pipes: &[Pipe],
            bin: &str,
            bins: &IndexMap<String, arnalisa::bins::SourceSinkDescription>,
        ) -> IndexMap<String, arnalisa::bins::SourceReference> {
            let pipes = raw_pipes
                .iter()
                .filter_map(|pipe| {
                    if &pipe.to.bin == bin {
                        Some((
                            pipe.to.sink.to_string(),
                            arnalisa::bins::SourceReference {
                                bin: if pipe.from.bin.is_empty() {
                                    None
                                } else {
                                    Some(pipe.from.bin.to_string())
                                },
                                source: pipe.from.source.to_string(),
                            },
                        ))
                    } else {
                        None
                    }
                })
                .collect::<IndexMap<_, _>>();
            let pipes = fixup_pipe_sources(pipes, bins);
            if let Some(bin) = bins.get(&bin.to_string()) {
                fixup_pipe_sinks(pipes, &bin)
            } else {
                pipes
            }
        }

        fn fixup_pipe_sources(
            mut p: IndexMap<String, arnalisa::bins::SourceReference>,
            bins: &IndexMap<String, arnalisa::bins::SourceSinkDescription>,
        ) -> IndexMap<String, arnalisa::bins::SourceReference> {
            for pipe in p.values_mut() {
                if let Some(ref bin) = pipe.bin {
                    if let Some(bin) = bins.get(bin) {
                        use arnalisa::bins::SourceSinkDescription::*;
                        match bin {
                            Add(_) => {
                                if pipe.source == "sum" {
                                    pipe.source = "output".to_string();
                                }
                            }
                            Blocker(_) => {}
                            Calibration(_) => {}
                            Cosinus(_) => {}
                            Cumulation(_) => {}
                            Derivation(_) => {}
                            Difference(_) => {
                                if pipe.source == "difference" {
                                    pipe.source = "output".to_string();
                                }
                            }
                            Divide(_) => {
                                if pipe.source == "quotient" {
                                    pipe.source = "output".to_string();
                                }
                            }
                            Fifo(_) => {}
                            FirstValue(_) => {}
                            FixedValues(_) => {}
                            GreaterThan(_) => {}
                            Invert(_) => {}
                            LastCalmPoint(_) => {}
                            LinearRegression(_) => {}
                            Maximum(_) => {}
                            Mean(_) => {}
                            Minimum(_) => {}
                            Multiplex(_) => {}
                            Multiply(_) => {
                                if pipe.source == "product" {
                                    pipe.source = "output".to_string();
                                }
                            }
                            Not(_) => {}
                            Pipeline(_) => {}
                            SingleNotNull(_) => {}
                            Sinus(_) => {}
                            Subtract(_) => {
                                if pipe.source == "difference" {
                                    pipe.source = "output".to_string();
                                }
                            }
                            Storage(_) => {}
                        }
                    }
                }
            }

            p
        }

        fn fixup_pipe_sinks(
            mut p: IndexMap<String, arnalisa::bins::SourceReference>,
            bin: &arnalisa::bins::SourceSinkDescription,
        ) -> IndexMap<String, arnalisa::bins::SourceReference> {
            use arnalisa::bins::SourceSinkDescription::*;
            match bin {
                Add(_) => {
                    if let Some(a) = p.remove(&"summand1".to_string()) {
                        p.insert("a".to_string(), a);
                    }
                    if let Some(b) = p.remove(&"summand2".to_string()) {
                        p.insert("b".to_string(), b);
                    }
                    p
                }
                Blocker(_) => p,
                Calibration(_) => p,
                Cosinus(_) => p,
                Cumulation(_) => p,
                Derivation(_) => p,
                Difference(_) => p,
                Divide(_) => {
                    if let Some(a) = p.remove(&"dividend".to_string()) {
                        p.insert("a".to_string(), a);
                    }
                    if let Some(b) = p.remove(&"divisor".to_string()) {
                        p.insert("b".to_string(), b);
                    }
                    p
                }
                Fifo(_) => p,
                FirstValue(_) => p,
                FixedValues(_) => {
                    if let Some(trigger) =
                        p.remove(&"finishDetectionInput".to_string())
                    {
                        p.insert("trigger".to_string(), trigger);
                    }
                    p
                }
                GreaterThan(_) => p,
                Invert(_) => p,
                LastCalmPoint(_) => p,
                LinearRegression(_) => {
                    if let Some(trigger) =
                        p.remove(&"numItems".to_string())
                    {
                        p.insert("num_items".to_string(), trigger);
                    }
                    p
                }
                Maximum(_) => p,
                Mean(_) => p,
                Minimum(_) => p,
                Multiplex(_) => {
                    if let Some(input) = p.remove(&"value".to_string()) {
                        p.insert("input".to_string(), input);
                    }
                    p
                }
                Multiply(_) => {
                    if let Some(a) = p.remove(&"factor1".to_string()) {
                        p.insert("a".to_string(), a);
                    }
                    if let Some(b) = p.remove(&"factor2".to_string()) {
                        p.insert("b".to_string(), b);
                    }
                    p
                }
                Not(_) => p,
                Pipeline(_) => p,
                SingleNotNull(_) => p,
                Sinus(_) => p,
                Subtract(_) => {
                    if let Some(a) = p.remove(&"minuend".to_string()) {
                        p.insert("a".to_string(), a);
                    }
                    if let Some(b) = p.remove(&"subtrahend".to_string()) {
                        p.insert("b".to_string(), b);
                    }
                    p
                }
                Storage(_) => p,
            }
        }

        let pipes = std::iter::once("".to_string())
            .chain(bins.keys().cloned())
            .filter_map(|k| {
                let pipes = pipes_for_bin(&self.pipes, &k, &bins);
                if pipes.is_empty() {
                    None
                } else {
                    Some((k, pipes))
                }
            })
            .collect::<IndexMap<_, _>>();

        Ok(arnalisa::bins::pipeline::Description {
            pipes,
            bins,
            inputs,
        })
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PipelineDetails {
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub bins: IndexMap<String, Bin>,
    pub pipes: Vec<Pipe>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CalibrationLookupDetails {
    pub calibration_id: String,
    pub reversed: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct FixedValuesDetails {
    pub fixed_values: IndexMap<String, arnalisa::Item>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct LinearInterpolatedLookupDetails {
    pub points: Vec<(f64, f64)>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct MultiplexDetails {
    pub outputs: IndexMap<String, usize>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Pipe {
    pub from: PipeSource,
    pub to: PipeSink,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PipeSource {
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub bin: String,
    pub source: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PipeSink {
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub bin: String,
    pub sink: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SingleNotNullDetails {
    pub inputs: IndexSet<String>,
}