tripod 0.7.18 → 0.7.19
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tripod/criteria/execution.rb +26 -35
- data/lib/tripod/criteria.rb +1 -1
- data/lib/tripod/resource_collection.rb +13 -0
- data/lib/tripod/version.rb +1 -1
- data/spec/tripod/criteria_execution_spec.rb +18 -18
- metadata +22 -22
@@ -12,7 +12,7 @@ module Tripod
|
|
12
12
|
# @option options [ String ] return_graph Indicates whether to return the graph as one of the variables.
|
13
13
|
def resources(opts={})
|
14
14
|
Tripod::ResourceCollection.new(
|
15
|
-
self.resource_class._resources_from_sparql(
|
15
|
+
self.resource_class._resources_from_sparql(self.as_query(opts)),
|
16
16
|
# pass in the criteria that was used to generate this collection, as well as whether the user specified return graph
|
17
17
|
:return_graph => (opts.has_key?(:return_graph) ? opts[:return_graph] : true),
|
18
18
|
:criteria => self
|
@@ -24,7 +24,7 @@ module Tripod
|
|
24
24
|
# @option options [ String ] return_graph Indicates whether to return the graph as one of the variables.
|
25
25
|
# @option options [ String ] accept_header The accept header to use for serializing (defaults to application/n-triples)
|
26
26
|
def serialize(opts={})
|
27
|
-
select_sparql =
|
27
|
+
select_sparql = self.as_query(:return_graph => opts[:return_graph])
|
28
28
|
self.resource_class._raw_describe_select_results(select_sparql, :accept_header => opts[:accept_header]) # note that this method defaults to using application/n-triples.
|
29
29
|
end
|
30
30
|
|
@@ -32,7 +32,7 @@ module Tripod
|
|
32
32
|
#
|
33
33
|
# @option options [ String ] return_graph Indicates whether to return the graph as one of the variables.
|
34
34
|
def first(opts={})
|
35
|
-
sq = Tripod::SparqlQuery.new(
|
35
|
+
sq = Tripod::SparqlQuery.new(self.as_query(opts))
|
36
36
|
first_sparql = sq.as_first_query_str
|
37
37
|
self.resource_class._resources_from_sparql(first_sparql).first
|
38
38
|
end
|
@@ -41,52 +41,43 @@ module Tripod
|
|
41
41
|
#
|
42
42
|
# @option options [ String ] return_graph Indicates whether to return the graph as one of the variables.
|
43
43
|
def count(opts={})
|
44
|
-
sq = Tripod::SparqlQuery.new(
|
44
|
+
sq = Tripod::SparqlQuery.new(self.as_query(opts))
|
45
45
|
count_sparql = sq.as_count_query_str
|
46
46
|
result = Tripod::SparqlClient::Query.select(count_sparql)
|
47
47
|
result[0]["tripod_count_var"]["value"].to_i
|
48
48
|
end
|
49
49
|
|
50
|
-
#
|
50
|
+
# turn this criteria into a query
|
51
|
+
def as_query(opts={})
|
52
|
+
Tripod.logger.debug("TRIPOD: building select query for criteria...")
|
51
53
|
|
52
|
-
|
54
|
+
return_graph = opts.has_key?(:return_graph) ? opts[:return_graph] : true
|
53
55
|
|
54
|
-
|
56
|
+
Tripod.logger.debug("TRIPOD: with return_graph: #{return_graph.inspect}")
|
55
57
|
|
56
|
-
|
57
|
-
def build_select_query(opts={})
|
58
|
+
select_query = "SELECT DISTINCT ?uri "
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
Tripod.logger.debug("TRIPOD: with return_graph: #{return_graph.inspect}")
|
64
|
-
|
65
|
-
select_query = "SELECT DISTINCT ?uri "
|
66
|
-
|
67
|
-
if return_graph
|
68
|
-
# if we are returing the graph, select it as a variable, and include either the <graph> or ?graph in the where clause
|
69
|
-
if graph_uri
|
70
|
-
select_query += "(<#{graph_uri}> as ?graph) WHERE { GRAPH <#{graph_uri}> { "
|
71
|
-
else
|
72
|
-
select_query += "?graph WHERE { GRAPH ?graph { "
|
73
|
-
end
|
60
|
+
if return_graph
|
61
|
+
# if we are returing the graph, select it as a variable, and include either the <graph> or ?graph in the where clause
|
62
|
+
if graph_uri
|
63
|
+
select_query += "(<#{graph_uri}> as ?graph) WHERE { GRAPH <#{graph_uri}> { "
|
74
64
|
else
|
75
|
-
select_query += "WHERE { "
|
76
|
-
# if we're not returning the graph, only restrict by the <graph> if there's one set at class level
|
77
|
-
select_query += "GRAPH <#{graph_uri}> { " if graph_uri
|
65
|
+
select_query += "?graph WHERE { GRAPH ?graph { "
|
78
66
|
end
|
67
|
+
else
|
68
|
+
select_query += "WHERE { "
|
69
|
+
# if we're not returning the graph, only restrict by the <graph> if there's one set at class level
|
70
|
+
select_query += "GRAPH <#{graph_uri}> { " if graph_uri
|
71
|
+
end
|
79
72
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
select_query += [order_clause, limit_clause, offset_clause].join(" ")
|
73
|
+
select_query += self.where_clauses.join(" . ")
|
74
|
+
select_query += " } "
|
75
|
+
select_query += "} " if return_graph || graph_uri # close the graph clause
|
76
|
+
select_query += self.extra_clauses.join(" ")
|
86
77
|
|
87
|
-
|
88
|
-
end
|
78
|
+
select_query += [order_clause, limit_clause, offset_clause].join(" ")
|
89
79
|
|
80
|
+
select_query.strip
|
90
81
|
end
|
91
82
|
|
92
83
|
end
|
data/lib/tripod/criteria.rb
CHANGED
@@ -9,13 +9,17 @@ module Tripod
|
|
9
9
|
|
10
10
|
attr_reader :resources
|
11
11
|
attr_reader :criteria # the criteria used to generate this collection
|
12
|
+
attr_reader :sparql_query_str # the sparql query used to generate this collection
|
12
13
|
|
13
14
|
# options:
|
14
15
|
# :criteria - the criteria used to create this collection
|
16
|
+
# :sparql_query_str - the sparql used to create this collection
|
15
17
|
# :return_graph - whether the original query returned the graphs or not.
|
16
18
|
def initialize(resources, opts={})
|
17
19
|
@resources = resources
|
18
20
|
@criteria = opts[:criteria]
|
21
|
+
@sparql_query_str = opts[:sparql_query_str]
|
22
|
+
@resource_class = opts[:resource_class]
|
19
23
|
@return_graph = opts[:return_graph]
|
20
24
|
end
|
21
25
|
|
@@ -50,6 +54,9 @@ module Tripod
|
|
50
54
|
time_serialization('nt') do
|
51
55
|
if @criteria
|
52
56
|
@criteria.serialize(:return_graph => @return_graph, :accept_header => "application/n-triples")
|
57
|
+
elsif @sparql_query_str && @resource_class
|
58
|
+
# run the query as a describe.
|
59
|
+
@resource_class._raw_describe_select_results(@sparql_query_str, :accept_header => "application/n-triples")
|
53
60
|
else
|
54
61
|
# for n-triples we can just concatenate them
|
55
62
|
nt = ""
|
@@ -72,6 +79,9 @@ module Tripod
|
|
72
79
|
time_serialization('rdf') do
|
73
80
|
if @criteria
|
74
81
|
@criteria.serialize(:return_graph => @return_graph, :accept_header => "application/rdf+xml")
|
82
|
+
elsif @sparql_query_str && @resource_class
|
83
|
+
# run the query as a describe.
|
84
|
+
@resource_class._raw_describe_select_results(@sparql_query_str, :accept_header => "application/rdf+xml")
|
75
85
|
else
|
76
86
|
get_graph.dump(:rdf)
|
77
87
|
end
|
@@ -82,6 +92,9 @@ module Tripod
|
|
82
92
|
time_serialization('ttl') do
|
83
93
|
if @criteria
|
84
94
|
@criteria.serialize(:return_graph => @return_graph, :accept_header => "text/turtle")
|
95
|
+
elsif @sparql_query_str && @resource_class
|
96
|
+
# run the query as a describe.
|
97
|
+
@resource_class._raw_describe_select_results(@sparql_query_str, :accept_header =>"text/turtle")
|
85
98
|
else
|
86
99
|
get_graph.dump(:turtle)
|
87
100
|
end
|
data/lib/tripod/version.rb
CHANGED
@@ -24,16 +24,16 @@ describe Tripod::Criteria do
|
|
24
24
|
p
|
25
25
|
end
|
26
26
|
|
27
|
-
describe "#
|
27
|
+
describe "#as_query" do
|
28
28
|
|
29
29
|
context "for a class with an rdf_type and graph" do
|
30
30
|
it "should return a SELECT query based with an rdf type restriction" do
|
31
|
-
person_criteria.
|
31
|
+
person_criteria.as_query.should == "SELECT DISTINCT ?uri (<http://example.com/graph> as ?graph) WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> . ?uri ?p ?o } }"
|
32
32
|
end
|
33
33
|
|
34
34
|
context "with include_graph option set to false" do
|
35
35
|
it "should not select graphs, but restrict to graph" do
|
36
|
-
person_criteria.
|
36
|
+
person_criteria.as_query(:return_graph => false).should == "SELECT DISTINCT ?uri WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> . ?uri ?p ?o } }"
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
@@ -41,7 +41,7 @@ describe Tripod::Criteria do
|
|
41
41
|
before { person_criteria.where("[pattern]") }
|
42
42
|
|
43
43
|
it "should return a SELECT query with the extra restriction" do
|
44
|
-
person_criteria.
|
44
|
+
person_criteria.as_query.should == "SELECT DISTINCT ?uri (<http://example.com/graph> as ?graph) WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> . ?uri ?p ?o . [pattern] } }"
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
@@ -49,19 +49,19 @@ describe Tripod::Criteria do
|
|
49
49
|
before { person_criteria.graph("http://example.com/anothergraph") }
|
50
50
|
|
51
51
|
it "should override the graph in the query" do
|
52
|
-
person_criteria.
|
52
|
+
person_criteria.as_query.should == "SELECT DISTINCT ?uri (<http://example.com/anothergraph> as ?graph) WHERE { GRAPH <http://example.com/anothergraph> { ?uri a <http://example.com/person> . ?uri ?p ?o } }"
|
53
53
|
end
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
57
|
context "for a class without an rdf_type and graph" do
|
58
58
|
it "should return a SELECT query without an rdf_type restriction" do
|
59
|
-
resource_criteria.
|
59
|
+
resource_criteria.as_query.should == "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o } }"
|
60
60
|
end
|
61
61
|
|
62
62
|
context "with include_graph option set to false" do
|
63
63
|
it "should not select graphs or restrict to graph" do
|
64
|
-
resource_criteria.
|
64
|
+
resource_criteria.as_query(:return_graph => false).should == "SELECT DISTINCT ?uri WHERE { ?uri ?p ?o }"
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
@@ -69,7 +69,7 @@ describe Tripod::Criteria do
|
|
69
69
|
before { resource_criteria.where("[pattern]") }
|
70
70
|
|
71
71
|
it "should return a SELECT query with the extra restrictions" do
|
72
|
-
resource_criteria.
|
72
|
+
resource_criteria.as_query.should == "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o . [pattern] } }"
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
@@ -77,7 +77,7 @@ describe Tripod::Criteria do
|
|
77
77
|
before { resource_criteria.graph("http://example.com/graphy") }
|
78
78
|
|
79
79
|
it "should override the graph in the query" do
|
80
|
-
resource_criteria.
|
80
|
+
resource_criteria.as_query.should == "SELECT DISTINCT ?uri (<http://example.com/graphy> as ?graph) WHERE { GRAPH <http://example.com/graphy> { ?uri ?p ?o } }"
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
@@ -87,7 +87,7 @@ describe Tripod::Criteria do
|
|
87
87
|
before { resource_criteria.where("[pattern]").extras("LIMIT 10").extras("OFFSET 20") }
|
88
88
|
|
89
89
|
it "should add the extras on the end" do
|
90
|
-
resource_criteria.
|
90
|
+
resource_criteria.as_query.should == "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o . [pattern] } } LIMIT 10 OFFSET 20"
|
91
91
|
end
|
92
92
|
end
|
93
93
|
end
|
@@ -95,8 +95,8 @@ describe Tripod::Criteria do
|
|
95
95
|
describe "#resources" do
|
96
96
|
|
97
97
|
context "with options passed" do
|
98
|
-
it "should pass the options to
|
99
|
-
person_criteria.should_receive(:
|
98
|
+
it "should pass the options to as_query" do
|
99
|
+
person_criteria.should_receive(:as_query).with(:return_graph => false).and_call_original
|
100
100
|
person_criteria.resources(:return_graph => false)
|
101
101
|
end
|
102
102
|
end
|
@@ -136,8 +136,8 @@ describe Tripod::Criteria do
|
|
136
136
|
describe "#first" do
|
137
137
|
|
138
138
|
context "with options passed" do
|
139
|
-
it "should pass the options to
|
140
|
-
person_criteria.should_receive(:
|
139
|
+
it "should pass the options to as_query" do
|
140
|
+
person_criteria.should_receive(:as_query).with(:return_graph => false).and_call_original
|
141
141
|
person_criteria.first(:return_graph => false)
|
142
142
|
end
|
143
143
|
end
|
@@ -147,7 +147,7 @@ describe Tripod::Criteria do
|
|
147
147
|
end
|
148
148
|
|
149
149
|
it "should call Query.select with the 'first sparql'" do
|
150
|
-
sparql = Tripod::SparqlQuery.new(person_criteria.
|
150
|
+
sparql = Tripod::SparqlQuery.new(person_criteria.as_query).as_first_query_str
|
151
151
|
Tripod::SparqlClient::Query.should_receive(:select).with(sparql).and_call_original
|
152
152
|
person_criteria.first
|
153
153
|
end
|
@@ -172,8 +172,8 @@ describe Tripod::Criteria do
|
|
172
172
|
describe "#count" do
|
173
173
|
|
174
174
|
context "with options passed" do
|
175
|
-
it "should pass the options to
|
176
|
-
person_criteria.should_receive(:
|
175
|
+
it "should pass the options to as_querys" do
|
176
|
+
person_criteria.should_receive(:as_query).with(:return_graph => false).and_call_original
|
177
177
|
person_criteria.count(:return_graph => false)
|
178
178
|
end
|
179
179
|
end
|
@@ -184,7 +184,7 @@ describe Tripod::Criteria do
|
|
184
184
|
end
|
185
185
|
|
186
186
|
it "should call Query.select with the 'count sparql'" do
|
187
|
-
sparql = Tripod::SparqlQuery.new(person_criteria.
|
187
|
+
sparql = Tripod::SparqlQuery.new(person_criteria.as_query).as_count_query_str
|
188
188
|
Tripod::SparqlClient::Query.should_receive(:select).with(sparql).and_call_original
|
189
189
|
person_criteria.count
|
190
190
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tripod
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.19
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,11 +11,11 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-07-
|
14
|
+
date: 2013-07-12 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rest-client
|
18
|
-
requirement: &
|
18
|
+
requirement: &70230396905700 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ! '>='
|
@@ -23,10 +23,10 @@ dependencies:
|
|
23
23
|
version: '0'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *70230396905700
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activemodel
|
29
|
-
requirement: &
|
29
|
+
requirement: &70230396905160 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - ~>
|
@@ -34,10 +34,10 @@ dependencies:
|
|
34
34
|
version: '3.2'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
|
-
version_requirements: *
|
37
|
+
version_requirements: *70230396905160
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: equivalent-xml
|
40
|
-
requirement: &
|
40
|
+
requirement: &70230396904740 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
43
|
- - ! '>='
|
@@ -45,10 +45,10 @@ dependencies:
|
|
45
45
|
version: '0'
|
46
46
|
type: :runtime
|
47
47
|
prerelease: false
|
48
|
-
version_requirements: *
|
48
|
+
version_requirements: *70230396904740
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
50
|
name: rdf
|
51
|
-
requirement: &
|
51
|
+
requirement: &70230396904200 !ruby/object:Gem::Requirement
|
52
52
|
none: false
|
53
53
|
requirements:
|
54
54
|
- - ~>
|
@@ -56,10 +56,10 @@ dependencies:
|
|
56
56
|
version: '1.0'
|
57
57
|
type: :runtime
|
58
58
|
prerelease: false
|
59
|
-
version_requirements: *
|
59
|
+
version_requirements: *70230396904200
|
60
60
|
- !ruby/object:Gem::Dependency
|
61
61
|
name: rdf-rdfxml
|
62
|
-
requirement: &
|
62
|
+
requirement: &70230396903780 !ruby/object:Gem::Requirement
|
63
63
|
none: false
|
64
64
|
requirements:
|
65
65
|
- - ! '>='
|
@@ -67,10 +67,10 @@ dependencies:
|
|
67
67
|
version: '0'
|
68
68
|
type: :runtime
|
69
69
|
prerelease: false
|
70
|
-
version_requirements: *
|
70
|
+
version_requirements: *70230396903780
|
71
71
|
- !ruby/object:Gem::Dependency
|
72
72
|
name: rdf-turtle
|
73
|
-
requirement: &
|
73
|
+
requirement: &70230396903320 !ruby/object:Gem::Requirement
|
74
74
|
none: false
|
75
75
|
requirements:
|
76
76
|
- - ! '>='
|
@@ -78,10 +78,10 @@ dependencies:
|
|
78
78
|
version: '0'
|
79
79
|
type: :runtime
|
80
80
|
prerelease: false
|
81
|
-
version_requirements: *
|
81
|
+
version_requirements: *70230396903320
|
82
82
|
- !ruby/object:Gem::Dependency
|
83
83
|
name: rdf-json
|
84
|
-
requirement: &
|
84
|
+
requirement: &70230396902900 !ruby/object:Gem::Requirement
|
85
85
|
none: false
|
86
86
|
requirements:
|
87
87
|
- - ! '>='
|
@@ -89,10 +89,10 @@ dependencies:
|
|
89
89
|
version: '0'
|
90
90
|
type: :runtime
|
91
91
|
prerelease: false
|
92
|
-
version_requirements: *
|
92
|
+
version_requirements: *70230396902900
|
93
93
|
- !ruby/object:Gem::Dependency
|
94
94
|
name: json-ld
|
95
|
-
requirement: &
|
95
|
+
requirement: &70230396902400 !ruby/object:Gem::Requirement
|
96
96
|
none: false
|
97
97
|
requirements:
|
98
98
|
- - ~>
|
@@ -100,10 +100,10 @@ dependencies:
|
|
100
100
|
version: 0.9.1
|
101
101
|
type: :runtime
|
102
102
|
prerelease: false
|
103
|
-
version_requirements: *
|
103
|
+
version_requirements: *70230396902400
|
104
104
|
- !ruby/object:Gem::Dependency
|
105
105
|
name: guid
|
106
|
-
requirement: &
|
106
|
+
requirement: &70230396901980 !ruby/object:Gem::Requirement
|
107
107
|
none: false
|
108
108
|
requirements:
|
109
109
|
- - ! '>='
|
@@ -111,10 +111,10 @@ dependencies:
|
|
111
111
|
version: '0'
|
112
112
|
type: :runtime
|
113
113
|
prerelease: false
|
114
|
-
version_requirements: *
|
114
|
+
version_requirements: *70230396901980
|
115
115
|
- !ruby/object:Gem::Dependency
|
116
116
|
name: dalli
|
117
|
-
requirement: &
|
117
|
+
requirement: &70230396901440 !ruby/object:Gem::Requirement
|
118
118
|
none: false
|
119
119
|
requirements:
|
120
120
|
- - ~>
|
@@ -122,7 +122,7 @@ dependencies:
|
|
122
122
|
version: '2.6'
|
123
123
|
type: :runtime
|
124
124
|
prerelease: false
|
125
|
-
version_requirements: *
|
125
|
+
version_requirements: *70230396901440
|
126
126
|
description: RDF ruby ORM
|
127
127
|
email:
|
128
128
|
- ric@swirrl.com
|