You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2016/10/03 10:35:39 UTC

[07/41] jena git commit: Setting the "@context" using a json string

Setting the "@context" using a json string

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/df454586
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/df454586
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/df454586

Branch: refs/heads/master
Commit: df45458624694f4605954467a135bd9c26d075bc
Parents: 0029b90
Author: Franc\u0327ois-Paul Servant <fp...@semanlink.net>
Authored: Fri May 6 11:08:38 2016 +0200
Committer: Franc\u0327ois-Paul Servant <fp...@semanlink.net>
Committed: Fri May 6 11:08:38 2016 +0200

----------------------------------------------------------------------
 .../org/apache/jena/riot/out/JsonLDWriter.java  |  14 +-
 .../jena/riot/writer/TestJsonLDWriter.java      | 141 +++++++++++++------
 2 files changed, 112 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/df454586/jena-arq/src/main/java/org/apache/jena/riot/out/JsonLDWriter.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/JsonLDWriter.java b/jena-arq/src/main/java/org/apache/jena/riot/out/JsonLDWriter.java
index 85a26b6..4c8d00b 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/out/JsonLDWriter.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/JsonLDWriter.java
@@ -79,8 +79,12 @@ import com.github.jsonldjava.utils.JsonUtils ;
  */
 public class JsonLDWriter extends WriterDatasetRIOTBase
 {
-		/** value: the context expected by JsonLdProcessor.compact anf flatten */
+		/** value: the context expected by JSON-LD JsonLdProcessor.compact and flatten */
 		public static final Symbol JSONLD_CONTEXT = Symbol.create("JSONLD_CONTEXT");
+		public static final Symbol JSONLD_CONTEXT_AS_JSON_STRING = Symbol.create("JSONLD_CONTEXT_AS_JSON_STRING");
+		
+		/** value: a context to replace the one used to compute the output */
+		public static final Symbol JSONLD_OUT_CONTEXT = Symbol.create("JSONLD_OUT_CONTEXT");
 		/** value: the frame object expected by JsonLdProcessor.frame */
 		public static final Symbol JSONLD_FRAME = Symbol.create("JSONLD_FRAME");
 		/** value: the option object expected by JsonLdProcessor (instance of JsonLdOptions) */
@@ -91,8 +95,8 @@ public class JsonLDWriter extends WriterDatasetRIOTBase
 			EXPAND,
 			FRAME
 		}
-		
-    private final RDFFormat format ;
+
+		private final RDFFormat format ;
 
     public JsonLDWriter(RDFFormat syntaxForm) {
         format = syntaxForm ;
@@ -173,6 +177,10 @@ public class JsonLDWriter extends WriterDatasetRIOTBase
       	  			if (jenaContext.isDefined(JSONLD_CONTEXT)) {
       	  				isCtxDefined = true;
       	  				ctx = jenaContext.get(JSONLD_CONTEXT);
+      	  			} else if (jenaContext.isDefined(JSONLD_CONTEXT_AS_JSON_STRING)) {
+      	  				isCtxDefined = true;
+      	  				String jsonString = (String) jenaContext.get(JSONLD_CONTEXT_AS_JSON_STRING);
+      	  				if (jsonString != null) ctx = JsonUtils.fromString(jsonString);
       	  			}
       	  		}
 

http://git-wip-us.apache.org/repos/asf/jena/blob/df454586/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java b/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java
index 4a5838f..25b10ff 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java
@@ -176,52 +176,65 @@ private Model simpleModel() {
 
 }
 
-@Test public final void testFrames() throws JsonParseException, IOException {
+/** verify that one may pass a context, and that it is used by the output */
+@Test public void testSettingContextAsJsonString() {
+	// 1) get the context generated by default by jena
+	// for a simple model with a prefix declaration
+	// 2) remove prefix declaration from model,
+	// output as jsonld is different
+	// 3) output the model as jsonld using the context:
+	// we should get the same output as in 1
 	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, RDF.type, person);
-	s = m.createResource();
-	m.add(s, m.createProperty(ns + "name"), "Gado Salamatou");
-	m.add(s, m.createProperty(ns + "url"), "http://www.salamatou.com");
-	m.add(s, RDF.type, person);
-	s = m.createResource();
-	m.add(s, m.createProperty(ns + "name"), "Not a person");
-	m.add(s, RDF.type, m.createResource(ns + "Event"));
+	String url = "http://www.semanlink.net/test/";
+	Resource s = m.createResource(url + "s");
+	Property p = m.createProperty(url + "p");
+	Resource o = m.createResource(url + "o");
+	m.add(s,p,o);
+	m.setNsPrefix("ex", url);
 	
-	Context jenaCtx = new Context();
-	JsonObject frame = new JsonObject();
+	String s1 = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, null);
+	// there's a prefix in m, and we find it in the output
+	String prefixStringInResult = "\"ex\":\"" + url + "\"";
+	assertTrue(s1.indexOf(prefixStringInResult) > 0);
+	Model m1 = parse(s1);
 	
-	// only ouput the persons using a frame
+	// must we pass the json object associated to "@context",
+	// or its parent node (that is, with the "@context") ?
+	// Actually, we can do both (JSONLD-java's code ensure it)
+	// We check it for here
 	
-	frame.put("@type", ns +"Person");
-	jenaCtx.set(JsonLDWriter.JSONLD_FRAME, JsonUtils.fromString(frame.toString()));
-	String jsonld = toString(m, RDFFormat.JSONLD_FRAME_PRETTY, jenaCtx);
-	Model m2 = parse(jsonld);
-	// 2 subjects with a type in m2
-	assertTrue(m2.listStatements((Resource) null, RDF.type, (RDFNode) null).toList().size() == 2);
-	// 2 persons in m2
-	assertTrue(m2.listStatements((Resource) null, RDF.type, person).toList().size() == 2);
-	// something we hadn't tested in prettyIsNotFlat
-	assertTrue(jsonld.trim().indexOf("\n") > -1);
+	// this is json object associated to "@context" in s1
+	// it includes the "ex" prefix
 	
-	// only output the subjects which have a jobTitle
+	String js = "{\"p\":{\"@id\":\"http://www.semanlink.net/test/p\",\"@type\":\"@id\"},\"ex\":\"http://www.semanlink.net/test/\"}";
+
+	// remove the prefix from m
+	m.removeNsPrefix("ex");	// RDFDataMgr.write(System.out, m, RDFFormat.JSONLD) ;
+	String s2 = toString(m, RDFFormat.JSONLD_COMPACT_PRETTY, null);
+	// model wo prefix -> no more prefix string in result:
+	assertTrue(s2.indexOf(prefixStringInResult) < 0);
+
+	// the model wo prefix, outputed as jsonld using a context that defines the prefix	
+	Context jenaCtx = new Context();
+	jenaCtx.set(JsonLDWriter.JSONLD_CONTEXT_AS_JSON_STRING, js);
+	String s3 = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx);
 	
-	frame = new JsonObject();
-	frame.put("http://schema.org/jobTitle", new JsonObject());
-	jenaCtx.set(JsonLDWriter.JSONLD_FRAME, JsonUtils.fromString(frame.toString()));
-	jsonld = toString(m, RDFFormat.JSONLD_FRAME_FLAT, jenaCtx);
-	m2 = parse(jsonld);
-	// 1 subject with a type in m2
-	assertTrue(m2.listStatements((Resource) null, RDF.type, (RDFNode) null).toList().size() == 1);
-	// 1 subject with a jobTitle in m2
-	assertTrue(m2.listStatements((Resource) null, m.createProperty(ns + "jobTitle"), (RDFNode) null).toList().size() == 1);
-	// something we hadn't tested in prettyIsNotFlat
-	assertTrue(jsonld.trim().indexOf("\n") < 0);
+	assertTrue(s3.length() == s1.length());
+	assertTrue(s3.indexOf(prefixStringInResult) > 0);
+	Model m3 = parse(s3);
+	assertTrue(m3.isIsomorphicWith(m));
+	assertTrue(m3.isIsomorphicWith(m1));
+	
+	// same thing, but passing also the "@context"
+	js = "{\"@context\":" + js + "}";
+	jenaCtx.set(JsonLDWriter.JSONLD_CONTEXT_AS_JSON_STRING, js);
+	String s4 = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx);
+	
+	assertTrue(s4.length() == s1.length());
+	assertTrue(s4.indexOf(prefixStringInResult) > 0);
+	Model m4 = parse(s4);
+	assertTrue(m4.isIsomorphicWith(m));
+	assertTrue(m4.isIsomorphicWith(m1));
 }
 
 @Test public final void testContextByUri() {
@@ -300,6 +313,54 @@ java.lang.NoSuchMethodError: org.apache.http.impl.client.cache.CacheConfig.custo
 	// How would we do that?
 }
 
+@Test public final void testFrames() throws JsonParseException, 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, RDF.type, person);
+	s = m.createResource();
+	m.add(s, m.createProperty(ns + "name"), "Gado Salamatou");
+	m.add(s, m.createProperty(ns + "url"), "http://www.salamatou.com");
+	m.add(s, RDF.type, person);
+	s = m.createResource();
+	m.add(s, m.createProperty(ns + "name"), "Not a person");
+	m.add(s, RDF.type, m.createResource(ns + "Event"));
+	
+	Context jenaCtx = new Context();
+	JsonObject frame = new JsonObject();
+	
+	// only ouput the persons using a frame
+	
+	frame.put("@type", ns +"Person");
+	jenaCtx.set(JsonLDWriter.JSONLD_FRAME, JsonUtils.fromString(frame.toString()));
+	String jsonld = toString(m, RDFFormat.JSONLD_FRAME_PRETTY, jenaCtx);
+	Model m2 = parse(jsonld);
+	// 2 subjects with a type in m2
+	assertTrue(m2.listStatements((Resource) null, RDF.type, (RDFNode) null).toList().size() == 2);
+	// 2 persons in m2
+	assertTrue(m2.listStatements((Resource) null, RDF.type, person).toList().size() == 2);
+	// something we hadn't tested in prettyIsNotFlat
+	assertTrue(jsonld.trim().indexOf("\n") > -1);
+	
+	// only output the subjects which have a jobTitle
+	
+	frame = new JsonObject();
+	frame.put("http://schema.org/jobTitle", new JsonObject());
+	jenaCtx.set(JsonLDWriter.JSONLD_FRAME, JsonUtils.fromString(frame.toString()));
+	jsonld = toString(m, RDFFormat.JSONLD_FRAME_FLAT, jenaCtx);
+	m2 = parse(jsonld);
+	// 1 subject with a type in m2
+	assertTrue(m2.listStatements((Resource) null, RDF.type, (RDFNode) null).toList().size() == 1);
+	// 1 subject with a jobTitle in m2
+	assertTrue(m2.listStatements((Resource) null, m.createProperty(ns + "jobTitle"), (RDFNode) null).toList().size() == 1);
+	// something we hadn't tested in prettyIsNotFlat
+	assertTrue(jsonld.trim().indexOf("\n") < 0);
+}
+
 // cf https://mail-archives.apache.org/mod_mbox/jena-users/201604.mbox/%3c218AC4A3-030B-4248-A7DA-2B2597328242@gmail.com%3e
 @Test public final void testStringPropsInContext() {
 	Model m = ModelFactory.createDefaultModel();