semantic_naming 2.0.0 → 2.0.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.
data/README.rdoc CHANGED
@@ -1,3 +1,9 @@
1
1
  = SemanticNaming
2
2
 
3
- Semantic URL handling that can be used with the Talia system or ActiveRDF
3
+ Semantic URL handling that can be used with the Talia system or ActiveRDF. This contains
4
+ a number of methods of encoding semantic URIs, dealing with namespaces and providing a
5
+ neat interface to access URIs.
6
+
7
+ The library offers additional functionality when active_rdf is installed. Note that at this
8
+ point in time you should use the 'activerdf_net7' gem, as it's tuned to work with the
9
+ semantic naming. The default activerdf gem *will* cause problems.
@@ -19,10 +19,10 @@ module N
19
19
  # (e.g. Onotology classes)
20
20
  def elements_with_type(type, element_type = N::URI)
21
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) }
22
+ qry = ::Query.new(element_type).distinct.select(:s)
23
+ qry.where(:s, RDF.type, type)
24
+ qry.filter_regexp(:s, "^#{@uri_s}")
25
+ qry.execute
26
26
  end
27
27
 
28
28
  # Returns a list of predicate names.
@@ -8,36 +8,77 @@ module N
8
8
  # Get the supertype of this class
9
9
  def supertypes
10
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) }
11
+ qry = Query.new(SourceClass).distinct.select(:o)
12
+ qry.where(self, RDFS.subClassOf, :o)
13
+ qry.where(:o, RDF.type, RDFS.Class)
14
+ qry.execute
15
15
  end
16
16
 
17
17
  # Get the subtypes of this type
18
18
  def subtypes
19
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) }
20
+ qry = Query.new(SourceClass).distinct.select(:s)
21
+ qry.where(:s, RDFS.subClassOf, self)
22
+ qry.where(:s, RDF.type, RDFS.Class)
23
+ qry.execute
24
24
  end
25
25
 
26
26
  # Get the instances of this type. return_type will be the class used to
27
27
  # create the objects that are returned.
28
28
  def instances(return_type)
29
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) }
30
+ qry = Query.new(SourceClass).distinct.select(:s)
31
+ qry.where(:s, RDF.type, self)
32
+ qry.execute
33
33
  end
34
34
 
35
35
  # Get all the existing types from the RDF store
36
36
  def self.rdf_types
37
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) }
38
+ qry = Query.new(SourceClass).distinct.select(:s)
39
+ qry.where(:s, RDF.type, RDFS.Class)
40
+ qry.execute
41
+ end
42
+
43
+ # Return a subclass hierarchy. This is quicker than going through
44
+ # the hierarchy step by step. It will look at all subclass (*not*
45
+ # superclass!) relationships and return a nested, tree-like structure
46
+ # build of hashes. Each key in the hash will be the class object, and
47
+ # the values are hashes with the child elements, and so on
48
+ #
49
+ # E.g. : { type1 => { subtype_a => {}, subtype_b => { xtype => {}} }, type_2 => {}}
50
+ def self.subclass_hierarchy
51
+ return nil unless(URI.active_rdf?)
52
+ types = rdf_types
53
+ qry = Query.new(SourceClass).distinct.select(:class, :subclass)
54
+ qry.where(:class, RDF.type, RDFS.Class)
55
+ qry.where(:subclass, RDFS.subClassOf, :class)
56
+ subtype_list = qry.execute
57
+ hierarchy = {}
58
+ # Sift through the triples and add the sub-items
59
+ subtype_list.each do |sub_items|
60
+ klass, subklass = sub_items
61
+ hierarchy[klass] ||= {}
62
+ hierarchy[klass][subklass] = true
63
+ end
64
+
65
+ # Now we link up the subclass relations
66
+ hierarchy.each do |key, values|
67
+ values.each_key do |subkey|
68
+ next if(subkey == :is_child)
69
+ hierarchy[subkey] ||= {}
70
+ values[subkey] = hierarchy[subkey]
71
+ values[subkey][:is_child] = true
72
+ end
73
+ end
74
+
75
+ # Join with the general types and remove the children
76
+ types.each do |type|
77
+ xtype = (hierarchy[type] ||= {})
78
+ hierarchy.delete(type) if(xtype.delete(:is_child))
79
+ end
80
+
81
+ hierarchy
41
82
  end
42
83
 
43
84
  end
@@ -1,4 +1,3 @@
1
- require 'active_rdf'
2
1
  module N
3
2
 
4
3
  # This class contains basic functionality for URIs
@@ -70,9 +69,9 @@ module N
70
69
  @uri_s
71
70
  end
72
71
 
73
- # Same for YAML
72
+ # YAML representation is the uri string
74
73
  def to_yaml
75
- @uri_s
74
+ self.to_s.to_yaml
76
75
  end
77
76
 
78
77
  # Alias "uri" for compatibility with ActiveRDF
@@ -140,7 +139,9 @@ module N
140
139
  type = str.split(separator)
141
140
  type = [type[1]] if(type[0] == "")
142
141
  if(type.size == 2)
143
- self.new(N::URI[type[0]] + type[1])
142
+ namespace = N::URI[type[0]]
143
+ raise(ArgumentError, "Unknown namespace #{type[0]} for #{str}") unless(namespace)
144
+ self.new(namespace + type[1])
144
145
  else
145
146
  default_namespace + type[0]
146
147
  end
@@ -265,20 +266,10 @@ module N
265
266
  URI.active_rdf?
266
267
  end
267
268
 
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
269
  # Gets the rdfs:labels from the rdf store
279
270
  def rdfs_labels
280
271
  if(active_rdf? && is_iri?)
281
- labels = make_res(@uri_s)[(N::RDFS::label).to_s]
272
+ labels = Query.new(N::URI).distinct.select(:label).where(self, N::RDFS.label, :label).execute
282
273
  if(labels && labels.size > 0)
283
274
  labels
284
275
  end # else nil
@@ -302,5 +293,7 @@ module N
302
293
  result
303
294
  end
304
295
 
296
+
297
+
305
298
  end
306
299
  end
@@ -6,6 +6,13 @@ this_dir = File.dirname(File.expand_path(file))
6
6
  $: << this_dir
7
7
  $: << this_dir + '/semantic_naming/'
8
8
 
9
+ require 'rubygems'
10
+ begin
11
+ require 'active_rdf'
12
+ rescue
13
+ puts "ActiveRDF not found"
14
+ end
15
+
9
16
  require 'semantic_naming/uri'
10
17
  require 'semantic_naming/default_namespaces'
11
18
  require 'semantic_naming/namespace'
@@ -43,4 +43,11 @@ class TypeTest < Test::Unit::TestCase
43
43
  types = N::SourceClass.rdf_types.collect { |type| type.uri.to_s }
44
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
45
  end
46
+
47
+ def test_subclass_hierarchy
48
+ hierarchy = N::SourceClass.subclass_hierarchy
49
+ assert_equal({N::RDFTEST.Type1 => { N::RDFTEST.Type2 => {}, N::RDFTEST.Type3 => {} }, N::RDFTEST.Type4 => { N::RDFTEST.Type3 => {}}}, hierarchy)
50
+ end
51
+
52
+
46
53
  end
metadata CHANGED
@@ -1,26 +1,13 @@
1
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: []
2
+ extensions: []
15
3
 
16
- description:
17
- email: hahn@netseven.it
4
+ homepage: http://talia.discovery-project.eu/
18
5
  executables: []
19
6
 
20
- extensions: []
21
-
22
- extra_rdoc_files:
23
- - README.rdoc
7
+ version: !ruby/object:Gem::Version
8
+ version: 2.0.1
9
+ post_install_message:
10
+ date: 2009-11-04 23:00:00 +00:00
24
11
  files:
25
12
  - lib/semantic_naming.rb
26
13
  - lib/semantic_naming/default_namespaces.rb
@@ -29,36 +16,49 @@ files:
29
16
  - lib/semantic_naming/source_class.rb
30
17
  - lib/semantic_naming/uri.rb
31
18
  - README.rdoc
19
+ rubygems_version: 1.3.5
20
+ rdoc_options:
21
+ - --charset=UTF-8
22
+ signing_key:
23
+ cert_chain: []
24
+
25
+ name: semantic_naming
32
26
  has_rdoc: true
33
- homepage: http://talia.discovery-project.eu/
27
+ platform: ruby
28
+ summary: Semantic Naming Extensions for ActiveRDF, Talia and others
29
+ default_executable:
30
+ bindir: bin
34
31
  licenses: []
35
32
 
36
- post_install_message:
37
- rdoc_options:
38
- - --charset=UTF-8
39
- require_paths:
40
- - lib
41
- required_ruby_version: !ruby/object:Gem::Requirement
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ version:
42
35
  requirements:
43
- - - ">="
36
+ - - '>='
44
37
  - !ruby/object:Gem::Version
45
38
  version: "0"
39
+ required_ruby_version: !ruby/object:Gem::Requirement
46
40
  version:
47
- required_rubygems_version: !ruby/object:Gem::Requirement
48
41
  requirements:
49
- - - ">="
42
+ - - '>='
50
43
  - !ruby/object:Gem::Version
51
44
  version: "0"
52
- version:
53
- requirements: []
54
-
55
- rubyforge_project:
56
- rubygems_version: 1.3.5
57
- signing_key:
45
+ require_paths:
46
+ - lib
58
47
  specification_version: 3
59
- summary: Semantic Naming Extensions for ActiveRDF, Talia and others
60
48
  test_files:
61
49
  - test/namespace_test.rb
62
50
  - test/predicate_test.rb
63
51
  - test/source_class_test.rb
64
52
  - test/uri_test.rb
53
+ dependencies: []
54
+
55
+ description:
56
+ email: hahn@netseven.it
57
+ authors:
58
+ - Daniel Hahn
59
+ extra_rdoc_files:
60
+ - README.rdoc
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ autorequire: