semantic_naming 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = SemanticNaming
2
+
3
+ Semantic URL handling that can be used with the Talia system or ActiveRDF
@@ -0,0 +1,10 @@
1
+ module N
2
+ class Namespace < N::URI
3
+ @@default_namespaces = {
4
+ 'rdf' => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
5
+ 'xsd' => "http://www.w3.org/2001/XMLSchema#",
6
+ 'rdfs' => "http://www.w3.org/2000/01/rdf-schema#",
7
+ 'owl' => "http://www.w3.org/2002/07/owl#"
8
+ }
9
+ end
10
+ end
@@ -0,0 +1,33 @@
1
+ module N
2
+ # This is for URIs that act as namespaces. A namespace
3
+ # is a "prefix" for URIs.
4
+ #
5
+ # Shortcuts for some default namespaces are automatically defined (rdf, owl,
6
+ # ...). See default_namespaces.rb for details.
7
+ #
8
+ # Usually there should be no need to change those default namespaces
9
+ class Namespace < N::URI
10
+
11
+ @@default_namespaces.each do |shortcut, uri|
12
+ Namespace.shortcut(shortcut, uri)
13
+ end
14
+
15
+ # Finds all members of the given type that are part of this namespace.
16
+ # *Attention*: Due to the workings of SPARQL this will retrieve *all*
17
+ # elements that match the type and filter them. Thus it's advised to
18
+ # use this only for types of which only a few elements are known to exist
19
+ # (e.g. Onotology classes)
20
+ def elements_with_type(type, element_type = N::URI)
21
+ return unless(rdf_active? && is_iri?)
22
+ qry = ::Query.new.distinct.select(:s)
23
+ qry.where(:s, make_res(RDF::type), make_res(type))
24
+ qry.filter_uri_regexp(:s, "^#{@uri_s}")
25
+ qry.execute.collect { |item| element_type.new(item.uri) }
26
+ end
27
+
28
+ # Returns a list of predicate names.
29
+ def predicates
30
+ elements_with_type(N::RDF.Property, N::Predicate).map { |p| p.local_name }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,6 @@
1
+ module N
2
+ # This is the type of URI that represents a type of predicate
3
+ # (or relation, or property)
4
+ class Predicate < URI
5
+ end
6
+ end
@@ -0,0 +1,44 @@
1
+ module N
2
+
3
+ # This is the type of URI that represents a class of sources. The methods
4
+ # that browse the ontology hierarchy depend on ActiveRDF for accessing the
5
+ # RDF store. If ActiveRDF is not present, these will return nitl.
6
+ class SourceClass < URI
7
+
8
+ # Get the supertype of this class
9
+ def supertypes
10
+ return nil unless(active_rdf? && is_iri?)
11
+ qry = Query.new.distinct.select(:o)
12
+ qry.where(make_res(@uri_s), make_res(RDFS::subClassOf), :o)
13
+ qry.where(:o, make_res(RDF::type), make_res(RDFS + 'Class'))
14
+ qry.execute.collect { |item| SourceClass.new(item.uri) }
15
+ end
16
+
17
+ # Get the subtypes of this type
18
+ def subtypes
19
+ return nil unless(active_rdf? && is_iri?)
20
+ qry = Query.new.distinct.select(:s)
21
+ qry.where(:s, make_res(RDFS::subClassOf), make_res(@uri_s))
22
+ qry.where(:s, make_res(RDF::type), make_res(RDFS + 'Class'))
23
+ qry.execute.collect { |item| SourceClass.new(item.uri) }
24
+ end
25
+
26
+ # Get the instances of this type. return_type will be the class used to
27
+ # create the objects that are returned.
28
+ def instances(return_type)
29
+ return nil unless(active_rdf? && is_iri?)
30
+ qry = Query.new.distinct.select(:s)
31
+ qry.where(:s, make_res(RDF::type.to_s), make_res(@uri_s))
32
+ qry.execute.collect { |item| return_type.new(item.uri) }
33
+ end
34
+
35
+ # Get all the existing types from the RDF store
36
+ def self.rdf_types
37
+ return nil unless(URI.active_rdf?)
38
+ qry = Query.new.distinct.select(:s)
39
+ qry.where(:s, make_res(RDF::type), make_res(RDFS + 'Class'))
40
+ qry.execute.collect { |item| SourceClass.new(item.uri) }
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,306 @@
1
+ require 'active_rdf'
2
+ module N
3
+
4
+ # This class contains basic functionality for URIs
5
+ class URI
6
+
7
+ # Should behave like an ActiveRDF resource
8
+ include RDFS::ResourceLike
9
+
10
+ # Contains the registered uris
11
+ @@registered_uris = Hash.new
12
+
13
+ # Contains an inverse hash to lookup the shortcuts by uir
14
+ @@inverse_register = Hash.new
15
+
16
+ # Regexp that can match the part up until last # or / character,
17
+ # and the part behind that (domain, localname)
18
+ @@domainsplit_re = Regexp.compile("(.*[/#])([^/#]*)$")
19
+
20
+ # Match the language part in labels
21
+ @@lang_re = /(.*)@(.+)$/
22
+
23
+ # make some builtin methods private because lookup doesn't work otherwise
24
+ # on e.g. RDF::type and FOAF::name
25
+ [:type, :id].each {|m| private(m) }
26
+
27
+ # Create a new URI
28
+ def initialize(uri_s)
29
+ uri_s = uri_s.to_s
30
+ # TODO: More checking
31
+ @uri_s = uri_s
32
+ end
33
+
34
+ # Compare operator
35
+ def ==(object)
36
+ return true if eql?(object)
37
+ return object == @uri_s if(object.kind_of?(String))
38
+ return false
39
+ end
40
+
41
+ # eql? compare operator
42
+ def eql?(object)
43
+ return object.to_s == @uri_s if(object.kind_of?(URI))
44
+ return false
45
+ end
46
+
47
+ # Returns a hash
48
+ def hash
49
+ @uri_s.hash
50
+ end
51
+
52
+ # Add operator
53
+ def +(uri)
54
+ new_s = @uri_s + uri.to_s
55
+ return URI.new(new_s)
56
+ end
57
+
58
+ # Checks if the current URI is local
59
+ def local?
60
+ N::LOCAL.domain_of?(self)
61
+ end
62
+
63
+ # Redirect for checking if this is remote
64
+ def remote?
65
+ !local?
66
+ end
67
+
68
+ # String representation is the uri itself
69
+ def to_s
70
+ @uri_s
71
+ end
72
+
73
+ # Same for YAML
74
+ def to_yaml
75
+ @uri_s
76
+ end
77
+
78
+ # Alias "uri" for compatibility with ActiveRDF
79
+ alias_method :uri, :to_s
80
+
81
+ # Get a string representation in the form of 'namespace:name'. It is
82
+ # possible to select a different separator from ':'
83
+ def to_name_s(separator = ':')
84
+ "#{namespace}#{separator}#{local_name}"
85
+ end
86
+
87
+ # This creates a helpers for a nice notation of
88
+ # like my_domain::myid
89
+ def const_missing(klass)
90
+ return URI.new(@uri_s + klass.to_s)
91
+ end
92
+
93
+ # See const_missing
94
+ def method_missing(method, *args)
95
+ # Quick sanity check: args make no sense for this
96
+ raise(NoMethodError, "Undefined method: " + method.to_s) if(args && args.size > 0)
97
+
98
+ return URI.new(@uri_s + method.to_s)
99
+ end
100
+
101
+ # Is true if this object describes the domain of the
102
+ # given uri, and the given uri is a resource in that
103
+ # domain
104
+ def domain_of?(uri)
105
+ uri_s = uri.to_s
106
+
107
+ (uri_s =~ /\A#{@uri_s}\w*/) != nil
108
+ end
109
+
110
+ # Request URI by shortcut. If called on a sublass, this accessor
111
+ # will only return an URI if it's of the same type as the subclass.
112
+ def self.[](shortcut)
113
+ shortcut = shortcut.to_s.downcase.to_sym
114
+
115
+ uri = @@registered_uris[shortcut]
116
+
117
+ # We only return the uri if it's of the same kind as ourselves
118
+ uri.kind_of?(self) ? uri : nil
119
+ end
120
+
121
+ # Returns a hash with all registered shortcuts.
122
+ # This will only return the shortcuts of the same class (and subclasses)
123
+ # than the one on which the method is called
124
+ def self.shortcuts
125
+ shortcuts = {}
126
+ @@registered_uris.each do |key, value|
127
+ shortcuts[key] = value if(value.kind_of?(self))
128
+ end
129
+
130
+ shortcuts
131
+ end
132
+
133
+ # Check if a shortcut is registered
134
+ def self.shortcut_exists?(shortcut)
135
+ @@registered_uris[shortcut.to_s.downcase.to_sym] != nil
136
+ end
137
+
138
+ # Get an URI string from the given string in ns:name notation
139
+ def self.make_uri(str, separator = ":", default_namespace = N::LOCAL)
140
+ type = str.split(separator)
141
+ type = [type[1]] if(type[0] == "")
142
+ if(type.size == 2)
143
+ self.new(N::URI[type[0]] + type[1])
144
+ else
145
+ default_namespace + type[0]
146
+ end
147
+ end
148
+
149
+ # Returns the local name
150
+ def local_name
151
+ localname = nil
152
+
153
+ if(md = @@domainsplit_re.match(@uri_s))
154
+ localname = md[2]
155
+ end
156
+
157
+ localname
158
+ end
159
+
160
+ # Returns the domain part of the URI
161
+ def domain_part
162
+ domainpart = nil
163
+
164
+ if(md = @@domainsplit_re.match(@uri_s))
165
+ domainpart = md[1]
166
+ end
167
+
168
+ domainpart ? URI.new(domainpart) : nil
169
+ end
170
+
171
+ # Returns the shortcut for the current URI, or nil
172
+ # if no shortcut is defined for this URI
173
+ def my_shortcut
174
+ @@inverse_register[@uri_s]
175
+ end
176
+
177
+ # Returns the namespace of this URI, if the URI
178
+ # is part of a namespace. The rule is quite strict:
179
+ # The URI is only part of the namespace if it is the
180
+ # namespace itself or the namespace plus a single local part
181
+ #
182
+ # If the URI is not part of a namespace, nil is returned
183
+ def namespace
184
+ nspace = nil
185
+
186
+ domain_shortcut = domain_part.my_shortcut
187
+
188
+ if(domain_shortcut && URI[domain_shortcut].is_a?(Namespace))
189
+ nspace = domain_shortcut
190
+ end
191
+
192
+ nspace
193
+ end
194
+
195
+ # Register a shortcut to the given URI. You may force to overwrite an
196
+ # existing shortcut, but this is not recommended. The option exists practically
197
+ # only to override default namespaces if there is a need.
198
+ def self.shortcut(shortcut, uri, force = false)
199
+ shortcut = shortcut.to_s.downcase.to_sym
200
+ constant = shortcut.to_s.upcase.to_sym
201
+
202
+ # make an object of my own type
203
+ uri = self.new(uri)
204
+
205
+ if(!force && (@@registered_uris[shortcut] || N.const_defined?(constant)))
206
+ raise(NameError, "Shortcut already defined: '#{shortcut.to_s}'")
207
+ end
208
+
209
+ if(!force && (@@inverse_register[uri.to_s]))
210
+ raise(NameError, "Shortcut for this uri already exists: #{uri.to_s}")
211
+ end
212
+
213
+ @@registered_uris[shortcut] = uri
214
+ @@inverse_register[uri.to_s] = shortcut
215
+ N.const_set(constant, uri)
216
+
217
+ return uri
218
+ end
219
+
220
+ # Check if a given string is an URI
221
+ def self.is_uri?(uri_str)
222
+ uri_str =~ /:/
223
+ end
224
+
225
+ # If the RDF store is active and the class has an rdfs:label, this will be
226
+ # returned. Otherwise, this will return the short name of the current source
227
+ # (like ns:name)
228
+ #
229
+ # You may give a language, if none is given 'en' is used by default. If
230
+ # no label with a language marker is found, the first none-language-label
231
+ # is used
232
+ def rdf_label(language = 'en')
233
+ @labels ||= {}
234
+ @labels[language] ||= label_by_lang(rdfs_labels, language)
235
+ # The rdf label is cache, while the default may change
236
+ @labels[language] ||= to_name_s
237
+ @labels[language]
238
+ end
239
+
240
+ private
241
+
242
+ # Check if the ActiveRDF library is present.
243
+ def self.active_rdf?
244
+ unless(defined?(@active_rdf))
245
+ @active_rdf = defined?(::ConnectionPool) && (::ConnectionPool.read_adapters.size > 0)
246
+ end
247
+
248
+ @active_rdf
249
+ end
250
+
251
+ # See the respective class method
252
+ def is_iri?
253
+ self.class.is_iri?(@uri_s)
254
+ end
255
+
256
+ # Checks if the current URI is a valid IRI (special for of URI defined
257
+ # in the SPARQL spec). URIs that are not IRIs may cause problems with
258
+ # SPARQL queries.
259
+ def self.is_iri?(string)
260
+ (string =~ /[{}|\\^`\s]/) == nil
261
+ end
262
+
263
+ # RDF check, this is a convenience for instances
264
+ def active_rdf?
265
+ URI.active_rdf?
266
+ end
267
+
268
+ # Create a resource from the given type
269
+ def self.make_res(type)
270
+ ::RDFS::Resource.new(type.to_s)
271
+ end
272
+
273
+ # Instance convenience accessor
274
+ def make_res(type)
275
+ self.class.make_res(type)
276
+ end
277
+
278
+ # Gets the rdfs:labels from the rdf store
279
+ def rdfs_labels
280
+ if(active_rdf? && is_iri?)
281
+ labels = make_res(@uri_s)[(N::RDFS::label).to_s]
282
+ if(labels && labels.size > 0)
283
+ labels
284
+ end # else nil
285
+ end # else nil
286
+ end
287
+
288
+ # Gets the label for the given language. If no labels are given, return nil
289
+ def label_by_lang(labels, language = 'en')
290
+ return nil unless(labels)
291
+ result = nil
292
+ labels.each do |label|
293
+ if(match = @@lang_re.match(label)) # check if there is a "language" string
294
+ if(match[2] == language)
295
+ return match[1] # We've found one, break here and return the label part
296
+ end
297
+ else
298
+ result ||= label # Keep the non-language label
299
+ end
300
+ end
301
+
302
+ result
303
+ end
304
+
305
+ end
306
+ end
@@ -0,0 +1,13 @@
1
+ # SemanticNaming loader
2
+
3
+ # adding semantic_naming subdirectory to the ruby loadpath
4
+ file = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
5
+ this_dir = File.dirname(File.expand_path(file))
6
+ $: << this_dir
7
+ $: << this_dir + '/semantic_naming/'
8
+
9
+ require 'semantic_naming/uri'
10
+ require 'semantic_naming/default_namespaces'
11
+ require 'semantic_naming/namespace'
12
+ require 'semantic_naming/source_class'
13
+ require 'semantic_naming/predicate'
@@ -0,0 +1,55 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/test_helper'
3
+
4
+ # Test the namespace functionality
5
+ class NamespaceTest < Test::Unit::TestCase
6
+ # Tests the basic registering of namespaces
7
+ def test_shortcut
8
+ new_ns = N::Namespace.shortcut(:ns_short, "http://www.ns_short.com/")
9
+ assert_equal(new_ns, N::NS_SHORT);
10
+ assert_equal(new_ns.to_s, "http://www.ns_short.com/")
11
+ assert_equal(new_ns, N::URI.new("http://www.ns_short.com/"))
12
+ assert_kind_of(N::Namespace, new_ns)
13
+ assert_kind_of(N::Namespace, N::NS_SHORT)
14
+ assert_raise(NameError) { N::Namespace.shortcut(:ns_short, "http://www.ns_short.com/") }
15
+ end
16
+
17
+ # Test if the "shortcuts" method works correctly for namespaces
18
+ def test_shortcuts
19
+ N::Namespace.shortcut(:at_least_one_ns, "http://atleast_namespace.com/")
20
+ N::URI.shortcut(:another_one_outside_uri, "http://anotherone_uri.com/")
21
+ assert(N::Namespace.shortcuts.size > 0, "There should be at least one namespace shortcut")
22
+ assert(!N::Namespace.shortcuts.include?(:another_one_outside))
23
+ end
24
+
25
+ # Test the array-type accessor
26
+ def test_array_type_access
27
+ new_ns = N::Namespace.shortcut(:ns_array_test, "http://www.ns_array_test.com/")
28
+ assert_equal(new_ns, N::Namespace[:ns_array_test])
29
+ end
30
+
31
+ # Test the array-type accessor if the superclass is excluded
32
+ def test_array_type_access_super
33
+ N::URI.shortcut(:ns_array_test_s, "http://www.ns_array_test_super.com/")
34
+ assert_equal(nil, N::Namespace[:ns_array_test_s])
35
+ end
36
+
37
+ # Test the array-type accessor if sibling classes are excluded
38
+ def test_array_type_access_sibling
39
+ N::SourceClass.shortcut(:ns_array_test_sib, "http://www.ns_array_test_sibling.com/")
40
+ assert_equal(nil, N::Namespace[:ns_array_test_sib])
41
+ end
42
+
43
+ # Test getting elements by type
44
+ def test_elements_by_type
45
+ return unless(RDF_ACTIVE)
46
+ namespace = N::Namespace.new(N::RDFTEST)
47
+ elements = namespace.elements_with_type(N::RDFTEST.Type1).collect { |type| type.uri.to_s }
48
+ assert_equal([N::RDFTEST.test1.to_s, N::RDFTEST.test2.to_s].sort, elements.sort)
49
+ end
50
+
51
+ def _ignore_test_predicates
52
+ assert_kind_of(Array, N::RDF.predicates) unless RDF_ACTIVE
53
+ end
54
+ end
55
+
@@ -0,0 +1,21 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/test_helper'
3
+
4
+ # Test the namespace functionality
5
+ class PredicateTest < Test::Unit::TestCase
6
+ include N
7
+ def setup
8
+ @uri = "http://www.predicate_shortcut.com/"
9
+ end
10
+
11
+ # Tests the basic registering of namespaces
12
+ def test_shortcut
13
+ new_pr = Predicate.shortcut(:newpr, @uri)
14
+ assert_equal(new_pr, NEWPR);
15
+ assert_equal(new_pr.to_s, @uri)
16
+ assert_equal(new_pr, URI.new(@uri))
17
+ assert_kind_of(Predicate, new_pr)
18
+ assert_kind_of(Predicate, NEWPR)
19
+ assert_raise(NameError) { Namespace.shortcut(:newpr, @uri) }
20
+ end
21
+ end
@@ -0,0 +1,46 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/test_helper'
3
+
4
+
5
+ # Test the uri class
6
+ class TypeTest < Test::Unit::TestCase
7
+
8
+ # Tests the basic registering of namespaces
9
+ def test_shortcut
10
+ new_sc = N::SourceClass.shortcut(:newsc, "http://www.source_shortcut.com/")
11
+ assert_equal(new_sc, N::NEWSC);
12
+ assert_equal(new_sc.to_s, "http://www.source_shortcut.com/")
13
+ assert_equal(new_sc, N::URI.new("http://www.source_shortcut.com/"))
14
+ assert_kind_of(N::SourceClass, new_sc)
15
+ assert_kind_of(N::SourceClass, N::NEWSC)
16
+ assert_raise(NameError) { N::Namespace.shortcut(:newsc, "http://www.source_shortcut.com/") }
17
+ end
18
+
19
+ # Test the supertypes method
20
+ def test_supertypes
21
+ return unless(RDF_ACTIVE)
22
+ src = N::SourceClass.new(N::RDFTEST.Type1)
23
+ subtypes = src.subtypes.collect { |type| type.uri.to_s }
24
+ assert_equal([N::RDFTEST.Type2.to_s, N::RDFTEST.Type3.to_s].sort, subtypes.sort)
25
+ end
26
+
27
+ def test_subtypes
28
+ return unless(RDF_ACTIVE)
29
+ src = N::SourceClass.new(N::RDFTEST.Type3)
30
+ supertypes = src.supertypes.collect { |type| type.uri.to_s }
31
+ assert_equal([N::RDFTEST.Type1.to_s, N::RDFTEST.Type4.to_s].sort, supertypes.sort)
32
+ end
33
+
34
+ def test_instances
35
+ return unless(RDF_ACTIVE)
36
+ src = N::SourceClass.new(N::RDFTEST.Type1)
37
+ instances = src.instances(N::URI).collect { |type| type.uri.to_s }
38
+ assert_equal([N::RDFTEST.test1.to_s, N::RDFTEST.test2.to_s, N::RDFTEST2.test1.to_s, N::RDFTEST2.test2.to_s].sort, instances.sort)
39
+ end
40
+
41
+ def test_rdf_types
42
+ return unless(RDF_ACTIVE)
43
+ types = N::SourceClass.rdf_types.collect { |type| type.uri.to_s }
44
+ assert_equal([N::RDFTEST.Type1.to_s, N::RDFTEST.Type2.to_s, N::RDFTEST.Type3.to_s, N::RDFTEST.Type4.to_s].sort, types.sort)
45
+ end
46
+ end
data/test/uri_test.rb ADDED
@@ -0,0 +1,225 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/test_helper'
3
+ # Test the uri class
4
+ class URITest < Test::Unit::TestCase
5
+
6
+ @@local_domain = "http://www.samplething.com/"
7
+ if(!N.const_defined?(:LOCAL))
8
+ N::Namespace.shortcut(:local, @@local_domain)
9
+ end
10
+
11
+ # Basic test for uri class
12
+ def test_uri
13
+ uri_string = "http://foobar.com/xyz/"
14
+ uri = N::URI.new(uri_string)
15
+ assert_equal(uri_string, uri.to_s)
16
+ end
17
+
18
+ # Test local and remote checks
19
+ def test_local_remote
20
+ local_string = @@local_domain + "/myid"
21
+ remote_string = "http://www.remote.com/something"
22
+
23
+
24
+ domain = N::URI.new(@@local_domain)
25
+ local = N::URI.new(local_string)
26
+ remote = N::URI.new(remote_string)
27
+
28
+ assert(local.local?)
29
+ assert(!local.remote?)
30
+ assert(remote.remote?)
31
+ assert(!remote.local?)
32
+ assert(domain.local?)
33
+ end
34
+
35
+ # Tests the equality operator
36
+ def test_equality
37
+ uri_string = "http://foobar.com/xyz/"
38
+ uri = N::URI.new(uri_string)
39
+ uri_2 = N::URI.new(uri_string)
40
+ uri_other = N::URI.new("http://otheruri.com/")
41
+
42
+ assert_equal(uri, uri_string)
43
+ assert_equal(uri, uri)
44
+ assert_equal(uri, uri_2)
45
+ assert_not_equal("http://something.org", uri)
46
+ assert_not_equal(uri, uri_other)
47
+ assert_not_equal(uri, Hash.new)
48
+ end
49
+
50
+ # Tests the domain_of operation
51
+ def test_domain_of
52
+ local_string = @@local_domain + "/myid"
53
+ remote_string = "http://www.remote.com/something"
54
+
55
+ local_domain = N::URI.new(@@local_domain)
56
+ local = N::URI.new(local_string)
57
+ remote = N::URI.new(remote_string)
58
+
59
+ assert(local.domain_of?(local))
60
+ assert(local.domain_of?(local_string))
61
+ assert(local_domain.domain_of?(local))
62
+ assert(local_domain.domain_of?(local_string))
63
+
64
+ assert(!local.domain_of?(local_domain))
65
+ assert(!local.domain_of?(remote))
66
+ assert(!local.domain_of?(remote_string))
67
+ assert(!remote.domain_of?(local))
68
+ end
69
+
70
+ # Test the add operator
71
+ def test_add
72
+ uri_string = "http://foobar.com/xyz/"
73
+ uri = N::URI.new(uri_string)
74
+ assert_equal(N::URI.new(uri_string + "add"), uri + "add")
75
+ assert_equal(N::URI.new(uri_string + "add"), uri + N::URI.new("add"))
76
+ end
77
+
78
+ # Test if builtin methods were overwritten
79
+ def test_builtin_overwrite
80
+ domain = N::URI.new(@@local_domain)
81
+ assert_kind_of(N::URI, domain::type)
82
+ assert_equal(@@local_domain + "type", domain.type.to_s)
83
+ end
84
+
85
+ # Test the easy accessors
86
+ def test_easy_accessors
87
+ domain = N::URI.new(@@local_domain)
88
+ assert_equal(@@local_domain + "foo", (domain.foo).to_s)
89
+ assert_equal(@@local_domain + "foo", (domain::foo).to_s)
90
+ assert_equal(@@local_domain + "FoO", (domain.FoO).to_s)
91
+ assert_raise(NoMethodError) { domain.foo(12) }
92
+ end
93
+
94
+ # Test registering of shortcuts
95
+ def test_shortcut_register
96
+ uri = N::URI.new("http://foo.foo.com/")
97
+ N::URI.shortcut(:foo, uri.to_s)
98
+ assert_equal(N::FOO, uri)
99
+ assert_kind_of(N::URI, N::FOO)
100
+ end
101
+
102
+ # Test the shortcuts method
103
+ def test_shortcuts
104
+ N::URI.shortcut(:at_least_one, "http://atleast.com")
105
+ N::URI.shortcut(:at_least_two, "http://atleasttwo.com")
106
+ assert(N::URI.shortcuts.size > 1, "There should be at least two shortcuts")
107
+ assert(N::URI.shortcuts.keys.include?(:at_least_one))
108
+ assert(N::URI.shortcuts.keys.include?(:at_least_two))
109
+ end
110
+
111
+ # Test if the assignment of illegal shortcuts fails correctly
112
+ def test_illegal_shortcuts
113
+ N::URI.shortcut(:illegal_short, "http://illegal.shortcut.com/")
114
+ assert_raises(NameError) { N::URI.shortcut(:illegal_short, "xxx") }
115
+ assert_raises(NameError) { N::URI.shortcut(:legal_short, "http://illegal.shortcut.com/")}
116
+ end
117
+
118
+ # Checks if nonexistent/illegal shortcuts fail correctly
119
+ def test_nonexistent_shortcut
120
+ assert_raises(NameError) { N::Foo }
121
+ end
122
+
123
+ # Tests the array-type accessor
124
+ def test_shortcut_accessor
125
+ assert_equal(N::LOCAL, N::URI[:local])
126
+ end
127
+
128
+ # Test array-type accessor with subclass
129
+ def test_shortcut_accessor_subclass
130
+ namesp = N::Namespace.shortcut(:uri_array_ns_short, "http://test_shortcut_accessor_subclass/")
131
+ assert_equal(namesp, N::URI[:uri_array_ns_short])
132
+ end
133
+
134
+ # Test the is_uri? convenience method
135
+ def test_is_uri
136
+ assert(N::URI.is_uri?("http://foobar.org/"))
137
+ assert(N::URI.is_uri?("http://foobar.org/"))
138
+ assert(N::URI.is_uri?("baa:boo"))
139
+ assert(!N::URI.is_uri?("foo"))
140
+ end
141
+
142
+ # Try to get the shortcut from a URL
143
+ def test_check_shortcut
144
+ N::Namespace.shortcut(:xyshortcut, "http://xyz/")
145
+ assert_equal(:xyshortcut, N::URI.new("http://xyz/").my_shortcut)
146
+ end
147
+
148
+ # Try to get inexistent shortcut from a URL
149
+ def test_inexistent_shortcut
150
+ assert_equal(nil, N::URI.new("http://noshortcut/").my_shortcut)
151
+ end
152
+
153
+ # Try to get the local name of an uri
154
+ def test_local_name
155
+ assert_equal("master", N::URI.new("http://somethingelse.com/master").local_name)
156
+ assert_equal("slave", N::URI.new("http://somethingelse.com/master#slave").local_name)
157
+ assert_equal("chicken", N::URI.new("http://somethingelse.com/animals/chicken").local_name)
158
+ end
159
+
160
+ # Special case for local name
161
+ def test_only_local_name
162
+ assert_equal(nil, N::URI.new("file:thingy").local_name)
163
+ end
164
+
165
+ # Only domain, no local
166
+ def test_inexistent_local_name
167
+ assert_equal("", N::URI.new("http://somethingelse.com/").local_name)
168
+ end
169
+
170
+ # Try to get the path name of an uri
171
+ def test_domain_part
172
+ assert_kind_of(N::URI, N::URI.new("http://somethingelse.com/foobar/bla/").domain_part)
173
+ assert_equal("http://somethingelse.com/foobar/bla/", N::URI.new("http://somethingelse.com/foobar/bla/").domain_part.to_s)
174
+ assert_equal("http://somethingelse.com/foobar/bla/", N::URI.new("http://somethingelse.com/foobar/bla/thing").domain_part.to_s)
175
+ assert_equal("http://somethingelse.com/foobar/bla#", N::URI.new("http://somethingelse.com/foobar/bla#thong").domain_part.to_s)
176
+ end
177
+
178
+ # Test special URI with no domain
179
+ def test_no_domain
180
+ assert_equal(nil, N::URI.new("file:thingy").domain_part)
181
+ end
182
+
183
+ # Try to get the namspace of an uri
184
+ def test_namespace
185
+ N::Namespace.shortcut(:test_namespace, "http://test_namespace/")
186
+ assert_equal(:test_namespace, N::URI.new("http://test_namespace/").namespace)
187
+ assert_equal(:test_namespace, N::URI.new("http://test_namespace/else").namespace)
188
+ assert_equal(nil, N::URI.new("http://test_namespace/else/other").namespace)
189
+ end
190
+
191
+ # No namespace
192
+ def test_namespace_inexistent
193
+ assert_equal(nil, N::URI.new("http://unrelated").namespace)
194
+ end
195
+
196
+ # Test make_uri
197
+ def test_make_uri
198
+ N::Namespace.shortcut(:makeuri, "http://test_makeuri/")
199
+ uri = N::URI.make_uri("makeuri:foo")
200
+ assert_equal("http://test_makeuri/foo", uri.to_s)
201
+ end
202
+
203
+ # Test namespace with shortcut of wrong time
204
+ def test_not_defined_as_namespace
205
+ N::URI.shortcut(:not_namespace, "http://iamnotanamespace/")
206
+ assert_equal(nil, N::URI.new("http://iamnotanamespace/").namespace)
207
+ end
208
+
209
+ # Test rdf label
210
+ def test_rdf_label
211
+ uri = N::URI.new(N::RDFTEST::test1)
212
+ if(RDF_ACTIVE)
213
+ assert_equal("Like a virgin", uri.rdf_label('it'))
214
+ assert_equal("come on", uri.rdf_label('en'))
215
+ assert_equal("come on", uri.rdf_label)
216
+ else
217
+ assert_equal("rdftest:test1", uri.rdf_label('it'))
218
+ end
219
+ end
220
+
221
+ def test_hash
222
+ assert_equal(N::RDFTEST.testme.to_s.hash, N::RDFTEST.testme.hash)
223
+ end
224
+
225
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: semantic_naming
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Hahn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-14 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: hahn@netseven.it
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - lib/semantic_naming.rb
26
+ - lib/semantic_naming/default_namespaces.rb
27
+ - lib/semantic_naming/namespace.rb
28
+ - lib/semantic_naming/predicate.rb
29
+ - lib/semantic_naming/source_class.rb
30
+ - lib/semantic_naming/uri.rb
31
+ - README.rdoc
32
+ has_rdoc: true
33
+ homepage: http://talia.discovery-project.eu/
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --charset=UTF-8
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.3.5
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Semantic Naming Extensions for ActiveRDF, Talia and others
60
+ test_files:
61
+ - test/namespace_test.rb
62
+ - test/predicate_test.rb
63
+ - test/source_class_test.rb
64
+ - test/uri_test.rb