1
votes

I have an application which produces a GraphViz dot file for a subgraph of my Neo4j database. It works like a charm, but there is somewhat of an issue.

Right now, the title of each node is the node id. Then the properties are listed, with their respective types. This is more information than I need and I would like to change the way the GraphViz writer is configured.

I noticed several classes/interfaces such as GraphStyle, StyleParameter, StyleConfiguration but I've tried several things and keep running into the issue that I cannot access certain classes/interfaces outside of their respective package. Maybe I'm doing it wrong, maybe it's designed so users cannot reconfigure the GraphViz writer, I don't know but I'd like to know.

How do I reconfigure the GraphViz writer so the dot file contains only that information which I want it to contain, namely a property of my choosing as the title, and nothing else as far as the nodes are concerned. Also, this is not always the same property, so for some nodes I'd like property A to be the title, and for nodes that don't have property A, I'd like property B to be the title.

Any help would be greatly appreciated.

2

2 Answers

0
votes

I managed to get it to work. First of all, you need to create two new classes:

class NodeStyleImpl implements NodeStyle
class RelationshipStyleImpl implements RelationshipStyle

Here you can define how nodes and relations should be written in the dot notation. An example implementation looks like this :

public class NodeStyleImpl implements NodeStyle {

    public void emitNodeStart(Appendable apndbl, Node node) throws IOException {
        apndbl.append("  N" + node.getId() + " [\n    label = \"");
    }

    public void emitEnd(Appendable apndbl) throws IOException {
        apndbl.append("\"\n]\n");
    }

    public void emitProperty(Appendable apndbl, String propkey, Object propvalue) throws IOException {
        if(propkey.equals("propkeyone") || propkey.equals("propkeytwo"){
            apndbl.append(propvalue.toString());
        }
    }
}

In an analog fashion, you can write the RelationshipStyleImpl. If you're looking for more advanced configuration, you can also write a StyleConfiguration implementation. You can look at the default implementations in the Neo4j code for an example.

Then there's the issue with the GraphStyle class. The GraphStyle class has a constructor which is protected, thus only accessible from within the package. I made a pull request to change it to public but for the moment, here's a little "hack" which provides a workaround.

package org.neo4j.visualization.graphviz

public class GraphStyleImpl extends GraphStyle {

    private GraphStyleImpl (NodeStyleImpl nstyle, RelationshipStyleImpl rstyle) {
        super(nstyle, rstyle);
    }
}

Note the package declaration. Because the GraphStyle constructor is protected, the super(nstyle, rstyle) method is only accessible from within the same package. By extending the class with a new public constructor, you can now do the following:

GraphStyle graphstyle = new GraphStyleImpl(new NodeStyleImpl(), new RelationshipStyleImpl());
GraphvizWriter writer = new GraphvizWriter(graphstyle);

If my pull request gets accepted, the use of the GraphStyleImpl class will no longer be necessary.