[ 
https://issues.apache.org/jira/browse/JENA-765?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14095402#comment-14095402
 ] 

Andy Seaborne commented on JENA-765:
------------------------------------

RDF graphs do not preserve the order of triples inserted. What's more, the data 
has bNodes which end with different internal identifiers every item the data is 
read.  The result is that iterating over the graph to write it can yield 
different orders.  Try with no blank nodes.

If you want specific control of the output for JSOn-LD, you will need to write 
code that decides the order and drives the JSON-LD writer engine directly.  
Jena uses a 3rd part component, 
[jsonld-java|https://github.com/jsonld-java/jsonld-java] for JSON-LD 
processing.  They implement the standard algorithm from the JSON-LD 
specification for transforming RDF into 
JSON-LD(http://www.w3.org/TR/json-ld/#serializing-deserializing-rdf). This 
contains "For each subject and node in graph ordered by subject:" but for blank 
nodes, that is not-deterministic.

Please look at {{org.apache.jena.riot.out.JsonLDWriter}}. We welcome 
contributions for improving Jena.

> org.apache.jena.riot.RDFDataMgr converter to JSONLD problem
> -----------------------------------------------------------
>
>                 Key: JENA-765
>                 URL: https://issues.apache.org/jira/browse/JENA-765
>             Project: Apache Jena
>          Issue Type: Bug
>          Components: RIOT
>    Affects Versions: Jena 2.12.0
>         Environment: Linux, eclipse, junit test
>            Reporter: Marcin
>            Priority: Minor
>
> Convertion from N-TRIPLE to JSONLD return a random JSONLD format. For example 
> for constant Ntriples string 'testNtriples' {{RDFDataMgr.write(osData, 
> modelJena.getGraph(), Lang.JSONLD);}} return random JSONLD string by 
> execution:
> a) execution 1 return string:
> {noformat}
> {
>     "@id" : "_:b3",
>     "@type" : "http://rdf.data-vocabulary.org/#Breadcrumb";,
>     "http://rdf.data-vocabulary.org/#title"; : {
>       "@language" : "pl",
>       "@value" : "Foto"
>     },
>     "http://rdf.data-vocabulary.org/#url"; : {
>       "@id" : "http://www.fake.com/foto.html";
>     }
>   }, 
> ...
> {noformat}
> b) execution 2 return string:
> {noformat}
> {
>   "@graph" : [ {
>     "@id" : "_:b0",
>     "@type" : "http://rdf.data-vocabulary.org/#Breadcrumb";,
>     "title" : {
>       "@language" : "pl",
>       "@value" : "Aparaty Cyfrowe Nikon "
>     },
>     "url" : "http://www.fake.com/site/cat/2/filtr/_283_0_0_0_0_0_0_0_0_0";
>   }, {
>     "@id" : "_:b1",
>     "@type" : "http://rdf.data-vocabulary.org/#Breadcrumb";,
>     "title" : {
>       "@language" : "pl",
>       "@value" : "Foto"
>     },
>     "url" : "http://www.fake.com/foto.html";
>   }, 
> ...
> {noformat}
> Simple jUnit test:
> {noformat}
> public class RdfaParserTest1 extends AbstractMarkupTest {
>     String testNtriples = "<http://www.fake.com> 
> <http://opengraph.org/schema/title> \"Nikon D5100 + 18-105 VR\"@pl 
> .\n<http://www.fake.com> <http://opengraph.org/schema/type> \"product\"@pl 
> .\n<http://www.fake.com> <http://opengraph.org/schema/url> 
> \"http://www.skapiec.pl/site/cat/2/comp/2231447\"@pl .\n<http://www.fake.com> 
> <http://opengraph.org/schema/image> 
> \"http://static3.skapiec.pl/2231447-1-1-6.jpg\"@pl .\n<http://www.fake.com> 
> <http://opengraph.org/schema/site_name> \"Skapiec.pl\"@pl 
> .\n<http://www.fake.com> <http://www.w3.org/1999/xhtml/vocab#stylesheet> 
> <http://www.fake.com/gallery.css?1407830722872> .\n<http://www.fake.com> 
> <http://www.w3.org/1999/xhtml/vocab#stylesheet> 
> <http://www.fake.com/style.nps.css> .\n<http://www.fake.com> 
> <http://www.w3.org/1999/xhtml/vocab#stylesheet> 
> <http://www.fake.com/style.header.css?1407830722872> .\n<http://www.fake.com> 
> <http://www.w3.org/1999/xhtml/vocab#search> 
> <http://www.fake.com/searchengine/skapiec.xml> .\n<http://www.fake.com> 
> <http://www.w3.org/1999/xhtml/vocab#icon> <http://www.fake.com/favicon.ico> 
> .\n<http://www.fake.com> <http://www.w3.org/1999/xhtml/vocab#stylesheet> 
> <http://static2.skapiec.pl/sfx/prettyPhoto/prettyPhoto.css> 
> .\n<http://www.fake.com> <http://www.w3.org/1999/xhtml/vocab#stylesheet> 
> <http://www.fake.com/sfx/colorbox.css> .\n<http://www.fake.com> 
> <http://www.w3.org/1999/xhtml/vocab#start> <http://www.skapiec.pl> .\n_:n0 
> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
> <http://rdf.data-vocabulary.org/#Breadcrumb> .\n_:n0 
> <http://rdf.data-vocabulary.org/#url> <http://www.fake.com/> .\n_:n0 
> <http://rdf.data-vocabulary.org/#title> \"Sk\\uFFFD\\uFFFDpiec.pl\"@pl 
> .\n_:n1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
> <http://rdf.data-vocabulary.org/#Breadcrumb> .\n_:n1 
> <http://rdf.data-vocabulary.org/#url> <http://www.fake.com/foto.html> .\n_:n1 
> <http://rdf.data-vocabulary.org/#title> \"Foto\"@pl .\n_:n2 
> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
> <http://rdf.data-vocabulary.org/#Breadcrumb> .\n_:n2 
> <http://rdf.data-vocabulary.org/#url> 
> <http://www.fake.com/cat/2-aparaty-cyfrowe.html> .\n_:n2 
> <http://rdf.data-vocabulary.org/#title> \"Aparaty Cyfrowe\"@pl .\n_:n3 
> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
> <http://rdf.data-vocabulary.org/#Breadcrumb> .\n_:n3 
> <http://rdf.data-vocabulary.org/#url> 
> <http://www.fake.com/site/cat/2/filtr/_283_0_0_0_0_0_0_0_0_0> .\n_:n3 
> <http://rdf.data-vocabulary.org/#title> \"Aparaty Cyfrowe Nikon \"@pl .\n_:n4 
> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
> <http://rdf.data-vocabulary.org/#Review-aggregate> .\n_:n4 
> <http://rdf.data-vocabulary.org/#itemreviewed> \"Nikon D5100 + 18-105 VR\"@pl 
> .\n_:n4 <http://rdf.data-vocabulary.org/#average> \"4.58\"@pl .\n_:n4 
> <http://rdf.data-vocabulary.org/#worst> \"0\"@pl .\n_:n4 
> <http://rdf.data-vocabulary.org/#votes> \"179\"@pl .\n_:n5 
> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
> <http://rdf.data-vocabulary.org/#Offer-aggregate> .\n_:n5 
> <http://rdf.data-vocabulary.org/#offerCount> \"50\"@pl .\n_:n5 
> <http://rdf.data-vocabulary.org/#condition> \"new\"@pl .\n_:n5 
> <http://rdf.data-vocabulary.org/#lowPrice> \"2199\"@pl .\n_:n5 
> <http://rdf.data-vocabulary.org/#highPrice> \"2711.2\"@pl 
> .\n<http://www.fake.com> <http://www.w3.org/1999/xhtml/vocab#copyright> 
> <http://www.fake.com/site/doc/7> .\n<http://www.fake.com> 
> <http://www.w3.org/1999/xhtml/vocab#index> <http://www.fake.com/site/doc/10> 
> .\n<http://www.fake.com> <http://www.w3.org/1999/xhtml/vocab#index> 
> <http://www.fake.com/site/doc/150/2/> .";
>     @Before
>     public void setUp() throws Exception {
>     }
>     @After
>     public void tearDown() throws Exception {
>     }
>     @Test
>     public void test1() throws IOException, SAXException, ParseException, 
> JSONException {
>         com.hp.hpl.jena.rdf.model.Model modelJena = 
> ModelFactory.createDefaultModel();
>         modelJena.read(IOUtils.toInputStream(testNtriples), 
> "http://www.fake.com";, "N-TRIPLE");
>         ByteArrayOutputStream osData = new java.io.ByteArrayOutputStream();
>         RDFDataMgr.write(osData, modelJena.getGraph(), Lang.JSONLD);
>         @SuppressWarnings("rawtypes")
>         Filter fData = 
> Filter.filter(Criteria.where("@type").exists(true).and("@type").in("http://rdf.data-vocabulary.org/#Breadcrumb";));
>         List<Object> breadcrumbs = JsonPath.read(osData.toString(), 
> "$..['@graph'][?]", fData);
>         try {
>             Object breadcumpPhoto = null;
>             for (Object object : breadcrumbs) {
>                 JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
> "$..title[?(@.['@value'] == Foto)].['@value']")));
>                 if (breadcump.size() != 0) {
>                     breadcumpPhoto = object;
>                 }
>             }
>             System.out.println("<--Test 1 start-->");
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['@value']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['http://rdf.data-vocabulary.org/#url']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
>             System.out.println("<--Test 1 end-->");
>         } catch (Exception e) {
>             System.out.println("Catch yes");
>             Object breadcumpPhoto = null;
>             for (Object object : breadcrumbs) {
>                 JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
> "$..['http://rdf.data-vocabulary.org/#title'][?(@.['@value'] == 
> Foto)].['@value']")));
>                 if (breadcump.size() != 0) {
>                     breadcumpPhoto = object;
>                 }
>             }
>             System.out.println("<--Test 1 start-->");
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['@value']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['http://rdf.data-vocabulary.org/#url']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
>             System.out.println("<--Test 1 end-->");
>         }
>     }
>     @Test
>     public void test2() throws IOException, SAXException, ParseException, 
> JSONException {
>         com.hp.hpl.jena.rdf.model.Model modelJena = 
> ModelFactory.createDefaultModel();
>         modelJena.read(IOUtils.toInputStream(testNtriples), 
> "http://www.fake.com";, "N-TRIPLE");
>         ByteArrayOutputStream osData = new java.io.ByteArrayOutputStream();
>         RDFDataMgr.write(osData, modelJena.getGraph(), Lang.JSONLD);
>         @SuppressWarnings("rawtypes")
>         Filter fData = 
> Filter.filter(Criteria.where("@type").exists(true).and("@type").in("http://rdf.data-vocabulary.org/#Breadcrumb";));
>         List<Object> breadcrumbs = JsonPath.read(osData.toString(), 
> "$..['@graph'][?]", fData);
>         try {
>             Object breadcumpPhoto = null;
>             for (Object object : breadcrumbs) {
>                 JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
> "$..title[?(@.['@value'] == Foto)].['@value']")));
>                 if (breadcump.size() != 0) {
>                     breadcumpPhoto = object;
>                 }
>             }
>             System.out.println("<--Test 2 start-->");
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['@value']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['http://rdf.data-vocabulary.org/#url']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
>             System.out.println("<--Test 2 end-->");
>         } catch (Exception e) {
>             System.out.println("Catch yes");
>             Object breadcumpPhoto = null;
>             for (Object object : breadcrumbs) {
>                 JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
> "$..['http://rdf.data-vocabulary.org/#title'][?(@.['@value'] == 
> Foto)].['@value']")));
>                 if (breadcump.size() != 0) {
>                     breadcumpPhoto = object;
>                 }
>             }
>             System.out.println("<--Test 2 start-->");
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['@value']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['http://rdf.data-vocabulary.org/#url']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
>             System.out.println("<--Test 2 end-->");
>         }
>     }
>     @Test
>     public void test3() throws IOException, SAXException, ParseException, 
> JSONException {
>         com.hp.hpl.jena.rdf.model.Model modelJena = 
> ModelFactory.createDefaultModel();
>         modelJena.read(IOUtils.toInputStream(testNtriples), 
> "http://www.fake.com";, "N-TRIPLE");
>         ByteArrayOutputStream osData = new java.io.ByteArrayOutputStream();
>         RDFDataMgr.write(osData, modelJena.getGraph(), Lang.JSONLD);
>         @SuppressWarnings("rawtypes")
>         Filter fData = 
> Filter.filter(Criteria.where("@type").exists(true).and("@type").in("http://rdf.data-vocabulary.org/#Breadcrumb";));
>         List<Object> breadcrumbs = JsonPath.read(osData.toString(), 
> "$..['@graph'][?]", fData);
>         try {
>             Object breadcumpPhoto = null;
>             for (Object object : breadcrumbs) {
>                 JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
> "$..title[?(@.['@value'] == Foto)].['@value']")));
>                 if (breadcump.size() != 0) {
>                     breadcumpPhoto = object;
>                 }
>             }
>             System.out.println("<--Test 3 start-->");
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['@value']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['http://rdf.data-vocabulary.org/#url']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
>             System.out.println("<--Test 3 end-->");
>         } catch (Exception e) {
>             System.out.println("Catch yes");
>             Object breadcumpPhoto = null;
>             for (Object object : breadcrumbs) {
>                 JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
> "$..['http://rdf.data-vocabulary.org/#title'][?(@.['@value'] == 
> Foto)].['@value']")));
>                 if (breadcump.size() != 0) {
>                     breadcumpPhoto = object;
>                 }
>             }
>             System.out.println("<--Test 3 start-->");
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['@value']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['http://rdf.data-vocabulary.org/#url']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
>             System.out.println("<--Test 3 end-->");
>         }
>     }
>     @Test
>     public void test4() throws IOException, SAXException, ParseException, 
> JSONException {
>         com.hp.hpl.jena.rdf.model.Model modelJena = 
> ModelFactory.createDefaultModel();
>         modelJena.read(IOUtils.toInputStream(testNtriples), 
> "http://www.fake.com";, "N-TRIPLE");
>         ByteArrayOutputStream osData = new java.io.ByteArrayOutputStream();
>         RDFDataMgr.write(osData, modelJena.getGraph(), Lang.JSONLD);
>         @SuppressWarnings("rawtypes")
>         Filter fData = 
> Filter.filter(Criteria.where("@type").exists(true).and("@type").in("http://rdf.data-vocabulary.org/#Breadcrumb";));
>         List<Object> breadcrumbs = JsonPath.read(osData.toString(), 
> "$..['@graph'][?]", fData);
>         try {
>             Object breadcumpPhoto = null;
>             for (Object object : breadcrumbs) {
>                 JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
> "$..title[?(@.['@value'] == Foto)].['@value']")));
>                 if (breadcump.size() != 0) {
>                     breadcumpPhoto = object;
>                 }
>             }
>             System.out.println("<--Test 4 start-->");
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['@value']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['http://rdf.data-vocabulary.org/#url']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
>             System.out.println("<--Test 4 end-->");
>         } catch (Exception e) {
>             System.out.println("Catch yes");
>             Object breadcumpPhoto = null;
>             for (Object object : breadcrumbs) {
>                 JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
> "$..['http://rdf.data-vocabulary.org/#title'][?(@.['@value'] == 
> Foto)].['@value']")));
>                 if (breadcump.size() != 0) {
>                     breadcumpPhoto = object;
>                 }
>             }
>             System.out.println("<--Test 4 start-->");
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['@value']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, 
> "$..['http://rdf.data-vocabulary.org/#url']"));
>             System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
>             System.out.println("<--Test 4 end-->");
>         }
>     }
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to