Class: RDF::Raptor::FFI::V1::Statement

Inherits:
FFI::Struct
  • Object
show all
Includes:
RDF::Raptor::FFI
Defined in:
lib/rdf/raptor/ffi/v1/statement.rb

Overview

Constant Summary

Constants included from RDF::Raptor::FFI

ENGINE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RDF::Raptor::FFI

#version

Constructor Details

#initialize(ptr = nil, factory = nil) ⇒ Statement

Returns a new instance of Statement.

Parameters:

  • ptr (FFI::Pointer) (defaults to: nil)
  • factory (#create_node) (defaults to: nil)


18
19
20
21
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 18

def initialize(ptr = nil, factory = nil)
  super(ptr)
  @factory = factory if factory
end

Instance Attribute Details

#graph_nameRDF::Resource

Returns:

  • (RDF::Resource)


36
37
38
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 36

def graph_name
  @graph_name
end

#idObject

Returns:

  • (Object)


33
34
35
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 33

def id
  @id
end

Class Method Details

.release(ptr)

This method returns an undefined value.

Releases libraptor memory associated with this structure.

Parameters:

  • ptr (FFI::Pointer)


28
29
30
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 28

def self.release(ptr)
  raptor_free_memory(ptr) unless ptr.null?
end

Instance Method Details

#free Also known as: release

This method returns an undefined value.

Releases libraptor memory associated with this structure.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 197

def free
  if self[:subject_type].nonzero? && !(self[:subject].null?)
    self[:subject] = case self[:subject_type]
      when RAPTOR_IDENTIFIER_TYPE_ANONYMOUS
        V1.raptor_free_string(self[:subject])
      when RAPTOR_IDENTIFIER_TYPE_RESOURCE
        V1.raptor_free_uri(self[:subject])
    end
    self[:subject_type] = RAPTOR_IDENTIFIER_TYPE_UNKNOWN
  end

  if self[:predicate_type].nonzero? && !(self[:predicate].null?)
    self[:predicate] = V1.raptor_free_uri(self[:predicate])
    self[:predicate_type] = RAPTOR_IDENTIFIER_TYPE_UNKNOWN
  end

  if self[:object_type].nonzero? && !(self[:object].null?)
    self[:object] = case self[:object_type]
      when RAPTOR_IDENTIFIER_TYPE_ANONYMOUS
        V1.raptor_free_string(self[:object])
      when RAPTOR_IDENTIFIER_TYPE_RESOURCE
        V1.raptor_free_uri(self[:object])
      when RAPTOR_IDENTIFIER_TYPE_LITERAL
        V1.raptor_free_string(self[:object])
        unless self[:object_literal_datatype].null?
          self[:object_literal_datatype] = V1.raptor_free_uri(self[:object_literal_datatype])
        end
        unless self[:object_literal_language].null?
          self[:object_literal_language] = V1.raptor_free_string(self[:object_literal_language])
        end
    end
    self[:object_type] = RAPTOR_IDENTIFIER_TYPE_UNKNOWN
  end
end

#objectRDF::Term

Returns:

  • (RDF::Term)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 111

def object
  @object ||= case self[:object_type]
    when RAPTOR_IDENTIFIER_TYPE_ANONYMOUS
      @factory.create_node(self[:object].read_string)
    when RAPTOR_IDENTIFIER_TYPE_RESOURCE
      @factory.create_uri(V1.raptor_uri_as_string(self[:object]))
    when RAPTOR_IDENTIFIER_TYPE_LITERAL
      str = self[:object].read_string.unpack('U*').pack('U*')
      case
        when !self[:object_literal_language].null?
          RDF::Literal.new(str, language: self[:object_literal_language].read_string)
        when !self[:object_literal_datatype].null?
          RDF::Literal.new(str, datatype: V1.raptor_uri_as_string(self[:object_literal_datatype]))
        else
          RDF::Literal.new(str)
      end
  end
end

#object=(value)

This method returns an undefined value.

Sets the object term from an RDF::Term.

The value must be one of RDF::Resource or RDF::Literal.

Parameters:

  • value (RDF::Term)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 137

def object=(value)
  @object = nil
  case value
    when RDF::Node
      self[:object_type] = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS
      self[:object] = V1.raptor_new_string(value.id.to_s)
    when RDF::URI
      self[:object_type] = RAPTOR_IDENTIFIER_TYPE_RESOURCE
      self[:object] = V1.raptor_new_uri(value.to_s)
    when RDF::Literal
      self[:object_type] = RAPTOR_IDENTIFIER_TYPE_LITERAL
      self[:object] = V1.raptor_new_string(value.value)
      self[:object_literal_datatype] = V1.raptor_new_uri(value.datatype.to_s) if value.datatype
      self[:object_literal_language] = V1.raptor_new_string(value.language.to_s) if value.language?
    else
      raise ArgumentError, "object term must be an RDF::Node, RDF::URI, or RDF::Literal"
  end
  @object = value
end

#object_as_stringString

Returns:

  • (String)


159
160
161
162
163
164
165
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 159

def object_as_string
  V1.raptor_statement_part_as_string(
    self[:object],
    self[:object_type],
    self[:object_literal_datatype],
    self[:object_literal_language])
end

#predicateRDF::URI

Returns:

  • (RDF::URI)


80
81
82
83
84
85
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 80

def predicate
  @predicate ||= case self[:predicate_type]
    when RAPTOR_IDENTIFIER_TYPE_RESOURCE
      RDF::URI.intern(V1.raptor_uri_as_string(self[:predicate]))
  end
end

#predicate=(uri)

This method returns an undefined value.

Sets the predicate term from an RDF::URI.

Parameters:

  • uri (RDF::URI)

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 92

def predicate=(uri)
  @predicate = nil
  raise ArgumentError, "predicate term must be an RDF::URI" unless uri.is_a?(RDF::URI)
  self[:predicate_type] = RAPTOR_IDENTIFIER_TYPE_RESOURCE
  self[:predicate] = V1.raptor_new_uri(uri.to_s)
  @predicate = uri
end

#predicate_as_stringString

Returns:

  • (String)


102
103
104
105
106
107
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 102

def predicate_as_string
  V1.raptor_statement_part_as_string(
    self[:predicate],
    self[:predicate_type],
    nil, nil)
end

#reset!

This method returns an undefined value.



189
190
191
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 189

def reset!
  @subject = @predicate = @object = @graph_name = nil
end

#subjectRDF::Resource

Returns:

  • (RDF::Resource)


40
41
42
43
44
45
46
47
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 40

def subject
  @subject ||= case self[:subject_type]
    when RAPTOR_IDENTIFIER_TYPE_ANONYMOUS
      @factory.create_node(self[:subject].read_string)
    when RAPTOR_IDENTIFIER_TYPE_RESOURCE
      @factory.create_uri(V1.raptor_uri_as_string(self[:subject]))
  end
end

#subject=(resource)

This method returns an undefined value.

Sets the subject term from an RDF::Resource.

Parameters:

  • resource (RDF::Resource)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 54

def subject=(resource)
  @subject = nil
  case resource
    when RDF::Node
      self[:subject_type] = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS
      self[:subject] = V1.raptor_new_string(resource.id.to_s)
    when RDF::URI
      self[:subject_type] = RAPTOR_IDENTIFIER_TYPE_RESOURCE
      self[:subject] = V1.raptor_new_uri(resource.to_s)
    else
      raise ArgumentError, "subject term must be an RDF::Node or RDF::URI"
  end
  @subject = resource
end

#subject_as_stringString

Returns:

  • (String)


71
72
73
74
75
76
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 71

def subject_as_string
  V1.raptor_statement_part_as_string(
    self[:subject],
    self[:subject_type],
    nil, nil)
end

#to_quadArray(RDF::Resource, RDF::URI, RDF::Term, nil)

Returns:

  • (Array(RDF::Resource, RDF::URI, RDF::Term, nil))

See Also:

  • Statement#to_quad


177
178
179
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 177

def to_quad
  [subject, predicate, object, graph_name]
end

#to_rdfRDF::Statement

Returns:

  • (RDF::Statement)


183
184
185
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 183

def to_rdf
  RDF::Statement.new(subject, predicate, object, graph_name: graph_name)
end

#to_tripleArray(RDF::Resource, RDF::URI, RDF::Term)

Returns:

  • (Array(RDF::Resource, RDF::URI, RDF::Term))

See Also:

  • Statement#to_triple


170
171
172
# File 'lib/rdf/raptor/ffi/v1/statement.rb', line 170

def to_triple
  [subject, predicate, object]
end