rdfobjects-changeset 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/README +25 -0
- data/lib/rdf_objects/changeset.rb +8 -0
- data/lib/rdf_objects/changeset/changeset.rb +131 -0
- metadata +98 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2007 Ross Singer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
RDFObjects ChangeSets
|
2
|
+
|
3
|
+
Provide a way to more easily work with RDF ChangeSets (http://n2.talis.com/wiki/Changesets) in RDFObjects.
|
4
|
+
|
5
|
+
Example:
|
6
|
+
|
7
|
+
>> require 'rdf_objects/changeset'
|
8
|
+
>> resource = RDFObject::Resource.new('http://example.org/1')
|
9
|
+
>> resource.assert("[foaf:name]", "Ross Singer")
|
10
|
+
>> resource.relate("[foaf:homepage]", "http://dilettantes.code4lib.org/")
|
11
|
+
>> changeset = RDFObject::Resource.new(resource)
|
12
|
+
>> changeset.delete_all
|
13
|
+
>> changeset.to_xml
|
14
|
+
=> "<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cs="http://purl.org/vocab/changeset/schema#"><rdf:Description rdf:nodeID="b4b928b0-5b8a-012d-d2b5-001b63a48175"><cs:subjectOfChange rdf:resource="http://example.org/1" /><cs:createdDate rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2010-06-16T11:35:24-04:00</cs:createdDate><cs:removal><rdf:Description rdf:nodeID="b7c740d0-5b8a-012d-d2b5-001b63a48175"><rdf:predicate rdf:resource="http://xmlns.com/foaf/0.1/name" /><rdf:subject rdf:resource="http://example.org/1" /><rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement" /><rdf:object>Ross Singer</rdf:object></rdf:Description></cs:removal><cs:removal><rdf:Description rdf:nodeID="b7d8d920-5b8a-012d-d2b5-001b63a48175"><rdf:predicate rdf:resource="http://xmlns.com/foaf/0.1/homepage" /><rdf:subject rdf:resource="http://example.org/1" /><rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement" /><rdf:object rdf:resource="http://dilettantes.code4lib.org/" /></rdf:Description></cs:removal><rdf:type rdf:resource="http://purl.org/vocab/changeset/schema#ChangeSet" /></rdf:Description></rdf:RDF>"
|
15
|
+
|
16
|
+
|
17
|
+
>> changeset = RDFObject::ChangeSet.new(resource)
|
18
|
+
>> changeset.remove_statement("[foaf:homepage]", resource.foaf['homepage]'])
|
19
|
+
>> changeset.add_statement("[foaf:homepage]", RDFObject::Resource.new('http://dilettantes.code4lib.org/blog/'))
|
20
|
+
>> changeset.to_xml
|
21
|
+
=> "<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cs="http://purl.org/vocab/changeset/schema#"><rdf:Description rdf:nodeID="d6efad10-5b8c-012d-d2b5-001b63a48175"><cs:subjectOfChange rdf:resource="http://example.org/1" /><cs:createdDate rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2010-06-16T11:50:41-04:00</cs:createdDate><cs:removal><rdf:Description rdf:nodeID="d7fe6d70-5b8c-012d-d2b5-001b63a48175"><rdf:predicate rdf:resource="http://xmlns.com/foaf/0.1/homepage" /><rdf:subject rdf:resource="http://example.org/1" /><rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement" /><rdf:object rdf:resource="http://dilettantes.code4lib.org/" /></rdf:Description></cs:removal><cs:addition><rdf:Description rdf:nodeID="077e1400-5b8d-012d-d2b5-001b63a48175"><rdf:predicate rdf:resource="http://xmlns.com/foaf/0.1/homepage" /><rdf:subject rdf:resource="http://example.org/1" /><rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement" /><rdf:object rdf:resource="http://dilettantes.code4lib.org/blog/" /></rdf:Description></cs:addition><rdf:type rdf:resource="http://purl.org/vocab/changeset/schema#ChangeSet" /></rdf:Description></rdf:RDF>"
|
22
|
+
|
23
|
+
Currently RDFObjects ChangeSets only uses blank nodes.
|
24
|
+
|
25
|
+
|
@@ -0,0 +1,131 @@
|
|
1
|
+
module RDFObject
|
2
|
+
|
3
|
+
class ChangeSet < BlankNode
|
4
|
+
attr_reader :resource, :additions, :removals
|
5
|
+
def initialize(resource, versioned=false)
|
6
|
+
@resource = resource
|
7
|
+
@versioned = versioned
|
8
|
+
@additions = []
|
9
|
+
@removals = []
|
10
|
+
super(UUID.generate)
|
11
|
+
|
12
|
+
assert("[cs:createdDate]", Time.now)
|
13
|
+
relate("[rdf:type]", "[cs:ChangeSet]")
|
14
|
+
relate("[cs:subjectOfChange]", @resource.uri)
|
15
|
+
end
|
16
|
+
|
17
|
+
def reason=(reason_note)
|
18
|
+
self.cs['changeReason'] = nil if self.cs['changeReason']
|
19
|
+
assert("[cs:changeReason]", reason_note)
|
20
|
+
end
|
21
|
+
|
22
|
+
def creator_name=(name)
|
23
|
+
self.cs['creatorName'] = nil if self.cs['creatorName']
|
24
|
+
assert("[cs:creatorName]", name)
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete_all()
|
28
|
+
@resource.assertions.each_pair do |predicate, objects|
|
29
|
+
[*objects].each do |object|
|
30
|
+
next unless object
|
31
|
+
@removals << create_statement(predicate, object)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_statement(predicate, object)
|
37
|
+
@additions << create_statement(predicate, object)
|
38
|
+
end
|
39
|
+
|
40
|
+
def remove_statement(predicate, object)
|
41
|
+
@removals << create_statement(predicate, object)
|
42
|
+
end
|
43
|
+
|
44
|
+
def replace_statements(from, to=nil)
|
45
|
+
from.each_pair do |pred, objects|
|
46
|
+
[*objects].each do |o|
|
47
|
+
next unless o
|
48
|
+
@removals << create_statement(pred, o)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
if to and to.is_a?(Hash) and !to.empty?
|
52
|
+
to.each_pair do |pred, objects|
|
53
|
+
[*objects].each do |o|
|
54
|
+
next unless o
|
55
|
+
@additions << create_statement(pred, o)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def clear_predicate(uri)
|
62
|
+
[*@resource[uri]].each do |object|
|
63
|
+
next unless object
|
64
|
+
@removals << create_statement(uri, object)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def create_statement(predicate, object)
|
69
|
+
stmt = BlankNode.new(UUID.generate)
|
70
|
+
stmt.relate("[rdf:type]", "[rdf:Statement]")
|
71
|
+
stmt.relate("[rdf:subject]", @resource.uri)
|
72
|
+
stmt.relate("[rdf:predicate]", predicate)
|
73
|
+
stmt.assert("[rdf:object]", object)
|
74
|
+
stmt
|
75
|
+
end
|
76
|
+
|
77
|
+
def rdf_description_block(depth=0)
|
78
|
+
rdf = "<rdf:Description #{xml_subject_attribute}>"
|
79
|
+
namespaces = {}
|
80
|
+
Curie.get_mappings.each_pair do |key, value|
|
81
|
+
if self.respond_to?(key.to_sym)
|
82
|
+
self.send(key.to_sym).each_pair do | predicate, objects |
|
83
|
+
[*objects].each do | object |
|
84
|
+
rdf << "<#{key}:#{predicate}"
|
85
|
+
namespaces["xmlns:#{key}"] = "#{Curie.parse("[#{key}:]")}"
|
86
|
+
if object.is_a?(RDFObject::ResourceReference) || object.is_a?(RDFObject::BlankNode)
|
87
|
+
if depth == 0
|
88
|
+
rdf << " #{object.xml_object_attribute} />"
|
89
|
+
else
|
90
|
+
rdf << ">"
|
91
|
+
ns, rdf_data = object.rdf_description_block(depth-1)
|
92
|
+
namespaces.merge!(ns)
|
93
|
+
rdf << rdf_data
|
94
|
+
rdf << "</#{key}:#{predicate}>"
|
95
|
+
end
|
96
|
+
else
|
97
|
+
object = RDF::Literal.new(object) unless object.is_a?(RDF::Literal)
|
98
|
+
if object.language
|
99
|
+
rdf << " xml:lang=\"#{object.language}\""
|
100
|
+
end
|
101
|
+
if object.datatype
|
102
|
+
rdf << " rdf:datatype=\"#{object.datatype}\""
|
103
|
+
end
|
104
|
+
rdf << ">#{CGI.escapeHTML(object.value)}</#{key}:#{predicate}>"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
if value == "http://purl.org/vocab/changeset/schema#"
|
110
|
+
@removals.each do |removal|
|
111
|
+
rdf << "<#{key}:removal>"
|
112
|
+
ns, rdf_data = removal.rdf_description_block
|
113
|
+
namespaces.merge!(ns)
|
114
|
+
rdf << rdf_data
|
115
|
+
rdf << "</#{key}:removal>"
|
116
|
+
end
|
117
|
+
@additions.each do |addition|
|
118
|
+
rdf << "<#{key}:addition>"
|
119
|
+
ns, rdf_data = addition.rdf_description_block
|
120
|
+
namespaces.merge!(ns)
|
121
|
+
rdf << rdf_data
|
122
|
+
rdf << "</#{key}:addition>"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
rdf << "</rdf:Description>"
|
128
|
+
[namespaces, rdf]
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rdfobjects-changeset
|
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: 2010-06-16 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rdfobjects
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: uuid
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: "An extension to RDFObjects to more easily work with RDF ChangeSets: http://n2.talis.com/wiki/Changesets"
|
50
|
+
email: rossfsinger@gmail.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files:
|
56
|
+
- LICENSE
|
57
|
+
- README
|
58
|
+
files:
|
59
|
+
- LICENSE
|
60
|
+
- README
|
61
|
+
- lib/rdf_objects/changeset.rb
|
62
|
+
- lib/rdf_objects/changeset/changeset.rb
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: http://github.com/rsinger/rdfobjects-changesets
|
65
|
+
licenses: []
|
66
|
+
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options:
|
69
|
+
- --charset=UTF-8
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.3.7
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: An extension to RDFObjects to more easily work with RDF ChangeSets.
|
97
|
+
test_files: []
|
98
|
+
|