lightrdf 0.1.5 → 0.1.6

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.1.6 2011-01-21
2
+
3
+ * Added Sesame functionality
4
+
1
5
  === 0.1.5 2010-11-16
2
6
 
3
7
  * Corrected problem when using Open3 and rapper
data/Manifest CHANGED
@@ -7,6 +7,7 @@ lib/lightrdf.rb
7
7
  lib/lightrdf/graph.rb
8
8
  lib/lightrdf/node.rb
9
9
  lib/lightrdf/parser.rb
10
+ lib/lightrdf/repository.rb
10
11
  lib/lightrdf/quri.rb
11
12
  test/test_helper.rb
12
13
  test/test_lightrdf.rb
data/README.rdoc CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- LightRDF is a gem that makes managing RDF data and graphs easily with a Ruby interface.
7
+ LightRDF is a gem that makes managing RDF data and graphs easy, with a Ruby interface.
8
8
 
9
9
  == INSTALL:
10
10
 
@@ -15,6 +15,8 @@ for outputting different RDF serialization formats.
15
15
 
16
16
  Additionally, PNG output of RDF graphs requires Graphviz (in Debian systems: sudo aptitude install graphviz).
17
17
 
18
+ Also, if you want to use the repository and Sesame functionality, Sesame must be installed. You can find furhter instructions at http://openrdf.org
19
+
18
20
  == RDF NODES:
19
21
 
20
22
  RDF nodes can be created using the Node method:
@@ -137,6 +139,40 @@ graphs and serialized using the parse and serialize methods:
137
139
  graph = RDF::Parser.parse(:rdf, open('http://planetrdf.com/guide/rss.rdf').read) # Builds a graph
138
140
  graph.serialize(:ntriples) # Outputs a string with triples
139
141
 
142
+ == RDF REPOSITORY
143
+
144
+ LightRDF can use a Sesame repository to save the RDF data.
145
+ The Sesame options are configured when creating the repository
146
+
147
+ repository = RDF::Repository.new :host=>"http://localhost", :port=>8080, :repository=>"memory"
148
+
149
+ If any of the values is missing, the values above will be used by default.
150
+
151
+ You have three ways of comunicating with Sesame:
152
+ - You can ask Sesame for the list of all the contexts in the repository:
153
+
154
+ repository = RDF::Repository.new
155
+ repository.contexts
156
+
157
+ - You can ask Sesame for all the data in several contexts, using the data method, which returns an RDF Graph:
158
+
159
+ repository = RDF::Repository.new
160
+ repository = repository.data "test:context1", "test:context2"
161
+ repository = repository.data "test:context3"
162
+
163
+ - You can ask Sesame for including data in a certain context, using the data= method, passing an RDF Graph as a parameter.
164
+
165
+ repository = RDF::Repository.new
166
+ context = "test:context4"
167
+ repository.data = graph, context # Associate graph to "test:context4"
168
+ repository.data = graph # In this case, data has no associated context
169
+
170
+ == CONTRIBUTORS:
171
+
172
+ * José Ignacio Fernández
173
+
174
+ * Alberto Mardomingo
175
+
140
176
  == LICENSE:
141
177
 
142
178
  (The MIT License)
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ Echoe.new('lightrdf', RDF::VERSION) do |p|
11
11
  p.email = "joseignacio.fernandez@gmail.com"
12
12
  p.install_message = '**Remember to install raptor RDF tools and (optionally for RDF PNG output) Graphviz**'
13
13
  p.ignore_pattern = ["pkg/*"]
14
- p.development_dependencies = [['activesupport','>= 2.0.2']]
14
+ p.dependencies = [['activesupport','>= 2.0.2'], ['rest-client', '>=1.6.1'], ['nokogiri', '>= 1.4.1']]
15
15
  end
16
16
 
17
17
  Rake::RDocTask.new(:rdoc) do |rdoc|
@@ -36,9 +36,10 @@ module RDF
36
36
  when :rdf
37
37
  serialize(:rdfxml)
38
38
  else
39
- tmpfile = File.join(Dir.tmpdir, "rapper#{$$}#{Thread.current.object_id}")
40
- File.open(tmpfile, 'w') { |f| f.write(serialize(:ntriples)) }
41
- %x[rapper -q -i ntriples -o #{format} #{tmpfile}]
39
+ stdin, stdout, stderr = Open3.popen3("rapper -q -i ntriples -o #{format} /dev/stdin")
40
+ stdin.puts serialize(:ntriples)
41
+ stdin.close
42
+ stdout.read
42
43
  end
43
44
  end
44
45
 
@@ -0,0 +1,85 @@
1
+ module RDF
2
+ class Repository
3
+ include MonitorMixin
4
+ include Parser
5
+
6
+ def initialize options={}
7
+ super()
8
+
9
+ # Assigns the default value to the options.
10
+ @options = {:host=>"http://localhost", :port=>8080, :repository=>"memory", :format=>:ntriples}.merge(options)
11
+ end
12
+
13
+ # Extracts the data in sesame from the indicated repositories
14
+ def data *contexts
15
+ synchronize do
16
+ params = ("?" + contexts.map{|context| "context=%3C#{CGI::escape(context)}%3E"}*"&&" if contexts.empty?)
17
+
18
+ # Prepares the URL to request the data
19
+ url = "#{repository_statements_url(@options[:repository])}?#{params}"
20
+
21
+ # Asks for the data
22
+ ntriples = RestClient.get url, :content_type=>content_type
23
+
24
+ # Builds the graph
25
+ Parser.parse :rdf, ntriples
26
+ end
27
+ end
28
+
29
+ # Gets the list of the sesame contexts
30
+ def contexts
31
+ synchronize do
32
+ # The URL to get the context list from sesame
33
+ url = "#{repository_url(@options[:repository])}/contexts"
34
+
35
+ # Asks for the context list and parses it
36
+ Nokogiri::XML( RestClient.get(url, :content_type=>'application/sparql-results+xml') ).search(:uri).map(&:text)
37
+ end
38
+ end
39
+
40
+ # Adds data to sesame without removing the previous data
41
+ def data= *args
42
+ synchronize do
43
+ # Retrieve arguments
44
+ graph, context = if args.first.is_a?(Array)
45
+ [args.first.first, args.first.last]
46
+ else
47
+ [args.first, ""]
48
+ end
49
+
50
+ # Prepares the dir to connect with the repository
51
+ url = "#{repository_statements_url(@options[:repository])}?context=%3C#{CGI::escape(context)}%3E"
52
+ data = graph.serialize :ntriples
53
+
54
+ # Adds the data to Sesame
55
+ RestClient.post url, data, :content_type=>content_type
56
+ end
57
+ end
58
+
59
+ protected
60
+
61
+ # Selects the type of data to send to sesame
62
+ def content_type
63
+ case @options[:format].to_sym
64
+ when :rdfxml then 'application/rdf+xml'
65
+ when :ntriples then 'text/plain'
66
+ when :turtle then 'application/x-turtle'
67
+ when :n3 then 'text/rdf+n3'
68
+ when :trix then 'application/trix'
69
+ when :trig then 'application/x-trig'
70
+ end
71
+ end
72
+
73
+ def repositories_url
74
+ "#{@options[:host]}:#{@options[:port]}/openrdf-sesame/repositories"
75
+ end
76
+
77
+ def repository_url repository
78
+ "#{repositories_url}/#{repository}"
79
+ end
80
+
81
+ def repository_statements_url repository
82
+ "#{repository_url(repository)}/statements"
83
+ end
84
+ end
85
+ end
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.1.5'
5
+ VERSION = '0.1.6'
6
6
  end
7
7
 
8
8
  require 'rubygems'
@@ -11,8 +11,13 @@ require 'uri'
11
11
  require 'open3'
12
12
  require 'open-uri'
13
13
  require 'tmpdir'
14
+ require 'rest-client'
15
+ require 'yaml'
16
+ require 'monitor'
17
+ require 'nokogiri'
14
18
 
15
19
  require "lightrdf/quri"
16
20
  require "lightrdf/parser"
17
21
  require "lightrdf/graph"
18
22
  require "lightrdf/node"
23
+ require "lightrdf/repository"
data/lightrdf.gemspec CHANGED
@@ -2,17 +2,17 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{lightrdf}
5
- s.version = "0.1.5"
5
+ s.version = "0.1.6"
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{2010-11-16}
9
+ s.date = %q{2011-01-21}
10
10
  s.default_executable = %q{yarfp}
11
11
  s.description = %q{RDF library}
12
12
  s.email = %q{joseignacio.fernandez@gmail.com}
13
13
  s.executables = ["yarfp"]
14
- s.extra_rdoc_files = ["README.rdoc", "bin/yarfp", "lib/lightrdf.rb", "lib/lightrdf/graph.rb", "lib/lightrdf/node.rb", "lib/lightrdf/parser.rb", "lib/lightrdf/quri.rb"]
15
- s.files = ["History.txt", "Manifest", "README.rdoc", "Rakefile", "bin/yarfp", "lib/lightrdf.rb", "lib/lightrdf/graph.rb", "lib/lightrdf/node.rb", "lib/lightrdf/parser.rb", "lib/lightrdf/quri.rb", "test/test_helper.rb", "test/test_lightrdf.rb", "lightrdf.gemspec"]
14
+ s.extra_rdoc_files = ["README.rdoc", "bin/yarfp", "lib/lightrdf.rb", "lib/lightrdf/graph.rb", "lib/lightrdf/node.rb", "lib/lightrdf/parser.rb", "lib/lightrdf/repository.rb", "lib/lightrdf/quri.rb"]
15
+ s.files = ["History.txt", "Manifest", "README.rdoc", "Rakefile", "bin/yarfp", "lib/lightrdf.rb", "lib/lightrdf/graph.rb", "lib/lightrdf/node.rb", "lib/lightrdf/parser.rb", "lib/lightrdf/repository.rb", "lib/lightrdf/quri.rb", "test/test_helper.rb", "test/test_lightrdf.rb", "lightrdf.gemspec"]
16
16
  s.homepage = %q{http://github.com/josei/lighrdf}
17
17
  s.post_install_message = %q{**Remember to install raptor RDF tools and (optionally for RDF PNG output) Graphviz**}
18
18
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Lightrdf", "--main", "README.rdoc"]
@@ -27,11 +27,17 @@ Gem::Specification.new do |s|
27
27
  s.specification_version = 3
28
28
 
29
29
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
30
- s.add_development_dependency(%q<activesupport>, [">= 2.0.2"])
30
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.0.2"])
31
+ s.add_runtime_dependency(%q<rest-client>, [">= 1.6.1"])
32
+ s.add_runtime_dependency(%q<nokogiri>, [">= 1.4.1"])
31
33
  else
32
34
  s.add_dependency(%q<activesupport>, [">= 2.0.2"])
35
+ s.add_dependency(%q<rest-client>, [">= 1.6.1"])
36
+ s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
33
37
  end
34
38
  else
35
39
  s.add_dependency(%q<activesupport>, [">= 2.0.2"])
40
+ s.add_dependency(%q<rest-client>, [">= 1.6.1"])
41
+ s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
36
42
  end
37
43
  end
@@ -4,7 +4,7 @@ class TestLightRDF < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
6
  Namespace :ex, 'http://www.example.com/ontology#'
7
- Namespace :foaf, 'http://xmlns.com/foaf/0.1/'
7
+ Namespace :foaf, 'http://xmlns.com/foaf/0.1/'
8
8
  end
9
9
 
10
10
  def test_equality
@@ -122,4 +122,29 @@ foaf: http://xmlns.com/foaf/0.1/
122
122
 
123
123
  assert 2, g.to_ntriples.split("\n").size
124
124
  end
125
+
126
+ def test_repository
127
+ repository = RDF::Repository.new
128
+ triple = [Node("http://testuri.org"), Node('rdf:type'), Node('rdf:Class')]
129
+ graph = RDF::Graph.new [triple]
130
+ context = "http://test_repository.org"
131
+ repository.data = graph, context
132
+
133
+ # Check if the added data is there
134
+ assert_equal graph, repository.data(context)
135
+
136
+ # Check if the triple is there when not filtering by context
137
+ assert repository.data.triples.include?(triple)
138
+ end
139
+
140
+ def test_repository_contexts
141
+ repository = RDF::Repository.new
142
+ graph = RDF::Graph.new [[Node("http://testuri.org"), Node('rdf:type'), Node('rdf:Class')]]
143
+ context = "http://test_repository_contexts.org"
144
+ repository.data = graph, context
145
+ contexts = repository.contexts
146
+
147
+ # Check if the added context is there
148
+ assert contexts.include?("http://test_repository_contexts.org")
149
+ end
125
150
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 5
9
- version: 0.1.5
8
+ - 6
9
+ version: 0.1.6
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: 2010-11-16 00:00:00 +01:00
17
+ date: 2011-01-21 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -29,8 +29,36 @@ dependencies:
29
29
  - 0
30
30
  - 2
31
31
  version: 2.0.2
32
- type: :development
32
+ type: :runtime
33
33
  version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rest-client
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 6
44
+ - 1
45
+ version: 1.6.1
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: nokogiri
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 1
57
+ - 4
58
+ - 1
59
+ version: 1.4.1
60
+ type: :runtime
61
+ version_requirements: *id003
34
62
  description: RDF library
35
63
  email: joseignacio.fernandez@gmail.com
36
64
  executables:
@@ -44,6 +72,7 @@ extra_rdoc_files:
44
72
  - lib/lightrdf/graph.rb
45
73
  - lib/lightrdf/node.rb
46
74
  - lib/lightrdf/parser.rb
75
+ - lib/lightrdf/repository.rb
47
76
  - lib/lightrdf/quri.rb
48
77
  files:
49
78
  - History.txt
@@ -55,6 +84,7 @@ files:
55
84
  - lib/lightrdf/graph.rb
56
85
  - lib/lightrdf/node.rb
57
86
  - lib/lightrdf/parser.rb
87
+ - lib/lightrdf/repository.rb
58
88
  - lib/lightrdf/quri.rb
59
89
  - test/test_helper.rb
60
90
  - test/test_lightrdf.rb