Skip to content

Commit c22c14c

Browse files
authored
Merge pull request #22 from isomd/master
添加树扁平化
2 parents 59861fa + c79e29a commit c22c14c

10 files changed

Lines changed: 572 additions & 23 deletions

File tree

.github/workflows/release.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
echo "PR Description: $commit_message"
3434
3535
env=""
36-
module="PromptoLab"
36+
module=""
3737
module_type=""
3838
version=""
3939
skip=""
@@ -48,6 +48,10 @@ jobs:
4848
skip="${BASH_REMATCH[1]}"
4949
fi
5050
51+
if [[ "$commit_message" =~ -m:([^\ ]*) ]]; then
52+
module="${BASH_REMATCH[1]}"
53+
fi
54+
5155
if [[ "$commit_message" =~ -rp:([^\ ]*) ]]; then
5256
run_port="${BASH_REMATCH[1]}"
5357
fi

prompto-lab-app/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM adoptopenjdk:11-jre-hotspot
2-
COPY *.jar /rvc-captcha.jar
2+
COPY *.jar /prompto-lab.jar
33

44
ARG SERVER_PORT
55
ARG ACTIVE
@@ -28,6 +28,6 @@ ENV UNIQUE_ID=${UNIQUE_ID}
2828
ENV JASYPT_PASSWORD = ${JASYPT_PASSWORD}
2929
EXPOSE ${SERVER_PORT}
3030

31-
ENTRYPOINT ["java","-jar","/.jar"]
31+
ENTRYPOINT ["java","-jar","/prompto-lab.jar"]
3232

33-
#<Auto> -e:test -type:single -m:rvc-captcha -v:1.0.2 -rp:8080 -de:<-e ACTIVE=test -e SERVER_PORT=8080>
33+
#<Auto> -e:test -type:single -m:prompto-lab -v:1.0.2 -rp:8080 -de:<-e ACTIVE=test -e SERVER_PORT=8080>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.github.timemachinelab.constant;
2+
3+
public class QATypeConstant {
4+
public static final String FORM_QA = "form";
5+
public static final String TEXT_QA = "text";
6+
7+
8+
9+
}

prompto-lab-app/src/main/java/io/github/timemachinelab/core/qatree/QaTree.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.timemachinelab.core.qatree;
22

3-
import lombok.Data;
43
import lombok.Getter;
54

65
import java.util.HashMap;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package io.github.timemachinelab.core.serializable;
2+
3+
import io.github.timemachinelab.core.qatree.*;
4+
import io.github.timemachinelab.core.question.*;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
10+
import com.alibaba.fastjson2.JSONObject;
11+
import com.fasterxml.jackson.core.JsonProcessingException;
12+
import lombok.Getter;
13+
14+
import java.util.*;
15+
16+
@Getter
17+
@Builder
18+
public class JsonNode {
19+
String nodeId;
20+
String parentId;
21+
String question;
22+
String answer;
23+
24+
public static JsonNode Convert2JsonNode(QaTreeNode node, String parentId) {
25+
String question = "";
26+
String answer = "";
27+
28+
BaseQuestion qa = node.getQa();
29+
if (qa != null) {
30+
question = qa.getQuestion() != null ? qa.getQuestion() : "";
31+
32+
// 根据问题类型获取答案
33+
QuestionType type = QuestionType.fromString(qa.getType());
34+
switch (type) {
35+
case INPUT:
36+
InputQuestion inputQA = (InputQuestion) qa;
37+
answer = inputQA.getAnswer() != null ? inputQA.getAnswer() : "";
38+
break;
39+
case SINGLE:
40+
SingleChoiceQuestion singleQA = (SingleChoiceQuestion) qa;
41+
if (singleQA.getAnswer() != null && !singleQA.getAnswer().isEmpty()) {
42+
List<String> answerLabels = new ArrayList<>();
43+
for (String answerId : singleQA.getAnswer()) {
44+
String label = findOptionLabel(singleQA.getOptions(), answerId);
45+
answerLabels.add(label != null ? label : answerId);
46+
}
47+
answer = String.join(",", answerLabels);
48+
}
49+
break;
50+
case MULTI:
51+
MultipleChoiceQuestion multiQA = (MultipleChoiceQuestion) qa;
52+
if (multiQA.getAnswer() != null && !multiQA.getAnswer().isEmpty()) {
53+
List<String> answerLabels = new ArrayList<>();
54+
for (String answerId : multiQA.getAnswer()) {
55+
String label = findOptionLabel(multiQA.getOptions(), answerId);
56+
answerLabels.add(label != null ? label : answerId);
57+
}
58+
answer = String.join(",", answerLabels);
59+
}
60+
break;
61+
case FORM:
62+
FormQuestion formQA = (FormQuestion) qa;
63+
// 将FormField转换为TempFormQuestion的JSON格式拼接到question后面
64+
if (formQA.getFields() != null && !formQA.getFields().isEmpty()) {
65+
List<TempFormQuestion> tempQuestions = formQA.getFields().stream()
66+
.map(field -> {
67+
return TempFormQuestion.builder()
68+
.id(field.getId())
69+
.question(field.getQuestion())
70+
.type(field.getType())
71+
.options(field.getOptions())
72+
.desc(field.getDesc())
73+
.build();
74+
})
75+
.collect(java.util.stream.Collectors.toList());
76+
String fieldsJson = JSONObject.toJSONString(tempQuestions);
77+
question = question + ":" + fieldsJson;
78+
}
79+
// 将AnswerItem转换为JSON格式作为answer
80+
if (formQA.getAnswer() != null && !formQA.getAnswer().isEmpty()) {
81+
answer = JSONObject.toJSONString(formQA.getAnswer());
82+
}
83+
break;
84+
default:
85+
break;
86+
}
87+
}
88+
89+
return JsonNode.builder()
90+
.nodeId(node.getId())
91+
.parentId(parentId)
92+
.question(question)
93+
.answer(answer)
94+
.build();
95+
}
96+
97+
/**
98+
* 根据选项id查找对应的标签
99+
* @param options 选项列表
100+
* @param id 选项id
101+
* @return 选项标签,如果未找到则返回null
102+
*/
103+
private static String findOptionLabel(List<Option> options, String id) {
104+
if (options == null || id == null) {
105+
return null;
106+
}
107+
for (Option option : options) {
108+
if (id.equals(option.getId())) {
109+
return option.getLabel();
110+
}
111+
}
112+
return null;
113+
}
114+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.github.timemachinelab.core.serializable;
2+
3+
import io.github.timemachinelab.core.question.*;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
7+
import java.util.*;
8+
9+
@Data
10+
@Builder
11+
public class TempFormQuestion {
12+
public String id;
13+
public String question;
14+
public String type;
15+
public List<Option> options;
16+
public String desc;
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.timemachinelab.util;
2+
3+
import com.alibaba.fastjson2.JSONObject;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import io.github.timemachinelab.core.qatree.QaTree;
7+
import io.github.timemachinelab.core.qatree.QaTreeNode;
8+
import io.github.timemachinelab.core.serializable.JsonNode;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
public class QaTreeSerializeUtil {
14+
15+
public static String serialize(QaTree t) throws JsonProcessingException {
16+
if (t == null || t.getRoot() == null) {
17+
return "[]";
18+
}
19+
20+
List<JsonNode> result = new ArrayList<>();
21+
22+
firstOrderTraversal(t.getRoot(), null, result);
23+
24+
return JSONObject.toJSONString(result);
25+
}
26+
27+
private static void firstOrderTraversal(QaTreeNode node, String parentId, List<JsonNode> result) throws JsonProcessingException {
28+
if (node == null) {
29+
return;
30+
}
31+
32+
// 获取子节点列表
33+
List<QaTreeNode> children = new ArrayList<>();
34+
35+
if (node.getChildren() != null) {
36+
children.addAll(node.getChildren().values());
37+
}
38+
39+
// 访问当前节点
40+
JsonNode jsonNode = JsonNode.Convert2JsonNode(node, parentId);
41+
42+
result.add(jsonNode);
43+
44+
// 先序遍历
45+
for (QaTreeNode child : children) {
46+
firstOrderTraversal(child, node.getId(), result);
47+
}
48+
}
49+
}

prompto-lab-app/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ spring:
77
application:
88
name: poet-agent
99
datasource:
10-
url: ${SPRING_DATASOURCE_URL:ENC(74AA8WeNmewtlYjGh4jIWPh8E6cxGrZ0M7zg+1wAfBJLR36B6GTw4cvCfA/EhuZiy0oSbrnGRERv5IxzsxfOFA==)}
10+
url: ${SPRING_DATASOURCE_URL:ENC(AdWk4PSD8S3YHvL824vcUWPkYgVIdSRID1Sy7d2xGa+/WoU0PHMTXvVyk+n7LjQAeidxhRDRQSw9ilmYnY5eef5Tb+1lP4LSa+C5up64Sal7lEqgXbTvalO50Yrsn+mgUGoPJ5DfJ8bETsD35HPKNc/AhcxbzeJHFxrHKhYMsI8EJRFhpzho81HAUd6ARO1l2Ugryj+nbXfWMIgD7PZ6Uo32hHOYzxivRN18YiKmSHU=)}
1111
username: ${SPRING_DATASOURCE_USERNAME:ENC(nOfIk0lODnZFZinDkKMeEX5zHzXpOoIj)}
1212
password: ${SPRING_DATASOURCE_PASSWORD:ENC(W52dd0i/Q8pT/yEJIKs6Zc/brOr6KjXBPVcwdjoe4g4=)}
1313
driver-class-name: ${SPRING_DATASOURCE_DRIVER_CLASS_NAME:org.postgresql.Driver}

0 commit comments

Comments
 (0)