@@ -41,10 +41,6 @@ class TermDefinition
41
41
# @return ['@index', '@language', '@index', '@type', '@id'] Container mapping
42
42
attr_reader :container_mapping
43
43
44
- # If container mapping was defined along with @set
45
- # @return [Boolean]
46
- attr_reader :as_set
47
-
48
44
# @return [String] Term used for nest properties
49
45
attr_accessor :nest
50
46
@@ -78,7 +74,7 @@ def prefix?; @prefix; end
78
74
# @param [String] term
79
75
# @param [String] id
80
76
# @param [String] type_mapping Type mapping
81
- # @param ['@index', '@language', '@index', '@set', '@type', '@id'] container_mapping
77
+ # @param [Array< '@index', '@language', '@index', '@set', '@type', '@id', '@graph'> ] container_mapping
82
78
# @param [String] language_mapping
83
79
# Language mapping of term, `false` is used if there is explicitly no language mapping for this term
84
80
# @param [Boolean] reverse_property
@@ -147,7 +143,7 @@ def to_context_definition(context)
147
143
end
148
144
end
149
145
150
- cm = [ container_mapping , ( '@set' if as_set ) ] . compact
146
+ cm = [ container_mapping , ( '@set' if as_set? ) ] . compact
151
147
cm = cm . first if cm . length == 1
152
148
defn [ '@container' ] = cm unless cm . empty?
153
149
# Language set as false to be output as null
@@ -168,21 +164,25 @@ def to_rb
168
164
%w( id type_mapping container_mapping language_mapping reverse_property nest simple prefix context ) . each do |acc |
169
165
v = instance_variable_get ( "@#{ acc } " . to_sym )
170
166
v = v . to_s if v . is_a? ( RDF ::Term )
171
- if acc == 'container_mapping' && as_set
167
+ if acc == 'container_mapping' && as_set?
172
168
v = v ? [ v , '@set' ] : '@set'
173
169
end
174
170
defn << "#{ acc } : #{ v . inspect } " if v
175
171
end
176
172
defn . join ( ', ' ) + ")"
177
173
end
178
174
175
+ # If container mapping was defined along with @set
176
+ # @return [Boolean]
177
+ def as_set? ; @as_set || false ; end
178
+
179
179
def inspect
180
180
v = %w( [TD )
181
181
v << "id=#{ @id } "
182
182
v << "term=#{ @term } "
183
183
v << "rev" if reverse_property
184
184
v << "container=#{ container_mapping } " if container_mapping
185
- v << "as_set=#{ as_set . inspect } "
185
+ v << "as_set=#{ as_set? . inspect } "
186
186
v << "lang=#{ language_mapping . inspect } " unless language_mapping . nil?
187
187
v << "type=#{ type_mapping } " unless type_mapping . nil?
188
188
v << "nest=#{ nest . inspect } " unless nest . nil?
@@ -876,21 +876,20 @@ def find_definition(term)
876
876
# @param [Term, #to_s] term in unexpanded form
877
877
# @return [String]
878
878
def container ( term )
879
- return '@set' if term == '@graph'
880
879
return term if KEYWORDS . include? ( term )
881
880
term = find_definition ( term )
882
881
term && term . container_mapping
883
882
end
884
883
885
884
##
886
- # Should values be represented as a set ?
885
+ # Should values be represented using an array ?
887
886
#
888
887
# @param [Term, #to_s] term in unexpanded form
889
888
# @return [Boolean]
890
889
def as_array? ( term )
891
- return true if term == '@graph' || term == '@list'
890
+ return true if CONTEXT_CONTAINER_ARRAY_TERMS . include? ( term )
892
891
term = find_definition ( term )
893
- term && ( term . as_set || term . container_mapping == '@list' )
892
+ term && ( term . as_set? || term . container_mapping == '@list' )
894
893
end
895
894
896
895
##
@@ -1122,6 +1121,11 @@ def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **opt
1122
1121
tl_value = common_language
1123
1122
end
1124
1123
#log_debug("") {"list: containers: #{containers.inspect}, type/language: #{tl.inspect}, type/language value: #{tl_value.inspect}"} unless quiet
1124
+ elsif graph? ( value )
1125
+ # TODO: support `@graphId`?
1126
+ # TODO: "@graph@set"?
1127
+ containers << '@graph'
1128
+ containers << '@set'
1125
1129
else
1126
1130
if value? ( value )
1127
1131
if value . has_key? ( '@language' ) && !index? ( value )
@@ -1442,6 +1446,8 @@ def alias(value)
1442
1446
1443
1447
private
1444
1448
1449
+ CONTEXT_CONTAINER_ARRAY_TERMS = %w( @set @list @graph ) . freeze
1450
+
1445
1451
def uri ( value )
1446
1452
case value . to_s
1447
1453
when /^_:(.*)$/
@@ -1482,7 +1488,26 @@ def bnode(value = nil)
1482
1488
#
1483
1489
# To make use of an inverse context, a list of preferred container mappings and the type mapping or language mapping are gathered for a particular value associated with an IRI. These parameters are then fed to the Term Selection algorithm, which will find the term that most appropriately matches the value's mappings.
1484
1490
#
1491
+ # @example Basic structure of resulting inverse context
1492
+ # {
1493
+ # "http://example.com/term": {
1494
+ # "@language": {
1495
+ # "@null": "term",
1496
+ # "@none": "term",
1497
+ # "en": "term"
1498
+ # },
1499
+ # "@type": {
1500
+ # "@reverse": "term",
1501
+ # "@none": "term",
1502
+ # "http://datatype": "term"
1503
+ # },
1504
+ # "@any": {
1505
+ # "@none": "term",
1506
+ # }
1507
+ # }
1508
+ # }
1485
1509
# @return [Hash{String => Hash{String => String}}]
1510
+ # @todo May want to include @set along with container to allow selecting terms using @set over those without @set. May require adding some notion of value cardinality to compact_iri
1486
1511
def inverse_context
1487
1512
@inverse_context ||= begin
1488
1513
result = { }
@@ -1491,7 +1516,14 @@ def inverse_context
1491
1516
a . length == b . length ? ( a <=> b ) : ( a . length <=> b . length )
1492
1517
end . each do |term |
1493
1518
next unless td = term_definitions [ term ]
1494
- container = td . container_mapping || ( td . as_set ? '@set' : '@none' )
1519
+
1520
+ container = td . container_mapping || ( td . as_set? ? '@set' : '@none' )
1521
+ # FIXME: Alternative to consider
1522
+ ## Creates "@language", "@language@set", "@set", or "@none"
1523
+ ## for each of "@language", "@index", "@type", "@id", "@list", and "@graph"
1524
+ #container = td.container_mapping.to_s
1525
+ #container += '@set' if td.as_set?
1526
+ #container = '@none' if container.empty?
1495
1527
container_map = result [ td . id . to_s ] ||= { }
1496
1528
tl_map = container_map [ container ] ||= { '@language' => { } , '@type' => { } , '@any' => { } }
1497
1529
type_map = tl_map [ '@type' ]
@@ -1630,7 +1662,7 @@ def check_container(container, local_context, defined, term)
1630
1662
# Okay
1631
1663
when '@language' , '@index' , nil
1632
1664
# Okay
1633
- when '@type' , '@id' , nil
1665
+ when '@type' , '@id' , '@graph' , nil
1634
1666
raise JsonLdError ::InvalidContainerMapping ,
1635
1667
"unknown mapping for '@container' to #{ container . inspect } on term #{ term . inspect } " if
1636
1668
( processingMode || 'json-ld-1.0' ) < 'json-ld-1.1'
0 commit comments