Class: Chronicle::Schema::RDFParsing::RDFSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/chronicle/schema/rdf_parsing/rdf_serializer.rb

Overview

Take a graph and serialize it as a ttl string

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph) ⇒ RDFSerializer

Returns a new instance of RDFSerializer.

Raises:

  • (ArgumentError)


20
21
22
23
24
# File 'lib/chronicle/schema/rdf_parsing/rdf_serializer.rb', line 20

def initialize(graph)
  raise ArgumentError, 'graph must be a SchemaGraph' unless graph.is_a?(Chronicle::Schema::SchemaGraph)

  @graph = graph
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



18
19
20
# File 'lib/chronicle/schema/rdf_parsing/rdf_serializer.rb', line 18

def graph
  @graph
end

Class Method Details

.serialize(graph, include_generator_comment: true) ⇒ Object



26
27
28
# File 'lib/chronicle/schema/rdf_parsing/rdf_serializer.rb', line 26

def self.serialize(graph, include_generator_comment: true)
  new(graph).serialize(include_generator_comment:)
end

Instance Method Details

#serialize(include_generator_comment: true) ⇒ Object



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
# File 'lib/chronicle/schema/rdf_parsing/rdf_serializer.rb', line 30

def serialize(include_generator_comment: true)
  schema_graph = RDF::Graph.new

  schema_graph << ontology_triple
  schema_graph << version_triple

  graph.types.each do |klass|
    serialize_class(klass).each do |triple|
      # binding.pry
      schema_graph << triple
    end
  end

  graph.properties.each do |property|
    serialize_property(property).each do |triple|
      schema_graph << triple
    end
  end

  prefixes = {
    '': default_namespace
  }.merge(PREFIXES)

  output_str = ''
  output_str += generation_header if include_generator_comment
  output_str + schema_graph.dump(:ttl, prefixes:)
end