Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ tool:
FILEEXTRACTNODESTATE: fileExtractionNodeState
LOOPNODESTATE: loopNodeState
PARALLELNODESTATE: parallelNodeState
TEXTCONCATENATENODESTATE: textConcatenateNodeState
export-meta:
version: 1.0.1
sensitive:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,10 @@
"type": "parallelNodeState",
"name": "Parallel",
"uniqueName": ""
},
{
"type": "textConcatenateNodeState",
"name": "Text Joiner",
"uniqueName": ""
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,10 @@
"type": "parallelNodeState",
"name": "并行",
"uniqueName": ""
},
{
"type": "textConcatenateNodeState",
"name": "文本拼接",
"uniqueName": ""
}
]
89 changes: 89 additions & 0 deletions app-builder/plugins/aipp-template-render/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>modelengine.fit.jade</groupId>
<artifactId>app-builder-plugin-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<groupId>modelengine.fit.jade.plugin</groupId>
<artifactId>aipp-template-render</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- Services -->
<dependency>
<groupId>modelengine.jade.service</groupId>
<artifactId>aipp-template-render-service</artifactId>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>modelengine.fit.jade</groupId>
<artifactId>aipp-service</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.fitframework</groupId>
<artifactId>fit-build-maven-plugin</artifactId>
<version>${fit.version}</version>
<executions>
<execution>
<id>build-plugin</id>
<goals>
<goal>build-plugin</goal>
</goals>
</execution>
<execution>
<id>package-plugin</id>
<goals>
<goal>package-plugin</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven.antrun.version}</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${project.build.finalName}.jar"
todir="../../../build/plugins"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package modelengine.fit.jade.aipp.template.render;

import modelengine.fitframework.annotation.Component;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* {@link TemplateService} 的实现类。
*
* @author 孙怡菲
* @since 2025-08-29
*/
@Component
public class TemplateServiceImpl implements TemplateService {
Comment thread
CodeCasterX marked this conversation as resolved.
Comment thread
reeeborn33 marked this conversation as resolved.
private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\{\\{\\s*(\\w+)\\s*}}");

@Override
public String renderTemplate(String template, Map<String, Object> args) {
if (template == null) {
return null;
}

if (args == null) {
args = Map.of();
}

Matcher matcher = PLACEHOLDER_PATTERN.matcher(template);
StringBuilder sb = new StringBuilder();

while (matcher.find()) {
String key = matcher.group(1);
Object value = args.getOrDefault(key, "");
matcher.appendReplacement(sb, Matcher.quoteReplacement(String.valueOf(value)));
}
matcher.appendTail(sb);

return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fit:
beans:
packages:
- 'modelengine.fit.jade.aipp.template.render'
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package modelengine.fit.jade.aipp.template.render;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* {@link TemplateServiceImpl} 的测试类。
*
* @author 孙怡菲
* @since 2025-08-28
*/
class TemplateServiceImplTest {
private TemplateServiceImpl textTool;

@BeforeEach
void setUp() {
this.textTool = new TemplateServiceImpl();
}

@Test
@DisplayName("基础模板变量替换成功")
void shouldReplaceBasicVariables() {
String template = "Hello {{name}}, your score is {{score}}.";
Map<String, Object> args = new HashMap<>();
args.put("name", "Tom");
args.put("score", 95);

String result = this.textTool.renderTemplate(template, args);
assertEquals("Hello Tom, your score is 95.", result);
}

@Test
@DisplayName("缺失变量时置为空字符串")
void shouldReplaceMissingVariableWithEmpty() {
String template = "Hello {{name}}, your score is {{score}}.";
Map<String, Object> args = new HashMap<>();
args.put("name", "Tom");

String result = this.textTool.renderTemplate(template, args);
assertEquals("Hello Tom, your score is .", result);
}

@Test
@DisplayName("空参数Map时模板变量置为空")
void shouldHandleEmptyArgsMap() {
String template = "Hello {{name}}!";
Map<String, Object> args = new HashMap<>();

String result = this.textTool.renderTemplate(template, args);
assertEquals("Hello !", result);
}

@Test
@DisplayName("参数为null时模板变量置为空")
void shouldHandleNullArgs() {
String template = "Hello {{name}}!";

String result = this.textTool.renderTemplate(template, null);
assertEquals("Hello !", result);
}

@Test
@DisplayName("模板无占位符时内容保持不变")
void shouldHandleTemplateWithoutPlaceholders() {
String template = "Hello world!";

String result = this.textTool.renderTemplate(template, Map.of("name", "Tom"));
assertEquals("Hello world!", result);
}

@Test
@DisplayName("变量中包含占位符内容保持不变")
void shouldHandleVariableWithPlaceholders() {
String template = "Hello {{name}}!";

String result = this.textTool.renderTemplate(template, Map.of("name", "{{Tom}}"));
assertEquals("Hello {{Tom}}!", result);
}

@Test
@DisplayName("变量为 List 时正常替换")
void shouldReplaceListVariableCorrectly() {
String template = "Items: {{items}}";
Map<String, Object> args = new HashMap<>();
args.put("items", Arrays.asList("apple", "banana", "cherry"));

String result = this.textTool.renderTemplate(template, args);
assertEquals("Items: [apple, banana, cherry]", result);
}

@Test
@DisplayName("变量为 Map 时正常替换")
void shouldReplaceMapVariableCorrectly() {
String template = "Map data: {{data}}";
Map<String, Object> args = new HashMap<>();
Map<String, Object> mapValue = new LinkedHashMap<>();
mapValue.put("a", 1);
mapValue.put("b", 2);
args.put("data", mapValue);

String result = this.textTool.renderTemplate(template, args);
assertEquals("Map data: {a=1, b=2}", result);
}
}
99 changes: 99 additions & 0 deletions app-builder/plugins/aipp-text-concatenation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>modelengine.fit.jade</groupId>
<artifactId>app-builder-plugin-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<groupId>modelengine.fit.jade.plugin</groupId>
<artifactId>aipp-text-concatenation</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- FIT -->
<dependency>
<groupId>org.fitframework</groupId>
<artifactId>fit-api</artifactId>
</dependency>
<dependency>
<groupId>org.fitframework</groupId>
<artifactId>fit-util</artifactId>
</dependency>

<!-- Services -->
<dependency>
<groupId>org.fitframework.fel</groupId>
<artifactId>tool-service</artifactId>
</dependency>
<dependency>
<groupId>modelengine.jade.service</groupId>
<artifactId>aipp-template-render-service</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.fitframework</groupId>
<artifactId>fit-build-maven-plugin</artifactId>
<version>${fit.version}</version>
<executions>
<execution>
<id>build-plugin</id>
<goals>
<goal>build-plugin</goal>
</goals>
</execution>
<execution>
<id>package-plugin</id>
<goals>
<goal>package-plugin</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.fitframework.fel</groupId>
<artifactId>tool-maven-plugin</artifactId>
<version>3.5.0-M6</version>
<executions>
<execution>
<id>build-tool</id>
<goals>
<goal>build-tool</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven.antrun.version}</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${project.build.finalName}.jar"
todir="../../../build/plugins"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Loading