activerubic 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,272 @@
1
+ # Schema model for a resource of type MEDIA::image.
2
+ #
3
+ # This model serves to be a simpler form for custom data to be addressed without using the RDF Schema (RDFS) definition. This is fairly hackish and is not intended to be reviewed. This model is required to be here, however, because ActiveRubic needs to have access to this model, as it is passing around objects of this type through the DRb.
4
+ #
5
+ # A different model for images is useful in Rails environment where different multimedia types should be built into various containers, even though the internal accessors are translated from the RDF Schema. Currently the schema is hard-coded, as stated above. This should be fairly easy to fix...
6
+ class Image < RDFS::Resource
7
+
8
+ # adding accessor to the class uri:
9
+ # the uri of the rdf resource being represented by this class
10
+ class << self
11
+ attr_accessor :class_uri
12
+ end
13
+
14
+ # uri of the resource (for instances of this class: rdf resources)
15
+ attr_reader :uri
16
+
17
+ # Creates a new Image.
18
+ #
19
+ # parameters:
20
+ # 1 resource ( RDFS::Resource or URI )
21
+ # 2 options ( Hash; )
22
+
23
+ # parameters:
24
+ # 1 resource ( RDFS::Resource or URI String )
25
+ def initialize( resource, options={} )
26
+ @@log.deep "Formulating new Image #{resource} from #{resource.class}"
27
+ @uri = case resource
28
+ when MEDIA::image
29
+ @@log.warn "Redundant new OWL::Class, resource already was one."
30
+ resource.uri
31
+ when RDFS::Resource
32
+ resource.uri
33
+ when String
34
+ resource
35
+ else
36
+ raise ActiveRdfError, "Resource #{resource} (#{resource.class}) is improperly formulated"
37
+ return nil
38
+ end
39
+
40
+ # @data_stores = options[ :from ] || RUBIC_DATASTORES
41
+ end
42
+
43
+ self.class_uri = Namespace.lookup(:media, :image)
44
+
45
+ def self.uri; class_uri.uri; end
46
+ def self.==(other)
47
+ other.respond_to?(:uri) ? other.uri == self.uri : false
48
+ end
49
+
50
+ ##### ######
51
+ ##### start of instance-level code
52
+ ##### ######
53
+
54
+ attr_accessor :marker_icon
55
+
56
+ # muuNimi
57
+ def aliases
58
+ begin
59
+ s = self.label.dup
60
+ s.shift # remove the first label
61
+ unless s.nil?
62
+ return s.join(', ')
63
+ else
64
+ return String.new
65
+ end
66
+ rescue
67
+ @@log.error $!
68
+ return String.new
69
+ end
70
+ end
71
+ alias :alias :aliases
72
+
73
+ def location
74
+ begin
75
+ if @location.nil? then
76
+ @location = my( DCTERMS::spatial )
77
+ end
78
+ return @location
79
+ rescue
80
+ return Array.new
81
+ end
82
+ end
83
+
84
+ # the target instance
85
+ def about
86
+ begin
87
+ if @about.nil? then
88
+ @about = my( RDF::subject ).uniq
89
+ end
90
+ return @about
91
+ rescue
92
+ return Array.new
93
+ end
94
+ end
95
+ alias :subject :about
96
+ alias :subjects :about
97
+
98
+ def original
99
+ begin
100
+ if @original.nil? then
101
+ @original = my( IMAGE::original ).first.uri
102
+ end
103
+ return @original
104
+ rescue
105
+ @@log.warn "Original not found for #{self.uri}"
106
+ return '/images/not_found/full.jpg'
107
+ end
108
+ end
109
+
110
+ # the 'full' web url
111
+ def url
112
+ begin
113
+ if @url.nil? then
114
+ @url = my( IMAGE::url ).first.uri
115
+ end
116
+ return @url
117
+ rescue
118
+ @@log.warn "Full web size image not found for #{self.uri}"
119
+ return '/images/not_found/full.jpg'
120
+ end
121
+ end
122
+ alias :large :url
123
+ alias :full :url
124
+
125
+ def preview
126
+ begin
127
+ if @preview.nil? then
128
+ @preview = my( IMAGE::preview ).first.uri
129
+ end
130
+ if (( @preview.empty? ) || ( @preview.nil? ))
131
+ # hack for psm_images. todo: think over how to solve missing resolutions
132
+ return self.uri
133
+ else
134
+ return @preview
135
+ end
136
+ rescue
137
+ @@log.warn "Preview not found for #{self.uri}"
138
+ return '/images/not_found/preview.jpg'
139
+ end
140
+ end
141
+ alias :medium :preview
142
+
143
+ #TODO: rescale to 350x262
144
+ def small
145
+ begin
146
+ if @small.nil? then
147
+ @small = my( IMAGE::small ).first.uri
148
+ end
149
+ if (( @small.empty? ) || ( @small.nil? ))
150
+ return self.thumb
151
+ else
152
+ return @small
153
+ end
154
+ rescue
155
+ @@log.warn "Small image not found for #{self.uri}"
156
+ return '/images/not_found/small.jpg'
157
+ end
158
+ end
159
+
160
+ def thumb
161
+ begin
162
+ if @thumb.nil? then
163
+ @thumb = my( IMAGE::thumbnail ).first.uri
164
+ end
165
+ return @thumb
166
+ rescue
167
+ @@log.warn "Thumbnail not found for #{self.uri}"
168
+ return '/images/not_found/thumb.jpg'
169
+ end
170
+ end
171
+ alias :thumbnail :thumb
172
+
173
+ def date
174
+ begin
175
+ if @date.nil? then
176
+ @date = my( EXIF::dateTime ).first
177
+ end
178
+ return @date
179
+ rescue
180
+ return nil
181
+ end
182
+ end
183
+
184
+ # for timeranges
185
+ def timerange_start
186
+ begin
187
+ if @timerange_start.nil? then
188
+ @timerange_start = my( BT::picture_timerange_start ).first
189
+ end
190
+ return @timerange_start
191
+ rescue
192
+ return nil
193
+ end
194
+ end
195
+ def timerange_end
196
+ begin
197
+ if @timerange_end.nil? then
198
+ @timerange_end = my( BT::picture_timerange_end ).first
199
+ end
200
+ return @timerange_end
201
+ rescue
202
+ return nil
203
+ end
204
+ end
205
+
206
+ def copyright
207
+ begin
208
+ if @c.nil? then
209
+ @c = my( IPTC::CopyrightNotice ).first
210
+ end
211
+ return @c
212
+ rescue
213
+ return nil
214
+ end
215
+ end
216
+
217
+ def rating
218
+ begin
219
+ if @rating.nil? then
220
+ @rating = my( IPTC::Rating ).first
221
+ end
222
+ return @rating
223
+ rescue
224
+ return nil
225
+ end
226
+ end
227
+
228
+ def model
229
+ begin
230
+ if @m.nil? then
231
+ @m = my( EXIF::Model ).first
232
+ end
233
+ return @m
234
+ rescue
235
+ return nil
236
+ end
237
+ end
238
+ alias :camera_model :model
239
+
240
+ def credit
241
+ begin
242
+ if @credit.nil? then
243
+ @credit = my( DC::creator ).first
244
+ end
245
+ return @credit
246
+ rescue
247
+ return nil
248
+ end
249
+ end
250
+ alias :photographer :credit
251
+ alias :creator :credit
252
+
253
+ # returns the keywords of the subject
254
+ def keywords
255
+ begin
256
+ # the class variable is set if this query has been executed already
257
+ if @keywords.nil? then
258
+ @keywords = Array.new
259
+ @keywords << my( IPTC::Keywords )
260
+ @keywords << my( DC::keywords )
261
+ @keywords << my( DC::subject )
262
+ @keywords.flatten!.uniq!
263
+ end
264
+ return @keywords
265
+ rescue
266
+ return nil
267
+ end
268
+ end
269
+
270
+
271
+
272
+ end
@@ -0,0 +1,287 @@
1
+ require 'active_rdf'
2
+ require 'objectmanager/object_manager'
3
+ # the modified namespace is loaded before registering the namespaces
4
+ # require 'objectmanager/namespace'
5
+ require 'queryengine/query'
6
+
7
+ module RDFS
8
+ # Represents an RDF resource and manages manipulations of that resource.
9
+ # Extends the ActiveRDF object by introducing a layer of data handling methods for RDFS resources.
10
+ #
11
+ # Other objects that currently extend this class:
12
+ # - OWL::ObjectProperty
13
+ #
14
+ # See also:: ActiveRubic, OWL::Class and OWL::Thing.
15
+ class RDFS::Resource
16
+
17
+ # adding accessor to the class uri:
18
+ # the uri of the rdf resource being represented by this class
19
+ class << self
20
+ attr_accessor :class_uri
21
+ end
22
+
23
+ # uri of the resource (for instances of this class: rdf resources)
24
+ attr_reader :uri
25
+
26
+ # creates new resource representing an RDF resource
27
+ #
28
+ # parameters:
29
+ # - 1 resource ( RDFS::Resource or URI String )
30
+ def initialize( resource, options={} )
31
+ @@log.deep "Formulating new RDFS::Resource #{resource} from #{resource.class}"
32
+ @uri = case resource
33
+ when RDFS::Resource then
34
+ @@log.warn "Redundant new RDFS::Resource, resource already was one."
35
+ resource.uri
36
+ when String then
37
+ resource
38
+ else
39
+ raise ActiveRdfError, "Resource #{resource} (#{resource.class}) is improperly formulated"
40
+ return nil
41
+ end
42
+ end
43
+
44
+ # setting our own class uri to rdfs:resource
45
+ # (has to be done after defining our RDFS::Resource.new
46
+ # because it cannot be found in Namespace.lookup otherwise)
47
+ self.class_uri = Namespace.lookup(:rdfs, :Resource)
48
+
49
+ def self.uri; class_uri.uri; end
50
+ def self.==(other)
51
+ other.respond_to?(:uri) ? other.uri == self.uri : false
52
+ end
53
+
54
+ ##### ######
55
+ ##### start of instance-level code
56
+ ##### ######
57
+
58
+ # Returns the Namespace localname of the resource.
59
+ def localname
60
+ Namespace.localname( self.uri )
61
+ end
62
+ alias :uri_id :localname
63
+
64
+ # Strips protocol from the URI.
65
+ def stripped_uri
66
+ return self.uri.gsub( /.*:\/\//, '' )
67
+ end
68
+
69
+ # HACK for the Image class. See the comments over there to find out why this method uses hard-coded values (FIXME).
70
+ def is_image?
71
+ if self.types.include? MEDIA::image
72
+ return true
73
+ else
74
+ return false
75
+ end
76
+ end
77
+
78
+ # returns the resource ancestors in the OWL graph
79
+ def ancestors
80
+ begin
81
+ # the class variable is set if this query has been executed already
82
+ if @ancestors.nil? then
83
+ @ancestors = ActiveRubic::Base.find( :ancestors, :subject => self )
84
+ end
85
+ return @ancestors
86
+ rescue
87
+ return Array.new
88
+ end
89
+ end
90
+
91
+ # returns the resource parents in the OWL graph
92
+ def parents
93
+ begin
94
+ if @parents.nil?
95
+ @parents = ActiveRubic::Base.find( :parents, :subject => self )
96
+ end
97
+ return @parents
98
+ rescue
99
+ return Array.new
100
+ end
101
+ end
102
+
103
+ # children and descendants are only defined for OWL::Class
104
+
105
+ # get all object properties
106
+ def properties
107
+ @@log.debug "Looking up properties for #{self.uri}"
108
+ begin
109
+ # the class variable is set if this query has been executed already
110
+ if @properties.nil? then
111
+ @properties = ActiveRubic::Base.find( :properties, :subject => self )
112
+ end
113
+ return @properties
114
+ rescue
115
+ return Array.new
116
+ end
117
+ end
118
+
119
+ # Checks if the resource has latitude and longitude, in which case returns self in an Array.
120
+ #
121
+ # Perhaps this should return GMarkers or a GeoRuby object.
122
+ def geopoints( options={} )
123
+ # limit = options[ :limit ]
124
+ @@log.debug "Looking up marker for #{self.uri}"
125
+ if ( self.has_markers? )
126
+ return [ self ]
127
+ else
128
+ return Array.new
129
+ end
130
+ end
131
+ alias :markers :geopoints
132
+
133
+ # returns a single geopoint
134
+ def geopoint
135
+ return self.geopoints.first
136
+ end
137
+ alias :marker :geopoint
138
+ alias :wgs84 :geopoint
139
+ alias :location :geopoint
140
+
141
+ def gmarker # :nodoc:
142
+ end
143
+
144
+ # Returns true if the object has lat and long, otherwise false.
145
+ def is_mappable?
146
+ if self.lat and self.long
147
+ return true
148
+ else
149
+ return false
150
+ end
151
+ end
152
+ alias :has_markers? :is_mappable? # :nodoc:
153
+ alias :has_coordinates? :is_mappable? # :nodoc:
154
+
155
+ # Returns (the first) GEO::lat as Float.
156
+ # uses the private method get_lat_and_long.
157
+ def latitude
158
+ # STDERR.puts "get lat"
159
+ begin
160
+ if @lat.nil? then
161
+ get_lat_and_long
162
+ end
163
+ # @@log.debug "lat: #{@lat.first}"
164
+ return @lat.first if @lat.any?
165
+ rescue
166
+ return nil
167
+ end
168
+ end
169
+ alias :lat :latitude
170
+
171
+ # Returns (the first) GEO::long as Float.
172
+ # uses the private method get_lat_and_long.
173
+ def longitude
174
+ begin
175
+ if @long.nil? then
176
+ get_lat_and_long
177
+ end
178
+ return @long.first if @long.any?
179
+ rescue
180
+ return nil
181
+ end
182
+ end
183
+ alias :long :longitude
184
+
185
+ private
186
+ # submethod which collects the lat and long.
187
+ def get_lat_and_long
188
+ begin
189
+ @lat = my( GEO::lat )
190
+ @long = my( GEO::long )
191
+
192
+ # convert to Float
193
+ # NOTE: nil.to_f will yield 0.0
194
+ @lat.dup.each_with_index do |lat,index|
195
+ @lat[ index ] == lat.to_f unless lat.nil?
196
+ end
197
+
198
+ @long.dup.each_with_index do |long,index|
199
+ @long[ index ] == long.to_f unless long.nil?
200
+ end
201
+
202
+ self.lat = @lat
203
+ self.long = @long
204
+
205
+ rescue
206
+ end
207
+ end
208
+
209
+ public
210
+
211
+ # returns the resources in the range of a property.
212
+ #
213
+ # parameters:
214
+ # - 1 property (OWL::ObjectProperty)
215
+ def range( property )
216
+ begin
217
+ results = ActiveRubic::Base.find( :within_range, :subject => self, :property => property )
218
+ return results
219
+ rescue
220
+ Array.new
221
+ end
222
+ end
223
+
224
+ # Returns resource RDF::type(s).
225
+ def types
226
+ begin
227
+ if @types.nil? then
228
+ @types = my( RDF::type )
229
+ end
230
+ return @types
231
+ rescue
232
+ return Array.new
233
+ end
234
+ end
235
+ alias :type :types
236
+
237
+ # returns true if the resource types include OWL::Class, false otherwise
238
+ def is_class?
239
+ begin
240
+ if @is_class.nil? then
241
+ @@log.rubic "Detecting whether #{self} is a class or an instance.."
242
+
243
+ if ( self.types.include? OWL::Class ) then
244
+ @is_class = true
245
+ else
246
+ @is_class = false
247
+ end
248
+ end
249
+ return @is_class
250
+ rescue
251
+ @@log.error $!
252
+ end
253
+ end
254
+
255
+ # returns true if the resource types include OWL::ObjectProperty, false otherwise
256
+ def is_property?
257
+ begin
258
+ if @is_property.nil? then
259
+ @@log.rubic "Detecting whether #{self} is a property.."
260
+ if ( self.types.include? OWL::ObjectProperty ) then
261
+ @is_property = true
262
+ else
263
+ @is_property = false
264
+ end
265
+ end
266
+ return @is_property
267
+ rescue
268
+ @@log.error $!
269
+ end
270
+ end
271
+
272
+ # Returns the results wrapped in RDFS::Resource model -- this is OK
273
+ # when working with OWL-DL, where properties can only be assigned to
274
+ # resources (AFAIK) whereas in OWL-FULL the results could be also
275
+ # classes and other properties .. this makes less queries so is faster.
276
+ def resources_for_property( p )
277
+ begin
278
+ return my( p.uri )
279
+ rescue
280
+ @@log.error $!
281
+ end
282
+ end
283
+ alias :values_for_property :resources_for_property
284
+ alias :predicate :resources_for_property
285
+
286
+ end # class
287
+ end # module