Search in sources :

Example 1 with BasicProgressMonitor

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;
}
Also used : IGraphLayoutEngine(org.eclipse.elk.core.IGraphLayoutEngine) ElkNode(org.eclipse.elk.graph.ElkNode) RecursiveGraphLayoutEngine(org.eclipse.elk.core.RecursiveGraphLayoutEngine) BasicProgressMonitor(org.eclipse.elk.core.util.BasicProgressMonitor) DiagramDescription(org.eclipse.sirius.components.diagrams.description.DiagramDescription) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) IncrementalLayoutConvertedDiagram(org.eclipse.sirius.components.diagrams.layout.incremental.IncrementalLayoutConvertedDiagram) Diagram(org.eclipse.sirius.components.diagrams.Diagram)

Example 2 with BasicProgressMonitor

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);
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) RecursiveGraphLayoutEngine(org.eclipse.elk.core.RecursiveGraphLayoutEngine) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) BasicProgressMonitor(org.eclipse.elk.core.util.BasicProgressMonitor) PortSide(org.eclipse.elk.core.options.PortSide) LayoutMapping(org.eclipse.elk.core.service.LayoutMapping) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement)

Example 3 with BasicProgressMonitor

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);
                }
            }
        }
    }
}
Also used : BasicProgressMonitor(org.eclipse.elk.core.util.BasicProgressMonitor) Test(org.junit.Test)

Example 4 with BasicProgressMonitor

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();
}
Also used : IElkProgressMonitor(org.eclipse.elk.core.util.IElkProgressMonitor) BasicProgressMonitor(org.eclipse.elk.core.util.BasicProgressMonitor)

Example 5 with BasicProgressMonitor

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();
}
Also used : IElkProgressMonitor(org.eclipse.elk.core.util.IElkProgressMonitor) BasicProgressMonitor(org.eclipse.elk.core.util.BasicProgressMonitor) LGraph(org.eclipse.elk.alg.layered.graph.LGraph)

Aggregations

BasicProgressMonitor (org.eclipse.elk.core.util.BasicProgressMonitor)38 Test (org.junit.Test)23 ElkNode (org.eclipse.elk.graph.ElkNode)20 ElkPadding (org.eclipse.elk.core.math.ElkPadding)15 RecursiveGraphLayoutEngine (org.eclipse.elk.core.RecursiveGraphLayoutEngine)10 IElkProgressMonitor (org.eclipse.elk.core.util.IElkProgressMonitor)7 LGraph (org.eclipse.elk.alg.layered.graph.LGraph)4 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 LNode (org.eclipse.elk.alg.layered.graph.LNode)2 LayerSweepCrossingMinimizer (org.eclipse.elk.alg.layered.p3order.LayerSweepCrossingMinimizer)2 IGraphLayoutEngine (org.eclipse.elk.core.IGraphLayoutEngine)2 LayoutMetaDataService (org.eclipse.elk.core.data.LayoutMetaDataService)2 ElkGraphElement (org.eclipse.elk.graph.ElkGraphElement)2 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)2 Set (java.util.Set)1 CGroup (org.eclipse.elk.alg.common.compaction.oned.CGroup)1 CNode (org.eclipse.elk.alg.common.compaction.oned.CNode)1 NGraph (org.eclipse.elk.alg.common.networksimplex.NGraph)1 NNode (org.eclipse.elk.alg.common.networksimplex.NNode)1