Skip to content

Commit

Permalink
Remove a lot of expensive debugging statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Aug 11, 2016
1 parent 99a3d34 commit 452b579
Show file tree
Hide file tree
Showing 9 changed files with 958 additions and 994 deletions.
9 changes: 3 additions & 6 deletions lib/json/ld/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def self.flatten(input, context, options = {})

if context && !flattened.empty?
# Otherwise, return the result of compacting flattened according the Compaction algorithm passing context ensuring that the compaction result uses the @graph keyword (or its alias) at the top-level, even if the context is empty or if there is only one element to put in the @graph array. This ensures that the returned document has a deterministic structure.
compacted = log_depth {compact(flattened, nil)}
compacted = compact(flattened, nil)
compacted = [compacted] unless compacted.is_a?(Array)
kwgraph = self.context.compact_iri('@graph', quiet: true)
flattened = self.context.serialize.merge(kwgraph => compacted)
Expand Down Expand Up @@ -355,24 +355,21 @@ def self.frame(input, frame, options = {})

# Initialize input using frame as context
API.new(expanded_input, nil, options) do
#log_debug(".frame") {"context from frame: #{context.inspect}"}
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
old_logger, @options[:logger] = @options[:logger], []
create_node_map(value, framing_state[:graphs], graph: '@merged')
@options[:logger] = old_logger
framing_state[:subjects] = framing_state[:graphs]['@merged']
log_debug(".frame") {"subjects: #{framing_state[:subjects].to_json(JSON_STATE) rescue 'malformed json'}"}

result = []
frame(framing_state, framing_state[:subjects].keys.sort, (expanded_frame.first || {}), options.merge(parent: result))
log_debug(".frame") {"after frame: #{result.to_json(JSON_STATE) rescue 'malformed json'}"}

# Initalize context from frame
@context = log_depth {@context.parse(frame['@context'])}
@context = @context.parse(frame['@context'])
# Compact result
compacted = log_depth {compact(result, nil)}
compacted = compact(result, nil)
compacted = [compacted] unless compacted.is_a?(Array)

# Add the given context to the output
Expand Down
51 changes: 25 additions & 26 deletions lib/json/ld/compact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ module Compact
# @param [String] property (nil)
# @return [Array, Hash]
def compact(element, property = nil)
if property.nil?
log_debug("compact") {"element: #{element.inspect}, ec: #{context.inspect}"}
else
log_debug("compact") {"property: #{property.inspect}"}
end
#if property.nil?
# log_debug("compact") {"element: #{element.inspect}, ec: #{context.inspect}"}
#else
# log_debug("compact") {"property: #{property.inspect}"}
#end
case element
when Array
log_debug("") {"Array #{element.inspect}"}
result = log_depth {element.map {|item| compact(item, property)}.compact}
#log_debug("") {"Array #{element.inspect}"}
result = element.map {|item| compact(item, property)}.compact

# If element has a single member and the active property has no
# @container mapping to @list or @set, the compacted value is that
# member; otherwise the compacted value is element
if result.length == 1 && context.container(property).nil? && @options[:compactArrays]
log_debug("=> extract single element: #{result.first.inspect}")
#log_debug("=> extract single element: #{result.first.inspect}")
result.first
else
log_debug("=> array result: #{result.inspect}")
#log_debug("=> array result: #{result.inspect}")
result
end
when Hash
Expand All @@ -40,7 +40,7 @@ def compact(element, property = nil)
if element.keys.any? {|k| %w(@id @value).include?(k)}
result = context.compact_value(property, element, log_depth: @options[:log_depth])
unless result.is_a?(Hash)
log_debug("") {"=> scalar result: #{result.inspect}"}
#log_debug("") {"=> scalar result: #{result.inspect}"}
return result
end
end
Expand All @@ -50,80 +50,79 @@ def compact(element, property = nil)

element.each_key do |expanded_property|
expanded_value = element[expanded_property]
log_debug("") {"#{expanded_property}: #{expanded_value.inspect}"}
#log_debug("") {"#{expanded_property}: #{expanded_value.inspect}"}

if %w(@id @type).include?(expanded_property)
compacted_value = [expanded_value].flatten.compact.map do |expanded_type|
log_depth {context.compact_iri(expanded_type, vocab: (expanded_property == '@type'), log_depth: @options[:log_depth])}
context.compact_iri(expanded_type, vocab: (expanded_property == '@type'), log_depth: @options[:log_depth])
end
compacted_value = compacted_value.first if compacted_value.length == 1

al = context.compact_iri(expanded_property, vocab: true, quiet: true)
log_debug(expanded_property) {"result[#{al}] = #{compacted_value.inspect}"}
#log_debug(expanded_property) {"result[#{al}] = #{compacted_value.inspect}"}
result[al] = compacted_value
next
end

if expanded_property == '@reverse'
compacted_value = log_depth {compact(expanded_value, '@reverse')}
log_debug("@reverse") {"compacted_value: #{compacted_value.inspect}"}
compacted_value = compact(expanded_value, '@reverse')
#log_debug("@reverse") {"compacted_value: #{compacted_value.inspect}"}
compacted_value.each do |prop, value|
if context.reverse?(prop)
value = [value] if !value.is_a?(Array) &&
(context.container(prop) == '@set' || !@options[:compactArrays])
log_debug("") {"merge #{prop} => #{value.inspect}"}
#log_debug("") {"merge #{prop} => #{value.inspect}"}
merge_compacted_value(result, prop, value)
compacted_value.delete(prop)
end
end

unless compacted_value.empty?
al = context.compact_iri('@reverse', quiet: true)
log_debug("") {"remainder: #{al} => #{compacted_value.inspect}"}
#log_debug("") {"remainder: #{al} => #{compacted_value.inspect}"}
result[al] = compacted_value
end
next
end

if expanded_property == '@index' && context.container(property) == '@index'
log_debug("@index") {"drop @index"}
#log_debug("@index") {"drop @index"}
next
end

# Otherwise, if expanded property is @index, @value, or @language:
if %w(@index @value @language).include?(expanded_property)
al = context.compact_iri(expanded_property, vocab: true, quiet: true)
log_debug(expanded_property) {"#{al} => #{expanded_value.inspect}"}
#log_debug(expanded_property) {"#{al} => #{expanded_value.inspect}"}
result[al] = expanded_value
next
end

if expanded_value == []
item_active_property = log_depth do
item_active_property =
context.compact_iri(expanded_property,
value: expanded_value,
vocab: true,
reverse: inside_reverse,
log_depth: @options[:log_depth])
end

iap = result[item_active_property] ||= []
result[item_active_property] = [iap] unless iap.is_a?(Array)
end

# At this point, expanded value must be an array due to the Expansion algorithm.
expanded_value.each do |expanded_item|
item_active_property = log_depth do
item_active_property =
context.compact_iri(expanded_property,
value: expanded_item,
vocab: true,
reverse: inside_reverse,
log_depth: @options[:log_depth])
end

container = context.container(item_active_property)
value = list?(expanded_item) ? expanded_item['@list'] : expanded_item
compacted_item = log_depth {compact(value, item_active_property)}
log_debug("") {" => compacted key: #{item_active_property.inspect} for #{compacted_item.inspect}"}
compacted_item = compact(value, item_active_property)
#log_debug("") {" => compacted key: #{item_active_property.inspect} for #{compacted_item.inspect}"}

if list?(expanded_item)
compacted_item = [compacted_item] unless compacted_item.is_a?(Array)
Expand Down Expand Up @@ -161,7 +160,7 @@ def compact(element, property = nil)
result.keys.kw_sort.inject({}) {|map, kk| map[kk] = result[kk]; map}
else
# For other types, the compacted value is the element value
log_debug("compact") {element.class.to_s}
#log_debug("compact") {element.class.to_s}
element
end
end
Expand Down
Loading

0 comments on commit 452b579

Please sign in to comment.