Class: RDF::Vocabulary::Writer

Inherits:
Writer show all
Includes:
Util::Logger
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocab/writer.rb

Instance Attribute Summary collapse

Attributes inherited from Writer

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Methods inherited from Writer

#base_uri, buffer, #canonicalize?, dump, each, #encoding, #escaped, #flush, for, format, #format_list, #format_literal, #format_node, #format_term, #format_uri, #node_id, open, #prefix, #prefixes, #prefixes=, #puts, #quoted, #to_sym, to_sym, #uri_for, #validate?, #write_comment, #write_prologue, #write_statement, #write_triples

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Writable

#<<, #insert, #insert_graph, #insert_reader, #insert_statement, #insert_statements, #writable?

Constructor Details

#initialize(output = $stdout, base_uri:, **options) {|writer| ... } ⇒ Writer

Initializes the writer.

Parameters:

Options Hash (**options):

  • :class_name (String)

    Class name for this vocabulary

  • :module_name (String) — default: "RDF"

    Module name for this vocabulary

  • extra (Hash)

    Extra properties to add to the output (programatic only)

  • patch (String)

    An LD Patch to run against the graph before writing

  • strict (Boolean) — default: false

    Create an RDF::StrictVocabulary instead of an RDF::Vocabulary

Yields:

  • (writer)

    self

Yield Parameters:

Yield Returns:

  • (void)


79
80
81
82
83
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocab/writer.rb', line 79

def initialize(output = $stdout, base_uri:, **options, &block)
  @graph = RDF::Repository.new
  options.merge(base_uri: base_uri)
  super
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name



18
19
20
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocab/writer.rb', line 18

def class_name
  @class_name
end

#module_nameObject

Returns the value of attribute module_name



18
19
20
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocab/writer.rb', line 18

def module_name
  @module_name
end

Class Method Details

.optionsObject



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
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocab/writer.rb', line 20

def self.options
  [
    RDF::CLI::Option.new(
      symbol: :class_name,
      datatype: String,
      control: :text,
      on: ["--class-name NAME"],
      use: :required,
      description: "Name of created Ruby class (vocabulary format)."),
    RDF::CLI::Option.new(
      symbol: :module_name,
      datatype: String,
      control: :text,
      on: ["--module-name NAME"],
      description: "Name of Ruby module containing class-name (vocabulary format)."),
    RDF::CLI::Option.new(
      symbol: :strict,
      datatype: TrueClass,
      control: :checkbox,
      on: ["--strict"],
      description: "Make strict vocabulary"
    ) {true},
    RDF::CLI::Option.new(
      symbol: :extra,
      datatype: String,
      control: :none,
      on: ["--extra URIEncodedJSON"],
      description: "URI Encoded JSON representation of extra data"
    ) do |arg|
      ::JSON.parse(::URI.decode(arg)).inject({}) do |m1, (term, defs)|
        d1 = defs.inject({}) {|m, (k,v)| m.merge(k.to_sym => v)}
        m1.merge(term.to_sym => d1)
      end
    end,
  ]
end

Instance Method Details

#write_epilogueObject

Generate vocabulary



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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocab/writer.rb', line 91

def write_epilogue
  class_name = options[:class_name]
  module_name = options.fetch(:module_name, "RDF")
  source = options.fetch(:location, base_uri)
  strict = options.fetch(:strict, false)

  # Passing a graph for the location causes it to serialize the written triples.
  vocab = RDF::Vocabulary.from_graph(@graph,
                                     url: base_uri,
                                     class_name: class_name,
                                     extra: options[:extra])

  @output.print %(# -*- encoding: utf-8 -*-
    # frozen_string_literal: true
    # This file generated automatically using rdf vocabulary format from #{source}
    require 'rdf'
    module #{module_name}
      # @!parse
      #   # Vocabulary for <#{base_uri}>
      #   class #{class_name} < RDF::#{"Strict" if strict}Vocabulary
      #   end
      class #{class_name} < RDF::#{"Strict" if strict}Vocabulary("#{base_uri}")
    ).gsub(/^          /, '')

  # Split nodes into Class/Property/Datatype/Other
  term_nodes = {
    ontology: {},
    class: {},
    property: {},
    datatype: {},
    other: {}
  }

  # Generate Ontology first
  if vocab.ontology
    term_nodes[:ontology][vocab.ontology.to_s] = vocab.ontology.attributes
  end

  vocab.each.to_a.sort.each do |term|
    name = term.to_s[base_uri.length..-1].to_sym
    next if name.to_s.empty?  # Ontology serialized separately
    kind = begin
      case term.type.to_s
      when /Class/    then :class
      when /Property/ then :property
      when /Datatype/ then :datatype
      else                 :other
      end
    rescue KeyError
      # This can try to resolve referenced terms against the previous version of this vocabulary, which may be strict, and fail if the referenced term hasn't been created yet.
      :other
    end
    term_nodes[kind][name] = term.attributes
  end

  {
    ontology: "Ontology definition",
    class: "Class definitions",
    property: "Property definitions",
    datatype: "Datatype definitions",
    other: "Extra definitions"
  }.each do |tt, comment|
    next if term_nodes[tt].empty?
    @output.puts "\n    # #{comment}"
    term_nodes[tt].each {|name, attributes| from_node name, attributes, tt}
  end

  # Query the vocabulary to extract property and class definitions
  @output.puts "  end\nend"
end

#write_triple(subject, predicate, object) ⇒ Object



85
86
87
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocab/writer.rb', line 85

def write_triple(subject, predicate, object)
  @graph << RDF::Statement(subject, predicate, object)
end