Debian Codegraph @

ruby/lib/rdf/redland/statement.rb

raw symbols SCIP
1 require 'rdf/redland'
2
3 module Redland
4
5 # Redland Statement (triple class). The main means of manipulating
6 # statements is by the subject,predicate,and object properties
7 #
8 # require 'rdf'
9 # Statement creation from nodes
10 # statement1 = Statement.new(node1,node2,node3)
11 # statement2 = Statement.new(:statement=>statement)
12 #
13 # if statement2.resource?
14 # puts "statement2.subject has uri #{statement2.subject.uri}"
15 #
16 class Statement
17 include Util
18
19 # the internal librdf statement
20 attr_reader :statement
21
22 # create a new Statement with subject,predicate,and object
23 # the subject, predicate, and objects can be of class String or
24 # Uri.
25 def initialize(subject=nil,predicate=nil,object=nil,model=nil)
26 @model = model
27
28 if subject.class == Hash
29 if subject.key?(:from_object)
30 @statement = Redland.librdf_new_statement_from_statement(subject[:from_object])
31 @model = subject[:model]
32 end
33 else
34
35 unless subject
36 s = nil
37 else
38 if (subject.class == Uri)|| (subject.class == String)
39 subject = Node.new(subject)
40 end
41 s = Redland.librdf_new_node_from_node(subject.node)
42 end
43
44 unless predicate
45 p = nil
46 else
47 if (predicate.class == Uri) || (predicate.class == String)
48 predicate = Node.new(predicate)
49 end
50 p = Redland.librdf_new_node_from_node(predicate.node)
51 end
52
53 unless object
54 o = nil
55 else
56 if (object.class == Uri) || (object.class == String)
57 object = Node.new(object)
58 end
59 o = Redland.librdf_new_node_from_node(object.node)
60 end
61 @statement = Redland.librdf_new_statement_from_nodes($world.world,s,p,o)
62 end
63
64 raise RedlandError.new("Statement construction failed") if !@statement
65 ObjectSpace.define_finalizer(self,Statement.create_finalizer(@statement))
66
67 end
68
69 # You shouldn't use this. Used internally for cleanup.
70 def Statement.create_finalizer(statement)
71 proc {|id| Redland::librdf_free_statement(statement) }
72 end
73
74 def wrap_node(rednode)
75 return Node.new(:from_object=>rednode)
76 end
77
78 private :wrap_node
79
80 # return the subject of the statement triple. The subject is of class
81 # Resource
82 def subject()
83 return Resource.new(Redland.librdf_statement_get_subject(self.statement),@model)
84 end
85
86 # return the predicate of the statement triple.
87 def predicate()
88 return wrap_node(Redland.librdf_statement_get_predicate(self.statement))
89 end
90
91 # return the object of the statement triple. The return value is either
92 # a Literal or Resource depending if object returned from the store
93 # is literal or not
94 def object()
95 object_node = Redland.librdf_statement_get_object(self.statement)
96 return Literal.from_node(object_node) if is_literal?(object_node)
97 return Resource.new(object_node,@model)
98 end
99
100 # set the subject of the statement
101 def subject=(value)
102 if value
103 Redland.librdf_statement_set_subject(self.statement,
104 Redland.librdf_new_node_from_node(value.node))
105 else
106 Redland.librdf_statement_set_subject(self.statement, nil)
107 end
108 end
109
110 # set the object of the statement
111 def object=(value)
112 if value
113 Redland.librdf_statement_set_object(self.statement,
114 Redland.librdf_new_node_from_node(value.node))
115 else
116 Redland.librdf_statement_set_object(self.statement, nil)
117 end
118 end
119
120 # set the predicate of the statement
121 def predicate=(value)
122 if value
123 Redland.librdf_statement_set_predicate(self.statement,
124 Redland.librdf_new_node_from_node(value.node))
125 else
126 Redland.librdf_statement_set_predicate(self.statement, nil)
127 end
128 end
129
130 # Convert this Statement to a string
131 def to_s()
132 unless self.statement
133 raise RedlandError.new("Statement is invalid")
134 else
135 return Redland.librdf_statement_to_string(self.statement)
136 end
137 end
138
139 # Equivalency. Only works for comparing two Statements
140 def ==(other)
141 return (Redland.librdf_statement_equals(self.statement,other.statement) != 0)
142 end
143
144 end
145
146 end #module Redland