Class: OpenStudio::Metadata::BrickGraph

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

Overview

Class to serialize entities into Brick Graph

Examples:

Use BrickGraph to make a ‘ttl` from a list of entities

entities = creator.entities
brick_graph = BrickGraph.new
brick_graph.create_graph_from_entities(entities)
ttl = brick_graph.dump(:ttl)

Instance Method Summary collapse

Constructor Details

#initialize(building_namespace: 'http://example.com/mybuilding#') ⇒ BrickGraph

Returns new instance of BrickGraph

Parameters:

  • building_namespace (String) (defaults to: 'http://example.com/mybuilding#')

    used for ‘bldg` prefix in ttl



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/openstudio/metadata/serializer.rb', line 52

def initialize(building_namespace: 'http://example.com/mybuilding#')
  @brick = RDF::Vocabulary.new('https://brickschema.org/schema/1.1/Brick#')
  @bldg = RDF::Vocabulary.new(building_namespace)
  @prefixes = {
    rdf: RDF.to_uri,
    rdfs: RDF::RDFS.to_uri,
    brick: @brick,
    bldg: @bldg
  }
  @g = nil
end

Instance Method Details

#create_from_entities(entities) ⇒ Object

Creates graph from list of entities

Parameters:



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

def create_from_entities(entities)
  @g = RDF::Repository.new
  entities.each do |entity|
    @g << RDF::Statement.new(@bldg[entity['id']], RDF.type, @brick[entity['type']])
    @g << RDF::Statement.new(@bldg[entity['id']], RDF::RDFS.label, entity['dis'])
    if entity.key? 'relationships'
      entity['relationships'].each do |relationship, reference|
        @g << RDF::Statement.new(@bldg[entity['id']], @brick[relationship], @bldg[reference])
      end
    end
  end
end

#dump(format = :ttl) ⇒ String

Outputs Brick graph in desired ‘format`

Parameters:

  • format (Symbol) (defaults to: :ttl)

    A symbol declaring to format to dump the graph as

Returns:

  • (String)

    A string representation of the graph in the desired format

See Also:



89
90
91
# File 'lib/openstudio/metadata/serializer.rb', line 89

def dump(format = :ttl)
  return @g.dump(format, prefixes: @prefixes)
end