activerubic 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ module ActiveRubic
2
+ class Cacher < MysqlCache::Base # :nodoc:
3
+ def version
4
+ MysqlCache::Base.version
5
+ end
6
+ def create
7
+ MysqlCache::Base.create_db_index
8
+ end
9
+ def set_options( options={} )
10
+ if options[ :use_rubic ]
11
+ MysqlCache::Base.use_rubic options[ :use_rubic ]
12
+ end
13
+ end
14
+
15
+ class << self
16
+
17
+ def read( query, options={} )
18
+
19
+ case query
20
+ when :all
21
+
22
+ db_result = @@rails_db.connection.execute("SELECT * FROM #{table}s")
23
+
24
+ results = Array.new
25
+ db_result.each_hash do |h|
26
+ uri = h[ 'uri' ]
27
+ results << RDFS::Resource.new( uri ) if uri
28
+ end
29
+
30
+ when RDFS::label
31
+
32
+ return [ "my label from mysql" ]
33
+
34
+ when :images
35
+
36
+ @@rails_db.connection.execute("SELECT i.full, i.preview, i.thumb, i.original, i.license, i.photographer, i.date, l.fi AS label_fi, l.en AS label_en, d.fi AS description_fi, d.en AS description_en, g.lat, g.lon FROM images AS i LEFT JOIN (labels AS l, descriptions AS d, geopoints AS g) ON (i.label=l.id AND i.description=d.id AND i.geopoint=g.id), (SELECT id, uri FROM rdf_resources) AS rdf_r, (SELECT image_id, rdf_resource_id FROM image_rdf_resource) AS i_rdf_r WHERE rdf_r.uri = 'http://testi/uri3' AND i_rdf_r.image_id = i.id AND i_rdf_r.rdf_resource_id = rdf_r.id;")
37
+
38
+ return [ "my images from mysql" ]
39
+
40
+ when :random_image
41
+
42
+ @@rails_db.connection.execute("SELECT i.full, i.preview, i.thumb, i.original, i.license, i.photographer, i.date, l.fi AS label_fi, l.en AS label_en, d.fi AS description_fi, d.en AS description_en, g.lat, g.lon FROM images AS i LEFT JOIN (labels AS l, descriptions AS d, geopoints AS g) ON (i.label=l.id AND i.description=d.id AND i.geopoint=g.id), (SELECT id, uri FROM rdf_resources) AS rdf_r, (SELECT image_id, rdf_resource_id FROM image_rdf_resource) AS i_rdf_r WHERE rdf_r.uri = 'http://testi/uri3' AND i_rdf_r.image_id = i.id AND i_rdf_r.rdf_resource_id = rdf_r.id ORDER BY RAND() LIMIT 1;")
43
+
44
+ when :properties
45
+
46
+ @@rails_db.connection.execute("p.uri, l.fi AS label_fi, l.en AS label_en FROM property AS p LEFT JOIN (labels AS l) ON (p.label=l.id) WHERE p.uri = 'http://testi/property';");
47
+
48
+ return [ "my properties from mysql" ]
49
+
50
+ when :parents
51
+
52
+ @@rails_db.connection.execute("SELECT sc.uri FROM rdf_resources AS sc, (SELECT id, uri FROM rdf_resources) as rdf_r, (SELECT * FROM rdf_resource_superclass_rdf_resource) AS schelp WHERE rdf_r.uri = 'http://testi/uri2' AND rdf_r.id = schelp.rdf_resource_id AND sc.id = schelp.superclass_rdf_resource_id")
53
+
54
+ when :instances_of
55
+
56
+ @@rails_db.connection.execute("SELECT inst.uri FROM rdf_resources AS inst, (SELECT id, uri FROM rdf_resources) as rdf_r, (SELECT * FROM rdf_resource_instance_rdf_resource) AS insthelp WHERE rdf_r.uri = 'http://testi/uri2' AND rdf_r.id = insthelp.rdf_resource_id AND inst.id = insthelp.instance_rdf_resource_id;")
57
+
58
+ when :range
59
+
60
+ @@rails_db.connection.execute("SELECT rdf_r.uri FROM rdf_resources AS rdf_r, (SELECT id, uri FROM property) as p, (SELECT * FROM rdf_resource_range) AS rangehelp WHERE p.uri = 'http://testi/property' AND rdf_r.id = rangehelp.rdf_resource_id AND p.id = rangehelp.property_id;")
61
+
62
+ when :domain
63
+
64
+ @@rails_db.connection.execute("SELECT rdf_r.uri FROM rdf_resources AS rdf_r, (SELECT id, uri FROM property) as p, (SELECT * FROM rdf_resource_domain) AS domainhelp WHERE p.uri = 'http://testi/property' AND rdf_r.id = domainhelp.rdf_resource_id AND p.id = domainhelp.property_id;")
65
+
66
+ end
67
+
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+
74
+
75
+
76
+
@@ -0,0 +1,226 @@
1
+ # require 'yaml'
2
+ require 'drb/drb'
3
+
4
+ #############################################################################
5
+ # ActiveRubinstein provides connection to Rubinstein, a DRb server with libraries written in native C Ruby, JRuby and Java. The CRuby backend provides access to SPARQL data stores with the ActiveRDF_sparql adapter whereas the JRuby part uses the JRuby for direct exchange of data via the CombinedJenaLuc JAR with Jena and Lucene.
6
+ #
7
+ # Copyright:: Copyright (c) 2007 Savonia University of Applied Sciences
8
+ # Authors:: Mikael Lammentausta, Lauri Miettinen
9
+ #
10
+ # == Rubinstein backend
11
+ # Copyright:: Copyright (c) 2007 Savonia University of Applied Sciences
12
+ # Authors:: Mikael Lammentausta, Pasi Tiirikainen
13
+ #
14
+ # == CombinedJenaLuc JAR
15
+ # Copyright:: Copyright (c) 2007 Savonia University of Applied Sciences
16
+ # Author:: Pasi Tiirikainen
17
+ #
18
+ #############################################################################
19
+ module ActiveRubinstein
20
+ class ActiveRubinsteinError < StandardError #:nodoc:
21
+ end
22
+ class AdapterNotSpecifiedError < ActiveRubinsteinError # :nodoc:
23
+ end
24
+ class AdapterNotFoundError < ActiveRubinsteinError # :nodoc:
25
+ end
26
+ class ConnectionNotEstablishedError < ActiveRubinsteinError #:nodoc:
27
+ end
28
+ class ConnectionFailedError < ActiveRubinsteinError #:nodoc:
29
+ end
30
+ class EmptyServerPoolError < ActiveRubinsteinError #:nodoc:
31
+ end
32
+ class RubinsteinNotFoundError < ActiveRubinsteinError #:nodoc:
33
+ end
34
+
35
+ # The serverpool is initialized here. The actual query methods are in these classes:
36
+ # - ActiveRubinstein::JenaQuery
37
+ # - ActiveRubinstein::LuceneSearcher
38
+ # - ActiveRubinstein::JosekiQuery
39
+ #
40
+ # The SPARQL is created by ActiveRubinstein::SparqlFormulator
41
+ #
42
+ class Base
43
+ # the default language for xml:lang matches
44
+ @@language = 'fi'
45
+
46
+ ### load submodules
47
+ RUBIC_DATASTORES.to_a.each do |engine|
48
+ case engine
49
+ when :mysql
50
+ @@log.rubic " => loading ActiveRecord"
51
+ require 'active_record'
52
+ @@log.rubic " => loading MysqlCache"
53
+ require RUBINSTEIN_HOME + '/lib/mysql_cache'
54
+ require RUBIC_HOME + '/active_rubic/cacher'
55
+
56
+ ### adds an accessor RailsDB when working outside of Rails
57
+ unless defined? RAILS_ROOT
58
+ require RUBINSTEIN_HOME + '/lib/rails_database'
59
+ end
60
+
61
+ when :jena
62
+ require RUBIC_HOME + '/active_rubinstein/sparql_formulator'
63
+ require RUBIC_HOME + '/active_rubinstein/jena_query'
64
+
65
+ when :joseki
66
+ # NOTE!! might be broken, hasn't been tested since 0.70 changes
67
+ require RUBIC_HOME + '/active_rubinstein/sparql_formulator'
68
+ require RUBIC_HOME + '/active_rubinstein/joseki_query'
69
+
70
+ when :lucene
71
+ require RUBIC_HOME + '/active_rubinstein/lucene_searcher'
72
+
73
+ end
74
+ end
75
+
76
+ # creates the server pool and starts the DRb client
77
+ def initialize
78
+ self.shuffle #unless defined? $serverPool
79
+ self.roll
80
+
81
+ unless $serverPool.any? then
82
+ raise EmptyServerPoolError, "No servers in the pool"
83
+
84
+ else # pick a server from the pool
85
+ RUBIC_DATASTORES.to_a.each do |adapter|
86
+ begin
87
+ # instantiate the server for the class
88
+ case adapter
89
+ when :jena
90
+ ActiveRubinstein::JenaQuery.new
91
+
92
+ when :joseki
93
+ ActiveRubinstein::JosekiQuery.new
94
+
95
+ when :lucene
96
+ ActiveRubinstein::LuceneSearcher.new
97
+
98
+ when :geolucene
99
+ ActiveRubinstein::LuceneSearcher.new( :geolucene )
100
+
101
+ else
102
+ raise AdapterNotFoundError, adapter
103
+ end
104
+ rescue
105
+ raise ConnectionNotEstablishedError, $!
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+
112
+ # # # # # # # # # # # # # # # # # # # # # # # # # # # #
113
+
114
+ # starts the DRb client
115
+ def roll
116
+
117
+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
118
+
119
+ # the daemon also includes this class, so do not attemp to start
120
+ # the DRb client then.
121
+ if @@in_daemon_mode then
122
+ # TODO: check first if any of the servers are online and continue
123
+ # pinging for..ever?
124
+ @@log.rubinstein " * starting the DRb client"
125
+ DRb.start_service
126
+ end
127
+ end
128
+
129
+ ### ##### ###### #### #### ###### ###### ##### # #####
130
+
131
+ ### ##### ####### ######### ################# ###############
132
+
133
+ # initializes the server pool
134
+ def shuffle
135
+
136
+ ################ ############################## ###########
137
+
138
+ ####### ##### ## # ######### #### # ####
139
+
140
+ unless RUBIC_DATASTORES.any? then
141
+ raise AdapterNotSpecifiedError, "No adapters specified"
142
+
143
+ else
144
+ $serverPool = {} unless defined? $serverPool
145
+
146
+ RUBIC_DATASTORES.to_a.each do |adapter|
147
+ begin
148
+ case adapter
149
+ when :jena
150
+ server = JenaEndpoint.new
151
+ if server.ping
152
+ $serverPool.update :jena => [ server ]
153
+ @@log.info " => added #{server.inspect} to pool"
154
+ end
155
+
156
+ when :joseki
157
+ server = JosekiEndpoint.new
158
+ if server.ping
159
+ $serverPool.update :joseki => [ server ]
160
+ @@log.info " => added #{server.inspect} to pool"
161
+ end
162
+
163
+ when :geolucene
164
+ server = GeoLuceneEndpoint.new
165
+ if server.ping
166
+ $serverPool.update :geolucene => [ server ]
167
+ @@log.info " => added #{server.inspect} to pool"
168
+ end
169
+ end
170
+ rescue
171
+ raise ConnectionFailedError, $!.message
172
+ end
173
+ end
174
+ end
175
+ end
176
+
177
+
178
+ class << self # Class methods
179
+
180
+ public
181
+
182
+ # propagates the xml:lang preference to all submodules
183
+ def set_language( lang )
184
+ @@language = lang
185
+ ActiveRubinstein::JenaQuery.language = lang
186
+ end
187
+
188
+ private
189
+
190
+ protected
191
+
192
+ def do_log( results ) # :nodoc:
193
+ @@log.boulderdash results.inspect
194
+ results.to_a! unless results.is_a? Array
195
+ if results.size < 3
196
+ @@log.rubinstein " => #{results.size} results: #{results}"
197
+ else
198
+ @@log.rubinstein " => #{results.size} results"
199
+ end
200
+ end
201
+
202
+ end # class << self
203
+
204
+ # gets a greeting message from one or all of the servers in the pool
205
+ #
206
+ # parameters:
207
+ # - 1 server (Symbol) -- by default query all servers in the server pool
208
+ def hello( server=nil )
209
+ begin
210
+ if server.nil? then # try all
211
+ $serverPool.each_pair do |store, url|
212
+ @@log.debug "Store: #{store.inspect}, url: #{url.inspect}"
213
+
214
+ server = url.first
215
+ @@log.rubinstein server.hello
216
+ end
217
+ else
218
+ return server.hello
219
+ end
220
+ rescue
221
+ @@log.error $!
222
+ end
223
+ end
224
+
225
+ end # Base
226
+ end # Module
@@ -0,0 +1,208 @@
1
+ # DEPRECATED methods
2
+ # originally written due to poor understanding of the Ruby language
3
+
4
+ # jena_query #
5
+ # Pass an unadultered query to Rubinstein daemon
6
+ #
7
+ # this may be called from several places,
8
+ # mostly from the models.
9
+ #
10
+ # Parameters:
11
+ # 1 Query
12
+ def jena_query( query, options={}, &block )
13
+ begin
14
+ # @language might be set here
15
+ unless options[ :lang ]
16
+ options.update :lang => @language if @language
17
+ end
18
+
19
+ @@log.rubinstein " => Querying Jena, options: #{options.values.join(' ')}"
20
+ @@log.debug " * Sparql: #{query.to_sp}"
21
+ @@log.debug "@@jena = #{@@jena.inspect}"
22
+ results = @@jena.query( query, options )
23
+ do_log( results )
24
+ return results
25
+ rescue
26
+ @@log.error $!
27
+ return Array.new
28
+ end
29
+ end
30
+ alias :query_from_jena :jena_query
31
+
32
+
33
+ # constructs a common triplet-query
34
+ def find_from_jena( s, p, o )
35
+ begin
36
+ # STDERR.puts "find: #{s}, #{p}, #{o}"
37
+ lang = @language || nil
38
+ # using the lang tag slows the queries down really bad..
39
+ # so we better use it only when necessary
40
+ @@jena.find( s, p, o )
41
+ # @@joseki.find( s, p, o, lang )
42
+ rescue
43
+ Array.new
44
+ end
45
+ end
46
+ alias :find :find_from_jena
47
+
48
+
49
+ ########################################################
50
+ ### Jena graph handling
51
+ ########################################################
52
+
53
+ # Returns the roots of the ontology
54
+ # parameters:
55
+ # 1 subject (OwlThing, RDFS::Resource or URI string)
56
+ def getRoot( ontology=nil )
57
+ begin
58
+ @@log.rubinstein "getRoot of #{ontology}"
59
+ @@jena.getRoot( ontology )
60
+ rescue
61
+ @@log.error $!
62
+ end
63
+ end
64
+
65
+ # Returns the descendants of the subject
66
+ # parameters:
67
+ # 1 subject (RdfAbout, OwlThing, RDFS::Resource or URI string)
68
+ def getDescendants( subject )
69
+ @@log.rubinstein( "getDescendants: #{subject}" )
70
+ begin
71
+ @@jena.getDescendants( subject )
72
+ end
73
+ end
74
+
75
+ # Returns the children of the subject
76
+ # parameters:
77
+ # 1 subject (RdfAbout, OwlClass, RDFS::Resource or URI string)
78
+ def getChildren( subject )
79
+ begin
80
+ @@jena.getChildren( subject, false )
81
+ end
82
+ end
83
+
84
+ # Returns the parents of the subject
85
+ # parameters:
86
+ # 1 subject (RdfAbout, OwlClass, RDFS::Resource or URI string)
87
+ def getParents( subject )
88
+ begin
89
+ @@jena.getParents( subject, false )
90
+ end
91
+ end
92
+
93
+ # Returns the ancestors of the subject
94
+ # parameters:
95
+ # 1 subject (RdfAbout, OwlClass, RDFS::Resource or URI string)
96
+ def getAncestors( subject )
97
+ begin
98
+ x = @@jena.getAncestors( subject )
99
+ # do not return the RDFS::Resource object
100
+ x.delete( RDFS::Resource )
101
+ return x
102
+ end
103
+ end
104
+
105
+ # getInstances #
106
+ # Returns the instances of a specific class
107
+ # Parameters:
108
+ # 1 subject (RdfAbout, OwlClass, RDFS::Resource or URI string)
109
+ # 2 reasoner (boolean)
110
+ def getInstances( subject, reasoner=true )
111
+ @@log.rubinstein " * getInstances of #{subject}, reasoner: #{reasoner}"
112
+ begin
113
+ x = @@jena.getInstances( subject, reasoner )
114
+ @@log.deep " => #{x.size} results: #{x}"
115
+ return x
116
+ end
117
+ end
118
+
119
+ ####################
120
+ # getInstanceClass #
121
+ # gets the immediate RDF::type of an instance
122
+ # Parameters:
123
+ # 1 subject (RdfAbout, OwlClass, RDFS::Resource or URI string)
124
+ def getInstanceClass( subject )
125
+ @@log.rubinstein " * getInstanceClass for #{subject}"
126
+ begin
127
+ results = @@jena.getInstanceClass( subject )
128
+ do_log( results )
129
+ return results
130
+ end
131
+ end
132
+
133
+
134
+ ##############
135
+ # getInverse #
136
+ # returns the inverse of the given property
137
+ # eg. getInverse: KUOPIO::isLocatedIn => KUOPIO::isLocationOf
138
+ # Parameters:
139
+ # 1 Property ( Property, RDFS::Resource or URI string )
140
+ def getInverse( property )
141
+ @@log.rubinstein " * getInverse of #{property}"
142
+ begin
143
+ results = @@jena.getInverse( property )
144
+ do_log( results ) # crashes
145
+ return results
146
+ end
147
+ end
148
+
149
+
150
+ # returns the classes from the semantic circle, around a certain uri, within a certain radius
151
+ # 1 subject (RdfAbout, OwlClass, RDFS::Resource or URI string)
152
+ # 2 radius (Float)
153
+ def getClassesWithin( subject, radius=0.2 )
154
+ begin
155
+ @@jena.getClassesWithin( subject, radius )
156
+ end
157
+ end
158
+
159
+ # returns the full graph path in RDFS::Resource Array
160
+ # 1 subject (RdfAbout, OwlClass, RDFS::Resource or URI string)
161
+ def getFullPath( subject )
162
+ begin
163
+
164
+ # this uses the Java function
165
+ @@jena.getFullPath( subject )
166
+
167
+ # this uses the Ruby function
168
+ # @@jena.getFullPath_( subject )
169
+ end
170
+ end
171
+
172
+
173
+ def getNearbyTargets( lat, long, radius )
174
+ begin
175
+ @@jena.getNearbyTargets( lat, long, radius )
176
+ end
177
+ end
178
+
179
+ ###########################################################
180
+
181
+
182
+
183
+ def joseki_query( query, options={}, &block )
184
+ begin
185
+ @@log.rubinstein " => Querying Joseki, options: #{options}"
186
+ @@log.debug " * Sparql: #{query.to_sp}"
187
+ results = @@joseki.query( query )
188
+ do_log( results )
189
+ return results
190
+ rescue
191
+ @@log.error $!
192
+ return Array.new
193
+ end
194
+ end
195
+ alias :query_from_joseki :joseki_query
196
+
197
+ def find_from_joseki( s, p, o )
198
+ begin
199
+ # STDERR.puts "find: #{s}, #{p}, #{o}"
200
+ lang = @language || nil
201
+ # using the lang tag slows the queries down really bad..
202
+ # so we better use it only when necessary
203
+ @@joseki.find( s, p, o )
204
+ # @@joseki.find( s, p, o, lang )
205
+ rescue
206
+ Array.new
207
+ end
208
+ end