|
1 | | -mod configuration; |
2 | 1 | pub mod context; |
3 | 2 | pub mod error; |
4 | 3 | pub mod implementation; |
5 | 4 | pub mod registry; |
| 5 | +mod executor; |
| 6 | +mod config; |
6 | 7 |
|
7 | | -use crate::configuration::Config; |
| 8 | +use crate::context::signal::Signal; |
| 9 | +use crate::executor::Executor; |
8 | 10 | use crate::implementation::collect; |
9 | 11 | use code0_flow::flow_config::load_env_file; |
10 | | -use context::{Context, ContextEntry, ContextResult}; |
11 | | -use error::RuntimeError; |
| 12 | +use context::Context; |
12 | 13 | use futures_lite::StreamExt; |
13 | 14 | use log::error; |
14 | 15 | use prost::Message; |
15 | 16 | use registry::FunctionStore; |
16 | 17 | use std::collections::HashMap; |
17 | 18 | use tonic_health::pb::health_server::HealthServer; |
18 | 19 | use tucana::shared::value::Kind; |
19 | | -use tucana::shared::{ExecutionFlow, ListValue, NodeFunction, Value}; |
20 | | - |
21 | | -fn handle_node_function( |
22 | | - function: NodeFunction, |
23 | | - node_functions: &HashMap<i64, NodeFunction>, |
24 | | - store: &FunctionStore, |
25 | | - context: &mut Context, |
26 | | -) -> Result<Value, RuntimeError> { |
27 | | - let runtime_function = match store.get(function.runtime_function_id.as_str()) { |
28 | | - Some(fc) => fc, |
29 | | - None => todo!("Retrun if no funtion is present"), |
30 | | - }; |
31 | | - |
32 | | - let mut parameter_collection: Vec<Value> = vec![]; |
33 | | - for parameter in function.parameters { |
34 | | - if let Some(node_value) = parameter.value { |
35 | | - if let Some(value) = node_value.value { |
36 | | - match value { |
37 | | - // Its just a normal value, directly a paramter |
38 | | - tucana::shared::node_value::Value::LiteralValue(v) => { |
39 | | - parameter_collection.push(v) |
40 | | - } |
41 | | - // Its a reference to an already executed function that returns value is the parameter of this function |
42 | | - tucana::shared::node_value::Value::ReferenceValue(reference) => { |
43 | | - let optional_value = context.get(&reference); |
44 | | - |
45 | | - // Look if its even present |
46 | | - let context_result = match optional_value { |
47 | | - Some(context_result) => context_result, |
48 | | - None => { |
49 | | - todo!("Required function that holds the parameter wasnt executed") |
50 | | - } |
51 | | - }; |
52 | | - |
53 | | - // A reference is present. Look up the real value |
54 | | - match context_result { |
55 | | - // The reference is a exeuction result of a node |
56 | | - ContextResult::NodeExecutionResult(node_result) => match node_result { |
57 | | - Ok(value) => { |
58 | | - parameter_collection.push(value.clone()); |
59 | | - } |
60 | | - Err(err) => return Err(err), |
61 | | - }, |
62 | | - // The reference is a parameter of a node |
63 | | - ContextResult::ParameterResult(parameter_result) => { |
64 | | - parameter_collection.push(parameter_result.clone()); |
65 | | - } |
66 | | - } |
67 | | - } |
68 | | - |
69 | | - // Its another function, that result is a direct parameter to this function |
70 | | - tucana::shared::node_value::Value::NodeFunctionId(another_node_function) => { |
71 | | - // As this is another new indent, a new context will be opened |
72 | | - |
73 | | - let function_result = match node_functions.get(&another_node_function) { |
74 | | - Some(function_result) => { |
75 | | - context.next_context(); |
76 | | - handle_node_function(function_result.clone(), node_functions, store, context ) |
77 | | - }, |
78 | | - None => { |
79 | | - todo!("Handle node not found. This should normally not happen") |
80 | | - } |
81 | | - }; |
82 | | - |
83 | | - |
84 | | - let entry = ContextEntry::new( |
85 | | - Result::Ok(function_result.clone()?), |
86 | | - parameter_collection.clone(), |
87 | | - ); |
88 | | - |
89 | | - context.write_to_current_context(entry); |
90 | | - } |
91 | | - } |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - let result = runtime_function(¶meter_collection, context); |
96 | | - |
97 | | - // Result will be added to the current context |
98 | | - let entry = ContextEntry::new(result.clone(), parameter_collection.clone()); |
99 | | - context.write_to_current_context(entry); |
100 | | - |
101 | | - // Check if there is a next node, if not then this was the last one |
102 | | - match function.next_node_id { |
103 | | - Some(next_node_function_id) => { |
104 | | - let node = match node_functions.get(&next_node_function_id) { |
105 | | - Some(node) => node, |
106 | | - None => todo!("Handle node not found. This should normally not happen"), |
107 | | - }; |
108 | | - |
109 | | - // Increment the context node! |
110 | | - context.next_node(); |
111 | | - |
112 | | - return handle_node_function(node.clone(), node_functions, store, context); |
113 | | - } |
114 | | - None => { |
115 | | - if context.is_end() { |
116 | | - return result; |
117 | | - } |
118 | | - |
119 | | - context.leave_context(); |
120 | | - } |
121 | | - } |
122 | | - } |
123 | | - |
124 | | - Err(RuntimeError::default()) |
125 | | -} |
| 20 | +use tucana::shared::{ExecutionFlow, NodeFunction, Value}; |
| 21 | +use crate::config::Config; |
126 | 22 |
|
127 | 23 | fn handle_message(flow: ExecutionFlow, store: &FunctionStore) -> Option<Value> { |
128 | | - let mut context = Context::new(); |
| 24 | + let context = Context::new(); |
129 | 25 |
|
130 | 26 | let node_functions: HashMap<i64, NodeFunction> = flow |
131 | 27 | .node_functions |
132 | 28 | .into_iter() |
133 | 29 | .map(|node| return (node.database_id, node)) |
134 | 30 | .collect(); |
135 | | - |
136 | | - if let Some(node) = node_functions.get(&flow.starting_node_id) { |
137 | | - return match handle_node_function(node.clone(), &node_functions, store, &mut context) { |
138 | | - Ok(result) => { |
139 | | - println!( |
140 | | - "Execution completed successfully: The value is {:?}", |
141 | | - result |
142 | | - ); |
143 | | - Some(result) |
144 | | - } |
145 | | - Err(runtime_error) => { |
146 | | - println!("Runtime Error: {:?}", runtime_error); |
147 | | - None |
148 | | - } |
149 | | - }; |
| 31 | + |
| 32 | + let mut executor = Executor::new(store, node_functions, context); |
| 33 | + match executor.execute(flow.starting_node_id) { |
| 34 | + Signal::Success(v) => Some(v.clone()), |
| 35 | + _ => None |
150 | 36 | } |
151 | | - None |
152 | 37 | } |
153 | 38 |
|
154 | 39 | #[tokio::main] |
|
0 commit comments