Skip to content

Commit 102e0cb

Browse files
committed
* Make simple_compact_iris the default (remove the option).
* For JSON-LD 1.1, favor simple terms ending with ':' * Otherwise, require terms to map to IRIs ending with a gen-delim `(/ . [ ] @ :)` For json-ld/json-ld.org#405.
1 parent 9e2901b commit 102e0cb

File tree

7 files changed

+116
-16
lines changed

7 files changed

+116
-16
lines changed

bin/jsonld

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ OPT_ARGS = [
115115
["--quiet", GetoptLong::NO_ARGUMENT, "Supress most output other than progress indicators"],
116116
["--rename_bnodes", GetoptLong::OPTIONAL_ARGUMENT,"Rename bnodes as part of expansion, or keep them the same"],
117117
["--requireAll", GetoptLong::OPTIONAL_ARGUMENT,"Rename bnodes as part of expansion, or keep them the same"],
118-
["--simple_compact_iris",GetoptLong::OPTIONAL_ARGUMENT,"When compacting IRIs, do not use terms with expanded term definitions"],
119118
["--stream", GetoptLong::NO_ARGUMENT, "Use Streaming reader/writer"],
120119
["--unique_bnodes", GetoptLong::OPTIONAL_ARGUMENT,"Use unique bnode identifiers"],
121120
["--uri", GetoptLong::REQUIRED_ARGUMENT,"URI to be used as the document base"],

lib/json/ld/api.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ class API
8181
# Rename bnodes as part of expansion, or keep them the same.
8282
# @option options [Boolean] :unique_bnodes (false)
8383
# Use unique bnode identifiers, defaults to using the identifier which the node was originally initialized with (if any).
84-
# @option options [Boolean] :simple_compact_iris (false)
85-
# When compacting IRIs, do not use terms with expanded term definitions
8684
# @option options [Symbol] :adapter used with MultiJson
8785
# @option options [Boolean] :validate Validate input, if a string or readable object.
8886
# @yield [api]

lib/json/ld/context.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ def self.parse(local_context, **options)
252252
# The callback of the loader to be used to retrieve remote documents and contexts. If specified, it must be used to retrieve remote documents and contexts; otherwise, if not specified, the processor's built-in loader must be used. See {API.documentLoader} for the method signature.
253253
# @option options [Hash{Symbol => String}] :prefixes
254254
# See `RDF::Reader#initialize`
255-
# @option options [Boolean] :simple_compact_iris (false)
256-
# When compacting IRIs, do not use terms with expanded term definitions
257255
# @option options [String, #to_s] :vocab
258256
# Initial value for @vocab
259257
# @option options [String, #to_s] :language
@@ -1156,17 +1154,32 @@ def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **opt
11561154
# The iri could not be compacted using the active context's vocabulary mapping. Try to create a compact IRI, starting by initializing compact IRI to null. This variable will be used to tore the created compact IRI, if any.
11571155
candidates = []
11581156

1157+
# For 1.1, favor simple terms that end in a ':'
11591158
term_definitions.each do |term, td|
1160-
next if term.include?(":")
1159+
# Skip things that don't end with a ':' or contain a ':'
1160+
next unless term.end_with?(':')
11611161
next if td.nil? || td.id.nil? || td.id == iri || !iri.start_with?(td.id)
11621162

1163-
# Also skip term if it was not a simple term and the :simple_compact_iris flag is true
1164-
next if @options[:simple_compact_iris] && !td.simple?
1163+
# Also skip term if it was not a simple term with an id ends in a gen-delim, or if the term ends with ':'
1164+
next unless td.simple?
1165+
1166+
suffix = iri[td.id.length..-1]
1167+
ciri = "#{term}#{suffix}"
1168+
candidates << ciri unless value && term_definitions.has_key?(ciri)
1169+
end if processingMode >= 'json-ld-1.1'
1170+
1171+
term_definitions.each do |term, td|
1172+
next if term =~ /:\w+/ # Skip things that already look like compact IRIs
1173+
next if td.nil? || td.id.nil? || td.id == iri || !iri.start_with?(td.id)
1174+
1175+
# Also skip term if it was not a simple term with an id ends in a gen-delim, or if the term ends with ':'
1176+
next unless td.simple?
1177+
next unless td.id.to_s.end_with?(*%w(: / ? # [ ] @))
11651178

11661179
suffix = iri[td.id.length..-1]
11671180
ciri = "#{term}:#{suffix}"
11681181
candidates << ciri unless value && term_definitions.has_key?(ciri)
1169-
end
1182+
end if candidates.empty?
11701183

11711184
if !candidates.empty?
11721185
#log_debug("") {"=> compact iri: #{candidates.term_sort.first.inspect}"} unless quiet

lib/json/ld/writer.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ def self.options
7777
datatype: TrueClass,
7878
on: ["--compact-arrays"],
7979
description: "Replaces arrays with just one element with that element during compaction.") {true},
80+
RDF::CLI::Option.new(
81+
symbol: :compactToRelative,
82+
datatype: TrueClass,
83+
on: ["--compact-to-relative"],
84+
description: "Creates document relative IRIs when compacting, if `true`, otherwise leaves expanded. Default is `true` use --no-compact-to-relative to disable.") {true},
8085
RDF::CLI::Option.new(
8186
symbol: :context,
8287
datatype: RDF::URI,

script/gen_context

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ require 'json/ld'
1515

1616
options = {
1717
output: STDOUT,
18-
prefixes: {},
19-
simple_compact_iris: true
18+
prefixes: {}
2019
}
2120

2221
OPT_ARGS = [

spec/compact_spec.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,94 @@
13361336
it(title) {run_compact(params)}
13371337
end
13381338
end
1339+
1340+
context "compact IRI selection" do
1341+
{
1342+
"does not compact using expanded term" => {
1343+
input: %({"http://example.org/foo": "term"}),
1344+
context: %({"ex": {"@id": "http://example.org/"}}),
1345+
output: %({
1346+
"@context": {"ex": {"@id": "http://example.org/"}},
1347+
"http://example.org/foo": "term"
1348+
})
1349+
},
1350+
"does not compact using simple term not ending in gen-delim" => {
1351+
input: %({"http://example.org/foo": "term"}),
1352+
context: %({"ex": "http://example.org/f"}),
1353+
output: %({
1354+
"@context": {"ex": "http://example.org/f"},
1355+
"http://example.org/foo": "term"
1356+
})
1357+
},
1358+
"compacts using simple term ending in gen-delim ('/')" => {
1359+
input: %({"http://example.org/foo": "term"}),
1360+
context: %({"ex": "http://example.org/"}),
1361+
output: %({
1362+
"@context": {"ex": "http://example.org/"},
1363+
"ex:foo": "term"
1364+
})
1365+
},
1366+
"compacts using simple term ending in gen-delim (':')" => {
1367+
input: %({"http://example.org/foo:bar": "term"}),
1368+
context: %({"ex": "http://example.org/foo:"}),
1369+
output: %({
1370+
"@context": {"ex": "http://example.org/foo:"},
1371+
"ex:bar": "term"
1372+
})
1373+
},
1374+
"compacts using simple term ending in gen-delim ('?')" => {
1375+
input: %({"http://example.org/foo?bar": "term"}),
1376+
context: %({"ex": "http://example.org/foo?"}),
1377+
output: %({
1378+
"@context": {"ex": "http://example.org/foo?"},
1379+
"ex:bar": "term"
1380+
})
1381+
},
1382+
"compacts using simple term ending in gen-delim ('#')" => {
1383+
input: %({"http://example.org/foo#bar": "term"}),
1384+
context: %({"ex": "http://example.org/foo#"}),
1385+
output: %({
1386+
"@context": {"ex": "http://example.org/foo#"},
1387+
"ex:bar": "term"
1388+
})
1389+
},
1390+
"compacts using simple term ending in gen-delim ('[')" => {
1391+
input: %({"http://example.org/foo[bar": "term"}),
1392+
context: %({"ex": "http://example.org/foo["}),
1393+
output: %({
1394+
"@context": {"ex": "http://example.org/foo["},
1395+
"ex:bar": "term"
1396+
})
1397+
},
1398+
"compacts using simple term ending in gen-delim (']')" => {
1399+
input: %({"http://example.org/foo]bar": "term"}),
1400+
context: %({"ex": "http://example.org/foo]"}),
1401+
output: %({
1402+
"@context": {"ex": "http://example.org/foo]"},
1403+
"ex:bar": "term"
1404+
})
1405+
},
1406+
"compacts using simple term ending in gen-delim ('@')" => {
1407+
input: %({"http://example.org/foo@bar": "term"}),
1408+
context: %({"ex": "http://example.org/foo@"}),
1409+
output: %({
1410+
"@context": {"ex": "http://example.org/foo@"},
1411+
"ex:bar": "term"
1412+
})
1413+
},
1414+
"prefers compacting using simple term ending ':' in 1.1" => {
1415+
input: %({"http://example.org/foo/bar": "term"}),
1416+
context: %({"ex": "http://example.org/foo/", "xe:": "http://example.org/foo/"}),
1417+
output: %({
1418+
"@context": {"ex": "http://example.org/foo/", "xe:": "http://example.org/foo/"},
1419+
"xe:bar": "term"
1420+
}),
1421+
processingMode: 'json-ld-1.1'
1422+
},
1423+
}.each do |title, params|
1424+
it(title) {run_compact(params)}
1425+
end
1426+
end
13391427
end
13401428

13411429
def run_compact(params)

spec/context_spec.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ def containers
858858
"unmapped" => ["foo", "foo"],
859859
"bnode" => ["_:a", RDF::Node("a")],
860860
"relative" => ["foo/bar", "http://base/foo/bar"],
861-
"odd CURIE" => ["exp:s", "http://example.org/perts"]
861+
"odd CURIE" => ["ex:perts", "http://example.org/perts"]
862862
}.each do |title, (result, input)|
863863
it title do
864864
expect(subject.compact_iri(input)).to produce(result, logger)
@@ -904,7 +904,7 @@ def containers
904904
subject.set_mapping("name", "http://xmlns.com/foaf/0.1/name")
905905
subject.set_mapping("ex", nil)
906906
expect(subject.compact_iri("http://example.org/name", position: :predicate)).
907-
to produce("lex:name", logger)
907+
not_to produce("name", logger)
908908
end
909909
end
910910

@@ -984,9 +984,7 @@ def containers
984984
end
985985
end
986986

987-
context "with :simple_compact_iris" do
988-
before(:each) { subject.instance_variable_get(:@options)[:simple_compact_iris] = true}
989-
987+
context "CURIE compaction" do
990988
{
991989
"nil" => [nil, nil],
992990
"absolute IRI" => ["http://example.com/", "http://example.com/"],

0 commit comments

Comments
 (0)