Module: JSON::LD::Flatten

Includes:
Utils
Included in:
API
Defined in:
lib/json/ld/flatten.rb

Instance Method Summary collapse

Methods included from Utils

#add_value, #as_array, #as_resource, #blank_node?, #compare_values, #graph?, #has_property, #has_value, #index?, #list?, #node?, #node_or_ref?, #node_reference?, #simple_graph?, #value?

Instance Method Details

#create_node_map(element, graph_map, active_graph: '@default', active_subject: nil, active_property: nil, list: nil) ⇒ Object

This algorithm creates a JSON object node map holding an indexed representation of the graphs and nodes represented in the passed expanded document. All nodes that are not uniquely identified by an IRI get assigned a (new) blank node identifier. The resulting node map will have a member for every graph in the document whose value is another object with a member for every node represented in the document. The default graph is stored under the @default member, all other graphs are stored under their graph name.

Parameters:

  • element (Array, Hash)

    Expanded JSON-LD input

  • graph_map (Hash)

    A map of graph name to subjects

  • active_graph (String) (defaults to: '@default')

    The name of the currently active graph that the processor should use when processing.

  • active_subject (String) (defaults to: nil)

    (nil) Node identifier

  • active_property (String) (defaults to: nil)

    (nil) Property within current node

  • list (Array) (defaults to: nil)

    (nil) Used when property value is a list



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/json/ld/flatten.rb', line 21

def create_node_map(element, graph_map,
                    active_graph: '@default',
                    active_subject: nil,
                    active_property: nil,
                    list: nil)
  if element.is_a?(Array)
    # If element is an array, process each entry in element recursively by passing item for element, node map, active graph, active subject, active property, and list.
    element.map do |o|
      create_node_map(o, graph_map,
                      active_graph: active_graph,
                      active_subject: active_subject,
                      active_property: active_property,
                      list: list)
    end
  elsif !element.is_a?(Hash)
    raise "Expected hash or array to create_node_map, got #{element.inspect}"
  else
    graph = (graph_map[active_graph] ||= {})
    subject_node = graph[active_subject]

    # Transform BNode types
    if element.has_key?('@type')
      element['@type'] = Array(element['@type']).map {|t| blank_node?(t) ? namer.get_name(t) : t}
    end

    if value?(element)
      element['@type'] = element['@type'].first if element ['@type']
      if list.nil?
        add_value(subject_node, active_property, element, property_is_array: true, allow_duplicate: false)
      else
        list['@list'] << element
      end
    elsif list?(element)
      result = {'@list' => []}
      create_node_map(element['@list'], graph_map,
                      active_graph: active_graph,
                      active_subject: active_subject,
                      active_property: active_property,
                      list: result)
      if list.nil?
        add_value(subject_node, active_property, result, property_is_array: true)
      else
        list['@list'] << result
      end
    else
      # Element is a node object
      id = element.delete('@id')
      id = namer.get_name(id) if blank_node?(id)

      node = graph[id] ||= {'@id' => id}

      if active_subject.is_a?(Hash)
        # If subject is a hash, then we're processing a reverse-property relationship.
        add_value(node, active_property, active_subject, property_is_array: true, allow_duplicate: false)
      elsif active_property
        reference = {'@id' => id}
        if list.nil?
          add_value(subject_node, active_property, reference, property_is_array: true, allow_duplicate: false)
        else
          list['@list'] << reference
        end
      end

      if element.has_key?('@type')
        add_value(node, '@type', element.delete('@type'), property_is_array: true, allow_duplicate: false)
      end

      if element['@index']
        raise JsonLdError::ConflictingIndexes,
              "Element already has index #{node['@index']} dfferent from #{element['@index']}" if
              node.key?('@index') && node['@index'] != element['@index']
        node['@index'] = element.delete('@index')
      end

      if element['@reverse']
        referenced_node, reverse_map = {'@id' => id}, element.delete('@reverse')
        reverse_map.each do |property, values|
          values.each do |value|
            create_node_map(value, graph_map,
                            active_graph: active_graph,
                            active_subject: referenced_node,
                            active_property: property)
          end
        end
      end

      if element['@graph']
        create_node_map(element.delete('@graph'), graph_map,
                        active_graph: id)
      end

      if element['@included']
        create_node_map(element.delete('@included'), graph_map,
                        active_graph: active_graph)
      end

      element.keys.each do |property|
        value = element[property]

        property = namer.get_name(property) if blank_node?(property)
        node[property] ||= []
        create_node_map(value, graph_map,
                        active_graph: active_graph,
                        active_subject: id,
                        active_property: property)
      end
    end
  end
end