rdf-rdfobjects 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +64 -0
- data/VERSION +1 -0
- data/lib/rdf/rdfobjects.rb +28 -0
- data/lib/rdf/rdfobjects/assertion_set.rb +31 -0
- data/lib/rdf/rdfobjects/graph.rb +62 -0
- data/lib/rdf/rdfobjects/object_set.rb +105 -0
- data/lib/rdf/rdfobjects/resource.rb +220 -0
- data/lib/rdf/rdfobjects/uri.rb +27 -0
- data/lib/rdf/rdfobjects/version.rb +0 -0
- metadata +139 -0
data/README
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
RDF-RDFObjects: An Object Based Interface for RDF.rb.
|
2
|
+
|
3
|
+
Usage:
|
4
|
+
|
5
|
+
require 'rdf/rdfobjects'
|
6
|
+
|
7
|
+
require 'rdf/ntriples'
|
8
|
+
|
9
|
+
graph = RDF::Graph.load("http://rdf.rubyforge.org/doap.nt")
|
10
|
+
|
11
|
+
rdf = graph['http://rubygems.org/gems/rdf']
|
12
|
+
=> #<RDF::URI:0x8143aba8(http://rubygems.org/gems/rdf)>
|
13
|
+
|
14
|
+
Properties can be accessed either via a hash syntax or dot syntax using RDF::Vocabulary constants:
|
15
|
+
|
16
|
+
rdf[DOAP.name]
|
17
|
+
=> [#<RDF::Literal:0x81429c90("RDF.rb")>]
|
18
|
+
|
19
|
+
rdf.DOAP.documenter
|
20
|
+
=> [#<RDF::URI:0x81433254(http://ar.to/#self)>, #<RDF::URI:0x81432764(http://bhuga.net/#ben)>, #<RDF::URI:0x81431be8(http://kellogg-assoc.com/#me)>]
|
21
|
+
|
22
|
+
You can also return all the properties associated with a particular vocabulary:
|
23
|
+
|
24
|
+
rdf.DOAP
|
25
|
+
=> {"http://usefulinc.com/ns/doap#maintainer"=>[#<RDF::URI:0x8142beb4(http://ar.to/#self)>, #<RDF::URI:0x8142b3c4(http://bhuga.net/#ben)>, #<RDF::URI:0x8142a848(http://kellogg-assoc.com/#me)>], "http://usefulinc.com/ns/doap#shortdesc"=>[#<RDF::Literal:0x814288cc("A Ruby library for working with Resource Description Framework (RDF) data."@en)>], "http://usefulinc.com/ns/doap#vendor"=>[#<RDF::URI:0x81428228(http://datagraph.org/)>], "http://usefulinc.com/ns/doap#created"=>[#<RDF::Literal:0x81436580("2007-10-23")>], "http://usefulinc.com/ns/doap#documenter"=>[#<RDF::URI:0x81433254(http://ar.to/#self)>, #<RDF::URI:0x81432764(http://bhuga.net/#ben)>, #<RDF::URI:0x81431be8(http://kellogg-assoc.com/#me)>], "http://usefulinc.com/ns/doap#platform"=>[#<RDF::Literal:0x814293bc("Ruby")>], "http://usefulinc.com/ns/doap#category"=>[#<RDF::URI:0x81437b88(http://dbpedia.org/resource/Resource_Description_Framework)>, #<RDF::URI:0x814370fc(http://dbpedia.org/resource/Ruby_(programming_language))>], "http://usefulinc.com/ns/doap#description"=>[#<RDF::Literal:0x81435a90("RDF.rb is a pure-Ruby library for working with Resource Description Framework (RDF) data."@en)>], "http://usefulinc.com/ns/doap#developer"=>[#<RDF::URI:0x814353ec(http://ar.to/#self)>, #<RDF::URI:0x814348fc(http://bhuga.net/#ben)>, #<RDF::URI:0x81433d80(http://kellogg-assoc.com/#me)>], "http://usefulinc.com/ns/doap#download-page"=>[#<RDF::URI:0x814310bc(http://rubyforge.org/projects/rdf/)>], "http://usefulinc.com/ns/doap#license"=>[#<RDF::URI:0x8142c940(http://creativecommons.org/licenses/publicdomain/)>], "http://usefulinc.com/ns/doap#blog"=>[#<RDF::URI:0x81439b90(http://ar.to/)>, #<RDF::URI:0x81439104(http://blog.datagraph.org/)>], "http://usefulinc.com/ns/doap#bug-database"=>[#<RDF::URI:0x81438614(http://github.com/bendiken/rdf/issues)>], "http://usefulinc.com/ns/doap#name"=>[#<RDF::Literal:0x81429c90("RDF.rb")>], "http://usefulinc.com/ns/doap#helper"=>[#<RDF::Node:0x81430630(_:genid1)>, #<RDF::Node:0x8142feec(_:genid2)>, #<RDF::Node:0x8142f7bc(_:genid3)>, #<RDF::Node:0x8142f08c(_:genid4)>, #<RDF::Node:0x8142e95c(_:genid5)>, #<RDF::Node:0x8142e22c(_:genid6)>, #<RDF::Node:0x8142dafc(_:genid7)>], "http://usefulinc.com/ns/doap#homepage"=>[#<RDF::URI:0x8142d3cc(http://rdf.rubyforge.org/)>]}
|
26
|
+
|
27
|
+
Related resources (besides literals) work the same way:
|
28
|
+
|
29
|
+
rdf.DOAP.helper.last.FOAF.name
|
30
|
+
=> [#<RDF::Literal:0x8141f754("Pius Uzamere")>]
|
31
|
+
|
32
|
+
All changes to properties on resources affect the graph that the resource belongs to. All properties (even undefined ones) return an ObjectSet Array (which will be empty if the property is undefined). Passing a Vocabulary reference will return an Assertion Set Hash. The equals operator will replace all the statements in the graph that match the subject and predicate with the supplied object (or array of objects).
|
33
|
+
|
34
|
+
rdfobjects = graph.create('http://github.com/rsinger/rdf-rdfobjects')
|
35
|
+
=> #<RDF::URI:0x8185c9ac(http://github.com/rsinger/rdf-rdfobjects)>
|
36
|
+
|
37
|
+
rdfobjects.DOAP.maintainer << RDF::URI.new('http://dilettantes.code4lib.org/blog/about-me/#me')
|
38
|
+
=> [#<RDF::URI:0x817aeb54(http://dilettantes.code4lib.org/blog/about-me/#me)>]
|
39
|
+
|
40
|
+
rdfobjects.DC.requires << rdf
|
41
|
+
=> [#<RDF::URI:0x8143aba8(http://rubygems.org/gems/rdf)>]
|
42
|
+
|
43
|
+
rdfobjects.DOAP.name
|
44
|
+
=> []
|
45
|
+
|
46
|
+
rdfobjects.DOAP.category = rdf.DOAP.category
|
47
|
+
=> [#<RDF::URI:0x81437b88(http://dbpedia.org/resource/Resource_Description_Framework)>, #<RDF::URI:0x814370fc(http://dbpedia.org/resource/Ruby_(programming_language))>]
|
48
|
+
|
49
|
+
rdfobjects.DOAP.category = rdf.DOAP.category.first
|
50
|
+
=> #<RDF::URI:0x81437b88(http://dbpedia.org/resource/Resource_Description_Framework)>
|
51
|
+
|
52
|
+
It also provides some convenience methods:
|
53
|
+
|
54
|
+
rdfobjects.predicates
|
55
|
+
=> [#<RDF::URI:0x8172f5e8(http://purl.org/dc/terms/requires)>, #<RDF::URI:0x8145ce10(http://usefulinc.com/ns/doap#maintainer)>, #<RDF::URI:0x8146b514(http://usefulinc.com/ns/doap#category)>]
|
56
|
+
|
57
|
+
rdfobjects.properties
|
58
|
+
=> {#<RDF::URI:0x8172f5e8(http://purl.org/dc/terms/requires)>=>[#<RDF::URI:0x8143aba8(http://rubygems.org/gems/rdf)>], #<RDF::URI:0x8145ce10(http://usefulinc.com/ns/doap#maintainer)>=>[#<RDF::URI:0x817aeb54(http://dilettantes.code4lib.org/blog/about-me/#me)>], #<RDF::URI:0x8146b514(http://usefulinc.com/ns/doap#category)>=>[#<RDF::URI:0x81437b88(http://dbpedia.org/resource/Resource_Description_Framework)>]}
|
59
|
+
|
60
|
+
rdfobjects.to_ntriples
|
61
|
+
=> "<http://github.com/rsinger/rdf-rdfobjects> <http://purl.org/dc/terms/requires> <http://rubygems.org/gems/rdf> .\n<http://github.com/rsinger/rdf-rdfobjects> <http://usefulinc.com/ns/doap#maintainer> <http://dilettantes.code4lib.org/blog/about-me/#me> .\n<http://github.com/rsinger/rdf-rdfobjects> <http://usefulinc.com/ns/doap#category> <http://dbpedia.org/resource/Resource_Description_Framework> .\n"
|
62
|
+
|
63
|
+
.to_ntriples is also available on RDF::Graph.
|
64
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rdf' # @see http://rubygems.org/gems/rdf
|
2
|
+
|
3
|
+
module RDF
|
4
|
+
module RDFObjects
|
5
|
+
autoload :VERSION, File.dirname(__FILE__) + '/rdfobjects/version'
|
6
|
+
autoload :AssertionSet, File.dirname(__FILE__) + '/rdfobjects/assertion_set'
|
7
|
+
autoload :Graph, File.dirname(__FILE__) + '/rdfobjects/graph'
|
8
|
+
autoload :ObjectSet, File.dirname(__FILE__) + '/rdfobjects/object_set'
|
9
|
+
autoload :Resource, File.dirname(__FILE__) + '/rdfobjects/resource'
|
10
|
+
autoload :Resource, File.dirname(__FILE__) + '/rdfobjects/resource'
|
11
|
+
end
|
12
|
+
class URI
|
13
|
+
include RDFObjects::Resource
|
14
|
+
# Don't freeze the URIs. Keep an eye on this, since, presumably, URIs are being frozen for a reason.
|
15
|
+
# However, we cannot add graphs to frozen objects and the ObjectStore complicates things.
|
16
|
+
def self.intern(str)
|
17
|
+
(cache[str = str.to_s] ||= self.new(str))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Node
|
22
|
+
include RDFObjects::Resource
|
23
|
+
end
|
24
|
+
|
25
|
+
class Graph
|
26
|
+
include RDFObjects::Graph
|
27
|
+
end
|
28
|
+
end # RDF
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module RDF::RDFObjects
|
2
|
+
module AssertionSet
|
3
|
+
attr_reader :vocabulary, :subject
|
4
|
+
def vocabulary=(v)
|
5
|
+
@vocabulary = v
|
6
|
+
end
|
7
|
+
def subject=(s)
|
8
|
+
@subject=s
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_missing(meth, *args)
|
12
|
+
if meth.to_s =~ /\=$/
|
13
|
+
send(:set_property, @vocabulary.send(meth.to_s.sub(/\=$/,'').to_sym), args)
|
14
|
+
elsif prop = @vocabulary.send(meth)
|
15
|
+
send(:"[]", prop)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def [](key)
|
20
|
+
if self.keys.index(key.to_s)
|
21
|
+
@subject[key]
|
22
|
+
else
|
23
|
+
send(:set_property, key, nil)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
def set_property(p, args)
|
27
|
+
@subject[p] = args
|
28
|
+
@subject[p]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module RDF::RDFObjects
|
2
|
+
module Graph
|
3
|
+
def create(*args, &block)
|
4
|
+
r = RDF::Resource.new(args.shift, *args, &block)
|
5
|
+
r.graph = self
|
6
|
+
r
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](id)
|
10
|
+
id = RDF::Resource.new(id) unless id.is_a?(Resource)
|
11
|
+
r = self.first_subject(:subject=>id)
|
12
|
+
r = self.first_object(:object=>id) unless r
|
13
|
+
r.graph = self if r
|
14
|
+
r
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_ntriples
|
18
|
+
RDF::Writer.for(:ntriples).buffer do |writer|
|
19
|
+
self.each_statement do |statement|
|
20
|
+
writer << statement
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def <<(data)
|
26
|
+
case data
|
27
|
+
when RDF::RDFObjects::Resource
|
28
|
+
data.statements.each do |stmt|
|
29
|
+
self.insert(stmt)
|
30
|
+
end
|
31
|
+
else
|
32
|
+
super(data)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def insert_statement(statement)
|
37
|
+
statement = statement.dup
|
38
|
+
statement.context = context
|
39
|
+
statement.each_triple.each do |s,p,o|
|
40
|
+
puts s.to_s
|
41
|
+
s.graph = self if s.is_a?(RDF::Resource)
|
42
|
+
end
|
43
|
+
@data.insert(statement)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module RDF
|
49
|
+
class Graph
|
50
|
+
def insert_statement(statement)
|
51
|
+
statement = statement.dup
|
52
|
+
statement.context = context
|
53
|
+
statement.to_triple.each_cons(3) do |s,p,o|
|
54
|
+
s.graph = self
|
55
|
+
o.graph = self if o.is_a?(RDF::Resource) && !o.frozen?
|
56
|
+
end
|
57
|
+
@data.insert(statement)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module RDF::RDFObjects
|
2
|
+
module ObjectSet
|
3
|
+
attr_reader :subject, :predicate
|
4
|
+
def <<(o)
|
5
|
+
[*o].each do |object|
|
6
|
+
next unless object
|
7
|
+
@subject.push(@predicate, object)
|
8
|
+
end
|
9
|
+
self.replace(@subject[@predicate])
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_statement(stmt)
|
13
|
+
@subject = stmt.subject
|
14
|
+
@predicate = stmt.predicate
|
15
|
+
end
|
16
|
+
def clear
|
17
|
+
@subject[@predicate]=nil
|
18
|
+
super
|
19
|
+
end
|
20
|
+
def concat(ary)
|
21
|
+
ary.each do |i|
|
22
|
+
@subject.push(@predicate, i)
|
23
|
+
end
|
24
|
+
self.replace(@subject[@predicate])
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete(o)
|
28
|
+
d = super(o)
|
29
|
+
@subject.remove(@predicate, o)
|
30
|
+
d
|
31
|
+
end
|
32
|
+
|
33
|
+
def delete_at(i)
|
34
|
+
d = super(i)
|
35
|
+
@subject.remove(@predicate, d) if d
|
36
|
+
d
|
37
|
+
end
|
38
|
+
|
39
|
+
def fill(*args)
|
40
|
+
super(args)
|
41
|
+
@subject[@predicate]=self
|
42
|
+
@subject[@predicate]
|
43
|
+
end
|
44
|
+
def replace(ary)
|
45
|
+
obj_set=@subject[@predicate]=ary
|
46
|
+
super(obj_set)
|
47
|
+
end
|
48
|
+
|
49
|
+
def insert(index, *obj)
|
50
|
+
super(index, obj)
|
51
|
+
@subject[@predicate]=self
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def pop(n=nil)
|
56
|
+
p = super(n)
|
57
|
+
[*p].each do |obj|
|
58
|
+
@subject.remove(@predicate, obj) if obj
|
59
|
+
end
|
60
|
+
self
|
61
|
+
end
|
62
|
+
|
63
|
+
# def push(*obj)
|
64
|
+
# super(obj)
|
65
|
+
# @subject[@predicate] = self
|
66
|
+
# self
|
67
|
+
# end
|
68
|
+
|
69
|
+
def reverse!
|
70
|
+
super
|
71
|
+
@subject[@predicate] = self
|
72
|
+
self
|
73
|
+
end
|
74
|
+
|
75
|
+
def rotate!(cnt=1)
|
76
|
+
super(cnt)
|
77
|
+
@subject[@predicate] = self
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
def shift(n=nil)
|
82
|
+
s = super(n)
|
83
|
+
@subject[@predicate] = self
|
84
|
+
s
|
85
|
+
end
|
86
|
+
|
87
|
+
def shuffle!
|
88
|
+
super
|
89
|
+
@subject[@predicate] = self
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
93
|
+
def sort!
|
94
|
+
super
|
95
|
+
@subject[@predicate] = self
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
def unshift(*obj)
|
100
|
+
super(obj)
|
101
|
+
@subject[@predicate] = self
|
102
|
+
self
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,220 @@
|
|
1
|
+
module RDF::RDFObjects
|
2
|
+
module Resource
|
3
|
+
attr_reader :graph
|
4
|
+
def graph=(graph)
|
5
|
+
@graph = graph
|
6
|
+
end
|
7
|
+
|
8
|
+
def assert(p, o)
|
9
|
+
p = RDF::URI.intern(p) unless p.is_a?(RDF::URI)
|
10
|
+
unless o.is_a?(RDF::Resource) || o.is_a?(RDF::Literal)
|
11
|
+
o = RDF::Literal.new(o)
|
12
|
+
end
|
13
|
+
@graph << [self, p, o]
|
14
|
+
end
|
15
|
+
|
16
|
+
def relate(p, o)
|
17
|
+
o = RDF::Resource.new(o) unless o.is_a?(RDF::Resource)
|
18
|
+
assert(p, o)
|
19
|
+
end
|
20
|
+
|
21
|
+
def [](p)
|
22
|
+
p = RDF::Resource.new(p) if p.is_a?(String)
|
23
|
+
results = {}
|
24
|
+
if p.is_a?(RDF::Resource)
|
25
|
+
if p == RDF.to_rdf
|
26
|
+
regex = Regexp.new(p.to_s)
|
27
|
+
if assertions
|
28
|
+
assertions.each_pair do |pred, objects|
|
29
|
+
results[pred.to_s] = objects if pred.to_s =~ /^#{regex}/
|
30
|
+
end
|
31
|
+
end
|
32
|
+
results.extend(AssertionSet)
|
33
|
+
results.vocabulary = p
|
34
|
+
results.subject = self
|
35
|
+
else
|
36
|
+
@graph.query(:subject=>self, :predicate=>p).each do |r|
|
37
|
+
if r.object.is_a?(RDF::Resource)
|
38
|
+
unless r.object.frozen?
|
39
|
+
r.object.extend(RDF::RDFObjects::Resource)
|
40
|
+
r.object.graph = self.graph
|
41
|
+
end
|
42
|
+
end
|
43
|
+
results[r.predicate] ||=[]
|
44
|
+
results[r.predicate] << r.object
|
45
|
+
end
|
46
|
+
end
|
47
|
+
elsif p.is_a?(Class) && p.inspect =~ /^RDF::Vocabulary\(/
|
48
|
+
regex = Regexp.new(p.to_s)
|
49
|
+
if assertions
|
50
|
+
assertions.each_pair do |pred, objects|
|
51
|
+
results[pred.to_s] = objects if pred.to_s =~ /^#{regex}/
|
52
|
+
end
|
53
|
+
end
|
54
|
+
results.extend(AssertionSet)
|
55
|
+
results.vocabulary = p
|
56
|
+
results.subject = self
|
57
|
+
|
58
|
+
elsif (RDF.const_defined?(p) && RDF.const_get(p).ancestors.index(RDF::Vocabulary))
|
59
|
+
regex = Regexp.new(RDF.const_get(p).to_s)
|
60
|
+
if assertions
|
61
|
+
assertions.each_pair do |pred, objects|
|
62
|
+
results[pred.to_s] = objects if pred.to_s =~ /^#{regex}/
|
63
|
+
end
|
64
|
+
end
|
65
|
+
results.extend(AssertionSet)
|
66
|
+
results.vocabulary = RDF.const_get(p)
|
67
|
+
results.subject = self
|
68
|
+
end
|
69
|
+
|
70
|
+
if results.is_a?(AssertionSet)
|
71
|
+
results.each_pair do |pred, objs|
|
72
|
+
objs.extend(ObjectSet)
|
73
|
+
objs.set_statement(RDF::Statement.new(self, pred, ""))
|
74
|
+
end
|
75
|
+
obj_set = results
|
76
|
+
else
|
77
|
+
obj_set = case results.keys.length
|
78
|
+
when 0
|
79
|
+
set = []
|
80
|
+
set.extend(ObjectSet)
|
81
|
+
set.set_statement(RDF::Statement.new(self,p,""))
|
82
|
+
set
|
83
|
+
when 1
|
84
|
+
set = []
|
85
|
+
results.values.first.each do |v|
|
86
|
+
set << v
|
87
|
+
end
|
88
|
+
set.extend(ObjectSet)
|
89
|
+
set.set_statement(RDF::Statement.new(self,p,""))
|
90
|
+
set
|
91
|
+
end
|
92
|
+
end
|
93
|
+
obj_set
|
94
|
+
end
|
95
|
+
|
96
|
+
def []=(p,o)
|
97
|
+
p = RDF::URI.intern(p) unless p.is_a?(RDF::URI)
|
98
|
+
@graph.query(:subject=>self, :predicate=>p).each {|stmt| @graph.delete(stmt)}
|
99
|
+
[*o].each do |obj|
|
100
|
+
next unless obj
|
101
|
+
obj = cast_as_typed_object(obj)
|
102
|
+
if obj.is_a?(RDF::Resource) && !obj.frozen?
|
103
|
+
obj.graph = @graph
|
104
|
+
elsif obj.is_a?(RDF::Resource) && obj.frozen? && obj.graph != @graph
|
105
|
+
obj = RDF::URI.new(obj)
|
106
|
+
obj.graph = @graph
|
107
|
+
obj.freeze
|
108
|
+
end
|
109
|
+
@graph << [self, p, obj] unless obj.nil?
|
110
|
+
end
|
111
|
+
self[p]
|
112
|
+
end
|
113
|
+
|
114
|
+
def remove(p, o=nil)
|
115
|
+
p = RDF::URI.intern(p) unless p.is_a?(RDF::URI)
|
116
|
+
removals = []
|
117
|
+
if o
|
118
|
+
o = cast_as_typed_object(o)
|
119
|
+
query = {:subject=>self, :predicate=>p, :object=>o}
|
120
|
+
else
|
121
|
+
query = {:subject=>self, :predicate=>p}
|
122
|
+
end
|
123
|
+
@graph.query(query).each do |stmt|
|
124
|
+
removals << stmt
|
125
|
+
@graph.delete(stmt)
|
126
|
+
end
|
127
|
+
if o
|
128
|
+
removals.first
|
129
|
+
else
|
130
|
+
removals
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def push(p, o)
|
135
|
+
p = RDF::URI.intern(p) unless p.is_a?(RDF::URI)
|
136
|
+
o = cast_as_typed_object(o)
|
137
|
+
@graph << [self, p, o]
|
138
|
+
end
|
139
|
+
|
140
|
+
def cast_as_typed_object(o)
|
141
|
+
return o if o.is_a?(RDF::Literal) || o.is_a?(RDF::Resource)
|
142
|
+
return Literal.new(o)
|
143
|
+
end
|
144
|
+
|
145
|
+
def predicates
|
146
|
+
p = []
|
147
|
+
@graph.query(:subject=>self).each {|stmt| p << stmt.predicate unless p.include?(stmt.predicate)}
|
148
|
+
p
|
149
|
+
end
|
150
|
+
|
151
|
+
def statements
|
152
|
+
stmts = []
|
153
|
+
@graph.query(:subject=>self, :predicate=>p).each {|stmt| stmts << stmt}
|
154
|
+
stmts
|
155
|
+
end
|
156
|
+
|
157
|
+
def object_of
|
158
|
+
subjects = []
|
159
|
+
@graph.query(:object=>self).each do |stmt|
|
160
|
+
s = stmt.subject
|
161
|
+
s.extend(RDF::RDFObjects::Resource)
|
162
|
+
s.graph = @graph
|
163
|
+
subjects << s
|
164
|
+
end
|
165
|
+
subjects
|
166
|
+
end
|
167
|
+
|
168
|
+
def properties
|
169
|
+
results = {}
|
170
|
+
@graph.query(:subject=>self).each do |r|
|
171
|
+
if r.object.is_a?(RDF::Resource)
|
172
|
+
r.object.graph = self.graph unless r.object.frozen?
|
173
|
+
end
|
174
|
+
results[r.predicate] ||=[]
|
175
|
+
results[r.predicate] << r.object
|
176
|
+
end
|
177
|
+
|
178
|
+
obj_set = case results.keys.length
|
179
|
+
when 0 then nil
|
180
|
+
else
|
181
|
+
results.each_pair do |pred, objs|
|
182
|
+
#objs.each do |o|
|
183
|
+
objs.extend(ObjectSet)
|
184
|
+
objs.set_statement(RDF::Statement.new(self, pred, ""))
|
185
|
+
#end
|
186
|
+
#results[pred] = objs.first if objs.length == 1
|
187
|
+
results
|
188
|
+
end
|
189
|
+
end
|
190
|
+
obj_set
|
191
|
+
end
|
192
|
+
alias :assertions :properties
|
193
|
+
|
194
|
+
def type=(rdf_type)
|
195
|
+
self[RDF.type] = rdf_type
|
196
|
+
end
|
197
|
+
|
198
|
+
def type
|
199
|
+
self[RDF.type]
|
200
|
+
end
|
201
|
+
|
202
|
+
def clear
|
203
|
+
@graph.query(:subject=>self).each {|stmt| @graph.delete(stmt)}
|
204
|
+
end
|
205
|
+
|
206
|
+
def method_missing(meth, *args)
|
207
|
+
if RDF.const_defined?(meth) && RDF.const_get(meth).ancestors.index(RDF::Vocabulary)
|
208
|
+
send(:"[]", meth)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
def to_ntriples
|
213
|
+
RDF::Writer.for(:ntriples).buffer do |writer|
|
214
|
+
@graph.query(:subject=>self).each_statement do |statement|
|
215
|
+
writer << statement
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RDF
|
2
|
+
class URI
|
3
|
+
##
|
4
|
+
# @return [RDF::Util::Cache]
|
5
|
+
# @private
|
6
|
+
def self.cache
|
7
|
+
require 'rdf/util/cache' unless defined?(::RDF::Util::Cache)
|
8
|
+
@cache ||= {}
|
9
|
+
@cache[@graph] ||= RDF::Util::Cache.new(CACHE_SIZE)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.intern(str)
|
13
|
+
(cache[@graph][str = str.to_s] ||= self.new(str)).freeze
|
14
|
+
end
|
15
|
+
|
16
|
+
def graph=(graph)
|
17
|
+
raise ArgumentError unless graph.is_a?(::RDF::Graph)
|
18
|
+
if !self.frozen? && graph != self.graph
|
19
|
+
@graph = graph
|
20
|
+
elsif self.frozen? && graph != self.graph
|
21
|
+
new_uri = self.dup
|
22
|
+
self = new_uri
|
23
|
+
@graph = graph
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rdf-rdfobjects
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ross Singer
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-25 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: addressable
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 2
|
33
|
+
version: "2.2"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rdf
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 17
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
- 3
|
48
|
+
- 1
|
49
|
+
version: 0.3.1
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: yard
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 7
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
- 6
|
64
|
+
- 0
|
65
|
+
version: 0.6.0
|
66
|
+
type: :development
|
67
|
+
version_requirements: *id003
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: rspec
|
70
|
+
prerelease: false
|
71
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 11
|
77
|
+
segments:
|
78
|
+
- 2
|
79
|
+
- 1
|
80
|
+
- 0
|
81
|
+
version: 2.1.0
|
82
|
+
type: :development
|
83
|
+
version_requirements: *id004
|
84
|
+
description: An object oriented interface for working with Resource Description Framework (RDF) data in RDF.rb.
|
85
|
+
email: rdfobjects@googlegroups.com
|
86
|
+
executables: []
|
87
|
+
|
88
|
+
extensions: []
|
89
|
+
|
90
|
+
extra_rdoc_files: []
|
91
|
+
|
92
|
+
files:
|
93
|
+
- README
|
94
|
+
- VERSION
|
95
|
+
- lib/rdf/rdfobjects/assertion_set.rb
|
96
|
+
- lib/rdf/rdfobjects/graph.rb
|
97
|
+
- lib/rdf/rdfobjects/object_set.rb
|
98
|
+
- lib/rdf/rdfobjects/resource.rb
|
99
|
+
- lib/rdf/rdfobjects/uri.rb
|
100
|
+
- lib/rdf/rdfobjects/version.rb
|
101
|
+
- lib/rdf/rdfobjects.rb
|
102
|
+
has_rdoc: false
|
103
|
+
homepage: http://github.com/rsinger/rdf-rdfobjects
|
104
|
+
licenses:
|
105
|
+
- Public Domain
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
hash: 53
|
117
|
+
segments:
|
118
|
+
- 1
|
119
|
+
- 8
|
120
|
+
- 1
|
121
|
+
version: 1.8.1
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
hash: 3
|
128
|
+
segments:
|
129
|
+
- 0
|
130
|
+
version: "0"
|
131
|
+
requirements: []
|
132
|
+
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 1.3.7
|
135
|
+
signing_key:
|
136
|
+
specification_version: 3
|
137
|
+
summary: An object oriented interface for working with Resource Description Framework (RDF) data in RDF.rb.
|
138
|
+
test_files: []
|
139
|
+
|