lightrdf 0.3.9 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.4.0 2011-07-08
2
+
3
+ * SPARQL support in repository
4
+
1
5
  === 0.3.9 2011-06-11
2
6
 
3
7
  * Wrapped delete method in RDF::Graph for simple deletion of nodes
data/README.rdoc CHANGED
@@ -213,7 +213,12 @@ You have three ways of comunicating with Sesame:
213
213
  context = "test:context4"
214
214
  repository.data = graph, context # Associate graph to "test:context4"
215
215
  repository.data = graph # In this case, data has no associated context
216
-
216
+
217
+ - You can easily make SPARQL queries by just typing the "WHERE" part of the statement:
218
+
219
+ repository = RDF::Repository.new
220
+ repository.query '?q rdfs:label "john"'
221
+
217
222
  == CONTRIBUTORS:
218
223
 
219
224
  * José Ignacio Fernández
@@ -44,17 +44,49 @@ module RDF
44
44
  graph, context = if args.first.is_a?(Array)
45
45
  [args.first.first, args.first.last]
46
46
  else
47
- [args.first, ""]
47
+ [args.first, nil]
48
48
  end
49
49
 
50
50
  # Prepares the dir to connect with the repository
51
- url = "#{repository_statements_url(@options[:repository])}?context=%3C#{CGI::escape(context)}%3E"
51
+ url = "#{repository_statements_url(@options[:repository])}"
52
+ url += "?context=%3C#{CGI::escape(context)}%3E" if context
52
53
  data = graph.serialize :ntriples
53
54
 
54
55
  # Adds the data to Sesame
55
56
  RestClient.post url, data, :content_type=>content_type
56
57
  end
57
58
  end
59
+
60
+ # Performs a query based on turtle syntax and assuming ?q as variable
61
+ def query string
62
+ query = ""
63
+ RDF::ID.ns.each do |prefix, uri|
64
+ query += "PREFIX #{prefix}: <#{uri}>\n"
65
+ end
66
+ query += "SELECT ?q\n"
67
+ query += "WHERE {#{string}}"
68
+
69
+ results = sparql(query)
70
+ results.children.first.children[3].children.select { |result| !result.text? }.map do |result|
71
+ node = result.children[1].children[1]
72
+ case node.name.to_sym
73
+ when :uri then
74
+ Node(node.content)
75
+ when :bnode then
76
+ Node("_:#{node.content}")
77
+ when :literal then
78
+ node.content
79
+ end
80
+ end
81
+ end
82
+
83
+ # Performs a SPARQL query
84
+ def sparql query
85
+ synchronize do
86
+ url = "#{repository_url(@options[:repository])}?query=#{CGI::escape(query)}"
87
+ Nokogiri::XML( RestClient.get(url, :content_type=>'application/sparql-results+xml') )
88
+ end
89
+ end
58
90
 
59
91
  protected
60
92
 
data/lib/lightrdf.rb CHANGED
@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module RDF
5
- VERSION = '0.3.9'
5
+ VERSION = '0.4.0'
6
6
  end
7
7
 
8
8
  require 'rubygems'
data/lightrdf.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{lightrdf}
5
- s.version = "0.3.9"
5
+ s.version = "0.4.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jose Ignacio"]
9
- s.date = %q{2011-06-11}
9
+ s.date = %q{2011-07-08}
10
10
  s.default_executable = %q{yarfp}
11
11
  s.description = %q{RDF library}
12
12
  s.email = %q{joseignacio.fernandez@gmail.com}
@@ -305,6 +305,15 @@ foaf: http://xmlns.com/foaf/0.1/
305
305
  assert contexts.include?("http://test_repository_contexts.org")
306
306
  end
307
307
 
308
+ def test_repository_sparql
309
+ repository = RDF::Repository.new
310
+ graph = RDF::Graph.new [[Node("http://testuri.org"), Node('rdf:type'), Node('rdf:Class')]]
311
+ repository.data = graph
312
+ results = repository.query("?q rdf:type rdf:Class")
313
+
314
+ assert_equal [Node("http://testuri.org")], results.uniq
315
+ end
316
+
308
317
  def test_instantiation
309
318
  assert_equal Node("foaf:Person"), Foaf::Person.new.rdf::type.first
310
319
  assert_equal Node("foaf:Agent"), Foaf::Thing.new.rdf::type.first
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 9
9
- version: 0.3.9
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jose Ignacio
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-06-11 00:00:00 +02:00
17
+ date: 2011-07-08 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency