reddy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HACKNOTES +2 -0
- data/History.txt +3 -0
- data/Manifest.txt +80 -0
- data/README.rdoc +48 -0
- data/README.txt +62 -0
- data/Rakefile +67 -0
- data/lib/reddy.rb +8 -0
- data/lib/reddy/bnode.rb +70 -0
- data/lib/reddy/exceptions/about_each_exception.rb +2 -0
- data/lib/reddy/exceptions/uri_relative_exception.rb +2 -0
- data/lib/reddy/graph.rb +182 -0
- data/lib/reddy/libxml_hacks.rb +6 -0
- data/lib/reddy/literal.rb +211 -0
- data/lib/reddy/n3_grammar.treetop +129 -0
- data/lib/reddy/n3parser.rb +145 -0
- data/lib/reddy/namespace.rb +73 -0
- data/lib/reddy/rdfaparser.rb +63 -0
- data/lib/reddy/rdfxmlparser.rb +254 -0
- data/lib/reddy/rexml_hacks.rb +97 -0
- data/lib/reddy/triple.rb +95 -0
- data/lib/reddy/uriref.rb +66 -0
- data/reddy.gemspec +50 -0
- data/spec/bnode_spec.rb +29 -0
- data/spec/graph_spec.rb +138 -0
- data/spec/literal_spec.rb +142 -0
- data/spec/n3parser_spec.rb +86 -0
- data/spec/namespaces_spec.rb +44 -0
- data/spec/parser_spec.rb +391 -0
- data/spec/rdfa_parser_spec.rb +28 -0
- data/spec/rexml_hacks_spec.rb +99 -0
- data/spec/triple_spec.rb +108 -0
- data/spec/uriref_spec.rb +96 -0
- data/test/longtests_spec.rb +25 -0
- data/test/n3_tests/lcsh/sh85062913.n3 +41 -0
- data/test/n3_tests/lcsh/sh85062913.nt +21 -0
- data/test/n3_tests/lcsh/sh85082139.n3 +157 -0
- data/test/n3_tests/lcsh/sh85082139.nt +79 -0
- data/test/n3_tests/lcsh/sh85118553.n3 +123 -0
- data/test/n3_tests/lcsh/sh85118553.nt +63 -0
- data/test/n3_tests/misc/on_now-01.n3 +30 -0
- data/test/n3_tests/misc/on_now-01.nt +15 -0
- data/test/n3_tests/n3p/simple-01.n3 +1 -0
- data/test/n3_tests/n3p/simple-01.nt +0 -0
- data/test/n3_tests/n3p/simple-02.n3 +4 -0
- data/test/n3_tests/n3p/simple-02.nt +0 -0
- data/test/n3_tests/n3p/simple-03.n3 +5 -0
- data/test/n3_tests/n3p/simple-03.nt +1 -0
- data/test/n3_tests/n3p/simple-04.n3 +6 -0
- data/test/n3_tests/n3p/simple-04.nt +3 -0
- data/test/n3_tests/n3p/simple-05.n3 +7 -0
- data/test/n3_tests/n3p/simple-05.nt +2 -0
- data/test/n3_tests/n3p/simple-06.n3 +6 -0
- data/test/n3_tests/n3p/simple-06.nt +4 -0
- data/test/n3_tests/n3p/simple-07.n3 +7 -0
- data/test/n3_tests/n3p/simple-07.nt +6 -0
- data/test/perf_test/test.rb +11 -0
- data/test/perf_test/tommorris.rdf +2267 -0
- data/test/rdf_tests/cc197bad-dc9c-440d-a5b5-d52ba2e14234.nt +24 -0
- data/test/rdf_tests/cc197bad-dc9c-440d-a5b5-d52ba2e14234.rdf +46 -0
- data/test/rdf_tests/tm_001.nt +1 -0
- data/test/rdf_tests/tm_001.rdf +7 -0
- data/test/rdf_tests/xml-literal-mixed.nt +7 -0
- data/test/rdf_tests/xml-literal-mixed.rdf +15 -0
- data/test/ruby_fundamentals.spec.rb +17 -0
- data/test/test_helper.rb +2 -0
- data/test/test_reddy.rb +11 -0
- data/test/test_uris.rb +13 -0
- data/test/xml.rdf +6 -0
- metadata +198 -0
data/HACKNOTES
ADDED
@@ -0,0 +1,2 @@
|
|
1
|
+
AboutEachPrefix:
|
2
|
+
if we actually wanted to handle aboutEachPrefix attributes (we should get confirmation as to how often they are used), then the approach to doing so would be pretty simple - put them in a temporary array, then when processing a file has *finished* apply them all.
|
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
HACKNOTES
|
2
|
+
History.txt
|
3
|
+
lib
|
4
|
+
lib/reddy
|
5
|
+
lib/reddy/libxml_hacks.rb
|
6
|
+
lib/reddy/rexml_hacks.rb
|
7
|
+
lib/reddy/n3_grammar.treetop
|
8
|
+
lib/reddy/uriref.rb
|
9
|
+
lib/reddy/rdfxmlparser.rb
|
10
|
+
lib/reddy/n3parser.rb
|
11
|
+
lib/reddy/exceptions
|
12
|
+
lib/reddy/exceptions/about_each_exception.rb
|
13
|
+
lib/reddy/exceptions/uri_relative_exception.rb
|
14
|
+
lib/reddy/namespace.rb
|
15
|
+
lib/reddy/graph.rb
|
16
|
+
lib/reddy/bnode.rb
|
17
|
+
lib/reddy/triple.rb
|
18
|
+
lib/reddy/rdfaparser.rb
|
19
|
+
lib/reddy/literal.rb
|
20
|
+
lib/reddy.rb
|
21
|
+
Manifest.txt
|
22
|
+
Rakefile
|
23
|
+
README.rdoc
|
24
|
+
README.txt
|
25
|
+
reddy.gemspec
|
26
|
+
spec
|
27
|
+
spec/uriref_spec.rb
|
28
|
+
spec/rdfa_parser_spec.rb
|
29
|
+
spec/n3parser_spec.rb
|
30
|
+
spec/graph_spec.rb
|
31
|
+
spec/literal_spec.rb
|
32
|
+
spec/bnode_spec.rb
|
33
|
+
spec/triple_spec.rb
|
34
|
+
spec/parser_spec.rb
|
35
|
+
spec/rexml_hacks_spec.rb
|
36
|
+
spec/namespaces_spec.rb
|
37
|
+
tasks
|
38
|
+
test
|
39
|
+
test/xml.rdf
|
40
|
+
test/n3_tests
|
41
|
+
test/n3_tests/misc
|
42
|
+
test/n3_tests/misc/on_now-01.nt
|
43
|
+
test/n3_tests/misc/on_now-01.n3
|
44
|
+
test/n3_tests/n3p
|
45
|
+
test/n3_tests/n3p/simple-07.nt
|
46
|
+
test/n3_tests/n3p/simple-05.n3
|
47
|
+
test/n3_tests/n3p/simple-06.n3
|
48
|
+
test/n3_tests/n3p/simple-07.n3
|
49
|
+
test/n3_tests/n3p/simple-06.nt
|
50
|
+
test/n3_tests/n3p/simple-02.nt
|
51
|
+
test/n3_tests/n3p/simple-04.nt
|
52
|
+
test/n3_tests/n3p/simple-02.n3
|
53
|
+
test/n3_tests/n3p/simple-03.nt
|
54
|
+
test/n3_tests/n3p/simple-01.nt
|
55
|
+
test/n3_tests/n3p/simple-01.n3
|
56
|
+
test/n3_tests/n3p/simple-04.n3
|
57
|
+
test/n3_tests/n3p/simple-03.n3
|
58
|
+
test/n3_tests/n3p/simple-05.nt
|
59
|
+
test/n3_tests/lcsh
|
60
|
+
test/n3_tests/lcsh/sh85082139.nt
|
61
|
+
test/n3_tests/lcsh/sh85118553.nt
|
62
|
+
test/n3_tests/lcsh/sh85118553.n3
|
63
|
+
test/n3_tests/lcsh/sh85062913.n3
|
64
|
+
test/n3_tests/lcsh/sh85082139.n3
|
65
|
+
test/n3_tests/lcsh/sh85062913.nt
|
66
|
+
test/perf_test
|
67
|
+
test/perf_test/tommorris.rdf
|
68
|
+
test/perf_test/test.rb
|
69
|
+
test/rdf_tests
|
70
|
+
test/rdf_tests/xml-literal-mixed.rdf
|
71
|
+
test/rdf_tests/xml-literal-mixed.nt
|
72
|
+
test/rdf_tests/tm_001.nt
|
73
|
+
test/rdf_tests/cc197bad-dc9c-440d-a5b5-d52ba2e14234.rdf
|
74
|
+
test/rdf_tests/tm_001.rdf
|
75
|
+
test/rdf_tests/cc197bad-dc9c-440d-a5b5-d52ba2e14234.nt
|
76
|
+
test/test_helper.rb
|
77
|
+
test/test_uris.rb
|
78
|
+
test/test_reddy.rb
|
79
|
+
test/longtests_spec.rb
|
80
|
+
test/ruby_fundamentals.spec.rb
|
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= reddy
|
2
|
+
|
3
|
+
* FIX (url)
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
FIX (describe your package)
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* FIX (list of features or problems)
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
FIX (code sample of usage)
|
16
|
+
|
17
|
+
== REQUIREMENTS:
|
18
|
+
|
19
|
+
* FIX (list of requirements)
|
20
|
+
|
21
|
+
== INSTALL:
|
22
|
+
|
23
|
+
* FIX (sudo gem install, anything else)
|
24
|
+
|
25
|
+
== LICENSE:
|
26
|
+
|
27
|
+
(The MIT License)
|
28
|
+
|
29
|
+
Copyright (c) 2008 FIXME full name
|
30
|
+
|
31
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
32
|
+
a copy of this software and associated documentation files (the
|
33
|
+
'Software'), to deal in the Software without restriction, including
|
34
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
35
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
36
|
+
permit persons to whom the Software is furnished to do so, subject to
|
37
|
+
the following conditions:
|
38
|
+
|
39
|
+
The above copyright notice and this permission notice shall be
|
40
|
+
included in all copies or substantial portions of the Software.
|
41
|
+
|
42
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
43
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
44
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
45
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
46
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
47
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
48
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.txt
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
= reddy
|
2
|
+
|
3
|
+
* http://github.com/tommorris/reddy
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Reddy is an RDF library for Ruby.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* RDF/XML parser is beginning to parse tests.
|
12
|
+
* N3 parser
|
13
|
+
* Working on an RDFa parser - for now, please use a web service (etc.) to process
|
14
|
+
* No serializer except N-Triples
|
15
|
+
|
16
|
+
== SYNOPSIS:
|
17
|
+
|
18
|
+
A simple RDF library for Ruby has been necessary for some time.
|
19
|
+
|
20
|
+
== REQUIREMENTS:
|
21
|
+
|
22
|
+
* Addressable
|
23
|
+
* Treetop
|
24
|
+
* Whatlanguage
|
25
|
+
* libxml-ruby
|
26
|
+
* facets
|
27
|
+
|
28
|
+
== INSTALL:
|
29
|
+
|
30
|
+
* (sudo gem install reddy)
|
31
|
+
|
32
|
+
== LICENSE:
|
33
|
+
|
34
|
+
(The MIT License)
|
35
|
+
|
36
|
+
Copyright (c) 2008 Tom Morris and contributors
|
37
|
+
|
38
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
39
|
+
a copy of this software and associated documentation files (the
|
40
|
+
'Software'), to deal in the Software without restriction, including
|
41
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
42
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
43
|
+
permit persons to whom the Software is furnished to do so, subject to
|
44
|
+
the following conditions:
|
45
|
+
|
46
|
+
The above copyright notice and this permission notice shall be
|
47
|
+
included in all copies or substantial portions of the Software.
|
48
|
+
|
49
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
50
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
51
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
52
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
53
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
54
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
55
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
56
|
+
|
57
|
+
== FEEDBACK
|
58
|
+
|
59
|
+
* tom@tommorris.org
|
60
|
+
* github.com/tommorris/reddy
|
61
|
+
* tommorris.lighthouseapp.com for bug reports
|
62
|
+
* public-rdf-ruby mailing list on w3.org
|
data/Rakefile
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
require File.dirname(__FILE__) + '/lib/reddy'
|
4
|
+
|
5
|
+
# Generate all the Rake tasks
|
6
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
7
|
+
$hoe = Hoe.new('reddy', Reddy::VERSION) do |p|
|
8
|
+
p.developer('Tom Morris', 'tom@tommorris.org')
|
9
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
10
|
+
p.rubyforge_name = p.name # TODO this is default value
|
11
|
+
p.extra_deps = [
|
12
|
+
['addressable', '>= 2.0.0'],
|
13
|
+
['treetop', '>= 1.2.4'],
|
14
|
+
['libxml-ruby', '>= 0.8.3'],
|
15
|
+
['whatlanguage', '>= 1.0.0']
|
16
|
+
]
|
17
|
+
p.extra_dev_deps = [
|
18
|
+
['newgem', ">= #{::Newgem::VERSION}"]
|
19
|
+
]
|
20
|
+
|
21
|
+
p.clean_globs |= %w[**/.DS_Store .git tmp *.log]
|
22
|
+
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
23
|
+
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
24
|
+
p.rsync_args = '-av --delete --ignore-errors'
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
28
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
29
|
+
|
30
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
31
|
+
#task :default => [:spec, :features]
|
32
|
+
|
33
|
+
desc "Pushes to git"
|
34
|
+
task :push do
|
35
|
+
sh "git push --all"
|
36
|
+
sh "growlnotify -m \"Updates pushed\" \"Git\""
|
37
|
+
end
|
38
|
+
|
39
|
+
task :spec do
|
40
|
+
sh "spec --colour spec"
|
41
|
+
end
|
42
|
+
|
43
|
+
desc "Turns spec results into HTML and publish to web (Tom only!)"
|
44
|
+
task :spec_html do
|
45
|
+
sh "spec --format html:reddy_new_spec.html spec"
|
46
|
+
sh "scp reddy_new_spec.html bbcityco@bbcity.co.uk:www/tom/files/rena_new_spec.html"
|
47
|
+
sh "rm reddy_new_spec.html"
|
48
|
+
end
|
49
|
+
|
50
|
+
desc "Turns spec results into local HTML"
|
51
|
+
task :spec_local do
|
52
|
+
sh "spec --format html:reddy_new_spec.html spec/"
|
53
|
+
# sh "open reddy_new_spec.html"
|
54
|
+
end
|
55
|
+
|
56
|
+
desc "Run specs through RCov"
|
57
|
+
Spec::Rake::SpecTask.new('coverage') do |t|
|
58
|
+
t.spec_files = FileList['spec']
|
59
|
+
t.rcov = true
|
60
|
+
t.rcov_opts = ['--exclude', 'spec,test,\/Library\/Ruby\/Gems\/1.8\/gems']
|
61
|
+
end
|
62
|
+
|
63
|
+
desc "Runs specs on JRuby"
|
64
|
+
task :jspec do
|
65
|
+
sh "jruby -S `whereis spec` --colour spec"
|
66
|
+
end
|
67
|
+
# vim: syntax=Ruby
|
data/lib/reddy.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
#$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
2
|
+
|
3
|
+
$:.unshift(File.dirname('reddy'))
|
4
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'reddy/**.rb')).each { |f| require f }
|
5
|
+
|
6
|
+
module Reddy
|
7
|
+
VERSION = '0.1.0'
|
8
|
+
end
|
data/lib/reddy/bnode.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
module Reddy
|
2
|
+
class BNode
|
3
|
+
attr_accessor :identifier
|
4
|
+
def initialize(identifier = nil)
|
5
|
+
if identifier != nil && self.valid_id?(identifier) != false
|
6
|
+
@identifier = identifier
|
7
|
+
else
|
8
|
+
@identifier = "bn" + self.hash.to_i.abs.to_s
|
9
|
+
# perhaps this needs to be slightly cleverer - check whether it's negative, and if it is, append an extra bit on the end aaaaaas distinction. TODO
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def eql? (other)
|
14
|
+
other.is_a?(self.class) && other.identifier == self.identifier
|
15
|
+
end
|
16
|
+
|
17
|
+
alias_method :==, :eql?
|
18
|
+
|
19
|
+
##
|
20
|
+
# Exports the BNode in N-Triples form.
|
21
|
+
#
|
22
|
+
# ==== Example
|
23
|
+
# b = BNode.new; b.to_n3 # => returns a string of the BNode in n3 form
|
24
|
+
#
|
25
|
+
# ==== Returns
|
26
|
+
# @return [String] The BNode in n3.
|
27
|
+
#
|
28
|
+
# @author Tom Morris
|
29
|
+
|
30
|
+
def to_n3
|
31
|
+
"_:" + @identifier
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
##
|
36
|
+
# Exports the BNode in N-Triples form.
|
37
|
+
#
|
38
|
+
# ==== Example
|
39
|
+
# b = BNode.new; b.to_ntriples # => returns a string of the BNode in N-Triples form
|
40
|
+
#
|
41
|
+
# ==== Returns
|
42
|
+
# @return [String] The BNode in N-Triples.
|
43
|
+
#
|
44
|
+
# @author Tom Morris
|
45
|
+
|
46
|
+
def to_ntriples
|
47
|
+
self.to_n3
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Returns the identifier as a string.
|
52
|
+
#
|
53
|
+
# === Returns
|
54
|
+
# @return [String] Blank node identifier.
|
55
|
+
#
|
56
|
+
# @author Tom Morris
|
57
|
+
def to_s
|
58
|
+
@identifier
|
59
|
+
end
|
60
|
+
|
61
|
+
protected
|
62
|
+
def valid_id? name
|
63
|
+
if name =~ /^[a-zA-Z_][a-zA-Z0-9]*$/
|
64
|
+
true
|
65
|
+
else
|
66
|
+
false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/reddy/graph.rb
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
module Reddy
|
4
|
+
class Graph
|
5
|
+
attr_accessor :triples, :nsbinding
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@triples = []
|
9
|
+
@nsbinding = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.load (uri)
|
13
|
+
RdfXmlParser.new(open(uri)).graph
|
14
|
+
end
|
15
|
+
|
16
|
+
def size
|
17
|
+
@triples.size
|
18
|
+
end
|
19
|
+
|
20
|
+
def each
|
21
|
+
@triples.each { |value| yield value }
|
22
|
+
end
|
23
|
+
|
24
|
+
def [] (item)
|
25
|
+
@triples[item]
|
26
|
+
end
|
27
|
+
|
28
|
+
def each_with_subject(subject)
|
29
|
+
@triples.each do |value|
|
30
|
+
yield value if value.subject == subject
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_resource(subject)
|
35
|
+
@triples.find_all { |i| true if i.subject == subject}
|
36
|
+
end
|
37
|
+
|
38
|
+
##
|
39
|
+
# Adds a triple to a graph directly from the intended subject, predicate, and object.
|
40
|
+
#
|
41
|
+
# ==== Example
|
42
|
+
# g = Graph.new; g.add_triple(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new) # => results in the triple being added to g; returns an array of g's triples
|
43
|
+
#
|
44
|
+
# @param [URIRef, BNode] s the subject of the triple
|
45
|
+
# @param [URIRef] p the predicate of the triple
|
46
|
+
# @param [URIRef, BNode, Literal, TypedLiteral] o the object of the triple
|
47
|
+
#
|
48
|
+
# ==== Returns
|
49
|
+
# @return [Array] An array of the triples (leaky abstraction? consider returning the graph instead)
|
50
|
+
#
|
51
|
+
# @raise [Error] Checks parameter types and raises if they are incorrect.
|
52
|
+
# @author Tom Morris
|
53
|
+
|
54
|
+
def add_triple(s, p, o)
|
55
|
+
@triples += [ Triple.new(s, p, o) ]
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Adds an extant triple to a graph
|
60
|
+
#
|
61
|
+
# ==== Example
|
62
|
+
# g = Graph.new; t = Triple.new(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new); g << t) # => results in the triple being added to g; returns an array of g's triples
|
63
|
+
#
|
64
|
+
# @param [Triple] t the triple to be added to the graph
|
65
|
+
#
|
66
|
+
# ==== Returns
|
67
|
+
# @return [Array] An array of the triples (leaky abstraction? consider returning the graph instead)
|
68
|
+
#
|
69
|
+
# @author Tom Morris
|
70
|
+
|
71
|
+
|
72
|
+
def << (triple)
|
73
|
+
# self.add_triple(s, p, o)
|
74
|
+
@triples += [ triple ]
|
75
|
+
end
|
76
|
+
|
77
|
+
##
|
78
|
+
# Exports the graph to RDF in N-Triples form.
|
79
|
+
#
|
80
|
+
# ==== Example
|
81
|
+
# g = Graph.new; g.add_triple(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new); g.to_ntriples # => returns a string of the graph in N-Triples form
|
82
|
+
#
|
83
|
+
# ==== Returns
|
84
|
+
# @return [String] The graph in N-Triples.
|
85
|
+
#
|
86
|
+
# @author Tom Morris
|
87
|
+
|
88
|
+
def to_ntriples
|
89
|
+
@triples.collect do |t|
|
90
|
+
t.to_ntriples
|
91
|
+
end * "\n" + "\n"
|
92
|
+
end
|
93
|
+
|
94
|
+
##
|
95
|
+
# Creates a new namespace given a URI and the short name and binds it to the graph.
|
96
|
+
#
|
97
|
+
# ==== Example
|
98
|
+
# g = Graph.new; g.namespace("http://xmlns.com/foaf/0.1/", "foaf") # => binds the Foaf namespace to g
|
99
|
+
#
|
100
|
+
# @param [String] uri the URI of the namespace
|
101
|
+
# @param [String] short the short name of the namespace
|
102
|
+
#
|
103
|
+
# ==== Returns
|
104
|
+
# @return [Namespace] The newly created namespace.
|
105
|
+
#
|
106
|
+
# @raise [Error] Checks validity of the desired shortname and raises if it is incorrect.
|
107
|
+
# @raise [Error] Checks that the newly created Namespace is of type Namespace and raises if it is incorrect.
|
108
|
+
# @author Tom Morris
|
109
|
+
|
110
|
+
def namespace(uri, short)
|
111
|
+
self.bind Namespace.new(uri, short)
|
112
|
+
end
|
113
|
+
|
114
|
+
def bind(namespace)
|
115
|
+
if namespace.class == Namespace
|
116
|
+
@nsbinding["#{namespace.short}"] = namespace
|
117
|
+
else
|
118
|
+
raise
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def has_bnode_identifier?(bnodeid)
|
123
|
+
temp_bnode = BNode.new(bnodeid)
|
124
|
+
returnval = false
|
125
|
+
@triples.each { |triple|
|
126
|
+
if triple.subject.eql?(temp_bnode)
|
127
|
+
returnval = true
|
128
|
+
break
|
129
|
+
end
|
130
|
+
if triple.object.eql?(temp_bnode)
|
131
|
+
returnval = true
|
132
|
+
break
|
133
|
+
end
|
134
|
+
}
|
135
|
+
return returnval
|
136
|
+
end
|
137
|
+
|
138
|
+
def get_bnode_by_identifier(bnodeid)
|
139
|
+
temp_bnode = BNode.new(bnodeid)
|
140
|
+
each do |triple|
|
141
|
+
if triple.subject == temp_bnode
|
142
|
+
return triple.subject
|
143
|
+
end
|
144
|
+
if triple.object == temp_bnode
|
145
|
+
return triple.object
|
146
|
+
end
|
147
|
+
end
|
148
|
+
return false
|
149
|
+
end
|
150
|
+
|
151
|
+
def get_by_type(object)
|
152
|
+
out = []
|
153
|
+
each do |t|
|
154
|
+
next unless t.is_type?
|
155
|
+
next unless case object
|
156
|
+
when String
|
157
|
+
object == t.object.to_s
|
158
|
+
when Regexp
|
159
|
+
object.match(t.object.to_s)
|
160
|
+
else
|
161
|
+
object == t.object
|
162
|
+
end
|
163
|
+
out << t.subject
|
164
|
+
end
|
165
|
+
return out
|
166
|
+
end
|
167
|
+
|
168
|
+
def join(graph)
|
169
|
+
if graph.class == Graph
|
170
|
+
graph.each { |t|
|
171
|
+
self << t
|
172
|
+
}
|
173
|
+
else
|
174
|
+
raise "join requires you provide a graph object"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
# alias :add, :add_triple
|
178
|
+
# alias (=+, add_triple)
|
179
|
+
private
|
180
|
+
|
181
|
+
end
|
182
|
+
end
|