From 0e76e7672de923074bf1a1308bdd57c19a08ec0a Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Thu, 11 Aug 2016 11:37:18 -0700 Subject: [PATCH] Tweaks to preloaded contexts. --- README.md | 6 +++++- example-files/nicholas-arduino-bench.rb | 1 + example-files/schema-context.rb | 7 +++++-- lib/json/ld/context.rb | 27 ++++++++++++++++--------- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8271813c..df84beeb 100755 --- a/README.md +++ b/README.md @@ -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: diff --git a/example-files/nicholas-arduino-bench.rb b/example-files/nicholas-arduino-bench.rb index 0828417f..995dbb5c 100755 --- a/example-files/nicholas-arduino-bench.rb +++ b/example-files/nicholas-arduino-bench.rb @@ -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 %({ diff --git a/example-files/schema-context.rb b/example-files/schema-context.rb index cc20487a..3d3f7113 100644 --- a/example-files/schema-context.rb +++ b/example-files/schema-context.rb @@ -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), @@ -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 diff --git a/lib/json/ld/context.rb b/lib/json/ld/context.rb index f03bdd9b..7fb7993e 100644 --- a/lib/json/ld/context.rb +++ b/lib/json/ld/context.rb @@ -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 @@ -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 @@ -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 @@ -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