1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-02 00:20:49 +00:00
plantuml/src/net/sourceforge/plantuml/bpm/BpmDiagram.java

201 lines
6.3 KiB
Java
Raw Normal View History

2017-03-21 21:37:59 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2017-03-21 21:37:59 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2017-03-21 21:37:59 +00:00
*
* If you like this project or if you find it useful, you can support us at:
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2017-03-21 21:37:59 +00:00
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*/
package net.sourceforge.plantuml.bpm;
import java.io.IOException;
import java.io.OutputStream;
2017-04-05 17:37:42 +00:00
import java.util.ArrayDeque;
2017-03-21 21:37:59 +00:00
import java.util.ArrayList;
2017-04-05 17:37:42 +00:00
import java.util.Deque;
2017-03-21 21:37:59 +00:00
import java.util.List;
2023-02-22 18:43:48 +00:00
import net.atmp.ImageBuilder;
2017-03-21 21:37:59 +00:00
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.UmlDiagram;
import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.ImageData;
2021-05-23 15:35:13 +00:00
import net.sourceforge.plantuml.core.UmlSource;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.shape.TextBlock;
import net.sourceforge.plantuml.klimt.shape.UDrawable;
import net.sourceforge.plantuml.skin.SkinParam;
import net.sourceforge.plantuml.skin.UmlDiagramType;
2017-03-21 21:37:59 +00:00
public class BpmDiagram extends UmlDiagram {
2023-02-28 21:22:51 +00:00
// ::remove folder when __CORE__
2017-03-21 21:37:59 +00:00
2017-04-19 18:30:16 +00:00
private void cleanGrid(Grid grid) {
while (true) {
final boolean v1 = new CleanerEmptyLine().clean(grid);
final boolean v2 = new CleanerInterleavingLines().clean(grid);
2017-05-10 19:51:15 +00:00
final boolean v3 = new CleanerMoveBlock().clean(grid);
if (v1 == false && v2 == false && v3 == false) {
2017-04-19 18:30:16 +00:00
return;
}
}
}
2017-03-21 21:37:59 +00:00
private final BpmElement start = new BpmElement(null, BpmElementType.START);
2021-05-14 08:42:57 +00:00
private List<BpmEvent> events = new ArrayList<>();
private Deque<BpmBranch> branches = new ArrayDeque<>();
2017-03-21 21:37:59 +00:00
public DiagramDescription getDescription() {
return new DiagramDescription("(Bpm Diagram)");
}
2022-09-18 17:08:06 +00:00
public BpmDiagram(UmlSource source) {
super(source, UmlDiagramType.BPM, null);
2017-03-21 21:37:59 +00:00
}
@Override
public ImageBuilder createImageBuilder(FileFormatOption fileFormatOption) throws IOException {
2022-02-10 18:16:18 +00:00
return super.createImageBuilder(fileFormatOption).annotations(false);
}
2017-03-21 21:37:59 +00:00
@Override
protected ImageData exportDiagramInternal(OutputStream os, int index, FileFormatOption fileFormatOption)
throws IOException {
2022-02-10 18:16:18 +00:00
return createImageBuilder(fileFormatOption).drawable(getUDrawable()).write(os);
2017-03-21 21:37:59 +00:00
}
private UDrawable getUDrawable() {
final Grid grid = createGrid();
2017-04-05 17:37:42 +00:00
cleanGrid(grid);
2022-09-18 17:08:06 +00:00
final GridArray gridArray = grid.toArray(SkinParam.create(getUmlDiagramType()));
2017-04-05 17:37:42 +00:00
// gridArray.addEdges(edges);
2019-09-14 18:12:04 +00:00
// System.err.println("gridArray=" + gridArray);
2017-03-21 21:37:59 +00:00
return gridArray;
}
public CommandExecutionResult addEvent(BpmEvent event) {
this.events.add(event);
return CommandExecutionResult.ok();
}
private Coord current;
2017-04-05 17:37:42 +00:00
private Cell last;
2017-03-21 21:37:59 +00:00
private Grid createGrid() {
final Grid grid = new Grid();
this.current = grid.getRoot();
2017-04-05 17:37:42 +00:00
// this.edges.clear();
last = grid.getCell(current);
2017-03-21 21:37:59 +00:00
grid.getCell(current).setData(start);
for (BpmEvent event : events) {
if (event instanceof BpmEventAdd) {
2017-04-05 17:37:42 +00:00
final BpmEventAdd tmp = (BpmEventAdd) event;
addInGrid(grid, tmp.getElement());
2017-03-21 21:37:59 +00:00
} else if (event instanceof BpmEventResume) {
final String idDestination = ((BpmEventResume) event).getId();
current = grid.getById(idDestination);
2017-04-05 17:37:42 +00:00
last = grid.getCell(current);
2017-03-21 21:37:59 +00:00
if (last == null) {
throw new IllegalStateException();
}
final Navigator<Line> nav = grid.linesOf(current);
final Line newLine = new Line();
nav.insertAfter(newLine);
2017-04-05 17:37:42 +00:00
final Col row = current.getCol();
current = new Coord(newLine, row);
2017-03-21 21:37:59 +00:00
} else if (event instanceof BpmEventGoto) {
2017-04-05 17:37:42 +00:00
final BpmEventGoto tmp = (BpmEventGoto) event;
final String idDestination = tmp.getId();
2017-03-21 21:37:59 +00:00
current = grid.getById(idDestination);
2017-04-05 17:37:42 +00:00
final Cell src = last;
last = grid.getCell(current);
2017-03-21 21:37:59 +00:00
if (last == null) {
throw new IllegalStateException();
}
final Navigator<Line> nav = grid.linesOf(current);
final Line newLine = new Line();
nav.insertAfter(newLine);
2017-04-05 17:37:42 +00:00
final Col row = current.getCol();
current = new Coord(newLine, row);
2017-04-19 18:30:16 +00:00
src.addConnectionTo2(last.getData());
2017-03-21 21:37:59 +00:00
} else {
throw new IllegalStateException();
}
}
2017-04-05 17:37:42 +00:00
grid.addConnections();
// for (GridEdge edge : edges) {
// System.err.println("EDGE=" + edge.getEdgeDirection());
// edge.addLineIn(grid);
// }
// grid.addEdge(edges);
2017-03-21 21:37:59 +00:00
return grid;
}
private void addInGrid(Grid grid, BpmElement element) {
2017-04-05 17:37:42 +00:00
final Navigator<Col> nav = grid.colsOf(current);
final Col newRow = new Col();
2017-03-21 21:37:59 +00:00
nav.insertAfter(newRow);
2017-04-05 17:37:42 +00:00
current = new Coord(current.getLine(), newRow);
2017-03-21 21:37:59 +00:00
grid.getCell(current).setData(element);
2017-04-19 18:30:16 +00:00
last.addConnectionTo2(grid.getCell(current).getData());
2017-04-05 17:37:42 +00:00
last = grid.getCell(current);
}
public CommandExecutionResult newBranch() {
final BpmBranch branch = new BpmBranch(events.size());
branches.addLast(branch);
return addEvent(new BpmEventAdd(branch.getEntryElement()));
}
public CommandExecutionResult elseBranch() {
final BpmBranch branch = branches.getLast();
final int counter = branch.incAndGetCounter();
if (counter == 2) {
addEvent(new BpmEventAdd(branch.getElseElement()));
return addEvent(branch.getResumeEntryEvent());
}
addEvent(branch.getGoToEndEvent());
return addEvent(branch.getResumeEntryEvent());
}
2017-03-21 21:37:59 +00:00
2017-04-05 17:37:42 +00:00
public CommandExecutionResult endBranch() {
final BpmBranch branch = branches.removeLast();
return addEvent(branch.getGoToEndEvent());
2017-03-21 21:37:59 +00:00
}
2023-02-22 18:43:48 +00:00
@Override
2023-12-11 17:34:23 +00:00
protected TextBlock getTextMainBlock(FileFormatOption fileFormatOption) {
2023-02-22 18:43:48 +00:00
throw new UnsupportedOperationException();
}
2017-03-21 21:37:59 +00:00
}