LoginSignup
4
0

More than 3 years have passed since last update.

PlantUMLソースコードリーディング(1)

Last updated at Posted at 2021-01-03

概要

PlantUML の仕様をソースコードから分析したくなったため、現時点(2020/01/03)でどのような設計で実装しているのか追いかけるための足ががりとなるポイントをまとめておく。(個別のPlantUMLのコマンド実装の設計については別記事で書いていきたい)

ソースコードリポジトリ

大まかな構造

net.sourceforge.plantuml.Run の実行時に受け取った文字列またはファイル内容のPlantUML用の文字列を解析処理に渡す処理がある。以下の順番で呼ばれる。

  1. net.sourceforge.plantuml.Run#main()
  2. net.sourceforge.plantuml.Run#manageAllFiles()
  3. net.sourceforge.plantuml.Run#processArgs()
  4. net.sourceforge.plantuml.Run#manageFileInternal()
  5. net.sourceforge.plantuml.BlockUml#getDiagram()
  6. net.sourceforge.plantuml.PSystemBuilder#createPSystem()
  7. net.sourceforge.plantuml.PSystemBuilder#getAllFactories()

ここで、net.sourceforge.plantuml.PSystemBuilder#getAllFactories() の処理で各モジュール(文字解析)が初期化されている。
Factoryを定義する際に、文字を解析するための文字パターンを各モジュールのどこかで定義している。
例えば、 colors というPlantUMLのコマンド の正規表現は以下の部分で定義されている。

ソースコード

public class PSystemColorsFactory extends PSystemSingleLineFactory {

    @Override
    protected AbstractPSystem executeLine(String line) {
        final Pattern pattern = Pattern.compile("^colors?\\s*(#?\\w+)?\\s*$");
        final Matcher matcher = pattern.matcher(line);
        if (matcher.matches()) {
            return new PSystemColors(matcher.group(1));
        }
        return null;
    }

}

シーケンス図を表示するPlantUMLのコマンドは以下の辺りで実装している。

ソースコード

public class SequenceDiagramFactory extends PSystemCommandFactory {

    // ...中略 ...

    @Override
    protected List<Command> createCommands() {

        final List<Command> cmds = new ArrayList<Command>();

        addCommonCommands1(cmds);

        cmds.add(new CommandActivate());
        cmds.add(new CommandDeactivateShort());

        cmds.add(new CommandParticipantA());
        cmds.add(new CommandParticipantA2());
        cmds.add(new CommandParticipantA3());
        cmds.add(new CommandParticipantA4());
        cmds.add(new CommandArrow());
        // ... 中略 ...
    }
}

矢印でノードおよび関係を定義する正規表現は この辺り で実装していると思われる。

まとめ

  • PlantUMLの実装解析についてのとっかかりとなる部分を記載
  • src/net/sourceforge/plantuml/モジュール名 という階層構造で整理されている
  • 機能が多い分、文字解析用のモジュールが多く読み解くのは大変
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0