rdf_context 0.4.6 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ === 0.4.7
2
+ * Graph identifiers only URIRef or BNode; Literal not supported by SQL Store.
3
+ * Namespace#+ updated to not use URIRef logic for creating URIs, as this removes part of the URI in the case that the URI does not end with / or #.
4
+ * Incorporated pdlug's changes:
5
+ * Support using trailing underscores to work around generating URI's with Namespace that use methods that are on core ruby classes or reserved words (like Nokogiri Builder does). For example dc.type_ to generate the dc:type URI.
6
+ * Tests for Date/DateTime literals, added support for detecting them and generating the right literals in Triple.
7
+
1
8
  === 0.4.6
2
9
  * Added Graph#uri_mapping and AbstractStore#uri_mapping for uri => Namespace mappings in graph
3
10
  * Fix BNode creation from existing identifier (named or un-named)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.6
1
+ 0.4.7
@@ -29,7 +29,7 @@ module RdfContext
29
29
  #
30
30
  # @param [Hash] options:: Options
31
31
  # <em>options[:store]</em>:: storage, defaults to a new ListStore instance. May be symbol :list_store or :memory_store
32
- # <em>options[:identifier]</em>:: Identifier for this graph, Literal, BNode or URIRef
32
+ # <em>options[:identifier]</em>:: Identifier for this graph, BNode or URIRef
33
33
  def initialize(options = {})
34
34
  @nsbinding = {}
35
35
 
@@ -1,3 +1,9 @@
1
+ begin
2
+ require 'xml'
3
+ $libxml_enabled = true
4
+ rescue LoadError
5
+ end
6
+
1
7
  module RdfContext
2
8
  # An RDF Literal, with value, encoding and language elements.
3
9
  class Literal
@@ -23,7 +29,17 @@ module RdfContext
23
29
  def self.string
24
30
  @string ||= coerce "http://www.w3.org/2001/XMLSchema#string"
25
31
  end
26
-
32
+
33
+ # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#date")</tt>
34
+ def self.date
35
+ @date ||= coerce "http://www.w3.org/2001/XMLSchema#date"
36
+ end
37
+
38
+ # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#dateTime")</tt>
39
+ def self.datetime
40
+ @datetime ||= coerce "http://www.w3.org/2001/XMLSchema#dateTime"
41
+ end
42
+
27
43
  # Create from URI, empty or nil string
28
44
  def self.coerce(string_or_nil)
29
45
  if string_or_nil.nil? || string_or_nil == ''
@@ -179,7 +195,9 @@ module RdfContext
179
195
 
180
196
  # Add already mapped namespaces and language
181
197
  @contents = contents.map do |c|
182
- c = Nokogiri::XML.parse(c.copy(true).to_s) if c.is_a?(LibXML::XML::Node)
198
+ if $libxml_enabled
199
+ c = Nokogiri::XML.parse(c.copy(true).to_s) if c.is_a?(LibXML::XML::Node)
200
+ end
183
201
  if c.is_a?(Nokogiri::XML::Element)
184
202
  # Gather namespaces from self and decendant nodes
185
203
  c.traverse do |n|
@@ -36,6 +36,8 @@ module RdfContext
36
36
  # foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf"); foaf.knows # => returns a new URIRef with URI "http://xmlns.com/foaf/0.1/knows"
37
37
  # foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf", true); foaf.knows # => returns a new URIRef with URI "http://xmlns.com/foaf/0.1/#knows"
38
38
  #
39
+ # To avoid naming problems, a suffix may have an appended '_', which will be removed when the URI is generated.
40
+ #
39
41
  # @return [URIRef]:: The newly created URIRegerence.
40
42
  # @raise [Error]:: Checks validity of the desired prefix and raises if it is incorrect.
41
43
  # @author Tom Morris, Pius Uzamere
@@ -45,7 +47,10 @@ module RdfContext
45
47
 
46
48
  # Construct a URIRef from a namespace as in method_missing, but without method collision issues
47
49
  def +(suffix)
48
- URIRef.new((fragment ? "##{suffix}" : suffix.to_s), @uri)
50
+ prefix = @uri.to_s
51
+ prefix += '#' if fragment && !prefix.match(/\#$/)
52
+ suffix = suffix.to_s.sub(/_$/, '')
53
+ URIRef.new(prefix + suffix)
49
54
  end
50
55
 
51
56
  # Bind this namespace to a Graph
@@ -1,4 +1,3 @@
1
- require 'xml'
2
1
  require File.join(File.dirname(__FILE__), 'parser')
3
2
 
4
3
  module RdfContext
@@ -74,7 +74,7 @@ module RdfContext
74
74
  oi = resource_to_int(triple.object) || gen_key(triple.object)
75
75
  ci = resource_to_int(context) || gen_key(context)
76
76
 
77
- puts "add: #{si}, #{pi}, #{oi}, #{ci}" if $DEBUG
77
+ puts "add: #{si}=#{triple.subject}, #{pi}=#{triple.predicate}, #{oi}=#{triple.object}, #{ci}=#{context ? context.identifier : 'none'}" if $DEBUG
78
78
  set_nested_index(@cspo, ci, si, pi, oi)
79
79
  set_nested_index(@cpos, ci, pi, oi, si)
80
80
  set_nested_index(@cosp, ci, oi, si, pi)
@@ -132,7 +132,7 @@ module RdfContext
132
132
 
133
133
  results = []
134
134
  si, pi, oi = triple_to_int(triple)
135
- puts "triples: si=#{si}, pi=#{pi}, oi=#{oi}, ci=#{ci}" if $DEBUG
135
+ puts "triples? #{si}=#{triple.subject}, #{pi}=#{triple.predicate}, #{oi}=#{triple.object}, #{ci}=#{context ? context.identifier : 'none'}" if $DEBUG
136
136
 
137
137
  def result(v, si, pi, oi, ctx)
138
138
  triple = int_to_triple(si, pi, oi)
@@ -141,12 +141,15 @@ module RdfContext
141
141
  # keys are contexts
142
142
  v.keys.each do |ci|
143
143
  context = int_to_resource(ci)
144
+ puts "triples= #{si}=#{triple.subject}, #{pi}=#{triple.predicate}, #{oi}=#{triple.object}, #{ci}=#{context ? context.identifier : 'none'}" if $DEBUG
144
145
  yield triple, context
145
146
  end
146
147
  else
147
- #puts "ctx: #{ctx}"
148
+ puts "triples= #{si}=#{triple.subject}, #{pi}=#{triple.predicate}, #{oi}=#{triple.object}, #{ctx ? ctx.identifier : 'none'}" if $DEBUG
148
149
  yield triple, ctx
149
150
  end
151
+ else
152
+ puts "triples= #{si}=#{triple.subject}, #{pi}=#{triple.predicate}, #{oi}=#{triple.object}, #{ctx ? ctx.identifier : 'none'}" if $DEBUG
150
153
  end
151
154
  triple
152
155
  end
@@ -214,13 +217,13 @@ module RdfContext
214
217
  elsif !triple.object.nil?
215
218
  # Subject+predicate unspecified, object specified but not found, skip
216
219
  else # subject+predicate+object unbound
217
- puts "spo = #{spo.inspect}" if $DEBUG
220
+ #puts "spo = #{spo.inspect}" if $DEBUG
218
221
  spo.keys.each do |si|
219
- puts "spo[#{si}] = #{spo[si].inspect}" if $DEBUG
222
+ #puts "spo[#{si}] = #{spo[si].inspect}" if $DEBUG
220
223
  spo[si].keys.each do |pi|
221
- puts "spo[#{si}][#{pi}] = #{spo[si][pi].inspect}" if $DEBUG
224
+ #puts "spo[#{si}][#{pi}] = #{spo[si][pi].inspect}" if $DEBUG
222
225
  spo[si][pi].each_pair do |oi, value|
223
- puts "spo[#{si}][#{pi}][#{oi}] = #{spo[si][pi][oi].inspect}" if $DEBUG
226
+ #puts "spo[#{si}][#{pi}][#{oi}] = #{spo[si][pi][oi].inspect}" if $DEBUG
224
227
  results << result(value, si, pi, oi, context, &block)
225
228
  end
226
229
  end
@@ -102,8 +102,8 @@ module RdfContext
102
102
 
103
103
  # Coerce a predicate to the appropriate RdfContext type.
104
104
  #
105
- # @param[URI, URIRef, String] subject:: If a String looks like a URI, a URI is created
106
- # @raise[InvalidSubject]:: If subject can't be predicate.
105
+ # @param[URI, URIRef, String] predicate:: If a String looks like a URI, a URI is created
106
+ # @raise[InvalidPredicate]:: If predicate can't be predicate.
107
107
  def self.coerce_predicate(predicate)
108
108
  case predicate
109
109
  when Addressable::URI
@@ -124,7 +124,7 @@ module RdfContext
124
124
  # Coerce a object to the appropriate RdfContext type.
125
125
  #
126
126
  # @param[URI, URIRef, String, Integer, Float, BNode, Literal] object:: If a String looks like a URI, a URI is created, otherwise an untyped Literal.
127
- # @raise[InvalidSubject]:: If subject can't be predicate.
127
+ # @raise[InvalidObject]:: If object can't be predicate.
128
128
  def self.coerce_object(object)
129
129
  case object
130
130
  when Addressable::URI
@@ -135,11 +135,11 @@ module RdfContext
135
135
  else
136
136
  Literal.untyped(object)
137
137
  end
138
- when Integer, Float
138
+ when Integer, Float, Date
139
139
  Literal.build_from(object)
140
140
  when URIRef, BNode, Literal
141
141
  object
142
- when nil, regexp
142
+ when nil
143
143
  object
144
144
  else
145
145
  raise InvalidObject, "#{object.class}: #{object.inspect} is not a valid object"
data/spec/literal_spec.rb CHANGED
@@ -132,7 +132,37 @@ describe "Literals: " do
132
132
  float.encoding.should == "http://www.w3.org/2001/XMLSchema#float"
133
133
  end
134
134
  end
135
+
136
+ describe "a date" do
137
+ describe "encodings" do
138
+ subject { Literal.typed("2010-01-02", "http://www.w3.org/2001/XMLSchema#date") }
139
+ it "should return n3" do subject.to_n3.should == "\"2010-01-02\"^^<http://www.w3.org/2001/XMLSchema#date>" end
140
+ it "should return ntriples" do subject.to_ntriples.should == subject.to_n3 end
141
+ it "should return xml_args" do subject.xml_args.should == ["2010-01-02", {"rdf:datatype" => "http://www.w3.org/2001/XMLSchema#date"}] end
142
+ it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#date\">2010-01-02</typedLiteral>" end
143
+ end
144
+
145
+ it "should infer type" do
146
+ int = Literal.build_from(Date::civil(2010, 1, 2))
147
+ int.encoding.should == "http://www.w3.org/2001/XMLSchema#date"
148
+ end
149
+ end
150
+
151
+ describe "a date time" do
152
+ describe "encodings" do
153
+ subject { Literal.typed("2010-01-03T01:02:03", "http://www.w3.org/2001/XMLSchema#dateTime") }
154
+ it "should return n3" do subject.to_n3.should == "\"2010-01-03T01:02:03\"^^<http://www.w3.org/2001/XMLSchema#dateTime>" end
155
+ it "should return ntriples" do subject.to_ntriples.should == subject.to_n3 end
156
+ it "should return xml_args" do subject.xml_args.should == ["2010-01-03T01:02:03", {"rdf:datatype" => "http://www.w3.org/2001/XMLSchema#dateTime"}] end
157
+ it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2010-01-03T01:02:03</typedLiteral>" end
158
+ end
135
159
 
160
+ it "should infer type" do
161
+ int = Literal.build_from(DateTime.parse('2010-01-03T01:02:03'))
162
+ int.encoding.should == "http://www.w3.org/2001/XMLSchema#dateTime"
163
+ end
164
+ end
165
+
136
166
  describe "XML Literal" do
137
167
  describe "with no namespace" do
138
168
  subject { Literal.typed("foo <sup>bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral") }
@@ -300,6 +330,12 @@ describe "Literals: " do
300
330
  specify "string" do
301
331
  Literal::Encoding.string.should == Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#string")
302
332
  end
333
+ specify "date" do
334
+ Literal::Encoding.date.should == Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#date")
335
+ end
336
+ specify "date time" do
337
+ Literal::Encoding.datetime.should == Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#dateTime")
338
+ end
303
339
  specify "xmlliteral" do
304
340
  Literal::Encoding.xmlliteral.should == Literal::XMLLiteral.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
305
341
  end
@@ -50,14 +50,20 @@ describe "Namespace" do
50
50
  subject.foo.should == "http://xmlns.com/foaf/0.1/foo"
51
51
  end
52
52
 
53
- it "should construct URI with +" do
54
- (subject + "foo").class.should == URIRef
55
- (subject + "foo").should == "http://xmlns.com/foaf/0.1/foo"
56
- end
57
-
58
- it "will cause method conflict" do
59
- (subject + "class").should == "http://xmlns.com/foaf/0.1/class"
60
- subject.class.should == Namespace
53
+ describe '#+' do
54
+ it "should construct URI with +" do
55
+ (subject + "foo").class.should == URIRef
56
+ (subject + "foo").should == "http://xmlns.com/foaf/0.1/foo"
57
+ end
58
+
59
+ it "should strip trailing _ (used to work around reserved method names)" do
60
+ (subject + "type_").should == "http://xmlns.com/foaf/0.1/type"
61
+ end
62
+
63
+ it "will cause method conflict" do
64
+ (subject + "class").should == "http://xmlns.com/foaf/0.1/class"
65
+ subject.class.should == Namespace
66
+ end
61
67
  end
62
68
 
63
69
  it "should be be equivalent" do
data/spec/rdfxml_spec.rb CHANGED
@@ -20,10 +20,8 @@ EOF
20
20
  end
21
21
 
22
22
  it "should parse simple doc without a base URI" do
23
- sampledoc = <<-EOF;
24
- <?xml version="1.0" ?>
25
- <NotRDF />
26
- EOF
23
+ sampledoc = %(<?xml version="1.0" ?>
24
+ <NotRDF />)
27
25
  graph = @parser.parse(sampledoc, nil, :strict => true)
28
26
  graph.size.should == 1
29
27
  statement = graph[0]
data/spec/triple_spec.rb CHANGED
@@ -61,7 +61,7 @@ describe "Triples" do
61
61
  it "should accept a uri string and make URIRef" do
62
62
  Triple.coerce_subject('http://localhost/').should == URIRef.new('http://localhost/')
63
63
  end
64
-
64
+
65
65
  it "should accept an Addressable::URI object and make URIRef" do
66
66
  Triple.coerce_subject(Addressable::URI.parse("http://localhost/")).should == URIRef.new("http://localhost/")
67
67
  end
@@ -91,6 +91,26 @@ describe "Triples" do
91
91
  end
92
92
 
93
93
  describe "with coerced object" do
94
+ it 'should make a literal for Integer types' do
95
+ ref = 5
96
+ Triple.coerce_object(ref).should == Literal.build_from(ref)
97
+ end
98
+
99
+ it 'should make a literal for Float types' do
100
+ ref = 15.4
101
+ Triple.coerce_object(ref).should == Literal.build_from(ref)
102
+ end
103
+
104
+ it 'should make a literal for Date types' do
105
+ ref = Date::civil(2010, 1, 2)
106
+ Triple.coerce_object(ref).should == Literal.build_from(ref)
107
+ end
108
+
109
+ it 'should make a literal for DateTime types' do
110
+ ref = DateTime.parse('2010-01-03T01:02:03')
111
+ Triple.coerce_object(ref).should == Literal.build_from(ref)
112
+ end
113
+
94
114
  it "should leave URIRefs alone" do
95
115
  ref = URIRef.new("http://localhost/")
96
116
  Triple.coerce_object(ref).should == ref
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdf_context
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregg Kellogg
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-15 00:00:00 -08:00
12
+ date: 2010-01-16 00:00:00 -08:00
13
13
  default_executable: rdf_context
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency