@Test public void testGeoShapeSerializationReadWriteAsGraphSONEmbedded() throws Exception { GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true); GraphSONMapper m = graph.io(IoCore.graphson()).mapper().embedTypes(true).create(); GraphWriter writer = graph.io(IoCore.graphson()).writer().mapper(m).create(); FileOutputStream fos = new FileOutputStream("/tmp/test.json"); writer.writeGraph(fos, graph); clearGraph(config); open(config); GraphReader reader = graph.io(IoCore.graphson()).reader().mapper(m).create(); FileInputStream fis = new FileInputStream("/tmp/test.json"); reader.readGraph(fis, graph); TitanIndexTest.assertGraphOfTheGods(graph); }
@Parameterized.Parameters(name = "{0}") public static Iterable<Object[]> data() { return Arrays.asList(new Object[][]{ {"graphson-v1", false, false, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().create()}, {"graphson-v1-embedded", true, false, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v2", false, false, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create()}, {"graphson-v2-embedded", true, false, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v3", true, false, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create()}, {"gryo-v1", true, true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).reader().create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).writer().create()}, {"gryo-v3", true, true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).reader().create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).writer().create()} }); }
private void loadGraph() { final File f = new File(graphLocation); if (f.exists() && f.isFile()) { try { if (graphFormat.equals("graphml")) { io(IoCore.graphml()).readGraph(graphLocation); } else if (graphFormat.equals("graphson")) { io(IoCore.graphson()).readGraph(graphLocation); } else if (graphFormat.equals("gryo")) { io(IoCore.gryo()).readGraph(graphLocation); } else { io(IoCore.createIoBuilder(graphFormat)).readGraph(graphLocation); } } catch (Exception ex) { throw new RuntimeException(String.format("Could not load graph at %s with %s", graphLocation, graphFormat), ex); } } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertex() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper(); final Vertex v = graph.vertices(convertToVertexId("marko")).next(); final String json = mapper.writeValueAsString(v); final Map<String, Object> m = mapper.readValue(json, mapTypeReference); assertEquals(GraphSONTokens.VERTEX, m.get(GraphSONTokens.TYPE)); assertEquals(v.label(), m.get(GraphSONTokens.LABEL)); assertNotNull(m.get(GraphSONTokens.ID)); final Map<String,List<Map<String,Object>>> properties = (Map<String,List<Map<String,Object>>>) m.get(GraphSONTokens.PROPERTIES); assertEquals(v.value("name").toString(), properties.get("name").get(0).get(GraphSONTokens.VALUE).toString()); assertEquals((Integer) v.value("age"), properties.get("age").get(0).get(GraphSONTokens.VALUE)); assertEquals(1, properties.get("name").size()); assertEquals(1, properties.get("age").size()); assertEquals(2, properties.size()); }
/** * Only need to execute this test with TinkerGraph or other graphs that support user supplied identifiers. */ @Test @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) @FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_USER_SUPPLIED_IDS) @FeatureRequirement(featureClass = Graph.Features.VariableFeatures.class, feature = FEATURE_VARIABLES) @LoadGraphWith(LoadGraphWith.GraphData.CLASSIC) public void shouldWriteNormalizedGraphSON() throws Exception { try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { final GraphSONMapper mapper = graph.io(graphson).mapper().version(GraphSONVersion.V3_0).normalize(true).create(); final GraphSONWriter w = graph.io(graphson).writer().mapper(mapper).create(); w.writeGraph(bos, graph); final String expected = streamToString(IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphSONResourceAccess.class) + "tinkerpop-classic-normalized-v3d0.json")); assertEquals(expected.replace("\n", "").replace("\r", ""), bos.toString().replace("\n", "").replace("\r", "")); } }
/** * {@inheritDoc} */ @Override public void writeGraph(final String file) throws IOException { try (final OutputStream out = new FileOutputStream(file)) { writer().create().writeGraph(out, graph); } }
/** * {@inheritDoc} */ @Override public GraphSONReader.Builder reader() { return GraphSONReader.build().mapper(mapper().create()); }
/** * Create a new builder using the default version of GraphSON - v3. */ public static Io.Builder<GraphSONIo> build() { return build(GraphSONVersion.V3_0); }
/** * {@inheritDoc} */ @Override public void readGraph(final String file) throws IOException { try (final InputStream in = new FileInputStream(file)) { reader().create().readGraph(in, graph); } }
private void saveGraph() { final File f = new File(graphLocation); if (f.exists()) { f.delete(); } else { final File parent = f.getParentFile(); // the parent would be null in the case of an relative path if the graphLocation was simply: "f.gryo" if (parent != null && !parent.exists()) { parent.mkdirs(); } } try { if (graphFormat.equals("graphml")) { io(IoCore.graphml()).writeGraph(graphLocation); } else if (graphFormat.equals("graphson")) { io(IoCore.graphson()).writeGraph(graphLocation); } else if (graphFormat.equals("gryo")) { io(IoCore.gryo()).writeGraph(graphLocation); } else { io(IoCore.createIoBuilder(graphFormat)).writeGraph(graphLocation); } } catch (Exception ex) { throw new RuntimeException(String.format("Could not save graph at %s with %s", graphLocation, graphFormat), ex); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithProperties() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper(); final VertexProperty vp = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final String json = mapper.writeValueAsString(vp); final Map<String, Object> m = mapper.readValue(json, mapTypeReference); assertEquals(vp.label(), m.get(GraphSONTokens.LABEL)); assertNotNull(m.get(GraphSONTokens.ID)); assertEquals(vp.value(), m.get(GraphSONTokens.VALUE)); assertEquals(vp.values("startTime").next(), ((Map) m.get(GraphSONTokens.PROPERTIES)).get("startTime")); assertEquals(vp.values("endTime").next(), ((Map) m.get(GraphSONTokens.PROPERTIES)).get("endTime")); }
/** * Only need to execute this test with TinkerGraph or other graphs that support user supplied identifiers. */ @Test @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) @FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_USER_SUPPLIED_IDS) @FeatureRequirement(featureClass = Graph.Features.VariableFeatures.class, feature = FEATURE_VARIABLES) @LoadGraphWith(LoadGraphWith.GraphData.CLASSIC) public void shouldWriteNormalizedGraphSON() throws Exception { try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { final GraphSONMapper mapper = graph.io(graphson).mapper().version(GraphSONVersion.V1_0).normalize(true).create(); final GraphSONWriter w = graph.io(graphson).writer().mapper(mapper).create(); w.writeGraph(bos, graph); final String expected = streamToString(IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphSONResourceAccess.class) + "tinkerpop-classic-normalized-v1d0.json")); assertEquals(expected.replace("\n", "").replace("\r", ""), bos.toString().replace("\n", "").replace("\r", "")); } }
public Serializer serializer(Graph g) { // TODO: cache Serializer return new JsonSerializer(g.io(IoCore.graphson()).writer() .wrapAdjacencyList(true).create()); }
/** * {@inheritDoc} */ @Override public GraphSONWriter.Builder writer() { return GraphSONWriter.build().mapper(mapper().create()); }
/** * Creates a basic GraphSON-based {@link org.apache.tinkerpop.gremlin.structure.io.Io.Builder}. */ public static Io.Builder<GraphSONIo> graphson() { return GraphSONIo.build(); }
/** * {@inheritDoc} */ @Override public void readGraph(final String file) throws IOException { try (final InputStream in = new FileInputStream(file)) { reader().create().readGraph(in, graph); } }
private void saveGraph() { final File f = new File(graphLocation); if (f.exists()) { f.delete(); } else { final File parent = f.getParentFile(); // the parent would be null in the case of an relative path if the graphLocation was simply: "f.gryo" if (parent != null && !parent.exists()) { parent.mkdirs(); } } try { if (graphFormat.equals("graphml")) { io(IoCore.graphml()).writeGraph(graphLocation); } else if (graphFormat.equals("graphson")) { io(IoCore.graphson()).writeGraph(graphLocation); } else if (graphFormat.equals("gryo")) { io(IoCore.gryo()).writeGraph(graphLocation); } else { io(IoCore.createIoBuilder(graphFormat)).writeGraph(graphLocation); } } catch (Exception ex) { throw new RuntimeException(String.format("Could not save graph at %s with %s", graphLocation, graphFormat), ex); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) @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) public void shouldReadWriteModernWrappedInJsonObject() throws Exception { final GraphSONMapper mapper = graph.io(graphson).mapper().version(GraphSONVersion.V1_0).create(); try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = graph.io(graphson()).writer().wrapAdjacencyList(true).mapper(mapper).create(); writer.writeGraph(os, graph); final Configuration configuration = graphProvider.newGraphConfiguration("readGraph", this.getClass(), name.getMethodName(), LoadGraphWith.GraphData.MODERN); graphProvider.clear(configuration); final Graph g1 = graphProvider.openTestGraph(configuration); final GraphReader reader = graph.io(graphson()).reader().mapper(mapper).unwrapAdjacencyList(true).create(); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readGraph(bais, g1); } // modern uses double natively so always assert as such IoTest.assertModernGraph(g1, true, true); graphProvider.clear(g1, configuration); } }
@Parameterized.Parameters(name = "{0}") public static Iterable<Object[]> data() { return Arrays.asList(new Object[][]{ {"graphson-v1", false, false, (Function<Graph,GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().create()}, {"graphson-v1-embedded", true, true, (Function<Graph,GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v2", false, false, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create()}, {"graphson-v2-embedded", true, true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v3", true, true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create()}, {"gryo-v1", true, true, (Function<Graph,GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).reader().create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).writer().create()}, {"gryo-v3", true, true, (Function<Graph,GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).reader().create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).writer().create()} }); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializePath() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper(); final Path p = g.V(convertToVertexId("marko")).as("a").outE().as("b").inV().as("c").path() .filter(t -> ((Vertex) t.get().objects().get(2)).value("name").equals("lop")).next(); final String json = mapper.writeValueAsString(p); final Map<String, Object> m = mapper.readValue(json, mapTypeReference); assertEquals(2, m.size()); final List<List<String>> labels = (List<List<String>>) m.get(GraphSONTokens.LABELS); assertEquals(3, labels.size()); assertEquals("a", labels.get(0).get(0)); assertEquals(1, labels.get(0).size()); assertEquals("b", labels.get(1).get(0)); assertEquals(1, labels.get(1).size()); assertEquals("c", labels.get(2).get(0)); assertEquals(1, labels.get(2).size()); final List<Object> objects = (List<Object>) m.get(GraphSONTokens.OBJECTS); assertEquals(3, objects.size()); assertEquals("marko", ((List<Map>) ((Map) ((Map) objects.get(0)).get(GraphSONTokens.PROPERTIES)).get("name")).get(0).get(GraphSONTokens.VALUE).toString()); assertEquals("created", ((Map) objects.get(1)).get(GraphSONTokens.LABEL)); assertEquals("lop", ((List<Map>) ((Map) ((Map) objects.get(2)).get(GraphSONTokens.PROPERTIES)).get("name")).get(0).get(GraphSONTokens.VALUE).toString()); }