Skip to content

Commit ab922e4

Browse files
committed
head to head
1 parent 88c574f commit ab922e4

File tree

4 files changed

+33
-91
lines changed

4 files changed

+33
-91
lines changed

characters.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
{
44
"firstname": "Ash",
55
"lastname":"Ketchum",
6-
"items":[ {"name_variation":"bowl of soup","weight":1000},
6+
"items":[ {"name_variation":"bowl of soup","weight":30},
77
{"name":"spoon","alias":"Ash's spoon"},
8-
{"name":"gloves", "weight":340,
8+
{"name":"gloves", "weight":20,
99
"wikidata_item":"Q169031"},
10-
{"name":"sword", "weight":44400}
10+
{"name":"sword", "weight":400}
1111
]
1212
},
1313
{
1414
"firstname": "Misty",
1515
"lastname":"Mountain",
16-
"items":[ {"name":"gloves", "weight":340},
16+
"items":[ {"name":"gloves", "weight":16},
1717
{"name_variation":"mittens"}
1818
]
1919
},

characters.pp.json

+1-60
Original file line numberDiff line numberDiff line change
@@ -1,60 +1 @@
1-
{
2-
"characters": [
3-
{
4-
"firstname": "Ash",
5-
"lastname": "Ketchum",
6-
"items": [
7-
{
8-
"name_variation": "bowl of soup",
9-
"weight": 1000,
10-
"_id": 1
11-
},
12-
{
13-
"name": "spoon",
14-
"alias": "Ash's spoon",
15-
"_id": 2
16-
},
17-
{
18-
"name": "gloves",
19-
"weight": 340,
20-
"wikidata_item": "Q169031",
21-
"_id": 3
22-
},
23-
{
24-
"name": "sword",
25-
"weight": 44400,
26-
"_id": 4
27-
}
28-
],
29-
"_id": 5
30-
},
31-
{
32-
"firstname": "Misty",
33-
"lastname": "Mountain",
34-
"items": [
35-
{
36-
"name": "gloves",
37-
"weight": 340,
38-
"_id": 6
39-
},
40-
{
41-
"name_variation": "mittens",
42-
"_id": 7
43-
}
44-
],
45-
"_id": 8
46-
},
47-
{
48-
"firstname": "Misty",
49-
"wikidata_item": "Q1057356",
50-
"items": [
51-
{
52-
"name": "hair tie",
53-
"_id": 9
54-
}
55-
],
56-
"_id": 10
57-
}
58-
],
59-
"_id": 11
60-
}
1+
{"characters":[{"firstname":"Ash","lastname":"Ketchum","items":[{"name_variation":"bowl of soup","weight":30,"_id":1},{"name":"spoon","alias":"Ash's spoon","_id":2},{"name":"gloves","weight":20,"wikidata_item":"Q169031","_id":3},{"name":"sword","weight":400,"_id":4}],"_id":5},{"firstname":"Misty","lastname":"Mountain","items":[{"name":"gloves","weight":16,"_id":6},{"name_variation":"mittens","_id":7}],"_id":8},{"firstname":"Misty","wikidata_item":"Q1057356","items":[{"name":"hair tie","_id":9}],"_id":10}],"_id":11}

json2rdf_and_sparql/query.sparql

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
prefix wd: <http://www.wikidata.org/entity/>
2+
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
13
prefix tmp: <http://tmp.com/#>
2-
prefix ex: <http://example.com/#>
4+
prefix ex: <http://example.com/>
35
prefix sch: <http://schema.org/>
4-
prefix owl: <http://www.w3.org/2002/07/owl>
6+
prefix owl: <http://www.w3.org/2002/07/owl#>
57
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
68

79
construct {?char a sch:Person .
8-
?char sch:has ?o .
910
?char sch:givenName ?fname .
1011
?char sch:familyName ?lname .
1112
?char owl:sameAs ?sameURI .
@@ -14,25 +15,23 @@ construct {?char a sch:Person .
1415
?collection sch:hasPart ?item .
1516
?item sch:name ?item_name .
1617
?item sch:weight ?item_weight_structure .
18+
?item rdf:type ?typeURI .
1719
?item_weight_structure rdf:value ?item_weight .
1820
?item_weight_structure ex:units ex:ounces .
19-
} where{
21+
} where {
2022
{ ?top_array tmp:characters ?char .
2123
bind(bnode() as ?collection) .} # make a collection for each character
22-
optional {?char tmp:firstname ?fname .}
23-
optional {?char tmp:lastname ?lname .}
24-
optional {?char tmp:wikidata_item ?same .
25-
bind(uri(concat("http://wikidata.org/entity/",?same)) as ?sameURI)}
26-
optional {?char tmp:items ?item .
27-
optional {?item tmp:name ?item_name .}
28-
optional {?item tmp:weight ?item_weight .
29-
bind(bnode() as ?item_weight_structure) # make a weight structure for each weight
30-
}
31-
.}
24+
optional {?char tmp:firstname ?fname .}
25+
optional {?char tmp:lastname ?lname .}
26+
optional {?char tmp:wikidata_item ?same .
27+
bind(uri(concat("http://www.wikidata.org/entity/",?same)) as ?sameURI)}
28+
optional {?char tmp:items ?item .
29+
optional {?item tmp:name ?item_name .}
30+
optional {?item tmp:wikidata_item ?item_type_q .
31+
bind(uri(concat("http://www.wikidata.org/entity/",?item_type_q)) as ?typeURI)}
32+
optional {?item tmp:weight ?item_weight .
33+
bind(bnode() as ?item_weight_structure) # make a weight structure for each weight
34+
}}
3235
}
3336

34-
# notes https://www.w3.org/TR/sparql11-query/#templatesWithBNodes does not work if you want the same blank node to used for multiple query solutions
35-
# so i needed to preprocess -- NOPE!
36-
37-
3837
# major thanks to https://stackoverflow.com/questions/46155991/produce-the-same-blank-node-in-construct-across-multiple-solutions

mapping.rml.ttl

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
44
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
55
@prefix : <http://example.org/rules/> .
6-
@prefix ex: <http://example.org/example/> .
6+
@prefix ex: <http://example.com/> .
77
@prefix schema: <http://schema.org/> .
88
@prefix dbo: <http://dbpedia.org/ontology/> .
99
@prefix owl: <http://www.w3.org/2002/07/owl#> .
1010

1111
# this approach assumes:
12-
# - the .json was preprocessed so that each object has a unique "_id" key.
12+
# - the .json was preprocessed so that each object has a unique "_id" key.
1313
# - that you want to error on the side of generating a blank node (only referencing a URI if you have enough individuating keys)
1414

1515
# it uses structured values (https://www.w3.org/TR/rdf-schema/#ch_value)
1616

17-
# NOTE i have not taken the time to pick proper predicates
18-
1917
# TODO how can i autoindent .ttl?
2018

2119
:Chars a rr:triplesMap ;
@@ -34,11 +32,15 @@ rr:constant schema:Person;
3432
];
3533
];
3634
rr:predicateObjectMap [
37-
rr:predicate schema:name;
35+
rr:predicate schema:givenName;
3836
rr:objectMap [ rml:reference "firstname" ]
3937
];
4038
rr:predicateObjectMap [
41-
rr:predicate schema:has;
39+
rr:predicate schema:familyName;
40+
rr:objectMap [ rml:reference "lastname" ]
41+
];
42+
rr:predicateObjectMap [
43+
rr:predicate schema:owns;
4244
rr:objectMap [ rr:template "char-{_id}-pos" ;
4345
rr:termType rr:BlankNode ]
4446
];
@@ -65,7 +67,7 @@ rr:objectMap [
6567
]
6668
];
6769
rr:predicateObjectMap [
68-
rr:predicate schema:contains;
70+
rr:predicate schema:hasPart;
6971
rr:objectMap [ rr:template "item-{items[*]._id}" ;
7072
rr:termType rr:BlankNode ]
7173
].
@@ -123,8 +125,8 @@ rr:predicateObjectMap [
123125
];
124126
];
125127
rr:predicateObjectMap [
126-
rr:predicate :units ;
128+
rr:predicate ex:units ;
127129
rr:objectMap [
128-
rr:constant :ounces ;
130+
rr:constant ex:ounces ;
129131
];
130132
].

0 commit comments

Comments
 (0)