在我的Java应用程序中,我想使用PlantUML来创建图表。我想在不使用PlantUML图表代码的情况下,使用Java API在PlantUML中创建图表。不幸的是,互联网上缺乏这样的例子或文档,唯一有文档记录的API是生成图表from diagram code as string的API,这对我没有帮助。PlantUML中的什么接口可以用来以这种方式创建图表?
发布于 2021-10-12 20:16:27
在我看来,PlantUML不像是被设计成这样使用的:它没有文档记录。话虽如此,但它是一个开源项目,因此您可以下载源代码并跟踪给定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相同,如下所示:

https://stackoverflow.com/questions/69508989
复制相似问题