use of org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel in project elk by eclipse.
the class DotExporter method transform.
/**
* Transforms the KGraph instance to a Dot instance using the given command.
*
* @param transData the transformation data instance
*/
public void transform(final IDotTransformationData<ElkNode, GraphvizModel> transData) {
BiMap<String, ElkGraphElement> graphElems = HashBiMap.create();
transData.setProperty(GRAPH_ELEMS, graphElems);
ElkNode elkgraph = transData.getSourceGraph();
GraphvizModel graphvizModel = DotFactory.eINSTANCE.createGraphvizModel();
Graph graph = DotFactory.eINSTANCE.createGraph();
graph.setType(GraphType.DIGRAPH);
graphvizModel.getGraphs().add(graph);
transformNodes(elkgraph, graph.getStatements(), new KVector(), transData);
transformEdges(elkgraph, graph.getStatements(), transData);
transData.getTargetGraphs().add(graphvizModel);
}
use of org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel in project elk by eclipse.
the class AbstractGraphvizDotSemanticSequencer method sequence.
@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
EPackage epackage = semanticObject.eClass().getEPackage();
ParserRule rule = context.getParserRule();
Action action = context.getAssignedAction();
Set<Parameter> parameters = context.getEnabledBooleanParameters();
if (epackage == DotPackage.eINSTANCE)
switch(semanticObject.eClass().getClassifierID()) {
case DotPackage.ATTRIBUTE:
if (rule == grammarAccess.getStatementRule() || rule == grammarAccess.getAttributeRule()) {
sequence_Attribute(context, (Attribute) semanticObject);
return;
} else if (rule == grammarAccess.getListAttributeRule()) {
sequence_ListAttribute(context, (Attribute) semanticObject);
return;
} else
break;
case DotPackage.ATTRIBUTE_STATEMENT:
sequence_AttributeStatement(context, (AttributeStatement) semanticObject);
return;
case DotPackage.EDGE_STATEMENT:
sequence_EdgeStatement(context, (EdgeStatement) semanticObject);
return;
case DotPackage.EDGE_TARGET:
sequence_EdgeTarget(context, (EdgeTarget) semanticObject);
return;
case DotPackage.GRAPH:
sequence_Graph(context, (Graph) semanticObject);
return;
case DotPackage.GRAPHVIZ_MODEL:
sequence_GraphvizModel(context, (GraphvizModel) semanticObject);
return;
case DotPackage.NODE:
sequence_Node(context, (Node) semanticObject);
return;
case DotPackage.NODE_STATEMENT:
sequence_NodeStatement(context, (NodeStatement) semanticObject);
return;
case DotPackage.PORT:
sequence_Port(context, (Port) semanticObject);
return;
case DotPackage.SUBGRAPH:
sequence_Subgraph(context, (Subgraph) semanticObject);
return;
}
if (errorAcceptor != null)
errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
use of org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel in project elk by eclipse.
the class GraphvizLayoutProvider method layout.
@Override
public void layout(final ElkNode parentNode, final IElkProgressMonitor progressMonitor) {
if (command == Command.INVALID) {
throw new IllegalStateException("The Graphviz layout provider is not initialized.");
}
progressMonitor.begin("Graphviz layout (" + command + ")", 2);
if (parentNode.getChildren().isEmpty()) {
// return if there is nothing in this node
progressMonitor.done();
return;
}
boolean debugMode = parentNode.getProperty(CoreOptions.DEBUG_MODE);
myCallNo = ++serialCallNo;
// start the graphviz process, or retrieve the previously used process
graphvizTool.initialize();
// create an Xtext resource set for parsing and serialization
XtextResourceSet resourceSet = (XtextResourceSet) dotResourceSetProvider.createResourceSet();
// create the dot exporter we'll be using
DotExporter dotExporter = new LayoutDotExporter();
// translate the KGraph to Graphviz and write to the process
IDotTransformationData<ElkNode, GraphvizModel> transData = new DotTransformationData<ElkNode, GraphvizModel>();
transData.setSourceGraph(parentNode);
transData.setProperty(DotExporter.COMMAND, command);
dotExporter.transform(transData);
GraphvizModel graphvizInput = transData.getTargetGraphs().get(0);
writeDotGraph(graphvizInput, progressMonitor.subTask(1), debugMode, resourceSet);
try {
// read Graphviz output and apply layout information to the KGraph
GraphvizModel graphvizOutput = readDotGraph(progressMonitor.subTask(1), debugMode, resourceSet);
transData.getTargetGraphs().set(0, graphvizOutput);
dotExporter.transferLayout(transData);
} finally {
boolean reuseProcess = GraphvizLayouterPreferenceStoreAccess.getUISaveBoolean(PREF_GRAPHVIZ_REUSE_PROCESS, REUSE_PROCESS_DEFAULT);
graphvizTool.cleanup(reuseProcess ? Cleanup.NORMAL : Cleanup.STOP);
progressMonitor.done();
}
}
use of org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel in project elk by eclipse.
the class GraphvizLayoutProvider method readDotGraph.
/**
* Reads and parses a serialized Graphviz model.
*
* @param monitor
* a monitor to which progress is reported
* @param debugMode
* whether debug mode is active
* @param resourceSet
* the resoure set for parsing
* @return an instance of the parsed graphviz model
*/
private GraphvizModel readDotGraph(final IElkProgressMonitor monitor, final boolean debugMode, final XtextResourceSet resourceSet) {
monitor.begin("Parse output", 1);
InputStream inputStream = graphvizTool.output();
// enable debug output if needed
FileOutputStream debugStream = null;
if (debugMode) {
try {
String path = System.getProperty("user.home");
if (path.endsWith(File.separator)) {
path += "tmp" + File.separator + "graphviz";
} else {
path += File.separator + "tmp" + File.separator + "graphviz";
}
new File(path).mkdirs();
debugStream = new FileOutputStream(new File(path + File.separator + debugFileBase() + "-out.dot"));
inputStream = new ForwardingInputStream(inputStream, debugStream);
} catch (Exception exception) {
System.out.println("GraphvizLayouter: Could not initialize debug output: " + exception.getMessage());
}
}
// parse the output stream of the dot process
XtextResource resource = (XtextResource) resourceSet.createResource(URI.createURI("input.graphviz_dot"));
try {
resource.load(inputStream, null);
} catch (IOException exception) {
graphvizTool.cleanup(Cleanup.ERROR);
throw new WrappedException("Failed to read Graphviz output.", exception);
} finally {
if (debugStream != null) {
try {
debugStream.close();
} catch (IOException exception) {
// ignore exception
}
}
}
// analyze errors and retrieve parse result
if (!resource.getErrors().isEmpty()) {
StringBuilder errorString = new StringBuilder("Errors in Graphviz output:");
for (Diagnostic diagnostic : resource.getErrors()) {
errorString.append("\n" + diagnostic.getLine() + ": " + diagnostic.getMessage());
}
graphvizTool.cleanup(Cleanup.ERROR);
throw new GraphvizException(errorString.toString());
}
GraphvizModel graphvizModel = (GraphvizModel) resource.getParseResult().getRootASTElement();
if (graphvizModel == null || graphvizModel.getGraphs().isEmpty()) {
graphvizTool.cleanup(Cleanup.ERROR);
throw new GraphvizException("No output from the Graphviz process." + " Try increasing the timeout value in the Eclipse Diagram Layout preferences.");
}
monitor.done();
return graphvizModel;
}
Aggregations