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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#![deny(missing_docs)]

//! A module containing data structures for XIO job sets.

#[macro_use]
extern crate serde_derive;

#[cfg(test)]
#[macro_use]
extern crate serde_json;

extern crate indexmap;
extern crate ommui_string_patterns;
extern crate uuid;
extern crate xio_base_datatypes;

mod extract_descriptions;

pub use extract_descriptions::{
    CommandDescription, ConditionDescription, ExtractDescriptions,
};
use indexmap::IndexMap;
use uuid::Uuid;
use xio_base_datatypes as base;

/// A complete job set.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct JobSet {
    /// Metadata for this job set.
    pub metadata: Metadata,

    /// The parameter layers.
    ///
    /// The `ParameterSet` items get applied in the order in which
    /// they are stored in the vector. This means values stored in the
    /// `ParameterSet` at index 0 would be overridden by values stored
    /// in the `ParameterSet` at index 2 for example.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<ParameterSet>,

    /// The jobs contained in the set.
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub jobs: IndexMap<String, Job>,

    /// The channel mappings for parameters stored to channels.
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub channels: IndexMap<String, ChannelAssignment>,
}

/// Metadata of a job set.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Metadata {
    /// A uuid for identification of the job set.
    pub uuid: Uuid,
}

/// A set of parameters (a.k.a. parameter layer).
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ParameterSet {
    /// The metadata of the parameter set.
    pub metadata: ParameterSetMetadata,

    /// The descriptions of the parameters.
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub descriptions: IndexMap<String, ParameterDescription>,

    /// The concrete values of the parameters.
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub values: IndexMap<String, base::DataValueDescriptive>,
}

/// Metadata for a parameter set.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ParameterSetMetadata {
    /// The name of the parameter set.
    pub name: String,
}

/// The description of a parameter.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ParameterDescription {
    /// The description of the parameter.
    #[serde(rename = "description")]
    pub description: String,

    /// The data type of the parameter.
    #[serde(rename = "type")]
    pub data_type: base::DataType,

    /// The storage type of the parameter.
    pub storage: base::StorageType,

    /// The override option of the parameter.
    #[serde(rename = "override")]
    pub override_option: base::OverrideOption,
}

/// A job, defined by a list of commands.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Job {
    /// The commands which get processed when the job is run.
    pub commands: Vec<Command>,
}

/// A command which can be executed in a job.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Command {
    /// A human-readable message describing the current action.
    #[serde(
        rename = "_message",
        skip_serializing_if = "String::is_empty",
        default
    )]
    pub message: String,

    /// The description of the command.
    pub description: String,

    /// The command type.
    ///
    /// This identifies a command by it's string id which needs to be
    /// defined in the XIO commandset that gets used for compilation.
    #[serde(rename = "type")]
    pub command_type: String,

    /// The parameter assignments.
    ///
    /// The keys are the parameter string identifiers as defined in the
    /// XIO commandset definition for the command.
    /// The values reference the string identifier of the definition
    /// in the `ParameterSet` layers of the job set.
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub parameters: IndexMap<String, String>,

    /// The conditions required for the command to continue running.
    ///
    /// This can only be applied to commands that have a temporal extent
    /// (e.g. `wait`, but not the `setValue` or
    /// `startMeasurementDataTransfer`).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub conditions: Vec<Condition>,
}

/// A condition which gets checked while a command is running.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Condition {
    /// A human-readable message describing the condition match case.
    #[serde(
        rename = "_message",
        skip_serializing_if = "String::is_empty",
        default
    )]
    pub message: String,

    /// The description of the condition.
    pub description: String,

    /// The condition command type.
    ///
    /// This identifies a command by it's string id which needs to be
    /// defined in the XIO commandset that gets used for compilation.
    #[serde(rename = "type")]
    pub command_type: String,

    /// When the condition is met:
    ///
    /// * When true, the job gets stopped with the
    ///   `::xio_webapi::JobStoppedReason::ExitCriterionMatched` reason.
    /// * When false, continue job execution at the next command.
    pub exit_job: bool,

    /// The parameters assignments.
    ///
    /// The keys are the parameter string identifiers as defined in the
    /// XIO commandset definition for the command.
    /// The values reference the string identifier of the definition
    /// in the `ParameterSet` layers of the job set.
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub parameters: IndexMap<String, String>,
}

/// An assignment of a parameter to a XIO module channel.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ChannelAssignment {
    /// The capability identifier.
    ///
    /// The capability must be defined in the
    /// `xio_hwdb::HardwareBoardDescription` used.
    pub capability: String,

    /// The assigned channel in the XIO module.
    ///
    /// The channel must be defined in the `xio_hwdb::Module` description
    /// that backs the referenced capability.
    pub channel: String,
}

/// The assignment of variables mapped to a hardware board
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct HardwareAssignment {
    /// The hardware board identifier as defined in the XIO hwdb.
    pub hardware_board: String,

    /// The mapping of channel parameters to the XIO hardware channels.
    ///
    /// The keys are the same as the keys used in the parameter set
    /// layers.
    pub mapping: IndexMap<String, ChannelAssignment>,
}

#[cfg(test)]
mod tests {
    use base::{
        DataType, DataValue, DataValueDescriptive, OverrideOption,
        StorageType,
    };
    use indexmap::IndexSet;
    use {
        serde_json, ChannelAssignment, Command, Condition, IndexMap, Job,
        JobSet, Metadata, ParameterDescription, ParameterSet,
        ParameterSetMetadata, Uuid,
    };

    #[test]
    fn example_jobset() {
        let json = json!({
            "metadata": {
                "uuid": "bf6e3ca9-176e-46e0-a886-84bfc57e13b4"
            },
            "channels":{
                "a1":{
                    "capability":"motor",
                    "channel":"speed"
                },
                "a3":{
                    "capability":"adc",
                    "channel":"voltage"
                }
            },
            "jobs":{
                "init":{
                    "commands":[
                        {
                            "description":"init job command 1",
                            "conditions":[
                            {
                                "_message": "condition 1 matched",
                                "description":"init job cmd 1 condition 1",
                                "exitJob":true,
                                "parameters":{"a":"a1","b":"b1"},
                                "type":"equals"
                            }
                            ],
                            "parameters":{"a":"b1","b":"a1"},
                            "type":"wait"
                        },
                        {
                            "_message":"waiting some time",
                            "description":"init job command 2",
                            "parameters":{"a":"b1"},
                            "type":"wait"
                        }
                    ]
                },
                "measurement":{
                    "commands": [
                        {
                            "_message":"waiting for something",
                            "description":"measurement job command 1",
                            "type":"wait"
                        },
                        {
                            "_message":"waiting for another thing",
                            "description":"measurement job command 2",
                            "type":"wait"
                        }
                    ]
                }
            },
            "parameters":[
                {
                    "descriptions":{
                        "a1":{
                            "description":"Parameter A 1",
                            "override":"forbid",
                            "storage":"channel",
                            "type":"uint8"
                        },
                        "a2":{
                            "description":"Parameter A 2",
                            "override":"enforce",
                            "storage":"fixed",
                            "type":"parametermask"
                        },
                        "a3":{
                            "description":"Parameter A 3",
                            "override":"allow",
                            "storage":"channel",
                            "type":"boolean"
                        }
                    },
                    "metadata": {
                        "name":"base"
                    },
                    "values":{"a1":3,"a2":["a3","b1"]}
                },
                {
                    "descriptions":{
                        "b1":{
                            "description":"Parameter B 1",
                            "override":"forbid",
                            "storage":"channel",
                            "type":"uint8"
                        }
                    },
                    "metadata": {
                        "name":"overlay"
                    },
                    "values":{"b1":14}
                }
            ]
        });
        let parameterset1 = ParameterSet {
            metadata: ParameterSetMetadata {
                name: "base".to_string(),
            },
            descriptions: vec![
                (
                    "a1".to_string(),
                    ParameterDescription {
                        description: "Parameter A 1".to_string(),
                        data_type: DataType::UInt8,
                        storage: StorageType::Channel,
                        override_option: OverrideOption::Forbid,
                    },
                ),
                (
                    "a2".to_string(),
                    ParameterDescription {
                        description: "Parameter A 2".to_string(),
                        data_type: DataType::ParameterMask,
                        storage: StorageType::Fixed,
                        override_option: OverrideOption::Enforce,
                    },
                ),
                (
                    "a3".to_string(),
                    ParameterDescription {
                        description: "Parameter A 3".to_string(),
                        data_type: DataType::Boolean,
                        storage: StorageType::Channel,
                        override_option: OverrideOption::Allow,
                    },
                ),
            ]
            .into_iter()
            .collect::<IndexMap<String, ParameterDescription>>(),
            values: vec![
                ("a1".to_string(), DataValue::UInt8(3u8)),
                (
                    "a2".to_string(),
                    DataValue::ParameterMask(
                        vec!["a3".to_string(), "b1".to_string()]
                            .into_iter()
                            .collect::<IndexSet<String>>(),
                    ),
                ),
            ]
            .into_iter()
            .collect::<IndexMap<String, DataValueDescriptive>>(),
        };
        let parameterset2 = ParameterSet {
            metadata: ParameterSetMetadata {
                name: "overlay".to_string(),
            },
            descriptions: vec![(
                "b1".to_string(),
                ParameterDescription {
                    description: "Parameter B 1".to_string(),
                    data_type: DataType::UInt8,
                    storage: StorageType::Channel,
                    override_option: OverrideOption::Forbid,
                },
            )]
            .into_iter()
            .collect::<IndexMap<String, ParameterDescription>>(),
            values: vec![("b1".to_string(), DataValue::UInt8(14u8))]
                .into_iter()
                .collect::<IndexMap<String, DataValueDescriptive>>(),
        };
        let job1 = Job {
            commands: vec![
                Command {
                    message: "".to_string(),
                    description: "init job command 1".to_string(),
                    conditions: vec![Condition {
                        message: "condition 1 matched".to_string(),
                        description: "init job cmd 1 condition 1"
                            .to_string(),
                        exit_job: true,
                        parameters: vec![("a", "a1"), ("b", "b1")]
                            .into_iter()
                            .map(|(k, v)| (k.to_string(), v.to_string()))
                            .collect(),
                        command_type: "equals".to_string(),
                    }],
                    command_type: "wait".to_string(),
                    parameters: vec![("a", "b1"), ("b", "a1")]
                        .into_iter()
                        .map(|(k, v)| (k.to_string(), v.to_string()))
                        .collect(),
                },
                Command {
                    message: "waiting some time".to_string(),
                    description: "init job command 2".to_string(),
                    conditions: Vec::new(),
                    command_type: "wait".to_string(),
                    parameters: vec![("a", "b1")]
                        .into_iter()
                        .map(|(k, v)| (k.to_string(), v.to_string()))
                        .collect(),
                },
            ],
        };
        let job2 = Job {
            commands: vec![
                Command {
                    message: "waiting for something".to_string(),
                    description: "measurement job command 1".to_string(),
                    conditions: Vec::new(),
                    command_type: "wait".to_string(),
                    parameters: IndexMap::new(),
                },
                Command {
                    message: "waiting for another thing".to_string(),
                    description: "measurement job command 2".to_string(),
                    conditions: Vec::new(),
                    command_type: "wait".to_string(),
                    parameters: IndexMap::new(),
                },
            ],
        };
        let jobset = JobSet {
            metadata: Metadata {
                uuid: Uuid::from_bytes([
                    0xbf, 0x6e, 0x3c, 0xa9, 0x17, 0x6e, 0x46, 0xe0, 0xa8,
                    0x86, 0x84, 0xbf, 0xc5, 0x7e, 0x13, 0xb4,
                ]),
            },
            parameters: vec![parameterset1, parameterset2],
            jobs: vec![
                ("init".to_string(), job1),
                ("measurement".to_string(), job2),
            ]
            .into_iter()
            .collect(),
            channels: vec![
                (
                    "a1".to_string(),
                    ChannelAssignment {
                        capability: "motor".to_string(),
                        channel: "speed".to_string(),
                    },
                ),
                (
                    "a3".to_string(),
                    ChannelAssignment {
                        capability: "adc".to_string(),
                        channel: "voltage".to_string(),
                    },
                ),
            ]
            .into_iter()
            .collect(),
        };
        let deserialized = serde_json::from_value(json.clone()).unwrap();

        assert_eq!(jobset, deserialized);

        let serialized = serde_json::to_value(&jobset).unwrap();
        assert_eq!(serialized, json);
    }
}