-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
153 lines (123 loc) · 5.9 KB
/
example.php
File metadata and controls
153 lines (123 loc) · 5.9 KB
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
<?php
/**
* NORP PHP Reference Implementation - Usage Example
*
* Demonstrates NORP-001, 004, 005, 007 compliance
*
* @license MIT
* @copyright 2026 NeuraScope CONVERWAY
*/
require_once __DIR__ . '/BlueprintValidator.php';
require_once __DIR__ . '/BlueprintCompiler.php';
require_once __DIR__ . '/DTOs/ValidationResult.php';
require_once __DIR__ . '/DTOs/ExecutionPlan.php';
use NORP\PHP\BlueprintValidator;
use NORP\PHP\BlueprintCompiler;
// Example workflow (diamond pattern)
$workflow = [
'name' => 'Content Processing Workflow',
'nodes' => [
[
'id' => 'extract',
'type' => 'datasource',
'depends_on' => []
],
[
'id' => 'summarize',
'type' => 'llm_call',
'config' => [
'model' => 'gpt-4-turbo',
'prompt' => 'Summarize this text...',
'max_tokens' => 500
],
'depends_on' => ['extract']
],
[
'id' => 'classify',
'type' => 'llm_call',
'config' => [
'model' => 'claude-3-haiku',
'prompt' => 'Classify sentiment...',
'max_tokens' => 200
],
'depends_on' => ['extract']
],
[
'id' => 'publish',
'type' => 'output',
'depends_on' => ['summarize', 'classify']
]
]
];
// ═══════════════════════════════════════════════════════════
// NORP-001: Pre-Execution Validation Pipeline
// ═══════════════════════════════════════════════════════════
echo "═══ NORP-001 + NORP-004 + NORP-007: Validation ═══\n\n";
$validator = new BlueprintValidator();
$validationResult = $validator->validate($workflow);
echo "Valid: " . ($validationResult->valid ? 'YES' : 'NO') . "\n";
echo "Errors: " . count($validationResult->errors) . "\n";
echo "Warnings: " . count($validationResult->warnings) . "\n";
echo "Estimated Cost: \$" . number_format($validationResult->estimated_cost, 4) . "\n";
echo "Summary: " . $validationResult->getSummary() . "\n\n";
if (!$validationResult->valid) {
echo "Validation failed:\n";
foreach ($validationResult->errors as $error) {
echo " - $error\n";
}
exit(1);
}
// ═══════════════════════════════════════════════════════════
// NORP-007: Budget Enforcement
// ═══════════════════════════════════════════════════════════
echo "═══ NORP-007: Budget Enforcement ═══\n\n";
$budget = 10.00; // $10 budget
if ($validationResult->estimated_cost > $budget) {
echo "❌ BUDGET EXCEEDED\n";
echo "Estimated: \$" . $validationResult->estimated_cost . "\n";
echo "Budget: \$" . $budget . "\n";
exit(1);
}
echo "✅ Budget OK (${$validationResult->estimated_cost} < \${$budget})\n\n";
// ═══════════════════════════════════════════════════════════
// NORP-005: Deterministic Topological Ordering
// ═══════════════════════════════════════════════════════════
echo "═══ NORP-005: Compilation ═══\n\n";
$compiler = new BlueprintCompiler();
$executionPlan = $compiler->compile($workflow);
echo "Execution Order: " . implode(' → ', $executionPlan->execution_order) . "\n";
echo "Total Levels: " . $executionPlan->getLevelsCount() . "\n";
echo "Estimated Duration: " . $executionPlan->estimated_duration_ms . "ms\n\n";
echo "Parallel Groups:\n";
foreach ($executionPlan->parallel_groups as $group) {
$parallelFlag = $group['parallel'] ? '(parallel)' : '';
echo " Level {$group['level']}: " . implode(', ', $group['nodes']) . " $parallelFlag\n";
}
echo "\n";
// ═══════════════════════════════════════════════════════════
// NORP-003: Immutability Test
// ═══════════════════════════════════════════════════════════
echo "═══ NORP-003: Immutability Test ═══\n\n";
try {
// Attempt to mutate (should fail with PHP 8.2 readonly)
// $validationResult->valid = false;
// This would throw: Error: Cannot modify readonly property
echo "✅ DTOs are immutable (readonly properties)\n";
} catch (\Error $e) {
echo "✅ Mutation blocked: " . $e->getMessage() . "\n";
}
echo "\n";
// ═══════════════════════════════════════════════════════════
// NORP-005: Determinism Test
// ═══════════════════════════════════════════════════════════
echo "═══ NORP-005: Determinism Test ═══\n\n";
$plan1 = $compiler->compile($workflow);
$plan2 = $compiler->compile($workflow);
if ($plan1->execution_order === $plan2->execution_order) {
echo "✅ Deterministic: Same workflow → Same order\n";
echo " Order 1: " . implode(', ', $plan1->execution_order) . "\n";
echo " Order 2: " . implode(', ', $plan2->execution_order) . "\n";
} else {
echo "❌ Non-deterministic: Orders differ\n";
}
echo "\n═══ NORP Compliance Verified ═══\n";