You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by "Elie Roux (JIRA)" <ji...@apache.org> on 2017/02/20 11:51:44 UTC
[jira] [Created] (JENA-1292) almost impossible to get @vocab prefix
in JsonLDContext
Elie Roux created JENA-1292:
-------------------------------
Summary: almost impossible to get @vocab prefix in JsonLDContext
Key: JENA-1292
URL: https://issues.apache.org/jira/browse/JENA-1292
Project: Apache Jena
Issue Type: Bug
Components: RIOT
Affects Versions: Jena 3.2.0
Environment: Linux, Ecplise 4.2
Reporter: Elie Roux
Priority: Minor
It's almost impossible to get the empty prefix of a model mapped to Json-ld "@vocab" context, to do so I have to rebuild a context from scratch, which is not really nice...
The fix should be a one-liner, see comment on
https://github.com/apache/jena/blob/master/jena-arq/src/main/java/org/apache/jena/riot/writer/JsonLDWriter.java#L402
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)
Re: [jira] [Created] (JENA-1292) almost impossible to get @vocab prefix in JsonLDContext
Posted by François-Paul Servant <fr...@gmail.com>.
note that regarding the code that I sent, it was probably enough to just add the @vocab node (removing the props in the ns is just a matter of clarity of the output, and of not wasting bandwidth).
> Le 20 févr. 2017 à 15:47, François-Paul Servant <fr...@gmail.com> a écrit :
>
> Hi Elie,
>
> (I wrote the comment in the line you point to. And then forgot it).
>
> The fix is probably more than one line (it must take care of the other parts of the definition of the “context" passed to the JSON-LD java API, and of the behavior of that API when it includes an @vocab)
>
> I’ll try to see what can be done. In the meantime, you maybe can use a new possibility in 3.2.0: you can access the object that jena creates and pass to the JSON-LD java API. You can modify it before sending it to the JSON-LD java API. Please find below a quick example.
>
> HTH
>
> fps
>
> /* Created on 20 févr. 2017 */
> package com.renault.sicg.rasse.commons;
>
> import java.io.IOException;
> import java.io.PrintWriter;
> import java.util.ArrayList;
> import java.util.List;
> import java.util.Map;
> import java.util.Map.Entry;
>
> import org.apache.jena.query.DatasetFactory;
> import org.apache.jena.rdf.model.Model;
> import org.apache.jena.rdf.model.ModelFactory;
> import org.apache.jena.rdf.model.Resource;
> import org.apache.jena.riot.RDFFormat;
> import org.apache.jena.riot.system.PrefixMap;
> import org.apache.jena.riot.system.RiotLib;
> import org.apache.jena.riot.writer.JsonLDWriter;
> import org.apache.jena.sparql.core.DatasetGraph;
> import org.apache.jena.sparql.util.Context;
> import org.apache.jena.sparql.vocabulary.FOAF;
> import org.apache.jena.vocabulary.RDF;
> import org.junit.Test;
>
> import com.fasterxml.jackson.core.JsonParseException;
> import com.github.jsonldjava.core.JsonLdError;
> import com.github.jsonldjava.utils.JsonUtils;
>
> public class AtVocab {
> @Test public final void test() throws JsonParseException, JsonLdError, IOException {
> Model m = ModelFactory.createDefaultModel();
> String ns = "http://schema.org/";
> Resource person = m.createResource(ns + "Person");
> Resource s = m.createResource();
> m.add(s, m.createProperty(ns + "name"), "Jane Doe");
> m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
> m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
> m.add(s, FOAF.nick, "jd");
> m.add(s, RDF.type, person);
>
> m.setNsPrefix("", ns);
>
> DatasetGraph g = DatasetFactory.create(m).asDatasetGraph();
> PrefixMap pm = RiotLib.prefixMap(g);
> String base = null;
> Context jenaContext = null;
>
> // the JSON-LD API object. It's a map
> Map map = (Map) JsonLDWriter.toJsonLDJavaAPI((RDFFormat.JSONLDVariant)RDFFormat.JSONLD.getVariant()
> , g, pm, base, jenaContext);
>
> // get the @context:
> Map<String, Object> ctx = (Map<String, Object>) map.get("@context");
>
> // add the "@vocab" key, to ctx
> // and remove from it declaration of props in ns
>
> // remove from ctx declaration of props in ns
> List<String> remove = new ArrayList<>();
> for (Entry<String, Object> e : ctx.entrySet()) {
> // is it the declaration of a prop in ns?
> Object o = e.getValue();
> if (o instanceof Map) {
> o = ((Map) o).get("@id");
> }
> if ((o != null) && (o instanceof String)) {
> if (((String) o).equals(ns + e.getKey())) {
> remove.add(e.getKey());
> }
> }
> }
> for (String key : remove) {
> ctx.remove(key);
> }
>
> // add the "@vocab" key to ctx
> ctx.put("@vocab", "http://schema.org/");
>
> JsonUtils.writePrettyPrint(new PrintWriter(System.out), map) ;
> }
>
> }
>
Re: [jira] [Created] (JENA-1292) almost impossible to get @vocab prefix in JsonLDContext
Posted by François-Paul Servant <fr...@gmail.com>.
Hi Elie,
(I wrote the comment in the line you point to. And then forgot it).
The fix is probably more than one line (it must take care of the other parts of the definition of the “context" passed to the JSON-LD java API, and of the behavior of that API when it includes an @vocab)
I’ll try to see what can be done. In the meantime, you maybe can use a new possibility in 3.2.0: you can access the object that jena creates and pass to the JSON-LD java API. You can modify it before sending it to the JSON-LD java API. Please find below a quick example.
HTH
fps
/* Created on 20 févr. 2017 */
package com.renault.sicg.rasse.commons;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.riot.RDFFormat;
import org.apache.jena.riot.system.PrefixMap;
import org.apache.jena.riot.system.RiotLib;
import org.apache.jena.riot.writer.JsonLDWriter;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.util.Context;
import org.apache.jena.sparql.vocabulary.FOAF;
import org.apache.jena.vocabulary.RDF;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.github.jsonldjava.core.JsonLdError;
import com.github.jsonldjava.utils.JsonUtils;
public class AtVocab {
@Test public final void test() throws JsonParseException, JsonLdError, IOException {
Model m = ModelFactory.createDefaultModel();
String ns = "http://schema.org/";
Resource person = m.createResource(ns + "Person");
Resource s = m.createResource();
m.add(s, m.createProperty(ns + "name"), "Jane Doe");
m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
m.add(s, FOAF.nick, "jd");
m.add(s, RDF.type, person);
m.setNsPrefix("", ns);
DatasetGraph g = DatasetFactory.create(m).asDatasetGraph();
PrefixMap pm = RiotLib.prefixMap(g);
String base = null;
Context jenaContext = null;
// the JSON-LD API object. It's a map
Map map = (Map) JsonLDWriter.toJsonLDJavaAPI((RDFFormat.JSONLDVariant)RDFFormat.JSONLD.getVariant()
, g, pm, base, jenaContext);
// get the @context:
Map<String, Object> ctx = (Map<String, Object>) map.get("@context");
// add the "@vocab" key, to ctx
// and remove from it declaration of props in ns
// remove from ctx declaration of props in ns
List<String> remove = new ArrayList<>();
for (Entry<String, Object> e : ctx.entrySet()) {
// is it the declaration of a prop in ns?
Object o = e.getValue();
if (o instanceof Map) {
o = ((Map) o).get("@id");
}
if ((o != null) && (o instanceof String)) {
if (((String) o).equals(ns + e.getKey())) {
remove.add(e.getKey());
}
}
}
for (String key : remove) {
ctx.remove(key);
}
// add the "@vocab" key to ctx
ctx.put("@vocab", "http://schema.org/");
JsonUtils.writePrettyPrint(new PrintWriter(System.out), map) ;
}
}