oxigdal-workflow 0.1.4

DAG-based workflow engine for complex geospatial processing pipelines
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
//! Built-in workflow template library.

use crate::error::{Result, WorkflowError};
use crate::templates::{
    Parameter, ParameterConstraints, ParameterType, ParameterValue, TemplateCategory,
    WorkflowTemplate,
};
use dashmap::DashMap;
use std::sync::Arc;

/// Workflow template library.
pub struct WorkflowTemplateLibrary {
    templates: Arc<DashMap<String, WorkflowTemplate>>,
}

impl WorkflowTemplateLibrary {
    /// Create a new template library.
    pub fn new() -> Self {
        let library = Self {
            templates: Arc::new(DashMap::new()),
        };

        // Add built-in templates
        library.register_builtin_templates();

        library
    }

    /// Register all built-in templates.
    fn register_builtin_templates(&self) {
        // Satellite processing template
        if let Ok(template) = Self::create_satellite_processing_template() {
            let _ = self.add_template(template);
        }

        // Change detection template
        if let Ok(template) = Self::create_change_detection_template() {
            let _ = self.add_template(template);
        }

        // Batch processing template
        if let Ok(template) = Self::create_batch_processing_template() {
            let _ = self.add_template(template);
        }

        // Terrain analysis template
        if let Ok(template) = Self::create_terrain_analysis_template() {
            let _ = self.add_template(template);
        }
    }

    /// Add a template to the library.
    pub fn add_template(&self, template: WorkflowTemplate) -> Result<()> {
        if self.templates.contains_key(&template.id) {
            return Err(WorkflowError::already_exists(&template.id));
        }

        self.templates.insert(template.id.clone(), template);
        Ok(())
    }

    /// Get a template by ID.
    pub fn get_template(&self, id: &str) -> Option<WorkflowTemplate> {
        self.templates.get(id).map(|entry| entry.clone())
    }

    /// Remove a template.
    pub fn remove_template(&self, id: &str) -> Option<WorkflowTemplate> {
        self.templates.remove(id).map(|(_, template)| template)
    }

    /// List all templates.
    pub fn list_templates(&self) -> Vec<WorkflowTemplate> {
        self.templates
            .iter()
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// List templates by category.
    pub fn list_by_category(&self, category: &TemplateCategory) -> Vec<WorkflowTemplate> {
        self.templates
            .iter()
            .filter(|entry| &entry.value().metadata.category == category)
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// Search templates by tag.
    pub fn search_by_tag(&self, tag: &str) -> Vec<WorkflowTemplate> {
        self.templates
            .iter()
            .filter(|entry| entry.value().tags.contains(&tag.to_string()))
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// Create satellite processing template.
    fn create_satellite_processing_template() -> Result<WorkflowTemplate> {
        let mut template = WorkflowTemplate::new(
            "satellite-processing",
            "Satellite Image Processing",
            "Process satellite imagery with atmospheric correction and cloud masking",
        );

        template.set_category(TemplateCategory::SatelliteProcessing);
        template.metadata.complexity = 3;
        template.add_tag("satellite");
        template.add_tag("processing");
        template.add_tag("imagery");

        template.add_parameter(Parameter {
            name: "input_path".to_string(),
            param_type: ParameterType::FilePath,
            description: "Input satellite image path".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        template.add_parameter(Parameter {
            name: "output_path".to_string(),
            param_type: ParameterType::FilePath,
            description: "Output processed image path".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        template.add_parameter(Parameter {
            name: "apply_cloud_mask".to_string(),
            param_type: ParameterType::Boolean,
            description: "Apply cloud masking".to_string(),
            required: false,
            default_value: Some(ParameterValue::Boolean(true)),
            constraints: None,
        });

        template.set_template(
            r#"{
                "id": "satellite-processing-workflow",
                "name": "Satellite Processing",
                "description": "Process satellite imagery",
                "version": "1.0.0",
                "tasks": [],
                "dependencies": [],
                "metadata": {}
            }"#,
        );

        Ok(template)
    }

    /// Create change detection template.
    fn create_change_detection_template() -> Result<WorkflowTemplate> {
        let mut template = WorkflowTemplate::new(
            "change-detection",
            "Change Detection Workflow",
            "Detect changes between two time periods using multi-temporal imagery",
        );

        template.set_category(TemplateCategory::ChangeDetection);
        template.metadata.complexity = 4;
        template.add_tag("change-detection");
        template.add_tag("temporal");

        template.add_parameter(Parameter {
            name: "before_image".to_string(),
            param_type: ParameterType::FilePath,
            description: "Image from before period".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        template.add_parameter(Parameter {
            name: "after_image".to_string(),
            param_type: ParameterType::FilePath,
            description: "Image from after period".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        template.add_parameter(Parameter {
            name: "threshold".to_string(),
            param_type: ParameterType::Float,
            description: "Change detection threshold".to_string(),
            required: false,
            default_value: Some(ParameterValue::Float(0.5)),
            constraints: Some(ParameterConstraints {
                min: Some(0.0),
                max: Some(1.0),
                min_length: None,
                max_length: None,
                pattern: None,
            }),
        });

        template.set_template(
            r#"{
                "id": "change-detection-workflow",
                "name": "Change Detection",
                "description": "Detect changes between imagery",
                "version": "1.0.0",
                "tasks": [],
                "dependencies": [],
                "metadata": {}
            }"#,
        );

        Ok(template)
    }

    /// Create batch processing template.
    fn create_batch_processing_template() -> Result<WorkflowTemplate> {
        let mut template = WorkflowTemplate::new(
            "batch-processing",
            "Batch Processing Workflow",
            "Process multiple files in parallel",
        );

        template.set_category(TemplateCategory::BatchProcessing);
        template.metadata.complexity = 2;
        template.add_tag("batch");
        template.add_tag("parallel");

        template.add_parameter(Parameter {
            name: "input_directory".to_string(),
            param_type: ParameterType::FilePath,
            description: "Input directory containing files to process".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        template.add_parameter(Parameter {
            name: "output_directory".to_string(),
            param_type: ParameterType::FilePath,
            description: "Output directory for processed files".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        template.add_parameter(Parameter {
            name: "parallel_tasks".to_string(),
            param_type: ParameterType::Integer,
            description: "Number of parallel tasks".to_string(),
            required: false,
            default_value: Some(ParameterValue::Integer(4)),
            constraints: Some(ParameterConstraints {
                min: Some(1.0),
                max: Some(32.0),
                min_length: None,
                max_length: None,
                pattern: None,
            }),
        });

        template.set_template(
            r#"{
                "id": "batch-processing-workflow",
                "name": "Batch Processing",
                "description": "Process files in batch",
                "version": "1.0.0",
                "tasks": [],
                "dependencies": [],
                "metadata": {}
            }"#,
        );

        Ok(template)
    }

    /// Create terrain analysis template.
    fn create_terrain_analysis_template() -> Result<WorkflowTemplate> {
        let mut template = WorkflowTemplate::new(
            "terrain-analysis",
            "Terrain Analysis Workflow",
            "Analyze terrain from DEM data",
        );

        template.set_category(TemplateCategory::TerrainAnalysis);
        template.metadata.complexity = 3;
        template.add_tag("terrain");
        template.add_tag("dem");
        template.add_tag("analysis");

        template.add_parameter(Parameter {
            name: "dem_path".to_string(),
            param_type: ParameterType::FilePath,
            description: "Digital Elevation Model file path".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        template.add_parameter(Parameter {
            name: "analysis_type".to_string(),
            param_type: ParameterType::Enum {
                allowed_values: vec![
                    "slope".to_string(),
                    "aspect".to_string(),
                    "hillshade".to_string(),
                    "all".to_string(),
                ],
            },
            description: "Type of terrain analysis to perform".to_string(),
            required: false,
            default_value: Some(ParameterValue::String("all".to_string())),
            constraints: None,
        });

        template.set_template(
            r#"{
                "id": "terrain-analysis-workflow",
                "name": "Terrain Analysis",
                "description": "Analyze terrain from DEM",
                "version": "1.0.0",
                "tasks": [],
                "dependencies": [],
                "metadata": {}
            }"#,
        );

        Ok(template)
    }

    /// Export library to JSON.
    pub fn export_to_json(&self) -> Result<String> {
        let templates = self.list_templates();
        serde_json::to_string_pretty(&templates)
            .map_err(|e| WorkflowError::template(format!("Failed to export library: {}", e)))
    }

    /// Import library from JSON.
    pub fn import_from_json(&self, json: &str) -> Result<usize> {
        let templates: Vec<WorkflowTemplate> = serde_json::from_str(json)
            .map_err(|e| WorkflowError::template(format!("Failed to import library: {}", e)))?;

        let mut count = 0;
        for template in templates {
            if self.add_template(template).is_ok() {
                count += 1;
            }
        }

        Ok(count)
    }
}

impl Default for WorkflowTemplateLibrary {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_library_creation() {
        let library = WorkflowTemplateLibrary::new();
        let templates = library.list_templates();

        // Should have built-in templates
        assert!(!templates.is_empty());
    }

    #[test]
    fn test_add_get_template() {
        let library = WorkflowTemplateLibrary::new();

        let template = WorkflowTemplate::new("test", "Test", "Test template");

        assert!(library.add_template(template).is_ok());
        assert!(library.get_template("test").is_some());
    }

    #[test]
    fn test_remove_template() {
        let library = WorkflowTemplateLibrary::new();

        let template = WorkflowTemplate::new("test", "Test", "Test template");
        library.add_template(template).expect("Failed to add");

        assert!(library.remove_template("test").is_some());
        assert!(library.get_template("test").is_none());
    }

    #[test]
    fn test_list_by_category() {
        let library = WorkflowTemplateLibrary::new();

        let satellite_templates = library.list_by_category(&TemplateCategory::SatelliteProcessing);

        assert!(!satellite_templates.is_empty());
    }

    #[test]
    fn test_search_by_tag() {
        let library = WorkflowTemplateLibrary::new();

        let batch_templates = library.search_by_tag("batch");

        assert!(!batch_templates.is_empty());
    }

    #[test]
    fn test_duplicate_template() {
        let library = WorkflowTemplateLibrary::new();

        let template1 = WorkflowTemplate::new("dup", "Dup", "Duplicate");
        let template2 = WorkflowTemplate::new("dup", "Dup2", "Duplicate 2");

        assert!(library.add_template(template1).is_ok());
        assert!(library.add_template(template2).is_err());
    }

    #[test]
    fn test_export_import() {
        let library1 = WorkflowTemplateLibrary::new();

        let json = library1.export_to_json().expect("Failed to export");

        let library2 = WorkflowTemplateLibrary::default();
        library2.templates.clear(); // Clear built-ins for test

        let count = library2.import_from_json(&json).expect("Failed to import");

        assert!(count > 0);
    }
}