1+ /*---------------------------------------------------------------------------------------------
2+ * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
3+ * This file is a part of the ModelEngine Project.
4+ * Licensed under the MIT License. See License.txt in the project root for license information.
5+ *--------------------------------------------------------------------------------------------*/
6+
7+ package modelengine .fit .jade .aipp .variable .updater ;
8+
9+ import modelengine .fit .jober .aipp .constants .AippConst ;
10+ import modelengine .fit .jober .common .ErrorCodes ;
11+ import modelengine .fit .jober .common .exceptions .JobberException ;
12+ import modelengine .fit .waterflow .spi .FlowableService ;
13+ import modelengine .fitframework .annotation .Component ;
14+ import modelengine .fitframework .annotation .Fitable ;
15+ import modelengine .fitframework .util .ObjectUtils ;
16+
17+ import java .util .List ;
18+ import java .util .Map ;
19+
20+ /**
21+ * 变量更新算子服务。
22+ *
23+ * @author 鲁为
24+ * @since 2025-09-26
25+ */
26+ @ Component
27+ public class AippVariableUpdater implements FlowableService {
28+ public static final String UPDATE_VARIABLES = "updateVariables" ;
29+ public static final String INTERNAL = "_internal" ;
30+ public static final String OUTPUT_SCOPE = "outputScope" ;
31+ public static final String KEY = "key" ;
32+ public static final String VALUE = "value" ;
33+
34+ @ Override
35+ @ Fitable ("modelengine.fit.jade.aipp.variable.updater" )
36+ public List <Map <String , Object >> handleTask (List <Map <String , Object >> flowData ) {
37+ Map <String , Object > businessData = this .getBusinessData (flowData );
38+ List <Map <String , Object >> updateVariables = ObjectUtils .cast (businessData .get (UPDATE_VARIABLES ));
39+ Map <String , Object > internal = ObjectUtils .cast (businessData .get (INTERNAL ));
40+ Map <String , Object > outputScope = ObjectUtils .cast (internal .get (OUTPUT_SCOPE ));
41+ for (Map <String , Object > variable : updateVariables ) {
42+ List <String > path = ObjectUtils .cast (variable .get (KEY ));
43+ Object newValue = variable .get (VALUE );
44+ this .updateNestedMapByPath (outputScope , path , newValue );
45+ }
46+ return flowData ;
47+ }
48+
49+ private Map <String , Object > getBusinessData (List <Map <String , Object >> flowData ) {
50+ if (flowData .isEmpty () || !flowData .get (0 ).containsKey (AippConst .BS_DATA_KEY )) {
51+ throw new JobberException (ErrorCodes .INPUT_PARAM_IS_EMPTY , AippConst .BS_DATA_KEY );
52+ }
53+ return ObjectUtils .cast (flowData .get (0 ).get (AippConst .BS_DATA_KEY ));
54+ }
55+
56+ private void updateNestedMapByPath (Map <String , Object > businessData , List <String > path , Object newValue ) {
57+ if (businessData == null || path == null || path .isEmpty ()) {
58+ return ;
59+ }
60+
61+ Map <String , Object > currentMap = businessData ;
62+
63+ for (int i = 0 ; i < path .size () - 1 ; i ++) {
64+ String key = path .get (i );
65+ Object value = currentMap .get (key );
66+
67+ if (value instanceof Map ) {
68+ currentMap = ObjectUtils .cast (value );
69+ }
70+ }
71+
72+ String finalKey = path .get (path .size () - 1 );
73+ currentMap .put (finalKey , newValue );
74+ }
75+ }
0 commit comments