@@ -2,87 +2,51 @@ package a2l
2
2
3
3
import (
4
4
"errors"
5
- "math"
6
5
7
6
"github.com/rs/zerolog/log"
8
7
)
9
8
10
- type compuMethod struct {
11
- name string
12
- nameSet bool
13
- longIdentifier string
14
- longIdentifierSet bool
15
- conversionType conversionTypeEnum
16
- conversionTypeSet bool
17
- format string
18
- formatSet bool
19
- unit string
20
- unitSet bool
21
- coeffs coeffs
22
- coeffsLinear coeffsLinear
23
- compuTabRef compuTabRef
24
- formula []Formula
25
- refUnit refUnit
26
- statusStringRef statusStringRef
9
+ type CompuMethod struct {
10
+ Name string
11
+ NameSet bool
12
+ LongIdentifier string
13
+ LongIdentifierSet bool
14
+ ConversionType conversionTypeEnum
15
+ ConversionTypeSet bool
16
+ Format string
17
+ FormatSet bool
18
+ Unit string
19
+ UnitSet bool
20
+ Coeffs Coeffs
21
+ CoeffsLinear CoeffsLinear
22
+ CompuTabRef CompuTabRef
23
+ Formula []Formula
24
+ RefUnit refUnit
25
+ StatusStringRef StatusStringRef
27
26
}
28
27
29
- func (cm * compuMethod ) convDecToPhy (dec float64 ) (float64 , error ) {
30
- var err error
31
- switch cm .conversionType {
32
- case Identical :
33
- return dec , nil
34
- case Form :
35
- //implement later.
36
- case Linear :
37
- if ! (cm .coeffsLinear .aSet && cm .coeffsLinear .bSet ) {
38
- err = errors .New ("coeffsLinear not set in compuMethod: " + cm .name )
39
- log .Err (err ).Msg ("decimal value could not be converted" )
40
- return 0 , err
41
- }
42
- return cm .coeffsLinear .a * dec + cm .coeffsLinear .b , err
43
- case RatFunc :
44
- if ! (cm .coeffs .aSet && cm .coeffs .bSet && cm .coeffs .cSet && cm .coeffs .dSet && cm .coeffs .eSet && cm .coeffs .fSet ) {
45
- err = errors .New ("coeffs not set in compuMethod: " + cm .name )
46
- log .Err (err ).Msg ("decimal value could not be converted" )
47
- return 0 , err
48
- }
49
- phy , err := cm .calcRatFunc (dec )
50
- if err != nil {
51
- log .Err (err ).Msg ("decimal value could not be converted" )
52
- return phy , err
53
- }
54
- case TabIntp :
55
- case TabNointp :
56
- case TabVerb :
57
- default :
58
- err = errors .New ("conversion Type undefined in compuMethod: " + cm .name )
59
- log .Err (err ).Msg ("decimal value could not be converted" )
60
- return 0 , err
61
- }
62
- }
63
-
64
- func parseCompuMethod (tok * tokenGenerator ) (compuMethod , error ) {
65
- cm := compuMethod {}
28
+ func parseCompuMethod (tok * tokenGenerator ) (CompuMethod , error ) {
29
+ cm := CompuMethod {}
66
30
var err error
67
31
forLoop:
68
32
for {
69
33
switch tok .next () {
70
34
case coeffsToken :
71
- cm .coeffs , err = parseCoeffs (tok )
35
+ cm .Coeffs , err = parseCoeffs (tok )
72
36
if err != nil {
73
37
log .Err (err ).Msg ("compuMethod coeffs could not be parsed" )
74
38
break forLoop
75
39
}
76
40
log .Info ().Msg ("compuMethod coeffs successfully parsed" )
77
41
case coeffsLinearToken :
78
- cm .coeffsLinear , err = parseCoeffsLinear (tok )
42
+ cm .CoeffsLinear , err = parseCoeffsLinear (tok )
79
43
if err != nil {
80
44
log .Err (err ).Msg ("compuMethod coeffsLinear could not be parsed" )
81
45
break forLoop
82
46
}
83
47
log .Info ().Msg ("compuMethod coeffsLinear successfully parsed" )
84
48
case compuTabRefToken :
85
- cm .compuTabRef , err = parseCompuTabRef (tok )
49
+ cm .CompuTabRef , err = parseCompuTabRef (tok )
86
50
if err != nil {
87
51
log .Err (err ).Msg ("compuMethod compuTabRef could not be parsed" )
88
52
break forLoop
@@ -95,17 +59,17 @@ forLoop:
95
59
log .Err (err ).Msg ("compuMethod formula could not be parsed" )
96
60
break forLoop
97
61
}
98
- cm .formula = append (cm .formula , buf )
62
+ cm .Formula = append (cm .Formula , buf )
99
63
log .Info ().Msg ("compuMethod formula successfully parsed" )
100
64
case refUnitToken :
101
- cm .refUnit , err = parseRefUnit (tok )
65
+ cm .RefUnit , err = parseRefUnit (tok )
102
66
if err != nil {
103
67
log .Err (err ).Msg ("compuMethod refUnit could not be parsed" )
104
68
break forLoop
105
69
}
106
70
log .Info ().Msg ("compuMethod refUnit successfully parsed" )
107
71
case statusStringRefToken :
108
- cm .statusStringRef , err = parseStatusStringRef (tok )
72
+ cm .StatusStringRef , err = parseStatusStringRef (tok )
109
73
if err != nil {
110
74
log .Err (err ).Msg ("compuMethod statusStringRef could not be parsed" )
111
75
break forLoop
@@ -122,71 +86,32 @@ forLoop:
122
86
err = errors .New ("unedecpected token " + tok .current ())
123
87
log .Err (err ).Msg ("compuMethod could not be parsed" )
124
88
break forLoop
125
- } else if ! cm .nameSet {
126
- cm .name = tok .current ()
127
- cm .nameSet = true
89
+ } else if ! cm .NameSet {
90
+ cm .Name = tok .current ()
91
+ cm .NameSet = true
128
92
log .Info ().Msg ("compuMethod name successfully parsed" )
129
- } else if ! cm .longIdentifierSet {
130
- cm .longIdentifier = tok .current ()
131
- cm .longIdentifierSet = true
93
+ } else if ! cm .LongIdentifierSet {
94
+ cm .LongIdentifier = tok .current ()
95
+ cm .LongIdentifierSet = true
132
96
log .Info ().Msg ("compuMethod longIdentifier successfully parsed" )
133
- } else if ! cm .conversionTypeSet {
134
- cm .conversionType , err = parseConversionTypeEnum (tok )
97
+ } else if ! cm .ConversionTypeSet {
98
+ cm .ConversionType , err = parseConversionTypeEnum (tok )
135
99
if err != nil {
136
100
log .Err (err ).Msg ("compuMethod conversionType could not be parsed" )
137
101
break forLoop
138
102
}
139
- cm .conversionTypeSet = true
103
+ cm .ConversionTypeSet = true
140
104
log .Info ().Msg ("compuMethod conversionType successfully parsed" )
141
- } else if ! cm .formatSet {
142
- cm .format = tok .current ()
143
- cm .formatSet = true
105
+ } else if ! cm .FormatSet {
106
+ cm .Format = tok .current ()
107
+ cm .FormatSet = true
144
108
log .Info ().Msg ("compuMethod format successfully parsed" )
145
- } else if ! cm .unitSet {
146
- cm .unit = tok .current ()
147
- cm .unitSet = true
109
+ } else if ! cm .UnitSet {
110
+ cm .Unit = tok .current ()
111
+ cm .UnitSet = true
148
112
log .Info ().Msg ("compuMethod unit successfully parsed" )
149
113
}
150
114
}
151
115
}
152
116
return cm , err
153
117
}
154
-
155
- func (cm * compuMethod ) calcRatFunc (dec float64 ) (float64 , error ) {
156
- //following formula defines f(Physical) = Decimal
157
- //y = (axx + bx + c) / (dxx + ex + f)
158
- //inverted fi(Decimal) = Physical
159
- //y = (e dec - b)/(2 (a - d dec)) ± sqrt((e dec - b)^2 - 4 (d dec - a) (f dec - c))/(2 (a - d dec))
160
- firstDivisor := (2 * (cm .coeffs .a - cm .coeffs .d * dec ))
161
- if firstDivisor == 0 {
162
- err = errors .New ("rationality function cannot be computed(zero divisor) for compuMethod: " + cm .name )
163
- log .Err (err ).Msg ("decimal value could not be converted" )
164
- return 0 , err
165
- }
166
- secondDivisorPositive := (2 * (cm .coeffs .a - cm .coeffs .d * dec )) +
167
- math .Sqrt (math .Pow ((cm .coeffs .e * dec - cm .coeffs .b ), 2 )- 4 * (cm .coeffs .d * dec - cm .coeffs .a )* (cm .coeffs .f * dec - cm .coeffs .c ))/ firstDivisor
168
- secondDivisorNegative := (2 * (cm .coeffs .a - cm .coeffs .d * dec )) -
169
- math .Sqrt (math .Pow ((cm .coeffs .e * dec - cm .coeffs .b ), 2 )- 4 * (cm .coeffs .d * dec - cm .coeffs .a )* (cm .coeffs .f * dec - cm .coeffs .c ))/ firstDivisor
170
-
171
- if secondDivisorPositive != 0 && secondDivisorNegative != 0 {
172
- plusVal := (cm .coeffs .e * dec - cm .coeffs .b ) / secondDivisorPositive
173
- minusVal := (cm .coeffs .e * dec - cm .coeffs .b ) / secondDivisorNegative
174
- testVal := (cm .coeffs .a * plusVal * plusVal + cm .coeffs .b * plusVal + cm .coeffs .c ) / (cm .coeffs .d * plusVal * plusVal + cm .coeffs .e * plusVal + cm .coeffs .f )
175
- if testVal == dec {
176
- return plusVal , err
177
- } else {
178
- return minusVal , err
179
- }
180
- } else if secondDivisorPositive != 0 {
181
- plusVal := (cm .coeffs .e * dec - cm .coeffs .b ) / secondDivisorPositive
182
- return plusVal , err
183
- } else if secondDivisorNegative != 0 {
184
- minusVal := (cm .coeffs .e * dec - cm .coeffs .b ) / secondDivisorNegative
185
- return minusVal , err
186
- } else {
187
- err = errors .New ("rationality function cannot be computed(zero divisor) for compuMethod: " + cm .name )
188
- log .Err (err ).Msg ("decimal value could not be converted" )
189
- return 0 , err
190
- }
191
-
192
- }
0 commit comments