Class: RDFS::Resource
- Inherits:
-
Object
- Object
- RDFS::Resource
- Defined in:
- lib/active_rdf/objectmanager/resource.rb
Overview
Represents an RDF resource and manages manipulations of that resource, including data lookup (e.g. eyal.age), data updates (e.g. eyal.age=20), class-level lookup (Person.find_by_name ‘eyal’), and class-membership (eyal.class …Person).
Direct Known Subclasses
Class Attribute Summary collapse
-
.class_uri ⇒ Object
Returns the value of attribute class_uri.
Instance Attribute Summary collapse
-
#uri ⇒ Object
(also: #to_s)
readonly
uri of the resource (for instances of this class: rdf resources).
Class Method Summary collapse
- .==(other) ⇒ Object
- .eql?(other) ⇒ Boolean
-
.find(uri) ⇒ Object
Find an existing resource with the given uri, otherwise returns nil.
-
.find_all(options = {}, &blk) ⇒ Object
Find all resources of this type.
-
.find_by(context = nil) ⇒ Object
Find resources of this type, restricted by optional property args see ActiveRDF::ResourceQuery usage.
- .localname ⇒ Object
-
.method_missing(method, *args) ⇒ Object
Pass all other methods to class_uri.
-
.predicates ⇒ Object
returns the predicates that have this resource as their domain (applicable predicates for this resource).
- .properties ⇒ Object
- .uri ⇒ Object
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
overriding sort based on uri.
-
#==(other) ⇒ Object
(also: #eql?)
a resource is same as another if they both represent the same uri.
-
#abbr ⇒ Object
get an abbreviation from to_s returns a copy of uri if no abbreviation found.
-
#abbr? ⇒ Boolean
checks if an abbrivation exists for this resource.
- #abbreviation ⇒ Object
-
#class_level_predicates ⇒ Object
returns array of RDFS::Resources for properties that belong to this resource.
-
#class_predicates ⇒ Object
returns array of RDFS::Resources for the class properties of this resource, including those of its supertypes.
-
#class_properties ⇒ Object
returns array of RDF::Propertys for properties that belong to this resource.
-
#classes ⇒ Object
returns array of Classes for all types.
- #contexts ⇒ Object
-
#direct_predicates ⇒ Object
returns array of RDFS::Resources for properties that are directly defined for this resource.
-
#direct_properties ⇒ Object
returns array of RDF::Propertys that are directly defined for this resource.
- #direct_types ⇒ Object
-
#empty_predicates ⇒ Object
returns array RDFS::Resources for known properties that do not have a value.
-
#empty_properties ⇒ Object
returns array RDF::Propertys for known properties that do not have a value.
-
#hash ⇒ Object
overriding hash to use uri.hash needed for array.uniq.
-
#initialize(uri_or_resource) ⇒ Resource
constructor
creates new resource representing an RDF resource.
- #inspect ⇒ Object
- #instance_of?(klass) ⇒ Boolean
-
#instance_predicates ⇒ Object
for resources of type RDFS::Class, returns array of RDFS::Resources for the known properties of their objects, including those of its supertypes.
-
#instance_properties ⇒ Object
for resources of type RDFS::Class, returns array of RDF::Propertys for the known properties of their objects.
- #is_a?(klass) ⇒ Boolean
- #localname ⇒ Object
-
#method_missing(method, *args) ⇒ Object
Searches for property belonging to this resource.
- #new_record? ⇒ Boolean
-
#predicates ⇒ Object
returns array of RDFS::Resources for all known properties of this resource.
-
#properties ⇒ Object
returns array of RDF::Propertys for all known properties of this resource.
-
#register_predicate(localname, fulluri) ⇒ Object
TODO: remove define a localname for a predicate URI.
-
#save ⇒ Object
saves instance into datastore.
-
#sub_predicates ⇒ Object
for resources of type RDF::Property, returns array of RDFS::Resources for all sub properties defined by RDFS::subPropertyOf.
-
#sub_properties ⇒ Object
for resources of type RDF::Property, returns array of RDF::Propertys for all sub properties defined by RDFS::subPropertyOf.
-
#super_classes ⇒ Object
for resources of type RDFS::Class, returns array of classes for all super types defined by RDF::subClassOf otherwise returns empty array.
-
#super_predicates ⇒ Object
for resources of type RDF::Property, returns array of RDFS::Resources for all super properties defined by RDFS::subPropertyOf.
-
#super_properties ⇒ Object
for resources of type RDF::Property, returns array of RDF::Propertys for all super properties defined by RDFS::subPropertyOf.
-
#super_types ⇒ Object
for resources of type RDFS::Class, returns array of RDFS::Resources for all super types defined by RDF::subClassOf.
- #to_literal_s ⇒ Object
- #to_xml ⇒ Object
-
#type ⇒ Object
returns an RDF::Property for RDF::type’s of this resource, e.g.
- #type=(type) ⇒ Object
-
#types ⇒ Object
returns array of RDFS::Resources for all types, including supertypes.
Constructor Details
#initialize(uri_or_resource) ⇒ Resource
creates new resource representing an RDF resource
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 73 def initialize(uri_or_resource) @uri = case uri_or_resource # allow Resource.new(other_resource) when RDFS::Resource uri_or_resource.uri # allow Resource.new('<uri>') by stripping out <> when /^<([^>]*)>$/ $1 # allow Resource.new('uri') when String uri_or_resource else raise ActiveRdfError, "cannot create resource <#{uri_or_resource}>" end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Searches for property belonging to this resource. Returns RDF::Property. Replaces any existing values if method is an assignment: resource.prop = new_value
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 385 def method_missing(method, *args) # check for custom written method # eyal.age is a custom-written method in class Person # evidence: eyal type ?c, ?c.methods includes age # action: return results from calling custom method classes.each do |klass| if klass.instance_methods.include?(method.to_s) _dup = klass.new(uri) return _dup.send(method,*args) end end # otherwise pass search on to PropertyQuery ActiveRDF::PropertyLookup.new(self).method_missing(method, *args) end |
Class Attribute Details
.class_uri ⇒ Object
Returns the value of attribute class_uri.
18 19 20 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 18 def class_uri @class_uri end |
Instance Attribute Details
#uri ⇒ Object (readonly) Also known as: to_s
uri of the resource (for instances of this class: rdf resources)
70 71 72 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 70 def uri @uri end |
Class Method Details
.==(other) ⇒ Object
25 26 27 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 25 def Resource.==(other) other.respond_to?(:uri) ? other.uri == self.uri : false end |
.eql?(other) ⇒ Boolean
29 30 31 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 29 def Resource.eql?(other) self == other end |
.find(uri) ⇒ Object
Find an existing resource with the given uri, otherwise returns nil
59 60 61 62 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 59 def Resource.find(uri) res = Resource.new(uri) res unless res.new_record? end |
.find_all(options = {}, &blk) ⇒ Object
Find all resources of this type
48 49 50 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 48 def Resource.find_all( = {}, &blk) ActiveRDF::ResourceQuery.new(self,.delete(:context)).execute(,&blk) end |
.find_by(context = nil) ⇒ Object
Find resources of this type, restricted by optional property args see ActiveRDF::ResourceQuery usage
54 55 56 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 54 def Resource.find_by(context = nil) ActiveRDF::ResourceQuery.new(self,context) end |
.localname ⇒ Object
33 34 35 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 33 def Resource.localname ActiveRDF::Namespace.localname(self) end |
.method_missing(method, *args) ⇒ Object
Pass all other methods to class_uri
65 66 67 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 65 def Resource.method_missing(method,*args) class_uri.send(method,*args) end |
.predicates ⇒ Object
returns the predicates that have this resource as their domain (applicable predicates for this resource)
39 40 41 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 39 def Resource.predicates class_uri.instance_predicates end |
.properties ⇒ Object
43 44 45 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 43 def Resource.properties predicates.collect{|prop| RDF::Property.new(prop)} end |
.uri ⇒ Object
21 22 23 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 21 def Resource.uri class_uri.uri end |
Instance Method Details
#<=>(other) ⇒ Object
overriding sort based on uri
111 112 113 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 111 def <=>(other) uri <=> other.uri end |
#==(other) ⇒ Object Also known as: eql?
a resource is same as another if they both represent the same uri
99 100 101 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 99 def ==(other); other.respond_to?(:uri) ? other.uri == self.uri : false end |
#abbr ⇒ Object
get an abbreviation from to_s returns a copy of uri if no abbreviation found
141 142 143 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 141 def abbr (abbr = ActiveRDF::Namespace.abbreviate(uri)) ? abbr : uri end |
#abbr? ⇒ Boolean
checks if an abbrivation exists for this resource
146 147 148 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 146 def abbr? ActiveRDF::Namespace.prefix(self) ? true : false end |
#abbreviation ⇒ Object
135 136 137 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 135 def abbreviation [ActiveRDF::Namespace.prefix(uri).to_s, localname] end |
#class_level_predicates ⇒ Object
returns array of RDFS::Resources for properties that belong to this resource
197 198 199 200 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 197 def class_predicates ActiveRDF::Query.new.distinct(:p).where(:p,RDFS::domain,:t).where(self,RDF::type,:t).execute | ActiveRDF::Query.new.distinct(:p).where(:p,RDFS::domain,RDFS::Resource).execute # all resources share RDFS::Resource properties end |
#class_predicates ⇒ Object
returns array of RDFS::Resources for the class properties of this resource, including those of its supertypes
193 194 195 196 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 193 def class_predicates ActiveRDF::Query.new.distinct(:p).where(:p,RDFS::domain,:t).where(self,RDF::type,:t).execute | ActiveRDF::Query.new.distinct(:p).where(:p,RDFS::domain,RDFS::Resource).execute # all resources share RDFS::Resource properties end |
#class_properties ⇒ Object
returns array of RDF::Propertys for properties that belong to this resource
200 201 202 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 200 def class_properties class_predicates.collect{|prop| RDF::Property.new(prop,self)} end |
#classes ⇒ Object
returns array of Classes for all types
174 175 176 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 174 def classes types.collect{|type_res| ActiveRDF::ObjectManager.construct_class(type_res)} end |
#contexts ⇒ Object
248 249 250 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 248 def contexts ActiveRDF::Query.new.distinct(:c).where(self,nil,nil,:c).execute end |
#direct_predicates ⇒ Object
returns array of RDFS::Resources for properties that are directly defined for this resource
205 206 207 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 205 def direct_predicates ActiveRDF::Query.new.distinct(:p).where(self,:p,:o).execute end |
#direct_properties ⇒ Object
returns array of RDF::Propertys that are directly defined for this resource
210 211 212 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 210 def direct_properties direct_predicates.collect{|prop| RDF::Property.new(prop,self)} end |
#direct_types ⇒ Object
171 172 173 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 171 def types type.to_a | [RDFS::Resource.class_uri] # all resources are subtype of RDFS::Resource end |
#empty_predicates ⇒ Object
returns array RDFS::Resources for known properties that do not have a value
225 226 227 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 225 def empty_predicates empty_properties.collect{|prop| RDFS::Resource.new(prop)} end |
#empty_properties ⇒ Object
returns array RDF::Propertys for known properties that do not have a value
230 231 232 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 230 def empty_properties properties.reject{|prop| prop.size > 0} end |
#hash ⇒ Object
overriding hash to use uri.hash needed for array.uniq
106 107 108 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 106 def hash uri.hash end |
#inspect ⇒ Object
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 324 def inspect if ActiveRDF::ConnectionPool.adapters.size > 0 type = if (t = self.type) and t.size > 0 t = t.collect{|res| res.abbr } t.size > 1 ? t.inspect : t.first else self.class end label = if abbr? abbr elsif (l = self.label) and l.size > 0 if l.size == 1 then l.only else l.inspect end else uri end else type = self.class label = self.uri end "#<#{type} #{label}>" end |
#instance_of?(klass) ⇒ Boolean
120 121 122 123 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 120 def instance_of?(klass) klass = ActiveRDF::ObjectManager.construct_class(klass) super || direct_types.any?{|t| klass == t} end |
#instance_predicates ⇒ Object
for resources of type RDFS::Class, returns array of RDFS::Resources for the known properties of their objects, including those of its supertypes
235 236 237 238 239 240 241 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 235 def instance_predicates ip = ActiveRDF::Query.new.distinct(:p).where(:p,RDFS::domain,self).execute if ip.size > 0 ip |= ActiveRDF::Query.new.distinct(:p).where(:p,RDFS::domain,RDFS::Resource).execute # all resources share RDFS::Resource properties else [] end end |
#instance_properties ⇒ Object
for resources of type RDFS::Class, returns array of RDF::Propertys for the known properties of their objects
244 245 246 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 244 def instance_properties instance_predicates.collect{|prop| RDF::Property.new(prop,self)} end |
#is_a?(klass) ⇒ Boolean
115 116 117 118 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 115 def is_a?(klass) klass = ActiveRDF::ObjectManager.construct_class(klass) super || types.any?{|t| klass == t} end |
#localname ⇒ Object
150 151 152 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 150 def localname ActiveRDF::Namespace.localname(self) end |
#new_record? ⇒ Boolean
125 126 127 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 125 def new_record? ActiveRDF::Query.new.count(:p).where(self,:p,:o).execute == 0 end |
#predicates ⇒ Object
returns array of RDFS::Resources for all known properties of this resource
215 216 217 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 215 def predicates direct_predicates | class_predicates end |
#properties ⇒ Object
returns array of RDF::Propertys for all known properties of this resource
220 221 222 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 220 def properties predicates.collect{|prop| RDF::Property.new(prop,self)} end |
#register_predicate(localname, fulluri) ⇒ Object
TODO: remove define a localname for a predicate URI
localname should be a Symbol or String, fulluri a Resource or String, e.g. register_predicate(:name, FOAF::lastName)
183 184 185 186 187 188 189 190 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 183 def register_predicate(localname, fulluri) warn "Registered predicates is deprecated. Please use registered namespaces instead." localname = localname.to_s fulluri = RDFS::Resource.new(fulluri) if fulluri.is_a? String # predicates is a hash from abbreviation string to full uri resource (@predicates ||= {})[localname] = fulluri end |
#save ⇒ Object
saves instance into datastore
130 131 132 133 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 130 def save ActiveRDF::ConnectionPool.write_adapter.add(self,RDF::type,self.class) self end |
#sub_predicates ⇒ Object
for resources of type RDF::Property, returns array of RDFS::Resources for all sub properties defined by RDFS::subPropertyOf
305 306 307 308 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 305 def sub_predicates subs = ActiveRDF::Query.new.distinct(:sub_property).where(:sub_property, RDFS::subPropertyOf, self).execute subs |= subs.inject([]){|subsubs, sub| subsubs |= sub.sub_predicates} end |
#sub_properties ⇒ Object
for resources of type RDF::Property, returns array of RDF::Propertys for all sub properties defined by RDFS::subPropertyOf
311 312 313 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 311 def sub_properties sub_predicates.collect{|prop| RDF::Property.new(prop,self)} end |
#super_classes ⇒ Object
for resources of type RDFS::Class, returns array of classes for all super types defined by RDF::subClassOf otherwise returns empty array
289 290 291 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 289 def super_classes super_types.collect{|type_res| ActiveRDF::ObjectManager.construct_class(type_res)} end |
#super_predicates ⇒ Object
for resources of type RDF::Property, returns array of RDFS::Resources for all super properties defined by RDFS::subPropertyOf
294 295 296 297 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 294 def super_predicates sups = ActiveRDF::Query.new.distinct(:super_property).where(self, RDFS::subPropertyOf, :super_property).execute sups |= sups.inject([]){|supsups, sup| supsups |= sup.super_predicates} end |
#super_properties ⇒ Object
for resources of type RDF::Property, returns array of RDF::Propertys for all super properties defined by RDFS::subPropertyOf
300 301 302 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 300 def super_properties super_predicates.collect{|prop| RDF::Property.new(prop,self)} end |
#super_types ⇒ Object
for resources of type RDFS::Class, returns array of RDFS::Resources for all super types defined by RDF::subClassOf
282 283 284 285 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 282 def super_types sups = ActiveRDF::Query.new.distinct(:super_class).where(self,RDFS::subClassOf,:super_class).execute sups |= sups.inject([]){|supsups, sup| supsups |= sup.super_types} end |
#to_literal_s ⇒ Object
319 320 321 322 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 319 def to_literal_s raise ActiveRDF::ActiveRdfError, "emtpy RDFS::Resources not allowed" if self.uri.size == 0 "<#{uri}>" end |
#to_xml ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 350 def to_xml base = ActiveRDF::Namespace.(ActiveRDF::Namespace.prefix(self),'').chop xml = "<?xml version=\"1.0\"?>\n" xml += "<rdf:RDF xmlns=\"#{base}\#\"\n" ActiveRDF::Namespace.abbreviations.each { |p| uri = ActiveRDF::Namespace.(p,''); xml += " xmlns:#{p.to_s}=\"#{uri}\"\n" if uri != base + '#' } xml += " xml:base=\"#{base}\">\n" xml += "<rdf:Description rdf:about=\"\##{localname}\">\n" direct_predicates.each do |p| objects = ActiveRDF::Query.new.distinct(:o).where(self, p, :o).execute objects.each do |obj| prefix, localname = ActiveRDF::Namespace.prefix(p), ActiveRDF::Namespace.localname(p) pred_xml = if prefix "%s:%s" % [prefix, localname] else p.uri end case obj when RDFS::Resource xml += " <#{pred_xml} rdf:resource=\"#{obj.uri}\"/>\n" when LocalizedString xml += " <#{pred_xml} xml:lang=\"#{obj.lang}\">#{obj}</#{pred_xml}>\n" else xml += " <#{pred_xml} rdf:datatype=\"#{obj.xsd_type.uri}\">#{obj}</#{pred_xml}>\n" end end end xml += "</rdf:Description>\n" xml += "</rdf:RDF>" end |
#type ⇒ Object
returns an RDF::Property for RDF::type’s of this resource, e.g. [RDFS::Resource, FOAF::Person]
Note: this method performs a database lookup for { self rdf:type ?o }. For simple type-checking (to know if you are handling an ActiveRDF object, use self.class, which does not do a database query, but simply returns RDFS::Resource.
160 161 162 |
# File 'lib/active_rdf/objectmanager/resource.rb', line 160 def type RDF::Property.new(RDF::type, self) end |