-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization.go
More file actions
367 lines (302 loc) · 10.7 KB
/
serialization.go
File metadata and controls
367 lines (302 loc) · 10.7 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
360
361
362
363
364
365
366
367
package vsm
import (
"encoding/json"
"errors"
"fmt"
"os"
)
// Serializer provides JSON serialization/deserialization for VSM objects
type Serializer struct {
validator *Validator
}
// NewSerializer creates a new VSM serializer
func NewSerializer() *Serializer {
return &Serializer{
validator: NewValidator(),
}
}
// SerializeValueStream serializes a value stream to JSON
func (s *Serializer) SerializeValueStream(vs *ValueStream) ([]byte, error) {
if vs == nil {
return nil, errors.New("value stream cannot be nil")
}
// Validate before serialization
if err := s.validator.ValidateValueStream(vs); err != nil {
return nil, fmt.Errorf("validation failed: %w", err)
}
return json.MarshalIndent(vs, "", " ")
}
// DeserializeValueStream deserializes a value stream from JSON
func (s *Serializer) DeserializeValueStream(data []byte) (*ValueStream, error) {
var vs ValueStream
if err := json.Unmarshal(data, &vs); err != nil {
return nil, fmt.Errorf("failed to unmarshal value stream: %w", err)
}
// Validate after deserialization
if err := s.validator.ValidateValueStream(&vs); err != nil {
return nil, fmt.Errorf("validation failed: %w", err)
}
return &vs, nil
}
// SerializeVSMProject serializes a VSM project to JSON
func (s *Serializer) SerializeVSMProject(project *VSMProject) ([]byte, error) {
if project == nil {
return nil, errors.New("VSM project cannot be nil")
}
// Validate before serialization
if err := s.validator.ValidateVSMProject(project); err != nil {
return nil, fmt.Errorf("validation failed: %w", err)
}
return json.MarshalIndent(project, "", " ")
}
// DeserializeVSMProject deserializes a VSM project from JSON
func (s *Serializer) DeserializeVSMProject(data []byte) (*VSMProject, error) {
var project VSMProject
if err := json.Unmarshal(data, &project); err != nil {
return nil, fmt.Errorf("failed to unmarshal VSM project: %w", err)
}
// Validate after deserialization
if err := s.validator.ValidateVSMProject(&project); err != nil {
return nil, fmt.Errorf("validation failed: %w", err)
}
return &project, nil
}
// SerializeAnalysisResult serializes analysis results to JSON
func (s *Serializer) SerializeAnalysisResult(result *AnalysisResult) ([]byte, error) {
if result == nil {
return nil, errors.New("analysis result cannot be nil")
}
return json.MarshalIndent(result, "", " ")
}
// DeserializeAnalysisResult deserializes analysis results from JSON
func (s *Serializer) DeserializeAnalysisResult(data []byte) (*AnalysisResult, error) {
var result AnalysisResult
if err := json.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal analysis result: %w", err)
}
return &result, nil
}
// SerializeDesignResult serializes design results to JSON
func (s *Serializer) SerializeDesignResult(result *DesignResult) ([]byte, error) {
if result == nil {
return nil, errors.New("design result cannot be nil")
}
return json.MarshalIndent(result, "", " ")
}
// DeserializeDesignResult deserializes design results from JSON
func (s *Serializer) DeserializeDesignResult(data []byte) (*DesignResult, error) {
var result DesignResult
if err := json.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal design result: %w", err)
}
return &result, nil
}
// SerializePlanningResult serializes planning results to JSON
func (s *Serializer) SerializePlanningResult(result *PlanningResult) ([]byte, error) {
if result == nil {
return nil, errors.New("planning result cannot be nil")
}
return json.MarshalIndent(result, "", " ")
}
// DeserializePlanningResult deserializes planning results from JSON
func (s *Serializer) DeserializePlanningResult(data []byte) (*PlanningResult, error) {
var result PlanningResult
if err := json.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal planning result: %w", err)
}
return &result, nil
}
// SerializeAssessmentResult serializes assessment results to JSON
func (s *Serializer) SerializeAssessmentResult(result *AssessmentResult) ([]byte, error) {
if result == nil {
return nil, errors.New("assessment result cannot be nil")
}
return json.MarshalIndent(result, "", " ")
}
// DeserializeAssessmentResult deserializes assessment results from JSON
func (s *Serializer) DeserializeAssessmentResult(data []byte) (*AssessmentResult, error) {
var result AssessmentResult
if err := json.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal assessment result: %w", err)
}
return &result, nil
}
// SaveToFile saves any serializable object to a JSON file
func (s *Serializer) SaveToFile(data interface{}, filename string) error {
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal data: %w", err)
}
if err := os.WriteFile(filename, jsonData, 0644); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
// LoadFromFile loads any serializable object from a JSON file
func (s *Serializer) LoadFromFile(filename string, target interface{}) error {
data, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}
if err := json.Unmarshal(data, target); err != nil {
return fmt.Errorf("failed to unmarshal data: %w", err)
}
return nil
}
// ExportValueStreamMap exports a value stream as a text-based map representation
func (s *Serializer) ExportValueStreamMap(vs *ValueStream) (string, error) {
if vs == nil {
return "", errors.New("value stream cannot be nil")
}
mapStr := fmt.Sprintf("Value Stream Map: %s\n", vs.Name)
mapStr += fmt.Sprintf("Product Family: %s\n", vs.ProductFamily)
mapStr += fmt.Sprintf("State: %s\n", vs.State)
mapStr += "═" + "═══════════════════════════════════════════════\n\n"
// Customer
if vs.Customer != nil {
mapStr += fmt.Sprintf("Customer: %s\n", vs.Customer.Name)
mapStr += fmt.Sprintf("Demand: %.2f units/period\n", vs.Customer.Demand)
mapStr += fmt.Sprintf("Takt Time: %.2f time units\n\n", vs.Customer.TaktTime)
}
// Supplier
if vs.Supplier != nil {
mapStr += fmt.Sprintf("Supplier: %s\n", vs.Supplier.Name)
mapStr += fmt.Sprintf("Delivery Time: %.2f time units\n\n", vs.Supplier.DeliveryTime)
}
// Processes
mapStr += "Processes:\n"
for i, process := range vs.Processes {
mapStr += fmt.Sprintf(" %d. %s (%s)\n", i+1, process.Name, process.Type)
mapStr += fmt.Sprintf(" Process Time: %.2f %s\n", process.Parameters.ProcessTime, process.Parameters.TimeUnit)
mapStr += fmt.Sprintf(" Cycle Time: %.2f %s\n", process.Parameters.CycleTime, process.Parameters.TimeUnit)
mapStr += fmt.Sprintf(" Yield Factor: %.2f%%\n", process.Parameters.YieldFactor*100)
if process.Parameters.ScrapRate > 0 {
mapStr += fmt.Sprintf(" Scrap Rate: %.2f%%\n", process.Parameters.ScrapRate*100)
}
if process.Parameters.ReworkRate > 0 {
mapStr += fmt.Sprintf(" Rework Rate: %.2f%%\n", process.Parameters.ReworkRate*100)
}
mapStr += "\n"
}
// Inventory
if len(vs.Inventory) > 0 {
mapStr += "Inventory:\n"
for _, inv := range vs.Inventory {
mapStr += fmt.Sprintf(" %s: %.2f units (%.2f time units range)\n", inv.Name, inv.Quantity, inv.Range)
}
mapStr += "\n"
}
// Flow Elements
if len(vs.FlowElements) > 0 {
mapStr += "Flow Control Elements:\n"
for _, element := range vs.FlowElements {
mapStr += fmt.Sprintf(" %s (%s): Capacity %.2f\n", element.ID, element.Type, element.Capacity)
}
mapStr += "\n"
}
// KPIs
if len(vs.KPIs) > 0 {
mapStr += "Key Performance Indicators:\n"
for key, value := range vs.KPIs {
mapStr += fmt.Sprintf(" %s: %.4f\n", key, value)
}
mapStr += "\n"
}
return mapStr, nil
}
// ImportFromCSV imports basic value stream data from CSV format
// This is a simplified implementation for basic process data
func (s *Serializer) ImportFromCSV(csvData string) (*ValueStream, error) {
// This would implement CSV parsing for basic VSM data
// For now, return an error indicating this feature needs implementation
return nil, errors.New("CSV import not yet implemented - use JSON format instead")
}
// Validator provides validation for VSM objects
type Validator struct{}
// NewValidator creates a new VSM validator
func NewValidator() *Validator {
return &Validator{}
}
// ValidateValueStream validates a value stream
func (v *Validator) ValidateValueStream(vs *ValueStream) error {
if vs == nil {
return errors.New("value stream cannot be nil")
}
if vs.ID == "" {
return errors.New("value stream ID cannot be empty")
}
if vs.Name == "" {
return errors.New("value stream name cannot be empty")
}
if len(vs.Processes) == 0 {
return errors.New("value stream must have at least one process")
}
// Validate processes
for i, process := range vs.Processes {
if err := v.ValidateProcess(&process); err != nil {
return fmt.Errorf("process %d validation failed: %w", i, err)
}
}
// Validate inventory
for i, inv := range vs.Inventory {
if err := v.ValidateInventory(&inv); err != nil {
return fmt.Errorf("inventory %d validation failed: %w", i, err)
}
}
return nil
}
// ValidateProcess validates a process
func (v *Validator) ValidateProcess(process *Process) error {
if process.ID == "" {
return errors.New("process ID cannot be empty")
}
if process.Name == "" {
return errors.New("process name cannot be empty")
}
if process.Parameters.ProcessTime < 0 {
return errors.New("process time cannot be negative")
}
if process.Parameters.CycleTime < 0 {
return errors.New("cycle time cannot be negative")
}
return nil
}
// ValidateInventory validates inventory
func (v *Validator) ValidateInventory(inv *Inventory) error {
if inv.ID == "" {
return errors.New("inventory ID cannot be empty")
}
if inv.Quantity < 0 {
return errors.New("inventory quantity cannot be negative")
}
if inv.Range < 0 {
return errors.New("inventory range cannot be negative")
}
return nil
}
// ValidateVSMProject validates a VSM project
func (v *Validator) ValidateVSMProject(project *VSMProject) error {
if project == nil {
return errors.New("VSM project cannot be nil")
}
if project.ID == "" {
return errors.New("project ID cannot be empty")
}
if project.Name == "" {
return errors.New("project name cannot be empty")
}
if err := v.ValidateValueStream(&project.CurrentState); err != nil {
return fmt.Errorf("current state validation failed: %w", err)
}
if project.FutureState != nil {
if err := v.ValidateValueStream(project.FutureState); err != nil {
return fmt.Errorf("future state validation failed: %w", err)
}
}
if project.IdealState != nil {
if err := v.ValidateValueStream(project.IdealState); err != nil {
return fmt.Errorf("ideal state validation failed: %w", err)
}
}
return nil
}