rubyrdf-sesame 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-04-07
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/License.txt ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2008 Paul Stadig
2
+
3
+ Redistribution and use in source and binary forms, with or without modification,
4
+ are permitted provided that the following conditions are
5
+ met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ 3. The name of the author may not be used to endorse or promote products
13
+ derived from this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
18
+ SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20
+ OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22
+ CONTRACT,STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23
+ IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
24
+ OF SUCH DAMAGE.
data/Manifest.txt ADDED
@@ -0,0 +1,28 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ config/hoe.rb
7
+ config/requirements.rb
8
+ lib/rdf/graph/sesame.rb
9
+ lib/rdf/graph/sesame/version.rb
10
+ lib/rdf/graph/sesame/xml_parser.rb
11
+ lib/rubyrdf-sesame.rb
12
+ log/debug.log
13
+ script/console
14
+ script/destroy
15
+ script/generate
16
+ script/txt2html
17
+ setup.rb
18
+ tasks/deployment.rake
19
+ tasks/environment.rake
20
+ tasks/website.rake
21
+ test/helper.rb
22
+ test/test_sesame.rb
23
+ test/test_xml_parser.rb
24
+ website/index.html
25
+ website/index.txt
26
+ website/javascripts/rounded_corners_lite.inc.js
27
+ website/stylesheets/screen.css
28
+ website/template.html.erb
data/README.txt ADDED
@@ -0,0 +1,84 @@
1
+ = rubyrdf-sesame
2
+
3
+ * {https://launchpad.net/rubyrdf-sesame/}[https://launchpad.net/rubyrdf-sesame/]
4
+ * {http://rubyforge.org/rubyrdf/}[http://rubyforge.org/rubyrdf/]
5
+
6
+ == DESCRIPTION:
7
+
8
+ A RubyRDF graph implementation for the Sesame triple store.
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * A RubyRDF graph implementation for the Sesame triple store.
13
+
14
+ == SYNOPSIS:
15
+
16
+ Can be used like any other RubyRDF graph:
17
+
18
+ === Adding data to a graph
19
+
20
+ g = RDF::Graph::Sesame.new('http://openrdf.org/sesame/',
21
+ 'museum',
22
+ 'testuser',
23
+ 'opensesame')
24
+ g.add(UriNode.new('http://paul.stadig.name/'), RDF::type, RDF::Resource)
25
+
26
+ === Registering a new namespace
27
+
28
+ RDF::Namespace.register(:dc, 'http://purl.org/dc/elements/1.1/')
29
+
30
+ # you can also create instances of Triple and add them to the graph
31
+ t = RDF::Triple.new(UriNode.new('http://paul.stadig.name/'),
32
+ DC::author,
33
+ g.new_blank_node('paul'))
34
+ g.add(t)
35
+
36
+ === Querying a graph
37
+
38
+ q = Query.new
39
+ q.select(:x, :y).where(:x, DC::author, :y)
40
+ result = g.execute(q)
41
+ result.bindings.each do |b|
42
+ puts "x = #{b[:x]}"
43
+ puts "y = #{b[:y]}"
44
+ end
45
+
46
+ == REQUIREMENTS:
47
+
48
+ * ruby >= 1.8
49
+ * rubygems >= 0.9.8
50
+ * rubyrdf >= 0.0.1
51
+ * mechanize >= 0.7.5
52
+
53
+ == INSTALL:
54
+
55
+ * sudo gem install rubyrdf-sesame
56
+
57
+ == LICENSE:
58
+
59
+ (The BSD License (revised))
60
+
61
+ Copyright (c) 2008 Paul Stadig
62
+
63
+ Redistribution and use in source and binary forms, with or without modification,
64
+ are permitted provided that the following conditions are
65
+ met:
66
+
67
+ 1. Redistributions of source code must retain the above copyright notice,
68
+ this list of conditions and the following disclaimer.
69
+ 2. Redistributions in binary form must reproduce the above copyright notice,
70
+ this list of conditions and the following disclaimer in the documentation
71
+ and/or other materials provided with the distribution.
72
+ 3. The name of the author may not be used to endorse or promote products
73
+ derived from this software without specific prior written permission.
74
+
75
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
76
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
77
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
78
+ SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
79
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
80
+ OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
81
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
82
+ CONTRACT,STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
83
+ IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
84
+ OF SUCH DAMAGE.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/config/hoe.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'rdf/graph/sesame/version'
2
+
3
+ AUTHOR = 'Paul Stadig' # can also be an array of Authors
4
+ EMAIL = 'paul@stadig.name'
5
+ DESCRIPTION = 'A RubyRDF graph implementation for the Sesame triple store'
6
+ GEM_NAME = 'rubyrdf-sesame' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'rubyrdf' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "unknown"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+
31
+ REV = nil
32
+ # UNCOMMENT IF REQUIRED:
33
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34
+ VERS = RDF::Graph::Sesame::VERSION::STRING + (REV ? ".#{REV}" : "")
35
+ RDOC_OPTS = ['--quiet', '--title', 'rubyrdf-sesame documentation',
36
+ "--opname", "index.html",
37
+ "--line-numbers",
38
+ "--main", "README",
39
+ "--inline-source"]
40
+
41
+ class Hoe
42
+ def extra_deps
43
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44
+ @extra_deps
45
+ end
46
+ end
47
+
48
+ # Generate all the Rake tasks
49
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
50
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
51
+ p.developer(AUTHOR, EMAIL)
52
+ p.description = DESCRIPTION
53
+ p.summary = DESCRIPTION
54
+ p.url = HOMEPATH
55
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
56
+ p.test_globs = ["test/**/test_*.rb"]
57
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
58
+
59
+ # == Optional
60
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
61
+ p.extra_deps = [['rubyrdf', '>=0.0.1'], ['mechanize', '>=0.7.5']] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
62
+
63
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
64
+
65
+ end
66
+
67
+ CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
68
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
69
+ $hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
70
+ $hoe.rsync_args = '-av --delete --ignore-errors'
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -0,0 +1,125 @@
1
+ module RDF
2
+ module Graph
3
+ class Sesame < Base
4
+ attr_reader :host, :repository, :user
5
+
6
+ def initialize(host, repository, user = nil, password = nil)
7
+ super()
8
+ @host, @repository = host, repository
9
+ @user = user
10
+ @agent = WWW::Mechanize.new
11
+ @agent.pluggable_parser['text/xml'] = XMLParser
12
+ unless user.to_s.empty?
13
+ options = {}
14
+ options['user'] = @user
15
+ options['password'] = password unless password.to_s.empty?
16
+ @agent.post("#{host}/servlets/login", options)
17
+ end
18
+ end
19
+
20
+ def add(*triple)
21
+ triple = Triple.construct(*triple)
22
+
23
+ raise RDF::UnassociatedBlankNodeError, "#{triple.subject} is not associated with this graph" if RDF::BlankNode?(triple.subject) && triple.subject.graph != self
24
+ raise RDF::UnassociatedBlankNodeError, "#{triple.object} is not associated with this graph" if RDF::BlankNode?(triple.object) && triple.object.graph != self
25
+
26
+ @agent.post("#{host}/servlets/uploadData",
27
+ :repository => repository,
28
+ :data => triple.to_s,
29
+ :dataFormat => 'ntriples')
30
+ end
31
+
32
+ def delete(*triple)
33
+ triple = Triple.construct(*triple)
34
+ @agent.post("#{host}/servlets/removeStatements",
35
+ :repository => repository,
36
+ :subject => triple.subject.to_s,
37
+ :predicate => triple.predicate.to_s,
38
+ :object => triple.object.to_s)
39
+ end
40
+
41
+ def variable?(node)
42
+ node.is_a?(Symbol) || (RDF::BlankNode?(node) && node.graph != self)
43
+ end
44
+
45
+ def match(s, p, o, result = nil)
46
+ result ||= RDF::Query::Result.new
47
+ map = {}
48
+ select = []
49
+ if variable?(s)
50
+ select << s
51
+ map[s] = RDF::BlankNode?(s) ? s.name : s.to_s
52
+ else
53
+ map[s] = s
54
+ end
55
+
56
+ if variable?(p)
57
+ select << p
58
+ map[p] = RDF::BlankNode?(p) ? p.name : p.to_s
59
+ else
60
+ map[p] = p
61
+ end
62
+
63
+ if variable?(o)
64
+ select << o
65
+ map[o] = RDF::BlankNode?(o) ? o.name : o.to_s
66
+ else
67
+ map[o] = o
68
+ end
69
+
70
+ q = Query.new
71
+ if select.any?
72
+ q.select(select)
73
+ end
74
+ q.where(map[s], map[p], map[o])
75
+ execute(q)
76
+ end
77
+
78
+ def execute(query)
79
+ query_string = "select "
80
+ if query.select_clause.any?
81
+ query_string += query.select_clause.join(", ")
82
+ else
83
+ query_string += " * "
84
+ end
85
+
86
+ if query.where_clause.any?
87
+ query_string += " from " +
88
+ query.where_clause.map{|w| "{#{to_q(w[0])}} #{to_q(w[1])} {#{to_q(w[2])}}"}.join(", ")
89
+ end
90
+ perform_select(query_string).result(self)
91
+ end
92
+
93
+ def to_q(n)
94
+ RDF::BlankNode?(n) ? n.name : n.to_s
95
+ end
96
+
97
+ def include?(*triple)
98
+ triple = Triple.construct(*triple)
99
+ perform_select("select Pred from {#{triple.subject}} Pred {#{triple.object}} where Pred = #{triple.predicate}").solution?
100
+ end
101
+
102
+ def size
103
+ match(:s, :p, :o).bindings.size
104
+ end
105
+
106
+ def clear
107
+ @agent.post("#{host}/servlets/clearRepository", :repository => repository)
108
+ end
109
+
110
+ def perform_select(query)
111
+ #puts query
112
+ result = @agent.post("#{host}/servlets/evaluateTableQuery",
113
+ :repository => repository,
114
+ :query => query,
115
+ :queryLanguage => 'SeRQL',
116
+ :resultFormat => 'xml')
117
+ #puts result.header["Content-Type"]
118
+ #puts result.body
119
+ result
120
+ rescue WWW::Mechanize::ResponseCodeError
121
+ raise "invalid query '#{query}'"
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,13 @@
1
+ module RDF #:nodoc:
2
+ module Graph #:nodoc:
3
+ module Sesame #:nodoc:
4
+ module VERSION #:nodoc:
5
+ MAJOR = 0
6
+ MINOR = 0
7
+ TINY = 1
8
+
9
+ STRING = [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,49 @@
1
+ module RDF
2
+ module Graph
3
+ class Sesame
4
+ class XMLParser < WWW::Mechanize::File
5
+ attr_reader :parser
6
+ alias :root :parser
7
+
8
+ def initialize(uri=nil, response=nil, body=nil, code=nil)
9
+ super(uri, response, body, code)
10
+ @parser = Hpricot.XML(body)
11
+ end
12
+
13
+ def solution?
14
+ !@parser.search('tableQueryResult/tuple').empty?
15
+ end
16
+
17
+ def result(graph)
18
+ header_map = []
19
+ @parser.search('tableQueryResult/header/columnName').each do |c|
20
+ header_map << c.inner_html
21
+ end
22
+
23
+ @parser.search('tableQueryResult/tuple').inject(RDF::Query::Result.success!) do |r, row|
24
+ b = RDF::Query::Binding.new
25
+ header_map.each_with_index do |name, i|
26
+ b[name.to_sym] = parse_node(row.containers[i], graph)
27
+ end
28
+ r.bindings << b
29
+ r
30
+ end
31
+ end
32
+
33
+ def parse_node(node, graph)
34
+ if node.name == 'uri'
35
+ RDF::UriNode.new(node.inner_html)
36
+ elsif node.name == 'bNode'
37
+ RDF::BlankNode.new(node.inner_html, graph)
38
+ elsif node.name == 'literal'
39
+ if node['datatype']
40
+ RDF::TypedLiteralNode.new(node.inner_html, node['datatype'])
41
+ elsif node['xml:lang']
42
+ RDF::PlainLiteralNode.new(node.inner_html, node['xml:lang'])
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ lib_dir = File.dirname(__FILE__)
2
+ $:.unshift lib_dir unless $:.include?(lib_dir) || $:.include?(File.expand_path(lib_dir))
3
+
4
+ gem 'mechanize'
5
+ require 'mechanize'
6
+
7
+ gem 'rubyrdf'
8
+ require 'rubyrdf'
9
+
10
+ require 'rdf/graph/sesame'
11
+ require 'rdf/graph/sesame/xml_parser'
data/log/debug.log ADDED
File without changes