Module: JSON::LD::Triples

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

Instance Method Summary collapse

Methods included from Utils

#blank_node?, #list?, #node?, #node_reference?, #value?

Instance Method Details

#add_quad(path, subject, predicate, object, name) {|:statement| ... } ⇒ Object

add a statement, object can be literal or URI or bnode

Parameters:

  • path (String)
  • subject (RDF::Resource)

    the subject of the statement

  • predicate (RDF::URI)

    the predicate of the statement

  • object (RDF::Term)

    the object of the statement

  • name (RDF::Resource)

    the named graph context of the statement

Yields:

  • :statement

Yield Parameters:

  • :statement (RDF::Statement)


172
173
174
175
176
177
178
# File 'lib/json/ld/to_rdf.rb', line 172

def add_quad(path, subject, predicate, object, name)
  predicate = RDF.type if predicate == '@type'
  object = context.expand_iri(object.to_s, :quiet => true) if object.literal? && predicate == RDF.type
  statement = RDF::Statement.new(subject, predicate, object, :context => name)
  debug(path) {"statement: #{statement.to_nquads}"}
  yield statement
end

#nodeObject

Create a new named node using the sequence



156
157
158
159
160
# File 'lib/json/ld/to_rdf.rb', line 156

def node
  n = RDF::Node.new(@node_seq)
  @node_seq = @node_seq.succ
  n
end

#parse_list(path, list, property, name) {|:statement| ... } ⇒ RDF::Resource

Parse a List

Parameters:

  • path (String)

    location within JSON hash

  • list (Array)

    The Array to serialize as a list

  • property (RDF::URI)

    Inherited property

  • name (RDF::Resource)

    Inherited named graph context

Yields:

  • :statement

Yield Parameters:

  • :statement (RDF::Statement)

Returns:

  • (RDF::Resource)

    BNode or nil for head of list



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/json/ld/to_rdf.rb', line 132

def parse_list(path, list, property, name, &block)
  debug(path) {"list: #{list.inspect}, p=#{property.inspect}, n=#{name.inspect}"}

  last = list.pop
  result = first_bnode = last ? node : RDF.nil

  depth do
    list.each do |list_item|
      # Traverse the value
      statements("#{path}", list_item, first_bnode, RDF.first, name, &block)
      rest_bnode = node
      add_quad("#{path}", first_bnode, RDF.rest, rest_bnode, name, &block)
      first_bnode = rest_bnode
    end
    if last
      statements("#{path}", last, first_bnode, RDF.first, name, &block)
      add_quad("#{path}", first_bnode, RDF.rest, RDF.nil, name, &block)
    end
  end
  result
end

#statements(path, element, subject, property, name) {|:statement| ... } ⇒ RDF::Resource

Returns defined by this element.

Parameters:

  • path (String)

    location within JSON hash

  • element (Hash, Array, String)

    The current JSON element being processed

  • subject (RDF::Node)

    Inherited subject

  • property (RDF::URI)

    Inherited property

  • name (RDF::Node)

    Inherited inherited graph name

Yields:

  • :statement

Yield Parameters:

  • :statement (RDF::Statement)

Returns:

  • (RDF::Resource)

    defined by this element



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
# File 'lib/json/ld/to_rdf.rb', line 22

def statements(path, element, subject, property, name, &block)
  debug(path) {"statements: e=#{element.inspect}, s=#{subject.inspect}, p=#{property.inspect}, n=#{name.inspect}"}
  @node_seq = "t0" unless subject || property

  traverse_result = depth do
    case element
    when Hash
      # Other shortcuts to allow use of this method for terminal associative arrays
      object = if element.has_key?('@value')
        # 1.2) If the JSON object has a @value key, set the active object to a literal value as follows ...
        literal_opts = {}
        literal_opts[:datatype] = RDF::URI(element['@type']) if element['@type']
        literal_opts[:language] = element['@language'].to_sym if element['@language']
        RDF::Literal.new(element['@value'], literal_opts)
      elsif element.has_key?('@list')
        # 1.3 (Lists)
        parse_list("#{path}[#{'@list'}]", element['@list'], property, name, &block)
      end

      if object
        # 1.4
        add_quad(path, subject, property, object, name, &block) if subject && property
        return object
      end
    
      active_subject = if element.fetch('@id', nil).is_a?(String)
        # 1.5 Subject
        # 1.5.1 Set active object (subject)
        context.expand_iri(element['@id'], :quiet => true)
      else
        # 1.6) Generate a blank node identifier and set it as the active subject.
        node
      end

      # 1.7) For each key in the JSON object that has not already been processed,
      # perform the following steps:
      element.keys.sort.each do |key|
        value = element[key]
        active_property = case key
        when '@type'
          # If the key is @type, set the active property to rdf:type.
          RDF.type
        when '@graph'
          # Otherwise, if property is @graph, process value algorithm recursively, using active subject
          # as graph name and null values for active subject and active property and then continue to
          # next property
          statements("#{path}[#{key}]", value, nil, nil, active_subject, &block)
          next
        when /^@/
          # Otherwise, if property is a keyword, skip this step.
          next
        else
          # 1.7.1) If a key that is not @id, @graph, or @type, set the active property by
          # performing Property Processing on the key.
          context.expand_iri(key, :quiet => true)
        end

        debug("statements[Step 1.7.4]")
        statements("#{path}[#{key}]", value, active_subject, active_property, name, &block)
      end
    
      # 1.8) The active_subject is returned
      active_subject
    when Array
      # 2) If a regular array is detected ...
      debug("statements[Step 2]")
      element.each_with_index do |v, i|
        statements("#{path}[#{i}]", v, subject, property, name, &block)
      end
      nil # No real value returned from an array
    when String
      object = RDF::Literal.new(element)
      debug(path) {"statements[Step 3]: plain: #{object.inspect}"}
      object
    when Float
      object = RDF::Literal::Double.new(element)
      debug(path) {"statements[Step 4]: native: #{object.inspect}"}
      object
    when Fixnum
      object = RDF::Literal.new(element)
      debug(path) {"statements[Step 5]: native: #{object.inspect}"}
      object
    when TrueClass, FalseClass
      object = RDF::Literal::Boolean.new(element)
      debug(path) {"statements[Step 6]: native: #{object.inspect}"}
      object
    else
      raise RDF::ReaderError, "Traverse to unknown element: #{element.inspect} of type #{element.class}"
    end
  end

  # Yield and return traverse_result
  add_quad(path, subject, property, traverse_result, name, &block) if subject && property && traverse_result
  traverse_result
end