Class: OpenStudio::Metadata::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/metadata/writer.rb

Overview

Class to write serialized metadata models to file

Examples:

Write Haystack JSON to file with Writer

creator = OpenStudio::Metadata::Creator.new(path_to_model)
creator.apply_mappings('Haystack')
writer = OpenStudio::Metadata::Writer.new(creator: creator)
writer.write_output_to_file(output_format: 'json')

Instance Method Summary collapse

Constructor Details

#initialize(creator:) ⇒ Writer

Initialize Writer

Parameters:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/openstudio/metadata/writer.rb', line 57

def initialize(creator:)
  @creator = creator
  @files_path = File.join(File.dirname(__FILE__), '../../files')
  @metadata_type = @creator.
  @output_format = nil # set by write_output_to_file
  @brick_graph = nil #
  @haystack = nil

   = ['Brick', 'Haystack']
  raise "metadata_type must be one of #{}" unless .include? @metadata_type
end

Instance Method Details

#create_outputObject

Generates BrickGraph or Haystack from entities



70
71
72
73
74
75
76
77
78
79
# File 'lib/openstudio/metadata/writer.rb', line 70

def create_output
  case @metadata_type
  when 'Brick'
    @brick_graph = BrickGraph.new
    @brick_graph.create_from_entities(@creator.entities)
  when 'Haystack'
    @haystack = Haystack.new
    @haystack = @haystack.create_from_entities(@creator.entities)
  end
end

#write_output_to_file(output_format:, file_path: '.', file_name_without_extension: 'model') ⇒ Object

Write metadata model to file

Parameters:

  • output_format (String)

    One of: [‘json’, ‘ttl’, ‘nq’]

  • file_path (String) (defaults to: '.')

    Path to output folder

  • file_name_without_extension (String) (defaults to: 'model')

    output name without extension



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/openstudio/metadata/writer.rb', line 87

def write_output_to_file(output_format:, file_path: '.', file_name_without_extension: 'model')
  @output_format = output_format
  supported_haystack_formats = ['json']
  supported_brick_formats = ['ttl', 'nq']
  raise "Brick output format must be one of: #{supported_brick_formats}" if (@metadata_type == 'Brick') && !supported_brick_formats.include?(@output_format)
  raise "Haystack output format must be one of: #{supported_haystack_formats}" if (@metadata_type == 'Haystack') && !supported_haystack_formats.include?(@output_format)
  case @metadata_type
  when 'Brick'
    case @output_format
    when 'ttl'
      File.open(File.join(file_path, "#{file_name_without_extension}.ttl"), 'w') { |f| f << @brick_graph.dump(:ttl) }
    when 'nq'
      File.open(File.join(file_path, "#{file_name_without_extension}.nq"), 'w') { |f| f << @brick_graph.dump(:nquads) }
    end
  when 'Haystack'
    File.open(File.join(file_path, "#{file_name_without_extension}.json"), 'w') { |f| f << @haystack }
  end
end