use of org.eclipse.elk.core.util.BasicProgressMonitor in project sirius-components by eclipse-sirius.
the class LayoutService method layout.
@Override
public Diagram layout(IEditingContext editingContext, Diagram diagram) {
ELKConvertedDiagram convertedDiagram = this.elkDiagramConverter.convert(diagram);
ElkNode elkDiagram = convertedDiagram.getElkDiagram();
// @formatter:off
var optionalDiagramDescription = this.representationDescriptionSearchService.findById(editingContext, diagram.getDescriptionId()).filter(DiagramDescription.class::isInstance).map(DiagramDescription.class::cast);
// @formatter:on
ISiriusWebLayoutConfigurator layoutConfigurator;
if (optionalDiagramDescription.isPresent()) {
var diagramDescription = optionalDiagramDescription.get();
elkDiagram = this.layoutConfiguratorRegistry.applyBeforeLayout(elkDiagram, editingContext, diagram, diagramDescription);
layoutConfigurator = this.layoutConfiguratorRegistry.getLayoutConfigurator(diagram, diagramDescription);
} else {
layoutConfigurator = this.layoutConfiguratorRegistry.getDefaultLayoutConfigurator();
}
ElkUtil.applyVisitors(elkDiagram, layoutConfigurator);
IGraphLayoutEngine engine = new RecursiveGraphLayoutEngine();
engine.layout(elkDiagram, new BasicProgressMonitor());
if (optionalDiagramDescription.isPresent()) {
var diagramDescription = optionalDiagramDescription.get();
elkDiagram = this.layoutConfiguratorRegistry.applyAfterLayout(elkDiagram, editingContext, diagram, diagramDescription);
}
Map<String, ElkGraphElement> id2ElkGraphElements = convertedDiagram.getId2ElkGraphElements();
Diagram layoutedDiagram = this.elkLayoutedDiagramProvider.getLayoutedDiagram(diagram, elkDiagram, id2ElkGraphElements);
if (this.logger.isDebugEnabled()) {
// @formatter:off
String json = ElkGraphJson.forGraph(elkDiagram).omitLayout(true).omitZeroDimension(true).omitZeroPositions(true).shortLayoutOptionKeys(false).prettyPrint(true).toJson();
// @formatter:on
this.logger.debug(json);
}
return layoutedDiagram;
}
use of org.eclipse.elk.core.util.BasicProgressMonitor in project osate2 by osate.
the class DiagramElementLayoutUtil method layout.
private static void layout(final DiagramModification m, final Collection<? extends DiagramNode> nodesToLayout, final StyleProvider styleProvider, final LayoutInfoProvider layoutInfoProvider, final LayoutOptions options) {
Objects.requireNonNull(nodesToLayout, "nodesToLayout must not be null");
try {
// Layout the nodes
final RecursiveGraphLayoutEngine layoutEngine = new RecursiveGraphLayoutEngine();
for (final DiagramNode dn : nodesToLayout) {
LayoutMapping mapping;
ElkNode layoutGraph;
// Perform the first layout. This layout will not include nested ports. This will allow ELK additional flexibility when determining port
// placement.
mapping = ElkGraphBuilder.buildLayoutGraph(dn, styleProvider, layoutInfoProvider, options, !options.layoutPortsOnDefaultSides, ElkGraphBuilder.FixedPortPositionProvider.NO_OP);
layoutGraph = mapping.getLayoutGraph();
layoutGraph.setProperty(CoreOptions.ALGORITHM, LAYOUT_ALGORITHM);
applyProperties(dn, mapping);
LayoutDebugUtil.saveElkGraphToDebugProject(layoutGraph, "pass1");
layoutEngine.layout(layoutGraph, new BasicProgressMonitor());
// nested ports and performing edge routing.
if (layoutGraph.getProperty(AgeLayoutOptions.NESTED_PORTS_WERE_OMITTED)) {
final LayoutMapping initialLayoutMapping = mapping;
mapping = ElkGraphBuilder.buildLayoutGraph(dn, styleProvider, layoutInfoProvider, options, false, new ElkGraphBuilder.FixedPortPositionProvider() {
@Override
public PortSide getPortSide(final DiagramElement de) {
final ElkGraphElement ge = initialLayoutMapping.getGraphMap().inverse().get(de);
if (ge instanceof ElkPort) {
return ge.getProperty(CoreOptions.PORT_SIDE);
}
return null;
}
@Override
public Double getPortPosition(final DiagramElement de) {
final ElkGraphElement ge = initialLayoutMapping.getGraphMap().inverse().get(de);
if (ge instanceof ElkPort) {
final ElkPort port = (ElkPort) ge;
final PortSide ps = port.getProperty(CoreOptions.PORT_SIDE);
if (PortSide.SIDES_EAST_WEST.contains(ps)) {
return port.getY();
} else {
return port.getX();
}
}
return null;
}
});
layoutGraph = mapping.getLayoutGraph();
layoutGraph.setProperty(CoreOptions.ALGORITHM, LAYOUT_ALGORITHM);
applyProperties(dn, mapping);
LayoutDebugUtil.saveElkGraphToDebugProject(layoutGraph, "pass2");
layoutEngine.layout(layoutGraph, new BasicProgressMonitor());
}
LayoutDebugUtil.saveElkGraphToDebugProject(layoutGraph, "final");
applyShapeLayout(mapping, m);
applyConnectionLayout(mapping, m);
// Layout feature self loop connections. These are omitted from the ELK based layout.
dn.getAllDiagramNodes().filter(DiagramElementLayoutUtil::isFeatureSelfLoopConnection).map(DiagramElement.class::cast).forEachOrdered(de -> layoutFeatureSelfLoopConnection(de, m, layoutInfoProvider));
}
} catch (final RuntimeException ex) {
// If a layout error occurs, display the exception but do not rethrow. This is so that the operation that attempted to do the layout will continue.
// This is important because otherwise simple operations such a adding elements to the diagram will completely fail. Suppressing the error will
// degrade performance but allow the user to keep working and should ensure things stay in a valid state.
// It would be best for other parts of the code to handle exceptions properly to avoid entering into an invalid state but this is the best
// workaround.
final Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "A layout error occured.", ex);
StatusManager.getManager().handle(status, StatusManager.SHOW | StatusManager.LOG);
}
}
use of org.eclipse.elk.core.util.BasicProgressMonitor in project elk by eclipse.
the class NetworkSimplexTest method testDeltas.
@Test
public void testDeltas() {
for (int j = 0; j < 5; ++j) {
@SuppressWarnings("unused") long start = System.currentTimeMillis();
// Integer.MAX_VALUE
int n = 5;
for (int i = 0; i < n; ++i) {
NGraph graph = generateRandomGraph();
Assert.assertTrue(graph.isAcyclic());
NetworkSimplex.forGraph(graph).execute(new BasicProgressMonitor());
for (NNode node : graph.nodes) {
for (NEdge e : node.getOutgoingEdges()) {
Assert.assertTrue("Valid delta", e.getTarget().layer - e.getSource().layer >= e.delta);
}
}
}
}
}
use of org.eclipse.elk.core.util.BasicProgressMonitor in project elk by eclipse.
the class ElkLayered method doCompoundLayout.
// //////////////////////////////////////////////////////////////////////////////
// Compound Graph Layout
/**
* Does a layout on the given compound graph. Connected components processing is currently not
* supported.
*
* @param lgraph the graph to layout
* @param monitor a progress monitor to show progress information in, or {@code null}
*/
public void doCompoundLayout(final LGraph lgraph, final IElkProgressMonitor monitor) {
IElkProgressMonitor theMonitor = monitor;
if (theMonitor == null) {
theMonitor = new BasicProgressMonitor().withMaxHierarchyLevels(0);
}
// SUPPRESS CHECKSTYLE MagicNumber
theMonitor.begin("Layered layout", 2);
// Preprocess the compound graph by splitting cross-hierarchy edges
notifyProcessorReady(lgraph, compoundGraphPreprocessor);
compoundGraphPreprocessor.process(lgraph, theMonitor.subTask(1));
notifyProcessorFinished(lgraph, compoundGraphPreprocessor);
hierarchicalLayout(lgraph, theMonitor.subTask(1));
// Postprocess the compound graph by combining split cross-hierarchy edges
notifyProcessorReady(lgraph, compoundGraphPostprocessor);
compoundGraphPostprocessor.process(lgraph, theMonitor.subTask(1));
notifyProcessorFinished(lgraph, compoundGraphPostprocessor);
theMonitor.done();
}
use of org.eclipse.elk.core.util.BasicProgressMonitor in project elk by eclipse.
the class ElkLayered method doLayout.
// //////////////////////////////////////////////////////////////////////////////
// Regular Layout
/**
* Does a layout on the given graph. If the graph contains compound nodes (see class documentation),
* the nested graphs are ignored.
*
* @param lgraph the graph to layout
* @param monitor a progress monitor to show progress information in, or {@code null}
* @see #doCompoundLayout(LGraph, IElkProgressMonitor)
*/
public void doLayout(final LGraph lgraph, final IElkProgressMonitor monitor) {
IElkProgressMonitor theMonitor = monitor;
if (theMonitor == null) {
theMonitor = new BasicProgressMonitor().withMaxHierarchyLevels(0);
}
theMonitor.begin("Layered layout", 1);
// Update the modules depending on user options
graphConfigurator.prepareGraphForLayout(lgraph);
// Split the input graph into components and perform layout on them
List<LGraph> components = componentsProcessor.split(lgraph);
if (components.size() == 1) {
// Execute layout on the sole component using the top-level progress monitor
layout(components.get(0), theMonitor);
} else {
// Execute layout on each component using a progress monitor subtask
float compWork = 1.0f / components.size();
for (LGraph comp : components) {
if (monitor.isCanceled()) {
return;
}
layout(comp, theMonitor.subTask(compWork));
}
}
componentsProcessor.combine(components, lgraph);
// Resize the resulting graph, according to minimal size constraints and such
resizeGraph(lgraph);
theMonitor.done();
}
Aggregations