Module: RDFObject::Node

Included in:
BlankNode, Resource
Defined in:
lib/rdf_objects/rdf_resource.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rdf_objects/rdf_resource.rb', line 223

def ==(other)
  return false unless other.is_a?(self.class) or other.is_a?(ResourceReference)
  return false unless self.uri == other.uri
  return false unless self.assertions.keys.sort == other.assertions.keys.sort
  self.assertions.each_pair do |pred, objects|
    return false if objects.class != other[pred].class
    objects = [objects] unless objects.is_a?(Array)
    objects.each do | o |
      match = false
      [*other[pred]].each do |oo|
        next unless oo
        next unless o.class == oo.class
        if oo.is_a?(RDF::Literal)
          match = o == oo
        else
          match = o.uri == oo.uri
        end
        break if match
      end
      return false unless match
    end

  end
  return true      
end

#[](uri) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rdf_objects/rdf_resource.rb', line 42

def [](uri)
  curie = case
  when uri.could_be_a_safe_curie? then Curie.new_from_curie(uri)
  when Curie.curie_from_uri(uri) then Curie.curie_from_uri(uri)
  else 
    return nil
  end
  vocab = self.send(curie.prefix.to_sym)
  return nil unless vocab
  return vocab if curie.reference.empty?
  return vocab[curie.reference]
end

#assert(predicate, object) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rdf_objects/rdf_resource.rb', line 7

def assert(predicate, object)
  curied_predicate = case
  when predicate.could_be_a_safe_curie? then Curie.new_from_curie(predicate)
  when Curie.curie_from_uri(predicate) then Curie.curie_from_uri(predicate)
  else Curie.create_from_uri(predicate)
  end
  self.register_vocabulary(curied_predicate.prefix)
  pred_attr = self.send(curied_predicate.prefix.to_sym)
  if object.is_a?(Resource)
    object = ResourceReference.new(object)
  elsif !object.is_a?(ResourceReference) && !object.is_a?(BlankNode)
    object = RDF::Literal.new(object) unless object.is_a?(RDF::Literal)
  end
  return if assertion_exists?(predicate, object)
  if pred_attr[curied_predicate.reference]
    unless pred_attr[curied_predicate.reference].is_a?(Array)
      pred_attr[curied_predicate.reference] = [pred_attr[curied_predicate.reference]]
    end
    pred_attr[curied_predicate.reference] << object
  else
    pred_attr[curied_predicate.reference] = object
  end
end

#assertion_exists?(predicate, object) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
# File 'lib/rdf_objects/rdf_resource.rb', line 32

def assertion_exists?(predicate, object)
  return false unless self[predicate]
  if self[predicate].is_a?(Array)
    return true if self[predicate].index(object)
  else
    return true if self[predicate] == object
  end
  return false
end

#assertionsObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rdf_objects/rdf_resource.rb', line 87

def assertions
  assertions = {}
  Curie.get_mappings.each do | prefix, uri |
    if self[uri]
      self[uri].keys.each do | pred |
        assertions["#{uri}#{pred}"] = self[uri][pred]
      end
    end
  end
  assertions
end

#describeObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/rdf_objects/rdf_resource.rb', line 76

def describe
  response = HTTPClient.fetch(self.uri)
  local_collection = Parser.parse(response[:content], {:base_uri=>response[:uri]})
  return unless local_collection && local_collection[self.uri]
  local_collection[self.uri].assertions.each do | predicate, object |
    [*object].each do | obj |
      self.assert(predicate, obj) unless self.assertion_exists?(predicate, obj)
    end
  end
end

#empty_graph?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
# File 'lib/rdf_objects/rdf_resource.rb', line 99

def empty_graph?
  Curie.get_mappings.each do | prefix, uri |
    return false if self.respond_to?(prefix.to_sym)
  end
  return true
end

#object_to_json_hash(object) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rdf_objects/rdf_resource.rb', line 167

def object_to_json_hash(object)
  if object.is_a?(Resource) or object.is_a?(ResourceReference)
    o = {:type=>"uri", :value=>object.uri}
  elsif object.is_a?(BlankNode)
    o = {:type=>"bnode", :value=>object.uri}
  else
    object = RDF::Literal.new(object) unless object.is_a?(RDF::Literal)
    o = {:type=>"literal", :value=>object.value}
    o[:lang] = object.language.to_s if object.has_language?
    o[:datatype] = object.datatype.to_s if object.has_datatype?        
  end
  o
end

#prefix_for(uri) ⇒ Object



55
56
57
# File 'lib/rdf_objects/rdf_resource.rb', line 55

def prefix_for(uri)
  Curie.prefix_for(uri)
end

#rdf_description_block(depth = 0) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rdf_objects/rdf_resource.rb', line 186

def rdf_description_block(depth=0)
  rdf = "<rdf:Description #{xml_subject_attribute}>"
  namespaces = {}
  Curie.get_mappings.each_pair do |key, value|
    if self.respond_to?(key.to_sym)
      self.send(key.to_sym).each_pair do | predicate, objects |
        [*objects].each do | object |
          rdf << "<#{key}:#{predicate}"
          namespaces["xmlns:#{key}"] = "#{Curie.parse("[#{key}:]")}"
          if object.is_a?(RDFObject::ResourceReference) || object.is_a?(RDFObject::BlankNode)
            if depth == 0
              rdf << " #{object.xml_object_attribute} />"
            else
              rdf << ">"                  
              ns, rdf_data = object.rdf_description_block(depth-1)
              namespaces.merge!(ns)
              rdf << rdf_data
              rdf << "</#{key}:#{predicate}>"
            end
          else
            object = RDF::Literal.new(object) unless object.is_a?(RDF::Literal)
            if object.language
              rdf << " xml:lang=\"#{object.language}\""
            end
            if object.datatype
              rdf << " rdf:datatype=\"#{object.datatype}\""
            end
            rdf << ">#{CGI.escapeHTML(object.value)}</#{key}:#{predicate}>"
          end
        end
      end
    end
  end
  rdf << "</rdf:Description>"
  [namespaces, rdf]
end

#register_vocabulary(name) ⇒ Object



59
60
61
62
63
# File 'lib/rdf_objects/rdf_resource.rb', line 59

def register_vocabulary(name)
  return if self.respond_to?(name.to_sym)
  self.new_ostruct_member(name)
  self.send("#{name}=".to_sym, {})
end

#relate(predicate, resource) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/rdf_objects/rdf_resource.rb', line 65

def relate(predicate, resource)
  unless resource.is_a?(Resource) or resource.is_a?(BlankNode) or resource.is_a?(ResourceReference)
    if BlankNode.is_bnode_id?(resource)
      resource = BlankNode.new(resource)
    else
      resource = Resource.new(resource)
    end
  end
  self.assert(predicate, resource)
end

#to_jsonObject



182
183
184
# File 'lib/rdf_objects/rdf_resource.rb', line 182

def to_json
  JSON.generate(to_json_hash)
end

#to_json_hashObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rdf_objects/rdf_resource.rb', line 152

def to_json_hash
  j_hash = {self.uri=>{}}
  self.assertions.each_pair do |pred,objects|
    j_hash[self.uri][pred] = []
    if objects.is_a?(Array)
      objects.each do |object|
        j_hash[self.uri][pred] << object_to_json_hash(object)              
      end
    else
      j_hash[self.uri][pred] << object_to_json_hash(objects)   
    end
  end
  j_hash
end

#to_ntriplesObject



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
# File 'lib/rdf_objects/rdf_resource.rb', line 106

def to_ntriples
  ntriples = ""
  Curie.get_mappings.each do | prefix, uri |
    if self[uri]
      self[uri].keys.each do | pred |    
        if self[uri][pred].is_a?(Array)
          objects = self[uri][pred]
        else
          objects = [self[uri][pred]]
        end
        objects.each do | object |           
          line = "#{ntriples_format} <#{uri}#{pred}> "
          if object.is_a?(ResourceReference) || object.is_a?(BlankNode)
            line << object.ntriples_format
          else
            object = RDF::Literal.new(object) unless object.is_a?(RDF::Literal)
            line << "#{object.value.to_json(:ascii_only=>true)}"
            line << "^^<#{object.datatype}>" if object.has_datatype?
            line << "@#{object.language}" if object.has_language?
          end
          line << " .\n"
          ntriples << line              
        end
      end
    end
  end 
  ntriples     
end

#to_xml(depth = 0) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rdf_objects/rdf_resource.rb', line 135

def to_xml(depth=0)
  namespaces, rdf_data = rdf_description_block(depth)
  unless namespaces["xmlns:rdf"]
    if  x = namespaces.index("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
      namespaces.delete(x)
    end
    namespaces["xmlns:rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  end

  rdf = "<rdf:RDF"
  namespaces.each_pair {|key, value| rdf << " #{key}=\"#{value}\""}
  rdf <<">"
  rdf << rdf_data
  rdf << "</rdf:RDF>"
  rdf      
end