~kubuntu-members/korundum/4.11

« back to all changes in this revision

Viewing changes to soprano/activerdf-soprano/lib/activerdf_soprano/activerdf_soprano.rb

  • Committer: Ian Monroe
  • Date: 2010-11-21 15:55:01 UTC
  • Revision ID: git-v1:c37670e4e3c59f5eb2ba112f5341a5e706217f6f
Split up Smoke into Qt and KDE directories. 
Move libsmoke stuff into the generator directory
Split up Ruby into qtruby and korundum directories

svn path=/trunk/KDE/kdebindings/ruby/; revision=1199320

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'active_rdf'
2
 
require 'queryengine/query2sparql'
3
 
require 'korundum4'
4
 
 
5
 
# Soprano DBus server adapter
6
 
class SopranoAdapter < ActiveRdfAdapter
7
 
  $activerdflog.info "loading Soprano adapter"
8
 
  ConnectionPool.register_adapter(:soprano, self)
9
 
 
10
 
  attr_reader :engine
11
 
  attr_reader :caching
12
 
 
13
 
  @@soprano_cache = {}
14
 
 
15
 
  def SopranoAdapter.get_cache
16
 
    return @@soprano_cache
17
 
  end
18
 
  
19
 
  # Instantiate the connection with the SPARQL Endpoint.
20
 
  # available parameters:
21
 
  # * :model => name of model to use, defaults to 'main'
22
 
  # * :service => DBus service to use, defaults to 'org.soprano.Server'
23
 
  # * :backend => the name of a backend, such as 'virtuoso'
24
 
  def initialize(params = {})
25
 
    super()  
26
 
    @reads = true
27
 
    @writes = true
28
 
 
29
 
    @model_name = params[:model] || 'main'
30
 
    @caching = params[:caching] || false
31
 
    @backend_name = params[:backend]
32
 
 
33
 
    if @backend_name
34
 
      @backend = Soprano.discoverBackendByName(@backend_name)
35
 
      settings = []
36
 
      if @backend_name =~ /^virtuoso/
37
 
        @host = params[:host] || 'localhost'
38
 
        @port = params[:port] || 1111
39
 
        @username = params[:username] || 'dba'
40
 
        @password = params[:password] || 'dba'
41
 
        settings << Soprano::BackendSetting.new(Soprano::BackendOptionHost, @host)
42
 
        settings << Soprano::BackendSetting.new(Soprano::BackendOptionPort, @port)
43
 
        settings << Soprano::BackendSetting.new(Soprano::BackendOptionUsername, @username)
44
 
        settings << Soprano::BackendSetting.new(Soprano::BackendOptionPassword, @password)
45
 
      end
46
 
      @model = @backend.createModel(settings)
47
 
    else
48
 
      # For accessing the Nepomuk store in KDE, use 'org.kde.NepomukServer'
49
 
      @service = params[:service] || 'org.soprano.Server'
50
 
 
51
 
      @client = Soprano::Client::DBusClient.new(@service)
52
 
      @model = @client.createModel(@model_name)
53
 
    end
54
 
  end
55
 
 
56
 
  def size
57
 
    @model.statementCount
58
 
  end
59
 
 
60
 
  def clear
61
 
    @model.removeStatement(Soprano::Node.new, Soprano::Node.new, Soprano::Node.new, Soprano::Node.new)
62
 
  end
63
 
 
64
 
  # load a file from the given location with the given syntax into the model.
65
 
  def load(location, syntax="n-triples")
66
 
    if @backend_name
67
 
      system("sopranocmd --backend #{@backend_name} --host #{@host} --port #{@port} --username #{@username} --password #{@password} --serialization #{syntax} import #{location}")
68
 
    else
69
 
      system("sopranocmd --dbus #{@service} --serialization #{syntax} --model #{@model_name} import #{location}")
70
 
    end
71
 
  end
72
 
 
73
 
  # query datastore with query string (SPARQL), returns array with query results
74
 
  # may be called with a block
75
 
  def query(query, &block)
76
 
    qs = Query2SPARQL.translate(query)
77
 
 
78
 
    if @caching
79
 
       result = query_cache(qs)
80
 
       if result.nil?
81
 
         $activerdflog.debug "cache miss for query #{qs}"
82
 
       else
83
 
         $activerdflog.debug "cache hit for query #{qs}"
84
 
         return result
85
 
       end
86
 
    end
87
 
 
88
 
    result = execute_soprano_query(qs, query.select_clauses, &block)
89
 
    add_to_cache(qs, result) if @caching
90
 
    result = [] if result == "timeout"
91
 
    return result
92
 
  end
93
 
    
94
 
  # do the real work of executing the sparql query
95
 
  def execute_soprano_query(qs, select_clauses, &block)
96
 
    results = []
97
 
 
98
 
    # querying soprano server
99
 
    binding_set = @model.executeQuery(qs, Soprano::Query::QueryLanguageSparql)
100
 
 
101
 
    binding_set.each do |binding|
102
 
      result = []
103
 
      select_clauses.each do |var|
104
 
        if binding[var]
105
 
          result << soprano_node_to_activerdf(binding[var])
106
 
        end
107
 
      end
108
 
      results << result
109
 
    end
110
 
 
111
 
    if block_given?
112
 
      results.each do |*clauses|
113
 
        yield(*clauses)
114
 
      end
115
 
    else
116
 
      results
117
 
    end
118
 
  end
119
 
 
120
 
  def flush
121
 
    true
122
 
  end  
123
 
 
124
 
  def save
125
 
    true
126
 
  end
127
 
 
128
 
  def close
129
 
    ConnectionPool.remove_data_source(self)
130
 
  end
131
 
  
132
 
  # add triple to datamodel
133
 
  def add(s, p, o, c=nil)
134
 
    $activerdflog.debug "adding triple #{s} #{p} #{o} #{c}"
135
 
 
136
 
    # verify input
137
 
    if s.nil? || p.nil? || o.nil?
138
 
      $activerdflog.debug "cannot add triple with empty subject, exiting"
139
 
      return false
140
 
    end 
141
 
    
142
 
    unless s.respond_to?(:uri) && p.respond_to?(:uri)
143
 
      $activerdflog.debug "cannot add triple where s/p are not resources, exiting"
144
 
      return false
145
 
    end
146
 
  
147
 
    @model.addStatement(Soprano::Statement.new(wrap(s), wrap(p), wrap(o), wrap(c)))
148
 
    save if ConnectionPool.auto_flush?
149
 
  end
150
 
 
151
 
  # deletes triple(s,p,o) from datastore
152
 
  # nil parameters match anything: delete(nil,nil,nil) will delete all triples
153
 
  # ActiveRDF will pass the symbol :all as 'o' when all values of
154
 
  # object for the subject/predicate should be deleted
155
 
  def delete(s, p, o, c=nil)
156
 
    $activerdflog.debug "removing triple(s) #{s} #{p} #{o} #{c}"
157
 
 
158
 
    o = nil if o == :all
159
 
    @model.removeAllStatements(Soprano::Statement.new(wrap(s), wrap(p), wrap(o), wrap(c)))
160
 
  end
161
 
 
162
 
  private
163
 
  def add_to_cache(query_string, result)
164
 
    unless result.nil? or result.empty?
165
 
      if result == "timeout"
166
 
        @@soprano_cache.store(query_string, [])
167
 
      else 
168
 
        $activerdflog.debug "adding to soprano cache - query: #{query_string}"
169
 
        @@soprano_cache.store(query_string, result) 
170
 
      end
171
 
    end
172
 
  end
173
 
  
174
 
  def query_cache(query_string)
175
 
    if @@soprano_cache.include?(query_string)
176
 
      return @@soprano_cache.fetch(query_string)
177
 
    else
178
 
      return nil
179
 
    end
180
 
  end
181
 
 
182
 
  # Converts a Soprano::Node to the ActiveRDF equivalent
183
 
  def soprano_node_to_activerdf(node)
184
 
      case node.type.to_i
185
 
      when Soprano::Node::EmptyNode:
186
 
        "(empty)"
187
 
      when Soprano::Node::ResourceNode:
188
 
        RDFS::Resource.new(node.uri.toString)
189
 
      when Soprano::Node::LiteralNode:
190
 
        node.literal.variant.value
191
 
      when Soprano::Node::BlankNode:
192
 
        nil
193
 
      end
194
 
  end
195
 
 
196
 
  def wrap(node)
197
 
    case node
198
 
    when nil
199
 
      Soprano::Node.new
200
 
    when RDFS::Resource
201
 
      Soprano::Node.new(Qt::Url.new(node.uri))
202
 
    when String
203
 
      if node =~ /^_:(.*)/
204
 
        Soprano::Node.new($1)
205
 
      elsif node =~ /(.*)@(.*)/
206
 
        Soprano::Node.new(Soprano::LiteralValue.new($1), $2)
207
 
      else
208
 
        Soprano::Node.new(Soprano::LiteralValue.new(node))
209
 
      end
210
 
    else
211
 
      Soprano::Node.new(Soprano::LiteralValue.new(node))
212
 
    end
213
 
  end
214
 
 
215
 
end
216
 
 
217
 
# kate: space-indent on; indent-width 2; replace-tabs on; mixed-indent off;