Skip to content

Commit 9396d1f

Browse files
committed
Added fix for wrong rdf:type to @type conversion
1 parent e337766 commit 9396d1f

File tree

7 files changed

+132
-33
lines changed

7 files changed

+132
-33
lines changed

core/src/main/java/com/github/jsonldjava/core/JsonLdApi.java

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,15 @@ public UsagesNode(NodeMapNode node, String property, Map<String, Object> value)
18611861
public Map<String, Object> value = null;
18621862
}
18631863

1864+
private class Node {
1865+
private String predicate;
1866+
private RDFDataset.Node object;
1867+
public Node(String predicate, RDFDataset.Node object) {
1868+
this.predicate = predicate;
1869+
this.object = object;
1870+
}
1871+
}
1872+
18641873
private class NodeMapNode extends LinkedHashMap<String, Object> {
18651874
public List<UsagesNode> usages = new ArrayList(4);
18661875

@@ -1968,48 +1977,61 @@ public List<Object> fromRDF(final RDFDataset dataset, boolean noDuplicatesInData
19681977
}
19691978

19701979
// 3.5)
1980+
final Map<String, List<Node>> nodes = new HashMap<>();
1981+
19711982
for (final RDFDataset.Quad triple : graph) {
19721983
final String subject = triple.getSubject().getValue();
19731984
final String predicate = triple.getPredicate().getValue();
19741985
final RDFDataset.Node object = triple.getObject();
1986+
final List<Node> list = nodes.getOrDefault(subject, new ArrayList());
1987+
list.add(new Node(predicate, object));
1988+
nodes.put(subject, list);
1989+
}
1990+
for (final Map.Entry<String, List<Node>> nodeEntry : nodes.entrySet()) {
1991+
final String subject = nodeEntry.getKey();
1992+
1993+
for (final Node n : nodeEntry.getValue()) {
1994+
final String predicate = n.predicate;
1995+
final RDFDataset.Node object = n.object;
1996+
1997+
// 3.5.1+3.5.2)
1998+
NodeMapNode node;
1999+
if (!nodeMap.containsKey(subject)) {
2000+
node = new NodeMapNode(subject);
2001+
nodeMap.put(subject, node);
2002+
} else {
2003+
node = nodeMap.get(subject);
2004+
}
19752005

1976-
// 3.5.1+3.5.2)
1977-
NodeMapNode node;
1978-
if (!nodeMap.containsKey(subject)) {
1979-
node = new NodeMapNode(subject);
1980-
nodeMap.put(subject, node);
1981-
} else {
1982-
node = nodeMap.get(subject);
1983-
}
1984-
1985-
// 3.5.3)
1986-
if ((object.isIRI() || object.isBlankNode())
1987-
&& !nodeMap.containsKey(object.getValue())) {
1988-
nodeMap.put(object.getValue(), new NodeMapNode(object.getValue()));
1989-
}
2006+
// 3.5.3)
2007+
if ((object.isIRI() || object.isBlankNode())
2008+
&& !nodeMap.containsKey(object.getValue())) {
2009+
nodeMap.put(object.getValue(), new NodeMapNode(object.getValue()));
2010+
}
19902011

1991-
// 3.5.4)
1992-
if (RDF_TYPE.equals(predicate) && (object.isIRI() || object.isBlankNode())
1993-
&& !opts.getUseRdfType()) {
1994-
JsonLdUtils.mergeValue(node, JsonLdConsts.TYPE, object.getValue());
1995-
continue;
1996-
}
2012+
// 3.5.4)
2013+
if (RDF_TYPE.equals(predicate) && (object.isIRI() || object.isBlankNode())
2014+
&& !opts.getUseRdfType() && !nodes.containsKey(object.getValue())) {
2015+
JsonLdUtils.mergeValue(node, JsonLdConsts.TYPE, object.getValue());
2016+
continue;
2017+
}
19972018

1998-
// 3.5.5)
1999-
final Map<String, Object> value = object.toObject(opts.getUseNativeTypes());
2019+
// 3.5.5)
2020+
final Map<String, Object> value = object.toObject(opts.getUseNativeTypes());
20002021

2001-
// 3.5.6+7)
2002-
if (noDuplicatesInDataset) {
2003-
JsonLdUtils.laxMergeValue(node, predicate, value);
2004-
} else {
2005-
JsonLdUtils.mergeValue(node, predicate, value);
2006-
}
2022+
// 3.5.6+7)
2023+
if (noDuplicatesInDataset) {
2024+
JsonLdUtils.laxMergeValue(node, predicate, value);
2025+
} else {
2026+
JsonLdUtils.mergeValue(node, predicate, value);
2027+
}
20072028

2008-
// 3.5.8)
2009-
if (object.isBlankNode() || object.isIRI()) {
2010-
// 3.5.8.1-3)
2011-
nodeMap.get(object.getValue()).usages
2012-
.add(new UsagesNode(node, predicate, value));
2029+
// 3.5.8)
2030+
if (object.isBlankNode() || object.isIRI()) {
2031+
// 3.5.8.1-3)
2032+
nodeMap.get(object.getValue()).usages
2033+
.add(new UsagesNode(node, predicate, value));
2034+
}
20132035
}
20142036
}
20152037
}

core/src/test/java/com/github/jsonldjava/core/JsonLdFramingTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,29 @@ public void testFrame0009() throws IOException, JsonLdError {
152152
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0009-out.jsonld"));
153153
assertEquals(out, frame2);
154154
}
155+
156+
@Test
157+
public void testFrame0010() throws IOException, JsonLdError {
158+
final Object frame = JsonUtils
159+
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0010-frame.jsonld"));
160+
//{
161+
// "@id": "http://example.com/main/id",
162+
// "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": {
163+
// "@id": "http://example.com/rdf/id",
164+
// "http://www.w3.org/1999/02/22-rdf-syntax-ns#label": "someLabel"
165+
// }
166+
//}
167+
final RDFDataset ds = new RDFDataset();
168+
ds.addTriple("http://example.com/main/id", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://example.com/rdf/id");
169+
ds.addTriple("http://example.com/rdf/id", "http://www.w3.org/1999/02/22-rdf-syntax-ns#label", "someLabel", null, null);
170+
final JsonLdOptions opts = new JsonLdOptions();
171+
opts.setProcessingMode(JsonLdOptions.JSON_LD_1_0);
172+
173+
final Object in = new JsonLdApi(opts).fromRDF(ds, true);
174+
175+
final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts);
176+
final Object out = JsonUtils
177+
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0010-out.jsonld"));
178+
assertEquals(out, frame2);
179+
}
155180
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"@context" : {
3+
"rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
4+
},
5+
"@id" : "http://example.com/main/id"
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"@context" : {
3+
"rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
4+
},
5+
"@graph" : [ {
6+
"@id" : "http://example.com/main/id",
7+
"rdf:type" : {
8+
"@id" : "http://example.com/rdf/id",
9+
"rdf:label" : "someLabel"
10+
}
11+
}
12+
]
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<http://example.com/Subj1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Type> .
2+
<http://example.com/Type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#label> "myLabel" .
3+
<http://example.com/Type> <http://example.com/prop2> "2012-05-12"^^<http://www.w3.org/2001/XMLSchema#date> .
4+
<http://example.com/Subj1> <http://example.com/prop1> <http://example.com/Obj1> .
5+
<http://example.com/Subj1> <http://example.com/prop2> "Plain" .
6+
<http://example.com/Subj1> <http://example.com/prop2> "2012-05-12"^^<http://www.w3.org/2001/XMLSchema#date> .
7+
<http://example.com/Subj1> <http://example.com/prop2> "English"@en .
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"@id": "http://example.com/Subj1",
4+
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type" : [{
5+
"@id": "http://example.com/Type"
6+
}],
7+
"http://example.com/prop1": [{"@id": "http://example.com/Obj1"}],
8+
"http://example.com/prop2": [
9+
{"@value": "Plain"},
10+
{"@value": "2012-05-12", "@type": "http://www.w3.org/2001/XMLSchema#date"},
11+
{"@value": "English", "@language": "en"}
12+
]
13+
},
14+
{
15+
"@id": "http://example.com/Type",
16+
"http://www.w3.org/1999/02/22-rdf-syntax-ns#label": [{"@value": "myLabel"}],
17+
"http://example.com/prop2": [{"@value": "2012-05-12", "@type": "http://www.w3.org/2001/XMLSchema#date"}]
18+
}
19+
]

core/src/test/resources/json-ld.org/fromRdf-manifest.jsonld

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@
145145
},
146146
"input": "fromRdf-0019-in.nq",
147147
"expect": "fromRdf-0019-out.jsonld"
148+
}, {
149+
"@id": "#t0020",
150+
"@type": ["jld:PositiveEvaluationTest", "jld:FromRDFTest"],
151+
"name": "rdf:type as an @id with values",
152+
"purpose": "Tests the proper formatting of @type (even with useRdfType to false) into rdf:type when the object contains more triples.",
153+
"input": "fromRdf-0020-in.nq",
154+
"expect": "fromRdf-0020-out.jsonld"
148155
}
149156
]
150157
}

0 commit comments

Comments
 (0)