openlogic-rdf 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/AUTHORS +3 -0
  2. data/CREDITS +9 -0
  3. data/README +361 -0
  4. data/UNLICENSE +24 -0
  5. data/VERSION +1 -0
  6. data/bin/rdf +18 -0
  7. data/etc/doap.nt +62 -0
  8. data/lib/df.rb +1 -0
  9. data/lib/rdf/cli.rb +200 -0
  10. data/lib/rdf/format.rb +383 -0
  11. data/lib/rdf/mixin/countable.rb +39 -0
  12. data/lib/rdf/mixin/durable.rb +31 -0
  13. data/lib/rdf/mixin/enumerable.rb +637 -0
  14. data/lib/rdf/mixin/indexable.rb +26 -0
  15. data/lib/rdf/mixin/inferable.rb +5 -0
  16. data/lib/rdf/mixin/mutable.rb +191 -0
  17. data/lib/rdf/mixin/queryable.rb +265 -0
  18. data/lib/rdf/mixin/readable.rb +15 -0
  19. data/lib/rdf/mixin/type_check.rb +21 -0
  20. data/lib/rdf/mixin/writable.rb +152 -0
  21. data/lib/rdf/model/graph.rb +263 -0
  22. data/lib/rdf/model/list.rb +731 -0
  23. data/lib/rdf/model/literal/boolean.rb +121 -0
  24. data/lib/rdf/model/literal/date.rb +73 -0
  25. data/lib/rdf/model/literal/datetime.rb +72 -0
  26. data/lib/rdf/model/literal/decimal.rb +86 -0
  27. data/lib/rdf/model/literal/double.rb +189 -0
  28. data/lib/rdf/model/literal/integer.rb +126 -0
  29. data/lib/rdf/model/literal/numeric.rb +184 -0
  30. data/lib/rdf/model/literal/time.rb +87 -0
  31. data/lib/rdf/model/literal/token.rb +47 -0
  32. data/lib/rdf/model/literal/xml.rb +39 -0
  33. data/lib/rdf/model/literal.rb +373 -0
  34. data/lib/rdf/model/node.rb +156 -0
  35. data/lib/rdf/model/resource.rb +28 -0
  36. data/lib/rdf/model/statement.rb +296 -0
  37. data/lib/rdf/model/term.rb +77 -0
  38. data/lib/rdf/model/uri.rb +570 -0
  39. data/lib/rdf/model/value.rb +133 -0
  40. data/lib/rdf/nquads.rb +152 -0
  41. data/lib/rdf/ntriples/format.rb +48 -0
  42. data/lib/rdf/ntriples/reader.rb +239 -0
  43. data/lib/rdf/ntriples/writer.rb +219 -0
  44. data/lib/rdf/ntriples.rb +104 -0
  45. data/lib/rdf/query/pattern.rb +329 -0
  46. data/lib/rdf/query/solution.rb +252 -0
  47. data/lib/rdf/query/solutions.rb +237 -0
  48. data/lib/rdf/query/variable.rb +221 -0
  49. data/lib/rdf/query.rb +404 -0
  50. data/lib/rdf/reader.rb +511 -0
  51. data/lib/rdf/repository.rb +389 -0
  52. data/lib/rdf/transaction.rb +161 -0
  53. data/lib/rdf/util/aliasing.rb +63 -0
  54. data/lib/rdf/util/cache.rb +139 -0
  55. data/lib/rdf/util/file.rb +38 -0
  56. data/lib/rdf/util/uuid.rb +36 -0
  57. data/lib/rdf/util.rb +6 -0
  58. data/lib/rdf/version.rb +19 -0
  59. data/lib/rdf/vocab/cc.rb +18 -0
  60. data/lib/rdf/vocab/cert.rb +13 -0
  61. data/lib/rdf/vocab/dc.rb +63 -0
  62. data/lib/rdf/vocab/dc11.rb +23 -0
  63. data/lib/rdf/vocab/doap.rb +45 -0
  64. data/lib/rdf/vocab/exif.rb +168 -0
  65. data/lib/rdf/vocab/foaf.rb +69 -0
  66. data/lib/rdf/vocab/geo.rb +13 -0
  67. data/lib/rdf/vocab/http.rb +26 -0
  68. data/lib/rdf/vocab/owl.rb +59 -0
  69. data/lib/rdf/vocab/rdfs.rb +17 -0
  70. data/lib/rdf/vocab/rsa.rb +12 -0
  71. data/lib/rdf/vocab/rss.rb +14 -0
  72. data/lib/rdf/vocab/sioc.rb +93 -0
  73. data/lib/rdf/vocab/skos.rb +36 -0
  74. data/lib/rdf/vocab/wot.rb +21 -0
  75. data/lib/rdf/vocab/xhtml.rb +9 -0
  76. data/lib/rdf/vocab/xsd.rb +58 -0
  77. data/lib/rdf/vocab.rb +215 -0
  78. data/lib/rdf/writer.rb +475 -0
  79. data/lib/rdf.rb +192 -0
  80. metadata +173 -0
@@ -0,0 +1,184 @@
1
+ module RDF; class Literal
2
+ ##
3
+ # Shared methods and class ancestry for numeric literal classes.
4
+ #
5
+ # @since 0.3.0
6
+ class Numeric < Literal
7
+ ##
8
+ # Compares this literal to `other` for sorting purposes.
9
+ #
10
+ # @param [Object] other
11
+ # @return [Integer] `-1`, `0`, or `1`
12
+ # @since 0.3.0
13
+ def <=>(other)
14
+ case other
15
+ when ::Numeric
16
+ to_d <=> other
17
+ when Numeric
18
+ to_d <=> other.to_d
19
+ else super
20
+ end
21
+ end
22
+
23
+ ##
24
+ # Returns `true` if this literal is equal to `other`.
25
+ #
26
+ # @param [Object] other
27
+ # @return [Boolean] `true` or `false`
28
+ # @since 0.3.0
29
+ def ==(other)
30
+ # If lexically invalid, use regular literal testing
31
+ return super unless self.valid?
32
+
33
+ case other
34
+ when Literal::Numeric
35
+ return super unless other.valid?
36
+ (cmp = (self <=> other)) ? cmp.zero? : false
37
+ when RDF::URI, RDF::Node
38
+ # Interpreting SPARQL data-r2/expr-equal/eq-2-2, numeric can't be compared with other types
39
+ type_error("unable to determine whether #{self.inspect} and #{other.inspect} are equivalent")
40
+ else
41
+ super
42
+ end
43
+ end
44
+
45
+ ##
46
+ # Returns `self`.
47
+ #
48
+ # @return [RDF::Literal::Numeric]
49
+ # @since 0.2.3
50
+ def +@
51
+ self # unary plus
52
+ end
53
+
54
+ ##
55
+ # Returns `self` negated.
56
+ #
57
+ # @return [RDF::Literal::Numeric]
58
+ # @since 0.2.3
59
+ def -@
60
+ self.class.new(-self.object)
61
+ end
62
+
63
+ ##
64
+ # Returns the sum of `self` plus `other`.
65
+ #
66
+ # For xs:float or xs:double values, if one of the operands is a zero or a finite number
67
+ # and the other is INF or -INF, INF or -INF is returned. If both operands are INF, INF is returned.
68
+ # If both operands are -INF, -INF is returned. If one of the operands is INF
69
+ # and the other is -INF, NaN is returned.
70
+ # @param [Literal::Numeric, #to_i, #to_f, #to_d] other
71
+ # @return [RDF::Literal::Numeric]
72
+ # @since 0.2.3
73
+ # @see http://www.w3.org/TR/xpath-functions/#func-numeric-add
74
+ def +(other)
75
+ if self.class == Double || other.class == Double
76
+ RDF::Literal::Double.new(to_f + other.to_f)
77
+ elsif self.class == Float || other.class == Float
78
+ RDF::Literal::Float.new(to_f + other.to_f)
79
+ elsif self.class == Decimal || other.class == Decimal
80
+ RDF::Literal::Decimal.new(to_d + (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
81
+ else
82
+ RDF::Literal::Integer.new(to_i + other.to_i)
83
+ end
84
+ end
85
+
86
+ ##
87
+ # Returns the difference of `self` minus `other`.
88
+ #
89
+ # @param [Literal::Numeric, #to_i, #to_f, #to_d] other
90
+ # @return [RDF::Literal::Numeric]
91
+ # @since 0.2.3
92
+ # @see http://www.w3.org/TR/xpath-functions/#func-numeric-subtract
93
+ def -(other)
94
+ if self.class == Double || other.class == Double
95
+ RDF::Literal::Double.new(to_f - other.to_f)
96
+ elsif self.class == Float || other.class == Float
97
+ RDF::Literal::Float.new(to_f - other.to_f)
98
+ elsif self.class == Decimal || other.class == Decimal
99
+ RDF::Literal::Decimal.new(to_d - (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
100
+ else
101
+ RDF::Literal::Integer.new(to_i - other.to_i)
102
+ end
103
+ end
104
+
105
+ ##
106
+ # Returns the product of `self` times `other`.
107
+ #
108
+ # @param [Literal::Numeric, #to_i, #to_f, #to_d] other
109
+ # @return [RDF::Literal::Numeric]
110
+ # @since 0.2.3
111
+ # @see http://www.w3.org/TR/xpath-functions/#func-numeric-multiply
112
+ def *(other)
113
+ if self.class == Double || other.class == Double
114
+ RDF::Literal::Double.new(to_f * other.to_f)
115
+ elsif self.class == Float || other.class == Float
116
+ RDF::Literal::Float.new(to_f * other.to_f)
117
+ elsif self.class == Decimal || other.class == Decimal
118
+ RDF::Literal::Decimal.new(to_d * (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
119
+ else
120
+ RDF::Literal::Integer.new(to_i * other.to_i)
121
+ end
122
+ end
123
+
124
+ ##
125
+ # Returns the quotient of `self` divided by `other`.
126
+ #
127
+ # As a special case, if the types of both $arg1 and $arg2 are xs:integer,
128
+ # then the return type is xs:decimal.
129
+ #
130
+ # @param [Literal::Numeric, #to_i, #to_f, #to_d] other
131
+ # @return [RDF::Literal::Numeric]
132
+ # @raise [ZeroDivisionError] if divided by zero
133
+ # @since 0.2.3
134
+ # @see http://www.w3.org/TR/xpath-functions/#func-numeric-divide
135
+ def /(other)
136
+ if self.class == Double || other.class == Double
137
+ RDF::Literal::Double.new(to_f / other.to_f)
138
+ elsif self.class == Float || other.class == Float
139
+ RDF::Literal::Float.new(to_f / other.to_f)
140
+ elsif self.class == Decimal || other.class == Decimal
141
+ RDF::Literal::Decimal.new(to_d / (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
142
+ else
143
+ RDF::Literal::Integer.new(to_i / other.to_i)
144
+ end
145
+ end
146
+
147
+ ##
148
+ # Returns the value as an integer.
149
+ #
150
+ # @return [Integer]
151
+ def to_i
152
+ @object.to_i
153
+ end
154
+ alias_method :to_int, :to_i
155
+ alias_method :ord, :to_i
156
+
157
+ ##
158
+ # Returns the value as a floating point number.
159
+ #
160
+ # The usual accuracy limits and errors of binary float arithmetic apply.
161
+ #
162
+ # @return [Float]
163
+ # @see BigDecimal#to_f
164
+ def to_f
165
+ @object.to_f
166
+ end
167
+
168
+ ##
169
+ # Returns the value as a decimal number.
170
+ #
171
+ # @return [BigDecimal]
172
+ def to_d
173
+ @object.respond_to?(:to_d) ? @object.to_d : BigDecimal(@object.to_s)
174
+ end
175
+
176
+ ##
177
+ # Returns the value as a rational number.
178
+ #
179
+ # @return [Rational]
180
+ def to_r
181
+ @object.to_r
182
+ end
183
+ end # Numeric
184
+ end; end # RDF::Literal
@@ -0,0 +1,87 @@
1
+ # coding: utf-8
2
+ module RDF; class Literal
3
+ ##
4
+ # A time literal.
5
+ #
6
+ # The lexical representation for time is the left truncated lexical
7
+ # representation for `xsd:dateTime`: "hh:mm:ss.sss" with an optional
8
+ # following time zone indicator.
9
+ #
10
+ # @see http://www.w3.org/TR/xmlschema-2/#time
11
+ # @since 0.2.1
12
+ class Time < Literal
13
+ DATATYPE = XSD.time
14
+ GRAMMAR = %r(\A\d{2}:\d{2}:\d{2}(\.\d+)?(([\+\-]\d{2}:\d{2})|UTC|Z)?\Z).freeze
15
+
16
+ ##
17
+ # @param [Time] value
18
+ # @option options [String] :lexical (nil)
19
+ def initialize(value, options = {})
20
+ @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
21
+ @string = options[:lexical] if options.has_key?(:lexical)
22
+ @string ||= value if value.is_a?(String)
23
+ @object = case
24
+ when value.is_a?(::Time) then value
25
+ when value.respond_to?(:to_time) then value.to_time # Ruby 1.9+
26
+ else ::Time.parse(value.to_s)
27
+ end rescue nil
28
+ end
29
+
30
+ ##
31
+ # Converts this literal into its canonical lexical representation.
32
+ #
33
+ # §3.2.8.2 Canonical representation
34
+ #
35
+ # The canonical representation for time is defined by prohibiting
36
+ # certain options from the Lexical representation (§3.2.8.1).
37
+ # Specifically, either the time zone must be omitted or, if present, the
38
+ # time zone must be Coordinated Universal Time (UTC) indicated by a "Z".
39
+ # Additionally, the canonical representation for midnight is 00:00:00.
40
+ #
41
+ # @return [RDF::Literal] `self`
42
+ # @see http://www.w3.org/TR/xmlschema-2/#time
43
+ def canonicalize!
44
+ @string = @object.utc.strftime('%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z') if self.valid?
45
+ self
46
+ end
47
+
48
+ ##
49
+ # Returns `true` if the value adheres to the defined grammar of the
50
+ # datatype.
51
+ #
52
+ # Special case for date and dateTime, for which '0000' is not a valid year
53
+ #
54
+ # @return [Boolean]
55
+ # @since 0.2.1
56
+ def valid?
57
+ super && object
58
+ end
59
+
60
+ ##
61
+ # Returns the value as a string.
62
+ #
63
+ # @return [String]
64
+ def to_s
65
+ @string || @object.strftime('%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
66
+ end
67
+
68
+ ##
69
+ # Equal compares as Time objects
70
+ def ==(other)
71
+ # If lexically invalid, use regular literal testing
72
+ return super unless self.valid?
73
+
74
+ case other
75
+ when Literal::Time
76
+ return super unless other.valid?
77
+ # Compare as strings, as time includes a date portion, and adjusting for UTC
78
+ # can create a mismatch in the date portion.
79
+ self.object.utc.strftime('%H%M%S') == other.object.utc.strftime('%H%M%S')
80
+ when Literal::DateTime, Literal::Date
81
+ false
82
+ else
83
+ super
84
+ end
85
+ end
86
+ end # Time
87
+ end; end # RDF::Literal
@@ -0,0 +1,47 @@
1
+ module RDF; class Literal
2
+ ##
3
+ # A token literal.
4
+ #
5
+ # @see http://www.w3.org/TR/xmlschema-2/#token
6
+ # @since 0.2.3
7
+ class Token < Literal
8
+ DATATYPE = XSD.token
9
+ GRAMMAR = /\A[^\x0D\x0A\x09]+\z/i.freeze # FIXME
10
+
11
+ ##
12
+ # @param [Symbol, #to_s] value
13
+ # @option options [String] :lexical (nil)
14
+ def initialize(value, options = {})
15
+ @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
16
+ @string = options[:lexical] if options.has_key?(:lexical)
17
+ @string ||= value if value.is_a?(String)
18
+ @object = value.is_a?(Symbol) ? value : value.to_s
19
+ end
20
+
21
+ ##
22
+ # Converts this literal into its canonical lexical representation.
23
+ #
24
+ # @return [RDF::Literal] `self`
25
+ # @see http://www.w3.org/TR/xmlschema-2/#boolean
26
+ def canonicalize!
27
+ @string = @object.to_s if @object
28
+ self
29
+ end
30
+
31
+ ##
32
+ # Returns the value as a symbol.
33
+ #
34
+ # @return [Symbol]
35
+ def to_sym
36
+ @object.to_sym
37
+ end
38
+
39
+ ##
40
+ # Returns the value as a string.
41
+ #
42
+ # @return [String]
43
+ def to_s
44
+ @string || @object.to_s
45
+ end
46
+ end # Token
47
+ end; end # RDF::Literal
@@ -0,0 +1,39 @@
1
+ module RDF; class Literal
2
+ ##
3
+ # An XML literal.
4
+ #
5
+ # @see http://www.w3.org/TR/rdf-concepts/#section-XMLLiteral
6
+ # @see http://www.w3.org/TR/rdfa-core/#s_xml_literals
7
+ # @since 0.2.1
8
+ class XML < Literal
9
+ DATATYPE = RDF.XMLLiteral
10
+ GRAMMAR = nil
11
+
12
+ ##
13
+ # @param [Object] value
14
+ # @option options [String] :lexical (nil)
15
+ def initialize(value, options = {})
16
+ @datatype = options[:datatype] || self.class.const_get(:DATATYPE)
17
+ @string = options[:lexical] if options.has_key?(:lexical)
18
+ @object = value # TODO: parse XML string using REXML
19
+ end
20
+
21
+ ##
22
+ # Converts this literal into its canonical lexical representation.
23
+ #
24
+ # @return [RDF::Literal] `self`
25
+ # @see http://www.w3.org/TR/xml-exc-c14n/
26
+ def canonicalize!
27
+ # TODO: implement XML canonicalization
28
+ self
29
+ end
30
+
31
+ ##
32
+ # Returns the value as a string.
33
+ #
34
+ # @return [String]
35
+ def to_s
36
+ @string || @object.to_s # TODO
37
+ end
38
+ end # XML
39
+ end; end # RDF::Literal