Class: TaliaCore::RdfResource

Inherits:
Object
  • Object
show all
Includes:
ActiveRDF::ResourceLike
Defined in:
lib/talia_core/rdf_resource.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ RdfResource

Initialize a new resource with the given URI



31
32
33
# File 'lib/talia_core/rdf_resource.rb', line 31

def initialize(uri)
  @uri = N::URI.new(uri)
end

Instance Attribute Details

#object_classObject



26
27
28
# File 'lib/talia_core/rdf_resource.rb', line 26

def object_class
  @object_class ||= TaliaCore::Source
end

Class Method Details

.default_typesObject

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



15
16
17
18
19
# File 'lib/talia_core/rdf_resource.rb', line 15

def default_types
  @default_types ||= [
    N::SourceClass.new(N::RDFS.Resource)
  ]
end

Instance Method Details

#[](predicate) ⇒ Object

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



53
54
55
56
57
58
59
# File 'lib/talia_core/rdf_resource.rb', line 53

def [](predicate)
  predicate = N::URI.new(predicate) unless(predicate.kind_of?(N::URI))
  
  property_list = ActiveRDF::Query.new(object_class).distinct(:o).where(self, predicate, :o).execute
  
  PropertyList.new(predicate, property_list, self, source_exists?)
end

#clear_rdfObject

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



41
42
43
# File 'lib/talia_core/rdf_resource.rb', line 41

def clear_rdf
  ActiveRDF::FederationManager.delete_all(self)
end

#direct_predicatesObject

Returns the predicates that are directly defined for this resource



95
96
97
# File 'lib/talia_core/rdf_resource.rb', line 95

def direct_predicates
  ActiveRDF::Query.new(N::Predicate).distinct(:p).where(self, :p, :o).execute
end

#direct_write_predicate(predicate, value) ⇒ Object

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



36
37
38
# File 'lib/talia_core/rdf_resource.rb', line 36

def direct_write_predicate(predicate, value)
  ActiveRDF::FederationManager.add(self, predicate, value)
end

#inverseObject

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/talia_core/rdf_resource.rb', line 71

def inverse
  inverseobj = Object.new
  inverseobj.instance_variable_set(:@obj_uri, self)
  inverseobj.instance_variable_set(:@obj_class, object_class)
  
  class <<inverseobj     
    
    def [](property)
      property = N::URI.new(property) unless(property.kind_of?(N::URI))
      ActiveRDF::Query.new(@obj_class).distinct(:s).where(:s, property, @obj_uri).execute
    end
    private(:type)
  end
  
  return inverseobj
end

#inverse_predicatesObject

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



101
102
103
104
105
# File 'lib/talia_core/rdf_resource.rb', line 101

def inverse_predicates
  qry = ActiveRDF::Query.new.distinct.select(:p)
  qry.where(:s, :p, N::URI.new(uri.to_s))
  qry.execute.collect{ |res| N::Predicate.new(res.uri) }
end

#remove(predicate, value = nil) ⇒ Object

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



47
48
49
# File 'lib/talia_core/rdf_resource.rb', line 47

def remove(predicate, value = nil)
  ActiveRDF::FederationManager.delete(self, predicate, value)
end

#saveObject

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.



110
111
112
113
114
115
116
117
# File 'lib/talia_core/rdf_resource.rb', line 110

def save
  if((ActiveRDF::ConnectionPool.read_adapters.size == 1) &&
        (ActiveRDF::ConnectionPool.write_adapter == ActiveRDF::ConnectionPool.read_adapters.first))
    save_default_types # Only write the "default" types to the store
  else
    full_save # Do the full save operation
  end
end

#typesObject

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



120
121
122
123
124
125
126
127
128
129
# File 'lib/talia_core/rdf_resource.rb', line 120

def types
  types = ActiveRDF::Query.new(N::SourceClass).distinct(:t).where(self,N::RDF::type,:t).execute
  # Add the "default" types if necessary
  self.class.default_types.each do |def_type|
    types << def_type unless(types.include?(def_type))
  end
  
  # Make a property list for the types.
  PropertyList.new(N::RDF::type, types, self, source_exists?)
end

#uriObject

Returns the uri of this resource as a string



90
91
92
# File 'lib/talia_core/rdf_resource.rb', line 90

def uri
  @uri.to_s
end