/** * Writes an individual node to the BDIO document. */ public void next(Map<String, Object> node) throws IOException { checkState(started.get(), "not started"); // TODO Have a reusable pool of ByteArrayOutputStream wrapped writers? byte[] serializedNode = JsonUtils.toPrettyString(node).replace("\n", "\n ").getBytes(UTF_8); if (remaining.addAndGet((delimiter.length + serializedNode.length) * -1) > 0L) { // It fits, write it out out.write(delimiter); out.write(serializedNode); } else { // It didn't fit, create a new entry and try again nextEntry(); if (remaining.addAndGet(serializedNode.length * -1) > 0L) { out.write(serializedNode); } else { throw new EntrySizeViolationException(null, Math.abs(remaining.get()) + Bdio.MAX_ENTRY_SIZE); } } }
/** * Return the current prefixes as a JSON-LD string. * * @return the current prefixes as a JSON-LD string * @throws IOException on any error */ public String getContextString() throws IOException { try { Object compact = JsonLdProcessor.compact( JsonUtils.fromString("{}"), context.getPrefixes(false), new JsonLdOptions()); return JsonUtils.toPrettyString(compact); } catch (Exception e) { throw new IOException(jsonldContextCreationError, e); } }
public String process(String uri) throws JsonGenerationException, IOException { String query = PREFIXES + "SELECT (GROUP_CONCAT(distinct ?type;separator=' ') as ?types) (GROUP_CONCAT(distinct ?property;separator=' ') as ?properties) WHERE {\n" + "<" + uri + "> rdf:type ?type . FILTER(STRSTARTS(STR(?type), 'http://dbpedia.org/ontology')) . \n" + "<" + uri + "> ?property ?value . FILTER(STRSTARTS(STR(?property), 'http://dbpedia.org/ontology')) . \n" + "}"; QueryExecution queryExecution = executeQuery(query); // Get all Ontology Classes and Properties for given entity Iterator<QuerySolution> results = queryExecution.execSelect(); LinkedHashSet<Field> fields = new LinkedHashSet<>(); while (results.hasNext()) { QuerySolution solution = results.next(); if (solution.get("types") != null && solution.get("properties") != null) { List<String> types = Arrays.asList(solution.get("types").asLiteral().getString().split(" ")); String[] split = solution.get("properties").asLiteral().getString().split(" "); HashSet<String> properties = Sets .newHashSet(split); // Get Relevant Properties based on CouchDB List<Field> relevantProperties = getRelevantProperties(uri, types, properties); fields.addAll(relevantProperties); } } queryExecution.close(); return JsonUtils.toPrettyString(fields); }
+ JsonUtils.toPrettyString(expect) + "\nresult: " + (result instanceof JsonLdError ? ((JsonLdError) result).toString() : JsonUtils.toPrettyString(result)), testpassed);
@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")); }
for (int i = 0; i < rounds; i++) { final long start = System.nanoTime(); JsonUtils.toPrettyString(fromRDF3); if (i < 5000) { statsFirst5000Part3.accept(System.nanoTime() - start);
@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")); }
inputRdf.getContext(), options); final String jsonld1 = JsonUtils.toPrettyString(fromRDF1); final String jsonld2 = JsonUtils.toPrettyString(fromRDF2);
@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/")); }