flowc 0.91.0

A compiler for 'flow' programs
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
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
//! This module is responsible for parsing the flow tree and gathering information into a set of
//! flat tables that the compiler can use for code generation.

use log::{debug, error, info};

use flowcore::errors::ResultExt;
use flowcore::model::connection::Connection;
use flowcore::model::datatype::DataType;
use flowcore::model::flow_definition::FlowDefinition;
use flowcore::model::io::{IO, IOType};
use flowcore::model::process::Process::FlowProcess;
use flowcore::model::process::Process::FunctionProcess;
use flowcore::model::route::HasRoute;
use flowcore::model::route::Route;

use crate::compiler::compile::CompilerTables;
use crate::errors::*;

/// Recursively go through the flow hierarchy, harvesting out functions and connections within
/// each flow into the `CompilerTables` that will be used in later compilers.
pub fn gather_functions_and_connections(flow: &FlowDefinition, tables: &mut CompilerTables) -> Result<()> {
    info!("\n=== Compiler: Gathering Functions and Connections");
    _gather_functions_and_connections(flow, tables)?;

    tables.sort_functions();

    tables.create_routes_table();

    info!("Gathered {} functions and {} connections", tables.functions.len(), tables.connections.len());

    Ok(())
}

fn _gather_functions_and_connections(flow: &FlowDefinition, tables: &mut CompilerTables) -> Result<()> {
    // Add Connections from this flow hierarchy to the connections table
    let mut connections = flow.connections.clone();
    tables.connections.append(&mut connections);

    // Do the same for all subprocesses referenced from this one
    for subprocess in &flow.subprocesses {
        match subprocess.1 {
            FlowProcess(ref flow) => {
                _gather_functions_and_connections(flow, tables)?; // recurse
            }
            FunctionProcess(ref function) => {
                // Give function a unique id and add to the table of functions
                let mut table_function = function.clone();
                table_function.set_id(tables.functions.len());
                tables.functions.push(table_function);
            }
        }
    }

    // Add the library references of this flow into the tables list
    let lib_refs = &flow.lib_references;
    tables.libs.extend(lib_refs.iter().cloned());

    // Add the context references of this flow into the tables list
    let context_refs = &flow.context_references;
    tables.context_functions.extend(context_refs.iter().cloned());

    Ok(())
}

/// Take the original table of connections as gathered from the flow hierarchy, and for each one
/// follow it through any intermediate connections (sub-flow boundaries) to arrive at the final
/// destination. Then create a new direct connection from source to destination and add that
/// to the table of "collapsed" connections which will be used to configure the outputs of the
/// functions.
/// Valid initiation points of collapsed connections are:
/// - FunctionIO (Output)
/// - FlowInput with an initializer
/// - FlowOutput with an initializer
///
/// Prerequisites for this compilation phase are:
/// - `tables.functions` is populated by `gather_functions_and_connections`
/// - `tables.functions` functions must be indexed by `gather_functions_and_connections`
/// - `tables.connections`is populated by `gather_functions_and_connections`
/// - `tables.destination_routes` is populated by `create_routes_table`
pub fn collapse_connections(tables: &mut CompilerTables) -> Result<()> {
    info!("\n=== Compiler: Collapsing {} flow connections", tables.connections.len());
    let mut collapsed_connections: Vec<Connection> = Vec::new();

    for connection in &tables.connections {
        match connection.from_io().io_type() {
            // connection starts at a Function output, or is a connection from a value at a Function's input
            &IOType::FunctionOutput | &IOType::FunctionInput => {
                debug!("Trying to create connection from function IO at '{}'",
                       connection.from_io().route());
                if connection.to_io().io_type() == &IOType::FunctionInput {
                    debug!("\tFound direct connection to function input at '{}'",
                        connection.to_io().route());
                    collapsed_connections.push(connection.clone());
                } else {
                    // If the connection enters or leaves this flow, then follow it to all destinations at function inputs
                    for (source_subroute, destination_io) in find_connection_destinations(
                        Route::from(""),
                        connection.to_io(),
                        connection.level(),
                        &tables.connections)? {
                        let mut collapsed_connection = connection.clone();
                        // append the subroute from the origin function IO - to select from with in that IO
                        // as prescribed by the connections along the way
                        let from_route = connection
                            .from_io()
                            .route()
                            .clone()
                            .extend(&source_subroute)
                            .clone();
                        collapsed_connection
                            .from_io_mut()
                            .set_route(&from_route, &IOType::FunctionOutput);
                        *collapsed_connection.to_io_mut() = destination_io;
                        DataType::compatible_types(collapsed_connection.from_io().datatypes(),
                        collapsed_connection.to_io().datatypes(), &source_subroute)
                            .chain_err(|| format!("Incompatible types in collapsed connection from '{}' to '{}'",
                            collapsed_connection.from_io().route(), collapsed_connection.to_io().route()))?;
                        debug!("\tIndirect connection {}", collapsed_connection);
                        collapsed_connections.push(collapsed_connection);
                    }
                }
            },

            // connection starts at a flow's input via a FlowInputInitializer - propagate to destination function
            IOType::FlowInput | IOType::FlowOutput => {
                if connection.from_io().get_initializer().is_some() {
                    // find the destination functions (the connection could split to multiple destinations)
                    let destinations = if connection.to_io().io_type() == &IOType::FunctionInput {
                        // Flow input (or output) (that has an initializer) connects directly to a function's Input
                        vec!((Route::default(), connection.to_io().clone()))
                    } else {
                        find_connection_destinations(
                            Route::from(""),
                            connection.to_io(),
                            connection.level(),
                            &tables.connections,
                        )?
                    };

                    for (_, destination_io) in destinations {
                        let (destination_function_id, destination_input_index, _) =
                        tables.destination_routes.get(destination_io.route())
                            .ok_or(format!("Could not find a destination route matching '{}'", destination_io.route()))?;

                        // get a mutable reference to the destination function and set the initializer on it
                        let destination_function = tables.functions.get_mut(*destination_function_id)
                            .ok_or(format!("Could not find a function #{destination_function_id}"))?;

                        let flow_initializer = connection.from_io().get_initializer().clone();

                        // TODO check types

                        destination_function.
                            set_flow_initializer(*destination_input_index, flow_initializer)?;
                    }
                }
            }
        }
    }

    info!("{} connections collapsed down to {}", tables.connections.len(), collapsed_connections.len());
    tables.collapsed_connections = collapsed_connections;

    Ok(())
}

/*
    Given a route we have a connection to, attempt to find the final destinations, potentially
    traversing multiple intermediate connections (recursively) until we find any that do not
    end at a flow. Return them as the final destinations.

    As a connection at a flow boundary can connect to multiple destinations, one
    original connection can branch to connect to multiple destinations.

        Chained connections allowed
               origin          intermediate    destination
                 +-----------------+--------------+
               function          flow io       function

        Connections (before combining them) only exist within a given flow, to Functions within it
        or to/from outputs/inputs of that flow, and to/from inputs/outputs of sub-flows referenced

        There are six connection types:
            - Function Input
            - Function Output
            - Sub-flow Input
            - Sub-flow Output
            - Flow Input
            - Flow Output

        If we process from From->To, then there are only 3 source types

        Connection Source types:
            - Function Output
            - Sub-flow Output
            - Flow Input

        and 3 destination types:
            - Function Input
            - Sub-flow Input
            - Flow Output

        Giving nine possible combinations:

         Case Source Type               Destination Type        Description
         ------------------------------------------------------------------------------------------------
   Handled directly by collapse_connections
         1    Function Output           Function Input          Direct connection within a flow
         2    Function Output           Flow Input (subflow)
         3    Function Output           Flow Output             From a function and exits this flow

   Handled by find_function_destinations
         4    Flow Output (subflow)     Function Input          From a subflow into a function in this flow
         5    Flow Output (subflow)     Flow Input (subflow)    Connector between two sub-flows
         6    Flow Output (subflow)     Flow Output (parent)    From a sub-flow and exits this flow

         7    Flow Input (from parent)  Function Input          Enters flow from higher level into a Function
         8    Flow Input (from parent)  Flow Input (subflow)    Enters flow from higher level into a Sub-flow
         9    Flow Input (from parent)  Flow Output             A pass-through connection within a flow

         Output is: source_subroute: Route, final_destination: Route
*/
fn find_connection_destinations(
    prev_subroute: Route,
    from_io: &IO,
    from_level: usize,
    connections: &[Connection],
) -> Result<Vec<(Route, IO)>> {
    let mut destinations = vec![];

    for next_connection in connections {
        if let Some(subroute) = next_connection
            .from_io()
            .route()
            .sub_route_of(from_io.route())
        {
            let next_level = match *next_connection.from_io().io_type() {
                // Can't escape the root!
                IOType::FlowOutput if from_level > 0 => from_level - 1,
                IOType::FlowOutput if from_level == 0 => usize::MAX,
                IOType::FlowInput => from_level + 1,
                _ => from_level,
            };

            // Avoid infinite recursion and stack overflow
            if next_connection.level() == next_level {
                // Accumulate any subroute from this connection to the origin subroute
                let accumulated_source_subroute = prev_subroute.clone().extend(&subroute).clone();

                match *next_connection.to_io().io_type() {
                    IOType::FunctionInput => {
                        debug!("\t\tFound destination function input at '{}'",
                            next_connection.to_io().route());
                        destinations
                            .push((accumulated_source_subroute, next_connection.to_io().clone()));
                    },

                    IOType::FunctionOutput => error!("Error - destination of {:?} is a functions output!",
                        next_connection),

                    IOType::FlowInput => {
                        debug!("\t\tFollowing connection into sub-flow via '{}'", from_io.route());
                        let new_destinations = &mut find_connection_destinations(
                            accumulated_source_subroute,
                            next_connection.to_io(),
                            next_connection.level(),
                            connections,
                        )?;
                        destinations.append(new_destinations);
                    },

                    IOType::FlowOutput => {
                        debug!("\t\tFollowing connection out of flow via '{}'", from_io.route());
                        let new_destinations = &mut find_connection_destinations(
                            accumulated_source_subroute,
                            next_connection.to_io(),
                            next_connection.level(),
                            connections,
                        )?;
                        destinations.append(new_destinations);
                    }
                }
            }
        }
    }

    // Some chains or sub-chains of connections maybe dead ends, without that being an error
    if destinations.is_empty() {
        info!("Connection from '{}' : did not find a destination Function Input", from_io.route());
    } else {
        // check that the partial connection has compatible source and destinations types
        for (sub_route, destination_io) in &destinations {
            DataType::compatible_types(from_io.datatypes(), destination_io.datatypes(), sub_route)
                .chain_err(|| format!("Failed to connect '{}{sub_route}' to '{}' due to incompatible types",
                from_io.route(), destination_io.route()))?;
        }
    }

    Ok(destinations)
}

#[cfg(test)]
mod test {
    use flowcore::model::connection::Connection;
    use flowcore::model::datatype::STRING_TYPE;
    use flowcore::model::io::{IO, IOType};
    use flowcore::model::route::HasRoute;
    use flowcore::model::route::Route;

    use crate::compiler::compile::CompilerTables;

    use super::collapse_connections;

    #[test]
    fn collapse_drops_a_useless_connections() {
        let mut unused = Connection::new("/f1/a", "/f2/a");
        unused
            .connect(IO::new(vec!(STRING_TYPE.into()), "/f1/a"),
                     IO::new(vec!(STRING_TYPE.into()), "/f2/a"), 1)
            .expect("Could not connect IOs");
        unused.to_io_mut().set_io_type(IOType::FlowInput);

        let mut tables = CompilerTables::new();
        tables.connections = vec![unused];
        collapse_connections(&mut tables).expect("Could not collapse connections");
        assert_eq!(tables.collapsed_connections.len(), 0);
    }

    #[test]
    fn no_collapse_of_a_loopback_connection() {
        let mut only_connection = Connection::new("/function1/out", "/function1/in");
        only_connection
            .connect(
                IO::new(vec!(STRING_TYPE.into()), "/function1/out"),
                IO::new(vec!(STRING_TYPE.into()), "/function1/in"),
                0,
            ).expect("Could not connect IOs");
        only_connection.from_io_mut().set_io_type(IOType::FunctionOutput);
        only_connection.to_io_mut().set_io_type(IOType::FunctionInput);

        let mut tables = CompilerTables::new();
        tables.connections = vec![only_connection];
        collapse_connections(&mut tables).expect("Could not collapse connections");
        assert_eq!(tables.collapsed_connections.len(), 1);
        assert_eq!(*tables.collapsed_connections[0].from_io().route(), Route::from("/function1/out"));
        assert_eq!(*tables.collapsed_connections[0].to_io().route(), Route::from("/function1/in"));
    }

    #[test]
    fn no_collapse_of_a_direct_connection() {
        let mut only_connection = Connection::new("/function1/out", "/function2/in");
        only_connection
            .connect(
                IO::new(vec!(STRING_TYPE.into()), "/function1/out"),
                IO::new(vec!(STRING_TYPE.into()), "/function2/in"),
                0,
            )
            .expect("Could not connect IOs");
        only_connection.from_io_mut().set_io_type(IOType::FunctionOutput);
        only_connection.to_io_mut().set_io_type(IOType::FunctionInput);

        let mut tables = CompilerTables::new();
        tables.connections = vec![only_connection];
        collapse_connections(&mut tables).expect("Could not collapse connections");
        assert_eq!(tables.collapsed_connections.len(), 1);
        assert_eq!(*tables.collapsed_connections[0].from_io().route(), Route::from("/function1/out"));
        assert_eq!(*tables.collapsed_connections[0].to_io().route(), Route::from("/function2/in"));
    }

    #[test]
    fn collapse_a_connection() {
        let mut left_side = Connection::new("/function1", "/flow2/a");
        left_side
            .connect(
                IO::new(vec!(STRING_TYPE.into()), "/function1"),
                IO::new(vec!(STRING_TYPE.into()), "/flow2/a"),
                0,
            )
            .expect("Could not connect IOs");
        left_side.from_io_mut().set_io_type(IOType::FunctionOutput);
        left_side.to_io_mut().set_io_type(IOType::FlowInput);

        // This one goes to a flow but then nowhere, so should be dropped
        let mut extra_one = Connection::new("/flow2/a", "/flow2/f4/a");
        extra_one
            .connect(
                IO::new(vec!(STRING_TYPE.into()), "/flow2/a"),
                IO::new(vec!(STRING_TYPE.into()), "/flow2/f4/a"),
                1,
            )
            .expect("Could not connect IOs");
        extra_one.from_io_mut().set_io_type(IOType::FlowInput);
        extra_one.to_io_mut().set_io_type(IOType::FlowInput); // /flow2/f4 doesn't exist

        let mut right_side = Connection::new("/flow2/a", "/flow2/function3");
        right_side
            .connect(
                IO::new(vec!(STRING_TYPE.into()), "/flow2/a"),
                IO::new(vec!(STRING_TYPE.into()), "/flow2/function3"),
                1,
            )
            .expect("Could not connect IOs");
        right_side.from_io_mut().set_io_type(IOType::FlowInput);
        right_side.to_io_mut().set_io_type(IOType::FunctionInput);

        let mut tables = CompilerTables::new();
        tables.connections = vec![left_side, extra_one, right_side];
        collapse_connections(&mut tables).expect("Could not collapse connections");
        assert_eq!(tables.collapsed_connections.len(), 1);
        assert_eq!(*tables.collapsed_connections[0].from_io().route(), Route::from("/function1"));
        assert_eq!(*tables.collapsed_connections[0].to_io().route(), Route::from("/flow2/function3"));
    }

    /*
        This tests a connection into a sub-flow, that in the sub-flow branches with two
        connections to different elements in it.
        This should result in two end to end connections from the original source to the elements
        in the subflow
    */
    #[test]
    fn collapse_two_connections_from_flow_boundary() {
        let mut left_side = Connection::new("/f1", "/f2/a");
        left_side
            .connect(IO::new(vec!(STRING_TYPE.into()), "/f1"),
                     IO::new(vec!(STRING_TYPE.into()), "/f2/a"), 0)
            .expect("Could not connect IOs");
        left_side.from_io_mut().set_io_type(IOType::FunctionOutput);
        left_side.to_io_mut().set_io_type(IOType::FlowInput);

        let mut right_side_one = Connection::new("/f2/a", "/f2/value1");
        right_side_one
            .connect(
                IO::new(vec!(STRING_TYPE.into()), "/f2/a"),
                IO::new(vec!(STRING_TYPE.into()), "/f2/value1"),
                1,
            )
            .expect("Could not connect IOs");
        right_side_one.from_io_mut().set_io_type(IOType::FlowInput);
        right_side_one.to_io_mut().set_io_type(IOType::FunctionInput);

        let mut right_side_two = Connection::new("/f2/a", "/f2/value2");
        right_side_two
            .connect(
                IO::new(vec!(STRING_TYPE.into()), "/f2/a"),
                IO::new(vec!(STRING_TYPE.into()), "/f2/value2"),
                1,
            )
            .expect("Could not connect IOs");
        right_side_two.from_io_mut().set_io_type(IOType::FlowInput);
        right_side_two.to_io_mut().set_io_type(IOType::FunctionInput);

        let mut tables = CompilerTables::new();
        tables.connections = vec![left_side, right_side_one, right_side_two];
        collapse_connections(&mut tables).expect("Could not collapse connections");
        assert_eq!(2, tables.collapsed_connections.len());

        assert_eq!(*tables.collapsed_connections[0].from_io().route(), Route::from("/f1"));
        assert_eq!(*tables.collapsed_connections[0].to_io().route(), Route::from("/f2/value1"));

        assert_eq!(*tables.collapsed_connections[1].from_io().route(), Route::from("/f1"));
        assert_eq!(*tables.collapsed_connections[1].to_io().route(), Route::from("/f2/value2"));
    }

    #[test]
    fn collapse_connection_into_sub_flow() {
        let mut first_level = Connection::new("/function1/out", "/flow1/a");
        first_level
            .connect(
                IO::new(vec!(STRING_TYPE.into()), "/function1/out"),
                IO::new(vec!(STRING_TYPE.into()), "/flow1/a"),
                0,
            )
            .expect("Could not connect IOs");
        first_level.from_io_mut().set_io_type(IOType::FunctionOutput);
        first_level.to_io_mut().set_io_type(IOType::FlowInput);

        let mut second_level = Connection::new("/flow1/a", "/flow1/flow2/a");
        second_level
            .connect(
                IO::new(vec!(STRING_TYPE.into()), "/flow1/a"),
                IO::new(vec!(STRING_TYPE.into()), "/flow1/flow2/a"),
                1,
            )
            .expect("Could not connect IOs");
        second_level.from_io_mut().set_io_type(IOType::FlowInput);
        second_level.to_io_mut().set_io_type(IOType::FlowInput);

        let mut third_level = Connection::new("/flow1/flow2/a", "/flow1/flow2/func/in");
        third_level
            .connect(
                IO::new(vec!(STRING_TYPE.into()), "/flow1/flow2/a"),
                IO::new(vec!(STRING_TYPE.into()), "/flow1/flow2/func/in"),
                2,
            )
            .expect("Could not connect IOs");
        third_level.from_io_mut().set_io_type(IOType::FlowInput);
        third_level.to_io_mut().set_io_type(IOType::FunctionInput);

        let mut tables = CompilerTables::new();
        tables.connections = vec![first_level, second_level, third_level];

        collapse_connections(&mut tables).expect("Could not collapse connections");
        assert_eq!(1, tables.collapsed_connections.len());

        assert_eq!(*tables.collapsed_connections[0].from_io().route(), Route::from("/function1/out"));
        assert_eq!(*tables.collapsed_connections[0].to_io().route(), Route::from("/flow1/flow2/func/in"));
    }

    #[test]
    fn does_not_collapse_a_non_connection() {
        let mut one = Connection::new("/f1/a", "/f2/a");
        one.connect(IO::new(vec!(STRING_TYPE.into()), "/f1/a"),
                    IO::new(vec!(STRING_TYPE.into()), "/f2/a"), 1)
            .expect("Could not connect IOs");

        let mut other = Connection::new("/f3/a", "/f4/a");
        other
            .connect(IO::new(vec!(STRING_TYPE.into()), "/f3/a"),
                     IO::new(vec!(STRING_TYPE.into()), "/f4/a"), 1)
            .expect("Could not connect IOs");
        let mut tables = CompilerTables::new();
        tables.connections = vec![one, other];
        collapse_connections(&mut tables).expect("Could not collapse connections");
        assert_eq!(tables.collapsed_connections.len(), 2);
    }
}