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.
- data/lib/semantic_naming/source_class.rb +36 -2
- data/test/source_class_test.rb +4 -0
- metadata +1 -1
@@ -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
|
-
|
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
|
-
|
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
|
|
data/test/source_class_test.rb
CHANGED
@@ -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
|