java - 如何在不使用 PlantUML 图代码的情况下使用 Java API 创建 PlantUML 图?
问题描述
在我的 Java 应用程序中,我想使用 PlantUML 创建图表。我想在不使用 PlantUML 图表代码的情况下使用它的 Java API 在 PlantUML 中创建图表。不幸的是,互联网上缺乏这方面的示例或文档,唯一记录在案的 API 是从图表代码生成图表作为字符串的 API ,这对我没有帮助。PlantUML 中的哪些 API 可用于以这种方式创建图表?
解决方案
在我看来,PlantUML 并没有被设计为以这种方式使用:API 没有记录。话虽如此,它是一个开源项目,因此您可以下载源代码并跟踪给定 UML 字符串的执行,然后使用避免 UML 源的方法调用重新创建该执行,如果您需要的话。如果你有一个可以单步调试第三方代码的调试器,比如 IntelliJ,那将有很大帮助。
我有一点尝试。这是一个类,它将使用 Java API 生成“Bob -> Alice : hello”图,而无需通过 UML 源字符串。我包括了 UML 字符串版本进行比较:
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.cucadiagram.Display;
import net.sourceforge.plantuml.sequencediagram.Message;
import net.sourceforge.plantuml.sequencediagram.Participant;
import net.sourceforge.plantuml.sequencediagram.SequenceDiagram;
import net.sourceforge.plantuml.sequencediagram.SequenceDiagramFactory;
import net.sourceforge.plantuml.skin.ArrowConfiguration;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import static com.google.common.base.Preconditions.checkState;
public class PlantUMLDemoMain {
public static void main(String[] args) throws Exception {
generateFromStringSource(new File("from-string.png"));
generateFromApi(new File("from-api.png"));
}
private static void generateFromApi(File file) throws IOException {
// 1. setup:
SequenceDiagramFactory f = new SequenceDiagramFactory();
SequenceDiagram diagram = f.createEmptyDiagram();
// 2. Build the diagram:
// "Bob -> Alice : hello"
// See net.sourceforge.plantuml.sequencediagram.command.CommandArrow#executeArg
Display bobD = Display.getWithNewlines("Bob");
Participant bobP = diagram.getOrCreateParticipant("Bob", bobD);
Display aliceD = Display.getWithNewlines("Alice");
Participant aliceP = diagram.getOrCreateParticipant("Alice", aliceD);
Display label = Display.getWithNewlines("hello");
ArrowConfiguration config = ArrowConfiguration.withDirectionNormal();
Message msg = new Message(bobP, aliceP, label, config, diagram.getNextMessageNumber());
checkState(null == diagram.addMessage(msg));
// 3. Output the diagram
// See net.sourceforge.plantuml.SourceStringReader#generateImage
diagram.makeDiagramReady();
checkState(1 == diagram.getNbImages());
try (OutputStream os = new FileOutputStream(file)) {
ImageData imageData = diagram.exportDiagram(os, 0, new FileFormatOption(FileFormat.PNG));
System.out.println("generateFromApi: " + diagram.getDescription().getDescription());
}
}
private static void generateFromStringSource(File file) throws IOException {
String source = "@startuml\n";
source += "Bob -> Alice : hello\n";
source += "@enduml\n";
SourceStringReader reader = new SourceStringReader(source);
// Write the first image to "png"
String desc = reader.generateImage(file);
// Return a null string if no generation
System.out.println("generateFromStringSource: " + desc);
}
}
这将使用build.sbt
如下文件构建:
libraryDependencies += "net.sourceforge.plantuml" % "plantuml" % "8059"
libraryDependencies += "com.google.guava" % "guava" % "31.0.1-jre"
的输出与generateFromApi
如下所示相同generateFromStringSource
:
推荐阅读
- delphi - Delphi、TAdoConnection、Azure SQL 托管实例
- docker - Dockerfile 在 COPY 中构建 ARG --from=
- java - 转换可迭代
列出 - mongoDB Java - vue.js - 允许用户添加输入的预填充输入
- java - 如何防止 iText 与页面底部的元素重叠
- http - 为什么现代 Web 浏览器不再显示 HTTP 身份验证的“领域”值?
- python - 在图形工具上绘制二分网络的最快方法
- reactjs - 反应 useState 钩子只更新一次 onClick
- php - 卷曲请求在本地主机上工作但不在线
- c# - 如何在 Azure 上发布多项目解决方案