Skip to content

Commit 5466b7f

Browse files
committed
Add support for compacting named graphs where @container: @graph and @graphId. (reverted from commit 9de3c5b)
1 parent 2fb2216 commit 5466b7f

File tree

6 files changed

+18
-210
lines changed

6 files changed

+18
-210
lines changed

lib/json/ld.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ module LD
5252
@id
5353
@index
5454
@graph
55-
@graphId
5655
@language
5756
@list
5857
@nest
@@ -107,7 +106,6 @@ class InvalidBaseIRI < JsonLdError; @code = "invalid base IRI"; end
107106
class InvalidContainerMapping < JsonLdError; @code = "invalid container mapping"; end
108107
class InvalidDefaultLanguage < JsonLdError; @code = "invalid default language"; end
109108
class InvalidIdValue < JsonLdError; @code = "invalid @id value"; end
110-
class InvalidGraphIdMapping < JsonLdError; @code = "invalid @graphId mapping"; end
111109
class InvalidIndexValue < JsonLdError; @code = "invalid @index value"; end
112110
class InvalidVersionValue < JsonLdError; @code = "invalid @version value"; end
113111
class InvalidIRIMapping < JsonLdError; @code = "invalid IRI mapping"; end

lib/json/ld/compact.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,10 @@ def compact(element, property: nil)
163163

164164
container = context.container(item_active_property)
165165
as_array = context.as_array?(item_active_property)
166-
graph_id = context.graph_id(item_active_property)
167166

168167
value = case
169168
when list?(expanded_item) then expanded_item['@list']
170-
when graph?(expanded_item) then expanded_item['@graph']
169+
when simple_graph?(expanded_item) then expanded_item['@graph']
171170
else expanded_item
172171
end
173172

@@ -191,16 +190,11 @@ def compact(element, property: nil)
191190
end
192191
end
193192

194-
# handle @graph, not the value of a property with @content: @graph, or where graph_id does not match
195-
if graph?(expanded_item) && (container != '@graph' || graph_id != expanded_item['@id'])
193+
# handle simple @graph, not the value of a property with @content: @graph
194+
if simple_graph?(expanded_item) && container != '@graph'
196195
compacted_item = [compacted_item] unless compacted_item.is_a?(Array)
197196
al = context.compact_iri('@graph', vocab: true, quiet: true)
198197
compacted_item = {al => compacted_item}
199-
if expanded_item['@id']
200-
al = context.compact_iri('@id', vocab: true, quiet: true)
201-
# Do not compact using vocabulary-relative, which is not valid for @id
202-
compacted_item[al] = context.compact_iri(expanded_item['@id'], vocab: false, quiet: true).to_s
203-
end
204198
if expanded_item.has_key?('@index')
205199
key = context.compact_iri('@index', vocab: true, quiet: true)
206200
compacted_item[key] = expanded_item['@index']

lib/json/ld/context.rb

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ class TermDefinition
6262
# @return [Hash{String => Object}]
6363
attr_accessor :context
6464

65-
# Graph identifier, used with @container @graph
66-
# @return [String]
67-
attr_accessor :graph_id
68-
6965
# This is a simple term definition, not an expanded term definition
7066
# @return [Boolean] simple
7167
def simple?; simple; end
@@ -96,7 +92,6 @@ def initialize(term,
9692
nest: nil,
9793
simple: false,
9894
prefix: nil,
99-
graph_id: nil,
10095
context: nil)
10196
@term = term
10297
@id = id.to_s unless id.nil?
@@ -107,7 +102,6 @@ def initialize(term,
107102
@nest = nest unless nest.nil?
108103
@simple = simple
109104
@prefix = prefix unless prefix.nil?
110-
@graph_i = graph_id unless graph_id.nil?
111105
@context = context unless context.nil?
112106
end
113107

@@ -152,7 +146,6 @@ def to_context_definition(context)
152146
cm = [container_mapping, ('@set' if as_set?)].compact
153147
cm = cm.first if cm.length == 1
154148
defn['@container'] = cm unless cm.empty?
155-
defn['@graphId'] = context.compact_iri(graph_id, vocab: true) if graph_id
156149
# Language set as false to be output as null
157150
defn['@language'] = (@language_mapping ? @language_mapping : nil) unless @language_mapping.nil?
158151
defn['@context'] = @context if @context
@@ -195,7 +188,6 @@ def inspect
195188
v << "nest=#{nest.inspect}" unless nest.nil?
196189
v << "simple=true" if @simple
197190
v << "prefix=#{@prefix.inspect}" unless @prefix.nil?
198-
v << "graph_id=#{@graph_id.inspect}" unless @graph_id.nil?
199191
v << "has-context" unless context.nil?
200192
v.join(" ") + "]"
201193
end
@@ -593,7 +585,7 @@ def create_term_definition(local_context, term, defined)
593585

594586
expected_keys = case processingMode
595587
when "json-ld-1.0", nil then %w(@container @id @language @reverse @type)
596-
else %w(@container @context @graphId @id @language @nest @prefix @reverse @type)
588+
else %w(@container @context @id @language @nest @prefix @reverse @type)
597589
end
598590

599591
extra_keys = value.keys - expected_keys
@@ -724,28 +716,6 @@ def create_term_definition(local_context, term, defined)
724716
end
725717
end
726718

727-
if value.has_key?('@graphId')
728-
graph_id = value['@graphId']
729-
graph_id = case graph_id
730-
when String
731-
begin
732-
expand_iri(graph_id, vocab: true, documentRelative: false, local_context: local_context, defined: defined)
733-
rescue JsonLdError::InvalidIRIMapping
734-
raise JsonLdError::InvalidGraphIdMapping, "invalid mapping for '@graphId': #{graph_id.inspect} on term #{term.inspect}"
735-
end
736-
else
737-
:error
738-
end
739-
unless (graph_id.is_a?(RDF::URI) && graph_id.absolute?) || graph_id.is_a?(RDF::Node)
740-
raise JsonLdError::InvalidGraphIdMapping, "unknown mapping for '@graphId': #{graph_id.inspect} on term #{term.inspect}"
741-
end
742-
unless definition.container_mapping == '@graph'
743-
raise JsonLdError::InvalidGraphIdMapping, "'@graphId' only valid with @container: @graph: #{graph_id.inspect} on term #{term.inspect}"
744-
end
745-
#log_debug("") {"type_mapping: #{type.inspect}"}
746-
definition.graph_id = graph_id.to_s
747-
end
748-
749719
term_definitions[term] = definition
750720
defined[term] = true
751721
else
@@ -923,13 +893,13 @@ def as_array?(term)
923893
end
924894

925895
##
926-
# Retrieve graph_id of a term
896+
# Retrieve content of a term
927897
#
928898
# @param [Term, #to_s] term in unexpanded form
929-
# @return [String]
930-
def graph_id(term)
899+
# @return [Hash]
900+
def content(term)
931901
term = find_definition(term)
932-
term && term.graph_id
902+
term && term.content
933903
end
934904

935905
##
@@ -1156,7 +1126,6 @@ def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **opt
11561126
# TODO: "@graph@set"?
11571127
containers << '@graph'
11581128
containers << '@set'
1159-
graph_id = value['@id']
11601129
else
11611130
if value?(value)
11621131
if value.has_key?('@language') && !index?(value)
@@ -1189,7 +1158,7 @@ def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **opt
11891158
preferred_values.concat([tl_value, '@none'].compact)
11901159
end
11911160
#log_debug("") {"preferred_values: #{preferred_values.inspect}"} unless quiet
1192-
if p_term = select_term(iri, containers, tl, preferred_values, graph_id)
1161+
if p_term = select_term(iri, containers, tl, preferred_values)
11931162
#log_debug("") {"=> term: #{p_term.inspect}"} unless quiet
11941163
return p_term
11951164
end
@@ -1588,10 +1557,8 @@ def inverse_context
15881557
# indicates whether to look for a term with a matching type mapping or language mapping
15891558
# @param [Array<String>] preferred_values
15901559
# for the type mapping or language mapping
1591-
# @param [String] graph_id
1592-
# @graphId required on term when container matches on @graph
15931560
# @return [String]
1594-
def select_term(iri, containers, type_language, preferred_values, graph_id)
1561+
def select_term(iri, containers, type_language, preferred_values)
15951562
#log_debug("select_term") {
15961563
# "iri: #{iri.inspect}, " +
15971564
# "containers: #{containers.inspect}, " +
@@ -1606,11 +1573,6 @@ def select_term(iri, containers, type_language, preferred_values, graph_id)
16061573
value_map = tl_map[type_language]
16071574
preferred_values.each do |item|
16081575
next unless value_map.has_key?(item)
1609-
if container == '@graph'
1610-
# Graph names must not exist or exactly match
1611-
td = term_definitions[item]
1612-
next if td && td.graph_id != graph_id
1613-
end
16141576
#log_debug("=>") {value_map[item].inspect}
16151577
return value_map[item]
16161578
end

lib/json/ld/expand.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,11 @@ def expand_object(input, active_property, context, output_object, ordered: false
435435
#log_debug(" => ") { "convert #{expanded_value.inspect} to list"}
436436
expanded_value = [expanded_value] unless expanded_value.is_a?(Array)
437437
expanded_value = {'@graph' => expanded_value}
438-
# Add any associated graphId
439-
graph_id = active_context.graph_id(key)
440-
expanded_value['@id'] = context.expand_iri(graph_id, vocab: true).to_s if graph_id
441438
end
442439

443440
# Otherwise, if the term definition associated to key indicates that it is a reverse property
444441
# Spec FIXME: this is not an otherwise.
445-
if context.reverse?(key)
442+
if (td = context.term_definitions[key]) && td.reverse_property
446443
# If result has no @reverse member, create one and initialize its value to an empty JSON object.
447444
reverse_map = output_object['@reverse'] ||= {}
448445
[expanded_value].flatten.each do |item|

spec/compact_spec.rb

Lines changed: 7 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -813,36 +813,13 @@
813813
}
814814
})
815815
},
816-
"Compacts named graph" => {
816+
"Does not compacts graph with @id" => {
817817
input: %([{
818818
"http://example.org/input": [{
819-
"@id": "http://example.org/gid",
820819
"@graph": [{
821820
"http://example.org/value": [{"@value": "x"}]
822-
}]
823-
}]
824-
}]),
825-
context: %({
826-
"@vocab": "http://example.org/",
827-
"input": {"@container": "@graph", "@graphId": "gid"}
828-
}),
829-
output: %({
830-
"@context": {
831-
"@vocab": "http://example.org/",
832-
"input": {"@container": "@graph", "@graphId": "gid"}
833-
},
834-
"input": {
835-
"value": "x"
836-
}
837-
})
838-
},
839-
"Does not compact graph with @id without @graphId" => {
840-
input: %([{
841-
"http://example.org/input": [{
842-
"@id": "http://example.org/gid",
843-
"@graph": [{
844-
"http://example.org/value": [{"@value": "x"}]
845-
}]
821+
}],
822+
"@id": "http://example.org/id"
846823
}]
847824
}]),
848825
context: %({
@@ -852,38 +829,12 @@
852829
output: %({
853830
"@context": {
854831
"@vocab": "http://example.org/",
855-
"input": {"@container": "@graph"}
856-
},
857-
"input": {
858-
"@id": "http://example.org/gid",
859-
"@graph": [
860-
{
861-
"value": "x"
862-
}
863-
]
864-
}
865-
})
866-
},
867-
"Does not compacts graph with @id without matching @graphId" => {
868-
input: %([{
869-
"http://example.org/input": [{
870-
"@id": "http://example.org/gid",
871-
"@graph": [{
872-
"http://example.org/value": [{"@value": "x"}]
873-
}]
874-
}]
875-
}]),
876-
context: %({
877-
"@vocab": "http://example.org/",
878-
"input": {"@container": "@graph", "@graphId": "nomatch"}
879-
}),
880-
output: %({
881-
"@context": {
882-
"@vocab": "http://example.org/",
883-
"input": {"@container": "@graph", "@graphId": "nomatch"}
832+
"input": {
833+
"@container": "@graph"
834+
}
884835
},
885836
"input": {
886-
"@id": "http://example.org/gid",
837+
"@id": "http://example.org/id",
887838
"@graph": [
888839
{
889840
"value": "x"
@@ -892,62 +843,6 @@
892843
}
893844
})
894845
},
895-
"Compacts named graph" => {
896-
input: %([{
897-
"http://example.org/input": [{
898-
"@id": "http://example.org/gid",
899-
"@graph": [{
900-
"http://example.org/value": [{"@value": "x"}]
901-
}]
902-
}]
903-
}]),
904-
context: %({
905-
"@vocab": "http://example.org/",
906-
"input": {"@container": "@graph", "@graphId": "gid"}
907-
}),
908-
output: %({
909-
"@context": {
910-
"@vocab": "http://example.org/",
911-
"input": {"@container": "@graph", "@graphId": "gid"}
912-
},
913-
"input": {
914-
"value": "x"
915-
}
916-
})
917-
},
918-
"Raises error when @graphId is an invalid IRI" => {
919-
input: %([{
920-
"http://example.org/input": [{
921-
"@id": "http://example.org/gid",
922-
"@graph": [{
923-
"http://example.org/value": [{"@value": "x"}]
924-
}]
925-
}]
926-
}]),
927-
context: %({
928-
"input": {
929-
"@id": "http://example.org/",
930-
"@container": "@graph",
931-
"@graphId": "gid"
932-
}
933-
}),
934-
exception: JSON::LD::JsonLdError::InvalidGraphIdMapping
935-
},
936-
"Raises error when @graphId used without @container: @graph" => {
937-
input: %([{
938-
"http://example.org/input": [{
939-
"@id": "http://example.org/gid",
940-
"@graph": [{
941-
"http://example.org/value": [{"@value": "x"}]
942-
}]
943-
}]
944-
}]),
945-
context: %({
946-
"@vocab": "http://example.org/",
947-
"input": {"@container": "@set", "@graphId": "gid"}
948-
}),
949-
exception: JSON::LD::JsonLdError::InvalidGraphIdMapping
950-
},
951846
}.each_pair do |title, params|
952847
it(title) {run_compact({processingMode: "json-ld-1.1"}.merge(params))}
953848
end

spec/expand_spec.rb

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -958,44 +958,6 @@
958958
}]
959959
}])
960960
},
961-
"Creates a @graph container with an @graphId" => {
962-
input: %({
963-
"@context": {
964-
"@vocab": "http://example.org/",
965-
"input": {"@container": "@graph", "@graphId": "gid"}
966-
},
967-
"input": {
968-
"value": "x"
969-
}
970-
}),
971-
output: %([{
972-
"http://example.org/input": [{
973-
"@id": "http://example.org/gid",
974-
"@graph": [{
975-
"http://example.org/value": [{"@value": "x"}]
976-
}]
977-
}]
978-
}])
979-
},
980-
"Creates a @graph container with an blank @graphId" => {
981-
input: %({
982-
"@context": {
983-
"@vocab": "http://example.org/",
984-
"input": {"@container": "@graph", "@graphId": "_:gid"}
985-
},
986-
"input": {
987-
"value": "x"
988-
}
989-
}),
990-
output: %([{
991-
"http://example.org/input": [{
992-
"@id": "_:gid",
993-
"@graph": [{
994-
"http://example.org/value": [{"@value": "x"}]
995-
}]
996-
}]
997-
}])
998-
},
999961
}.each do |title, params|
1000962
it(title) {run_expand({processingMode: "json-ld-1.1"}.merge(params))}
1001963
end

0 commit comments

Comments
 (0)