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
@@ -1,71 +1,102 @@
1
1
  # require 'active_rdf'
2
2
  require 'time'
3
3
 
4
- module Literal
5
- Namespace.register :xsd, 'http://www.w3.org/2001/XMLSchema#'
6
- def xsd_type
7
- case self
8
- when String
9
- XSD::string
10
- when Integer
11
- XSD::integer
12
- when Float
13
- XSD::double
14
- when TrueClass, FalseClass
15
- XSD::boolean
16
- when DateTime, Date, Time
17
- XSD::date
4
+ module RDFS
5
+ module Literal
6
+ ActiveRDF::Namespace.register :xsd, 'http://www.w3.org/2001/XMLSchema#'
7
+
8
+ class << self
9
+ attr_accessor :class_uri
18
10
  end
19
- end
20
11
 
21
- def self.typed(value, type)
22
- case type
23
- when XSD::string
24
- String.new(value)
25
- when XSD::date
26
- DateTime.parse(value)
27
- when XSD::boolean
28
- value == 'true' or value == 1
29
- when XSD::integer
30
- value.to_i
31
- when XSD::double
32
- value.to_f
12
+ # convert value to class type
13
+ def self.typed(value, type)
14
+ case type
15
+ when XSD::string
16
+ String.new(value)
17
+ when XSD::integer
18
+ value.to_i
19
+ when XSD::double
20
+ value.to_f
21
+ when XSD::boolean
22
+ value == 'true' or value == 1
23
+ when XSD::dateTime
24
+ DateTime.parse(value)
25
+ when XSD::time
26
+ Time.parse(value)
27
+ when XSD::date
28
+ Date.parse(value)
29
+ else
30
+ value
31
+ end
33
32
  end
34
- end
35
33
 
36
- def to_ntriple
37
- if $activerdf_without_xsdtype
38
- "\"#{to_s}\""
39
- else
40
- "\"#{to_s}\"^^#{xsd_type}"
34
+ def xsd_type
35
+ case self
36
+ when String
37
+ XSD::string
38
+ when Integer
39
+ XSD::integer
40
+ when Float
41
+ XSD::double
42
+ when TrueClass, FalseClass
43
+ XSD::boolean
44
+ when DateTime
45
+ XSD::dateTime
46
+ when Date
47
+ XSD::date
48
+ when Time
49
+ XSD::time
50
+ end
51
+ end
52
+ alias :datatype :xsd_type
53
+
54
+ def to_literal_s
55
+ s = kind_of?(Time) ? xmlschema : to_s
56
+ unless $activerdf_without_datatype
57
+ "\"#{s}\"^^<#{xsd_type}>"
58
+ else
59
+ "\"#{s}\""
60
+ end
41
61
  end
42
62
  end
63
+ Literal.class_uri = RDFS::Literal
43
64
  end
44
65
 
45
- class String; include Literal; end
46
- class Integer; include Literal; end
47
- class Float; include Literal; end
48
- class DateTime; include Literal; end
49
- class Date; include Literal; end
50
- class Time; include Literal; end
51
- class TrueClass; include Literal; end
52
- class FalseClass; include Literal; end
66
+ class String; include RDFS::Literal; end
67
+ class Integer; include RDFS::Literal; end
68
+ class Float; include RDFS::Literal; end
69
+ class DateTime; include RDFS::Literal; end
70
+ class Date; include RDFS::Literal; end
71
+ class Time; include RDFS::Literal; end
72
+ class TrueClass; include RDFS::Literal; end
73
+ class FalseClass; include RDFS::Literal; end
53
74
 
54
75
  class LocalizedString < String
55
- include Literal
76
+ include RDFS::Literal
77
+
56
78
  attr_reader :lang
57
- def initialize value, lang=nil
79
+ def initialize(value, lang)
58
80
  super(value)
59
-
60
- @lang = lang
61
- @lang = lang[1..-1] if @lang[0..0] == '@'
81
+ @lang = lang.sub(/^@/,'')
62
82
  end
63
83
 
64
- def to_ntriple
65
- if @lang
66
- "\"#{to_s}\"@#@lang"
84
+ def ==(other)
85
+ if other.is_a?(LocalizedString)
86
+ super && @lang == other.lang
67
87
  else
68
88
  super
69
89
  end
70
90
  end
71
- end
91
+ alias_method :eql?, :==
92
+
93
+ def inspect
94
+ super + "@#@lang"
95
+ end
96
+
97
+ # returns quoted string with language type if present.
98
+ # xsd:string isn't appended when lang missing (xsd:string should be considered the default type)
99
+ def to_literal_s
100
+ $activerdf_without_datatype ? "\"#{self}\"" : "\"#{self}\"@#@lang"
101
+ end
102
+ end
@@ -1,106 +1,139 @@
1
1
  # require 'active_rdf'
2
2
 
3
3
  # Manages namespace abbreviations and expansions
4
-
5
- class Namespace
6
- @@namespaces = Hash.new
7
- @@inverted_namespaces = Hash.new
8
-
9
- # registers a namespace prefix and its associated expansion (full URI)
10
- # e.g. :rdf and 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
11
- def self.register(prefix, fullURI)
12
- raise ActiveRdfError, 'prefix nor uri can be empty' if (prefix.to_s.empty? or fullURI.to_s.empty?)
13
- # raise ActiveRdfError, "namespace uri should end with # or /" unless /\/|#/ =~ fullURI.to_s[-1..-1]
14
- ActiveRdfLogger::log_info(self) { "Namespace: registering #{fullURI} to #{prefix}" }
15
- @@namespaces[prefix.to_sym] = fullURI.to_s
16
- @@inverted_namespaces[fullURI.to_s] = prefix.to_sym
17
-
18
- # enable namespace lookups through FOAF::name
19
- # if FOAF defined, add to it
20
- if Object.const_defined?(prefix.to_s.upcase)
21
- ns = Object.const_get(prefix.to_s.upcase)
22
- else
23
- # otherwise create a new module for it
24
- ns = Module.new
25
- Object.const_set(prefix.to_s.upcase, ns)
26
- end
27
-
28
- # catch FOAF::name or all other lookups
29
- class << ns
30
- def method_missing(method, *args)
31
- Namespace.lookup(self.to_s.downcase.to_sym, method)
4
+ module ActiveRDF
5
+ class Namespace
6
+ @@namespaces = Hash.new
7
+ @@inverted_namespaces = Hash.new
8
+
9
+ # registers a namespace prefix and its associated expansion (full URI)
10
+ # e.g. :rdf and 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
11
+ def Namespace.register(prefix, fullURI)
12
+ raise ActiveRdfError, 'prefix nor uri can be empty' if (prefix.to_s.empty? or fullURI.to_s.empty?)
13
+ raise ActiveRdfError, "namespace uri should end with # or /" unless /\/|#/ =~ fullURI.to_s[-1..-1]
14
+ klass_name = prefix.to_s.upcase
15
+ prefix = prefix.to_s.downcase.to_sym
16
+ ActiveRdfLogger::log_info(self) { "Namespace: registering #{fullURI} to #{klass_name}" }
17
+ @@namespaces[prefix] = fullURI.to_s
18
+ @@inverted_namespaces[fullURI.to_s] = prefix
19
+
20
+ # enable namespace lookups through FOAF::name
21
+ # if FOAF defined, extend it
22
+ if Object.const_defined?(klass_name)
23
+ ns = Object.const_get(klass_name)
24
+ else
25
+ # otherwise create a new module for it
26
+ ns = Module.new
27
+ Object.const_set(klass_name, ns)
32
28
  end
29
+ ns.extend NamespaceProxy
30
+ ns.prefix = prefix
31
+ ns
32
+ end
33
33
 
34
- def const_missing(klass)
35
- ObjectManager.construct_class(Namespace.lookup(self.to_s.downcase.to_sym, klass))
36
- end
34
+ # returns currently registered namespace abbreviations (e.g. :foaf, :rdf)
35
+ def Namespace.abbreviations
36
+ @@namespaces.keys
37
+ end
37
38
 
38
- # make some builtin methods private because lookup doesn't work otherwise
39
- # on e.g. RDF::type and FOAF::name
40
- [:type, :name, :id].each {|m| private(m) }
39
+ def Namespace.include?(name)
40
+ @@namespaces.keys.include?(name.to_s.downcase.to_sym)
41
41
  end
42
42
 
43
- # return the namespace proxy object
44
- ns
45
- end
43
+ # like include?, but returns the key for the namespace
44
+ def Namespace.find(name)
45
+ name = name.to_s.downcase.to_sym
46
+ @@namespaces.keys.find{|k| k == name}
47
+ end
46
48
 
47
- # returns a resource whose URI is formed by concatenation of prefix and localname
48
- def self.lookup(prefix, localname, resource_type = RDFS::Resource)
49
- resource_type.new(expand(prefix, localname))
50
- end
49
+ # returns a resource whose URI is formed by concatenation of prefix and localname
50
+ def Namespace.lookup(prefix, localname)
51
+ RDFS::Resource.new(expand(prefix, localname))
52
+ end
51
53
 
52
- # returns URI (string) formed by concatenation of prefix and localname
53
- def self.expand(prefix, localname)
54
- @@namespaces[prefix.to_sym].to_s + localname.to_s
55
- end
54
+ # returns URI (string) formed by concatenation of prefix and localname
55
+ def Namespace.expand(prefix, localname)
56
+ prefix = prefix.downcase if prefix.is_a?String
57
+ prefix = prefix.to_sym
58
+ @@namespaces[prefix].to_s + localname.to_s if @@namespaces[prefix]
59
+ end
56
60
 
57
- # returns prefix (if known) for the non-local part of the URI,
58
- # or nil if prefix not registered
59
- def self.prefix(resource)
60
- # get string representation of resource uri
61
- uri = if(resource.respond_to?(:uri))
62
- resource.uri.to_s
63
- else
64
- resource.to_s
61
+ # returns prefix (if known) for the non-local part of the URI,
62
+ # or nil if prefix not registered
63
+ def Namespace.prefix(obj)
64
+ uri = obj.respond_to?(:uri) ? obj.uri : obj.to_s
65
+ # uri.to_s gives us the uri of the resource (if resource given)
66
+ # then we find the last occurrence of # or / (heuristical namespace
67
+ # delimitor)
68
+ delimiter = uri.rindex(/#|\//)
69
+
70
+ # if delimiter not found, URI cannot be split into (non)local-part
71
+ return uri if delimiter.nil?
72
+
73
+ # extract non-local part (including delimiter)
74
+ nonlocal = uri[0..delimiter]
75
+ @@inverted_namespaces[nonlocal]
65
76
  end
66
77
 
67
- # uri.to_s gives us the uri of the resource (if resource given)
68
- # then we find the last occurrence of # or / (heuristical namespace
69
- # delimitor)
70
- delimiter = uri.rindex(/#|\//)
78
+ # returns local-part of URI
79
+ def Namespace.localname(obj)
80
+ uri = obj.respond_to?(:uri) ? obj.uri : obj.to_s
81
+ delimiter = uri.rindex(/#|\//)
82
+ if delimiter.nil? or delimiter == uri.size-1
83
+ uri
84
+ else
85
+ uri[delimiter+1..-1]
86
+ end
87
+ end
71
88
 
72
- # if delimiter not found, URI cannot be split into (non)local-part
73
- return uri if delimiter.nil?
89
+ # abbreviates resource if namespace is registered, otherwise returns nil
90
+ def Namespace.abbreviate(obj)
91
+ uri = obj.to_s
92
+ # uri.to_s gives us the uri of the resource (if resource given)
93
+ # then we find the last occurrence of # or / (heuristical namespace
94
+ # delimitor)
95
+ delimiter = uri.rindex(/#|\//)
96
+
97
+ if delimiter.nil? or delimiter == uri.size-1
98
+ abbr = @@inverted_namespaces[uri]
99
+ abbr.to_s if (abbr)
100
+ else
101
+ abbr = @@inverted_namespaces[uri[0..delimiter]]
102
+ abbr.to_s.upcase + "::" + uri[delimiter+1..-1] if (abbr)
103
+ end
104
+ end
105
+ end
74
106
 
75
- # extract non-local part (including delimiter)
76
- nonlocal = uri[0..delimiter]
107
+ module NamespaceProxy
108
+ attr_accessor :prefix, :klasses, :resources
77
109
 
78
- @@inverted_namespaces[nonlocal]
79
- end
110
+ def NamespaceProxy.extend_object(obj)
111
+ # make some builtin methods private because lookup doesn't work otherwise
112
+ # on e.g. RDF::type and FOAF::name
113
+ class << obj
114
+ [:type, :name, :id].each {|m| private(m) if respond_to?(m)}
115
+ end
116
+ super
117
+ end
80
118
 
81
- # returns local-part of URI
82
- def self.localname(resource)
83
- # get string representation of resource uri
84
- uri = if(resource.respond_to?(:uri))
85
- resource.uri.to_s
86
- else
87
- resource.to_s
119
+ # catch FOAF::name.
120
+ def method_missing(method, *args)
121
+ @resources ||={} # resource cache
122
+ @resources[method] ||= Namespace.lookup(@prefix, method)
88
123
  end
89
124
 
90
- delimiter = uri.rindex(/#|\//)
91
- if delimiter.nil? or delimiter == uri.size-1
92
- uri
93
- else
94
- uri[delimiter+1..-1]
125
+ # catch FOAF::Person
126
+ def const_missing(klass)
127
+ ActiveRdfLogger::log_info(self) { "Const missing on Namespace #{klass}" }
128
+ @klasses ||={} # class cache
129
+ @klasses[klass] ||= ObjectManager.construct_class(Namespace.lookup(@prefix, klass))
95
130
  end
96
- end
97
131
 
98
- # returns currently registered namespace abbreviations (e.g. :foaf, :rdf)
99
- def self.abbreviations
100
- @@namespaces.keys
101
- end
102
- end
132
+ end
103
133
 
104
- Namespace.register(:rdf, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
105
- Namespace.register(:rdfs, 'http://www.w3.org/2000/01/rdf-schema#')
106
- Namespace.register(:owl, 'http://www.w3.org/2002/07/owl#')
134
+ Namespace.register(:rdf, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
135
+ Namespace.register(:rdfs, 'http://www.w3.org/2000/01/rdf-schema#')
136
+ Namespace.register(:owl, 'http://www.w3.org/2002/07/owl#')
137
+ Namespace.register(:dc, 'http://purl.org/dc/elements/1.1/')
138
+ Namespace.register(:dcterms, 'http://purl.org/dc/terms/')
139
+ end
@@ -1,119 +1,124 @@
1
1
  # require 'active_rdf'
2
2
 
3
3
  # Constructs Ruby classes for RDFS classes (in the right namespace)
4
-
5
- class ObjectManager
6
- # Constructs empty Ruby classes for all RDF types found in the data. Allows
7
- # users to invoke methods on classes (e.g. FOAF::Person) without
8
- # getting symbol undefined errors (because e.g. foaf:person wasnt encountered
9
- # before so no class was created for it)
10
- def self.construct_classes
11
- # find all rdf:types and construct class for each of them
12
- #q = Query.new.select(:t).where(:s,Namespace.lookup(:rdf,:type),:t)
13
-
14
- # find everything defined as rdfs:class or owl:class
15
- type = Namespace.lookup(:rdf,:type)
16
- rdfsklass = Namespace.lookup(:rdfs,:Class)
17
-
18
- # TODO: we should not do this, we should not support OWL
19
- # instead, owl:Class is defined as subclass-of rdfs:Class, so if the
20
- # reasoner has access to owl definition it should work out fine.
21
- owlklass = Namespace.lookup(:owl,:Class)
22
-
23
- klasses = []
24
- klasses << Query.new.distinct(:s).where(:s,type,rdfsklass).execute
25
- klasses << Query.new.distinct(:s).where(:s,type,owlklass).execute
26
-
27
- # flattening to get rid of nested arrays
28
- # compacting array to get rid of nil (if one of these queries returned nil)
29
- klasses = klasses.flatten.compact
4
+ module ActiveRDF
5
+ class ObjectManager
6
+ # Constructs empty Ruby classes for all RDF types found in the data. Allows
7
+ # users to invoke methods on classes (e.g. FOAF::Person) without
8
+ # getting symbol undefined errors (because e.g. foaf:person wasnt encountered
9
+ # before so no class was created for it)
10
+ def ObjectManager.construct_classes
11
+ # find all rdf:types and construct class for each of them
12
+ #q = Query.new.select(:t).where(:s,Namespace.lookup(:rdf,:type),:t)
13
+
14
+ # TODO: we should not do this, we should not support OWL
15
+ # instead, owl:Class is defined as subclass-of rdfs:Class, so if the
16
+ # reasoner has access to owl definition it should work out fine.
17
+ klasses = []
18
+ klasses << Query.new.distinct(:s).where(:s,RDF::type,RDFS::Class).execute
19
+ klasses << Query.new.distinct(:s).where(:s,RDF::type,OWL::Class).execute
20
+
21
+ # flattening to get rid of nested arrays
22
+ # compacting array to get rid of nil (if one of these queries returned nil)
23
+ klasses = klasses.flatten.compact
30
24
  ActiveRdfLogger::log_debug(self) { "Construct_classes: classes found: #{klasses}" }
31
25
 
32
- # then we construct a Ruby class for each found rdfs:class
33
- # and return the set of all constructed classes
34
- klasses.collect { |t| construct_class(t) }
35
- end
26
+ # then we construct a Ruby class for each found rdfs:class
27
+ # and return the set of all constructed classes
28
+ klasses.collect { |t| construct_class(t) }
29
+ end
36
30
 
37
- # constructs Ruby class for the given resource (and puts it into the module as
38
- # defined by the registered namespace abbreviations)
39
- def self.construct_class(resource)
40
- # get prefix abbreviation and localname from type
41
- # e.g. :foaf and Person
42
- localname = Namespace.localname(resource)
43
- prefix = Namespace.prefix(resource)
44
-
45
- # find (ruby-acceptable) names for the module and class
46
- # e.g. FOAF and Person
47
- if prefix.nil?
48
- # if the prefix is unknown, we create our own from the full URI
49
- modulename = create_module_name(resource)
31
+ # constructs Ruby class for the given resource (and puts it into the module as
32
+ # defined by the registered namespace abbreviations)
33
+ def ObjectManager.construct_class(class_or_resource_or_uri)
34
+ case class_or_resource_or_uri
35
+ when Class, Module
36
+ return class_or_resource_or_uri
37
+ when RDFS::Resource
38
+ resource = class_or_resource_or_uri
39
+ when String
40
+ resource = RDFS::Resource.new(class_or_resource_or_uri)
41
+ else raise ActiveRdfError, "ObjectManager: can't construct class from #{class_or_resource_or_uri.inspect}"
42
+ end
43
+
44
+ # get prefix abbreviation and localname from type
45
+ # e.g. :foaf and Person
46
+ localname = Namespace.localname(resource)
47
+ prefix = Namespace.prefix(resource)
48
+
49
+ # find (ruby-acceptable) names for the module and class
50
+ # e.g. FOAF and Person
51
+ if prefix.nil?
52
+ # if the prefix is unknown, we create our own from the full URI
53
+ modulename = create_module_name(resource)
50
54
  ActiveRdfLogger::log_debug(self) { "Construct_class: constructing modulename #{modulename} from URI #{resource}" }
51
- else
52
- # otherwise we convert the registered prefix into a module name
53
- modulename = prefix_to_module(prefix)
55
+ else
56
+ # otherwise we convert the registered prefix into a module name
57
+ modulename = prefix_to_module(prefix)
54
58
  ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: constructing modulename #{modulename} from registered prefix #{prefix}" }
55
- end
56
- klassname = localname_to_class(localname)
59
+ end
60
+ klassname = localname_to_class(localname)
57
61
 
58
- # look whether module defined
59
- # else: create it
60
- _module = if Object.const_defined?(modulename.to_sym)
62
+ # look whether module defined
63
+ # else: create it
64
+ _module = if Object.const_defined?(modulename.to_sym)
61
65
  ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: module name #{modulename} previously defined" }
62
- Object.const_get(modulename.to_sym)
63
- else
66
+ Object.const_get(modulename.to_sym)
67
+ else
64
68
  ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: defining module name #{modulename} now" }
65
- Object.const_set(modulename, Module.new)
66
- end
69
+ Object.const_set(modulename, Module.new)
70
+ end
67
71
 
68
- # look whether class defined in that module
69
- if _module.const_defined?(klassname.to_sym)
72
+ # look whether class defined in that module
73
+ if _module.const_defined?(klassname.to_sym)
70
74
  ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: given class #{klassname} defined in the module" }
71
- # if so, return the existing class
72
- _module.const_get(klassname.to_sym)
73
- else
75
+ # if so, return the existing class
76
+ _module.const_get(klassname.to_sym)
77
+ else
74
78
  ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: creating given class #{klassname}" }
75
- # otherwise: create it, inside that module, as subclass of RDFS::Resource
76
- # (using toplevel Class.new to prevent RDFS::Class.new from being called)
77
- klass = _module.module_eval("#{klassname} = Object::Class.new(RDFS::Resource)")
78
- klass.class_uri = resource
79
- klass
79
+ # otherwise: create it, inside that module, as subclass of RDFS::Resource
80
+ # (using toplevel Class.new to prevent RDFS::Class.new from being called)
81
+ klass = _module.module_eval("#{klassname} = Object::Class.new(RDFS::Resource)")
82
+ klass.class_uri = resource
83
+ klass
84
+ end
80
85
  end
81
- end
82
86
 
83
- def self.prefix_to_module(prefix)
84
- # TODO: remove illegal characters
85
- prefix.to_s.upcase
86
- end
87
+ def ObjectManager.prefix_to_module(prefix)
88
+ # TODO: remove illegal characters
89
+ prefix.to_s.upcase
90
+ end
87
91
 
88
- def self.localname_to_class(localname)
89
- # replace illegal characters inside the uri
90
- # and capitalize the classname
91
- replace_illegal_chars(localname).capitalize
92
- end
92
+ def ObjectManager.localname_to_class(localname)
93
+ # replace illegal characters inside the uri
94
+ # and capitalize the classname
95
+ replace_illegal_chars(localname)
96
+ end
93
97
 
94
- def self.create_module_name(resource)
95
- # TODO: write unit test to verify replacement of all illegal characters
98
+ def ObjectManager.create_module_name(resource)
99
+ # TODO: write unit test to verify replacement of all illegal characters
96
100
 
97
- # extract non-local part (including delimiter)
98
- uri = resource.uri
99
- delimiter = uri.rindex(/#|\//)
100
- nonlocal = uri[0..delimiter]
101
+ # extract non-local part (including delimiter)
102
+ uri = resource.uri
103
+ delimiter = uri.rindex(/#|\//)
104
+ nonlocal = uri[0..delimiter]
101
105
 
102
- # remove illegal characters appearing at the end of the uri (e.g. trailing
103
- # slash)
104
- cleaned_non_local = nonlocal.gsub(/[^a-zA-Z0-9]+$/, '')
106
+ # remove illegal characters appearing at the end of the uri (e.g. trailing
107
+ # slash)
108
+ cleaned_non_local = nonlocal.gsub(/[^a-zA-Z0-9]+$/, '')
105
109
 
106
- # replace illegal chars within the uri
107
- replace_illegal_chars(cleaned_non_local).upcase
108
- end
110
+ # replace illegal chars within the uri
111
+ replace_illegal_chars(cleaned_non_local).upcase
112
+ end
109
113
 
110
- def self.replace_illegal_chars(name)
111
- name.gsub(/[^a-zA-Z0-9]+/, '_')
112
- end
114
+ def ObjectManager.replace_illegal_chars(name)
115
+ name.gsub(/[^a-zA-Z0-9]+/, '_')
116
+ end
113
117
 
114
- #declare the class level methods as private with these directives
115
- private_class_method :prefix_to_module
116
- private_class_method :localname_to_class
117
- private_class_method :create_module_name
118
- private_class_method :replace_illegal_chars
119
- end
118
+ #declare the class level methods as private with these directives
119
+ private_class_method :prefix_to_module
120
+ private_class_method :localname_to_class
121
+ private_class_method :create_module_name
122
+ private_class_method :replace_illegal_chars
123
+ end
124
+ end
@@ -98,7 +98,7 @@ class OrderedSet
98
98
  # execute query and return the result
99
99
  def query
100
100
  # execute query
101
- result = Query.new.select(:p, :o).where(self.uri, :p, :o).filter('regex(str(?p), "^' + (RDF::_).uri + '")').execute
101
+ result = Query.new.select(:p, :o).where(self.uri, :p, :o).regex(:p, "^#{(RDF::_).uri}").execute
102
102
 
103
103
  # order result
104
104
  result = result.sort_by { |items| items[0] }