Skip to content

Commit

Permalink
Indexing on @id and @type.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Jan 5, 2017
1 parent 3db66f1 commit a9f39d4
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 129 deletions.
28 changes: 25 additions & 3 deletions lib/json/ld/compact.rb
Expand Up @@ -145,10 +145,32 @@ def compact(element, property: nil)
end
end

if %w(@language @index).include?(container)
if %w(@language @index @id @type).include?(container)
map_object = result[item_active_property] ||= {}
compacted_item = compacted_item['@value'] if container == '@language' && value?(compacted_item)
map_key = expanded_item[container]
compacted_item = case container
when '@id'
id_prop = context.compact_iri('@id', vocab: true, quiet: true)
map_key = compacted_item[id_prop]
compacted_item.delete(id_prop)
compacted_item
when '@index'
index_prop = context.compact_iri('@index', vocab: true, quiet: true)
map_key = expanded_item[container]
#compacted_item.delete(index_prop)
compacted_item
when '@language'
map_key = expanded_item[container]
value?(expanded_item) ? expanded_item['@value'] : compacted_item
when '@type'
type_prop = context.compact_iri('@type', vocab: true, quiet: true)
map_key, types = Array(compacted_item[type_prop])
if Array(types).empty?
compacted_item.delete(type_prop)
else
compacted_item[type_prop] = types
end
compacted_item
end
merge_compacted_value(map_object, map_key, compacted_item)
else
compacted_item = [compacted_item] if
Expand Down
37 changes: 6 additions & 31 deletions lib/json/ld/context.rb
Expand Up @@ -36,7 +36,7 @@ class TermDefinition
# @return [String] Type mapping
attr_accessor :type_mapping

# @return [String] Container mapping
# @return ['@index', '@language', '@index', '@set', '@type', '@id'] Container mapping
attr_accessor :container_mapping

# Language mapping of term, `false` is used if there is explicitly no language mapping for this term.
Expand All @@ -62,7 +62,7 @@ def simple?; simple; end
# @param [String] term
# @param [String] id
# @param [String] type_mapping Type mapping
# @param [String] container_mapping
# @param ['@index', '@language', '@index', '@set', '@type', '@id'] container_mapping
# @param [String] language_mapping
# Language mapping of term, `false` is used if there is explicitly no language mapping for this term
# @param [Boolean] reverse_property
Expand Down Expand Up @@ -957,16 +957,10 @@ def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **opt
containers = []
tl, tl_value = "@language", "@null"

# If the value is a JSON Object, then for the keywords @index, @id, and @type along with the compacted version of all non-keyword properties of the object in order, if the value contains that property, append it to containers.
if value.is_a?(Hash)
%w(@index @id @type).each do |kw|
containers << kw if value.has_key?(kw)
end
containers.concat value.keys.
reject {|k| k.start_with?('@')}.
sort.
map {|k| compact_iri(k, vocab: true, quite: true)}
end
# If the value is a JSON Object, then for the keywords @index, @id, and @type, if the value contains that keyword, append it to containers.
%w(@index @id @type).each do |kw|
containers << kw if value.has_key?(kw)
end if value.is_a?(Hash)

if reverse
tl, tl_value = "@type", "@reverse"
Expand Down Expand Up @@ -1503,25 +1497,6 @@ def check_container(container, local_context, defined, term)
case container
when '@set', '@list', '@language', '@index', '@type', '@id', nil
# Okay
when /^@/
raise JsonLdError::InvalidContainerMapping,
"unknown mapping for '@container' to #{container.inspect} on term #{term.inspect}"
when String
expanded = expand_iri(container,
vocab: true,
documentRelative: false,
local_context: local_context,
defined: defined)
case expanded
when RDF::URI
raise JsonLdError::InvalidContainerMapping,
"unknown mapping for '@container' to #{container.inspect} on term #{term.inspect}" unless
expanded.absolute?
else
raise JsonLdError::InvalidContainerMapping,
"unknown mapping for '@container' to #{container.inspect} on term #{term.inspect}" unless
container.absolute?
end
else
raise JsonLdError::InvalidContainerMapping,
"unknown mapping for '@container' to #{container.inspect} on term #{term.inspect}"
Expand Down
12 changes: 1 addition & 11 deletions lib/json/ld/expand.rb
Expand Up @@ -273,7 +273,7 @@ def expand(input, active_property, context, ordered: true)
end

ary
elsif container && !%w(@set @list @language).include?(container) && value.is_a?(Hash)
elsif %w(@index @id @type).include?(container) && value.is_a?(Hash)
# Otherwise, if key's container mapping in active context is @index, @id, @type, an IRI or Blank Node and value is a JSON object then value is expanded from an index map as follows:

# Set ary to an empty array.
Expand All @@ -289,16 +289,6 @@ def expand(input, active_property, context, ordered: true)
when '@id', '@index' then item[container] ||= k
# If container is @type add the key-value pair (@type-[index]) to item, appending any existing values in item
when '@type' then item[container] = [k].concat(Array(item[container]))
else
#require 'byebug'; byebug
# Otherwise container is an IRI or Blank Node
# Expand index using the Value Expansion algorithm
prop = active_context.expand_iri(container, vocab: true).to_s
item_value = active_context.expand_value(container, k, log_depth: @options[:log_depth])
# add the key-value pair (container-[{"@id": index}]) to item, appending any existing values in item
values = item[prop]
values = [values].compact unless values.is_a?(Array)
item[prop] = [item_value].concat(values)
end

# Append item to expanded value.
Expand Down
149 changes: 148 additions & 1 deletion spec/compact_spec.rb
Expand Up @@ -406,7 +406,46 @@
end
end

context "language maps" do
context "@container: @index" do
{
"compact-0029" => {
input: %([{
"@id": "http://example.com/article",
"http://example.com/vocab/author": [{
"@id": "http://example.org/person/1",
"@index": "regular"
}, {
"@id": "http://example.org/guest/cd24f329aa",
"@index": "guest"
}]
}]),
context: %({
"author": {"@id": "http://example.com/vocab/author", "@container": "@index" }
}),
output: %({
"@context": {
"author": {
"@id": "http://example.com/vocab/author",
"@container": "@index"
}
},
"@id": "http://example.com/article",
"author": {
"regular": {
"@id": "http://example.org/person/1"
},
"guest": {
"@id": "http://example.org/guest/cd24f329aa"
}
}
})
},
}.each_pair do |title, params|
it(title) {run_compact(params)}
end
end

context "@container: @language" do
{
"compact-0024" => {
input: %([
Expand Down Expand Up @@ -440,6 +479,114 @@
end
end

context "@container: @id" do
{
"Indexes to object not having an @id" => {
input: %([{
"http://example/idmap": [
{"http://example/label": [{"@value": "Object with @id _:bar"}], "@id": "_:bar"},
{"http://example/label": [{"@value": "Object with @id <foo>"}], "@id": "http://example.org/foo"}
]
}]),
context: %({
"@vocab": "http://example/",
"idmap": {"@container": "@id"}
}),
output: %({
"@context": {
"@vocab": "http://example/",
"idmap": {"@container": "@id"}
},
"idmap": {
"http://example.org/foo": {"label": "Object with @id <foo>"},
"_:bar": {"label": "Object with @id _:bar"}
}
}),
},
"Indexes to object already having an @id" => {
input: %([{
"http://example/idmap": [
{"@id": "_:foo", "http://example/label": [{"@value": "Object with @id _:bar"}]},
{"@id": "http://example.org/bar", "http://example/label": [{"@value": "Object with @id <foo>"}]}
]
}]),
context: %({
"@vocab": "http://example/",
"idmap": {"@container": "@id"}
}),
output: %({
"@context": {
"@vocab": "http://example/",
"idmap": {"@container": "@id"}
},
"idmap": {
"_:foo": {"label": "Object with @id _:bar"},
"http://example.org/bar": {"label": "Object with @id <foo>"}
}
}),
},
}.each_pair do |title, params|
it(title) {run_compact(params)}
end
end

context "@container: @type" do
{
"Indexes to object not having an @type" => {
input: %([{
"http://example/typemap": [
{"http://example/label": [{"@value": "Object with @type _:bar"}], "@type": ["_:bar"]},
{"http://example/label": [{"@value": "Object with @type <foo>"}], "@type": ["http://example.org/foo"]}
]
}]),
context: %({
"@vocab": "http://example/",
"typemap": {"@container": "@type"}
}),
output: %({
"@context": {
"@vocab": "http://example/",
"typemap": {"@container": "@type"}
},
"typemap": {
"http://example.org/foo": {"label": "Object with @type <foo>"},
"_:bar": {"label": "Object with @type _:bar"}
}
})
},
"Indexes to object already having an @type" => {
input: %([{
"http://example/typemap": [
{
"@type": ["_:bar", "_:foo"],
"http://example/label": [{"@value": "Object with @type _:bar"}]
},
{
"@type": ["http://example.org/foo", "http://example.org/bar"],
"http://example/label": [{"@value": "Object with @type <foo>"}]
}
]
}]),
context: %({
"@vocab": "http://example/",
"typemap": {"@container": "@type"}
}),
output: %({
"@context": {
"@vocab": "http://example/",
"typemap": {"@container": "@type"}
},
"typemap": {
"http://example.org/foo": {"@type": "http://example.org/bar", "label": "Object with @type <foo>"},
"_:bar": {"@type": "_:foo", "label": "Object with @type _:bar"}
}
})
},
}.each_pair do |title, params|
it(title) {run_compact(params)}
end
end

context "@graph" do
{
"Uses @graph given mutliple inputs" => {
Expand Down
5 changes: 1 addition & 4 deletions spec/context_spec.rb
Expand Up @@ -1320,8 +1320,7 @@ def containers
"language" => {"@id" => "ex:language", "@container" => "@language"},
"ndx" => {"@id" => "ex:ndx", "@container" => "@index"},
"id" => {"@id" => "ex:id", "@container" => "@id"},
"type" => {"@id" => "ex:type", "@container" => "@type"},
'uri' => {"@id" => "ex:uri", "@container" => "ex:uri"}
"type" => {"@id" => "ex:type", "@container" => "@type"}
})
logger.clear
ctx
Expand All @@ -1335,7 +1334,6 @@ def containers
"ndx" => "@index",
"id" => "@id",
"type" => "@type",
'uri' => "ex:uri",
}.each do |defn, container|
expect(subject.container(subject.term_definitions[defn])).to eq container
end
Expand All @@ -1350,7 +1348,6 @@ def containers
"ndx" => "@index",
"id" => "@id",
"type" => "@type",
'uri' => "ex:uri",
}.each do |defn, container|
expect(subject.container(defn)).to eq container
end
Expand Down

0 comments on commit a9f39d4

Please sign in to comment.