-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefinition_validator.go
More file actions
359 lines (299 loc) · 9.86 KB
/
definition_validator.go
File metadata and controls
359 lines (299 loc) · 9.86 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
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
package statepro
import (
"encoding/json"
"fmt"
"strings"
"github.com/rendis/statepro/v3/theoretical"
)
// ValidateQuantumMachineDefinition validates both schema and semantic integrity.
// It is stricter than ValidateQuantumMachineBySchema and ensures references resolve.
func ValidateQuantumMachineDefinition(source *theoretical.QuantumMachineModel) error {
if source == nil {
return fmt.Errorf("source model cannot be nil")
}
if err := ValidateQuantumMachineBySchema(source); err != nil {
return err
}
if err := validateQuantumMachineSemantics(source); err != nil {
return fmt.Errorf("semantic validation failed: %w", err)
}
return nil
}
// ValidateQuantumMachineDefinitionFromMap validates both schema and semantic integrity from map payload.
func ValidateQuantumMachineDefinitionFromMap(source map[string]any) error {
if source == nil {
return fmt.Errorf("source map cannot be nil")
}
if err := ValidateQuantumMachineBySchemaFromMap(source); err != nil {
return err
}
model, err := DeserializeQuantumMachineFromMap(source)
if err != nil {
return fmt.Errorf("error deserializing model for semantic validation: %w", err)
}
if err = validateQuantumMachineSemantics(model); err != nil {
return fmt.Errorf("semantic validation failed: %w", err)
}
return nil
}
// ValidateQuantumMachineDefinitionFromBinary validates both schema and semantic integrity from JSON payload.
func ValidateQuantumMachineDefinitionFromBinary(source []byte) error {
if len(source) == 0 {
return fmt.Errorf("source payload cannot be empty")
}
if err := ValidateQuantumMachineBySchemaFromBinary(source); err != nil {
return err
}
model, err := DeserializeQuantumMachineFromBinary(source)
if err != nil {
return fmt.Errorf("error deserializing model for semantic validation: %w", err)
}
if err = validateQuantumMachineSemantics(model); err != nil {
return fmt.Errorf("semantic validation failed: %w", err)
}
return nil
}
type semanticValidationErrors struct {
errors []string
}
func (v *semanticValidationErrors) add(format string, args ...any) {
v.errors = append(v.errors, fmt.Sprintf(format, args...))
}
func (v *semanticValidationErrors) hasErrors() bool {
return len(v.errors) > 0
}
func (v *semanticValidationErrors) Error() string {
if len(v.errors) == 0 {
return ""
}
return strings.Join(v.errors, "; ")
}
func validateQuantumMachineSemantics(model *theoretical.QuantumMachineModel) error {
if model == nil {
return fmt.Errorf("model cannot be nil")
}
errCollector := &semanticValidationErrors{}
if len(model.Universes) == 0 {
errCollector.add("machine must define at least one universe")
}
for universeKey, universe := range model.Universes {
if universe == nil {
errCollector.add("universe '%s' cannot be nil", universeKey)
continue
}
if universe.ID != universeKey {
errCollector.add("universe key '%s' must match universe.id '%s'", universeKey, universe.ID)
}
if len(universe.Realities) == 0 {
errCollector.add("universe '%s' must define at least one reality", universeKey)
continue
}
for realityKey, reality := range universe.Realities {
if reality == nil {
errCollector.add("reality '%s' in universe '%s' cannot be nil", realityKey, universeKey)
continue
}
if reality.ID != realityKey {
errCollector.add("reality key '%s' in universe '%s' must match reality.id '%s'", realityKey, universeKey, reality.ID)
}
if reality.Type == theoretical.RealityTypeTransition && !hasEffectiveTransitionFlow(reality) {
errCollector.add("transition reality '%s' in universe '%s' must define non-empty 'on' or non-empty 'always'", realityKey, universeKey)
}
}
if universe.Initial != nil && *universe.Initial != "" {
if _, ok := universe.Realities[*universe.Initial]; !ok {
errCollector.add("universe '%s' initial '%s' does not reference an existing reality", universeKey, *universe.Initial)
}
}
}
for idx, initial := range model.Initials {
kind, universeID, realityID, ok := parseStateReference(initial)
if !ok {
errCollector.add("initials[%d] has invalid reference '%s'", idx, initial)
continue
}
if kind == referenceReality {
errCollector.add("initials[%d] must be external reference (U:<universe> or U:<universe>:<reality>), got '%s'", idx, initial)
continue
}
u, exists := model.Universes[universeID]
if !exists || u == nil {
errCollector.add("initials[%d] references unknown universe '%s'", idx, universeID)
continue
}
if kind == referenceUniverseReality {
if _, exists = u.Realities[realityID]; !exists {
errCollector.add("initials[%d] references unknown reality '%s' in universe '%s'", idx, realityID, universeID)
}
}
}
for universeID, universe := range model.Universes {
if universe == nil {
continue
}
for realityID, reality := range universe.Realities {
if reality == nil {
continue
}
for tIdx, transition := range reality.Always {
validateTransitionSemantics(errCollector, model, universeID, realityID, "always", tIdx, transition)
}
for eventName, transitions := range reality.On {
for tIdx, transition := range transitions {
validateTransitionSemantics(errCollector, model, universeID, realityID, "on."+eventName, tIdx, transition)
}
}
}
}
if errCollector.hasErrors() {
return errCollector
}
return nil
}
func hasEffectiveTransitionFlow(reality *theoretical.RealityModel) bool {
if reality == nil {
return false
}
if len(reality.Always) > 0 {
return true
}
if len(reality.On) == 0 {
return false
}
for _, transitions := range reality.On {
if len(transitions) > 0 {
return true
}
}
return false
}
func validateTransitionSemantics(
errCollector *semanticValidationErrors,
model *theoretical.QuantumMachineModel,
universeID string,
realityID string,
transitionPath string,
transitionIndex int,
transition *theoretical.TransitionModel,
) {
if transition == nil {
errCollector.add("universe '%s' reality '%s' transition '%s[%d]' cannot be null", universeID, realityID, transitionPath, transitionIndex)
return
}
if len(transition.Targets) == 0 {
errCollector.add("universe '%s' reality '%s' transition '%s[%d]' must define at least one target", universeID, realityID, transitionPath, transitionIndex)
return
}
isNotify := transition.Type != nil && *transition.Type == theoretical.TransitionTypeNotify
for targetIndex, target := range transition.Targets {
kind, targetUniverseID, targetRealityID, ok := parseStateReference(target)
if !ok {
errCollector.add(
"universe '%s' reality '%s' transition '%s[%d]' target[%d] has invalid reference '%s'",
universeID, realityID, transitionPath, transitionIndex, targetIndex, target,
)
continue
}
if isNotify && kind == referenceReality {
errCollector.add(
"universe '%s' reality '%s' transition '%s[%d]' with type 'notify' cannot target internal reality '%s'",
universeID, realityID, transitionPath, transitionIndex, target,
)
}
switch kind {
case referenceReality:
u := model.Universes[universeID]
if u == nil {
errCollector.add("unknown source universe '%s' while validating target '%s'", universeID, target)
continue
}
if _, exists := u.Realities[targetRealityID]; !exists {
errCollector.add(
"universe '%s' reality '%s' transition '%s[%d]' target[%d] references unknown internal reality '%s'",
universeID, realityID, transitionPath, transitionIndex, targetIndex, targetRealityID,
)
}
case referenceUniverse:
if tu := model.Universes[targetUniverseID]; tu == nil {
errCollector.add(
"universe '%s' reality '%s' transition '%s[%d]' target[%d] references unknown universe '%s'",
universeID, realityID, transitionPath, transitionIndex, targetIndex, targetUniverseID,
)
}
case referenceUniverseReality:
tu := model.Universes[targetUniverseID]
if tu == nil {
errCollector.add(
"universe '%s' reality '%s' transition '%s[%d]' target[%d] references unknown universe '%s'",
universeID, realityID, transitionPath, transitionIndex, targetIndex, targetUniverseID,
)
continue
}
if _, exists := tu.Realities[targetRealityID]; !exists {
errCollector.add(
"universe '%s' reality '%s' transition '%s[%d]' target[%d] references unknown reality '%s' in universe '%s'",
universeID, realityID, transitionPath, transitionIndex, targetIndex, targetRealityID, targetUniverseID,
)
}
}
}
}
type stateReferenceType int
const (
referenceUniverse stateReferenceType = iota
referenceUniverseReality
referenceReality
)
func parseStateReference(ref string) (stateReferenceType, string, string, bool) {
if ref == "" {
return 0, "", "", false
}
if strings.HasPrefix(ref, "U:") {
parts := strings.Split(ref, ":")
if len(parts) == 2 {
if !isValidIdentifier(parts[1]) {
return 0, "", "", false
}
return referenceUniverse, parts[1], "", true
}
if len(parts) == 3 {
if !isValidIdentifier(parts[1]) || !isValidIdentifier(parts[2]) {
return 0, "", "", false
}
return referenceUniverseReality, parts[1], parts[2], true
}
return 0, "", "", false
}
if !isValidIdentifier(ref) {
return 0, "", "", false
}
return referenceReality, "", ref, true
}
func isValidIdentifier(v string) bool {
if v == "" {
return false
}
if v[0] < 'A' || (v[0] > 'Z' && v[0] < 'a') || v[0] > 'z' {
return false
}
for i := 1; i < len(v); i++ {
ch := v[i]
isLetter := (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
isDigit := ch >= '0' && ch <= '9'
if !isLetter && !isDigit && ch != '_' && ch != '-' {
return false
}
}
last := v[len(v)-1]
isLetter := (last >= 'A' && last <= 'Z') || (last >= 'a' && last <= 'z')
isDigit := last >= '0' && last <= '9'
return isLetter || isDigit
}
// Unmarshal helper kept for future callers that validate schema-document payloads directly.
func unmarshalAny(source []byte) (any, error) {
var payload any
if err := json.Unmarshal(source, &payload); err != nil {
return nil, err
}
return payload, nil
}