activerdf_net7 1.6.16 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. data/CHANGELOG +63 -0
  2. data/activerdf-jena/lib/activerdf_jena/jena.rb +4 -4
  3. data/activerdf-jena/lib/activerdf_jena/jena_adapter.rb +55 -55
  4. data/activerdf-jena/lib/activerdf_jena/lucene.rb +1 -1
  5. data/activerdf-jena/lib/activerdf_jena/ng4j.rb +7 -7
  6. data/activerdf-jena/lib/activerdf_jena/ng4j_adapter.rb +47 -47
  7. data/activerdf-jena/lib/activerdf_jena/pellet.rb +1 -1
  8. data/activerdf-jena/test/test_jena_adapter.rb +121 -120
  9. data/activerdf-jena/test/test_ng4j_adapter.rb +111 -110
  10. data/activerdf-rdflite/lib/activerdf_rdflite/fetching.rb +23 -19
  11. data/activerdf-rdflite/lib/activerdf_rdflite/rdflite.rb +153 -277
  12. data/activerdf-rdflite/lib/activerdf_rdflite/suggesting.rb +2 -2
  13. data/activerdf-rdflite/test/test_fetching.rb +7 -22
  14. data/activerdf-rdflite/test/test_rdflite.rb +44 -257
  15. data/activerdf-redland/lib/activerdf_redland/redland.rb +246 -282
  16. data/activerdf-redland/test/test_redland_adapter.rb +62 -224
  17. data/activerdf-sesame/ext/wrapper-sesame2.jar +0 -0
  18. data/activerdf-sesame/java/build.number +2 -2
  19. data/activerdf-sesame/java/build.xml +0 -0
  20. data/activerdf-sesame/java/lib/junit-3.8.2.jar +0 -0
  21. data/activerdf-sesame/java/settings.xml +0 -0
  22. data/activerdf-sesame/java/src/org/activerdf/wrapper/sesame2/WrapperForSesame2.java +0 -0
  23. data/activerdf-sesame/java/temp/build/org/activerdf/wrapper/sesame2/WrapperForSesame2.class +0 -0
  24. data/activerdf-sesame/java/temp/manifest/MANIFEST.MF +2 -2
  25. data/activerdf-sesame/java/test-src/org/activerdf/wrapper/sesame2/TestWrapperForSesame2.java +0 -0
  26. data/activerdf-sesame/lib/activerdf_sesame/sesame.rb +360 -364
  27. data/activerdf-sesame/test/test_sesame_adapter.rb +85 -83
  28. data/activerdf-sparql/lib/activerdf_sparql/sparql.rb +147 -148
  29. data/activerdf-sparql/lib/activerdf_sparql/sparql_result_parser.rb +5 -5
  30. data/activerdf-sparql/test/test_sparql_adapter.rb +2 -0
  31. data/activerdf-yars/lib/activerdf_yars/jars2.rb +85 -83
  32. data/lib/active_rdf/federation/active_rdf_adapter.rb +26 -39
  33. data/lib/active_rdf/federation/connection_pool.rb +119 -110
  34. data/lib/active_rdf/federation/federation_manager.rb +51 -51
  35. data/lib/active_rdf/objectmanager/bnode.rb +8 -2
  36. data/lib/active_rdf/objectmanager/literal.rb +81 -50
  37. data/lib/active_rdf/objectmanager/namespace.rb +117 -84
  38. data/lib/active_rdf/objectmanager/object_manager.rb +101 -96
  39. data/lib/active_rdf/objectmanager/ordered_set.rb +1 -1
  40. data/lib/active_rdf/objectmanager/property.rb +345 -0
  41. data/lib/active_rdf/objectmanager/property_list.rb +4 -4
  42. data/lib/active_rdf/objectmanager/property_lookup.rb +57 -0
  43. data/lib/active_rdf/objectmanager/resource.rb +293 -501
  44. data/lib/active_rdf/objectmanager/resource_like.rb +2 -2
  45. data/lib/active_rdf/objectmanager/resource_query.rb +85 -0
  46. data/lib/active_rdf/queryengine/ntriples_parser.rb +75 -68
  47. data/lib/active_rdf/queryengine/query.rb +237 -183
  48. data/lib/active_rdf/queryengine/query2jars2.rb +17 -15
  49. data/lib/active_rdf/queryengine/query2sparql.rb +107 -101
  50. data/lib/active_rdf.rb +28 -17
  51. data/lib/active_rdf_helpers.rb +37 -5
  52. data/lib/active_rdf_log.rb +11 -11
  53. data/test/adapters/test_activerdf_adapter.rb +138 -0
  54. data/test/{test_adapters.rb → adapters/test_adapters.rb} +6 -24
  55. data/test/adapters/test_bnode_capable_adapter.rb +31 -0
  56. data/test/adapters/test_context_aware_adapter.rb +31 -0
  57. data/test/adapters/test_network_aware_adapter.rb +29 -0
  58. data/test/adapters/test_persistent_adapter.rb +21 -0
  59. data/test/adapters/test_read_only_adapter.rb +15 -0
  60. data/test/adapters/test_reasoning_adapter.rb +11 -0
  61. data/test/adapters/test_writable_adapter.rb +163 -0
  62. data/test/common.rb +78 -96
  63. data/test/federation/test_connection_pool.rb +25 -44
  64. data/test/federation/test_federation_manager.rb +45 -45
  65. data/test/objectmanager/test_literal.rb +47 -26
  66. data/test/objectmanager/test_namespace.rb +3 -1
  67. data/test/objectmanager/test_object_manager.rb +35 -45
  68. data/test/objectmanager/test_ordered_set.rb +1 -1
  69. data/test/objectmanager/test_property.rb +261 -0
  70. data/test/objectmanager/test_resource_reading.rb +196 -104
  71. data/test/objectmanager/test_resource_reasoning.rb +26 -0
  72. data/test/objectmanager/test_resource_writing.rb +34 -25
  73. data/test/queryengine/my_external_resource.rb +5 -1
  74. data/test/queryengine/test_external_resource_class.rb +1 -8
  75. data/test/queryengine/test_ntriples_parser.rb +5 -3
  76. data/test/queryengine/test_query.rb +3 -3
  77. data/test/queryengine/test_query2jars2.rb +2 -2
  78. data/test/queryengine/test_query2sparql.rb +2 -2
  79. data/test/queryengine/test_query_engine.rb +46 -28
  80. metadata +16 -8
  81. data/activerdf-rdflite/test/test_bnode_data.nt +0 -5
  82. data/activerdf-rdflite/test/test_data.nt +0 -32
  83. data/activerdf-rdflite/test/test_escaped_data.nt +0 -2
  84. data/activerdf-redland/test/test_person_data.nt +0 -42
  85. data/test/objectmanager/test_talia_syntax.rb +0 -68
@@ -0,0 +1,345 @@
1
+ require 'digest'
2
+ require 'set'
3
+
4
+ module RDF
5
+ # Represents an RDF property.
6
+ # If an optional subject is provided on instatiation, provides access to values of this property belonging to the
7
+ # associated subject. Property objects are +Enumerable+. Values are returned as copies with no order garunteed
8
+ # and may be accessed individually by key.
9
+ #
10
+ # == Usage
11
+ # age = RDF::Property.new('http://activerdf.org/test/age')
12
+ # age.domain => [#<RDFS::Resource @uri="http://activerdf.org/test/Person">]
13
+ # age.range => [#<RDFS::Resource @uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#Literal">]
14
+ # age.type => [#<RDFS::Resource @uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#Property">]
15
+ # age.to_a => ActiveRdfError: http://activerdf.org/test/age: no associated subject
16
+ #
17
+ # email = RDF::Property.new('http://activerdf.org/test/email', 'http://activerdf.org/test/eyal')
18
+ # email.replace("eyal@cs.cu.nl") # replace any existing values
19
+ # email.add("eyal.oren@deri.com") # add new value to this property
20
+ # email += ("eyal.oren@deri.net") # alternative way to add new value
21
+ # email.clear # delete any existing values
22
+ # email.add(["eyal@cs.cu.nl","eyal.oren@deri.com"]) # add array containing values
23
+ # email["eyal.oren@deri.com"] = "eyal.oren@deri.net" # replace existing value
24
+ # email[p.index("eyal.oren@deri.net")] = "eyal.oren@deri.org" # replace existing value by key
25
+ # email.include?("eyal.oren@deri.org") => true # check for existing value
26
+ # email == ["eyal.oren@deri.org","eyal@cs.cu.nl"] => true # compare value(s) to array (order is ignored)
27
+ # email.delete("eyal@cs.cu.nl") # delete specific value
28
+ # email == "eyal.oren@deri.org" => true # compare value(s) to single value
29
+ # email.collect!{|val| val.gsub(/@/,' at ').gsub(/\./,' dot ')} # update value(s) with result of block
30
+ class Property < RDFS::Resource
31
+ attr_reader :subject
32
+
33
+ def initialize(property, subject = nil)
34
+ super(property)
35
+ @subject = subject
36
+ @lang = nil
37
+ @exact_lang = true
38
+ @datatype = nil
39
+ @context = nil
40
+
41
+ if @subject
42
+ class<<self
43
+ include AssociatedProperty
44
+ end
45
+ end
46
+ end
47
+
48
+ self.class_uri = ActiveRDF::Namespace.lookup(:rdf, :Property)
49
+
50
+ def initialize_copy(property)
51
+ if @subject
52
+ class<<self
53
+ include AssociatedProperty
54
+ end
55
+ end
56
+ end
57
+
58
+ # Returns the property object for this property without @subject set
59
+ def property
60
+ RDF::Property.new(self)
61
+ end
62
+ end
63
+
64
+ # Provides methods for accessing property values when @subject is set
65
+ module AssociatedProperty
66
+ include Enumerable
67
+ # Value reference. Retrieves a copy of the value by the key or value. Returns nil if not found.
68
+ def [](md5_or_value)
69
+ unless md5_or_value.nil?
70
+ arr = to_a
71
+ arr.find{|value| value == md5_or_value} || arr.find{|value| get_key(value) == md5_or_value}
72
+ end
73
+ end
74
+ alias :at :[]
75
+
76
+ # Selective value replacement. Replaces the value given by key or value. Raises IndexError if value not found.
77
+ def []=(md5_or_value,new_value)
78
+ value = self[md5_or_value]
79
+ raise IndexError, "Couldn't find existing value to replace: #{md5_or_value}" unless value
80
+ ActiveRDF::FederationManager.delete(@subject, self.property, value)
81
+ add(new_value)
82
+ end
83
+
84
+ # Returns a new array with the object appended, or the objects values if obj.respond_to? :to_ary
85
+ def +(obj)
86
+ to_a + [*obj]
87
+ end
88
+
89
+ # Returns a new array with the object removed
90
+ def -(obj)
91
+ to_a - [*obj]
92
+ end
93
+
94
+ def ==(other)
95
+ if other.respond_to?(:to_ary)
96
+ Set.new(other) == Set.new(to_a)
97
+ else
98
+ arr = to_a
99
+ if arr.size == 1
100
+ other == arr[0] || super
101
+ else
102
+ super
103
+ end
104
+ end
105
+ end
106
+ alias :eql? :==
107
+
108
+ # Append. Adds the given object(s) to the values for this property belonging to @subject
109
+ # This expression returns the property itself, so several appends may be chained together.
110
+ def add(*args)
111
+ args.each do |arg|
112
+ if arg.respond_to?(:to_ary)
113
+ arg.to_ary.each {|item| ActiveRDF::FederationManager.add(@subject, self.property, item)}
114
+ else
115
+ ActiveRDF::FederationManager.add(@subject, self.property, arg)
116
+ end
117
+ end
118
+ self
119
+ end
120
+
121
+ # Removes all values
122
+ def clear
123
+ ActiveRDF::FederationManager.delete(@subject, self.property)
124
+ self
125
+ end
126
+
127
+ # Invokes the block once for each value, replacing the value with the value returned by block
128
+ def collect!(&block)
129
+ to_a.each do |item|
130
+ new_item = yield(item)
131
+ delete(item)
132
+ add(new_item)
133
+ end
134
+ self
135
+ end
136
+ alias :map! :collect!
137
+
138
+ # Returns the context for the property if context is nil.
139
+ # Returns a new RDF::Property object with the @context value set if context is provided
140
+ # see also #lang, #datatype
141
+ def context(context = nil)
142
+ if context.nil?
143
+ @context
144
+ else
145
+ property_with_context = self.dup
146
+ property_with_context.context = context
147
+ property_with_context
148
+ end
149
+ end
150
+
151
+ # Sets context for this property
152
+ def context=(context)
153
+ @context = context
154
+ end
155
+
156
+ # Returns the datatype if type is nil.
157
+ # Returns a new RDF::Property object with the @datatype set if type is provided
158
+ # see also #context, #lang
159
+ def datatype(type = nil)
160
+ if type.nil?
161
+ @datatype
162
+ else
163
+ property_with_datatype = dup
164
+ property_with_datatype.datatype = type
165
+ property_with_datatype
166
+ end
167
+ end
168
+
169
+ # Sets datatype for this property
170
+ def datatype=(type)
171
+ @datatype = type
172
+ end
173
+
174
+ # Deletes value given by key or value. If the item is not found, returns nil.
175
+ # If the optional code block is given, returns the result of block if the item is not found
176
+ def delete(md5_or_value)
177
+ value = self[md5_or_value]
178
+ if value
179
+ ActiveRDF::FederationManager.delete(@subject, self.property, value)
180
+ value
181
+ elsif block_given?
182
+ yield
183
+ end
184
+ end
185
+
186
+ # Deletes every value for which block evaluates to true
187
+ def delete_if(&block) # :yields: key, value
188
+ reject!(&block)
189
+ self
190
+ end
191
+
192
+ # Calls block once for each value, passing a copy of the value as a parameter
193
+ def each(&block) # :yields: value
194
+ q = ActiveRDF::Query.new.distinct(:o).where(@subject,self,:o,@context)
195
+ if @lang and !@datatype
196
+ q.lang(:o,@lang,@exact_lang)
197
+ elsif @datatype and !@lang
198
+ q.datatype(:o, @datatype)
199
+ elsif @lang and @datatype
200
+ raise ActiveRdfError, "@datatype and @lang may not both be set"
201
+ end
202
+ q.execute(&block)
203
+ self
204
+ end
205
+ alias :each_value :each
206
+
207
+ # Calls block once for each value, passing the key as a parameter
208
+ def each_key(&block) # :yields: key
209
+ keys.each(&block)
210
+ self
211
+ end
212
+
213
+ # Calls block once for each value, passing the key and value as a parameters
214
+ # See also #to_h
215
+ def each_pair(&block) # :yields: key, value
216
+ each{|value| yield get_key(value), value}
217
+ self
218
+ end
219
+
220
+ # Returns true if the property contains no values
221
+ def empty?
222
+ to_a.empty?
223
+ end
224
+ alias :blank? :empty?
225
+
226
+ # Returns a value from the property for the given key. If the key can't be found, there are several options:
227
+ # With no other arguments, it will raise an IndexError exception; if default is given, then that will be returned;
228
+ # if the optional code block is specified, then that will be run and its result returned.
229
+ def fetch(md5, default = nil, &block)
230
+ val = self[md5]
231
+ if val
232
+ val
233
+ else
234
+ if block_given?
235
+ yield md5
236
+ elsif default
237
+ default
238
+ else
239
+ raise IndexError, "could not find #{md5}"
240
+ end
241
+ end
242
+ end
243
+
244
+ # Returns true if the given key or value is present
245
+ def include?(obj)
246
+ !!self[obj]
247
+ end
248
+ alias :value? :include?
249
+ alias :has_value? :include?
250
+
251
+ # Returns the key for a given value. If not found, returns nil.
252
+ def index(obj)
253
+ value = to_a.find{|val| obj == val}
254
+ get_key(value) if value
255
+ end
256
+
257
+ # Return the value(s) of this property as a string.
258
+ def inspect
259
+ "#<RDF::Property #{abbr} [#{to_a.collect{|obj| obj.inspect}.join(", ")}]>"
260
+ end
261
+
262
+ # Returns a new array populated with the keys to the values
263
+ def keys
264
+ collect{|value| get_key(value)}
265
+ end
266
+
267
+ # Returns the language tag and the match settings for the property if tag is nil.
268
+ # Returns a new RDF::Property object with the @lang value set if tag is provided
269
+ # see also #context, #datatype
270
+ def lang(tag = nil, exact = true)
271
+ if tag.nil?
272
+ [@lang,@exact_lang]
273
+ else
274
+ property_with_lang = RDF::Property.new(self, @subject)
275
+ property_with_lang.lang = tag, exact
276
+ property_with_lang
277
+ end
278
+ end
279
+
280
+ # Sets lang and match settings
281
+ def lang=(*args)
282
+ args.flatten!
283
+ @lang = args[0].sub(/^@/,'')
284
+ @exact_lang = truefalse(args[1],true)
285
+ end
286
+
287
+ # Returns the number of values assigned to this property for this @subject
288
+ def length
289
+ to_a.length
290
+ end
291
+ alias :size :length
292
+
293
+ # Ensure the return of only one value assigned to this property for this @subject.
294
+ # If more than 1 value is found, ActiveRdfError is thrown.
295
+ def only
296
+ entries = self.entries
297
+ raise ActiveRDF::ActiveRdfError if entries.size > 1
298
+ entries[0]
299
+ end
300
+
301
+ # Equivalent to Property#delete_if, but returns nil if no changes were made
302
+ def reject!(&block) # :yields: key, value
303
+ change = false
304
+ each_pair do |key, value|
305
+ if yield(key, value)
306
+ delete(value)
307
+ change = true
308
+ end
309
+ end
310
+ self if change
311
+ end
312
+
313
+ # Value replacement. Replaces all current value(s) with the new value
314
+ def replace(new)
315
+ clear
316
+ add(new)
317
+ self
318
+ end
319
+ alias :size :length
320
+
321
+ # Allow this Property to be automatically converted to array
322
+ alias :to_ary :to_a
323
+
324
+ # Returns a hash of copies of all values with indexes.
325
+ # Changes to this hash will not effect the underlying values. Use #add or #replace to persist changes.
326
+ # See also #each_pair
327
+ def to_h
328
+ hash = {}
329
+ to_a.each do |value|
330
+ hash[get_key(value)] = value
331
+ end
332
+ hash
333
+ end
334
+
335
+ # Return an array containing the values for the given keys.
336
+ def values_at(*args)
337
+ args.collect{|md5| self[md5]}
338
+ end
339
+
340
+ private
341
+ def get_key(value)
342
+ Digest::MD5.hexdigest(value.to_s)
343
+ end
344
+ end
345
+ end
@@ -26,7 +26,7 @@ class PropertyList < Array
26
26
  add pv
27
27
 
28
28
  # insert the new statment into the store
29
- FederationManager.add(@s, @p, pv)
29
+ ActiveRDF::FederationManager.add(@s, @p, pv)
30
30
  end
31
31
 
32
32
  # delete a statment which contains old_p_value
@@ -35,7 +35,7 @@ class PropertyList < Array
35
35
  check_writeable!
36
36
  # delete the old statment
37
37
  self.delete(old_p_value)
38
- FederationManager.delete(@s, @p, old_p_value)
38
+ ActiveRDF::FederationManager.delete(@s, @p, old_p_value)
39
39
 
40
40
  # insert the new statment
41
41
  self << new_p_value
@@ -50,13 +50,13 @@ class PropertyList < Array
50
50
  # delete only triples whose values is specified by parameters
51
51
  params.each{|param|
52
52
  if self.delete(param)
53
- return FederationManager.delete(@s, @p, param)
53
+ return ActiveRDF::FederationManager.delete(@s, @p, param)
54
54
  end
55
55
  }
56
56
  else
57
57
  # delete every triple related to :s and :p whose values is contained by self...
58
58
  self.each{|value|
59
- if FederationManager.delete(@s, @p, value) == false
59
+ if ActiveRDF::FederationManager.delete(@s, @p, value) == false
60
60
  return false
61
61
  end
62
62
  }
@@ -0,0 +1,57 @@
1
+ module ActiveRDF
2
+ class PropertyLookup
3
+
4
+ # not directly chainable like ResourceQuery
5
+ def initialize(subject)
6
+ @ns = nil
7
+ @subject = subject
8
+ end
9
+
10
+ # Searches for property belonging to @subject. Returns RDF::Property.
11
+ # Replaces any existing values if method is an assignment: resource.prop=(new_value)
12
+ def method_missing(ns_or_property, *args)
13
+ # check registered namespace
14
+ # foaf::name, where foaf is a registered abbreviation
15
+ # evidence: foaf in Namespace
16
+ # action: return self with @ns set
17
+ if !@ns and Namespace.include?(ns_or_property)
18
+ @ns = ns_or_property
19
+ return self
20
+ end
21
+
22
+ property_name = ns_or_property.to_s
23
+ # are we doing an update or not?
24
+ # checking if method ends with '='
25
+ update = property_name[-1..-1] == '='
26
+ property_name = update ? property_name[0..-2] : property_name
27
+
28
+ property =
29
+ if @ns
30
+ # seen registered namespace, lookup property
31
+ RDF::Property.new(Namespace.lookup(@ns, property_name), @subject)
32
+ elsif (registered_predicates = @subject.instance_eval{@predicates}) and registered_predicates.include?(property_name)
33
+ # check for registered abbreviation
34
+ # eyal.age is registered abbreviation
35
+ # evidence: age in @predicates
36
+ # action: return RDF::Property(age,self) and store value if assignment
37
+ warn "Registered predicates is deprecated. Please use registered namespaces instead."
38
+ RDF::Property.new(@predicates[property_name],@subject)
39
+ else
40
+ # search for known property
41
+ @subject.properties.find{|prop| Namespace.localname(prop) == property_name}
42
+ end
43
+
44
+ if property
45
+ property.replace(*args) if update
46
+ return property
47
+ end
48
+
49
+ raise ActiveRdfError, "could not set #{methodname} to #{args}: no suitable predicate found. Maybe you are missing some schema information?" if update
50
+
51
+ # If property can't be found, return nil instead of throwing NoMethodError.
52
+ # In RDFS, we can't be sure that eyal cannot have an age, we just dont know the
53
+ # age right now)
54
+ nil
55
+ end
56
+ end
57
+ end