Method: JSON::LD::API.toRDF

Defined in:
lib/json/ld/api.rb

.toRDF(input, context = nil, options = {}) {|statement| ... } ⇒ Array<RDF::Statement>

Processes the input according to the RDF Conversion Algorithm, calling the provided callback for each triple generated.

Parameters:

  • input (String, #read, Hash, Array)

    The JSON-LD object to process when outputting statements.

  • context (String, #read, Hash, Array, JSON::LD::Context) (defaults to: nil)

    An external context to use additionally to the context embedded in input when expanding the input.

  • options ({Symbol,String => Object}) (defaults to: {})

    See options in #initialize Options passed to expand

Options Hash (options):

  • :produceGeneralizedRDF (Boolean) — default: false

    If true, output will include statements having blank node predicates, otherwise they are dropped.

Yields:

  • statement

Yield Parameters:

  • statement (RDF::Statement)

Returns:

  • (Array<RDF::Statement>)

    if no block given

Raises:



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/json/ld/api.rb', line 348

def self.toRDF(input, context = nil, options = {}, &block)
  results = []
  results.extend(RDF::Enumerable)

  # Expand input to simplify processing
  expanded_input = API.expand(input, context, options)

  API.new(expanded_input, context, options) do
    # 1) Perform the Expansion Algorithm on the JSON-LD input.
    #    This removes any existing context to allow the given context to be cleanly applied.
    debug(".toRDF") {"expanded input: #{expanded_input.to_json(JSON_STATE)}"}

    # Generate _nodeMap_
    node_map = Hash.ordered
    node_map['@default'] = Hash.ordered
    generate_node_map(expanded_input, node_map)
    debug(".toRDF") {"node map: #{node_map.to_json(JSON_STATE)}"}

    # Start generating statements
    node_map.each do |graph_name, graph|
      context = as_resource(graph_name) unless graph_name == '@default'
      debug(".toRDF") {"context: #{context ? context.to_ntriples : 'null'}"}
      graph_to_rdf(graph).each do |statement|
        next if statement.predicate.node? && !options[:produceGeneralizedRDF]
        statement.context = context if context
        if block_given?
          yield statement
        else
          results << statement
        end
      end
    end
  end
  results
end