Comprehensive Loop Examples Workflow
Demonstrates both for-loop and while-loop functionality with script tasks only
Workflow Information
ID: comprehensive_loop_examples
Namespace: examples
Version: 1.0
Created: 2025-07-21
Updated: 2025-07-21
Tasks: 5
Quick Actions
Inputs
| Name | Type | Required | Default |
|---|---|---|---|
dataset_size |
integer | Optional |
25
|
batch_size |
integer | Optional |
5
|
processing_threshold |
integer | Optional |
80
|
max_while_iterations |
integer | Optional |
20
|
Outputs
| Name | Type | Source |
|---|---|---|
for_loop_summary |
object | Summary of for-loop processing results |
comparison_report |
object | Comprehensive comparison of both loop types |
while_loop_summary |
object | Summary of while-loop processing results |
total_items_processed |
integer | Total items processed by for-loop |
quality_threshold_achieved |
integer | Whether while-loop achieved quality threshold |
Tasks
initialize_datasets
scriptCreate test datasets for for-loop and while-loop examples
for_loop_processor
loopProcess data in fixed batches using for-loop pattern
Loop Configuration
Type: for
Max Iterations: ${initialize_datasets.for_loop_batches}
Max Iterations: ${initialize_datasets.for_loop_batches}
Iterator Variable: batch_index
State Variables: category_counts, total_processed, total_value_sum, current_batch_start
State Variables: category_counts, total_processed, total_value_sum, current_batch_start
Loop Flow (3 steps)
Extract For-Loop Batch
script
Process For-Loop Batch
script
Update For-Loop State
script
while_loop_processor
loopProcess items until quality threshold met using while-loop pattern
Loop Configuration
Type: while
Max Iterations: ${max_while_iterations}
Exit Condition:
Max Iterations: ${max_while_iterations}
Exit Condition:
${pending_items} <= 0 || ${average_score} >= ${processing_threshold}
Iterator Variable: iteration_count
State Variables: total_score, average_score, pending_items, processed_items, working_dataset, reprocessing_count
State Variables: total_score, average_score, pending_items, processed_items, working_dataset, reprocessing_count
Loop Flow (3 steps)
Select Items for While-Loop Processing
script
Improve While-Loop Items
script
Update While-Loop State
script
generate_comparison_report
scriptCompare results from for-loop and while-loop processing
store_loop_results
storageStore comprehensive results from both loop types
YAML Source
id: comprehensive_loop_examples
name: Comprehensive Loop Examples Workflow
retry:
retryOn:
- TEMPORARY_FAILURE
maxDelay: 10s
maxAttempts: 2
initialDelay: 1s
backoffMultiplier: 1.5
tasks:
- id: initialize_datasets
name: Initialize Test Datasets
type: script
script: "import json\nimport random\n\ndataset_size = int(\"${dataset_size}\")\n\
batch_size = int(\"${batch_size}\")\n\n# Generate dataset for for-loop (fixed\
\ size array)\nfor_loop_data = []\nfor i in range(dataset_size):\n item = {\n\
\ \"id\": i + 1,\n \"name\": f\"Item_{i+1:03d}\",\n \"value\"\
: random.randint(1, 100),\n \"category\": random.choice([\"Alpha\", \"\
Beta\", \"Gamma\", \"Delta\"]),\n \"priority\": random.choice([\"High\"\
, \"Medium\", \"Low\"])\n }\n for_loop_data.append(item)\n\n# Generate dataset\
\ for while-loop (variable processing needs)\nwhile_loop_data = []\nfor i in range(dataset_size):\n\
\ item = {\n \"id\": i + 1,\n \"name\": f\"Record_{i+1:03d}\"\
,\n \"score\": random.randint(10, 100),\n \"status\": \"pending\"\
,\n \"attempts\": 0,\n \"needs_reprocessing\": random.choice([True,\
\ False])\n }\n while_loop_data.append(item)\n\nresult = {\n \"for_loop_dataset\"\
: for_loop_data,\n \"while_loop_dataset\": while_loop_data,\n \"total_items\"\
: dataset_size,\n \"batch_size\": batch_size,\n \"for_loop_batches\": (dataset_size\
\ + batch_size - 1) // batch_size # Ceiling division\n}\n\nprint(f\"Generated\
\ {dataset_size} items for processing\")\nprint(f\"For-loop will process {result['for_loop_batches']}\
\ batches\")\nprint(f\"__OUTPUTS__ {json.dumps(result)}\")\n"
description: Create test datasets for for-loop and while-loop examples
timeout_seconds: 60
- id: for_loop_processor
name: For-Loop Batch Processor
type: loop
loop_type: for
depends_on:
- initialize_datasets
loop_tasks:
- id: extract_for_batch
name: Extract For-Loop Batch
type: script
script: "import json\n\ndataset = ${initialize_datasets.for_loop_dataset}\nbatch_size\
\ = ${initialize_datasets.batch_size}\nbatch_index = ${batch_index}\n\n# Calculate\
\ batch boundaries\nstart_idx = batch_index * batch_size\nend_idx = min(start_idx\
\ + batch_size, len(dataset))\ncurrent_batch = dataset[start_idx:end_idx]\n\n\
print(f\"For-Loop: Processing batch {batch_index + 1}/{${initialize_datasets.for_loop_batches}}\"\
)\nprint(f\"Batch range: items {start_idx + 1} to {end_idx}\")\n\nresult = {\n\
\ \"current_batch\": current_batch,\n \"batch_number\": batch_index +\
\ 1,\n \"batch_size\": len(current_batch),\n \"start_index\": start_idx,\n\
\ \"end_index\": end_idx\n}\n\nprint(f\"__OUTPUTS__ {json.dumps(result)}\"\
)\n"
description: Extract the current batch using for-loop index
- id: process_for_batch
name: Process For-Loop Batch
type: script
script: "import json\n\ncurrent_batch = ${extract_for_batch.current_batch}\nbatch_number\
\ = ${extract_for_batch.batch_number}\n\n# Process each item in the batch\n\
processed_items = []\nbatch_value_sum = 0\ncategory_counts = {}\n\nfor item\
\ in current_batch:\n # Simulate processing logic\n processed_item = {\n\
\ \"id\": item[\"id\"],\n \"name\": item[\"name\"],\n \"\
original_value\": item[\"value\"],\n \"processed_value\": item[\"value\"\
] * 1.1, # 10% boost\n \"category\": item[\"category\"],\n \"\
priority\": item[\"priority\"],\n \"processed_at_batch\": batch_number,\n\
\ \"quality_score\": min(100, item[\"value\"] + 10)\n }\n \n \
\ processed_items.append(processed_item)\n batch_value_sum += processed_item[\"\
processed_value\"]\n \n # Count categories\n category = item[\"category\"\
]\n category_counts[category] = category_counts.get(category, 0) + 1\n\n\
result = {\n \"processed_items\": processed_items,\n \"batch_stats\":\
\ {\n \"items_processed\": len(processed_items),\n \"total_value\"\
: batch_value_sum,\n \"average_value\": batch_value_sum / len(processed_items)\
\ if processed_items else 0,\n \"category_distribution\": category_counts\n\
\ }\n}\n\nprint(f\"Processed {len(processed_items)} items in batch {batch_number}\"\
)\nprint(f\"Batch total value: {batch_value_sum:.2f}\")\nprint(f\"__OUTPUTS__\
\ {json.dumps(result)}\")\n"
depends_on:
- extract_for_batch
description: Process items in the current for-loop batch
- id: update_for_state
name: Update For-Loop State
type: script
script: "import json\n\n# Get current state\ncurrent_total = ${total_processed}\n\
current_sum = ${total_value_sum}\ncurrent_categories = ${category_counts} if\
\ \"${category_counts}\" != \"\" else {}\n\n# Get batch results\nbatch_stats\
\ = ${process_for_batch.batch_stats}\nitems_processed = batch_stats[\"items_processed\"\
]\nbatch_value = batch_stats[\"total_value\"]\nbatch_categories = batch_stats[\"\
category_distribution\"]\n\n# Update totals\nnew_total = current_total + items_processed\n\
new_sum = current_sum + batch_value\n\n# Merge category counts\nif isinstance(current_categories,\
\ str):\n current_categories = {}\n\nupdated_categories = current_categories.copy()\n\
for category, count in batch_categories.items():\n updated_categories[category]\
\ = updated_categories.get(category, 0) + count\n\nresult = {\n \"total_processed\"\
: new_total,\n \"total_value_sum\": new_sum,\n \"category_counts\": updated_categories,\n\
\ \"average_value\": new_sum / new_total if new_total > 0 else 0,\n \"\
batches_completed\": ${batch_index} + 1\n}\n\nprint(f\"For-Loop Progress: {new_total}\
\ items processed across {${batch_index} + 1} batches\")\nprint(f\"Running average\
\ value: {result['average_value']:.2f}\")\nprint(f\"__OUTPUTS__ {json.dumps(result)}\"\
)\n"
depends_on:
- process_for_batch
description: Update cumulative statistics for for-loop
description: Process data in fixed batches using for-loop pattern
previous_node: initialize_datasets
max_iterations: ${initialize_datasets.for_loop_batches}
state_variables:
category_counts: {}
total_processed: 0
total_value_sum: 0
current_batch_start: 0
iteration_variable: batch_index
- id: while_loop_processor
name: While-Loop Quality Processor
type: loop
loop_type: while
depends_on:
- for_loop_processor
loop_tasks:
- id: select_while_items
name: Select Items for While-Loop Processing
type: script
script: "import json\n\nworking_data = ${working_dataset}\niteration = ${iteration_count}\n\
threshold = int(\"${processing_threshold}\")\n\n# Find items that need processing\n\
items_to_process = []\nfor item in working_data:\n if (item[\"status\"] ==\
\ \"pending\" or \n (item[\"needs_reprocessing\"] and item[\"score\"\
] < threshold)):\n items_to_process.append(item)\n\n# Limit to batch\
\ size for this iteration\nbatch_size = min(int(\"${batch_size}\"), len(items_to_process))\n\
current_selection = items_to_process[:batch_size]\n\nprint(f\"While-Loop Iteration\
\ {iteration + 1}: Found {len(items_to_process)} items needing processing\"\
)\nprint(f\"Processing {len(current_selection)} items in this iteration\")\n\
\nresult = {\n \"selected_items\": current_selection,\n \"items_selected\"\
: len(current_selection),\n \"remaining_candidates\": len(items_to_process)\
\ - len(current_selection),\n \"iteration\": iteration + 1\n}\n\nprint(f\"\
__OUTPUTS__ {json.dumps(result)}\")\n"
description: Select items that need processing in this iteration
- id: improve_while_items
name: Improve While-Loop Items
type: script
script: "import json\nimport random\n\nselected_items = ${select_while_items.selected_items}\n\
threshold = int(\"${processing_threshold}\")\niteration = ${select_while_items.iteration}\n\
\nimproved_items = []\nprocessing_stats = {\n \"items_improved\": 0,\n \
\ \"items_completed\": 0,\n \"score_increase\": 0,\n \"reprocessing_needed\"\
: 0\n}\n\nfor item in selected_items:\n # Simulate quality improvement process\n\
\ old_score = item[\"score\"]\n \n # Apply improvement (random boost\
\ between 5-25 points)\n improvement = random.randint(5, 25)\n new_score\
\ = min(100, old_score + improvement)\n \n # Determine if item meets threshold\n\
\ if new_score >= threshold:\n status = \"completed\"\n needs_reprocessing\
\ = False\n processing_stats[\"items_completed\"] += 1\n else:\n \
\ status = \"pending\"\n needs_reprocessing = random.choice([True,\
\ False]) # 50% chance needs more work\n if needs_reprocessing:\n \
\ processing_stats[\"reprocessing_needed\"] += 1\n \n improved_item\
\ = {\n \"id\": item[\"id\"],\n \"name\": item[\"name\"],\n \
\ \"score\": new_score,\n \"status\": status,\n \"attempts\"\
: item[\"attempts\"] + 1,\n \"needs_reprocessing\": needs_reprocessing,\n\
\ \"improvement_applied\": improvement,\n \"processed_in_iteration\"\
: iteration\n }\n \n improved_items.append(improved_item)\n processing_stats[\"\
items_improved\"] += 1\n processing_stats[\"score_increase\"] += improvement\n\
\navg_improvement = processing_stats[\"score_increase\"] / len(improved_items)\
\ if improved_items else 0\n\nresult = {\n \"improved_items\": improved_items,\n\
\ \"processing_stats\": processing_stats,\n \"average_improvement\": avg_improvement\n\
}\n\nprint(f\"Improved {len(improved_items)} items with average score increase\
\ of {avg_improvement:.1f}\")\nprint(f\"Completed: {processing_stats['items_completed']},\
\ Still need reprocessing: {processing_stats['reprocessing_needed']}\")\nprint(f\"\
__OUTPUTS__ {json.dumps(result)}\")\n"
depends_on:
- select_while_items
description: Apply quality improvements to selected items
- id: update_while_state
name: Update While-Loop State
type: script
script: "import json\n\n# Get current state\ncurrent_dataset = ${working_dataset}\n\
improved_items = ${improve_while_items.improved_items}\ncurrent_processed =\
\ ${processed_items}\ncurrent_total_score = ${total_score}\ncurrent_reprocessing\
\ = ${reprocessing_count}\n\n# Update the working dataset with improved items\n\
updated_dataset = []\nimproved_ids = {item[\"id\"]: item for item in improved_items}\n\
\nfor item in current_dataset:\n if item[\"id\"] in improved_ids:\n \
\ updated_dataset.append(improved_ids[item[\"id\"]])\n else:\n updated_dataset.append(item)\n\
\n# Calculate new statistics\npending_count = sum(1 for item in updated_dataset\
\ if item[\"status\"] == \"pending\")\ncompleted_count = sum(1 for item in updated_dataset\
\ if item[\"status\"] == \"completed\")\ntotal_score = sum(item[\"score\"] for\
\ item in updated_dataset)\navg_score = total_score / len(updated_dataset) if\
\ updated_dataset else 0\n\nreprocessing_needed = sum(1 for item in updated_dataset\
\ \n if item[\"needs_reprocessing\"] and item[\"status\"\
] == \"pending\")\n\n# Check exit conditions\nthreshold = int(\"${processing_threshold}\"\
)\nshould_continue = pending_count > 0 and avg_score < threshold\n\nresult =\
\ {\n \"working_dataset\": updated_dataset,\n \"pending_items\": pending_count,\n\
\ \"processed_items\": completed_count,\n \"total_score\": total_score,\n\
\ \"average_score\": avg_score,\n \"reprocessing_count\": reprocessing_needed,\n\
\ \"should_continue\": should_continue,\n \"completion_percentage\": (completed_count\
\ / len(updated_dataset)) * 100 if updated_dataset else 0\n}\n\nprint(f\"While-Loop\
\ State Update:\")\nprint(f\" Pending: {pending_count}, Completed: {completed_count}\"\
)\nprint(f\" Average Score: {avg_score:.1f} (Threshold: {threshold})\")\nprint(f\"\
\ Completion: {result['completion_percentage']:.1f}%\")\nprint(f\" Should\
\ Continue: {should_continue}\")\nprint(f\"__OUTPUTS__ {json.dumps(result)}\"\
)\n"
depends_on:
- improve_while_items
description: Update state variables and working dataset for while-loop
description: Process items until quality threshold met using while-loop pattern
previous_node: for_loop_processor
exit_condition: ${pending_items} <= 0 || ${average_score} >= ${processing_threshold}
max_iterations: ${max_while_iterations}
state_variables:
total_score: 0
average_score: 0
pending_items: ${initialize_datasets.total_items}
processed_items: 0
working_dataset: ${initialize_datasets.while_loop_dataset}
reprocessing_count: 0
iteration_variable: iteration_count
- id: generate_comparison_report
name: Generate Loop Comparison Report
type: script
script: "import json\n\n# Get for-loop results\nfor_total = ${for_loop_processor.total_processed}\n\
for_value_sum = ${for_loop_processor.total_value_sum}\nfor_avg_value = ${for_loop_processor.average_value}\n\
for_categories = ${for_loop_processor.category_counts}\nfor_batches = ${for_loop_processor.batches_completed}\n\
\n# Get while-loop results\nwhile_pending = ${while_loop_processor.pending_items}\n\
while_completed = ${while_loop_processor.processed_items}\nwhile_avg_score = ${while_loop_processor.average_score}\n\
while_completion_pct = ${while_loop_processor.completion_percentage}\nwhile_reprocessing\
\ = ${while_loop_processor.reprocessing_count}\n\n# Calculate comparison metrics\n\
total_items = int(\"${dataset_size}\")\nthreshold = int(\"${processing_threshold}\"\
)\n\nreport = {\n \"execution_summary\": {\n \"total_items_generated\"\
: total_items,\n \"batch_size_used\": int(\"${batch_size}\"),\n \
\ \"quality_threshold\": threshold\n },\n \"for_loop_results\": {\n \
\ \"type\": \"Fixed iteration processing\",\n \"total_processed\": for_total,\n\
\ \"batches_completed\": for_batches,\n \"total_value_generated\"\
: for_value_sum,\n \"average_value\": for_avg_value,\n \"category_distribution\"\
: for_categories,\n \"processing_efficiency\": \"100% - All items processed\
\ in predetermined batches\"\n },\n \"while_loop_results\": {\n \"\
type\": \"Conditional processing until threshold met\",\n \"items_completed\"\
: while_completed,\n \"items_still_pending\": while_pending,\n \"\
average_quality_score\": while_avg_score,\n \"completion_percentage\":\
\ while_completion_pct,\n \"items_needing_reprocessing\": while_reprocessing,\n\
\ \"threshold_achieved\": while_avg_score >= threshold\n },\n \"\
comparison\": {\n \"for_loop_use_case\": \"Best for processing fixed datasets\
\ with known size\",\n \"while_loop_use_case\": \"Best for iterative improvement\
\ until quality criteria met\",\n \"for_loop_predictability\": \"High -\
\ Fixed number of iterations\",\n \"while_loop_adaptability\": \"High -\
\ Continues until conditions satisfied\",\n \"recommendation\": \"Use for-loop\
\ for batch processing, while-loop for quality-driven iterative processing\"\n\
\ },\n \"performance_metrics\": {\n \"for_loop_iterations\": for_batches,\n\
\ \"while_loop_iterations\": \"${while_loop_processor.iteration_count}\"\
,\n \"for_loop_completion_rate\": \"100%\",\n \"while_loop_completion_rate\"\
: f\"{while_completion_pct:.1f}%\"\n }\n}\n\nprint(\"=== LOOP COMPARISON REPORT\
\ ===\")\nprint(f\"For-Loop: Processed {for_total} items in {for_batches} fixed\
\ batches\")\nprint(f\"While-Loop: Achieved {while_completion_pct:.1f}% completion\
\ with {while_avg_score:.1f} average score\")\nprint(f\"Threshold target: {threshold}\
\ (Achieved: {while_avg_score >= threshold})\")\nprint()\nprint(\"FOR-LOOP SUMMARY:\"\
)\nprint(f\" - Processed all {for_total} items systematically\")\nprint(f\" \
\ - Generated total value of {for_value_sum:.2f}\")\nprint(f\" - Average value\
\ per item: {for_avg_value:.2f}\")\nprint()\nprint(\"WHILE-LOOP SUMMARY:\")\n\
print(f\" - Completed {while_completed} items to quality standard\")\nprint(f\"\
\ - {while_pending} items still pending improvement\")\nprint(f\" - Average\
\ quality score: {while_avg_score:.1f}/{threshold}\")\nprint(f\" - {while_reprocessing}\
\ items need additional reprocessing\")\n\nprint(f\"__OUTPUTS__ {json.dumps(report)}\"\
)\n"
depends_on:
- while_loop_processor
- for_loop_processor
description: Compare results from for-loop and while-loop processing
previous_node: while_loop_processor
- id: store_loop_results
name: Store Loop Processing Results
type: storage
operation: write
depends_on:
- generate_comparison_report
- for_loop_processor
description: Store comprehensive results from both loop types
storage_data: "{\n \"execution_metadata\": {\n \"execution_id\": \"${EXECUTION_ID}\"\
,\n \"workflow_id\": \"${WORKFLOW_ID}\",\n \"timestamp\": \"${_current_timestamp}\"\
,\n \"input_parameters\": {\n \"dataset_size\": \"${dataset_size}\",\n\
\ \"batch_size\": \"${batch_size}\",\n \"processing_threshold\": \"\
${processing_threshold}\",\n \"max_while_iterations\": \"${max_while_iterations}\"\
\n }\n },\n \"for_loop_final_state\": {\n \"total_processed\": \"${for_loop_processor.total_processed}\"\
,\n \"total_value_sum\": \"${for_loop_processor.total_value_sum}\",\n \"\
average_value\": \"${for_loop_processor.average_value}\",\n \"category_counts\"\
: \"${for_loop_processor.category_counts}\",\n \"batches_completed\": \"${for_loop_processor.batches_completed}\"\
\n },\n \"while_loop_final_state\": {\n \"pending_items\": \"${while_loop_processor.pending_items}\"\
,\n \"processed_items\": \"${while_loop_processor.processed_items}\",\n \
\ \"average_score\": \"${while_loop_processor.average_score}\",\n \"completion_percentage\"\
: \"${while_loop_processor.completion_percentage}\",\n \"reprocessing_count\"\
: \"${while_loop_processor.reprocessing_count}\",\n \"iterations_completed\"\
: \"${while_loop_processor.iteration_count}\"\n },\n \"comparison_report\":\
\ \"${generate_comparison_report}\"\n}\n"
storage_path: /results/${EXECUTION_ID}_loop_comparison.json
previous_node: generate_comparison_report
inputs:
- name: dataset_size
type: integer
default: 25
required: false
description: Size of the dataset to generate and process
- name: batch_size
type: integer
default: 5
required: false
description: Number of items to process per batch
- name: processing_threshold
type: integer
default: 80
required: false
description: Quality threshold for processing (0-100)
- name: max_while_iterations
type: integer
default: 20
required: false
description: Maximum iterations for while loop
outputs:
for_loop_summary:
type: object
source: for_loop_processor
description: Summary of for-loop processing results
comparison_report:
type: object
source: generate_comparison_report
description: Comprehensive comparison of both loop types
while_loop_summary:
type: object
source: while_loop_processor
description: Summary of while-loop processing results
total_items_processed:
type: integer
source: for_loop_processor.total_processed
description: Total items processed by for-loop
quality_threshold_achieved:
type: integer
source: while_loop_processor.average_score
description: Whether while-loop achieved quality threshold
version: '1.0'
namespace: examples
description: Demonstrates both for-loop and while-loop functionality with script tasks
only
timeout_seconds: 2400