Skip to content

Commit

Permalink
Finish 2.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Jul 10, 2017
2 parents 9665d09 + 6d11bd2 commit 7332ad6
Show file tree
Hide file tree
Showing 15 changed files with 370 additions and 112 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.4
2.1.5
1 change: 0 additions & 1 deletion bin/jsonld
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ OPT_ARGS = [
["--quiet", GetoptLong::NO_ARGUMENT, "Supress most output other than progress indicators"],
["--rename_bnodes", GetoptLong::OPTIONAL_ARGUMENT,"Rename bnodes as part of expansion, or keep them the same"],
["--requireAll", GetoptLong::OPTIONAL_ARGUMENT,"Rename bnodes as part of expansion, or keep them the same"],
["--simple_compact_iris",GetoptLong::OPTIONAL_ARGUMENT,"When compacting IRIs, do not use terms with expanded term definitions"],
["--stream", GetoptLong::NO_ARGUMENT, "Use Streaming reader/writer"],
["--unique_bnodes", GetoptLong::OPTIONAL_ARGUMENT,"Use unique bnode identifiers"],
["--uri", GetoptLong::REQUIRED_ARGUMENT,"URI to be used as the document base"],
Expand Down
27 changes: 27 additions & 0 deletions example-files/issue-507.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'pp'
require 'linkeddata'

context = JSON.parse %({
"@context": {
"id": "@id",
"rdfs": {"@id": "http://.../"},
"seeAlso": {"@id": "rdfs:seeAlso", "@container": "@set"}
}
})

input = JSON.parse %({
"@context": {
"id": "@id",
"rdfs": {"@id": "http://.../"},
"seeAlso": {"@id": "rdfs:seeAlso", "@container": "@set"}
},
"seeAlso": [
{
"id": "http://example.org/reference1"
},
"http://example.org/reference2",
{"id": "http://example.org/reference3", "format": "text/html"}
]
})

pp JSON::LD::API.compact(input, context['@context'])
10 changes: 10 additions & 0 deletions example-files/pr-509.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"@context": {
"name": {
"@type": "@id",
"@container": "@list",
"@id": "https://schema.org/name"
}
},
"name": []
}
8 changes: 4 additions & 4 deletions json-ld.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ Gem::Specification.new do |gem|
gem.requirements = []
gem.add_runtime_dependency 'rdf', '~> 2.2'
gem.add_runtime_dependency 'multi_json', '~> 1.12'
gem.add_development_dependency 'linkeddata', '~> 2.0'
gem.add_development_dependency 'jsonlint', '~> 0.2' unless RUBY_ENGINE == "jruby"
gem.add_development_dependency 'oj', '~> 2.17' unless RUBY_ENGINE == "jruby"
gem.add_development_dependency 'yajl-ruby', '~> 1.2' unless RUBY_ENGINE == "jruby"
gem.add_development_dependency 'linkeddata', '~> 2.2'
gem.add_development_dependency 'jsonlint', '~> 0.2' unless RUBY_ENGINE == "jruby"
gem.add_development_dependency 'oj', '~> 2.17' unless RUBY_ENGINE == "jruby"
gem.add_development_dependency 'yajl-ruby', '~> 1.2' unless RUBY_ENGINE == "jruby"
gem.add_development_dependency 'rdf-isomorphic', '~> 2.0'
gem.add_development_dependency 'rdf-spec', '~> 2.2'
gem.add_development_dependency 'rdf-trig', '~> 2.0'
Expand Down
1 change: 1 addition & 0 deletions lib/json/ld.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class InvalidLanguageTaggedString < JsonLdError; @code = "invalid language-tagge
class InvalidLanguageTaggedValue < JsonLdError; @code = "invalid language-tagged value"; end
class InvalidLocalContext < JsonLdError; @code = "invalid local context"; end
class InvalidNestValue < JsonLdError; @code = "invalid @nest value"; end
class InvalidPrefixValue < JsonLdError; @code = "invalid @prefix value"; end
class InvalidRemoteContext < JsonLdError; @code = "invalid remote context"; end
class InvalidReverseProperty < JsonLdError; @code = "invalid reverse property"; end
class InvalidReversePropertyMap < JsonLdError; @code = "invalid reverse property map"; end
Expand Down
76 changes: 49 additions & 27 deletions lib/json/ld/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class API
# The Base IRI to use when expanding the document. This overrides the value of `input` if it is a _IRI_. If not specified and `input` is not an _IRI_, the base IRI defaults to the current document IRI if in a browser context, or the empty string if there is no document context. If not specified, and a base IRI is found from `input`, options[:base] will be modified with this value.
# @option options [Boolean] :compactArrays (true)
# If set to `true`, the JSON-LD processor replaces arrays with just one element with that element during compaction. If set to `false`, all arrays will remain arrays even if they have just one element.
# @option options [Boolean] :compactToRelative (true)
# Creates document relative IRIs when compacting, if `true`, otherwise leaves expanded.
# @option options [Proc] :documentLoader
# 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 {documentLoader} for the method signature.
# @option options [String, #read, Hash, Array, JSON::LD::Context] :expandContext
Expand All @@ -79,29 +81,26 @@ class API
# Rename bnodes as part of expansion, or keep them the same.
# @option options [Boolean] :unique_bnodes (false)
# Use unique bnode identifiers, defaults to using the identifier which the node was originally initialized with (if any).
# @option options [Boolean] :simple_compact_iris (false)
# When compacting IRIs, do not use terms with expanded term definitions
# @option options [Symbol] :adapter used with MultiJson
# @option options [Boolean] :validate Validate input, if a string or readable object.
# @yield [api]
# @yieldparam [API]
# @raise [JsonLdError]
def initialize(input, context, options = {}, &block)
@options = {
compactArrays: true,
rename_bnodes: true,
documentLoader: self.class.method(:documentLoader)
}
@options = @options.merge(options)
compactArrays: true,
rename_bnodes: true,
documentLoader: self.class.method(:documentLoader)
}.merge(options)
@namer = options[:unique_bnodes] ? BlankNodeUniqer.new : (@options[:rename_bnodes] ? BlankNodeNamer.new("b") : BlankNodeMapper.new)

# For context via Link header
context_ref = nil
remote_base, context_ref = nil, nil

@value = case input
when Array, Hash then input.dup
when IO, StringIO
@options = {base: input.base_uri}.merge!(@options) if input.respond_to?(:base_uri)
@options = {base: input.base_uri}.merge(@options) if input.respond_to?(:base_uri)

# if input impelements #links, attempt to get a contextUrl from that link
content_type = input.respond_to?(:content_type) ? input.content_type : "application/json"
Expand All @@ -116,8 +115,9 @@ def initialize(input, context, options = {}, &block)
when String
remote_doc = @options[:documentLoader].call(input, @options)

@options = {base: remote_doc.documentUrl}.merge!(@options)
remote_base = remote_doc.documentUrl
context_ref = remote_doc.contextUrl
@options = {base: remote_doc.documentUrl}.merge(@options) unless @options[:no_default_base]

case remote_doc.document
when String
Expand All @@ -128,9 +128,6 @@ def initialize(input, context, options = {}, &block)
end
end

# Update calling context :base option, if not defined
options[:base] ||= @options[:base] if @options[:base]

# If not provided, first use context from document, or from a Link header
context ||= (@value['@context'] if @value.is_a?(Hash)) || context_ref
@context = Context.parse(context || {}, @options)
Expand Down Expand Up @@ -160,25 +157,38 @@ def initialize(input, context, options = {}, &block)
# @param [Hash{Symbol => Object}] options
# @option options (see #initialize)
# @raise [JsonLdError]
# @yield jsonld
# @yield jsonld, base_iri
# @yieldparam [Array<Hash>] jsonld
# The expanded JSON-LD document
# @yieldparam [RDF::URI] base_iri
# The document base as determined during expansion
# @yieldreturn [Object] returned object
# @return [Object, Array<Hash>]
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document
# @see http://json-ld.org/spec/latest/json-ld-api/#expansion-algorithm
def self.expand(input, options = {})
result = nil
API.new(input, options[:expandContext], options) do |api|
result = api.expand(api.value, nil, api.context, ordered: options.fetch(:ordered, true))
def self.expand(input, options = {}, &block)
result, doc_base = nil
API.new(input, options[:expandContext], options) do
result = self.expand(self.value, nil, self.context, ordered: options.fetch(:ordered, true))
doc_base = @options[:base]
end

# If, after the algorithm outlined above is run, the resulting element is an JSON object with just a @graph property, element is set to the value of @graph's value.
result = result['@graph'] if result.is_a?(Hash) && result.keys == %w(@graph)

# Finally, if element is a JSON object, it is wrapped into an array.
result = [result].compact unless result.is_a?(Array)
block_given? ? yield(result) : result

if block_given?
case block.arity
when 1 then yield(result)
when 2 then yield(result, doc_base)
else
raise "Unexpected number of yield parameters to expand"
end
else
result
end
end

##
Expand All @@ -204,13 +214,17 @@ def self.expand(input, options = {})
# @raise [JsonLdError]
# @see http://json-ld.org/spec/latest/json-ld-api/#compaction-algorithm
def self.compact(input, context, options = {})
expanded = result = nil
result = nil
options = {compactToRelative: true}.merge(options)

# 1) Perform the Expansion Algorithm on the JSON-LD input.
# This removes any existing context to allow the given context to be cleanly applied.
expanded_input = options[:expanded] ? input : API.expand(input, options)
expanded_input = options[:expanded] ? input : API.expand(input, options) do |result, base_iri|
options[:base] ||= base_iri if options[:compactToRelative]
result
end

API.new(expanded_input, context, options) do
API.new(expanded_input, context, options.merge(no_default_base: true)) do
log_debug(".compact") {"expanded input: #{expanded_input.to_json(JSON_STATE) rescue 'malformed json'}"}
result = compact(value)

Expand Down Expand Up @@ -246,12 +260,16 @@ def self.compact(input, context, options = {})
# @see http://json-ld.org/spec/latest/json-ld-api/#framing-algorithm
def self.flatten(input, context, options = {})
flattened = []
options = {compactToRelative: true}.merge(options)

# Expand input to simplify processing
expanded_input = options[:expanded] ? input : API.expand(input, options)
expanded_input = options[:expanded] ? input : API.expand(input, options) do |result, base_iri|
options[:base] ||= base_iri if options[:compactToRelative]
result
end

# Initialize input using
API.new(expanded_input, context, options) do
API.new(expanded_input, context, options.merge(no_default_base: true)) do
log_debug(".flatten") {"expanded input: #{value.to_json(JSON_STATE) rescue 'malformed json'}"}

# Initialize node map to a JSON object consisting of a single member whose key is @default and whose value is an empty JSON object.
Expand Down Expand Up @@ -316,13 +334,14 @@ def self.frame(input, frame, options = {})
options = {
base: (input if input.is_a?(String)),
compactArrays: true,
compactToRelative: true,
embed: '@last',
explicit: false,
requireAll: true,
omitDefault: false,
pruneBlankNodeIdentifiers: true,
documentLoader: method(:documentLoader)
}.merge!(options)
}.merge(options)

framing_state = {
graphMap: {},
Expand All @@ -344,13 +363,16 @@ def self.frame(input, frame, options = {})
end

# Expand input to simplify processing
expanded_input = options[:expanded] ? input : API.expand(input, options)
expanded_input = options[:expanded] ? input : API.expand(input, options) do |result, base_iri|
options[:base] ||= base_iri if options[:compactToRelative]
result
end

# Expand frame to simplify processing
expanded_frame = API.expand(frame, options.merge(processingMode: "json-ld-1.1-expand-frame"))

# Initialize input using frame as context
API.new(expanded_input, nil, options) do
API.new(expanded_input, nil, options.merge(no_default_base: true)) do
log_debug(".frame") {"expanded frame: #{expanded_frame.to_json(JSON_STATE) rescue 'malformed json'}"}

# Get framing nodes from expanded input, replacing Blank Node identifiers as necessary
Expand Down

0 comments on commit 7332ad6

Please sign in to comment.