Search in sources :

Example 1 with ElkNode

use of org.eclipse.elk.graph.ElkNode in project sirius-components by eclipse-sirius.

the class DiagramConverterTests method testDiagramOneNodeAndOneBorderNode.

@Test
public void testDiagramOneNodeAndOneBorderNode() {
    ImageSizeProvider imageSizeProvider = new ImageSizeProvider();
    ELKDiagramConverter diagramConverter = new ELKDiagramConverter(this.textBoundsService, imageSizeProvider);
    // @formatter:off
    TestDiagramBuilder diagramBuilder = new TestDiagramBuilder();
    Node borderNode = Node.newNode(diagramBuilder.getNode(FIRST_NODE_ID)).style(diagramBuilder.getRectangularNodeStyle()).build();
    Node node = Node.newNode(diagramBuilder.getNode(SECOND_NODE_ID)).style(diagramBuilder.getImageNodeStyle()).borderNodes(List.of(borderNode)).build();
    Diagram diagram = Diagram.newDiagram(diagramBuilder.getDiagram(DIAGRAM_ID)).nodes(List.of(node)).build();
    // @formatter:on
    ELKConvertedDiagram convertedDiagram = diagramConverter.convert(diagram);
    ElkNode elkDiagram = convertedDiagram.getElkDiagram();
    assertThat(elkDiagram.getChildren().size()).isEqualTo(1);
    ElkNode elkNode = elkDiagram.getChildren().get(0);
    assertThat(elkNode.getPorts().size()).isEqualTo(1);
    ElkPort elkPort = elkNode.getPorts().get(0);
    assertThat(elkPort.getIdentifier()).isEqualTo(borderNode.getId().toString());
    imageSizeProvider.dispose();
}
Also used : ELKDiagramConverter(org.eclipse.sirius.components.diagrams.layout.ELKDiagramConverter) ELKConvertedDiagram(org.eclipse.sirius.components.diagrams.layout.ELKConvertedDiagram) ElkNode(org.eclipse.elk.graph.ElkNode) TestDiagramBuilder(org.eclipse.sirius.components.diagrams.tests.TestDiagramBuilder) ElkPort(org.eclipse.elk.graph.ElkPort) Node(org.eclipse.sirius.components.diagrams.Node) ElkNode(org.eclipse.elk.graph.ElkNode) ImageSizeProvider(org.eclipse.sirius.components.diagrams.layout.incremental.provider.ImageSizeProvider) ELKConvertedDiagram(org.eclipse.sirius.components.diagrams.layout.ELKConvertedDiagram) Diagram(org.eclipse.sirius.components.diagrams.Diagram) Test(org.junit.jupiter.api.Test)

Example 2 with ElkNode

use of org.eclipse.elk.graph.ElkNode in project sirius-components by eclipse-sirius.

the class DiagramConverterTests method testDiagramOneEdgeBetweenTwoBorderNodes.

@Test
public void testDiagramOneEdgeBetweenTwoBorderNodes() {
    ImageSizeProvider imageSizeProvider = new ImageSizeProvider();
    ELKDiagramConverter diagramConverter = new ELKDiagramConverter(this.textBoundsService, imageSizeProvider);
    // @formatter:off
    TestDiagramBuilder diagramBuilder = new TestDiagramBuilder();
    Node firstBorderNode = Node.newNode(diagramBuilder.getNode(FIRST_NODE_ID)).style(diagramBuilder.getRectangularNodeStyle()).build();
    Node secondBorderNode = Node.newNode(diagramBuilder.getNode(SECOND_NODE_ID)).style(diagramBuilder.getRectangularNodeStyle()).build();
    Node node = Node.newNode(diagramBuilder.getNode(THIRD_NODE_ID)).style(diagramBuilder.getImageNodeStyle()).borderNodes(List.of(firstBorderNode, secondBorderNode)).build();
    Edge edge = Edge.newEdge(diagramBuilder.getEdge(FIRST_EDGE_ID, firstBorderNode.getId(), secondBorderNode.getId())).build();
    Diagram diagram = Diagram.newDiagram(diagramBuilder.getDiagram(DIAGRAM_ID)).nodes(List.of(node)).edges(List.of(edge)).build();
    // @formatter:on
    ELKConvertedDiagram convertedDiagram = diagramConverter.convert(diagram);
    ElkNode elkDiagram = convertedDiagram.getElkDiagram();
    assertThat(elkDiagram.getChildren().size()).isEqualTo(1);
    assertThat(elkDiagram.getChildren().get(0)).isInstanceOf(ElkNode.class);
    assertThat(elkDiagram.getContainedEdges().size()).isEqualTo(1);
    assertThat(elkDiagram.getContainedEdges().get(0)).isInstanceOf(ElkEdge.class);
    ElkEdge elkEdge = elkDiagram.getContainedEdges().get(0);
    assertThat(elkEdge.getSources().size()).isEqualTo(1);
    assertThat(elkEdge.getSources().get(0).getIdentifier()).isEqualTo(firstBorderNode.getId().toString());
    assertThat(elkEdge.getTargets().size()).isEqualTo(1);
    assertThat(elkEdge.getTargets().get(0).getIdentifier()).isEqualTo(secondBorderNode.getId().toString());
    imageSizeProvider.dispose();
}
Also used : ELKDiagramConverter(org.eclipse.sirius.components.diagrams.layout.ELKDiagramConverter) ELKConvertedDiagram(org.eclipse.sirius.components.diagrams.layout.ELKConvertedDiagram) ElkNode(org.eclipse.elk.graph.ElkNode) TestDiagramBuilder(org.eclipse.sirius.components.diagrams.tests.TestDiagramBuilder) Node(org.eclipse.sirius.components.diagrams.Node) ElkNode(org.eclipse.elk.graph.ElkNode) ImageSizeProvider(org.eclipse.sirius.components.diagrams.layout.incremental.provider.ImageSizeProvider) Edge(org.eclipse.sirius.components.diagrams.Edge) ElkEdge(org.eclipse.elk.graph.ElkEdge) ELKConvertedDiagram(org.eclipse.sirius.components.diagrams.layout.ELKConvertedDiagram) Diagram(org.eclipse.sirius.components.diagrams.Diagram) ElkEdge(org.eclipse.elk.graph.ElkEdge) Test(org.junit.jupiter.api.Test)

Example 3 with ElkNode

use of org.eclipse.elk.graph.ElkNode in project sirius-components by eclipse-sirius.

the class DiagramConverterTests method testDiagramOneNodeAndOneEdge.

@Test
public void testDiagramOneNodeAndOneEdge() {
    ImageSizeProvider imageSizeProvider = new ImageSizeProvider();
    ELKDiagramConverter diagramConverter = new ELKDiagramConverter(this.textBoundsService, imageSizeProvider);
    // @formatter:off
    TestDiagramBuilder diagramBuilder = new TestDiagramBuilder();
    Node node = Node.newNode(diagramBuilder.getNode(FIRST_NODE_ID)).style(diagramBuilder.getImageNodeStyle()).build();
    Edge edge = Edge.newEdge(diagramBuilder.getEdge(FIRST_EDGE_ID, node.getId(), node.getId())).build();
    Diagram diagram = Diagram.newDiagram(diagramBuilder.getDiagram(DIAGRAM_ID)).nodes(List.of(node)).edges(List.of(edge)).build();
    // @formatter:on
    ELKConvertedDiagram convertedDiagram = diagramConverter.convert(diagram);
    ElkNode elkDiagram = convertedDiagram.getElkDiagram();
    assertThat(elkDiagram.getContainedEdges().size()).isEqualTo(1);
    ElkEdge elkEdge = elkDiagram.getContainedEdges().get(0);
    assertThat(elkEdge.getIdentifier()).isEqualTo(edge.getId().toString());
    imageSizeProvider.dispose();
}
Also used : ELKDiagramConverter(org.eclipse.sirius.components.diagrams.layout.ELKDiagramConverter) ELKConvertedDiagram(org.eclipse.sirius.components.diagrams.layout.ELKConvertedDiagram) ElkNode(org.eclipse.elk.graph.ElkNode) TestDiagramBuilder(org.eclipse.sirius.components.diagrams.tests.TestDiagramBuilder) Node(org.eclipse.sirius.components.diagrams.Node) ElkNode(org.eclipse.elk.graph.ElkNode) ImageSizeProvider(org.eclipse.sirius.components.diagrams.layout.incremental.provider.ImageSizeProvider) Edge(org.eclipse.sirius.components.diagrams.Edge) ElkEdge(org.eclipse.elk.graph.ElkEdge) ELKConvertedDiagram(org.eclipse.sirius.components.diagrams.layout.ELKConvertedDiagram) Diagram(org.eclipse.sirius.components.diagrams.Diagram) ElkEdge(org.eclipse.elk.graph.ElkEdge) Test(org.junit.jupiter.api.Test)

Example 4 with ElkNode

use of org.eclipse.elk.graph.ElkNode 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 5 with ElkNode

use of org.eclipse.elk.graph.ElkNode in project sirius-components by eclipse-sirius.

the class ELKDiagramConverter method convert.

@Override
public ELKConvertedDiagram convert(Diagram diagram) {
    Diagram initializedDiagram = this.initializeDiagram(diagram);
    ElkNode elkDiagram = this.convertDiagram(initializedDiagram);
    Map<String, ElkGraphElement> id2ElkGraphElements = new HashMap<>();
    Map<String, ElkConnectableShape> connectableShapeIndex = new LinkedHashMap<>();
    initializedDiagram.getNodes().stream().forEach(node -> this.convertNode(node, elkDiagram, connectableShapeIndex, id2ElkGraphElements));
    initializedDiagram.getEdges().stream().forEach(edge -> this.convertEdge(edge, elkDiagram, connectableShapeIndex, id2ElkGraphElements));
    return new ELKConvertedDiagram(elkDiagram, id2ElkGraphElements);
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ElkConnectableShape(org.eclipse.elk.graph.ElkConnectableShape) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) Diagram(org.eclipse.sirius.components.diagrams.Diagram) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ElkNode (org.eclipse.elk.graph.ElkNode)267 ElkEdge (org.eclipse.elk.graph.ElkEdge)64 ElkPort (org.eclipse.elk.graph.ElkPort)55 Test (org.junit.Test)52 KVector (org.eclipse.elk.core.math.KVector)51 ElkLabel (org.eclipse.elk.graph.ElkLabel)39 ElkPadding (org.eclipse.elk.core.math.ElkPadding)36 ArrayList (java.util.ArrayList)27 List (java.util.List)25 ElkEdgeSection (org.eclipse.elk.graph.ElkEdgeSection)24 LinkedList (java.util.LinkedList)23 BasicProgressMonitor (org.eclipse.elk.core.util.BasicProgressMonitor)22 ElkGraphElement (org.eclipse.elk.graph.ElkGraphElement)21 ElkConnectableShape (org.eclipse.elk.graph.ElkConnectableShape)17 SizeConstraint (org.eclipse.elk.core.options.SizeConstraint)15 Lists (com.google.common.collect.Lists)12 Collection (java.util.Collection)12 KVectorChain (org.eclipse.elk.core.math.KVectorChain)10 HashMap (java.util.HashMap)9 RecursiveGraphLayoutEngine (org.eclipse.elk.core.RecursiveGraphLayoutEngine)9