@@ -70,66 +70,52 @@ public boolean isEmpty() {
70
70
}
71
71
72
72
public void translateAndSetValues (Map <String ,Object > varValues ) {
73
- // Check if bindings are available
74
- if (bindings == null || bindings .isEmpty ()) {
75
- log .error ("MvvService.translateAndSetValues: No bindings provided: {}" , bindings );
76
- return ;
77
- }
78
- Map <String , String > simpleBindings = bindings .get (ModelsService .SIMPLE_BINDING_KEY );
79
- Map <String , String > compositeBindings = bindings .get (ModelsService .COMPOSITE_BINDING_KEY );
80
- if (simpleBindings ==null || simpleBindings .isEmpty ()) {
81
- log .error ("MvvService.translateAndSetValues: No simple bindings provided: {}" , bindings );
82
- return ;
83
- }
84
-
85
- // Log new values
86
- log .info ("MvvService.translateAndSetValues: New Variable Values: {}" , varValues );
87
- Map <String , Double > newValues = new HashMap <>();
88
-
89
- // Process simple constants
90
- varValues .forEach ((k , v ) -> {
91
- String constName = simpleBindings .get (k );
92
- if (StringUtils .isNotBlank (constName )) {
93
- if (v instanceof Number n )
94
- newValues .put (constName , n .doubleValue ());
95
- else
96
- throw new IllegalArgumentException ("Solution variable value is not a number: " + v );
97
- log .trace ("MvvService.translateAndSetValues: Added Simple Constant value: {} = {}" , constName , n );
98
- } else
99
- log .warn ("MvvService.translateAndSetValues: No Constant found for Solution variable: {}" , k );
100
- });
101
- log .debug ("MvvService.translateAndSetValues: Simple Constant values: {}" , newValues );
102
-
103
- // Process composite constants
104
- if (compositeBindings !=null && ! compositeBindings .isEmpty ()) {
105
- Map <String , String > pending = new HashMap <>(bindings .get (ModelsService .COMPOSITE_BINDING_KEY ));
106
- @ NotNull final Map <String , Double > varValues1 = varValues .entrySet ().stream ().collect (Collectors .toMap (
107
- Map .Entry ::getKey , e -> ((Number ) e .getValue ()).doubleValue ()
108
- ));
109
- while (!pending .isEmpty ()) {
110
- Map <String , String > newPending = new HashMap <>();
111
- pending .forEach ((formula , constName ) -> {
112
- if (StringUtils .isNotBlank (formula ) && StringUtils .isNotBlank (constName )) {
113
- try {
114
- log .trace ("MvvService.translateAndSetValues: Calculating Composite Constant value: {} ::= {} -- Values: {}" , constName , formula , newValues );
115
- Double formulaValue = MathUtil .eval (formula , varValues1 );
116
- newValues .put (constName , formulaValue );
117
- log .trace ("MvvService.translateAndSetValues: Added Composite Constant value: {} = {}" , constName , formulaValue );
118
- } catch (Exception e ) {
119
- log .trace ("MvvService.translateAndSetValues: Could not calculate Composite Constant value: {} ::= {} -- Values: {} -- Reason: {}" , constName , formula , newValues , e .getMessage ());
120
- newPending .put (formula , constName );
121
- }
73
+ log .info ("MvvService.translateAndSetValues: New Variable Values: {}" , varValues );
74
+ Map <String , Double > newValues = new HashMap <>();
75
+
76
+ // Process simple constants
77
+ final Map <String , String > simpleBindings = bindings .get (ModelsService .SIMPLE_BINDING_KEY );
78
+ varValues .forEach ((k ,v ) -> {
79
+ String constName = simpleBindings .get (k );
80
+ if (StringUtils .isNotBlank (constName )) {
81
+ if (v instanceof Number n )
82
+ newValues .put (constName , n .doubleValue ());
83
+ else
84
+ throw new IllegalArgumentException ("Solution variable value is not a number: " +v );
85
+ log .trace ("MvvService.translateAndSetValues: Added Simple Constant value: {} = {}" , constName , n );
86
+ } else
87
+ log .warn ("MvvService.translateAndSetValues: No Constant found for Solution variable: {}" , k );
88
+ });
89
+ log .debug ("MvvService.translateAndSetValues: Simple Constant values: {}" , newValues );
90
+
91
+ // Process composite constants
92
+ Map <String , String > pending = new HashMap <>(bindings .get (ModelsService .COMPOSITE_BINDING_KEY ));
93
+ @ NotNull final Map <String , Double > varValues1 = varValues .entrySet ().stream ().collect (Collectors .toMap (
94
+ Map .Entry ::getKey , e -> ((Number ) e .getValue ()).doubleValue ()
95
+ ));
96
+ while (! pending .isEmpty ()) {
97
+ Map <String , String > newPending = new HashMap <>();
98
+ pending .forEach ((formula , constName ) -> {
99
+ if (StringUtils .isNotBlank (formula ) && StringUtils .isNotBlank (constName )) {
100
+ try {
101
+ log .trace ("MvvService.translateAndSetValues: Calculating Composite Constant value: {} ::= {} -- Values: {}" , constName , formula , newValues );
102
+ Double formulaValue = MathUtil .eval (formula , varValues1 );
103
+ newValues .put (constName , formulaValue );
104
+ log .trace ("MvvService.translateAndSetValues: Added Composite Constant value: {} = {}" , constName , formulaValue );
105
+ } catch (Exception e ) {
106
+ log .trace ("MvvService.translateAndSetValues: Could not calculate Composite Constant value: {} ::= {} -- Values: {} -- Reason: {}" , constName , formula , newValues , e .getMessage ());
107
+ newPending .put (formula , constName );
122
108
}
123
- });
124
- if ( pending . size () == newPending . size ())
125
- throw new IllegalArgumentException ( "Composite Constants cannot be calculated. Check for missing terms or circles in formulas: " + pending );
126
- pending = newPending ;
127
- }
109
+ }
110
+ });
111
+ if ( pending . size ()== newPending . size ())
112
+ throw new IllegalArgumentException ( "Composite Constants cannot be calculated. Check for missing terms or circles in formulas: " + pending ) ;
113
+ pending = newPending ;
128
114
}
129
- log .info ("MvvService.translateAndSetValues: New Constant values: {}" , newValues );
115
+ log .info ("MvvService.translateAndSetValues: New Constant values: {}" , newValues );
130
116
131
- setControlServiceConstants (newValues );
132
- }
117
+ setControlServiceConstants (newValues );
118
+ }
133
119
134
120
private void setControlServiceConstants (@ NonNull Map <String , Double > newValues ) {
135
121
this .values = newValues ;
0 commit comments