/
cellAllAcc.go
424 lines (345 loc) · 12.6 KB
/
cellAllAcc.go
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
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package db
import (
"errors"
"fmt"
"strconv"
)
// CellAllAcc is value of multiple output table accumulators.
type CellAllAcc struct {
DimIds []int // dimensions item as enum ids or int values if dimension type simple
SubId int // output table subvalue id
IsNull []bool // if true then value is NULL
Value []float64 // accumulator value(s)
}
// CellCodeAllAcc is value of multiple output table accumulators.
// Dimension(s) items are enum codes, not enum ids.
type CellCodeAllAcc struct {
Dims []string // dimensions as enum codes or string of item if dimension type simple then
SubId int // output table subvalue id
IsNull []bool // if true then value is NULL
Value []float64 // accumulator value(s)
}
// CellAllAccConverter is a converter for multiple output table accumulators to implement CsvConverter interface.
type CellAllAccConverter struct {
CellTableConverter // model metadata and output table name
ValueName string // If ValueName is "" empty then all accumulators use for csv else one
}
// return true if csv converter is using enum id's for dimensions
func (cellCvt *CellAllAccConverter) IsUseEnumId() bool { return cellCvt.IsIdCsv }
// CsvFileName return file name of csv file to store all accumulators rows
func (cellCvt *CellAllAccConverter) CsvFileName() (string, error) {
// find output table by name
_, err := cellCvt.tableByName()
if err != nil {
return "", err
}
// make csv file name
if cellCvt.IsIdCsv {
return cellCvt.Name + ".id.acc-all.csv", nil
}
return cellCvt.Name + ".acc-all.csv", nil
}
// CsvHeader return first line for csv file: column names.
// Column names can be like: sub_id,dim0,dim1,acc0,acc1,acc2
// If ValueName is "" empty then use all accumulators for csv else only one where accumulator name is ValueName
func (cellCvt *CellAllAccConverter) CsvHeader() ([]string, error) {
// find output table by name
table, err := cellCvt.tableByName()
if err != nil {
return []string{}, err
}
// make first line columns:
// if accumulator name specified then only one column else all accumlators
nAcc := 1
if cellCvt.ValueName == "" {
nAcc = len(table.Acc)
}
h := make([]string, 1+table.Rank+nAcc)
h[0] = "sub_id"
for k := range table.Dim {
h[k+1] = table.Dim[k].Name
}
if cellCvt.ValueName != "" {
h[table.Rank+1] = cellCvt.ValueName
} else {
for k := range table.Acc {
h[table.Rank+1+k] = table.Acc[k].Name
}
}
return h, nil
}
// KeyIds return converter to copy primary key: (sub id, dimension ids) into key []int.
//
// Converter will return error if len(key) not equal to row key size.
func (cellCvt *CellAllAccConverter) KeyIds(name string) (func(interface{}, []int) error, error) {
cvt := func(src interface{}, key []int) error {
cell, ok := src.(CellAllAcc)
if !ok {
return errors.New("invalid type, expected: CellAllAcc (internal error): " + name)
}
n := len(cell.DimIds)
if len(key) != n+1 {
return errors.New("invalid size of key buffer, expected: " + strconv.Itoa(n+1) + ": " + name)
}
key[0] = cell.SubId
for k, e := range cell.DimIds {
key[k+1] = e
}
return nil
}
return cvt, nil
}
// ToCsvIdRow return converter from output table cell (sub_id, dimensions, acc0, acc1, acc2) to csv id's row []string.
//
// Converter return isNotEmpty flag, it return false if IsNoZero or IsNoNull is set and all values are empty or zero.
// Converter simply does Sprint() for each dimension item id, subvalue number and value(s).
// Converter will return error if len(row) not equal to number of fields in csv record.
// Double format string is used if parameter type is float, double, long double
// If ValueName is "" empty then all accumulators converted else one
func (cellCvt *CellAllAccConverter) ToCsvIdRow() (func(interface{}, []string) (bool, error), error) {
// find output table by name
table, err := cellCvt.tableByName()
if err != nil {
return nil, err
}
// number of dimensions and number of accumulators to be converted
nAcc := 1
if cellCvt.ValueName == "" {
nAcc = len(table.Acc)
}
nRank := table.Rank
// make converter
cvt := func(src interface{}, row []string) (bool, error) {
cell, ok := src.(CellAllAcc)
if !ok {
return false, errors.New("invalid type, expected: CellAllAcc (internal error): " + cellCvt.Name)
}
if len(row) != 1+nRank+nAcc || len(cell.DimIds) != nRank || len(cell.IsNull) != nAcc || len(cell.Value) != nAcc {
return false, errors.New("invalid size of csv row buffer, expected: " + strconv.Itoa(1+nRank+nAcc) + ": " + cellCvt.Name)
}
row[0] = fmt.Sprint(cell.SubId)
for k, e := range cell.DimIds {
row[k+1] = fmt.Sprint(e)
}
// check for empty data: if all values are NULLs or zeros and no null or no zero flag is set
isAllEmpty := cellCvt.IsNoZeroCsv || cellCvt.IsNoNullCsv
for k := 0; isAllEmpty && k < nAcc; k++ {
if cell.IsNull[k] {
isAllEmpty = cellCvt.IsNoNullCsv
} else {
isAllEmpty = cellCvt.IsNoZeroCsv && cell.Value[k] == 0
}
}
// use "null" string for db NULL values and format for model float types
for k := 0; k < nAcc; k++ {
if cell.IsNull[k] {
row[1+nRank+k] = "null"
} else {
row[1+nRank+k] = "0"
if !cellCvt.IsNoZeroCsv || cell.Value[k] != 0 {
if cellCvt.DoubleFmt != "" {
row[1+nRank+k] = fmt.Sprintf(cellCvt.DoubleFmt, cell.Value[k])
} else {
row[1+nRank+k] = fmt.Sprint(cell.Value[k])
}
}
}
}
return !isAllEmpty, nil
}
return cvt, nil
}
// ToCsvRow return converter from output table cell (sub_id, dimensions, acc0, acc1, acc2
// to csv row []string (acc_name, sub_id, dimensions, value).
//
// Converter return isNotEmpty flag, it return false if IsNoZero or IsNoNull is set and all values are empty or zero.
// Converter will return error if len(row) not equal to number of fields in csv record.
// Double format string is used if parameter type is float, double, long double
// If dimension type is enum based then csv row is enum code and cell.DimIds is enum id.
// If ValueName is "" empty then all accumulators converted else one
func (cellCvt *CellAllAccConverter) ToCsvRow() (func(interface{}, []string) (bool, error), error) {
// find output table by name
table, err := cellCvt.tableByName()
if err != nil {
return nil, err
}
// number of dimensions and number of accumulators to be converted
nAcc := 1
if cellCvt.ValueName == "" {
nAcc = len(table.Acc)
}
nRank := table.Rank
// for each dimension create converter from item id to code
fd := make([]func(itemId int) (string, error), nRank)
for k := 0; k < nRank; k++ {
f, err := table.Dim[k].typeOf.itemIdToCode(cellCvt.Name+"."+table.Dim[k].Name, table.Dim[k].IsTotal)
if err != nil {
return nil, err
}
fd[k] = f
}
cvt := func(src interface{}, row []string) (bool, error) {
cell, ok := src.(CellAllAcc)
if !ok {
return false, errors.New("invalid type, expected: output table accumulator cell (internal error): " + cellCvt.Name)
}
if len(row) != 1+nRank+nAcc || len(cell.DimIds) != nRank || len(cell.IsNull) != nAcc || len(cell.Value) != nAcc {
return false, errors.New("invalid size of csv row buffer, expected: " + strconv.Itoa(1+nRank+nAcc) + ": " + cellCvt.Name)
}
row[0] = fmt.Sprint(cell.SubId)
// convert dimension item id to code
for k, e := range cell.DimIds {
v, err := fd[k](e)
if err != nil {
return false, err
}
row[k+1] = v
}
// check for empty data: if all values are NULLs or zeros and no null or no zero flag is set
isAllEmpty := cellCvt.IsNoZeroCsv || cellCvt.IsNoNullCsv
for k := 0; isAllEmpty && k < nAcc; k++ {
if cell.IsNull[k] {
isAllEmpty = cellCvt.IsNoNullCsv
} else {
isAllEmpty = cellCvt.IsNoZeroCsv && cell.Value[k] == 0
}
}
// use "null" string for db NULL values and format for model float types
for k := 0; k < nAcc; k++ {
if cell.IsNull[k] {
row[1+nRank+k] = "null"
} else {
row[1+nRank+k] = "0"
if !cellCvt.IsNoZeroCsv || cell.Value[k] != 0 {
if cellCvt.DoubleFmt != "" {
row[1+nRank+k] = fmt.Sprintf(cellCvt.DoubleFmt, cell.Value[k])
} else {
row[1+nRank+k] = fmt.Sprint(cell.Value[k])
}
}
}
}
return !isAllEmpty, nil
}
return cvt, nil
}
// CsvToCell return closure to convert csv row []string to output table accumulator cell (dimensions and value).
//
// It does return error if len(row) not equal to number of fields in cell db-record.
// If dimension type is enum based then csv row is enum code and it is converted into cell.DimIds (into dimension type type enum ids).
func (cellCvt *CellAllAccConverter) CsvToCell() (func(row []string) (interface{}, error), error) {
// find output table by name
table, err := cellCvt.tableByName()
if err != nil {
return nil, err
}
// number of dimensions and number of accumulators to be converted
nAcc := 1
if cellCvt.ValueName == "" {
nAcc = len(table.Acc)
}
nRank := table.Rank
// for each dimension create converter from item code to id
fd := make([]func(src string) (int, error), nRank)
for k := 0; k < nRank; k++ {
f, err := table.Dim[k].typeOf.itemCodeToId(cellCvt.Name+"."+table.Dim[k].Name, table.Dim[k].IsTotal)
if err != nil {
return nil, err
}
fd[k] = f
}
// do conversion
cvt := func(row []string) (interface{}, error) {
// make conversion buffer and check input csv row size
cell := CellAllAcc{
DimIds: make([]int, nRank),
IsNull: make([]bool, nAcc),
Value: make([]float64, nAcc)}
if len(row) != 1+nRank+nAcc {
return nil, errors.New("invalid size of csv row buffer, expected: " + strconv.Itoa(1+nRank+nAcc) + ": " + cellCvt.Name)
}
// subvalue number
nSub, err := strconv.Atoi(row[0])
if err != nil {
return nil, err
}
/* validation done at writing
if subCount == 1 && nSub != defaultSubId || nSub < 0 || nSub >= subCount {
return nil, errors.New("invalid sub-value id: " + strconv.Itoa(nSub) + " output table: " + name)
}
*/
cell.SubId = nSub
// convert dimensions: enum code to enum id or integer value for simple type dimension
for k := range cell.DimIds {
i, err := fd[k](row[k+1])
if err != nil {
return nil, err
}
cell.DimIds[k] = i
}
// value conversion
for k := 0; k < nAcc; k++ {
cell.IsNull[k] = row[1+nRank+k] == "" || row[1+nRank+k] == "null"
if cell.IsNull[k] {
cell.Value[k] = 0.0
} else {
v, err := strconv.ParseFloat(row[1+nRank+k], 64)
if err != nil {
return nil, err
}
cell.Value[k] = v
}
}
return cell, nil
}
return cvt, nil
}
// IdToCodeCell return converter from output table cell of ids: (sub_id, dimensions, acc0, acc1, acc2)
// to cell of codes: (sub_id, dimensions as enum code, acc0, acc1, acc2)
//
// If dimension type is enum based then dimensions enum ids can be converted to enum code.
// If dimension type is simple (bool or int) then dimension value converted to string.
func (cellCvt *CellAllAccConverter) IdToCodeCell(modelDef *ModelMeta, name string) (func(interface{}) (interface{}, error), error) {
// find output table by name
table, err := cellCvt.tableByName()
if err != nil {
return nil, err
}
// for each dimension create converter from item id to code
fd := make([]func(itemId int) (string, error), table.Rank)
for k := 0; k < table.Rank; k++ {
f, err := table.Dim[k].typeOf.itemIdToCode(name+"."+table.Dim[k].Name, table.Dim[k].IsTotal)
if err != nil {
return nil, err
}
fd[k] = f
}
// create cell converter
cvt := func(src interface{}) (interface{}, error) {
srcCell, ok := src.(CellAllAcc)
if !ok {
return nil, errors.New("invalid type, expected: output table accumulator cell (internal error): " + name)
}
if len(srcCell.DimIds) != table.Rank {
return nil, errors.New("invalid cell rank: " + strconv.Itoa(len(srcCell.DimIds)) + ", expected: " + strconv.Itoa(table.Rank) + ": " + name)
}
dstCell := CellCodeAllAcc{
Dims: make([]string, table.Rank),
SubId: srcCell.SubId,
IsNull: append([]bool{}, srcCell.IsNull...),
Value: append([]float64{}, srcCell.Value...),
}
// convert dimension item id to code
for k := range srcCell.DimIds {
v, err := fd[k](srcCell.DimIds[k])
if err != nil {
return nil, err
}
dstCell.Dims[k] = v
}
return dstCell, nil // converted OK
}
return cvt, nil
}