TangCheng
2025-04-13 f01ba74b891046b4c458b2a27d79873eecfa1abe
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
package com.smtaiserver.smtaiserver.javaai.metrics.base;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
 
import org.apache.commons.text.similarity.JaccardSimilarity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.smtaiserver.smtaiserver.control.SMTAIServerControl;
import com.smtaiserver.smtaiserver.core.SMTAIServerApp;
import com.smtaiserver.smtaiserver.database.SMTDatabase;
import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecord;
import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecords;
import com.smtservlet.util.Json;
import com.smtservlet.util.SMTStatic;
 
public class SMTDimensionDef 
{
    private String                _id;
    private String                _name;
    private String                _prompt;
    private char                _type;
    private String                _unitName;
    private List<String[]>        _listMatches = new ArrayList<>();
    private List<String>        _listEnums = null;
    
    private static Logger         _logger = LogManager.getLogger(SMTAIServerControl.class);
    
    public SMTDimensionDef(DBRecord rec) throws Exception
    {
        _id = rec.getString("dim_id");
        _name = rec.getString("dim_name");
        _prompt = rec.getString("dim_title");
        _unitName = rec.getString("dim_unit");
        if(SMTStatic.isNullOrEmpty(_unitName))
            _unitName = "";
        if(SMTStatic.isNullOrEmpty(_prompt))
            _prompt = _name;
        else
            _prompt = _name + "," + _prompt;
        _type = rec.getString("dim_type").charAt(0);
        
        String sAlias = rec.getString("dim_alias");
        if(!SMTStatic.isNullOrEmpty(sAlias))
        {
            Json jsonMatch = Json.read(sAlias);
            for(Entry<String, Json> entry : jsonMatch.asJsonMap().entrySet())
            {
                String key = entry.getKey();
                for(Json jsonValue : entry.getValue().asJsonList())
                {
                    _listMatches.add(new String[] {jsonValue.asString(), key});
                }
            }
        }
        
        // 如果枚举值存在,则读取枚举值
        String sJsonValueList = rec.getString("dim_value_list");
        if(!SMTStatic.isNullOrEmpty(sJsonValueList))
        {
            Json jsonValueList = Json.read(sJsonValueList);
            
            // 如果能取到直接值,则用直接值
            Json jsonValues = jsonValueList.safeGetJson("values");
            if(jsonValues != null && jsonValues.asJsonList().size() > 0)
            {
                if(_listEnums == null)
                    _listEnums = new ArrayList<>();
                for(Json jsonValue : jsonValues.asJsonList())
                {
                    _listEnums.add(jsonValue.asString());
                }
            }
            
            // 如果配置了SQL, 则查询sql
            Json jsonSqlConfig = jsonValueList.safeGetJson("sql");
            if(jsonSqlConfig != null)
            {
                try
                {
                    String dsId = jsonSqlConfig.getJson("ds_id").asString();
                    String sql = jsonSqlConfig.getJson("sql").asString();
                    SMTDatabase db = SMTAIServerApp.getApp().getDataSource(dsId).allocDatabase();
                    try
                    {
                        DBRecords recs = db.querySQL(sql, null);
                        if(recs.getRowCount() > 0)
                        {
                            if(_listEnums == null)
                                _listEnums = new ArrayList<>();
                            
                            for(DBRecord recValue : recs.getRecords())
                            {
                                _listEnums.add(recValue.getString(0));
                            }
                        }
                    }
                    finally
                    {
                        db.close();
                    }
                }
                catch(Exception ex)
                {
                    _logger.fatal("load dim value list from db error : " + _id + ", error=" + ex.getMessage());
                }
            }
 
        }
    }
    
    public List<String> getValueList()
    {
        return _listEnums;
    }
    
    public boolean hasValueList()
    {
        return _listEnums != null && _listEnums.size() > 0;
    }
    
    public String getUnitName(boolean hasBound)
    {
        if(SMTStatic.isNullOrEmpty(_unitName))
            return "";
        if(hasBound)
            return "(" + _unitName + ")";
        
        return _unitName;
    }
    
    public String matchDimension(String value, JaccardSimilarity jaccardSimilarity)
    {
        double maxSim = 0;
        String maxName = null;
        for(String[] match : _listMatches)
        {
            double curSim = jaccardSimilarity.apply(match[0], value);
            if(curSim == 1)
                return match[1];
            if(curSim < 0.8)
                continue;
            if(curSim > maxSim)
            {
                maxSim = curSim;
                maxName = match[1];
            }
        }
        
        if(maxName != null)
            return maxName;
        
        return value;
    }
    
    public char getType()
    {
        return _type;
    }
    
    public String getName()
    {
        return _name;
    }
    
    public String getPrompt()
    {
        return _prompt;
    }
    
    public String getId()
    {
        return _id;
    }
}