lib/json/ld/evaluation_context.rb in json-ld-0.1.6.1 vs lib/json/ld/evaluation_context.rb in json-ld-0.3.0
- old
+ new
@@ -181,11 +181,11 @@
context.delete(key)
debug("parse") {"Set #{key} to #{v.inspect}"}
new_ec.send(setter, v)
elsif v
raise InvalidContext::Syntax, "#{key.inspect} is invalid"
- end
+ end
end
num_updates = 1
while num_updates > 0 do
num_updates = 0
@@ -311,11 +311,11 @@
end
debug {"=> coerce(#{k}) => #{coerce(k)}"}
if coerce(k) && !NATIVE_DATATYPES.include?(coerce(k))
dt = coerce(k)
- dt = compact_iri(dt, :position => :datatype) unless dt == '@id'
+ dt = compact_iri(dt, :position => :type) unless dt == '@id'
# Fold into existing definition
ctx[k]["@type"] = dt
debug {"=> datatype[#{k}] => #{dt}"}
end
@@ -476,11 +476,11 @@
# Expand an IRI. Relative IRIs are expanded against any document base.
#
# @param [String] iri
# A keyword, term, prefix:suffix or possibly relative IRI
# @param [Hash{Symbol => Object}] options
- # @option options [:subject, :predicate, :object, :datatype] position
+ # @option options [:subject, :predicate, :type] position
# Useful when determining how to serialize.
# @option options [RDF::URI] base (self.base)
# Base IRI to use when expanding relative IRIs.
#
# @return [RDF::URI, String] IRI or String, if it's a keyword
@@ -489,11 +489,11 @@
def expand_iri(iri, options = {})
return iri unless iri.is_a?(String)
prefix, suffix = iri.split(':', 2)
return mapping(iri) if mapping(iri) # If it's an exact match
debug("expand_iri") {"prefix: #{prefix.inspect}, suffix: #{suffix.inspect}, vocab: #{vocab.inspect}"} unless options[:quiet]
- base = [:subject, :object].include?(options[:position]) ? options.fetch(:base, self.base) : nil
+ base = [:subject].include?(options[:position]) ? options.fetch(:base, self.base) : nil
prefix = prefix.to_s
case
when prefix == '_' && suffix then bnode(suffix)
when iri.to_s[0,1] == "@" then iri
when suffix.to_s[0,2] == '//' then uri(iri)
@@ -501,20 +501,20 @@
when base then base.join(iri)
when vocab then uri("#{vocab}#{iri}")
else
# Otherwise, it must be an absolute IRI
u = uri(iri)
- u if u.absolute? || [:subject, :object].include?(options[:position])
+ u if u.absolute? || [:subject].include?(options[:position])
end
end
##
# Compact an IRI
#
# @param [RDF::URI] iri
# @param [Hash{Symbol => Object}] options ({})
- # @option options [:subject, :predicate, :object, :datatype] position
+ # @option options [:subject, :predicate, :type] position
# Useful when determining how to serialize.
# @option options [Object] :value
# Value, used to select among various maps for the same IRI
# @option options [Boolean] :not_term (false)
# Don't return a term, but only a CURIE or IRI.
@@ -526,59 +526,58 @@
debug {"compact_iri(#{iri.inspect}, #{options.inspect})"}
value = options.fetch(:value, nil)
# Get a list of terms which map to iri
- terms = mappings.keys.select {|t| mapping(t).to_s == iri}
+ matched_terms = mappings.keys.select {|t| mapping(t).to_s == iri}
+ debug("compact_iri", "initial terms: #{matched_terms.inspect}")
- # Create an association term map for terms to their associated
- # term rank.
- term_map = {}
+ # Create an empty list of terms _terms_ that will be populated with terms that are ranked according to how closely they match value. Initialize highest rank to 0, and set a flag list container to false.
+ terms = {}
# If value is a @list add a term rank for each
# term mapping to iri which has @container @list.
debug("compact_iri", "#{value.inspect} is a list? #{list?(value).inspect}")
if list?(value)
- list_terms = terms.select {|t| container(t) == '@list'}
+ list_terms = matched_terms.select {|t| container(t) == '@list'}
- term_map = list_terms.inject({}) do |memo, t|
+ terms = list_terms.inject({}) do |memo, t|
memo[t] = term_rank(t, value)
memo
end unless list_terms.empty?
- debug("term map") {"remove zero rank terms: #{term_map.keys.select {|t| term_map[t] == 0}}"} if term_map.any? {|t,r| r == 0}
- term_map.delete_if {|t, r| r == 0}
+ debug("term map") {"remove zero rank terms: #{terms.keys.select {|t| terms[t] == 0}}"} if terms.any? {|t,r| r == 0}
+ terms.delete_if {|t, r| r == 0}
end
# Otherwise, value is @value or a native type.
# Add a term rank for each term mapping to iri
# which does not have @container @list
- if term_map.empty?
- non_list_terms = terms.reject {|t| container(t) == '@list'}
+ if terms.empty?
+ non_list_terms = matched_terms.reject {|t| container(t) == '@list'}
# If value is a @list, exclude from term map those terms
# with @container @set
non_list_terms.reject {|t| container(t) == '@set'} if list?(value)
- term_map = non_list_terms.inject({}) do |memo, t|
+ terms = non_list_terms.inject({}) do |memo, t|
memo[t] = term_rank(t, value)
memo
end unless non_list_terms.empty?
- debug("term map") {"remove zero rank terms: #{term_map.keys.select {|t| term_map[t] == 0}}"} if term_map.any? {|t,r| r == 0}
- term_map.delete_if {|t, r| r == 0}
+ debug("term map") {"remove zero rank terms: #{terms.keys.select {|t| terms[t] == 0}}"} if terms.any? {|t,r| r == 0}
+ terms.delete_if {|t, r| r == 0}
end
# If we don't want terms, remove anything that's not a CURIE or IRI
- term_map.keep_if {|t, v| t.index(':') } if options.fetch(:not_term, false)
+ terms.keep_if {|t, v| t.index(':') } if options.fetch(:not_term, false)
# Find terms having the greatest term match value
- least_distance = term_map.values.max
- terms = term_map.keys.select {|t| term_map[t] == least_distance}
+ least_distance = terms.values.max
+ terms = terms.keys.select {|t| terms[t] == least_distance}
- # If terms is empty, and the active context has a @vocab which is a
- # prefix of iri where the resulting relative IRI is not a term in the
- # active context. The resulting relative IRI is the unmatched part of iri.
- if vocab && terms.empty? && iri.to_s.index(vocab) == 0
+ # If terms is empty, and the active context has a @vocab which is a prefix of iri where the resulting relative IRI is not a term in the active context. The resulting relative IRI is the unmatched part of iri.
+ if vocab && terms.empty? && iri.to_s.index(vocab) == 0 &&
+ [:predicate, :type].include?(options[:position])
terms << iri.to_s.sub(vocab, '')
debug("vocab") {"vocab: #{vocab}, rel: #{terms.first}"}
end
# If terms is empty, add a compact IRI representation of iri for each
@@ -606,11 +605,11 @@
"lang: #{language(c).inspect}"
end.inspect
end
terms = curies.select do |curie|
- container(curie) != '@list' &&
+ (options[:position] != :predicate || container(curie) != '@list') &&
coerce(curie).nil? &&
language(curie) == default_language
end
debug("curies") {"selected #{terms.inspect}"}
@@ -753,11 +752,11 @@
res
else
debug("else")
case coerce(property)
when '@id'
- {'@id' => expand_iri(value, :position => :object).to_s}
+ {'@id' => expand_iri(value, :position => :subject).to_s}
when nil
debug("expand value") {"lang(prop): #{language(property).inspect}, def: #{default_language.inspect}"}
language(property) ? {"@value" => value.to_s, "@language" => language(property)} : {"@value" => value.to_s}
else
res = Hash.ordered
@@ -790,26 +789,26 @@
depth(options) do
debug("compact_value") {"property: #{property.inspect}, value: #{value.inspect}, coerce: #{coerce(property).inspect}"}
result = case
- #when %w(boolean integer double).any? {|t| expand_iri(value['@type'], :position => :datatype) == RDF::XSD[t]}
+ #when %w(boolean integer double).any? {|t| expand_iri(value['@type'], :position => :type) == RDF::XSD[t]}
# # Compact native type
# debug {" (native)"}
- # l = RDF::Literal(value['@value'], :datatype => expand_iri(value['@type'], :position => :datatype))
+ # l = RDF::Literal(value['@value'], :datatype => expand_iri(value['@type'], :position => :type))
# l.canonicalize.object
when coerce(property) == '@id' && value.has_key?('@id')
# Compact an @id coercion
debug {" (@id & coerce)"}
- compact_iri(value['@id'], :position => :object)
- when value['@type'] && expand_iri(value['@type'], :position => :datatype) == coerce(property)
+ compact_iri(value['@id'], :position => :subject)
+ when value['@type'] && expand_iri(value['@type'], :position => :type) == coerce(property)
# Compact common datatype
debug {" (@type & coerce) == #{coerce(property)}"}
value['@value']
when value.has_key?('@id')
# Compact an IRI
- value[self.alias('@id')] = compact_iri(value['@id'], :position => :object)
+ value[self.alias('@id')] = compact_iri(value['@id'], :position => :subject)
debug {" (#{self.alias('@id')} => #{value['@id']})"}
value
when value['@language'] && value['@language'] == language(property)
# Compact language
debug {" (@language) == #{language(property).inspect}"}
@@ -827,11 +826,11 @@
debug {" (@value && !@language && !@type && !coerce && language(property).false)"}
value['@value']
when value['@type']
# Compact datatype
debug {" (@type)"}
- value[self.alias('@type')] = compact_iri(value['@type'], :position => :datatype)
+ value[self.alias('@type')] = compact_iri(value['@type'], :position => :type)
value
else
# Otherwise, use original value
debug {" (no change)"}
value
@@ -926,20 +925,26 @@
# Otherwise, return the sum of the term ranks for every entry in the list.
depth {value['@list'].inject(0) {|memo, v| memo + term_rank(term, v)}}
end
elsif value?(value)
val_type = value.fetch('@type', nil)
- val_lang = value.fetch('@language', nil)
+ val_lang = value['@language'] || false if value.has_key?('@language')
debug("term rank") {"@val_type: #{val_type.inspect}, val_lang: #{val_lang.inspect}"}
if val_type
coerce(term) == val_type ? 3 : (default_term ? 1 : 0)
elsif !value['@value'].is_a?(String)
default_term ? 2 : 1
elsif val_lang.nil?
debug("val_lang.nil") {"#{language(term).inspect} && #{coerce(term).inspect}"}
- !language(term) && !coerce(term) ? 3 : 0
+ language(term) == false || (default_term && default_language.nil?) ? 3 : 0
else
- val_lang == language(term) ? 3 : (default_term ? 1 : 0)
+ if val_lang == language(term) || (default_term && default_language == val_lang)
+ 3
+ elsif default_term
+ 1
+ else
+ 0
+ end
end
else # node definition/reference
coerce(term) == '@id' ? 3 : (default_term ? 1 : 0)
end