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
mod error;
mod helpers;
mod operation_settings;
pub(crate) mod types;
use std::collections::HashMap;

pub use error::Error as GraphError;
use flow_expression_parser::ast::{
  BlockExpression,
  ConnectionExpression,
  ConnectionTargetExpression,
  FlowExpression,
  InstancePort,
  InstanceTarget,
};
use flow_graph::NodeReference;
use serde_json::Value;
use types::*;
use wick_config::config::components::{ComponentConfig, OperationConfig};
use wick_config::config::{ComponentImplementation, ExecutionSettings, FlowOperation};
use wick_packet::RuntimeConfig;

use self::helpers::{ensure_added, ParseHelper};
pub(crate) use self::operation_settings::{LiquidOperationConfig, OperationSettings};
use crate::interpreter::components::core;
use crate::HandlerMap;

pub(crate) trait NodeDecorator {
  fn decorate(node: &mut Node) -> Result<(), String>;
}

#[derive(Debug)]
#[must_use]
pub(crate) struct Reference(NodeReference);

impl From<&NodeReference> for Reference {
  fn from(v: &NodeReference) -> Self {
    Self(v.clone())
  }
}

impl Reference {
  pub(crate) fn name(&self) -> &str {
    self.0.name()
  }
  pub(crate) fn namespace(&self) -> &str {
    self.0.component_id()
  }
}

fn register_operation(
  mut scope: Vec<String>,
  network: &mut Network,
  flow: &mut FlowOperation,
  handlers: &HandlerMap,
  op_config_base: &LiquidOperationConfig,
) -> Result<(), GraphError> {
  scope.push(flow.name().to_owned());

  for flow in flow.flows_mut() {
    let scope = scope.clone();
    register_operation(scope, network, flow, handlers, op_config_base)?;
  }
  let name = scope.join("::");
  let mut schematic = Schematic::new(name, Default::default(), Default::default());
  let mut ids = flow.instances().keys().cloned().collect::<Vec<_>>();
  ids.sort();

  for name in ids {
    let def = flow.instances().get(&name).unwrap();
    debug!(%name, config=?def.data(),settings=?def.settings(), "registering operation");
    let mut op_config = op_config_base.clone();
    op_config.set_template(def.data().cloned());

    let node = schematic.add_and_get_mut(
      name,
      NodeReference::new(def.component_id(), def.name()),
      OperationSettings::new(op_config.clone(), def.settings().cloned()),
    );
    helpers::decorate(def.component_id(), def.name(), node, handlers)?;
  }

  expand_until_done(&mut schematic, flow, handlers, op_config_base, expand_expressions)?;

  for expression in flow.expressions() {
    process_flow_expression(&mut schematic, expression, handlers)?;
  }

  network.add_schematic(schematic);
  Ok(())
}

fn process_flow_expression(
  schematic: &mut Schematic,
  expr: &FlowExpression,
  handlers: &HandlerMap,
) -> Result<(), GraphError> {
  match expr {
    FlowExpression::ConnectionExpression(expr) => process_connection_expression(schematic, expr, handlers)?,
    FlowExpression::BlockExpression(expr) => {
      for expr in expr.iter() {
        process_flow_expression(schematic, expr, handlers)?;
      }
    }
  }
  Ok(())
}

fn process_connection_expression(
  schematic: &mut Schematic,
  expr: &ConnectionExpression,
  _handlers: &HandlerMap,
) -> Result<(), GraphError> {
  let from = expr.from();
  let to = expr.to();
  assert!(
    to.port().name().is_some(),
    "Missing downstream port for expr: {:?}",
    expr
  );
  let to_port = schematic
    .find_mut(to.instance().id().unwrap())
    .map(|component| component.add_input(to.port().name().unwrap()));

  if to_port.is_none() {
    error!("missing downstream: instance {:?}", to);
    return Err(GraphError::missing_downstream(to.instance().id().unwrap()));
  }
  let to_port = to_port.unwrap();

  if let Some(component) = schematic.find_mut(from.instance().id().unwrap()) {
    let from_port = component.add_output(from.port().name().unwrap());
    trace!(
      ?from_port,
      from = %expr.from(),
      ?to_port,
      to = %expr.to(),
      "graph:connecting"
    );
    schematic.connect(from_port, to_port, Default::default())?;
  } else {
    panic!("Can't find component {}", from.instance());
  }
  Ok(())
}

#[allow(trivial_casts)]
fn expand_until_done(
  schematic: &mut Schematic,
  expressions: &mut FlowOperation,
  handlers: &HandlerMap,
  config: &LiquidOperationConfig,
  func: fn(
    &mut Schematic,
    &mut FlowOperation,
    &HandlerMap,
    &LiquidOperationConfig,
    &mut usize,
  ) -> Result<ExpandResult, GraphError>,
) -> Result<(), GraphError> {
  let mut id_index = 0;
  loop {
    let result = func(schematic, expressions, handlers, config, &mut id_index)?;

    if result == ExpandResult::Done {
      break;
    }
  }
  Ok(())
}

#[derive(Debug, PartialEq, Clone, Copy)]
enum ExpandResult {
  Done,
  Continue,
}

impl ExpandResult {
  fn update(self, next: ExpandResult) -> Self {
    if self == ExpandResult::Continue {
      self
    } else {
      next
    }
  }
}

#[allow(clippy::option_if_let_else)]
fn expand_expressions(
  schematic: &mut Schematic,
  flow: &mut FlowOperation,
  handlers: &HandlerMap,
  config: &LiquidOperationConfig,
  inline_id: &mut usize,
) -> Result<ExpandResult, GraphError> {
  let result = ExpandResult::Done;

  let config_map = flow
    .instances()
    .iter()
    .map(|(k, v)| {
      let mut base = config.clone();
      base.set_template(v.data().cloned());

      Ok::<_, GraphError>((k.clone(), (base, v.settings().cloned())))
    })
    .collect::<Result<HashMap<_, _>, _>>()?;
  add_nodes_to_schematic(schematic, flow.expressions_mut(), handlers, &config_map, inline_id)?;
  let result = result.update(expand_port_paths(schematic, flow.expressions_mut())?);
  let result = result.update(expand_defaulted_ports(schematic, flow.expressions_mut())?);
  Ok(result)
}

fn add_nodes_to_schematic(
  schem: &mut Schematic,
  flow: &mut [FlowExpression],
  handlers: &HandlerMap,
  config_map: &HashMap<String, (LiquidOperationConfig, Option<ExecutionSettings>)>,
  id_index: &mut usize,
) -> Result<(), GraphError> {
  for (_i, expression) in flow.iter_mut().enumerate() {
    match expression {
      FlowExpression::ConnectionExpression(conn) => {
        let config = conn
          .from()
          .instance()
          .id()
          .and_then(|id| config_map.get(id).cloned())
          .unwrap_or((LiquidOperationConfig::default(), None));

        ensure_added(schem, conn.from_mut().instance_mut(), handlers, config, id_index)?;

        let config = conn
          .to()
          .instance()
          .id()
          .and_then(|id| config_map.get(id).cloned())
          .unwrap_or((LiquidOperationConfig::default(), None));

        ensure_added(schem, conn.to_mut().instance_mut(), handlers, config, id_index)?;
      }
      FlowExpression::BlockExpression(expressions) => {
        add_nodes_to_schematic(schem, expressions.inner_mut(), handlers, config_map, id_index)?;
      }
    }
  }

  Ok(())
}

fn connection(
  from: (InstanceTarget, impl Into<InstancePort>),
  to: (InstanceTarget, impl Into<InstancePort>),
) -> FlowExpression {
  FlowExpression::connection(ConnectionExpression::new(
    ConnectionTargetExpression::new(from.0, from.1),
    ConnectionTargetExpression::new(to.0, to.1),
  ))
}

#[allow(clippy::option_if_let_else, clippy::too_many_lines, clippy::cognitive_complexity)]
fn expand_defaulted_ports(
  schematic: &mut Schematic,
  expressions: &mut [FlowExpression],
) -> Result<ExpandResult, GraphError> {
  let mut result = ExpandResult::Done;
  for (_i, expression) in expressions.iter_mut().enumerate() {
    match expression {
      FlowExpression::ConnectionExpression(expr) => {
        let (from, to) = expr.clone().into_parts();
        let (from_inst, from_port, _) = from.into_parts();
        let (to_inst, to_port, _) = to.into_parts();
        match (from_port, to_port) {
          (InstancePort::None, InstancePort::None) => {
            let from_node = schematic.get_node(&from_inst)?;
            let to_node = schematic.get_node(&to_inst)?;
            let from_node_ports = from_node.outputs();
            let to_node_ports = to_node.inputs();
            debug!(
              from = %from_inst, from_ports = ?from_node_ports, to = %to_inst, to_ports = ?to_node_ports,
              "graph:inferring ports for both up and down"
            );
            if from_node_ports.is_empty() && to_node_ports.is_empty() {
              // can't do anything yet.
              continue;
            }

            // If there's only one port on each side, connect them.
            if from_node_ports.len() == 1 && to_node_ports.len() == 1 {
              let from_port = from_node_ports[0].name();
              let to_port = to_node_ports[0].name();
              debug!(from = %from_inst, from_port,to = %to_inst, to_port, reason="unary", "graph:inferred ports");
              expression.replace(connection((from_inst, from_port), (to_inst, to_port)));
              result = ExpandResult::Continue;
              continue;
            }

            let mut new_connections = Vec::new();
            // if either side is a schematic input/output node, adopt the names of all ports we're pointing to.
            if matches!(from_inst, InstanceTarget::Input | InstanceTarget::Default) {
              for port in to_node_ports {
                let port_name = port.name();
                debug!(from = %from_inst, from_port=port_name,to = %to_inst, to_port=port_name, reason="upstream_default", "graph:inferred ports");
                new_connections.push(connection((from_inst.clone(), port_name), (to_inst.clone(), port_name)));
              }
            } else if matches!(to_inst, InstanceTarget::Output | InstanceTarget::Default) {
              for port in from_node_ports {
                let port_name = port.name();
                debug!(from = %from_inst, from_port=port_name,to = %to_inst, to_port=port_name, reason="downstream_default", "graph:inferred ports");
                new_connections.push(connection((from_inst.clone(), port_name), (to_inst.clone(), port_name)));
              }
            } else {
              for port in from_node_ports {
                if !to_node_ports.contains(port) && !matches!(to_inst, InstanceTarget::Output | InstanceTarget::Default)
                {
                  return Err(GraphError::port_inference_down(
                    &from_inst,
                    port.name(),
                    to_inst,
                    to_node_ports,
                  ));
                }
                let port_name = port.name();
                debug!(from = %from_inst, from_port=port_name,to = %to_inst, to_port=port_name, reason="all_downstream", "graph:inferred ports");
                new_connections.push(connection(
                  (from_inst.clone(), port.name()),
                  (to_inst.clone(), port.name()),
                ));
              }
            }

            assert!(!new_connections.is_empty(), "unhandled case for port inference");
            result = ExpandResult::Continue;
            expression.replace(FlowExpression::block(BlockExpression::new(new_connections)));
          }
          (InstancePort::None, to_port) => {
            let port_name = to_port.name().unwrap();
            let from_node = schematic.get_node(&from_inst)?;
            let ports = from_node.outputs();
            debug!(
              from = %from_inst, from_ports = ?ports, to = %to_inst,
              "graph:inferring ports for upstream"
            );
            // if we're at a schematic input node, adopt the name of what we're pointing to.
            if matches!(from_inst, InstanceTarget::Input | InstanceTarget::Default) {
              expression.replace(connection((from_inst, port_name), (to_inst, to_port.clone())));
              result = ExpandResult::Continue;
              continue;
            }
            if ports.len() == 1 {
              expression.replace(connection((from_inst, ports[0].name()), (to_inst, to_port.clone())));
              result = ExpandResult::Continue;
              continue;
            }

            if !ports.iter().any(|p| p.name() == port_name) {
              return Err(GraphError::port_inference_up(&to_inst, port_name, from_inst, ports));
            }

            result = ExpandResult::Continue;
            expression.replace(connection((from_inst, port_name), (to_inst, to_port.clone())));
          }
          (from_port, InstancePort::None) => {
            let port_name = from_port.name().unwrap();
            let to_node = schematic.get_node(&to_inst)?;
            let ports = to_node.inputs();
            debug!(
              from = %from_inst, to = %to_inst, to_ports = ?ports,
              "graph:inferring ports for downstream"
            );

            // if we're at a schematic input node, adopt the name of what we're pointing to.
            if matches!(to_inst, InstanceTarget::Output | InstanceTarget::Default) {
              expression.replace(connection((from_inst, from_port.clone()), (to_inst, port_name)));
              result = ExpandResult::Continue;
              continue;
            }

            if ports.len() == 1 {
              expression.replace(connection((from_inst, from_port.clone()), (to_inst, ports[0].name())));
              result = ExpandResult::Continue;
              continue;
            }

            if !ports.iter().any(|p| p.name() == port_name) {
              return Err(GraphError::port_inference_down(&from_inst, port_name, to_inst, ports));
            }

            result = ExpandResult::Continue;
            expression.replace(connection((from_inst, from_port.clone()), (to_inst, port_name)));
          }
          _ => continue,
        }
      }
      FlowExpression::BlockExpression(expressions) => {
        result = result.update(expand_defaulted_ports(schematic, expressions.inner_mut())?);
      }
    }
  }
  Ok(result)
}

#[allow(clippy::option_if_let_else)]
fn expand_port_paths(
  schematic: &mut Schematic,
  expressions: &mut [FlowExpression],
) -> Result<ExpandResult, GraphError> {
  let mut result = ExpandResult::Done;
  for (i, expression) in expressions.iter_mut().enumerate() {
    match expression {
      FlowExpression::ConnectionExpression(expr) => {
        let (from, to) = expr.clone().into_parts();
        let (from_inst, from_port, _) = from.into_parts();
        let (to_inst, to_port, _) = to.into_parts();
        if let InstancePort::Path(name, parts) = from_port {
          let id = format!("{}_pluck_{}_{}_[{}]", schematic.name(), i, name, parts.join(","));
          let config = HashMap::from([(
            "path".to_owned(),
            Value::Array(parts.into_iter().map(Value::String).collect()),
          )]);

          let node = schematic.add_and_get_mut(
            &id,
            NodeReference::new("core", "pluck"),
            OperationSettings::new(Some(RuntimeConfig::from(config)).into(), None),
          );
          core::pluck::Op::decorate(node).map_err(|e| GraphError::config(id.clone(), e))?;

          expression.replace(FlowExpression::block(BlockExpression::new(vec![
            connection((from_inst, &name), (InstanceTarget::named(&id), InstancePort::None)),
            connection((InstanceTarget::named(&id), InstancePort::None), (to_inst, to_port)),
          ])));
          result = ExpandResult::Continue;
        }
      }
      FlowExpression::BlockExpression(expressions) => {
        result = result.update(expand_port_paths(schematic, expressions.inner_mut())?);
      }
    }
  }
  Ok(result)
}

pub fn from_def(
  manifest: &mut wick_config::config::ComponentConfiguration,
  handlers: &HandlerMap,
) -> Result<Network, GraphError> {
  let mut network = Network::new(
    manifest.name().cloned().unwrap_or_default(),
    OperationSettings::new(manifest.root_config().cloned().into(), None),
  );

  let mut op_config_base = LiquidOperationConfig::default();
  op_config_base.set_root(manifest.root_config().cloned());

  if let ComponentImplementation::Composite(composite) = manifest.component_mut() {
    for flow in composite.operations_mut() {
      register_operation(vec![], &mut network, flow, handlers, &op_config_base)?;
    }
  }

  #[cfg(debug_assertions)]
  {
    let names: Vec<_> = network.schematics().iter().map(|s| s.name()).collect();
    trace!(nodes=?names,"graph:nodes");
    for schematic in network.schematics() {
      let schem_name = &schematic.name();
      for node in schematic.nodes() {
        let name = &node.name;
        let inputs = node.inputs().iter().map(|n| n.name()).collect::<Vec<_>>();
        let outputs = node.outputs().iter().map(|n| n.name()).collect::<Vec<_>>();
        trace!(schematic = schem_name, node = name, ?inputs, ?outputs, data=?node.data(), "graph:node");
      }
    }
  }

  Ok(network)
}