Skip to content

Commit

Permalink
Change @annotation to @Index.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Feb 10, 2013
1 parent deca083 commit 999d11e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 37 deletions.
14 changes: 7 additions & 7 deletions lib/json/ld/compact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ def compact(element, property = nil)
v = [v].compact unless v.is_a?(Array)
unless context.container(property) == '@list'
v = {compacted_key => v}
if element['@annotation']
compacted_key = context.compact_iri('@annotation', :position => :predicate, :depth => @depth)
v[compacted_key] = element['@annotation']
if element['@index']
compacted_key = context.compact_iri('@index', :position => :predicate, :depth => @depth)
v[compacted_key] = element['@index']
end
end
debug("compact") {"@list result, return as #{v.inspect}"}
Expand Down Expand Up @@ -141,7 +141,7 @@ def compact(element, property = nil)
compacted_value = compacted_value.first if compacted_value.length == 1 && @options[:compactArrays]
compacted_value
end
elsif key == '@annotation' && context.container(property) == '@annotation'
elsif key == '@index' && context.container(property) == '@index'
# Skip the annotation key if annotations being applied
next
else
Expand All @@ -154,7 +154,7 @@ def compact(element, property = nil)
end

# For each item in value:
value = [value] if key == '@annotation' && value.is_a?(String)
value = [value] if key == '@index' && value.is_a?(String)
raise ProcessingError, "found #{value.inspect} for #{key} of #{element.inspect}" unless value.is_a?(Array)
value.each do |item|
compacted_key = context.compact_iri(key, :position => :predicate, :value => item, :depth => @depth)
Expand All @@ -166,7 +166,7 @@ def compact(element, property = nil)
next if compacted_key.nil?

# Language maps and annotations
if field = %w(@language @annotation).detect {|kk| context.container(compacted_key) == kk}
if field = %w(@language @index).detect {|kk| context.container(compacted_key) == kk}
item_result = result[compacted_key] ||= Hash.new
item_key = item[field]
end
Expand Down Expand Up @@ -219,7 +219,7 @@ def nodesEquivalent?(n1, n2)
n1 == n2
elsif list?(n1)
list?(n2) &&
n1.fetch('@annotation', true) == n2.fetch('@annotation', true) &&
n1.fetch('@index', true) == n2.fetch('@index', true) &&
nodesEquivalent?(n1['@list'], n2['@list'])
elsif (node?(n1) || node_reference?(n2))
(node?(n2) || node_reference?(n2)) && n1['@id'] == n2['@id']
Expand Down
16 changes: 7 additions & 9 deletions lib/json/ld/evaluation_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ def initialize(options = {})
# on a remote context load error, syntax error, or a reference to a term which is not defined.
def parse(context)
case context
when nil
EvaluationContext.new
when EvaluationContext
debug("parse") {"context: #{context.inspect}"}
context.dup
Expand Down Expand Up @@ -265,7 +263,7 @@ def parse(context)
new_ec.set_coerce(key, iri)
end
when '@container'
raise InvalidContext::Syntax, "unknown mapping for '@container' to #{value2.inspect}" unless %w(@list @set @language @annotation).include?(value2)
raise InvalidContext::Syntax, "unknown mapping for '@container' to #{value2.inspect}" unless %w(@list @set @language @index).include?(value2)
if new_ec.container(key) != value2
debug("parse") {"container #{key.inspect} as #{value2.inspect}"}
new_ec.set_container(key, value2)
Expand Down Expand Up @@ -345,7 +343,7 @@ def serialize(options = {})
end

debug {"=> container(#{k}) => #{container(k)}"}
if %w(@list @set @language @annotation).include?(container(k))
if %w(@list @set @language @index).include?(container(k))
ctx[k]["@container"] = container(k)
debug {"=> container[#{k}] => #{container(k).inspect}"}
end
Expand Down Expand Up @@ -589,7 +587,7 @@ def compact_iri(iri, options = {})

# If value is a @list select terms that match every item equivalently.
debug("compact_iri", "#{value.inspect} is a list? #{list?(value).inspect}") if value
if list?(value) && !annotation?(value)
if list?(value) && !index?(value)
list_terms = matched_terms.select {|t| container(t) == '@list'}

terms = list_terms.inject({}) do |memo, t|
Expand Down Expand Up @@ -791,13 +789,13 @@ def compact_value(property, value, options = {})
depth(options) do
debug("compact_value") {"property: #{property.inspect}, value: #{value.inspect}, coerce: #{coerce(property).inspect}"}

# Remove @annotation if property has annotation
value.delete('@annotation') if container(property) == '@annotation'
# Remove @index if property has annotation
value.delete('@index') if container(property) == '@index'

result = case
when value.has_key?('@annotation')
when value.has_key?('@index')
# Don't compact the value
debug {" (@annotation without container @annotation)"}
debug {" (@index without container @index)"}
value
when coerce(property) == '@id' && value.has_key?('@id')
# Compact an @id coercion
Expand Down
26 changes: 13 additions & 13 deletions lib/json/ld/expand.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ def expand(input, active_property, context, options = {})
# If expanded property is @language, value must be a string with the lexical form described in [BCP47] or null. Set the @language member of result to the lowercased value.
raise ProcessingError::Lossy, "Value of #{expanded_property} must be a string, was #{value.inspect}" if value.is_a?(Hash) || value.is_a?(Array)
value.to_s.downcase
when '@annotation'
# If expanded property is @annotation value must be a string. Set the @annotation member of result to value.
when '@index'
# If expanded property is @index value must be a string. Set the @index member of result to value.
value = value.first if value.is_a?(Array) && value.length == 1
raise ProcessingError, "Value of @annotation is not a string: #{value.inspect}" unless value.is_a?(String)
raise ProcessingError, "Value of @index is not a string: #{value.inspect}" unless value.is_a?(String)
value.to_s
when '@list', '@set', '@graph'
# If expanded property is @set, @list, or @graph, set the expanded property member of result to the result of expanding value by recursively using this algorithm, along with the active context and active property. If expanded property is @list and active property is null or @graph, pass @list as active property instead.
Expand Down Expand Up @@ -158,19 +158,19 @@ def expand(input, active_property, context, options = {})
end
# Set the value associated with property to the multilingual array.
language_map_values
elsif context.container(property) == '@annotation' && value.is_a?(Hash)
# Otherwise, if value is a JSON object and property is not a keyword and its associated term entry in the active context has a @container key associated with a value of @annotation, process the associated value as a annotation:
elsif context.container(property) == '@index' && value.is_a?(Hash)
# Otherwise, if value is a JSON object and property is not a keyword and its associated term entry in the active context has a @container key associated with a value of @index, process the associated value as a annotation:

# Set ary to an empty array.
annotation_map_values = []

# For each key-value in the object:
value.keys.sort.each do |k|
[value[k]].flatten.each do |v|
# Expand the value, adding an '@annotation' key with value equal to the key
# Expand the value, adding an '@index' key with value equal to the key
expanded_value = depth { expand(v, property, context, options) }
next unless expanded_value
expanded_value['@annotation'] ||= k
expanded_value['@index'] ||= k
annotation_map_values << expanded_value
end
end
Expand Down Expand Up @@ -220,10 +220,10 @@ def expand(input, active_property, context, options = {})

debug("output object") {output_object.inspect}

# If the active property is null or @graph and element has a @value member without an @annotation member, or element consists of only an @id member, set element to null.
# If the active property is null or @graph and element has a @value member without an @index member, or element consists of only an @id member, set element to null.
debug("output object(ap)") {((active_property || '@graph') == '@graph').inspect}
if (active_property || '@graph') == '@graph' &&
((output_object.has_key?('@value') && !output_object.has_key?('@annotation')) ||
((output_object.has_key?('@value') && !output_object.has_key?('@index')) ||
(output_object.keys - %w(@id)).empty?)
debug("empty top-level") {output_object.inspect}
return nil
Expand All @@ -233,8 +233,8 @@ def expand(input, active_property, context, options = {})
if output_object.has_key?('@value')
output_object.delete('@language') if output_object['@language'].to_s.empty?
output_object.delete('@type') if output_object['@type'].to_s.empty?
if (%w(@annotation @language @type) - output_object.keys).empty?
raise ProcessingError, "element must not have more than one other property other than @annotation, which can either be @language or @type with a string value." unless value.is_a?(String)
if (%w(@index @language @type) - output_object.keys).empty?
raise ProcessingError, "element must not have more than one other property other than @index, which can either be @language or @type with a string value." unless value.is_a?(String)
end

# if the value of @value equals null, replace element with the value of null.
Expand All @@ -245,10 +245,10 @@ def expand(input, active_property, context, options = {})
output_object['@type'] = [output_object['@type']]
end

# If element has an @set or @list property, it must be the only property (other tha @annotation). Set element to the value of @set;
# If element has an @set or @list property, it must be the only property (other tha @index). Set element to the value of @set;
# leave @list untouched.
if !(%w(@set @list) & output_object.keys).empty?
o_keys = output_object.keys - %w(@set @list @annotation)
o_keys = output_object.keys - %w(@set @list @index)
raise ProcessingError, "element must have only @set or @list: #{output_object.keys.inspect}" if o_keys.length > 1

output_object = output_object.values.first unless output_object.has_key?('@list')
Expand Down
4 changes: 2 additions & 2 deletions lib/json/ld/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def list?(value)
#
# @param [Object] value
# @return [Boolean]
def annotation?(value)
value.is_a?(Hash) && value.has_key?('@annotation')
def index?(value)
value.is_a?(Hash) && value.has_key?('@index')
end

##
Expand Down
4 changes: 2 additions & 2 deletions spec/evaluation_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -949,10 +949,10 @@ def ctx.content_type; "application/ld+json"; end
let(:ctx) do
subject.parse({"name" => {"@id" => "http://example.com/property", "@container" => "@list"}})
end
it "Does not use @list with @annotation" do
it "Does not use @list with @index" do
ctx.compact_iri("http://example.com/property", :value => {
"@list" => ["one item"],
"@annotation" => "an annotation"
"@index" => "an annotation"
}).should produce("http://example.com/property", @debug)
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/expand_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@
"@context" => {
"container" => {
"@id" => "http://example.com/container",
"@container" => "@annotation"
"@container" => "@index"
}
},
"@id" => "http://example.com/annotationsTest",
Expand All @@ -739,9 +739,9 @@
{
"@id" => "http://example.com/annotationsTest",
"http://example.com/container" => [
{"@value" => "Die Königin", "@annotation" => "de"},
{"@value" => "Ihre Majestät", "@annotation" => "de"},
{"@value" => "The Queen", "@annotation" => "en"}
{"@value" => "Die Königin", "@index" => "de"},
{"@value" => "Ihre Majestät", "@index" => "de"},
{"@value" => "The Queen", "@index" => "en"}
]
}
]
Expand Down

0 comments on commit 999d11e

Please sign in to comment.