Skip to content

Commit

Permalink
Tweaks to preloaded contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Aug 11, 2016
1 parent c23de4c commit 0e76e76
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,15 @@ Then, when performing something like expansion:


## Preloading contexts
In many cases, for small documents, processing time can be dominated by loading and parsing remote contexts. In particular, a small schema.org example may need to download a large context and turn it into an internal representation, before the actual document can be expanded for processing. Using `JSON::LD::Context.add_preloaded`, an implementation can perform this loading up-front, and make it available to the processor.
In many cases, for small documents, processing time can be dominated by loading and parsing remote contexts. In particular, a small schema.org example may need to download a large context and turn it into an internal representation, before the actual document can be expanded for processing. Using {JSON::LD::Context.add_preloaded}, an implementation can perform this loading up-front, and make it available to the processor.

ctx = JSON::LD::Context.new().parse('http://schema.org/')
JSON::LD::Context.add_preloaded('http://schema.org/', ctx)

On lookup, URIs with an `https` prefix are normalized to `http`.

A context may be serialized to Ruby to speed this process using `Context#to_rb`. When loaded, this generated file will add entries to the {JSON::LD::Context::PRELOADED}.

## RDF Reader and Writer
{JSON::LD} also acts as a normal RDF reader and writer, using the standard RDF.rb reader/writer interfaces:

Expand Down
1 change: 1 addition & 0 deletions example-files/nicholas-arduino-bench.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
require "bundler/setup"
require 'json/ld'
#require File.expand_path('../schema-context', __FILE__)
require 'benchmark/ips'

source = JSON.parse %({
Expand Down
7 changes: 5 additions & 2 deletions example-files/schema-context.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# -*- encoding: utf-8 -*-
# frozen_string_literal: true
# This file generated automatically from http://schema.org
require 'json/ld'
class JSON::LD::Context
PRELOADED["http://schema.org/"] = JSON::LD::Context.new(vocab: "http://schema.org/", term_definitions: {
add_preloaded("http://schema.org/", new(vocab: "http://schema.org/", term_definitions: {
"type" => TermDefinition.new("type", id: "@type", simple: true),
"id" => TermDefinition.new("id", id: "@id", simple: true),
"schema" => TermDefinition.new("schema", id: "http://schema.org/", simple: true),
Expand Down Expand Up @@ -2112,5 +2115,5 @@ class JSON::LD::Context
"worstRating" => TermDefinition.new("worstRating", id: "http://schema.org/worstRating"),
"yearlyRevenue" => TermDefinition.new("yearlyRevenue", id: "http://schema.org/yearlyRevenue"),
"yearsInOperation" => TermDefinition.new("yearsInOperation", id: "http://schema.org/yearsInOperation")
})
}))
end
27 changes: 17 additions & 10 deletions lib/json/ld/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ class Context
PRELOADED = {}

class << self
##
# Add preloaded context
# @param [String, RDF::URI] url
# @param [Context] context
def add_preloaded(url, context)
PRELOADED[url.to_sym] = context
PRELOADED[url.to_s.freeze] = context
end
end

Expand Down Expand Up @@ -268,14 +272,14 @@ def vocab=(value)
@vocab = case value
when /_:/
value
when String
v = as_resource(value)
raise JsonLdError::InvalidVocabMapping, "@value must be an absolute IRI: #{value.inspect}" if v.uri? && v.relative? && @options[:validate]
when String, RDF::URI
v = as_resource(value.to_s)
raise JsonLdError::InvalidVocabMapping, "@vocab must be an absolute IRI: #{value.inspect}" if v.uri? && v.relative? && @options[:validate]
v
when nil
nil
else
raise JsonLdError::InvalidVocabMapping, "@value must be a string: #{value.inspect}"
raise JsonLdError::InvalidVocabMapping, "@vocab must be an absolute IRI: #{value.inspect}"
end
end

Expand Down Expand Up @@ -336,10 +340,10 @@ def parse(local_context, remote_contexts = [])
context_no_base.base = nil
context_no_base.context_base = context.to_s

if PRELOADED[context_canon.to_s.to_sym]
if PRELOADED[context_canon.to_s]
# If we have a cached context, merge it into the current context (result) and use as the new context
log_debug("parse") {"=> cached_context: #{context_canon.to_s.inspect}"}
context = context_no_base.merge!(PRELOADED[context_canon.to_s.to_sym])
context = context_no_base.merge!(PRELOADED[context_canon.to_s])
else

# Load context document, if it is a string
Expand Down Expand Up @@ -1196,11 +1200,14 @@ def to_rb
term_defs = term_definitions.map do |term, td|
" " + term.inspect + " => " + td.to_rb
end
defn << "term_definitions: {\n#{term_defs.join(",\n") }\n}" unless term_defs.empty?
%(require 'json/ld'
defn << "term_definitions: {\n#{term_defs.join(",\n") }\n }" unless term_defs.empty?
%(# -*- encoding: utf-8 -*-
# frozen_string_literal: true
# This file generated automatically from #{context_base}
require 'json/ld'
class JSON::LD::Context
).gsub(/^ /, '') +
" PRELOADED[#{RDF::URI(context_base).canonicalize.to_s.inspect}] = Context.new(" + defn.join(", ") + ")\nend\n"
" add_preloaded(#{RDF::URI(context_base).canonicalize.to_s.inspect}, Context.new(" + defn.join(", ") + "))\nend\n"
end

def inspect
Expand Down

0 comments on commit 0e76e76

Please sign in to comment.