Class: Middleman::Ontologist::Ontology

Inherits:
Object
  • Object
show all
Defined in:
lib/ontologist/ontology.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Ontology

Returns a new instance of Ontology.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ontologist/ontology.rb', line 36

def initialize(app, options = {})
  @app = app
  @options = options

  @_dated_resources = []
  @_uri_map = Hash.new

  @store = RDF::Graph.new

  extend Utilities
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



33
34
35
# File 'lib/ontologist/ontology.rb', line 33

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



32
33
34
# File 'lib/ontologist/ontology.rb', line 32

def options
  @options
end

#storeObject (readonly)

Returns the value of attribute store.



34
35
36
# File 'lib/ontologist/ontology.rb', line 34

def store
  @store
end

Instance Method Details

#manipulate_resource_list(resources) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
# File 'lib/ontologist/ontology.rb', line 77

def manipulate_resource_list(resources)

  encountered_uris = Set.new

  resources.each do |resource|

    next unless options.resource_extension_list.index(resource.ext)
    
    if resource.data[options.uri_tag].nil?
      if options.strict
        raise "All resources with correct extensions must have a URI tag in their YAML frontmatter"
      else
        next
      end
    end

    resource.extend Middleman::Ontologist::SemanticResource
    resource.ontology = self
    resource.uri = resolve_key(resource.data[options.uri_tag])

    if encountered_uris.include? resource.uri
      raise "All resources must have unique URIs (multiple resources using #{resource.uri} found)"
    else
      encountered_uris << resource.uri
    end



    resource.data.each do |element|
      key   = element.first
      value = element.last

      # we already have the uri
      next if key == options.uri_tag

      # should we ignore this key?
      next if options.ignore_list.include? key

      key = resolve_key(key)

      processed_values = []

      if value.is_a? Array
        # value(s) are URIs
        value.each do |v|
          processed_values << RDF::URI.new(resolve_key(v))
        end
      elsif value.is_a? String
        # value is a string literal
          processed_values << RDF::Literal.new(value)
      else
        raise "Don't know how to handle value of class #{value.class.to_s}"
      end

      processed_values.each do |pv|
        statement = [RDF::URI.new(resource.uri), RDF::URI.new(key), pv]
        store << statement
      end

    end

  end

  resources # return the now slightly modified list

end

#resolve_key(key) ⇒ Object

takes a string key that should resolve into a full URI and tries to resolve it



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ontologist/ontology.rb', line 49

def resolve_key(key)
  return nil if key.nil? 

  # is this tag an alias?
  if alias_array = options.aliases.assoc(key)
    key = alias_array[1]
  end

  # is this a fully formed url?
  unless valid_url? key
    
    # resolve the namespace
    parts = key.split("/", 2)
    if parts.count == 1
      # default namespace
      key = options.default_namespace + parts.first
    else
      # should be a predefined namespace
      raise "Abbreviated namespace not found: #{parts.first}" unless options.namespaces.include? parts.first

      key = options.namespaces[parts.first] + parts.last
    end

  end

  key # return the final version
end

#resource_for_uri(uri) ⇒ Object



144
145
146
# File 'lib/ontologist/ontology.rb', line 144

def resource_for_uri(uri)
  @_uri_map[uri]
end