TangCheng
2025-02-28 d787e447e95c7b897c2cc9c0e832f8d2e5084934
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package com.smtaiserver.smtaiserver.javaai.jsonflow.node;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.ibatis.ognl.Ognl;
import com.smtaiserver.smtaiserver.javaai.SMTJavaAIError;
import com.smtaiserver.smtaiserver.javaai.jsonflow.core.SMTJsonFlowExecArg;
import com.smtaiserver.smtaiserver.javaai.jsonflow.core.SMTJsonFlowManager;
import com.smtaiserver.smtaiserver.javaai.jsonflow.core.SMTJsonFlowNode;
import com.smtservlet.util.Json;
import com.smtservlet.util.SMTStatic;
 
 
 
public class SMTJsonFlowNodeCondJson extends SMTJsonFlowNode
{
    protected enum SMTJsonFlowNodeCondType
    {
        AND,
        OR
    }
    
    protected enum SMTJsonFlowNodeCondOP
    {
        include,
        notInclude,
        empty,
        notEmpty,
        startWith,
        notStartWith,
        endWith,
        notEndWith,
        eq,
        neq,
        gt,
        lt,
        gte,
        lte
    }
    
    protected static class SMTJsonFlowNodeCondExpr
    {
        public SMTJsonFlowNodeCondOP        _op;
        public Object                        _leftExpr;
        public Object                        _righExpr;
        
        
        public SMTJsonFlowNodeCondExpr(Json jsonCondExpr) throws Exception
        {
            _op = SMTJsonFlowNodeCondOP.valueOf(jsonCondExpr.getJson("comparison_operation").asString());
            _leftExpr = Ognl.parseExpression(jsonCondExpr.safeGetStr("left_value", "null"));
            _righExpr = Ognl.parseExpression(jsonCondExpr.safeGetStr("right_value", "null"));
        }
        
        public SMTJavaAIError executeFlowNode(SMTJsonFlowExecArg execArg, boolean[] r_eq) throws Exception
        {
            OgnlRecMap ognlEnv = new OgnlRecMap(execArg);
            Object leftValue = Ognl.getValue(_leftExpr, ognlEnv);
            Object rightValue = Ognl.getValue(_righExpr, ognlEnv);
            
            // 检查非大小判断
            switch(_op)
            {
            case include:
                return checkInclude(leftValue, rightValue, true, r_eq);
                
            case notInclude:
                return checkInclude(leftValue, rightValue, false, r_eq);
                
            case empty:
                return checkEmpty(leftValue, true, r_eq);
                
            case notEmpty:
                return checkEmpty(leftValue, false, r_eq);
                
            case startWith:
                return checkStartWith(leftValue, rightValue, true, r_eq);
                
            case notStartWith:
                return checkStartWith(leftValue, rightValue, false, r_eq);        
                
            case endWith:
                return checkEndWith(leftValue, rightValue, true, r_eq);
                
            case notEndWith:
                return checkEndWith(leftValue, rightValue, false, r_eq);
            default:
                break;    
            }
            
            // 检查大小判断
            int[] result = new int[1];
            SMTJavaAIError error = compareValue(leftValue, rightValue, result);
            if(error != null)
                return error;
            
            if(result[0] == -2)
            {
                r_eq[0] = false;
                return null;
            }
            
            switch(_op)
            {
            case eq:
                r_eq[0] = result[0] == 0;
                return null;
                
            case neq:
                r_eq[0] = result[0] != 0;
                return null;
                
            case gt:
                r_eq[0] = result[0] > 0;
                return null;
                
            case lt:
                r_eq[0] = result[0] < 0;
                return null;
                
            case gte:
                r_eq[0] = result[0] >= 0;
                return null;
                
            case lte:
                r_eq[0] = result[0] <= 0;
                return null;
                
            default:
                break;            
            }
            
            return null;
        }
        
        private SMTJavaAIError compareValue(Object leftValue, Object rightValue, int[] r_result)
        {
            
            if(leftValue == null || rightValue == null)
            {
                r_result[0] = (leftValue == null && rightValue == null) ? 0 : -2;
;
                return null;
            }
            
            if(leftValue instanceof Integer || leftValue instanceof Double || leftValue instanceof Float)
            {
                try
                {
                    double fLeft = SMTStatic.toDouble(leftValue);
                    double fRight = SMTStatic.toDouble(rightValue);
                    
                    int result = 0;
                    if(fLeft < fRight)
                        result = -1;
                    else if(fLeft > fRight)
                        result = 1;
                    
                    r_result[0] = result;
                    
                    return null;
                }
                catch(Exception ex)
                {
                    r_result[0] = -2;
                    return null;                    
                }
            }
            
            String sLeft = SMTStatic.toString(leftValue);
            String sRight = SMTStatic.toString(rightValue);
            
            r_result[0] = sLeft.compareTo(sRight);
            
            return null;
        }
        
        private SMTJavaAIError checkEmpty(Object leftValue, boolean isEmpty, boolean[] r_eq)
        {
            boolean result = false;
            if(leftValue == null)
                result = true;
            else if(SMTStatic.isNullOrEmpty(SMTStatic.toString(leftValue)))
                result = true;
 
            if(!isEmpty)
                result = !result;
            
            r_eq[0] = result;
            
            return null;
        }
        
        private SMTJavaAIError checkInclude(Object leftValue, Object rightValue, boolean isInclude, boolean[] r_eq)
        {
            if(leftValue == null || rightValue == null)
            {
                r_eq[0] = false;
                return null;
            }
            
            String sLeft = SMTStatic.toString(leftValue);
            String sRight = SMTStatic.toString(rightValue);
            
            boolean result = sLeft.indexOf(sRight) >= 0;
            if(!isInclude)
                result = !result;
            r_eq[0] = result;
            
            return null;
        }
        
        private SMTJavaAIError checkStartWith(Object leftValue, Object rightValue, boolean isInclude, boolean[] r_eq)
        {
            if(leftValue == null || rightValue == null)
            {
                r_eq[0] = false;
                return null;
            }
            
            String sLeft = SMTStatic.toString(leftValue);
            String sRight = SMTStatic.toString(rightValue);
            
            boolean result = sLeft.startsWith(sRight);
            if(!isInclude)
                result = !result;
            r_eq[0] = result;
            
            return null;
        }
        
        private SMTJavaAIError checkEndWith(Object leftValue, Object rightValue, boolean isInclude, boolean[] r_eq)
        {
            if(leftValue == null || rightValue == null)
            {
                r_eq[0] = false;
                return null;
            }
            
            String sLeft = SMTStatic.toString(leftValue);
            String sRight = SMTStatic.toString(rightValue);
            
            boolean result = sLeft.startsWith(sRight);
            if(!isInclude)
                result = !result;
            r_eq[0] = result;
            
            return null;
        }
 
    }
    
    protected static class SMTJsonFlowNodeCondItem
    {
        public SMTJsonFlowNodeCondType    _condJoin;
        public String                    _sourceHandle; 
        public List<SMTJsonFlowNode>    _listDownNodes = new ArrayList<>();
        public List<SMTJsonFlowNodeCondExpr> _listCondExpr = new ArrayList<>();
        
        public SMTJsonFlowNodeCondItem(Json jsonCondItem) throws Exception
        {
            _sourceHandle = jsonCondItem.getJson("id").asString();
            _condJoin = "and".equalsIgnoreCase(jsonCondItem.getJson("operator").asString()) ? 
                    SMTJsonFlowNodeCondType.AND : SMTJsonFlowNodeCondType.OR;
            
            for(Json jsonCondExpr : jsonCondItem.getJson("conditions").asJsonList())
            {
                SMTJsonFlowNodeCondExpr condExpr = new SMTJsonFlowNodeCondExpr(jsonCondExpr);
                _listCondExpr.add(condExpr);
            }
        }
        
        public void initEdge(SMTJsonFlowNode tagNode, String sourceHandle)
        {
            if(!_sourceHandle.equals(sourceHandle))
                return;
            _listDownNodes.add(tagNode);
        }
        
        public SMTJavaAIError executeFlowNode(SMTJsonFlowExecArg execArg, boolean[] r_eq) throws Exception
        {
            r_eq[0] = false;
            if(_condJoin == SMTJsonFlowNodeCondType.OR)
            {
                for(SMTJsonFlowNodeCondExpr condExpr : _listCondExpr)
                {
                    SMTJavaAIError error = condExpr.executeFlowNode(execArg, r_eq);
                    if(error != null)
                        return error;
                    if(r_eq[0])
                        return null;
                }
                return null;
            }
            else if(_condJoin == SMTJsonFlowNodeCondType.AND)
            {
                for(SMTJsonFlowNodeCondExpr condExpr : _listCondExpr)
                {
                    SMTJavaAIError error = condExpr.executeFlowNode(execArg, r_eq);
                    if(error != null)
                        return error;
                    if(!r_eq[0])
                        return null;
                }                
            }
            
            return null;
        }
    }
    
    private static class OgnlRecMap implements Map<String, Object>
    {
        private SMTJsonFlowExecArg     _execArg;
        
        public OgnlRecMap(SMTJsonFlowExecArg execArg)
        {
            _execArg = execArg;
        }
        
        @Override
        public int size() {
            return 0;
        }
 
        @Override
        public boolean isEmpty() {
            return false;
        }
 
        @Override
        public boolean containsKey(Object key) {
            return false;
        }
 
        @Override
        public boolean containsValue(Object value) {
            return false;
        }
 
        @Override
        public Object get(Object key) {
            try 
            {
                String value = _execArg._jsonArgs.safeGetStr((String)key, null);
                return value;
            } 
            catch (Exception e) 
            {
                throw new RuntimeException("get record value error : " + key, e);
            }
 
        }
 
        @Override
        public Object put(String key, Object value) {
            return null;
        }
 
        @Override
        public Object remove(Object key) {
            return null;
        }
 
        @Override
        public void putAll(Map<? extends String, ? extends Object> m) {
        }
 
        @Override
        public void clear() {
        }
 
        @Override
        public Set<String> keySet() {
            return null;
        }
 
        @Override
        public Collection<Object> values() {
            return null;
        }
 
        @Override
        public Set<Entry<String, Object>> entrySet() {
            return null;
        }
    }
    
    ///////////////////////////////////////////////////////////////////////////////
    protected String                            _sourceHandleElse;
    protected List<SMTJsonFlowNode>                _listDownNodesElse = new ArrayList<>();
    protected List<SMTJsonFlowNodeCondItem>        _listCondItem = new ArrayList<>();
    
    @Override
    public void initInstane(SMTJsonFlowManager manager, Json jsonNode) throws Exception
    {
        super.initInstane(manager, jsonNode);
        
        Json jsonConds = jsonNode.getJsonPath("data|group_params|0|params|0|value", false);
        
        for(Json jsonCond : jsonConds.asJsonList())
        {
            // 如果存在操作符则代表需要做条件判断
            if(jsonCond.has("operator"))
            {
                SMTJsonFlowNodeCondItem condItem = new SMTJsonFlowNodeCondItem(jsonCond);
                _listCondItem.add(condItem);
            }
            // 如果不存在操作符则代表else结果
            else
            {
                _sourceHandleElse = jsonCond.getJson("id").asString();
            }
        }
    }
 
    @Override
    public void initEdge(SMTJsonFlowNode tagNode, Json jsonEdge)
    {
        super.initEdge(tagNode, jsonEdge);
        String sourceHandle = jsonEdge.safeGetStr("sourceHandle", "");
        
        // 扫描并加入所有条件节点
        for(SMTJsonFlowNodeCondItem condItem : _listCondItem)
        {
            condItem.initEdge(tagNode, sourceHandle);
        }
        
        // 如果else节点存在匹配关系,则加入else节点
        if(_sourceHandleElse.equals(sourceHandle))
            _listDownNodesElse.add(tagNode);
    }
    
    @Override
    public void afterInstance() throws Exception
    {
        super.afterInstance();
        
        if(_listCondItem.size() == 0)
            throw new Exception("condtion is empty : " + this.getId());
        
        for(SMTJsonFlowNodeCondItem condItem : _listCondItem)
        {
            if(condItem._listDownNodes.size() == 0)
                throw new Exception("condtion node not any source handle : " + this.getId());
        }
        
        if(this._listDownNodesElse.size() == 0)
            throw new Exception("condtion node not else source handle : " + this.getId());
    }
    
    @Override
    public SMTJavaAIError executeFlowNode(SMTJsonFlowExecArg execArg) throws Exception 
    {
        List<SMTJsonFlowNode> listDownNodes = null;
        
        // 判断条件是否成立
        boolean[] condEQ = new boolean[1];
        for(SMTJsonFlowNodeCondItem condItem : _listCondItem)
        {
            SMTJavaAIError error = condItem.executeFlowNode(execArg, condEQ);
            if(error != null)
                return error;
            
            if(condEQ[0])
            {
                listDownNodes = condItem._listDownNodes;
                break;
            }
        }
 
        // 如果前面的条件都不符合,则自动跳转到else节点
        if(listDownNodes == null)
            listDownNodes = _listDownNodesElse;
        
        if(listDownNodes != null)
        {
            for(SMTJsonFlowNode flowNode : listDownNodes)
            {
                execArg._stackNodeExec.addLast(flowNode.createFlowNodeExec());
            }
        }
        return null;
    }
    
    
}