/** * Converts RDF statements into JSON-LD. * * @param dataset * the RDF statements. * @return A list of JSON-LD objects found in the given dataset. * @throws JsonLdError * If there was an error during conversion from RDF to JSON-LD. */ public List<Object> fromRDF(final RDFDataset dataset) throws JsonLdError { return fromRDF(dataset, false); }
/** * Converts RDF statements into JSON-LD. * * @param dataset * the RDF statements. * @return A list of JSON-LD objects found in the given dataset. * @throws JsonLdError * If there was an error during conversion from RDF to JSON-LD. */ public List<Object> fromRDF(final RDFDataset dataset) throws JsonLdError { return fromRDF(dataset, false); }
final Object rval = new JsonLdApi(options).fromRDF(dataset);
final Object rval = new JsonLdApi(options).fromRDF(dataset);
final Object rval = new JsonLdApi(options).fromRDF(dataset);
Object obj = (new JsonLdApi(opts)).fromRDF(jsonldDataset, true); // true because we know that we don't have any duplicate in jsonldDataset
@Test public void testFrame0010() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0010-frame.jsonld")); //{ // "@id": "http://example.com/main/id", // "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": { // "@id": "http://example.com/rdf/id", // "http://www.w3.org/1999/02/22-rdf-syntax-ns#label": "someLabel" // } //} final RDFDataset ds = new RDFDataset(); ds.addTriple("http://example.com/main/id", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://example.com/rdf/id"); ds.addTriple("http://example.com/rdf/id", "http://www.w3.org/1999/02/22-rdf-syntax-ns#label", "someLabel", null, null); final JsonLdOptions opts = new JsonLdOptions(); opts.setProcessingMode(JsonLdOptions.JSON_LD_1_0); final Object in = new JsonLdApi(opts).fromRDF(ds, true); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0010-out.jsonld")); assertEquals(out, frame2); } }
for (int i = 0; i < rounds; i++) { final long start = System.nanoTime(); Object fromRDF = jsonLdApi.fromRDF(testData); if (i < 5000) { statsFirst5000.accept(System.nanoTime() - start); for (int i = 0; i < rounds; i++) { final long start = System.nanoTime(); Object fromRDF = jsonLdApiLax.fromRDF(testData, true); if (i < 5000) { statsLaxFirst5000.accept(System.nanoTime() - start); final LongSummaryStatistics statsFirst5000Part2 = new LongSummaryStatistics(); final LongSummaryStatistics statsPart2 = new LongSummaryStatistics(); final Object fromRDF2 = jsonLdApi2.fromRDF(testData); for (int i = 0; i < rounds; i++) { final long start = System.nanoTime(); final LongSummaryStatistics statsFirst5000Part3 = new LongSummaryStatistics(); final LongSummaryStatistics statsPart3 = new LongSummaryStatistics(); final Object fromRDF3 = jsonLdApi3.fromRDF(testData); for (int i = 0; i < rounds; i++) { final long start = System.nanoTime(); final LongSummaryStatistics statsFirst5000Part4 = new LongSummaryStatistics(); final LongSummaryStatistics statsPart4 = new LongSummaryStatistics(); final Object fromRDF4 = jsonLdApi4.fromRDF(testData); for (int i = 0; i < rounds; i++) { final long start = System.nanoTime();
@Test public void write() throws Exception { final Model model = ModelFactory.createDefaultModel(); final Resource resource = model.createResource("http://example.com/test"); final Property property = model.createProperty("http://example.com/value"); model.add(resource, property, "Test"); final JsonLdOptions options = new JsonLdOptions(); options.format = "application/ld+json"; final JenaRDFParser parser = new JenaRDFParser(); final RDFDataset dataset = parser.parse(model); final Object json = new JsonLdApi(options).fromRDF(dataset); final String jsonStr = JsonUtils.toPrettyString(json); // System.out.println(jsonStr); assertTrue(jsonStr.contains("@id")); assertTrue(jsonStr.contains("http://example.com/test")); assertTrue(jsonStr.contains("http://example.com/value")); assertTrue(jsonStr.contains("Test")); }
@Test public void modelToJsonLD() throws Exception { JenaJSONLD.init(); // Only needed once final Model model = ModelFactory.createDefaultModel(); final Resource resource = model.createResource("http://example.com/test"); final Property property = model.createProperty("http://example.com/value"); model.add(resource, property, "Test"); final JsonLdOptions options = new JsonLdOptions(); options.format = "application/ld+json"; final JsonLdApi api = new JsonLdApi(options); final RDFDataset dataset = new RDFDataset(api); final Object json = api.fromRDF(dataset); final String jsonStr = JsonUtils.toPrettyString(json); System.out.println(jsonStr); // [ { // "@id" : "http://example.com/test", // "http://example.com/value" : [ { // "@value" : "Test" // } ] // } ] } }
@Test public void prefixUsedToShortenPredicate() throws Exception { final RDFDataset inputRdf = new RDFDataset(); inputRdf.setNamespace("ex", "http://www.a.com/foo/"); inputRdf.addTriple("http://www.a.com/foo/s", "http://www.a.com/foo/p", "http://www.a.com/foo/o"); assertEquals("http://www.a.com/foo/", inputRdf.getNamespace("ex")); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final Object fromRDF = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final String toJSONLD = JsonUtils.toPrettyString(fromRDF); // System.out.println(toJSONLD); assertFalse("The lexicographically shortest URI was not chosen", toJSONLD.contains("http://www.a.com/foo/p")); }
@Test public void fromRdfWithNamespaceLexicographicallyShortestChosen() throws Exception { final RDFDataset inputRdf = new RDFDataset(); inputRdf.setNamespace("aat", "http://vocab.getty.edu/aat/"); inputRdf.setNamespace("aat_rev", "http://vocab.getty.edu/aat/rev/"); inputRdf.addTriple("http://vocab.getty.edu/aat/rev/5001065997", JsonLdConsts.RDF_TYPE, "http://vocab.getty.edu/aat/datatype"); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final Object fromRDF = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final RDFDataset rdf = (RDFDataset) JsonLdProcessor.toRDF(fromRDF, options); // System.out.println(rdf.getNamespaces()); assertEquals("http://vocab.getty.edu/aat/", rdf.getNamespace("aat")); assertEquals("http://vocab.getty.edu/aat/rev/", rdf.getNamespace("aat_rev")); final String toJSONLD = JsonUtils.toPrettyString(fromRDF); // System.out.println(toJSONLD); assertTrue("The lexicographically shortest URI was not chosen", toJSONLD.contains("aat:rev/")); }
@Test public void fromRdfWithNamespaceLexicographicallyShortestChosen2() throws Exception { final RDFDataset inputRdf = new RDFDataset(); inputRdf.setNamespace("aat", "http://vocab.getty.edu/aat/"); inputRdf.setNamespace("aatrev", "http://vocab.getty.edu/aat/rev/"); inputRdf.addTriple("http://vocab.getty.edu/aat/rev/5001065997", JsonLdConsts.RDF_TYPE, "http://vocab.getty.edu/aat/datatype"); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final Object fromRDF = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final RDFDataset rdf = (RDFDataset) JsonLdProcessor.toRDF(fromRDF, options); // System.out.println(rdf.getNamespaces()); assertEquals("http://vocab.getty.edu/aat/", rdf.getNamespace("aat")); assertEquals("http://vocab.getty.edu/aat/rev/", rdf.getNamespace("aatrev")); final String toJSONLD = JsonUtils.toPrettyString(fromRDF); // System.out.println(toJSONLD); assertFalse("The lexicographically shortest URI was not chosen", toJSONLD.contains("aat:rev/")); }