Class: RDF::Changeset

Inherits:
Object
  • Object
show all
Defined in:
lib/changesets/rdf_changeset.rb

Constant Summary collapse

CONTENT_TYPE_TURTLE =

Media type for Changesets

"application/vnd.talis.changeset+turtle"
CONTENT_TYPE_XML =
"application/vnd.talis.changeset+xml"
DEFAULT_REASON =

Default reason for applying the update to a graph

"Generated in changeset.rb"
DEFAULT_CREATOR =

Default name/label for the change agent

"changeset.rb"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject_of_change, change_reason = DEFAULT_REASON, creator_name = DEFAULT_CREATOR) ⇒ Changeset

Returns a new instance of Changeset.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/changesets/rdf_changeset.rb', line 30

def initialize(subject_of_change, change_reason=DEFAULT_REASON, creator_name=DEFAULT_CREATOR)
  @resource = RDF::Node.new
  @subject_of_change = subject_of_change
  @statements = []
  @statements.concat [RDF::Statement.new(@resource, RDF.type, RDF::Talis::Changeset.ChangeSet),
   RDF::Statement.new(@resource, RDF::Talis::Changeset.changeReason, change_reason),
   RDF::Statement.new(@resource, RDF::Talis::Changeset.creatorName, creator_name),
   RDF::Statement.new(@resource, RDF::Talis::Changeset.createdDate, Time.now),
   RDF::Statement.new(@resource, RDF::Talis::Changeset.subjectOfChange, subject_of_change)]
   if block_given?
     yield self
   end
end

Instance Attribute Details

#statementsObject (readonly)

Returns the value of attribute statements.



28
29
30
# File 'lib/changesets/rdf_changeset.rb', line 28

def statements
  @statements
end

#subject_of_changeObject (readonly)

Returns the value of attribute subject_of_change.



28
29
30
# File 'lib/changesets/rdf_changeset.rb', line 28

def subject_of_change
  @subject_of_change
end

Class Method Details

.remove_properties(subject, predicate, graph, change_reason = DEFAULT_REASON, creator_name = DEFAULT_CREATOR) ⇒ Object

Remove all statements with a specific predicate for a specific resource



105
106
107
108
109
110
111
112
# File 'lib/changesets/rdf_changeset.rb', line 105

def Changeset.remove_properties(subject, predicate, graph, change_reason=DEFAULT_REASON, creator_name=DEFAULT_CREATOR)
  cs = Changeset.new(subject, change_reason, creator_name) do |cs|
    graph.query( [subject, predicate, nil] ).each do |statement|
      cs.remove_statements( statement )
    end              
  end                  
  cs
end

.remove_property(subject, predicate, value, change_reason = DEFAULT_REASON, creator_name = DEFAULT_CREATOR) ⇒ Object

Remove a specific single property for a subject



92
93
94
# File 'lib/changesets/rdf_changeset.rb', line 92

def Changeset.remove_property(subject, predicate, value, change_reason=DEFAULT_REASON, creator_name=DEFAULT_CREATOR)
  return Changeset.remove_statement( RDF::Statement.new( subject, predicate, value ) )
end

.remove_statement(statement, change_reason = DEFAULT_REASON, creator_name = DEFAULT_CREATOR) ⇒ Object

Remove a statement



97
98
99
100
101
102
# File 'lib/changesets/rdf_changeset.rb', line 97

def Changeset.remove_statement( statement, change_reason=DEFAULT_REASON, creator_name=DEFAULT_CREATOR)
  cs = Changeset.new(statement.subject, change_reason, creator_name) do |cs|
    cs.remove_statements( statement )
  end
  cs
end

.remove_subject(subject, graph, change_reason = DEFAULT_REASON, creator_name = DEFAULT_CREATOR) ⇒ Object

Remove all statements where the indicated resource is the subject



115
116
117
118
119
120
121
122
# File 'lib/changesets/rdf_changeset.rb', line 115

def Changeset.remove_subject(subject, graph, change_reason=DEFAULT_REASON, creator_name=DEFAULT_CREATOR)
  cs = Changeset.new(subject, change_reason, creator_name) do |cs|
    graph.query( [subject, nil, nil] ).each do |statement|
      cs.remove_statements( statement )
    end              
  end                  
  cs      
end

.update(subject, new, old, change_reason = DEFAULT_REASON, creator_name = DEFAULT_CREATOR) ⇒ Object

Apply an update by removing all statements for the indicated subject from the old graph, replacing it with statements in the new graph



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/changesets/rdf_changeset.rb', line 126

def Changeset.update(subject, new, old, change_reason=DEFAULT_REASON, creator_name=DEFAULT_CREATOR)
  cs = Changeset.new(subject, change_reason, creator_name) do |cs|
    old.query( [subject, nil, nil] ).each do |statement|
      cs.remove_statements( statement )
    end        
    new.query( [subject, nil, nil] ).each do |statement|
      cs.add_statements( statement )
    end
  end      
  cs
end

.update_property(subject, predicate, old_value, new_value, change_reason = DEFAULT_REASON, creator_name = DEFAULT_CREATOR) ⇒ Object

Update a predicate from one value to another Will only remove the specified old value, other values for predicate will remain unchanged



83
84
85
86
87
88
89
# File 'lib/changesets/rdf_changeset.rb', line 83

def Changeset.update_property(subject, predicate, old_value, new_value, change_reason=DEFAULT_REASON, creator_name=DEFAULT_CREATOR)
  cs = Changeset.new(subject, change_reason, creator_name) do |cs|
    cs.remove_statements( RDF::Statement.new( subject, predicate, old_value ) )
    cs.add_statements( RDF::Statement.new( subject, predicate, new_value ) )        
  end
  cs
end

Instance Method Details

#add_statements(stmts) ⇒ Object

Add these statements to the graph



54
55
56
57
58
59
60
61
# File 'lib/changesets/rdf_changeset.rb', line 54

def add_statements(stmts)
  stmts = [stmts] if stmts.is_a?(RDF::Statement)
  stmts.each do |stmt|
    next unless stmt
    raise ArgumentError unless stmt.subject == @subject_of_change        
    @statements.concat changeset_statement(stmt, :addition)
  end
end

#changeset_statement(stmt, action) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/changesets/rdf_changeset.rb', line 63

def changeset_statement(stmt, action)
  s = RDF::Node.new
  [RDF::Statement.new(@resource, RDF::Talis::Changeset.send(action), s),
    RDF::Statement.new(s, RDF.type, RDF.to_rdf+"Statement"),
    RDF::Statement.new(s, RDF.subject, stmt.subject),
    RDF::Statement.new(s, RDF.predicate, stmt.predicate),
    RDF::Statement.new(s, RDF.object, stmt.object)]
end

#remove_statements(stmts) ⇒ Object

Remove these statements from the graph



45
46
47
48
49
50
51
# File 'lib/changesets/rdf_changeset.rb', line 45

def remove_statements(stmts)
  stmts = [stmts] if stmts.is_a?(RDF::Statement)
  stmts.each do |stmt|
    raise ArgumentError unless stmt.subject == @subject_of_change        
    @statements.concat changeset_statement(stmt, :removal)
  end
end

#to_graphObject

Convert into an RDF::Graph object



73
74
75
76
77
78
79
# File 'lib/changesets/rdf_changeset.rb', line 73

def to_graph
  graph = RDF::Graph.new()
  @statements.each do |s|
    graph << s
  end
  graph
end