001package com.aispeech.dui.dds.agent;
002
003import android.text.TextUtils;
004
005import com.aispeech.libbase.AIGsonUtil;
006
007import org.json.JSONArray;
008import org.json.JSONException;
009import org.json.JSONObject;
010
011import java.util.ArrayList;
012import java.util.List;
013
014/**
015 * Created by yu on 2018/12/20.
016 */
017
018public class SkillIntent {
019    private String skillId;
020    private String skillName;
021    private String taskName;
022    private String intentName;
023    private String slots;
024    private String input;
025    private List<ItemSlots> slotsList;
026    private JSONObject custom;
027
028    /**
029     * 技能意图类构造方法
030     */
031    public SkillIntent() {
032    }
033
034    /**
035     * 技能意图类构造方法
036     *
037     * @param skillId    技能id, 必填
038     * @param taskName   任务名称, 必填
039     * @param intentName 意图名称, 必填
040     * @param slots      语义槽, key-value Json, 可选, 不想填可以填null
041     * @param custom     扩展字段,类似于,对 trigger intent 添加了附加的功能 custom={"shouldShowUI" : "false"}
042     */
043    public SkillIntent(String skillId, String taskName, String intentName,
044                       String slots, JSONObject custom) {
045        this.skillId = skillId;
046        this.taskName = taskName;
047        this.intentName = intentName;
048        this.slots = slots;
049        this.custom = custom;
050    }
051
052    /**
053     * 技能意图类构造方法
054     *
055     * @param skillId    技能id, 必填
056     * @param taskName   任务名称, 必填
057     * @param intentName 意图名称, 必填
058     * @param slots      语义槽, key-value Json, 可选, 不想填可以填null
059     */
060    public SkillIntent(String skillId, String taskName, String intentName,
061                       String slots) {
062        this.skillId = skillId;
063        this.taskName = taskName;
064        this.intentName = intentName;
065        this.slots = slots;
066    }
067
068    /**
069     * 技能意图类构造方法
070     *
071     * @param skillId    技能id, 必填
072     * @param taskName   任务名称, 必填
073     * @param intentName 意图名称, 必填
074     * @param slotsList  语义槽, key-value Json, 可选, 不想填可以填null
075     */
076    public SkillIntent(String skillId, String taskName, String intentName,
077                       List<ItemSlots> slotsList) {
078        this.skillId = skillId;
079        this.taskName = taskName;
080        this.intentName = intentName;
081        this.slotsList = slotsList;
082    }
083
084    /**
085     * 获取技能id
086     *
087     * @return 技能id
088     */
089    public String getSkillId() {
090        return skillId;
091    }
092
093    /**
094     * 设置技能id
095     *
096     * @param skillId 技能id
097     */
098    public void setSkillId(String skillId) {
099        this.skillId = skillId;
100    }
101
102    public String getSkillName() {
103        return skillName;
104    }
105
106    public void setSkillName(String skillName) {
107        this.skillName = skillName;
108    }
109
110    /**
111     * 获取任务名称
112     *
113     * @return 任务名称
114     */
115    public String getTaskName() {
116        return taskName;
117    }
118
119    /**
120     * 设置任务名称
121     *
122     * @param taskName 任务名称
123     */
124    public void setTaskName(String taskName) {
125        this.taskName = taskName;
126    }
127
128    /**
129     * 获取意图名称
130     *
131     * @return 意图名称
132     */
133    public String getIntentName() {
134        return intentName;
135    }
136
137    /**
138     * 设置意图名称
139     *
140     * @param intentName 意图名称
141     */
142    public void setIntentName(String intentName) {
143        this.intentName = intentName;
144    }
145
146    /**
147     * 获取语义槽
148     *
149     * @return 语义槽
150     */
151    public String getSlots() {
152        return slots;
153    }
154
155    /**
156     * 设置语义槽
157     *
158     * @param slots 语义槽
159     */
160    public void setSlots(String slots) {
161        this.slots = slots;
162    }
163
164    public String getInput() {
165        return input;
166    }
167
168    public void setInput(String input) {
169        this.input = input;
170    }
171
172    public List<ItemSlots> getSlotsList() {
173        return slotsList;
174    }
175
176    public void setSlotsList(List<ItemSlots> slotsList) {
177        this.slotsList = slotsList;
178    }
179
180    public void addItemSlots(ItemSlots itemSlots) {
181        if (this.slotsList == null) {
182            this.slotsList = new ArrayList<>();
183        }
184        this.slotsList.add(itemSlots);
185    }
186
187    /**
188     * 设置扩展字段, 该字段不会上传到服务,只会在 sys.dialog.start 中透传出去
189     * 类似于,对 trigger intent 添加了附加的功能 custom={"shouldShowUI" : "false"}
190     *
191     * @param custom 扩展字段
192     */
193    public void setCustom(JSONObject custom) {
194        this.custom = custom;
195    }
196
197    public JSONObject getCustom() {
198        return custom;
199    }
200
201    @Override
202    public String toString() {
203        return toJson().toString();
204    }
205
206    public JSONObject toJson() {
207        JSONObject obj = new JSONObject();
208        try {
209            if (!TextUtils.isEmpty(skillId)) {
210                obj.put("skillId", skillId);
211            }
212            obj.put("task", taskName);
213            obj.put("intent", intentName);
214            if (!TextUtils.isEmpty(skillName)) {
215                obj.put("skill", skillName);
216            }
217            if (!TextUtils.isEmpty(slots)) {
218                obj.put("slots", new JSONObject(slots));
219            }
220            if (!TextUtils.isEmpty(input)) {
221                obj.put("input", input);
222            }
223            // TODO 离线情况下不支持slots为数组的格式!!
224            if (slotsList != null) {
225                String json = AIGsonUtil.getInstance().toJson(slotsList);
226                if (!TextUtils.isEmpty(json)) {
227                    obj.put("slots", new JSONArray(json));
228                }
229            }
230            if (custom != null) {
231                obj.put("custom", custom);
232            }
233        } catch (JSONException e) {
234            com.aispeech.dui.manager.AIJavaException.printException(e);
235        }
236        return obj;
237    }
238
239    public static class ItemSlots {
240
241        public ItemSlots(String name, String value) {
242            this.name = name;
243            this.value = value;
244        }
245
246        private String name;
247        private String value;
248        private String rawvalue;
249        private String rawpinyin;
250        private List<Integer> pos;
251
252        public String getName() {
253            return name;
254        }
255
256        public void setName(String name) {
257            this.name = name;
258        }
259
260        public String getValue() {
261            return value;
262        }
263
264        public void setValue(String value) {
265            this.value = value;
266        }
267
268        public String getRawvalue() {
269            return rawvalue;
270        }
271
272        public void setRawvalue(String rawvalue) {
273            this.rawvalue = rawvalue;
274        }
275
276        public String getRawpinyin() {
277            return rawpinyin;
278        }
279
280        public void setRawpinyin(String rawpinyin) {
281            this.rawpinyin = rawpinyin;
282        }
283
284        public List<Integer> getPos() {
285            return pos;
286        }
287
288        public void setPos(List<Integer> pos) {
289            this.pos = pos;
290        }
291    }
292}