semantic_naming 2.0.1 → 2.0.2

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.
@@ -54,9 +54,38 @@ module N
54
54
  qry.where(:class, RDF.type, RDFS.Class)
55
55
  qry.where(:subclass, RDFS.subClassOf, :class)
56
56
  subtype_list = qry.execute
57
+
58
+ build_hierarchy_from(subtype_list, rdf_types)
59
+ end
60
+
61
+ # This works like the subclass_hierarchy method, with the exception that
62
+ #
63
+ # * Ontology information is only used for subtype relations
64
+ # * Resources are considered a "type" if they appear as an rdf:type attribute
65
+ # * Only types that are actively used (that is they appear as an rdf:type attribute)
66
+ # are included
67
+ def self.used_subclass_hierarchy
68
+ all_types_qry = Query.new(SourceClass).distinct.select(:type)
69
+ all_types_qry.where(:element, RDF.type, :type)
70
+ all_types = all_types_qry.execute
71
+
72
+ qry = Query.new(SourceClass).distinct.select(:class, :subclass)
73
+ qry.where(:element, RDF.type, :class)
74
+ qry.where(:other, RDF.type, :subclass)
75
+ qry.where(:subclass, RDFS.subClassOf, :class)
76
+ subtype_list = qry.execute
77
+
78
+ build_hierarchy_from(subtype_list, all_types)
79
+ end
80
+
81
+ private
82
+
83
+ # If all_types is given, it must be a true superset of all
84
+ # types in the query result
85
+ def self.build_hierarchy_from(query_result, all_types = nil)
57
86
  hierarchy = {}
58
87
  # Sift through the triples and add the sub-items
59
- subtype_list.each do |sub_items|
88
+ query_result.each do |sub_items|
60
89
  klass, subklass = sub_items
61
90
  hierarchy[klass] ||= {}
62
91
  hierarchy[klass][subklass] = true
@@ -72,12 +101,17 @@ module N
72
101
  end
73
102
  end
74
103
 
104
+ all_types ||= hierarchy.keys
105
+
75
106
  # Join with the general types and remove the children
76
- types.each do |type|
107
+ all_types.each do |type|
77
108
  xtype = (hierarchy[type] ||= {})
78
109
  hierarchy.delete(type) if(xtype.delete(:is_child))
79
110
  end
80
111
 
112
+ hierarchy.delete(N::RDFS.Class)
113
+ hierarchy.delete(N::OWL.Class) if(defined?(N::OWL))
114
+
81
115
  hierarchy
82
116
  end
83
117
 
@@ -49,5 +49,9 @@ class TypeTest < Test::Unit::TestCase
49
49
  assert_equal({N::RDFTEST.Type1 => { N::RDFTEST.Type2 => {}, N::RDFTEST.Type3 => {} }, N::RDFTEST.Type4 => { N::RDFTEST.Type3 => {}}}, hierarchy)
50
50
  end
51
51
 
52
+ def test_used_subclass_hierarchy
53
+ hierarchy = N::SourceClass.used_subclass_hierarchy
54
+ assert_equal({N::RDFTEST.Type1 => { N::RDFTEST.Type2 => {}}}, hierarchy)
55
+ end
52
56
 
53
57
  end
metadata CHANGED
@@ -5,7 +5,7 @@ homepage: http://talia.discovery-project.eu/
5
5
  executables: []
6
6
 
7
7
  version: !ruby/object:Gem::Version
8
- version: 2.0.1
8
+ version: 2.0.2
9
9
  post_install_message:
10
10
  date: 2009-11-04 23:00:00 +00:00
11
11
  files: