activerdf_net7 1.6.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. data/CHANGELOG +79 -0
  2. data/LICENSE +504 -0
  3. data/README.rdoc +34 -0
  4. data/activerdf-jena/CHANGELOG +14 -0
  5. data/activerdf-jena/LICENSE +504 -0
  6. data/activerdf-jena/README +57 -0
  7. data/activerdf-jena/Rakefile +87 -0
  8. data/activerdf-jena/TODO +18 -0
  9. data/activerdf-jena/VERSION +1 -0
  10. data/activerdf-jena/ext/antlr-2.7.5.jar +0 -0
  11. data/activerdf-jena/ext/arq-extra.jar +0 -0
  12. data/activerdf-jena/ext/arq.jar +0 -0
  13. data/activerdf-jena/ext/aterm-java-1.6.jar +0 -0
  14. data/activerdf-jena/ext/commons-logging-1.1.jar +0 -0
  15. data/activerdf-jena/ext/concurrent.jar +0 -0
  16. data/activerdf-jena/ext/icu4j_3_4.jar +0 -0
  17. data/activerdf-jena/ext/iri.jar +0 -0
  18. data/activerdf-jena/ext/jena.jar +0 -0
  19. data/activerdf-jena/ext/jenatest.jar +0 -0
  20. data/activerdf-jena/ext/json.jar +0 -0
  21. data/activerdf-jena/ext/junit.jar +0 -0
  22. data/activerdf-jena/ext/log4j-1.2.12.jar +0 -0
  23. data/activerdf-jena/ext/lucene-core-2.0.0.jar +0 -0
  24. data/activerdf-jena/ext/ng4j.jar +0 -0
  25. data/activerdf-jena/ext/pellet.jar +0 -0
  26. data/activerdf-jena/ext/relaxngDatatype.jar +0 -0
  27. data/activerdf-jena/ext/stax-api-1.0.jar +0 -0
  28. data/activerdf-jena/ext/wstx-asl-3.0.0.jar +0 -0
  29. data/activerdf-jena/ext/xercesImpl.jar +0 -0
  30. data/activerdf-jena/ext/xml-apis.jar +0 -0
  31. data/activerdf-jena/ext/xsdlib.jar +0 -0
  32. data/activerdf-jena/lib/activerdf_jena/init.rb +26 -0
  33. data/activerdf-jena/lib/activerdf_jena/jena.rb +59 -0
  34. data/activerdf-jena/lib/activerdf_jena/jena_adapter.rb +515 -0
  35. data/activerdf-jena/lib/activerdf_jena/lucene.rb +22 -0
  36. data/activerdf-jena/lib/activerdf_jena/ng4j.rb +50 -0
  37. data/activerdf-jena/lib/activerdf_jena/ng4j_adapter.rb +379 -0
  38. data/activerdf-jena/lib/activerdf_jena/pellet.rb +25 -0
  39. data/activerdf-jena/test/bnode_org_rss.rdf +793 -0
  40. data/activerdf-jena/test/eyal-foaf.nt +39 -0
  41. data/activerdf-jena/test/fun_with_bnodes.nt +2 -0
  42. data/activerdf-jena/test/s1.n3 +18 -0
  43. data/activerdf-jena/test/test_data.nt +32 -0
  44. data/activerdf-jena/test/test_jena_adapter.rb +451 -0
  45. data/activerdf-jena/test/test_ng4j_adapter.rb +354 -0
  46. data/activerdf-rdflite/CHANGELOG +31 -0
  47. data/activerdf-rdflite/LICENSE +504 -0
  48. data/activerdf-rdflite/README +16 -0
  49. data/activerdf-rdflite/Rakefile +73 -0
  50. data/activerdf-rdflite/VERSION +1 -0
  51. data/activerdf-rdflite/lib/activerdf_rdflite/fetching.rb +34 -0
  52. data/activerdf-rdflite/lib/activerdf_rdflite/init.rb +13 -0
  53. data/activerdf-rdflite/lib/activerdf_rdflite/rdflite.rb +582 -0
  54. data/activerdf-rdflite/lib/activerdf_rdflite/suggesting.rb +87 -0
  55. data/activerdf-rdflite/test/test_bnode_data.nt +5 -0
  56. data/activerdf-rdflite/test/test_data.nt +32 -0
  57. data/activerdf-rdflite/test/test_escaped_data.nt +2 -0
  58. data/activerdf-rdflite/test/test_fetching.rb +33 -0
  59. data/activerdf-rdflite/test/test_rdflite.rb +277 -0
  60. data/activerdf-redland/CHANGELOG +12 -0
  61. data/activerdf-redland/LICENSE +504 -0
  62. data/activerdf-redland/README +9 -0
  63. data/activerdf-redland/Rakefile +72 -0
  64. data/activerdf-redland/VERSION +1 -0
  65. data/activerdf-redland/lib/activerdf_redland/init.rb +10 -0
  66. data/activerdf-redland/lib/activerdf_redland/redland.rb +362 -0
  67. data/activerdf-redland/test/test_person_data.nt +42 -0
  68. data/activerdf-redland/test/test_redland_adapter.rb +242 -0
  69. data/activerdf-sesame/CHANGELOG +6 -0
  70. data/activerdf-sesame/LICENSE +10 -0
  71. data/activerdf-sesame/LICENSE-aduna +10 -0
  72. data/activerdf-sesame/LICENSE-lgpl +504 -0
  73. data/activerdf-sesame/README +33 -0
  74. data/activerdf-sesame/Rakefile +77 -0
  75. data/activerdf-sesame/VERSION +1 -0
  76. data/activerdf-sesame/ext/commons-codec-1.3.jar +0 -0
  77. data/activerdf-sesame/ext/commons-dbcp-1.2.2.jar +0 -0
  78. data/activerdf-sesame/ext/commons-httpclient-3.1.jar +0 -0
  79. data/activerdf-sesame/ext/commons-logging-1.1.1.jar +0 -0
  80. data/activerdf-sesame/ext/commons-pool-1.3.jar +0 -0
  81. data/activerdf-sesame/ext/commons-pool-1.5.2.jar +0 -0
  82. data/activerdf-sesame/ext/junit-3.8.2.jar +0 -0
  83. data/activerdf-sesame/ext/openrdf-sesame-2.0-onejar.jar +0 -0
  84. data/activerdf-sesame/ext/openrdf-sesame-2.3-pr1-onejar.jar +0 -0
  85. data/activerdf-sesame/ext/slf4j-api-1.4.3.jar +0 -0
  86. data/activerdf-sesame/ext/slf4j-nop-1.4.3.jar +0 -0
  87. data/activerdf-sesame/ext/wrapper-sesame2.jar +0 -0
  88. data/activerdf-sesame/java/build.number +3 -0
  89. data/activerdf-sesame/java/build.xml +313 -0
  90. data/activerdf-sesame/java/javadoc/allclasses-frame.html +31 -0
  91. data/activerdf-sesame/java/javadoc/allclasses-noframe.html +31 -0
  92. data/activerdf-sesame/java/javadoc/constant-values.html +146 -0
  93. data/activerdf-sesame/java/javadoc/deprecated-list.html +146 -0
  94. data/activerdf-sesame/java/javadoc/help-doc.html +223 -0
  95. data/activerdf-sesame/java/javadoc/index-files/index-1.html +150 -0
  96. data/activerdf-sesame/java/javadoc/index-files/index-10.html +145 -0
  97. data/activerdf-sesame/java/javadoc/index-files/index-2.html +157 -0
  98. data/activerdf-sesame/java/javadoc/index-files/index-3.html +146 -0
  99. data/activerdf-sesame/java/javadoc/index-files/index-4.html +145 -0
  100. data/activerdf-sesame/java/javadoc/index-files/index-5.html +145 -0
  101. data/activerdf-sesame/java/javadoc/index-files/index-6.html +142 -0
  102. data/activerdf-sesame/java/javadoc/index-files/index-7.html +145 -0
  103. data/activerdf-sesame/java/javadoc/index-files/index-8.html +152 -0
  104. data/activerdf-sesame/java/javadoc/index-files/index-9.html +146 -0
  105. data/activerdf-sesame/java/javadoc/index.html +36 -0
  106. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/WrapperForSesame2.html +665 -0
  107. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/class-use/WrapperForSesame2.html +144 -0
  108. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/package-frame.html +32 -0
  109. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/package-summary.html +157 -0
  110. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/package-tree.html +150 -0
  111. data/activerdf-sesame/java/javadoc/org/activerdf/wrapper/sesame2/package-use.html +144 -0
  112. data/activerdf-sesame/java/javadoc/overview-summary.html +156 -0
  113. data/activerdf-sesame/java/javadoc/overview-tree.html +152 -0
  114. data/activerdf-sesame/java/javadoc/package-list +1 -0
  115. data/activerdf-sesame/java/javadoc/resources/inherit.gif +0 -0
  116. data/activerdf-sesame/java/javadoc/stylesheet.css +29 -0
  117. data/activerdf-sesame/java/lib/commons-codec-1.3.jar +0 -0
  118. data/activerdf-sesame/java/lib/commons-dbcp-1.2.2.jar +0 -0
  119. data/activerdf-sesame/java/lib/commons-httpclient-3.1.jar +0 -0
  120. data/activerdf-sesame/java/lib/commons-logging-1.1.1.jar +0 -0
  121. data/activerdf-sesame/java/lib/commons-pool-1.3.jar +0 -0
  122. data/activerdf-sesame/java/lib/commons-pool-1.5.2.jar +0 -0
  123. data/activerdf-sesame/java/lib/junit-3.8.2.jar +0 -0
  124. data/activerdf-sesame/java/lib/openrdf-sesame-2.0-onejar.jar +0 -0
  125. data/activerdf-sesame/java/lib/openrdf-sesame-2.3-pr1-onejar.jar +0 -0
  126. data/activerdf-sesame/java/lib/slf4j-api-1.4.3.jar +0 -0
  127. data/activerdf-sesame/java/lib/slf4j-nop-1.4.3.jar +0 -0
  128. data/activerdf-sesame/java/manifest.mf +3 -0
  129. data/activerdf-sesame/java/settings.xml +135 -0
  130. data/activerdf-sesame/java/src/org/activerdf/wrapper/sesame2/WrapperForSesame2.java +145 -0
  131. data/activerdf-sesame/java/test-src/org/activerdf/wrapper/sesame2/TestWrapperForSesame2.java +41 -0
  132. data/activerdf-sesame/lib/activerdf_sesame/init.rb +11 -0
  133. data/activerdf-sesame/lib/activerdf_sesame/sesame.rb +400 -0
  134. data/activerdf-sesame/test/eyal-foaf.nt +39 -0
  135. data/activerdf-sesame/test/eyal-foaf.rdf +65 -0
  136. data/activerdf-sesame/test/test_sesame_adapter.rb +341 -0
  137. data/activerdf-sparql/CHANGELOG +35 -0
  138. data/activerdf-sparql/LICENSE +504 -0
  139. data/activerdf-sparql/README +10 -0
  140. data/activerdf-sparql/Rakefile +78 -0
  141. data/activerdf-sparql/VERSION +1 -0
  142. data/activerdf-sparql/lib/activerdf_sparql/init.rb +10 -0
  143. data/activerdf-sparql/lib/activerdf_sparql/sparql.rb +212 -0
  144. data/activerdf-sparql/lib/activerdf_sparql/sparql_result_parser.rb +55 -0
  145. data/activerdf-sparql/test/test_sparql_adapter.rb +108 -0
  146. data/activerdf-yars/LICENSE +504 -0
  147. data/activerdf-yars/README +10 -0
  148. data/activerdf-yars/Rakefile +38 -0
  149. data/activerdf-yars/lib/activerdf_yars/init.rb +10 -0
  150. data/activerdf-yars/lib/activerdf_yars/jars2.rb +119 -0
  151. data/lib/active_rdf.rb +85 -0
  152. data/lib/active_rdf/directaccess/direct_access.rb +49 -0
  153. data/lib/active_rdf/federation/active_rdf_adapter.rb +47 -0
  154. data/lib/active_rdf/federation/connection_pool.rb +156 -0
  155. data/lib/active_rdf/federation/federation_manager.rb +112 -0
  156. data/lib/active_rdf/instance_exec.rb +13 -0
  157. data/lib/active_rdf/objectmanager/bnode.rb +7 -0
  158. data/lib/active_rdf/objectmanager/literal.rb +71 -0
  159. data/lib/active_rdf/objectmanager/namespace.rb +106 -0
  160. data/lib/active_rdf/objectmanager/object_manager.rb +119 -0
  161. data/lib/active_rdf/objectmanager/ordered_set.rb +116 -0
  162. data/lib/active_rdf/objectmanager/property_list.rb +76 -0
  163. data/lib/active_rdf/objectmanager/resource.rb +609 -0
  164. data/lib/active_rdf/objectmanager/resource_like.rb +28 -0
  165. data/lib/active_rdf/queryengine/ntriples_parser.rb +90 -0
  166. data/lib/active_rdf/queryengine/query.rb +245 -0
  167. data/lib/active_rdf/queryengine/query2jars2.rb +22 -0
  168. data/lib/active_rdf/queryengine/query2sparql.rb +139 -0
  169. data/lib/active_rdf_helpers.rb +30 -0
  170. data/lib/active_rdf_log.rb +100 -0
  171. data/test/common.rb +119 -0
  172. data/test/directaccess/test_direct_access.rb +64 -0
  173. data/test/federation/test_connection_pool.rb +86 -0
  174. data/test/federation/test_federation_manager.rb +145 -0
  175. data/test/objectmanager/test_literal.rb +52 -0
  176. data/test/objectmanager/test_namespace.rb +83 -0
  177. data/test/objectmanager/test_object_manager.rb +96 -0
  178. data/test/objectmanager/test_ordered_set.rb +110 -0
  179. data/test/objectmanager/test_resource_reading.rb +150 -0
  180. data/test/objectmanager/test_resource_writing.rb +39 -0
  181. data/test/objectmanager/test_talia_syntax.rb +68 -0
  182. data/test/queryengine/my_external_resource.rb +24 -0
  183. data/test/queryengine/test_external_resource_class.rb +49 -0
  184. data/test/queryengine/test_ntriples_parser.rb +71 -0
  185. data/test/queryengine/test_query.rb +55 -0
  186. data/test/queryengine/test_query2jars2.rb +51 -0
  187. data/test/queryengine/test_query2sparql.rb +76 -0
  188. data/test/queryengine/test_query_engine.rb +52 -0
  189. data/test/test_adapters.rb +58 -0
  190. metadata +266 -0
@@ -0,0 +1,112 @@
1
+ require 'federation/connection_pool'
2
+
3
+ include ActiveRdfBenchmark
4
+
5
+ # Manages the federation of datasources: distributes queries to right
6
+ # datasources and merges their results
7
+
8
+ class FederationManager
9
+ # add triple s,p,o (context is optional) to the currently selected write-adapter
10
+ def FederationManager.add(s,p,o,c=nil)
11
+ benchmark("SPARQL/RDF", Logger::DEBUG) do |bench_message|
12
+ bench_message << "ADD #{s} - #{p} - #{o} : #{c}" if(bench_message)
13
+ # TODO: allow addition of full graphs
14
+ raise ActiveRdfError, "cannot write without a write-adapter" unless ConnectionPool.write_adapter
15
+ ConnectionPool.write_adapter.add(s,p,o,c)
16
+ end
17
+ end
18
+
19
+ # delete triple s,p,o (context is optional) to the currently selected write-adapter
20
+ def FederationManager.delete(s,p,o,c=nil)
21
+ benchmark("SPARQL/RDF", Logger::DEBUG) do |bench_message|
22
+ bench_message << "DELETE #{s} - #{p} - #{o} : #{c}" if(bench_message)
23
+ raise ActiveRdfError, "cannot write without a write-adapter" unless ConnectionPool.write_adapter
24
+ # transform wildcard symbols to nil (for the adaptors)
25
+ s = nil if s.is_a? Symbol
26
+ p = nil if p.is_a? Symbol
27
+ o = nil if o.is_a? Symbol
28
+ ConnectionPool.write_adapter.delete(s,p,o,c)
29
+ end
30
+ end
31
+
32
+ # delete every triples about a specified resource
33
+ def FederationManager.delete_all(resource)
34
+ delete(resource, nil, nil)
35
+ end
36
+
37
+ # Clear the store (or the given context)
38
+ def FederationManager.clear(context=nil)
39
+ # FIXME: Make sure that all adapters support clearing
40
+ raise(RuntimeError, "Adapter #{ConnectionPool.write_adapter.class} doesn't support clear") unless(ConnectionPool.write_adapter.respond_to?(:clear))
41
+ ConnectionPool.write_adapter.clear(context)
42
+ end
43
+
44
+ # executes read-only queries
45
+ # by distributing query over complete read-pool
46
+ # and aggregating the results
47
+ def FederationManager.query(q, options={:flatten => true, :result_format => nil})
48
+ if ConnectionPool.read_adapters.empty?
49
+ raise ActiveRdfError, "cannot execute query without data sources"
50
+ end
51
+
52
+ benchmark("SPARQL/RDF", Logger::DEBUG) do |bench_message|
53
+ # Only build the benchmark message if we need it
54
+ bench_message << ((q.class == String) ? q : q.to_sp) if(bench_message)
55
+ # ask each adapter for query results
56
+ # and yield them consequtively
57
+ if block_given?
58
+ ConnectionPool.read_adapters.each do |source|
59
+ source.query(q) do |*clauses|
60
+ yield(*clauses)
61
+ end
62
+ end
63
+ else
64
+ # build Array of results from all sources
65
+ # TODO: write test for sebastian's select problem
66
+ # (without distinct, should get duplicates, they
67
+ # were filtered out when doing results.union)
68
+ # FIXME: Calling get_sparql_query_results? This *only* exists on the redland adapter
69
+ results = []
70
+ ConnectionPool.read_adapters.each do |source|
71
+ if (q.class != String)
72
+ source_results = source.query(q)
73
+ else
74
+ source_results = source.get_sparql_query_results(q, RDFS::Resource, options[:result_format])
75
+ end
76
+ source_results.each do |clauses|
77
+ results << clauses
78
+ end
79
+ end
80
+
81
+ # filter the empty results
82
+ results.reject {|ary| ary.empty? }
83
+
84
+ # remove duplicate results from multiple
85
+ # adapters if asked for distinct query
86
+ # (adapters return only distinct results,
87
+ # but they cannot check duplicates against each other)
88
+ results.uniq! if ((q.class != String) && (q.distinct?))
89
+
90
+ # flatten results array if only one select clause
91
+ # to prevent unnecessarily nested array [[eyal],[renaud],...]
92
+ if (q.class != String)
93
+ results.flatten! if (q.select_clauses.size == 1 or q.ask?)
94
+ else
95
+ results.flatten! if q.scan(/[?]/).length == 2
96
+ end
97
+
98
+ # remove array (return single value or nil) if asked to
99
+ if options[:flatten] or ((q.class != String) && (q.count?))
100
+ case results.size
101
+ when 0
102
+ results = nil
103
+ when 1
104
+ results = results.first
105
+ end
106
+ end
107
+ end
108
+
109
+ results
110
+ end # End benchmark
111
+ end # End query
112
+ end
@@ -0,0 +1,13 @@
1
+ module Kernel
2
+ # Like instace_eval but allows parameters to be passed.
3
+ def instance_exec(*args, &block)
4
+ mname = "__instance_exec_#{Thread.current.object_id.abs}_#{object_id.abs}"
5
+ Object.class_eval{ define_method(mname, &block) }
6
+ begin
7
+ ret = send(mname, *args)
8
+ ensure
9
+ Object.class_eval{ undef_method(mname) } rescue nil
10
+ end
11
+ ret
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ module RDFS
2
+ class BNode < Resource
3
+ def to_s
4
+ "<_:#{uri}>"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,71 @@
1
+ # require 'active_rdf'
2
+ require 'time'
3
+
4
+ module Literal
5
+ Namespace.register :xsd, 'http://www.w3.org/2001/XMLSchema#'
6
+ def xsd_type
7
+ case self
8
+ when String
9
+ XSD::string
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
18
+ end
19
+ end
20
+
21
+ def self.typed(value, type)
22
+ case type
23
+ when XSD::string
24
+ String.new(value)
25
+ when XSD::date
26
+ DateTime.parse(value)
27
+ when XSD::boolean
28
+ value == 'true' or value == 1
29
+ when XSD::integer
30
+ value.to_i
31
+ when XSD::double
32
+ value.to_f
33
+ end
34
+ end
35
+
36
+ def to_ntriple
37
+ if $activerdf_without_xsdtype
38
+ "\"#{to_s}\""
39
+ else
40
+ "\"#{to_s}\"^^#{xsd_type}"
41
+ end
42
+ end
43
+ end
44
+
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
53
+
54
+ class LocalizedString < String
55
+ include Literal
56
+ attr_reader :lang
57
+ def initialize value, lang=nil
58
+ super(value)
59
+
60
+ @lang = lang
61
+ @lang = lang[1..-1] if @lang[0..0] == '@'
62
+ end
63
+
64
+ def to_ntriple
65
+ if @lang
66
+ "\"#{to_s}\"@#@lang"
67
+ else
68
+ super
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,106 @@
1
+ # require 'active_rdf'
2
+
3
+ # Manages namespace abbreviations and expansions
4
+
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 self.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
+ ActiveRdfLogger::log_info(self) { "Namespace: registering #{fullURI} to #{prefix}" }
15
+ @@namespaces[prefix.to_sym] = fullURI.to_s
16
+ @@inverted_namespaces[fullURI.to_s] = prefix.to_sym
17
+
18
+ # enable namespace lookups through FOAF::name
19
+ # if FOAF defined, add to it
20
+ if Object.const_defined?(prefix.to_s.upcase)
21
+ ns = Object.const_get(prefix.to_s.upcase)
22
+ else
23
+ # otherwise create a new module for it
24
+ ns = Module.new
25
+ Object.const_set(prefix.to_s.upcase, ns)
26
+ end
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)
32
+ end
33
+
34
+ def const_missing(klass)
35
+ ObjectManager.construct_class(Namespace.lookup(self.to_s.downcase.to_sym, klass))
36
+ end
37
+
38
+ # make some builtin methods private because lookup doesn't work otherwise
39
+ # on e.g. RDF::type and FOAF::name
40
+ [:type, :name, :id].each {|m| private(m) }
41
+ end
42
+
43
+ # return the namespace proxy object
44
+ ns
45
+ end
46
+
47
+ # returns a resource whose URI is formed by concatenation of prefix and localname
48
+ def self.lookup(prefix, localname, resource_type = RDFS::Resource)
49
+ resource_type.new(expand(prefix, localname))
50
+ end
51
+
52
+ # returns URI (string) formed by concatenation of prefix and localname
53
+ def self.expand(prefix, localname)
54
+ @@namespaces[prefix.to_sym].to_s + localname.to_s
55
+ end
56
+
57
+ # returns prefix (if known) for the non-local part of the URI,
58
+ # or nil if prefix not registered
59
+ def self.prefix(resource)
60
+ # get string representation of resource uri
61
+ uri = if(resource.respond_to?(:uri))
62
+ resource.uri.to_s
63
+ else
64
+ resource.to_s
65
+ end
66
+
67
+ # uri.to_s gives us the uri of the resource (if resource given)
68
+ # then we find the last occurrence of # or / (heuristical namespace
69
+ # delimitor)
70
+ delimiter = uri.rindex(/#|\//)
71
+
72
+ # if delimiter not found, URI cannot be split into (non)local-part
73
+ return uri if delimiter.nil?
74
+
75
+ # extract non-local part (including delimiter)
76
+ nonlocal = uri[0..delimiter]
77
+
78
+ @@inverted_namespaces[nonlocal]
79
+ end
80
+
81
+ # returns local-part of URI
82
+ def self.localname(resource)
83
+ # get string representation of resource uri
84
+ uri = if(resource.respond_to?(:uri))
85
+ resource.uri.to_s
86
+ else
87
+ resource.to_s
88
+ end
89
+
90
+ delimiter = uri.rindex(/#|\//)
91
+ if delimiter.nil? or delimiter == uri.size-1
92
+ uri
93
+ else
94
+ uri[delimiter+1..-1]
95
+ end
96
+ end
97
+
98
+ # returns currently registered namespace abbreviations (e.g. :foaf, :rdf)
99
+ def self.abbreviations
100
+ @@namespaces.keys
101
+ end
102
+ end
103
+
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#')
@@ -0,0 +1,119 @@
1
+ # require 'active_rdf'
2
+
3
+ # Constructs Ruby classes for RDFS classes (in the right namespace)
4
+
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 self.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
+ # find everything defined as rdfs:class or owl:class
15
+ type = Namespace.lookup(:rdf,:type)
16
+ rdfsklass = Namespace.lookup(:rdfs,:Class)
17
+
18
+ # TODO: we should not do this, we should not support OWL
19
+ # instead, owl:Class is defined as subclass-of rdfs:Class, so if the
20
+ # reasoner has access to owl definition it should work out fine.
21
+ owlklass = Namespace.lookup(:owl,:Class)
22
+
23
+ klasses = []
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
30
+ ActiveRdfLogger::log_debug(self) { "Construct_classes: classes found: #{klasses}" }
31
+
32
+ # then we construct a Ruby class for each found rdfs:class
33
+ # and return the set of all constructed classes
34
+ klasses.collect { |t| construct_class(t) }
35
+ end
36
+
37
+ # constructs Ruby class for the given resource (and puts it into the module as
38
+ # defined by the registered namespace abbreviations)
39
+ def self.construct_class(resource)
40
+ # get prefix abbreviation and localname from type
41
+ # e.g. :foaf and Person
42
+ localname = Namespace.localname(resource)
43
+ prefix = Namespace.prefix(resource)
44
+
45
+ # find (ruby-acceptable) names for the module and class
46
+ # e.g. FOAF and Person
47
+ if prefix.nil?
48
+ # if the prefix is unknown, we create our own from the full URI
49
+ modulename = create_module_name(resource)
50
+ ActiveRdfLogger::log_debug(self) { "Construct_class: constructing modulename #{modulename} from URI #{resource}" }
51
+ else
52
+ # otherwise we convert the registered prefix into a module name
53
+ modulename = prefix_to_module(prefix)
54
+ ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: constructing modulename #{modulename} from registered prefix #{prefix}" }
55
+ end
56
+ klassname = localname_to_class(localname)
57
+
58
+ # look whether module defined
59
+ # else: create it
60
+ _module = if Object.const_defined?(modulename.to_sym)
61
+ ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: module name #{modulename} previously defined" }
62
+ Object.const_get(modulename.to_sym)
63
+ else
64
+ ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: defining module name #{modulename} now" }
65
+ Object.const_set(modulename, Module.new)
66
+ end
67
+
68
+ # look whether class defined in that module
69
+ if _module.const_defined?(klassname.to_sym)
70
+ ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: given class #{klassname} defined in the module" }
71
+ # if so, return the existing class
72
+ _module.const_get(klassname.to_sym)
73
+ else
74
+ ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: creating given class #{klassname}" }
75
+ # otherwise: create it, inside that module, as subclass of RDFS::Resource
76
+ # (using toplevel Class.new to prevent RDFS::Class.new from being called)
77
+ klass = _module.module_eval("#{klassname} = Object::Class.new(RDFS::Resource)")
78
+ klass.class_uri = resource
79
+ klass
80
+ end
81
+ end
82
+
83
+ def self.prefix_to_module(prefix)
84
+ # TODO: remove illegal characters
85
+ prefix.to_s.upcase
86
+ end
87
+
88
+ def self.localname_to_class(localname)
89
+ # replace illegal characters inside the uri
90
+ # and capitalize the classname
91
+ replace_illegal_chars(localname).capitalize
92
+ end
93
+
94
+ def self.create_module_name(resource)
95
+ # TODO: write unit test to verify replacement of all illegal characters
96
+
97
+ # extract non-local part (including delimiter)
98
+ uri = resource.uri
99
+ delimiter = uri.rindex(/#|\//)
100
+ nonlocal = uri[0..delimiter]
101
+
102
+ # remove illegal characters appearing at the end of the uri (e.g. trailing
103
+ # slash)
104
+ cleaned_non_local = nonlocal.gsub(/[^a-zA-Z0-9]+$/, '')
105
+
106
+ # replace illegal chars within the uri
107
+ replace_illegal_chars(cleaned_non_local).upcase
108
+ end
109
+
110
+ def self.replace_illegal_chars(name)
111
+ name.gsub(/[^a-zA-Z0-9]+/, '_')
112
+ end
113
+
114
+ #declare the class level methods as private with these directives
115
+ private_class_method :prefix_to_module
116
+ private_class_method :localname_to_class
117
+ private_class_method :create_module_name
118
+ private_class_method :replace_illegal_chars
119
+ end
@@ -0,0 +1,116 @@
1
+ # require 'active_rdf'
2
+ require 'queryengine/query'
3
+ require 'objectmanager/resource'
4
+
5
+ class OrderedSet
6
+
7
+ # the uri of current resource
8
+ attr_reader :uri
9
+
10
+ # Initialize SeqContainer
11
+ def initialize(uri)
12
+ @uri = RDF::Seq.new uri
13
+ end
14
+
15
+ # get all elements of Resource that match with 'rdf:_'
16
+ #
17
+ # return value: Array
18
+ def elements
19
+ # execute query
20
+ query.collect { |predicate, object| object }
21
+ end
22
+
23
+ # get element at position index
24
+ # *index: int
25
+ #
26
+ # return value: RDFS::Resource
27
+ def at(index)
28
+ # get predicate for next item
29
+ predicate = index_to_predicate(index)
30
+
31
+ # execute query
32
+ result = Query.new.select(:o).where(self.uri, predicate, :o).execute
33
+
34
+ if (!(result.nil?) and (result.size > 0))
35
+ result.first
36
+ else
37
+ nil
38
+ end
39
+ end
40
+
41
+ # return size of elements
42
+ #
43
+ # return value: int
44
+ def size
45
+ result = query
46
+
47
+ if result.empty?
48
+ return 0
49
+ else
50
+ index = predicate_to_index result.last[0]
51
+ return index.to_i
52
+ end
53
+ end
54
+
55
+ # add a new object
56
+ # * object: RDFS::Resource
57
+ def add(object)
58
+ # get predicate for next item
59
+ predicate = index_to_predicate(size + 1)
60
+
61
+ # add item
62
+ FederationManager.add(self.uri, predicate , object)
63
+ end
64
+
65
+ # remove an existing object
66
+ # * index: int
67
+ def delete(index)
68
+ # get predicate to delete
69
+ predicate = index_to_predicate(index)
70
+
71
+ # delete item
72
+ FederationManager.delete(self.uri, predicate, nil)
73
+ end
74
+
75
+ # remove all copy of object to OrderedSet
76
+ def delete_all()
77
+ # call delete method
78
+ (1..size).each do |index|
79
+ self.delete(index)
80
+ end
81
+ end
82
+
83
+ # replace item
84
+ # * index: int
85
+ # * object: RDFS::Resource
86
+ def replace(index, object)
87
+ # delete item
88
+ self.delete(index)
89
+
90
+ # get predicate for index
91
+ predicate = index_to_predicate(index)
92
+
93
+ # add item
94
+ FederationManager.add(self.uri, predicate , object)
95
+ end
96
+
97
+ private
98
+ # execute query and return the result
99
+ def query
100
+ # execute query
101
+ result = Query.new.select(:p, :o).where(self.uri, :p, :o).filter('regex(str(?p), "^' + (RDF::_).uri + '")').execute
102
+
103
+ # order result
104
+ result = result.sort_by { |items| items[0] }
105
+ end
106
+
107
+ # return predicate from index
108
+ def index_to_predicate(index)
109
+ RDFS::Resource.new(RDF::_.uri + "#{index}")
110
+ end
111
+
112
+ # return index of the predicate
113
+ def predicate_to_index(predicate)
114
+ predicate.uri.sub(RDF::_.uri, '')
115
+ end
116
+ end