Module: JSON::LD::ToRDF

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

Instance Method Summary collapse

Methods included from Utils

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

Instance Method Details

#graph_to_rdf(active_graph) ⇒ Array<RDF::Statement>

Returns statements in this graph, without context.

Parameters:

  • active_graph (Hash{String => Hash})

    A hash of IRI to Node definitions

Returns:

  • (Array<RDF::Statement>)

    statements in this graph, without context



13
14
15
16
17
18
19
20
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
# File 'lib/json/ld/to_rdf.rb', line 13

def graph_to_rdf(active_graph)
  debug('graph_to_rdf') {"graph_to_rdf: #{active_graph.inspect}"}

  # Initialize results as an empty array
  results = []

  depth do
    # For each id-node in active_graph
    active_graph.each do |id, node|
      # Initialize subject as the IRI or BNode representation of id
      subject = as_resource(id, context.doc_base)
      debug("graph_to_rdf")  {"subject: #{subject.to_ntriples}"}

      # For each property-values in node
      node.each do |property, values|
        case property
        when '@type'
          # If property is @type, construct triple as an RDF Triple composed of id, rdf:type, and object from values where id and object are represented either as IRIs or Blank Nodes
          results += values.map do |value|
            object = as_resource(value, context.doc_base)
            debug("graph_to_rdf")  {"type: #{object.to_ntriples}"}
            RDF::Statement.new(subject, RDF.type, object)
          end
        when /^@/
          # Otherwise, if @type is any other keyword, skip to the next property-values pair
        else
          # Otherwise, property is an IRI or Blank Node identifier
          # Initialize predicate from  property as an IRI or Blank node
          predicate = as_resource(property, context.doc_base)
          debug("graph_to_rdf")  {"predicate: #{predicate.to_ntriples}"}

          # For each item in values
          values.each do |item|
            if item.has_key?('@list')
              debug("graph_to_rdf")  {"list: #{item.inspect}"}
              # If item is a list object, initialize list_results as an empty array, and object to the result of the List Conversion algorithm, passing the value associated with the @list key from item and list_results.
              list_results = []
              object = parse_list(item['@list'], list_results)

              # Append a triple composed of subject, prediate, and object to results and add all triples from list_results to results.
              results << RDF::Statement.new(subject, predicate, object)
              results += list_results
            else
              # Otherwise, item is a value object or a node definition. Generate object as the result of the Object Converstion algorithm passing item.
              object = parse_object(item)
              debug("graph_to_rdf")  {"object: #{object.to_ntriples}"}
              # Append a triple composed of subject, prediate, and literal to results.
              results << RDF::Statement.new(subject, predicate, object)
            end
          end
        end
      end
    end
  end
  
  # Return results
  results
end

#nodeObject

Create a new named node using the sequence



145
146
147
# File 'lib/json/ld/to_rdf.rb', line 145

def node
  RDF::Node.new(namer.get_sym)
end

#parse_list(list, list_results) ⇒ RDF::Resource

Parse a List

Parameters:

  • list (Array)

    The Array to serialize as a list

  • list_results (Array<RDF::Statement>)

    Statements for each item in the list

Returns:

  • (RDF::Resource)

    BNode or nil for head of list



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/json/ld/to_rdf.rb', line 119

def parse_list(list, list_results)
  debug('parse_list') {"list: #{list.inspect}"}

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

  depth do
    list.each do |list_item|
      # Set first to the result of the Object Converstion algorithm passing item.
      object = parse_object(list_item)
      list_results << RDF::Statement.new(first_bnode, RDF.first, object)
      rest_bnode = node
      list_results << RDF::Statement.new(first_bnode, RDF.rest, rest_bnode)
      first_bnode = rest_bnode
    end
    if last
      object = parse_object(last)
      list_results << RDF::Statement.new(first_bnode, RDF.first, object)
      list_results << RDF::Statement.new(first_bnode, RDF.rest, RDF.nil)
    end
  end
  result
end

#parse_object(item) ⇒ RDF::Value

Parse an item, either a value object or a node definition

Parameters:

Returns:

  • (RDF::Value)


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

def parse_object(item)
  if item.has_key?('@value')
    # Otherwise, if element is a JSON object that contains the key @value
    # Initialize value to the value associated with the @value key in element. Initialize datatype to the value associated with the @type key in element, or null if element does not contain that key.
    value, datatype = item.fetch('@value'), item.fetch('@type', nil)

    case value
    when TrueClass, FalseClass
      # If value is true or false, then set value its canonical lexical form as defined in the section Data Round Tripping. If datatype is null, set it to xsd:boolean.
      value = value.to_s
      datatype ||= RDF::XSD.boolean.to_s
    when Float, Fixnum
      # Otherwise, if value is a number, then set value to its canonical lexical form as defined in the section Data Round Tripping. If datatype is null, set it to either xsd:integer or xsd:double, depending on if the value contains a fractional and/or an exponential component.
      lit = RDF::Literal.new(value, :canonicalize => true)
      value = lit.to_s
      datatype ||= lit.datatype
    else
      # Otherwise, if datatype is null, set it to xsd:string or xsd:langString, depending on if item has a @language key.
      datatype ||= item.has_key?('@language') ? RDF.langString : RDF::XSD.string
    end
              
    # Initialize literal as an RDF literal using value and datatype. If element has the key @language and datatype is xsd:string, then add the value associated with the @language key as the language of the object.
    language = item.fetch('@language', nil)
    literal = RDF::Literal.new(value, :datatype => datatype, :language => language)

    # Return literal
    literal
  else
    # Otherwise, value must be a node definition containing only @id whos value is an IRI or Blank Node identifier
    raise "Expected node reference, got #{item.inspect}" unless item.keys == %w(@id)
    # Return value associated with @id as an IRI or Blank node
    as_resource(item['@id'], context.doc_base)
  end
end