Module: TaliaCore::ActiveSourceParts::RdfHandler

Included in:
TaliaCore::ActiveSource
Defined in:
lib/talia_core/active_source_parts/rdf_handler.rb

Overview

Methods for the ActiveSource class to automatically create the RDF triples for a source and to access the RDF data of the source.

The RDF for a source will be automatically created through the auto_create_rdf callback when the source is set (and autosave_rdf is set)

Instance Method Summary collapse

Instance Method Details

#autosave_rdf=(value) ⇒ Object

This can be used to turn of automatic rdf creation. If set to true, create_rdf will not be called automatically after saving the source.

Attention: Improper use will compromise the integrity of the RDF data. However, it may be used in order to speed up operations that save a record several times and don’t need the RDF data in the meantime.



24
25
26
# File 'lib/talia_core/active_source_parts/rdf_handler.rb', line 24

def autosave_rdf=(value)
  @autosave_rdf = value
end

#autosave_rdf?Boolean

Returns the value of the autosave_rdf flag, as set by autosave_rdf=

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/talia_core/active_source_parts/rdf_handler.rb', line 12

def autosave_rdf?
  @autosave_rdf = true unless(defined?(@autosave_rdf))
  @autosave_rdf
end

#create_rdf(force = :false) ⇒ Object

This creates the RDF subgraph for this Source and saves it to disk. This may be an expensive operation since it removes the existing elements. (Could be optimised ;-)

Unless the force option is specified, this will ignore predicates that remain unchanged. This means that writing will be faster if a predicate will not changed, but if database objects were not added through the standard API they’ll be missed

The force option may have three values:

false

Normal operation. This retrieves the data for each of the cached SemanticCollectionWrappers that may have been modified and rewrites the respective attribute in the RDF store. (see the PredicateHandler module for an explanation of the wrapper cache). It will ignore wrappers that are obviously “clean”, but will do a “retrieve and write” for each wrapper separately.

force

Force a complete rewrite of the RDF data for this source. This will erase the RDF and write all triples for this source in one go. It will also remove any triples for this source that may have been added externally.

create

Do not check for any existing data, just write out the data that is in the cache. This is fast, but must only be used for new sources where it is certain that no data for the source exists in the RDF store.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/talia_core/active_source_parts/rdf_handler.rb', line 64

def create_rdf(force = :false)
  self.class.benchmark("\033[32m\033[4m\033[1mActiveSource::RD\033[0m Creating RDF for source", Logger::DEBUG, false) do
    assit(!new_record?, "Record must exist here: #{self.uri}")
    # Get the stuff to write. This will also erase the old data
    
    s_rels = case force 
      when :force 
        prepare_all_predicates_to_write 
      when :create
        prepare_predicates_to_create
      else
        prepare_predicates_to_write
      end
    s_rels.each do |sem_ref|
      # We pass the object on. If it's a SemanticProperty, we need to add
      # the value. If not the RDF handler will detect the #uri method and
      # will add it as Resource.
      obj = sem_ref.object
      assit(obj, "Must have an object here. #{sem_ref.inspect}")
      value = obj.is_a?(SemanticProperty) ? obj.value : obj
      my_rdf.direct_write_predicate(N::URI.new(sem_ref.predicate_uri), value)
    end
    my_rdf.direct_write_predicate(N::RDF.type, rdf_selftype)
    my_rdf.save
  end
end

#my_rdfObject

Returns the RDF object to use for this ActiveSource. This will return a RdfResource, which has a similiar (but more limited API) than the ActiveSource itself. All operations and queries on that resource will go to the RDF store instead of the database



33
34
35
36
37
38
39
# File 'lib/talia_core/active_source_parts/rdf_handler.rb', line 33

def my_rdf
  @rdf_resource ||= begin
    src = RdfResource.new(uri)
    src.object_class = TaliaCore::ActiveSource
    src
  end
end

#to_rdfObject

Creates an RDF/XML resprentation of the source. See the Xml::RdfBuilder and the Xml::SourceReader for more information.



93
94
95
96
97
98
99
100
101
# File 'lib/talia_core/active_source_parts/rdf_handler.rb', line 93

def to_rdf
  rdf = String.new

  ActiveSourceParts::Xml::RdfBuilder.open(:target => rdf, :indent => 2) do |builder|
    builder.write_source(self)
  end

  rdf
end