sparql-server 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +0 -0
  2. data/UNLICENSE +25 -0
  3. data/VERSION +1 -0
  4. data/lib/sparql-server.rb +237 -0
  5. metadata +106 -0
data/README.rdoc ADDED
File without changes
data/UNLICENSE ADDED
@@ -0,0 +1,25 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, contact Alex Serebryakov [serebryakov@gmail.com]
25
+ or visit <http://unlicense.org/>
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,237 @@
1
+ module SPARQL
2
+ module Server
3
+ class Request
4
+ class << self
5
+
6
+ ##
7
+ # [-]
8
+ ##
9
+ def to_xml(query, schema, options = {})
10
+ options[:namespaces] ||= {}
11
+ RDF::Writer.for(:xml).buffer do |writer|
12
+ options[:namespaces].each do |ns, uri|
13
+ writer.namespace!(uri, ns)
14
+ end
15
+ if options[:base]
16
+ writer.base!(options[:base])
17
+ end
18
+ find(query, schema).map do |item|
19
+ item.to_triples.each do |triple|
20
+ writer << triple
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ ##
27
+ # [-]
28
+ ##
29
+ def find(string, schema)
30
+ items = {}
31
+ reader = RDF::Reader.for(:sparql).new(string)
32
+
33
+ unless reader.type == :describe
34
+ raise BadRequest, 'Only `DESCRIBE` queries are allowed'
35
+ end
36
+
37
+ reader.triples.flatten.uniq.each do |item|
38
+ items[item] = Item.new(item, schema)
39
+ end
40
+
41
+ reader.each_statement do |statement|
42
+ triple = statement.to_triple.map { |item| items[item] }
43
+ triple.each { |item| item << triple }
44
+ end
45
+
46
+ reader.targets.each do |target|
47
+ items[target] ||= Item.new(target, schema)
48
+ end
49
+
50
+ reader.targets.map do |target|
51
+ items[target].instances.to_a
52
+ end.flatten
53
+ end
54
+ end
55
+
56
+ class BadRequest < RuntimeError; end
57
+
58
+ class Item
59
+
60
+ attr_reader :object
61
+ attr_reader :masters
62
+
63
+ ##
64
+ # [-]
65
+ ##
66
+ def initialize(object, schema)
67
+ @schema = schema
68
+ @object = object
69
+ @conditions = []
70
+ end
71
+
72
+ ##
73
+ # [-]
74
+ ##
75
+ def ==(other)
76
+ other.kind_of?(Item) ? @object == other.object : @object == other
77
+ end
78
+
79
+ ##
80
+ # [-]
81
+ ##
82
+ def <<(triple)
83
+ @conditions << triple
84
+ end
85
+
86
+ ##
87
+ # [-]
88
+ ##
89
+ def instances
90
+ rdf_class ? rdf_class.find(:all, :conditions => find_options) : []
91
+ end
92
+
93
+ ##
94
+ # [-]
95
+ ##
96
+ def to_sql(name)
97
+ predefined? ? value_to_sql(name) : model_to_sql(name)
98
+ end
99
+
100
+ ##
101
+ # [-]
102
+ ##
103
+ def solvable?
104
+ predefined? or rdf_type
105
+ end
106
+
107
+ ##
108
+ # [-]
109
+ ##
110
+ def rdf_class
111
+ @schema.select { |cls| cls.type == rdf_type }.first
112
+ end
113
+
114
+ ##
115
+ # [-]
116
+ ##
117
+ def belongs_to?(other)
118
+ if rdf_class.nil? or other.rdf_class.nil?
119
+ return false
120
+ end
121
+ attribute = rdf_class.has?(nil, other.rdf_class)
122
+ if attribute.nil? or not attribute.belongs_to?
123
+ return false
124
+ end
125
+ attribute.type
126
+ end
127
+
128
+ def inspect
129
+ @object
130
+ end
131
+
132
+ private
133
+
134
+ ##
135
+ # [-]
136
+ ##
137
+ def find_options #nodoc
138
+ text, ary = [], []
139
+ (parents + attributes).each do |c|
140
+ text.push(c.first)
141
+ ary.push(*c.last)
142
+ end
143
+ [text.join(' AND ')] + ary
144
+ end
145
+
146
+ ##
147
+ # [-]
148
+ ##
149
+ def parents #nodoc
150
+ as_objects.select { |c| belongs_to?(c[0]) }.map { |c| c[0].to_sql(belongs_to?(c[0])) }
151
+ end
152
+
153
+ ##
154
+ # [-]
155
+ ##
156
+ def attributes #nodoc
157
+ as_subject.select { |c| c[2].solvable? and not c[2].belongs_to?(self) }.map { |c| c[2].to_sql(c[1].object) }
158
+ end
159
+
160
+ ##
161
+ # [-]
162
+ ##
163
+ def rdf_type #nodoc
164
+ as_class.map { |c| c[2].object }.first
165
+ end
166
+
167
+ ##
168
+ # [-]
169
+ ##
170
+ def as_subject #nodoc
171
+ @conditions.select { |c| c[0] == self }.reject { |c| c[1] == RDF.type }
172
+ end
173
+
174
+ ##
175
+ # [-]
176
+ ##
177
+ def as_objects #nodoc
178
+ @conditions.select { |c| c[2] == self }
179
+ end
180
+
181
+ ##
182
+ # [-]
183
+ ##
184
+ def as_class #nodoc
185
+ @conditions.select { |c| c[0] == self && c[1] == RDF.type }
186
+ end
187
+
188
+ ##
189
+ # [-]
190
+ ##
191
+ def variable? #nodoc
192
+ @object.class == RDF::Query::Variable
193
+ end
194
+
195
+ ##
196
+ # [-]
197
+ ##
198
+ def model_to_sql(name) #nodoc
199
+ ["%s IN (?)" % name, instances]
200
+ end
201
+
202
+ ##
203
+ # [-]
204
+ ##
205
+ def value_to_sql(name) #nodoc
206
+ variable? ? variable_to_sql(name) : ["%s = %s" % [name, @object.to_s], []]
207
+ end
208
+
209
+ ##
210
+ # [-]
211
+ ##
212
+ def variable_to_sql(name) #nodoc
213
+ ary = @object.values.map do |modifier, value|
214
+ value = '"%s"' % value if value.class == String
215
+ "%s %s %s" % [name, modifier, value]
216
+ end
217
+ str = ary.join(@object.strict? ? " AND " : " OR ")
218
+ ["(%s)" % str, []]
219
+ end
220
+
221
+ ##
222
+ # [-]
223
+ ##
224
+ def predefined? #nodoc
225
+ case @object
226
+ when RDF::URI then true
227
+ when RDF::Literal then true
228
+ when RDF::Query::Variable then @object.bound?
229
+ else false
230
+ end
231
+ end
232
+
233
+ end # Item
234
+ end # Request
235
+ end # Server
236
+ end # SPARQL
237
+
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sparql-server
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Alex Serebryakov
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-26 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 3
30
+ - 0
31
+ version: 1.3.0
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rdf
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 1
44
+ - 1
45
+ version: 0.1.1
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rdf-sparql
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ - 0
58
+ - 2
59
+ version: 0.0.2
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ description: "Na\xC3\xAFve implementation of SPARQL server for RDFMapper"
63
+ email: serebryakov@gmail.com
64
+ executables: []
65
+
66
+ extensions: []
67
+
68
+ extra_rdoc_files: []
69
+
70
+ files:
71
+ - README.rdoc
72
+ - UNLICENSE
73
+ - VERSION
74
+ - lib/sparql-server.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/42cities/sparql-server/
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project: sparql-server
101
+ rubygems_version: 1.3.6
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: "Na\xC3\xAFve implementation of SPARQL server for RDFMapper"
105
+ test_files: []
106
+