Search in sources :

Example 1 with GraphvizModel

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);
}
Also used : GraphvizModel(org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel) Graph(org.eclipse.elk.alg.graphviz.dot.dot.Graph) ElkNode(org.eclipse.elk.graph.ElkNode) KVector(org.eclipse.elk.core.math.KVector) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement)

Example 2 with 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));
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) Attribute(org.eclipse.elk.alg.graphviz.dot.dot.Attribute) NodeStatement(org.eclipse.elk.alg.graphviz.dot.dot.NodeStatement) Node(org.eclipse.elk.alg.graphviz.dot.dot.Node) Port(org.eclipse.elk.alg.graphviz.dot.dot.Port) EdgeStatement(org.eclipse.elk.alg.graphviz.dot.dot.EdgeStatement) EPackage(org.eclipse.emf.ecore.EPackage) GraphvizModel(org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel) Graph(org.eclipse.elk.alg.graphviz.dot.dot.Graph) AttributeStatement(org.eclipse.elk.alg.graphviz.dot.dot.AttributeStatement) EdgeTarget(org.eclipse.elk.alg.graphviz.dot.dot.EdgeTarget) Subgraph(org.eclipse.elk.alg.graphviz.dot.dot.Subgraph) Parameter(org.eclipse.xtext.Parameter)

Example 3 with GraphvizModel

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();
    }
}
Also used : GraphvizModel(org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel) ElkNode(org.eclipse.elk.graph.ElkNode) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) DotTransformationData(org.eclipse.elk.alg.graphviz.dot.transform.DotTransformationData) IDotTransformationData(org.eclipse.elk.alg.graphviz.dot.transform.IDotTransformationData) DotExporter(org.eclipse.elk.alg.graphviz.dot.transform.DotExporter)

Example 4 with GraphvizModel

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;
}
Also used : WrappedException(org.eclipse.elk.core.util.WrappedException) ForwardingInputStream(org.eclipse.elk.alg.graphviz.layouter.util.ForwardingInputStream) InputStream(java.io.InputStream) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) XtextResource(org.eclipse.xtext.resource.XtextResource) IOException(java.io.IOException) ForwardingInputStream(org.eclipse.elk.alg.graphviz.layouter.util.ForwardingInputStream) WrappedException(org.eclipse.elk.core.util.WrappedException) IOException(java.io.IOException) GraphvizModel(org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

GraphvizModel (org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel)4 Graph (org.eclipse.elk.alg.graphviz.dot.dot.Graph)2 ElkNode (org.eclipse.elk.graph.ElkNode)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Attribute (org.eclipse.elk.alg.graphviz.dot.dot.Attribute)1 AttributeStatement (org.eclipse.elk.alg.graphviz.dot.dot.AttributeStatement)1 EdgeStatement (org.eclipse.elk.alg.graphviz.dot.dot.EdgeStatement)1 EdgeTarget (org.eclipse.elk.alg.graphviz.dot.dot.EdgeTarget)1 Node (org.eclipse.elk.alg.graphviz.dot.dot.Node)1 NodeStatement (org.eclipse.elk.alg.graphviz.dot.dot.NodeStatement)1 Port (org.eclipse.elk.alg.graphviz.dot.dot.Port)1 Subgraph (org.eclipse.elk.alg.graphviz.dot.dot.Subgraph)1 DotExporter (org.eclipse.elk.alg.graphviz.dot.transform.DotExporter)1 DotTransformationData (org.eclipse.elk.alg.graphviz.dot.transform.DotTransformationData)1 IDotTransformationData (org.eclipse.elk.alg.graphviz.dot.transform.IDotTransformationData)1 ForwardingInputStream (org.eclipse.elk.alg.graphviz.layouter.util.ForwardingInputStream)1 KVector (org.eclipse.elk.core.math.KVector)1