Refine search
AbstractJsonLdGraphLike(final String bnodePrefix) { this(new RDFDataset(), bnodePrefix); }
public static void toNQuads(RDFDataset dataset, StringBuilder output) { final List<String> quads = new ArrayList<String>(); for (String graphName : dataset.graphNames()) { final List<RDFDataset.Quad> triples = dataset.getQuads(graphName); if ("@default".equals(graphName)) { graphName = null; } for (final RDFDataset.Quad triple : triples) { quads.add(toNQuad(triple, graphName)); } } Collections.sort(quads); for (final String quad : quads) { output.append(quad); } }
/** * Adds a triple to the default graph of this dataset * * @param subject * the subject for the triple * @param predicate * the predicate for the triple * @param object * the object for the triple */ public void addTriple(final String subject, final String predicate, final String object) { addQuad(subject, predicate, object, "@default"); }
private void addStatement(RDFDataset result, URI context, Resource subject, URI predicate, String value) { if (context == null) { result.addTriple(subject.toString(), predicate.toString(), value, null, null); } else { result.addQuad(subject.toString(), predicate.toString(), value, null, null, context.toString()); } }
private void addTripleToDataset(RDFDataset dataset, RDFDataset.Node subject, RDFDataset.Node predicate, RDFDataset.Node object){ String name = "@default"; RDFDataset.Quad triple = new RDFDataset.Quad(subject, predicate, object, name); // initialise graph in dataset if (!dataset.containsKey(name)) { final List<RDFDataset.Quad> tmp = new ArrayList<RDFDataset.Quad>(); tmp.add(triple); dataset.put(name, tmp); } // add triple if unique to its graph else { final List<RDFDataset.Quad> triples = (List<RDFDataset.Quad>) dataset.get(name); if (!triples.contains(triple)) { triples.add(triple); } } }
@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")); }
final RDFDataset inputRdf = new RDFDataset(); final String ns = "http://www.example.com/foo/"; inputRdf.setNamespace("ex", ns); inputRdf.addTriple(ns + "s", ns + "p", ns + "o"); inputRdf.addTriple(ns + "s", ns + "p", ns + "o"); for (final Quad quad : inputRdf.getQuads("@default")) { final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; inputRdf.getContext(), options); new JsonLdApi(options).fromRDF(inputRdf, true), inputRdf.getContext(), options); final String jsonld2 = JsonUtils.toPrettyString(fromRDF2);
@Override public Object call(RDFDataset dataset) { for (final Entry<String, String> e : dataset.getNamespaces().entrySet()) { availableNamespaces.put(e.getValue(), e.getKey()); final Map<String, Map<String, List<Object>>> ttl = new LinkedHashMap<>(); for (String graphName : dataset.keySet()) { final List<RDFDataset.Quad> triples = dataset.getQuads(graphName); if ("@default".equals(graphName)) { graphName = null;
final RDFDataset inputRdf = new RDFDataset(); inputRdf.setNamespace("ex", ns); inputRdf.addTriple(subjectGenerator.apply(i), predicateGenerator.apply(i), objectGenerator.apply(i)); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true;
Collections.shuffle(potentialPredicates, prng); final RDFDataset testData = new RDFDataset(); testData.addQuad(potentialSubjects.get(prng.nextInt(potentialSubjects.size())), potentialPredicates.get(prng.nextInt(potentialPredicates.size())), nextObject, JsonLdConsts.XSD_STRING, potentialSubjects.get(prng.nextInt(potentialSubjects.size())), null); } else if (i % 5 == 0) { testData.addTriple( potentialSubjects.get(prng.nextInt(potentialSubjects.size())), potentialPredicates.get(prng.nextInt(potentialPredicates.size())), testData.addQuad(potentialSubjects.get(prng.nextInt(potentialSubjects.size())), potentialPredicates.get(prng.nextInt(potentialPredicates.size())), nextObject, potentialSubjects.get(prng.nextInt(potentialSubjects.size()))); } else if (i % 5 == 0) { testData.addTriple( potentialSubjects.get(prng.nextInt(potentialSubjects.size())), potentialPredicates.get(prng.nextInt(potentialPredicates.size())), final JsonLdOptions options = new JsonLdOptions(); final JsonLdApi jsonLdApi = new JsonLdApi(options); final int[] hashCodes = new int[rounds];
@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 (final String name : dataset.graphNames()) { final List<RDFDataset.Quad> graph = dataset.getQuads(name); && !opts.getUseRdfType()) { JsonLdUtils.mergeValue(node, "@type", object.getValue()); continue; final Map<String, Object> value = object.toObject(opts.getUseNativeTypes());
public RDFDataset parseXmlJson(JSONObject input) throws JSONException { RDFDataset dataset = new RDFDataset(); if(!input.has("rdf:RDF")){ }else{ JSONObject rdf = input.getJSONObject("rdf:RDF"); HashMap<String, String> namespaces = new HashMap<>(); Iterator<String> keys = rdf.keys(); while(keys.hasNext()){ String key = keys.next(); if(key.contains("xmlns:")){ namespaces.put(key.replace("xmlns:", ""), rdf.getString(key)); dataset.setNamespace(key.replace("xmlns:", ""), rdf.getString(key)); } } for(String ns : namespaces.keySet()){ rdf.remove("xmlns:"+ns); } //System.out.println(rdf.toString(2)); parseInner(rdf, dataset, namespaces); } return dataset; }
final RDFDataset dataset = new RDFDataset(); if (!dataset.containsKey(name)) { final List<RDFDataset.Quad> tmp = new ArrayList<>(); tmp.add(triple); dataset.put(name, tmp); final List<RDFDataset.Quad> triples = (List<RDFDataset.Quad>) dataset.get(name); if (!triples.contains(triple)) { triples.add(triple);
@Test public void literalsInDataset() throws Exception { final RDFDataset dataset = new RDFDataset(); dataset.addQuad("http://example.com/p", "http://example.com/p", "Same", null, null, "http://example.com/g1"); dataset.addQuad("http://example.com/p", "http://example.com/p", "Different", null, null, "http://example.com/g1"); final List<Quad> quads = dataset.getQuads("http://example.com/g1"); final Quad q1 = quads.get(0); final Quad q2 = quads.get(1); assertNotEquals(q1, q2); assertNotEquals(0, q1.compareTo(q2)); assertNotEquals(0, q1.getObject().compareTo(q2.getObject())); }
@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" // } ] // } ] } }
JsonUtils.getDefaultHttpClient()); assertNotNull(arrayContext); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; options.setDocumentLoader(documentLoader); final RDFDataset rdf = (RDFDataset) JsonLdProcessor.toRDF(arrayContext, options); assertEquals("http://example.org/", rdf.getNamespace("ex")); assertEquals("http://example.com/2/", rdf.getNamespace("ex2")); assertFalse(rdf.getNamespaces().containsKey("term1"));
final RDFDataset result = new RDFDataset(); final State state = new State((String) input); validateIRI(state, iri); state.namespaces.put(ns, iri); result.setNamespace(ns, iri); } else { String base = match.group(3) != null ? match.group(3) : match.group(6); result.addTriple(state.curSubject, state.curPredicate, bnode); state.advanceLinePosition(1); result.addTriple(state.curSubject, state.curPredicate, RDF_NIL); result.addTriple(state.curSubject, state.curPredicate, bnode); state.push(); state.curSubject = bnode; result.addTriple(state.curSubject, state.curPredicate, iri); } else { result.addTriple(state.curSubject, state.curPredicate, value, datatype, lang); result.addTriple(state.curSubject, RDF_REST, RDF_NIL); state.pop(); state.advanceLinePosition(1); result.addTriple(state.curSubject, RDF_REST, bnode);
/** * Adds RDF triples for each graph in the current node map to an RDF * dataset. * * @return the RDF dataset. * @throws JsonLdError * If there was an error converting from JSON-LD to RDF. */ public RDFDataset toRDF() throws JsonLdError { // TODO: make the default generateNodeMap call (i.e. without a // graphName) create and return the nodeMap final Map<String, Object> nodeMap = newMap(); nodeMap.put(JsonLdConsts.DEFAULT, newMap()); generateNodeMap(this.value, nodeMap); final RDFDataset dataset = new RDFDataset(this); for (final String graphName : nodeMap.keySet()) { // 4.1) if (JsonLdUtils.isRelativeIri(graphName)) { continue; } final Map<String, Object> graph = (Map<String, Object>) nodeMap.get(graphName); dataset.graphToRDF(graphName, graph); } return dataset; }
@Override public RDFDataset parse(Object object) throws JsonLdError { RDFDataset result = new RDFDataset() ; if ( object instanceof DatasetGraph ) dt = XSDDatatype.XSDstring.getURI() ; result.addQuad(sq, pq, lex, dt, lang, gq) ; result.addQuad(sq, pq, oq, gq) ;