I can't find any documentation on how to use the CSVImporter (1.5.0). I have a very simple csv file with integers that I'm trying to import using the following code:
Graph<String, DefaultEdge> wpCategories = new DirectedMultigraph(DefaultEdge.class);
CSVImporter<String, DefaultEdge> importer = new CSVImporter(CSVFormat.EDGE_LIST);
importer.importGraph(wpCategories, new File("hypernymGraphWithEntities_WP1-small.csv"));
I just get a "The graph contains no vertex supplier" exception. How do I create a vertex supplier?
A JGraphT graph consists of vertex and edge objects. When importing a graph from a text file, the importer must somehow create vertex objects for every vertex it encounters in the text file. These objects must be of the same type you defined in the graph. To generate these objects, JGraphT uses vertex suppliers.
Various examples of how to use the CSV importer can be found in the corresponding test class CSVImporterTest.
There are two different ways to create a graph with a vertex supplier. Either you use the GraphTypeBuilder
, or you use one of the graph constructors. Here's an example for a directed graph.
//Builder
Graph<String,DefaultEdge> g1 = GraphTypeBuilder.directed().allowingMultipleEdges(false).allowingSelfLoops(false).weighted(false).edgeClass(DefaultEdge.class).vertexSupplier(SupplierUtil.createStringSupplier(1)).buildGraph();
//Constructor
Graph<String,DefaultEdge> g2 = new DefaultDirectedGraph(SupplierUtil.createStringSupplier(1),SupplierUtil.DEFAULT_EDGE_SUPPLIER,false);
So applied to your example this would give:
Graph<String, DefaultEdge> wpCategories = new DirectedMultigraph(SupplierUtil.createStringSupplier(1),SupplierUtil.DEFAULT_EDGE_SUPPLIER,false);
CSVImporter<String, DefaultEdge> importer = new CSVImporter(CSVFormat.EDGE_LIST);
importer.importGraph(wpCategories, new File("hypernymGraphWithEntities_WP1-small.csv"));
Note that, as an alternative to the vertex supplier, you could also use the setVertexFactory
function in the CSVImporter
class. Again, using your code:
Graph<String, DefaultEdge> wpCategories = new DirectedMultigraph(DefaultEdge.class);
CSVImporter<String, DefaultEdge> importer = new CSVImporter(CSVFormat.EDGE_LIST);
Function<String, String> vertexFactory = x -> x;
importer.setVertexFactory(vertexFactory);
importer.importGraph(wpCategories, new File("hypernymGraphWithEntities_WP1-small.csv"));
Disclaimer: In absence of data, the above code isn't tested.