Class TaliaCore::RdfResource
In: lib/talia_core/rdf_resource.rb
Parent: Object

This class encapsulates all functionality to access a specific resource in the RDF store. It is analogous to the RDFS::Resource class in ActiveRDF.

However, it‘s specifically tailored for the use with Talia, and avoids some of the pitfalls of the original class.

Methods

Included Modules

ActiveRDF::ResourceLike

Attributes

object_class  [W]  The class of objects that will be "produced" by this RdfResource

Public Class methods

A list of "default" types that will be added to all resources

[Source]

    # File lib/talia_core/rdf_resource.rb, line 15
15:       def default_types
16:         @default_types ||= [
17:           N::SourceClass.new(N::RDFS.Resource)
18:         ]
19:       end

Initialize a new resource with the given URI

[Source]

    # File lib/talia_core/rdf_resource.rb, line 31
31:     def initialize(uri)
32:       raise ArgumentError, "No blank URI allowed here" if(uri.to_s.blank?)
33:       @uri = N::URI.new(uri)
34:     end

Public Instance methods

Returns the value(s) of the given predicates as a PropertyList filled with the defined object_class objects.

[Source]

    # File lib/talia_core/rdf_resource.rb, line 54
54:     def [](predicate)
55:       predicate = N::URI.new(predicate) unless(predicate.kind_of?(N::URI))
56:       
57:       property_list = ActiveRDF::Query.new(object_class).distinct(:o).where(self, predicate, :o).execute
58:       
59:       PropertyList.new(predicate, property_list, self, source_exists?)
60:     end

Clears all rdf for this resource. FIXME: Not context-aware.

[Source]

    # File lib/talia_core/rdf_resource.rb, line 42
42:     def clear_rdf
43:       ActiveRDF::FederationManager.delete_all(self)
44:     end

Returns the predicates that are directly defined for this resource

[Source]

    # File lib/talia_core/rdf_resource.rb, line 96
96:     def direct_predicates
97:       ActiveRDF::Query.new(N::Predicate).distinct(:p).where(self, :p, :o).execute
98:     end

Direct writing of a predicate, with having to fetch a list first

[Source]

    # File lib/talia_core/rdf_resource.rb, line 37
37:     def direct_write_predicate(predicate, value)
38:       ActiveRDF::FederationManager.add(self, predicate, value)
39:     end

Returns an on-the-fly object that can be used to query for "inverse" properties of this resource (meaning triples that have the current resource as an object.)

The returned object will respond to a [] (array accessor) call which allows the user to specify the predicate to use.

Example: resource.inverse[N::DNCS::title]

The [] method will return a list of objects that are instances of object_class

[Source]

    # File lib/talia_core/rdf_resource.rb, line 72
72:     def inverse
73:       inverseobj = Object.new
74:       inverseobj.instance_variable_set(:@obj_uri, self)
75:       inverseobj.instance_variable_set(:@obj_class, object_class)
76:       
77:       class <<inverseobj     
78:         
79:         def [](property)
80:           property = N::URI.new(property) unless(property.kind_of?(N::URI))
81:           ActiveRDF::Query.new(@obj_class).distinct(:s).where(:s, property, @obj_uri).execute
82:         end
83:         private(:type)
84:       end
85:       
86:       return inverseobj
87:     end

Returns the "inverse" predicates for the resource. these are the predicates for which this resource exists as an object

[Source]

     # File lib/talia_core/rdf_resource.rb, line 102
102:     def inverse_predicates
103:       qry = ActiveRDF::Query.new.distinct.select(:p)
104:       qry.where(:s, :p, N::URI.new(uri.to_s))
105:       qry.execute.collect{ |res| N::Predicate.new(res.uri) }
106:     end

[Source]

    # File lib/talia_core/rdf_resource.rb, line 26
26:     def object_class
27:       @object_class ||= TaliaCore::Source
28:     end

Removes the given predicate (restrict to the triple with the given value if a value is given).

[Source]

    # File lib/talia_core/rdf_resource.rb, line 48
48:     def remove(predicate, value = nil)
49:       ActiveRDF::FederationManager.delete(self, predicate, value)
50:     end

Saves the current resource and it‘s properties to the RDF. (This has been optimized so that if only one RDF backend is present it won‘t do any copying around.

[Source]

     # File lib/talia_core/rdf_resource.rb, line 111
111:     def save
112:       if((ActiveRDF::ConnectionPool.read_adapters.size == 1) &&
113:             (ActiveRDF::ConnectionPool.write_adapter == ActiveRDF::ConnectionPool.read_adapters.first))
114:         save_default_types # Only write the "default" types to the store
115:       else
116:         full_save # Do the full save operation
117:       end
118:     end

Returns the types of this resource as N::SourceClass objects

[Source]

     # File lib/talia_core/rdf_resource.rb, line 121
121:     def types
122:       types = ActiveRDF::Query.new(N::SourceClass).distinct(:t).where(self,N::RDF::type,:t).execute
123:       # Add the "default" types if necessary
124:       self.class.default_types.each do |def_type|
125:         types << def_type unless(types.include?(def_type))
126:       end
127:       
128:       # Make a property list for the types.
129:       PropertyList.new(N::RDF::type, types, self, source_exists?)
130:     end

Returns the uri of this resource as a string

[Source]

    # File lib/talia_core/rdf_resource.rb, line 91
91:     def uri
92:       @uri.to_s
93:     end

[Validate]