Skip to content

Commit

Permalink
Merge pull request #47 from eclipse/jk/gh46
Browse files Browse the repository at this point in the history
[layout] allow to dump ELK tree for debugging
  • Loading branch information
JanKoehnlein authored Sep 25, 2019
2 parents 0d0d245 + 8e9d099 commit 2df8db8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
* <p>The layout engine must be initialized once during the lifecycle of the application by calling
* {@link #initialize(ILayoutMetaDataProvider...)}. The arguments of that method should be all meta data
* providers of the layout algorithms that should be used by this layout engine,
* e.g. {@link org.eclipse.elk.alg.layered.options.LayeredMetaDataProvider}.</p>
* e.g. <code>org.eclipse.elk.alg.layered.options.LayeredMetaDataProvider</code>.</p>
*/
public class ElkLayoutEngine implements ILayoutEngine {

Expand Down Expand Up @@ -337,7 +337,7 @@ protected void applyBounds(BoundsAware bounds, ElkShape elkShape) {
* This requires the meta data providers of the referenced algorithms to be registered
* using {@link #initialize(ILayoutMetaDataProvider...)} before any layout is performed, e.g. on
* application start. Alternatively, you can use a specific layout algorithm directly, e.g.
* {@link org.eclipse.elk.alg.layered.LayeredLayoutProvider}.
* <code>org.eclipse.elk.alg.layered.LayeredLayoutProvider</code>.
*/
public void setEngine(IGraphLayoutEngine engine) {
if (engine == null)
Expand Down Expand Up @@ -557,5 +557,4 @@ public LayoutContext(Action cause) {
this.cause = cause;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/********************************************************************************
* Copyright (c) 2019 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
package org.eclipse.sprotty.layout;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Method;
import java.nio.charset.Charset;

import javax.inject.Inject;
import javax.inject.Provider;

import org.apache.log4j.Logger;
import org.eclipse.elk.graph.ElkNode;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;


/**
* Allows to dump an ELK model in ELKT, such that it can be debugged in
* https://rtsys.informatik.uni-kiel.de/elklive/elkgraph.html
*
* This class needs the Maven artifact
* groupID: org.eclipse.elk
* artifactId: org.eclipse.elk.graph.text
* on the classpath.
*/
public class ElktSerializer {

private static final Logger LOG = Logger.getLogger(ElktSerializer.class);

protected Provider<ResourceSetImpl> resourceSetProvider;

@Inject
public void setResourceSetProvider(Provider<ResourceSetImpl> resourceSetProvider) {
this.resourceSetProvider = resourceSetProvider;
}

/**
* Serlialize the given ELK model to ELKT
*/
public String toElkt(ElkNode node) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(32768)) {
registerElkt();
Resource resource = resourceSetProvider.get().createResource(URI.createURI("dump.elkt"));
resource.getContents().add(node);
resource.save(baos, null);
String text = new String(baos.toByteArray(), Charset.defaultCharset());
return text;
} catch (Throwable exc) {
LOG.error("Error serializing ELK model", exc);
}
return null;
}

/**
* Makes sure ELKT is registered to the EMF registries.
*/
protected void registerElkt() throws Exception {
Object factory = Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().get("elkt");
if (factory == null) {
Class<?> standaloneSetupClass = Class.forName("org.eclipse.elk.graph.text.ElkGraphStandaloneSetup");
if (standaloneSetupClass == null)
throw new IllegalStateException("'ElkGraphStandaloneSetup' is not on the classpath. Add 'org.eclipse.elk:org.eclipse.elk.graph.text' to your classpath.");
Method doSetupMethod = standaloneSetupClass.getMethod("doSetup");
doSetupMethod.invoke(null);
}
}
}

0 comments on commit 2df8db8

Please sign in to comment.