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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#[cfg(feature = "debugger")]
use std::fmt;

use log::debug;
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;
use url::Url;

use crate::errors::*;
use crate::model::input::Input;
use crate::model::output_connection::OutputConnection;

#[derive(Deserialize, Serialize, Clone, PartialEq, Eq, Debug,)]
/// `RuntimeFunction` contains all the information needed about a function and its implementation
/// to be able to execute a flow using it.
pub struct RuntimeFunction {
    #[cfg(feature = "debugger")]
    #[serde(default, skip_serializing_if = "String::is_empty")]
    name: String,

    #[cfg(feature = "debugger")]
    #[serde(default, skip_serializing_if = "String::is_empty")]
    route: String,

    /// The unique `function_id` of this function at run-time
    function_id: usize,

    /// The unique id of the flow this function was in at definition time
    flow_id: usize,

    // Implementation location formats are:
    // - "lib://lib_name/path/to/implementation" - library implementation reference
    // - "context://stdio/stdout"                - context implementation reference
    // - path relative to the manifest where the provided implementation file can be found
    implementation_location: String,

    // Implementation Urls formats are:
    // - "lib://lib_name/path/to/implementation" - library implementation reference
    // - "context://stdio/stdout"                - context implementation reference
    // - "file://{manifest_url}/{relative_path}  - provided implementation reference
    #[serde(skip_serializing_if = "is_default_url", default = "default_url")]
    implementation_url: Url,

    // TODO skip serializing this, if the vector ONLY contains objects that can be serialized
    // to "{}" and hence contain no info. I think the number of inputs is not needed?
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    inputs: Vec<Input>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    output_connections: Vec<OutputConnection>,
}

fn is_default_url(url: &Url) -> bool {
   url == &default_url()
}

fn default_url() -> Url {
    Url::parse("file:///").expect("Could not create default_url")
}

#[cfg(feature = "debugger")]
impl fmt::Display for RuntimeFunction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Function #{}({})", self.function_id, self.flow_id)?;

        if !self.name.is_empty() {
            write!(f, " '{}'", self.name)?;
        }

        if !self.route.is_empty() {
            writeln!(f, " @ '{}'", self.route)?;
        }

        writeln!(f, "\t({})", self.implementation_location)?;

        for (number, input) in self.inputs.iter().enumerate() {
            writeln!(f, "\tInput:{number} {input}")?;
        }

        for output_route in &self.output_connections {
            writeln!(f, "\t{output_route}",)?;
        }

        Ok(())
    }
}

impl RuntimeFunction {
    /// Create a new `RuntimeFunction` with the specified `name`, `route`, `implementation` etc.
    /// This only needs to be used by compilers or IDE generating `manifests` with functions
    /// The library `flowrlib` just deserializes them from the `manifest`
    /// The Vector of outputs:
    /// Output sub-path (or ""), destination function id, destination function io number, Optional path of destination
    #[allow(clippy::too_many_arguments)]
    pub fn new<
        #[cfg(feature = "debugger")] N: Into<String>,
        #[cfg(feature = "debugger")] R: Into<String>,
        I: Into<String>,
    >(
        #[cfg(feature = "debugger")] name: N,
        #[cfg(feature = "debugger")] route: R,
        implementation_location: I,
        inputs: Vec<Input>,
        id: usize,
        flow_id: usize,
        output_connections: &[OutputConnection],
        include_destination_routes: bool,
    ) -> Self {
        let mut connections = output_connections.to_vec();

        // Remove destination routes if not wanted
        if !include_destination_routes {
            for mut connection in &mut connections {
                connection.destination = String::default();
            }
        }

        RuntimeFunction {
            #[cfg(feature = "debugger")]
            name: name.into(),
            #[cfg(feature = "debugger")]
            route: route.into(),
            function_id: id,
            flow_id,
            implementation_location: implementation_location.into(),
            implementation_url: default_url(),
            output_connections: connections,
            inputs,
        }
    }

    #[cfg(feature = "debugger")]
    /// Reset a `Function` to initial state. Used by a debugger at run-time to reset a function
    /// as part of a whole flow reset to run it again.
    pub fn reset(&mut self) {
        for input in &mut self.inputs {
            input.reset();
        }
    }

    /// Accessor for a `RuntimeFunction` `name`
    #[cfg(feature = "debugger")]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Accessor for a `RuntimeFunction` `route`
    #[cfg(feature = "debugger")]
    pub fn route(&self) -> &str {
        &self.route
    }

    /// Accessor for a `RuntimeFunction` `id`
    pub fn id(&self) -> usize {
        self.function_id
    }

    /// Accessor for a `RuntimeFunction` `flow_id`
    pub fn get_flow_id(&self) -> usize {
        self.flow_id
    }

    /// Initialize the function to be ready to be called during flow execution
    pub fn init(&mut self) {
        self.init_inputs(true, false)
    }

    /// Initialize `Inputs` that have `InputInitializers` on them
    pub fn init_inputs(&mut self, first_time: bool, flow_idle: bool) {
        for (io_number, input) in &mut self.inputs.iter_mut().enumerate() {
            if input.init(first_time, flow_idle) {
                debug!("\tInitialized Input #{}:{io_number} in Flow #{}", self.function_id, self.flow_id);
            }
        }
    }

    /// Accessor for a `RuntimeFunction` `implementation_location`
    pub fn implementation_location(&self) -> &str {
        &self.implementation_location
    }

    /// Send a value or array of values to the specified input of this function
    pub fn send(&mut self, io_number: usize, value: Value) -> bool {
        self.inputs[io_number].send(value)
    }

    /// Accessor for a `RuntimeFunction` `output_connections` field
    pub fn get_output_connections(&self) -> &Vec<OutputConnection> {
        &self.output_connections
    }

    /// Get a reference to the implementation_location
    pub fn get_implementation_location(&self) -> &str {
        &self.implementation_location
    }

    /// Set the implementation_location, as an absolute Url relative to the manifest_url
    pub fn set_implementation_url(&mut self, manifest_url: &Url) -> Result<()> {
        self.implementation_url = Self::location_to_url(manifest_url,
                                                        self.implementation_location())?;
        Ok(())
    }

    /// Get a reference to the implementation_url
    pub fn get_implementation_url(&self) -> &Url {
        &self.implementation_url
    }

    fn location_to_url(manifest_url: &Url, location: &str) -> Result<Url> {
        Url::parse(location).or_else(|_| manifest_url.clone().join(location))
            .chain_err(|| "Could not create Url from 'manifest_url' and 'location'")
    }

    /// Determine if the `RuntimeFunction` `input` number `input_number` is full or not
    pub fn input_count(&self, input_number: usize) -> usize {
        self.inputs[input_number].count()
    }

    /// Returns how many inputs sets are available across all the `RuntimeFunction` `Inputs`
    /// NOTE: For Impure functions without inputs (that can always run and produce a value)
    /// this will return usize::MAX
    pub fn input_set_count(&self) -> usize {
        let mut num_input_sets = usize::MAX;

        for input in &self.inputs {
            num_input_sets = std::cmp::min(num_input_sets, input.count());
        }

        num_input_sets
    }

    /// Can this function run? Either because:
    ///     - it has input sets to allow it to run
    ///     - it has no inputs and so can always run
    pub fn can_run(&self) -> bool {
        self.inputs.is_empty() || self.input_set_count() > 0
    }

    /// Inspect the values of the `inputs` of a `RuntimeFunction`
    #[cfg(any(feature = "debugger", debug_assertions))]
    pub fn inputs(&self) -> &Vec<Input> {
        &self.inputs
    }

    /// Inspect the value of the `input` of a `RuntimeFunction`.
    #[cfg(feature = "debugger")]
    pub fn input(&self, id: usize) -> Option<&Input> {
        self.inputs.get(id)
    }

    /// Read the values from the inputs and return them for use in executing the `RuntimeFunction`
    pub fn take_input_set(&mut self) -> Result<Vec<Value>> {
        let mut input_set: Vec<Value> = Vec::new();
        for input in &mut self.inputs {
            input_set.push(input.take()?);
        }
        Ok(input_set)
    }
}

#[cfg(test)]
mod test {
    use serde_json::json;
    use serde_json::value::Value;

    use crate::model::input::Input;
    use crate::model::output_connection::OutputConnection;
    use crate::model::output_connection::Source::Output;

    use super::RuntimeFunction;

    /*************** Below are tests for basic json.pointer functionality *************************/

    #[test]
    fn destructure_output_base_route() {
        let json = json!("simple");
        assert_eq!(
            "simple",
            json.pointer("").expect("Couldn't get root element"),
            "json pointer functionality not working!"
        );
    }

    #[test]
    fn destructure_json_value() {
        let json: Value = json!({ "sub_route": "sub_output" });
        assert_eq!(
            "sub_output",
            json.pointer("/sub_route").expect("Couldn't get route"),
            "json pointer functionality not working!"
        );
    }

    #[test]
    fn access_array_elements() {
        let args: Vec<&str> = vec!["arg0", "arg1", "arg2"];
        let json = json!(args);
        assert_eq!(
            "arg0",
            json.pointer("/0").expect("Couldn't get /0 route"),
            "json pointer array indexing functionality not working!"
        );
        assert_eq!(
            "arg1",
            json.pointer("/1").expect("Couldn't get /1 route"),
            "json pointer array indexing functionality not working!"
        );
    }

    #[test]
    fn can_send_simple_object() {
        let mut function = test_function(0);
        function.init();
        function.send(0, json!(1));
        assert_eq!(
            json!(1),
            function
                .take_input_set()
                .expect("Couldn't get input set")
                .remove(0),
            "The value from input set wasn't what was expected"
        );
    }

    #[test]
    fn can_send_array_object() {
        let mut function = test_function(1);
        function.init();
        function.send(0, json!([1, 2]));
        assert_eq!(
            json!([1, 2]),
            function
                .take_input_set()
                .expect("Couldn't get input set")
                .remove(0),
            "The value from input set wasn't what was expected"
        );
    }

    #[test]
    fn test_array_to_non_array() {
        let mut function = test_function(0);
        function.init();
        function.send(0, json!([1, 2]));
        assert_eq!(
            function
                .take_input_set()
                .expect("Couldn't get input set")
                .remove(0),
            json!(1),
            "The value from input set wasn't what was expected"
        );
    }

    fn test_function(array_order: i32) -> RuntimeFunction {
        let out_conn = OutputConnection::new(
            Output("/other/input/1".into()),
            1,
            1,
            0,
            String::default(),
            #[cfg(feature = "debugger")]
            String::default(),
        );
        RuntimeFunction::new(
            #[cfg(feature = "debugger")]
            "test",
            #[cfg(feature = "debugger")]
            "/test",
            "file://fake/implementation",
            vec![Input::new(#[cfg(feature = "debugger")] "",
                            array_order, false, None, None)],
            1,
            0,
            &[out_conn],
            false,
        )
    }

    #[cfg(feature = "debugger")]
    #[test]
    fn debugger_can_inspect_non_full_input() {
        let mut function = test_function(0);
        function.init();
        function.send(0, json!(1));
        assert_eq!(
            function.inputs().len(),
            1,
            "Could not read incomplete input set"
        );
    }

    #[cfg(feature = "debugger")]
    #[test]
    fn can_display_function() {
        let function = test_function(0);
        let _ = format!("{function}");
    }

    #[cfg(feature = "debugger")]
    #[test]
    fn can_display_function_with_inputs() {
        let output_route = OutputConnection::new(
            Output("/other/input/1".into()),
            1,
            1,
            0,
            String::default(),
            #[cfg(feature = "debugger")]
            String::default(),
        );
        let mut function = RuntimeFunction::new(
            #[cfg(feature = "debugger")]
            "test",
            #[cfg(feature = "debugger")]
            "/test",
            "file://fake/test",
            vec![Input::new("", 0, false, None, None)],
            0,
            0,
            &[output_route.clone()],
            false,
        );
        function.init();
        function.send(0, json!(1));
        let _ = format!("{function}");
        assert_eq!(
            &vec!(output_route),
            function.get_output_connections(),
            "output routes not as originally set"
        );
    }

    #[test]
    fn can_get_function_name_and_id_and_location() {
        let function = test_function(0);
        #[cfg(feature = "debugger")]
        assert_eq!("test".to_string(), function.name());
        assert_eq!(1, function.id());
        assert_eq!(
            "file://fake/implementation",
            function.implementation_location()
        );
    }

    mod misc {
        use serde_json::{json, Value};

        use crate::model::input::Input;
        use crate::model::runtime_function::RuntimeFunction;

        fn test_function(array_order: i32, generic: bool) -> RuntimeFunction {
            RuntimeFunction::new(
                #[cfg(feature = "debugger")]
                    "test",
                #[cfg(feature = "debugger")]
                    "/test",
                "file://fake/test",
                vec![Input::new(
                    #[cfg(feature = "debugger")] "", array_order, generic,
                    None, None)],
                0,
                0,
                &[],
                false,
            )
        }

        // Test type conversion and sending
        //                         |                   Destination
        //                         |Generic     Non-Array       Array       Array of Arrays
        // Value       Value order |    N/A         0               1       2      <---- Array Order
        //  Non-Array       (0)    |   send     (0) send        (-1) wrap   (-2) wrap in array of arrays
        //  Array           (1)    |   send     (1) iter        (0) send    (-1) wrap in array
        //  Array of Arrays (2)    |   send     (2) iter/iter   (1) iter    (0) send
        #[test]
        fn test_sending() {
            #[derive(Debug)]
            struct TestCase {
                value: Value,
                destination_is_generic: bool,
                destination_array_order: i32,
                value_expected: Value,
            }

            let test_cases = vec![
                // Column 0 test cases
                TestCase {
                    value: json!(1),
                    destination_is_generic: true,
                    destination_array_order: 0,
                    value_expected: json!(1),
                },
                TestCase {
                    value: json!([1]),
                    destination_is_generic: true,
                    destination_array_order: 0,
                    value_expected: json!([1]),
                },
                TestCase {
                    value: json!([[1, 2], [3, 4]]),
                    destination_is_generic: true,
                    destination_array_order: 0,
                    value_expected: json!([[1, 2], [3, 4]]),
                },
                // Column 1 Test Cases
                TestCase {
                    value: json!(1),
                    destination_is_generic: false,
                    destination_array_order: 0,
                    value_expected: json!(1),
                },
                TestCase {
                    value: json!([1, 2]),
                    destination_is_generic: false,
                    destination_array_order: 0,
                    value_expected: json!(1),
                },
                TestCase {
                    value: json!([[1, 2], [3, 4]]),
                    destination_is_generic: false,
                    destination_array_order: 0,
                    value_expected: json!(1),
                },
                // Column 2 Test Cases
                TestCase {
                    value: json!(1),
                    destination_is_generic: false,
                    destination_array_order: 1,
                    value_expected: json!([1]),
                },
                TestCase {
                    value: json!([1, 2]),
                    destination_is_generic: false,
                    destination_array_order: 1,
                    value_expected: json!([1, 2]),
                },
                TestCase {
                    value: json!([[1, 2], [3, 4]]),
                    destination_is_generic: false,
                    destination_array_order: 1,
                    value_expected: json!([1, 2]),
                },
                // Column 3 Test Cases
                TestCase {
                    value: json!(1),
                    destination_is_generic: false,
                    destination_array_order: 2,
                    value_expected: json!([[1]]),
                },
                TestCase {
                    value: json!([1, 2]),
                    destination_is_generic: false,
                    destination_array_order: 2,
                    value_expected: json!([[1, 2]]),
                },
                TestCase {
                    value: json!([[1, 2], [3, 4]]),
                    destination_is_generic: false,
                    destination_array_order: 2,
                    value_expected: json!([[1, 2], [3, 4]]),
                },
            ];

            for test_case in test_cases {
                // Setup
                let mut function = test_function(test_case.destination_array_order,
                test_case.destination_is_generic);

                // Test
                assert!(function.send(0, test_case.value));

                // Check
                assert_eq!(
                    test_case.value_expected,
                    function
                        .take_input_set()
                        .expect("Couldn't get input set")
                        .remove(0)
                );
            }
        }
    }
}