FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Expanded bnode list Option by daltontc · Pull Request #335 · jsonld-java/jsonld-java · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
117 changes: 59 additions & 58 deletions core/src/main/java/com/github/jsonldjava/core/JsonLdApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2027,71 +2027,72 @@ public List<Object> fromRDF(final RDFDataset dataset, boolean noDuplicatesInData
}
}

// 4)
for (final String name : graphMap.keySet()) {
final Map<String, NodeMapNode> graph = graphMap.get(name);
if (!opts.getExpandBNodeList()) {
// 4)
for (final String name : graphMap.keySet()) {
final Map<String, NodeMapNode> graph = graphMap.get(name);

// 4.1)
if (!graph.containsKey(RDF_NIL)) {
continue;
}
// 4.1)
if (!graph.containsKey(RDF_NIL)) {
continue;
}

// 4.2)
final NodeMapNode nil = graph.get(RDF_NIL);
// 4.3)
for (final UsagesNode usage : nil.usages) {
// 4.3.1)
NodeMapNode node = usage.node;
String property = usage.property;
Map<String, Object> head = usage.value;
// 4.3.2)
final List<Object> list = new ArrayList<Object>(4);
final List<String> listNodes = new ArrayList<String>(4);
// 4.3.3)
while (RDF_REST.equals(property) && node.isWellFormedListNode()) {
// 4.3.3.1)
list.add(((List<Object>) node.get(RDF_FIRST)).get(0));
// 4.3.3.2)
listNodes.add((String) node.get(JsonLdConsts.ID));
// 4.3.3.3)
final UsagesNode nodeUsage = node.usages.get(0);
// 4.3.3.4)
node = nodeUsage.node;
property = nodeUsage.property;
head = nodeUsage.value;
// 4.3.3.5)
if (!JsonLdUtils.isBlankNode(node)) {
break;
// 4.2)
final NodeMapNode nil = graph.get(RDF_NIL);
// 4.3)
for (final UsagesNode usage : nil.usages) {
// 4.3.1)
NodeMapNode node = usage.node;
String property = usage.property;
Map<String, Object> head = usage.value;
// 4.3.2)
final List<Object> list = new ArrayList<Object>(4);
final List<String> listNodes = new ArrayList<String>(4);
// 4.3.3)
while (RDF_REST.equals(property) && node.isWellFormedListNode()) {
// 4.3.3.1)
list.add(((List<Object>) node.get(RDF_FIRST)).get(0));
// 4.3.3.2)
listNodes.add((String) node.get(JsonLdConsts.ID));
// 4.3.3.3)
final UsagesNode nodeUsage = node.usages.get(0);
// 4.3.3.4)
node = nodeUsage.node;
property = nodeUsage.property;
head = nodeUsage.value;
// 4.3.3.5)
if (!JsonLdUtils.isBlankNode(node)) {
break;
}
}
}
// 4.3.4)
if (RDF_FIRST.equals(property)) {
// 4.3.4.1)
if (RDF_NIL.equals(node.get(JsonLdConsts.ID))) {
continue;
// 4.3.4)
if (RDF_FIRST.equals(property)) {
// 4.3.4.1)
if (RDF_NIL.equals(node.get(JsonLdConsts.ID))) {
continue;
}
// 4.3.4.3)
final String headId = (String) head.get(JsonLdConsts.ID);
// 4.3.4.4-5)
head = (Map<String, Object>) ((List<Object>) graph.get(headId).get(RDF_REST))
.get(0);
// 4.3.4.6)
list.remove(list.size() - 1);
listNodes.remove(listNodes.size() - 1);
}
// 4.3.5)
head.remove(JsonLdConsts.ID);
// 4.3.6)
Collections.reverse(list);
// 4.3.7)
head.put(JsonLdConsts.LIST, list);
// 4.3.8)
for (final String nodeId : listNodes) {
graph.remove(nodeId);
}
// 4.3.4.3)
final String headId = (String) head.get(JsonLdConsts.ID);
// 4.3.4.4-5)
head = (Map<String, Object>) ((List<Object>) graph.get(headId).get(RDF_REST))
.get(0);
// 4.3.4.6)
list.remove(list.size() - 1);
listNodes.remove(listNodes.size() - 1);
}
// 4.3.5)
head.remove(JsonLdConsts.ID);
// 4.3.6)
Collections.reverse(list);
// 4.3.7)
head.put(JsonLdConsts.LIST, list);
// 4.3.8)
for (final String nodeId : listNodes) {
graph.remove(nodeId);
}
}
}

// 5)
final List<Object> result = new ArrayList<Object>(4);
// 6)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public JsonLdOptions copy() {
copy.setPruneBlankNodeIdentifiers(pruneBlankNodeIdentifiers);
copy.setRequireAll(requireAll);
copy.setAllowContainerSetOnType(allowContainerSetOnType);
copy.setExpandBNodeList(expandBNodeList);
copy.setUseRdfType(useRdfType);
copy.setUseNativeTypes(useNativeTypes);
copy.setProduceGeneralizedRdf(produceGeneralizedRdf);
Expand Down Expand Up @@ -102,6 +103,7 @@ public JsonLdOptions copy() {
private Boolean pruneBlankNodeIdentifiers = false;
private Boolean requireAll = false;
private Boolean allowContainerSetOnType = false;
private Boolean expandBNodeList = false;

// RDF conversion options :
// http://www.w3.org/TR/json-ld-api/#serialize-rdf-as-json-ld-algorithm
Expand Down Expand Up @@ -225,6 +227,14 @@ public void setAllowContainerSetOnType(Boolean allowContainerSetOnType) {
this.allowContainerSetOnType = allowContainerSetOnType;
}

public Boolean getExpandBNodeList() {
return expandBNodeList;
}

public void setExpandBNodeList(Boolean expandBNodeList) {
this.expandBNodeList = expandBNodeList;
}

public Boolean getCompactArrays() {
return compactArrays;
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jsonldjava.core;

import com.github.jsonldjava.utils.JsonUtils;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -28,4 +29,39 @@ public void testIssue301() throws JsonLdError {
out2.toString());
}

@Test
public void testExpandedList() throws Exception {
RDFDataset rdf = new RDFDataset();
rdf.addTriple(
"_:b0",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#first",
"Arnold");
rdf.addTriple(
"_:b0",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest",
"_:b1");
rdf.addTriple(
"_:b1",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#first",
"Bob");
rdf.addTriple(
"_:b1",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest",
"_:b2");
rdf.addTriple(
"_:b2",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#first",
"Catherine");
rdf.addTriple(
"_:b2",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#nil");

JsonLdOptions opts = new JsonLdOptions();
assertEquals("[{\"@id\":\"_:b0\",\"http://www.w3.org/1999/02/22-rdf-syntax-ns#first\":[{\"@id\":\"Arnold\"}],\"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest\":[{\"@list\":[{\"@id\":\"Bob\"},{\"@id\":\"Catherine\"}]}]}]", JsonUtils.toString(new JsonLdApi(opts).fromRDF(rdf)));

opts.setExpandBNodeList(true);
assertEquals("[{\"@id\":\"_:b0\",\"http://www.w3.org/1999/02/22-rdf-syntax-ns#first\":[{\"@id\":\"Arnold\"}],\"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest\":[{\"@id\":\"_:b1\"}]},{\"@id\":\"_:b1\",\"http://www.w3.org/1999/02/22-rdf-syntax-ns#first\":[{\"@id\":\"Bob\"}],\"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest\":[{\"@id\":\"_:b2\"}]},{\"@id\":\"_:b2\",\"http://www.w3.org/1999/02/22-rdf-syntax-ns#first\":[{\"@id\":\"Catherine\"}],\"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest\":[{\"@id\":\"http://www.w3.org/1999/02/22-rdf-syntax-ns#nil\"}]}]", JsonUtils.toString(new JsonLdApi(opts).fromRDF(rdf)));

}
}

Back | FazBrowse Home | New Git URL