org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLWriter Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IoTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Only need to execute this test with TinkerGraph or other graphs that support user supplied identifiers.
 */
@Test
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES)
@FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES)
@FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_USER_SUPPLIED_IDS)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_NUMERIC_IDS)
@LoadGraphWith(LoadGraphWith.GraphData.CLASSIC)
public void shouldWriteNormalizedGraphML() throws Exception {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        final GraphMLWriter w = GraphMLWriter.build().normalize(true).create();
        w.writeGraph(bos, graph);
        final String expected = streamToString(getResourceAsStream(GraphMLResourceAccess.class, "tinkerpop-classic-normalized.xml"));
        assertEquals(expected.replace("\n", "").replace("\r", ""), bos.toString().replace("\n", "").replace("\r", ""));
    }
}
 
Example #2
Source File: GraphMLRenderer.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void renderGraph(GraphContext context)
{
    Path outputFolder = createOutputFolder(context, "graphml");
    Path outputFile = outputFolder.resolve("graph.graphml");

    GraphMLWriter graphML = GraphMLWriter.build().create();
    try
    {
        graphML.writeGraph(new FileOutputStream(outputFile.toFile()), context.getGraph());
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: IoDataGenerationTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteClassicGraphAsGraphML() throws IOException {
    try (final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-classic.xml")) {
        GraphMLWriter.build().create().writeGraph(os, TinkerFactory.createClassic());
    }
}
 
Example #4
Source File: IoDataGenerationTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteModernGraphAsGraphML() throws IOException {
    try (final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-modern.xml")) {
        GraphMLWriter.build().create().writeGraph(os, TinkerFactory.createModern());
    }
}
 
Example #5
Source File: IoTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_REMOVE_EDGES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_REMOVE_VERTICES)
@FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES)
@FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES)
@FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES)
@FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_STRING_VALUES)
public void shouldReadGraphMLWithCommonVertexAndEdgePropertyNames() throws IOException {
    final GraphReader reader = GraphMLReader.build().create();
    try (final InputStream stream = new ByteArrayInputStream(CLASSIC_GRAPH_WITH_COLOR.getBytes("UTF-8"))) {
        reader.readGraph(stream, graph);
    }

    // there is also a "color" property on this dataset that is on both edges and vertices
    graph.vertices().forEachRemaining(v -> assertEquals("#6495ed", v.value("color")));
    graph.edges().forEachRemaining(e -> assertEquals("#ee0000", e.value("color")));

    final GraphWriter writer = GraphMLWriter.build().create();
    try (final OutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, graph);

        graph.vertices().forEachRemaining(Element::remove);
        try (final InputStream stream = new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray())) {
            reader.readGraph(stream, graph);
        }

        // there is also a "color" property on this dataset that is on both edges and vertices
        graph.vertices().forEachRemaining(v -> assertEquals("#6495ed", v.value("color")));
        graph.edges().forEachRemaining(e -> assertEquals("#ee0000", e.value("color")));
    }
}
 
Example #6
Source File: IoTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.CREW)
public void shouldNotWriteGraphMLFromGraphWithMultiProperties() throws Exception {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        final GraphMLWriter w = GraphMLWriter.build().create();
        w.writeGraph(bos, graph);
        fail("Should not be able to write multi-properties to GraphML");
    } catch (IllegalStateException iae) {
        assertThat(iae.getMessage(), containsString("multi-properties are not directly supported by GraphML format"));
    }
}
 
Example #7
Source File: IoTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Note: this is only a very lightweight test of writer/reader encoding. It is known that there are characters
 * which, when written by GraphMLWriter, cause parse errors for GraphMLReader. However, this happens uncommonly
 * enough that is not yet known which characters those are. Only need to execute this test with TinkerGraph
 * or other graphs that support user supplied identifiers.
 */
@Test
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = FEATURE_USER_SUPPLIED_IDS)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_STRING_IDS)
public void shouldProperlyEncodeWithGraphML() throws Exception {
    assumeThat("GraphML web site is down so XSD cannot be retrieved", is(isGraphMLXSDPresent()));

    final Vertex v = graph.addVertex(T.id, "1");
    v.property(VertexProperty.Cardinality.single, "text", "\u00E9");

    final GraphMLWriter w = GraphMLWriter.build().create();

    final File f = TestHelper.generateTempFile(this.getClass(), "test", ".txt");
    try (final OutputStream out = new FileOutputStream(f)) {
        w.writeGraph(out, graph);
    }

    validateXmlAgainstGraphMLXsd(f);

    // reusing the same config used for creation of "g".
    final Configuration configuration = graphProvider.newGraphConfiguration("g2", this.getClass(), name.getMethodName(), null);
    graphProvider.clear(configuration);
    final Graph g2 = graphProvider.openTestGraph(configuration);
    final GraphMLReader r = GraphMLReader.build().create();

    try (final InputStream in = new FileInputStream(f)) {
        r.readGraph(in, g2);
    }

    final Vertex v2 = g2.vertices("1").next();
    assertEquals("\u00E9", v2.property("text").value());

    // need to manually close the "g2" instance
    graphProvider.clear(g2, configuration);
}
 
Example #8
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteClassicGraphAsGraphML() throws IOException {
    try (final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-classic.xml"))) {
        GraphMLWriter.build().create().writeGraph(os, TinkerFactory.createClassic());
    }
}
 
Example #9
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteModernGraphAsGraphML() throws IOException {
    try (final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-modern.xml"))) {
        GraphMLWriter.build().create().writeGraph(os, TinkerFactory.createModern());
    }
}