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.
- data/CHANGELOG +63 -0
- data/activerdf-jena/lib/activerdf_jena/jena.rb +4 -4
- data/activerdf-jena/lib/activerdf_jena/jena_adapter.rb +55 -55
- data/activerdf-jena/lib/activerdf_jena/lucene.rb +1 -1
- data/activerdf-jena/lib/activerdf_jena/ng4j.rb +7 -7
- data/activerdf-jena/lib/activerdf_jena/ng4j_adapter.rb +47 -47
- data/activerdf-jena/lib/activerdf_jena/pellet.rb +1 -1
- data/activerdf-jena/test/test_jena_adapter.rb +121 -120
- data/activerdf-jena/test/test_ng4j_adapter.rb +111 -110
- data/activerdf-rdflite/lib/activerdf_rdflite/fetching.rb +23 -19
- data/activerdf-rdflite/lib/activerdf_rdflite/rdflite.rb +153 -277
- data/activerdf-rdflite/lib/activerdf_rdflite/suggesting.rb +2 -2
- data/activerdf-rdflite/test/test_fetching.rb +7 -22
- data/activerdf-rdflite/test/test_rdflite.rb +44 -257
- data/activerdf-redland/lib/activerdf_redland/redland.rb +246 -282
- data/activerdf-redland/test/test_redland_adapter.rb +62 -224
- data/activerdf-sesame/ext/wrapper-sesame2.jar +0 -0
- data/activerdf-sesame/java/build.number +2 -2
- data/activerdf-sesame/java/build.xml +0 -0
- data/activerdf-sesame/java/lib/junit-3.8.2.jar +0 -0
- data/activerdf-sesame/java/settings.xml +0 -0
- data/activerdf-sesame/java/src/org/activerdf/wrapper/sesame2/WrapperForSesame2.java +0 -0
- data/activerdf-sesame/java/temp/build/org/activerdf/wrapper/sesame2/WrapperForSesame2.class +0 -0
- data/activerdf-sesame/java/temp/manifest/MANIFEST.MF +2 -2
- data/activerdf-sesame/java/test-src/org/activerdf/wrapper/sesame2/TestWrapperForSesame2.java +0 -0
- data/activerdf-sesame/lib/activerdf_sesame/sesame.rb +360 -364
- data/activerdf-sesame/test/test_sesame_adapter.rb +85 -83
- data/activerdf-sparql/lib/activerdf_sparql/sparql.rb +147 -148
- data/activerdf-sparql/lib/activerdf_sparql/sparql_result_parser.rb +5 -5
- data/activerdf-sparql/test/test_sparql_adapter.rb +2 -0
- data/activerdf-yars/lib/activerdf_yars/jars2.rb +85 -83
- data/lib/active_rdf/federation/active_rdf_adapter.rb +26 -39
- data/lib/active_rdf/federation/connection_pool.rb +119 -110
- data/lib/active_rdf/federation/federation_manager.rb +51 -51
- data/lib/active_rdf/objectmanager/bnode.rb +8 -2
- data/lib/active_rdf/objectmanager/literal.rb +81 -50
- data/lib/active_rdf/objectmanager/namespace.rb +117 -84
- data/lib/active_rdf/objectmanager/object_manager.rb +101 -96
- data/lib/active_rdf/objectmanager/ordered_set.rb +1 -1
- data/lib/active_rdf/objectmanager/property.rb +345 -0
- data/lib/active_rdf/objectmanager/property_list.rb +4 -4
- data/lib/active_rdf/objectmanager/property_lookup.rb +57 -0
- data/lib/active_rdf/objectmanager/resource.rb +293 -501
- data/lib/active_rdf/objectmanager/resource_like.rb +2 -2
- data/lib/active_rdf/objectmanager/resource_query.rb +85 -0
- data/lib/active_rdf/queryengine/ntriples_parser.rb +75 -68
- data/lib/active_rdf/queryengine/query.rb +237 -183
- data/lib/active_rdf/queryengine/query2jars2.rb +17 -15
- data/lib/active_rdf/queryengine/query2sparql.rb +107 -101
- data/lib/active_rdf.rb +28 -17
- data/lib/active_rdf_helpers.rb +37 -5
- data/lib/active_rdf_log.rb +11 -11
- data/test/adapters/test_activerdf_adapter.rb +138 -0
- data/test/{test_adapters.rb → adapters/test_adapters.rb} +6 -24
- data/test/adapters/test_bnode_capable_adapter.rb +31 -0
- data/test/adapters/test_context_aware_adapter.rb +31 -0
- data/test/adapters/test_network_aware_adapter.rb +29 -0
- data/test/adapters/test_persistent_adapter.rb +21 -0
- data/test/adapters/test_read_only_adapter.rb +15 -0
- data/test/adapters/test_reasoning_adapter.rb +11 -0
- data/test/adapters/test_writable_adapter.rb +163 -0
- data/test/common.rb +78 -96
- data/test/federation/test_connection_pool.rb +25 -44
- data/test/federation/test_federation_manager.rb +45 -45
- data/test/objectmanager/test_literal.rb +47 -26
- data/test/objectmanager/test_namespace.rb +3 -1
- data/test/objectmanager/test_object_manager.rb +35 -45
- data/test/objectmanager/test_ordered_set.rb +1 -1
- data/test/objectmanager/test_property.rb +261 -0
- data/test/objectmanager/test_resource_reading.rb +196 -104
- data/test/objectmanager/test_resource_reasoning.rb +26 -0
- data/test/objectmanager/test_resource_writing.rb +34 -25
- data/test/queryengine/my_external_resource.rb +5 -1
- data/test/queryengine/test_external_resource_class.rb +1 -8
- data/test/queryengine/test_ntriples_parser.rb +5 -3
- data/test/queryengine/test_query.rb +3 -3
- data/test/queryengine/test_query2jars2.rb +2 -2
- data/test/queryengine/test_query2sparql.rb +2 -2
- data/test/queryengine/test_query_engine.rb +46 -28
- metadata +16 -8
- data/activerdf-rdflite/test/test_bnode_data.nt +0 -5
- data/activerdf-rdflite/test/test_data.nt +0 -32
- data/activerdf-rdflite/test/test_escaped_data.nt +0 -2
- data/activerdf-redland/test/test_person_data.nt +0 -42
- 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
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
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
|
65
|
-
if
|
66
|
-
|
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
|
-
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
34
|
+
# returns currently registered namespace abbreviations (e.g. :foaf, :rdf)
|
35
|
+
def Namespace.abbreviations
|
36
|
+
@@namespaces.keys
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
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
|
-
#
|
44
|
-
|
45
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
#
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
73
|
-
|
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
|
-
|
76
|
-
|
107
|
+
module NamespaceProxy
|
108
|
+
attr_accessor :prefix, :klasses, :resources
|
77
109
|
|
78
|
-
|
79
|
-
|
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
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
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
|
-
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
56
|
-
|
59
|
+
end
|
60
|
+
klassname = localname_to_class(localname)
|
57
61
|
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
-
|
63
|
-
|
66
|
+
Object.const_get(modulename.to_sym)
|
67
|
+
else
|
64
68
|
ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: defining module name #{modulename} now" }
|
65
|
-
|
66
|
-
|
69
|
+
Object.const_set(modulename, Module.new)
|
70
|
+
end
|
67
71
|
|
68
|
-
|
69
|
-
|
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
|
-
|
72
|
-
|
73
|
-
|
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
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
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
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
+
def ObjectManager.prefix_to_module(prefix)
|
88
|
+
# TODO: remove illegal characters
|
89
|
+
prefix.to_s.upcase
|
90
|
+
end
|
87
91
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
95
|
-
|
98
|
+
def ObjectManager.create_module_name(resource)
|
99
|
+
# TODO: write unit test to verify replacement of all illegal characters
|
96
100
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
+
# extract non-local part (including delimiter)
|
102
|
+
uri = resource.uri
|
103
|
+
delimiter = uri.rindex(/#|\//)
|
104
|
+
nonlocal = uri[0..delimiter]
|
101
105
|
|
102
|
-
|
103
|
-
|
104
|
-
|
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
|
-
|
107
|
-
|
108
|
-
|
110
|
+
# replace illegal chars within the uri
|
111
|
+
replace_illegal_chars(cleaned_non_local).upcase
|
112
|
+
end
|
109
113
|
|
110
|
-
|
111
|
-
|
112
|
-
|
114
|
+
def ObjectManager.replace_illegal_chars(name)
|
115
|
+
name.gsub(/[^a-zA-Z0-9]+/, '_')
|
116
|
+
end
|
113
117
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
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).
|
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] }
|