sparql 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +88 -0
- data/Rakefile +22 -0
- data/coverage/index.html +252 -0
- data/coverage/lib-sparql-execute_sparql_rb.html +621 -0
- data/coverage/lib-sparql_rb.html +622 -0
- data/lib/sparql.rb +28 -0
- data/lib/sparql/execute_sparql.rb +27 -0
- data/lib/sparql/sparql.treetop +159 -0
- data/sparql.gemspec +16 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/unit/graph_parsing_spec.rb +76 -0
- data/spec/unit/iri_parsing_spec.rb +46 -0
- data/spec/unit/prefixed_names_parsing_spec.rb +40 -0
- data/spec/unit/primitives_parsing_spec.rb +26 -0
- data/spec/unit/sparql_parsing_spec.rb +72 -0
- data/spec/unit/variables_parsing_spec.rb +36 -0
- metadata +80 -0
data/lib/sparql.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# This file is part of Sparql.rb.
|
2
|
+
#
|
3
|
+
# Sparql.rb is free software: you can redistribute it and/or modify
|
4
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
5
|
+
# the Free Software Foundation, either version 3 of the License, or
|
6
|
+
# (at your option) any later version.
|
7
|
+
#
|
8
|
+
# Sparql.rb is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
# GNU Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public License
|
14
|
+
# along with Sparql.rb. If not, see <http://www.gnu.org/licenses/>.
|
15
|
+
|
16
|
+
|
17
|
+
require 'rubygems'
|
18
|
+
require 'pathname'
|
19
|
+
|
20
|
+
class Pathname
|
21
|
+
def /(path)
|
22
|
+
(self + path).expand_path
|
23
|
+
end
|
24
|
+
end # class Pathname
|
25
|
+
|
26
|
+
dir = Pathname(__FILE__).dirname.expand_path / 'sparql'
|
27
|
+
|
28
|
+
require dir / 'execute_sparql'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# This file is part of Sparql.rb.
|
2
|
+
#
|
3
|
+
# Sparql.rb is free software: you can redistribute it and/or modify
|
4
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
5
|
+
# the Free Software Foundation, either version 3 of the License, or
|
6
|
+
# (at your option) any later version.
|
7
|
+
#
|
8
|
+
# Sparql.rb is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
# GNU Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public License
|
14
|
+
# along with Sparql.rb. If not, see <http://www.gnu.org/licenses/>.
|
15
|
+
|
16
|
+
|
17
|
+
require 'rubygems'
|
18
|
+
require 'treetop'
|
19
|
+
|
20
|
+
Treetop.load "lib/sparql/primitives"
|
21
|
+
Treetop.load "lib/sparql/prefixed_names"
|
22
|
+
Treetop.load "lib/sparql/variables"
|
23
|
+
Treetop.load "lib/sparql/iri"
|
24
|
+
Treetop.load "lib/sparql/logical_expressions"
|
25
|
+
Treetop.load "lib/sparql/graph"
|
26
|
+
Treetop.load "lib/sparql/series"
|
27
|
+
Treetop.load "lib/sparql/sparql"
|
@@ -0,0 +1,159 @@
|
|
1
|
+
#!/usr/bin/env tt
|
2
|
+
# Author:: Pius Uzamere, The Uyiosa Corporation (http://uyiosa.com/)
|
3
|
+
# Copyright:: 2008 Pius Uzamere
|
4
|
+
|
5
|
+
# This file is part of Sparql.rb.
|
6
|
+
#
|
7
|
+
# Sparql.rb is free software: you can redistribute it and/or modify
|
8
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
9
|
+
# the Free Software Foundation, either version 3 of the License, or
|
10
|
+
# (at your option) any later version.
|
11
|
+
#
|
12
|
+
# Sparql.rb is distributed in the hope that it will be useful,
|
13
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
# GNU Lesser General Public License for more details.
|
16
|
+
#
|
17
|
+
# You should have received a copy of the GNU Lesser General Public License
|
18
|
+
# along with Sparql.rb. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
grammar Sparql
|
21
|
+
include Primitives
|
22
|
+
include Variables
|
23
|
+
include LogicalExpressions
|
24
|
+
include Graph
|
25
|
+
include Series
|
26
|
+
|
27
|
+
# 'SELECT ?v WHERE { ?v ?p "cat"@en }'
|
28
|
+
|
29
|
+
rule query
|
30
|
+
prologue space query_part:( select_query / construct_query / describe_query / ask_query ) {
|
31
|
+
def well_formed?
|
32
|
+
true #=> if a node has been identified as a query, it's well-formed by definition.
|
33
|
+
end
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
rule prologue
|
38
|
+
base_decl? prefix_decl*
|
39
|
+
end
|
40
|
+
|
41
|
+
rule base_decl
|
42
|
+
'BASE' space iri_ref
|
43
|
+
end
|
44
|
+
|
45
|
+
rule prefix_decl
|
46
|
+
'PREFIX' space pname_ns space iri_ref
|
47
|
+
end
|
48
|
+
|
49
|
+
rule select_query
|
50
|
+
'SELECT' space (( 'DISTINCT' / 'REDUCED' ) space)? ( series_of_variables / '*' ) space (series_of_dataset_clauses space)? where space solution_modifier
|
51
|
+
{
|
52
|
+
def query_type
|
53
|
+
"SELECT"
|
54
|
+
end
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
rule ask_query
|
59
|
+
'ASK' space (series_of_dataset_clauses space)? where {
|
60
|
+
def query_type
|
61
|
+
"ASK"
|
62
|
+
end
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
rule describe_query
|
67
|
+
'DESCRIBE' space ( series_of_var_or_irirefs / '*' ) space (series_of_dataset_clauses space)? (where space)? solution_modifier {
|
68
|
+
def query_type
|
69
|
+
"DESCRIBE"
|
70
|
+
end
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
rule construct_query
|
75
|
+
'CONSTRUCT' space construct_template space (dataset_clause space)* (where space) solution_modifier {
|
76
|
+
def query_type
|
77
|
+
"CONSTRUCT"
|
78
|
+
end
|
79
|
+
}
|
80
|
+
end
|
81
|
+
|
82
|
+
rule where
|
83
|
+
('WHERE' space)? group_graph_pattern
|
84
|
+
end
|
85
|
+
|
86
|
+
rule construct_template
|
87
|
+
'{' space construct_triples? space '}'
|
88
|
+
end
|
89
|
+
|
90
|
+
rule construct_triples
|
91
|
+
triples_same_subject space ( '.' construct_triples? )?
|
92
|
+
end
|
93
|
+
|
94
|
+
rule solution_modifier
|
95
|
+
(order_clause space limit_offset_clauses)? / order_clause? / limit_offset_clauses?
|
96
|
+
end
|
97
|
+
|
98
|
+
rule order_clause
|
99
|
+
'ORDER BY' space series_of_order_conditions
|
100
|
+
end
|
101
|
+
|
102
|
+
rule offset_clause
|
103
|
+
'OFFSET' space integer
|
104
|
+
end
|
105
|
+
|
106
|
+
rule limit_clause
|
107
|
+
'LIMIT' space integer
|
108
|
+
end
|
109
|
+
|
110
|
+
rule limit_offset_clauses
|
111
|
+
( limit_clause space (offset_clause? / offset_clause space limit_clause?) )
|
112
|
+
end
|
113
|
+
|
114
|
+
rule order_condition
|
115
|
+
( ( 'ASC' / 'DESC' ) space bracketted_expression ) / ( constraint / var )
|
116
|
+
end
|
117
|
+
|
118
|
+
rule constraint
|
119
|
+
bracketted_expression / built_in_call / function_call
|
120
|
+
end
|
121
|
+
|
122
|
+
rule built_in_call
|
123
|
+
'STR' space '(' expression ')'
|
124
|
+
/ 'LANG' space '(' expression ')'
|
125
|
+
/ 'LANGMATCHES' space '(' expression ',' expression ')'
|
126
|
+
/ 'DATATYPE' space '(' expression ')' / 'BOUND' space '(' var ')'
|
127
|
+
/ 'sameTerm' '(' expression ',' expression ')'
|
128
|
+
/ 'isIRI' space '(' expression ')'
|
129
|
+
/ 'isURI' space '(' expression ')'
|
130
|
+
/ 'isBLANK' space '(' expression ')'
|
131
|
+
/ 'isLITERAL' space '(' expression ')'
|
132
|
+
/ regex_expression
|
133
|
+
end
|
134
|
+
|
135
|
+
rule regex_expression
|
136
|
+
'REGEX' space '(' expression ',' expression ( ',' expression )? ')'
|
137
|
+
end
|
138
|
+
|
139
|
+
rule function_call
|
140
|
+
iriref arg_list
|
141
|
+
end
|
142
|
+
|
143
|
+
rule dataset_clause
|
144
|
+
'FROM' space ( default_graph_clause / named_graph_clause )
|
145
|
+
end
|
146
|
+
|
147
|
+
rule default_graph_clause
|
148
|
+
source_selector
|
149
|
+
end
|
150
|
+
|
151
|
+
rule source_selector
|
152
|
+
iriref
|
153
|
+
end
|
154
|
+
|
155
|
+
rule named_graph_clause
|
156
|
+
'NAMED' space SourceSelector
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
data/sparql.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "sparql"
|
3
|
+
s.version = "0.0.1"
|
4
|
+
s.date = "2008-07-15"
|
5
|
+
s.summary = "SPARQL library for Ruby."
|
6
|
+
s.email = "pius+github@uyiosa.com"
|
7
|
+
s.homepage = "http://github.com/pius/sparql"
|
8
|
+
s.description = "sparql is a Ruby library for parsing SPARQL queries. Implements the formal grammar of SPARQL as a parsing expression grammar."
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ['Pius Uzamere']
|
11
|
+
s.files = ["README.markdown", "Rakefile", "sparql.gemspec", "lib/sparql.rb", "lib/sparql/execute_sparql.rb", "lib/sparql/sparql.treetop", "coverage/index.html", "coverage/lib-sparql-execute_sparql_rb.html", "coverage/lib-sparql_rb.html"]
|
12
|
+
s.test_files = ["spec/spec.opts", "spec/spec_helper.rb", "spec/unit/graph_parsing_spec.rb", "spec/unit/graph_parsing_spec.rb", "spec/unit/iri_parsing_spec.rb", "spec/unit/sparql_parsing_spec.rb", "spec/unit/variables_parsing_spec.rb", "spec/unit/primitives_parsing_spec.rb", "spec/unit/prefixed_names_parsing_spec.rb"]
|
13
|
+
#s.rdoc_options = ["--main", "README.txt"]
|
14
|
+
#s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
15
|
+
s.add_dependency("treetop", [">= 1.2.4"])
|
16
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pathname'
|
3
|
+
require 'treetop'
|
4
|
+
|
5
|
+
# rule triples_same_subject
|
6
|
+
# var_or_term space property_list_not_empty / triples_node space property_list
|
7
|
+
# end
|
8
|
+
|
9
|
+
|
10
|
+
class Pathname
|
11
|
+
def /(path)
|
12
|
+
(self + path).expand_path
|
13
|
+
end
|
14
|
+
end # class Pathname
|
15
|
+
|
16
|
+
spec_dir_path = Pathname(__FILE__).dirname.expand_path
|
17
|
+
require spec_dir_path.parent + 'lib/sparql'
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
# require fixture resources
|
22
|
+
Dir[spec_dir_path + "fixtures/*.rb"].each do |fixture_file|
|
23
|
+
require fixture_file
|
24
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# This file is part of Sparql.rb.
|
2
|
+
#
|
3
|
+
# Sparql.rb is free software: you can redistribute it and/or modify
|
4
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
5
|
+
# the Free Software Foundation, either version 3 of the License, or
|
6
|
+
# (at your option) any later version.
|
7
|
+
#
|
8
|
+
# Sparql.rb is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
# GNU Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public License
|
14
|
+
# along with Sparql.rb. If not, see <http://www.gnu.org/licenses/>.
|
15
|
+
|
16
|
+
require 'pathname'
|
17
|
+
|
18
|
+
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
19
|
+
|
20
|
+
describe "GraphParser", '#parse' do
|
21
|
+
|
22
|
+
before(:all) do
|
23
|
+
@parser = GraphParser.new
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should recognize a triple block where the predicate is a prefixed name" do
|
27
|
+
some_triple_block = '?x foaf:knows ?y'
|
28
|
+
@parser.parse(some_triple_block).should_not == nil
|
29
|
+
@parser.parse(some_triple_block).triples_same_subject.text_value.should == '?x foaf:knows ?y'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should recognize a triple block where the predicate is a variable" do
|
33
|
+
some_triple_block = '?x ?theta ?y'
|
34
|
+
@parser.parse(some_triple_block).should_not == nil
|
35
|
+
@parser.parse(some_triple_block).triples_same_subject.text_value.should == '?x ?theta ?y'
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should recognize 'triples_same_subject'" do
|
39
|
+
some_triple_block = '?x foaf:knows ?y'
|
40
|
+
triples_same_subject = @parser.parse(some_triple_block).triples_same_subject
|
41
|
+
triples_same_subject.property_list_not_empty.text_value.should == "foaf:knows ?y"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should recognize 'triples_same_subject' when the predicate is a variable" do
|
45
|
+
some_triple_block = '?x ?theta ?y'
|
46
|
+
triples_same_subject = @parser.parse(some_triple_block).triples_same_subject
|
47
|
+
triples_same_subject.property_list_not_empty.text_value.should == '?theta ?y'
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should recognize 'triples_same_subject' when the object is a literal" # do
|
51
|
+
# some_triple_block = '?x ?theta "foo"'
|
52
|
+
# triples_same_subject = @parser.parse(some_triple_block).triples_same_subject
|
53
|
+
# triples_same_subject.property_list_not_empty.text_value.should == '?theta ?y "foo"'
|
54
|
+
# end
|
55
|
+
|
56
|
+
it "should recognize a 'group_graph_pattern'" # do
|
57
|
+
# some_group_graph_pattern = '?v ?p "cat"'
|
58
|
+
# parsed = @parser.parse(some_group_graph_pattern)
|
59
|
+
# parsed.should_not == nil
|
60
|
+
# end
|
61
|
+
|
62
|
+
it "should recognize a 'graph_pattern_not_triples'" # do
|
63
|
+
# some_graph_pattern_not_triples = '{ ?v ?p "cat" }'
|
64
|
+
# parsed = @parser.parse(some_graph_pattern_not_triples)
|
65
|
+
# parsed.should_not == nil
|
66
|
+
# end
|
67
|
+
|
68
|
+
# it "should recognize a basic SELECT query with one variable and one triple pattern" do
|
69
|
+
# basic_valid_select_query = 'SELECT ?foo WHERE { ?x foaf:knows ?y . }'
|
70
|
+
# parser = SparqlParser.new
|
71
|
+
# parser.parse(basic_valid_select_query).well_formed?.should == true
|
72
|
+
# parser.parse(basic_valid_select_query).prologue.text_value.should == ""
|
73
|
+
# parser.parse(basic_valid_select_query).query_part.text_value.should == "SELECT ?foo WHERE { ?x foaf:knows ?y . }"
|
74
|
+
# parser.parse('SELECT ?foo ?bar WHERE { ?x foaf:knows ?y . ?z foaf:name ?y . }').should_not == nil
|
75
|
+
# end
|
76
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# This file is part of Sparql.rb.
|
2
|
+
#
|
3
|
+
# Sparql.rb is free software: you can redistribute it and/or modify
|
4
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
5
|
+
# the Free Software Foundation, either version 3 of the License, or
|
6
|
+
# (at your option) any later version.
|
7
|
+
#
|
8
|
+
# Sparql.rb is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
# GNU Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public License
|
14
|
+
# along with Sparql.rb. If not, see <http://www.gnu.org/licenses/>.
|
15
|
+
|
16
|
+
require 'pathname'
|
17
|
+
|
18
|
+
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
19
|
+
|
20
|
+
describe "IriParser", '#parse' do
|
21
|
+
|
22
|
+
before(:all) do
|
23
|
+
@parser = IriParser.new
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should be able to recognize an iriref in brackets" do
|
27
|
+
the_iriref = "<http://purl.org/dc/elements/1.1/title>"
|
28
|
+
@parser.parse(the_iriref).should_not == nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be able to recognize an 'iri_ref'" do
|
32
|
+
the_iri_ref = "<http://xmlns.com/foaf/0.1/>"
|
33
|
+
@parser.parse(the_iri_ref).should_not == nil
|
34
|
+
end
|
35
|
+
|
36
|
+
# it "should be able to recognize an 'irirefchars'" do
|
37
|
+
# the_iriref = "<http://xmlns.com/foaf/0.1/>"
|
38
|
+
# @parser.parse(the_iriref).should_not == nil
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# it "should be able to recognize a 'prefixed_name'" do
|
42
|
+
# the_iriref = "<http://xmlns.com/foaf/0.1/>"
|
43
|
+
# @parser.parse(the_iriref).should_not == nil
|
44
|
+
# end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# This file is part of Sparql.rb.
|
2
|
+
#
|
3
|
+
# Sparql.rb is free software: you can redistribute it and/or modify
|
4
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
5
|
+
# the Free Software Foundation, either version 3 of the License, or
|
6
|
+
# (at your option) any later version.
|
7
|
+
#
|
8
|
+
# Sparql.rb is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
# GNU Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public License
|
14
|
+
# along with Sparql.rb. If not, see <http://www.gnu.org/licenses/>.
|
15
|
+
|
16
|
+
require 'pathname'
|
17
|
+
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
18
|
+
|
19
|
+
describe "PrefixedNamesParser", '#parse' do
|
20
|
+
|
21
|
+
before(:all) do
|
22
|
+
@parser = PrefixedNamesParser.new
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should be able to recognize a common prefixed_name" do
|
26
|
+
the_pn = "foaf:knows"
|
27
|
+
tree = @parser.parse(the_pn)
|
28
|
+
tree.should_not == nil
|
29
|
+
tree.pname_ns.text_value.should == "foaf:"
|
30
|
+
tree.pn_local.text_value.should == "knows"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should be able to recognize a prefixed_name whose local name has a leading digit (SPARQL idiosyncrasy)" do
|
34
|
+
the_pn = "foaf:1knows"
|
35
|
+
tree = @parser.parse(the_pn)
|
36
|
+
tree.should_not == nil
|
37
|
+
tree.pname_ns.text_value.should == "foaf:"
|
38
|
+
tree.pn_local.text_value.should == "1knows"
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# This file is part of Sparql.rb.
|
2
|
+
#
|
3
|
+
# Sparql.rb is free software: you can redistribute it and/or modify
|
4
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
5
|
+
# the Free Software Foundation, either version 3 of the License, or
|
6
|
+
# (at your option) any later version.
|
7
|
+
#
|
8
|
+
# Sparql.rb is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
# GNU Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public License
|
14
|
+
# along with Sparql.rb. If not, see <http://www.gnu.org/licenses/>.
|
15
|
+
|
16
|
+
require 'pathname'
|
17
|
+
|
18
|
+
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
19
|
+
|
20
|
+
describe "PrimitivesParser", '#parse' do
|
21
|
+
|
22
|
+
before(:all) do
|
23
|
+
@parser = PrimitivesParser.new
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|