PortoutRanking.java

/* ========================================================================
 * PlantUML : a free UML diagram generator
 * ========================================================================
 *
 * (C) Copyright 2009-2024, Arnaud Roques
 *
 * Project Info:  https://plantuml.com
 *
 * If you like this project or if you find it useful, you can support us at:
 *
 * https://plantuml.com/patreon (only 1$ per month!)
 * https://plantuml.com/paypal
 *
 * 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.svek;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.plantuml.abel.EntityPosition;
import net.sourceforge.plantuml.klimt.font.StringBounder;

/**
 * Ranking of the portouts of a cluster, shared by {@link ClusterDotString} and
 * {@link ClusterDotStringKermor} (issues #2457, #1602, #2032).
 * <p>
 * {rank=sink} is a version-dependent post-pass in graphviz: it can silently
 * violate outgoing minlen edges (the last portout-&gt;za link), and older
 * graphviz versions resolve that conflict differently, leaving portouts at an
 * arbitrary height inside their container. For a cluster that really holds
 * portouts over content, emit plain {rank=same} (the documented pattern: the
 * chain only orders the ports left to right) plus invisible minlen edges from
 * every content leaf of the subtree: those have the same, unambiguous meaning
 * on every graphviz version. The za link is not emitted: it was violated under
 * rank=sink anyway, and honouring it would drag the title node below the
 * ports.
 * <p>
 * Content covers the plain leaves of the cluster and every leaf below it: a
 * nested cluster of ports only still occupies the container and must push the
 * outer portouts to its border. Clusters without content, or without any
 * portout, are left to the caller, which keeps its historical {rank=sink}
 * emission untouched (zero diff on simple port-only diagrams such as SVG0005).
 */
final class PortoutRanking {

	private PortoutRanking() {
	}

	/**
	 * Emits the portout ranking of {@code cluster} when it applies.
	 *
	 * @return {@code false} when nothing was written: the caller then emits its
	 *         usual {rank=sink} group for {@code outputs}
	 */
	static boolean print(Cluster cluster, List<SvekNode> outputs, StringBuilder sb, StringBounder stringBounder) {
		if (containsPortout(outputs) == false)
			return false;

		final List<SvekNode> content = new ArrayList<>();
		collectContentNodes(cluster, content, true);
		if (content.isEmpty())
			return false;

		sb.append("{rank=same;");
		for (SvekNode sh : outputs)
			sb.append(sh.getUid() + ";");

		sb.append("}");
		SvekUtils.println(sb);
		for (SvekNode sh : outputs)
			sh.appendShape(sb, stringBounder);

		SvekUtils.println(sb);
		boolean arrow = false;
		for (SvekNode sh : outputs) {
			if (arrow)
				sb.append("->");

			arrow = true;
			sb.append(sh.getUid());
		}
		sb.append(" [arrowhead=none];");
		SvekUtils.println(sb);
		for (SvekNode inside : content)
			for (SvekNode port : outputs)
				if (port.getEntityPosition() == EntityPosition.PORTOUT) {
					sb.append(inside.getUid() + "->" + port.getUid() + " [style=invis];");
					SvekUtils.println(sb);
				}

		return true;
	}

	private static boolean containsPortout(List<SvekNode> entries) {
		for (SvekNode sh : entries)
			if (sh.getEntityPosition() == EntityPosition.PORTOUT)
				return true;

		return false;
	}

	// The own-level ports are the objects being placed, not content, so only the
	// plain leaves of this cluster count here; below it every leaf counts,
	// whatever its position: a port-only child cluster still occupies layout
	// space.
	private static void collectContentNodes(Cluster cl, List<SvekNode> result, boolean ownLevel) {
		for (SvekNode sh : cl.getNodes())
			if (ownLevel == false || sh.getEntityPosition() == EntityPosition.NORMAL)
				result.add(sh);

		for (Cluster child : cl.getChildren())
			collectContentNodes(child, result, false);
	}

}