reddy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/HACKNOTES +2 -0
  2. data/History.txt +3 -0
  3. data/Manifest.txt +80 -0
  4. data/README.rdoc +48 -0
  5. data/README.txt +62 -0
  6. data/Rakefile +67 -0
  7. data/lib/reddy.rb +8 -0
  8. data/lib/reddy/bnode.rb +70 -0
  9. data/lib/reddy/exceptions/about_each_exception.rb +2 -0
  10. data/lib/reddy/exceptions/uri_relative_exception.rb +2 -0
  11. data/lib/reddy/graph.rb +182 -0
  12. data/lib/reddy/libxml_hacks.rb +6 -0
  13. data/lib/reddy/literal.rb +211 -0
  14. data/lib/reddy/n3_grammar.treetop +129 -0
  15. data/lib/reddy/n3parser.rb +145 -0
  16. data/lib/reddy/namespace.rb +73 -0
  17. data/lib/reddy/rdfaparser.rb +63 -0
  18. data/lib/reddy/rdfxmlparser.rb +254 -0
  19. data/lib/reddy/rexml_hacks.rb +97 -0
  20. data/lib/reddy/triple.rb +95 -0
  21. data/lib/reddy/uriref.rb +66 -0
  22. data/reddy.gemspec +50 -0
  23. data/spec/bnode_spec.rb +29 -0
  24. data/spec/graph_spec.rb +138 -0
  25. data/spec/literal_spec.rb +142 -0
  26. data/spec/n3parser_spec.rb +86 -0
  27. data/spec/namespaces_spec.rb +44 -0
  28. data/spec/parser_spec.rb +391 -0
  29. data/spec/rdfa_parser_spec.rb +28 -0
  30. data/spec/rexml_hacks_spec.rb +99 -0
  31. data/spec/triple_spec.rb +108 -0
  32. data/spec/uriref_spec.rb +96 -0
  33. data/test/longtests_spec.rb +25 -0
  34. data/test/n3_tests/lcsh/sh85062913.n3 +41 -0
  35. data/test/n3_tests/lcsh/sh85062913.nt +21 -0
  36. data/test/n3_tests/lcsh/sh85082139.n3 +157 -0
  37. data/test/n3_tests/lcsh/sh85082139.nt +79 -0
  38. data/test/n3_tests/lcsh/sh85118553.n3 +123 -0
  39. data/test/n3_tests/lcsh/sh85118553.nt +63 -0
  40. data/test/n3_tests/misc/on_now-01.n3 +30 -0
  41. data/test/n3_tests/misc/on_now-01.nt +15 -0
  42. data/test/n3_tests/n3p/simple-01.n3 +1 -0
  43. data/test/n3_tests/n3p/simple-01.nt +0 -0
  44. data/test/n3_tests/n3p/simple-02.n3 +4 -0
  45. data/test/n3_tests/n3p/simple-02.nt +0 -0
  46. data/test/n3_tests/n3p/simple-03.n3 +5 -0
  47. data/test/n3_tests/n3p/simple-03.nt +1 -0
  48. data/test/n3_tests/n3p/simple-04.n3 +6 -0
  49. data/test/n3_tests/n3p/simple-04.nt +3 -0
  50. data/test/n3_tests/n3p/simple-05.n3 +7 -0
  51. data/test/n3_tests/n3p/simple-05.nt +2 -0
  52. data/test/n3_tests/n3p/simple-06.n3 +6 -0
  53. data/test/n3_tests/n3p/simple-06.nt +4 -0
  54. data/test/n3_tests/n3p/simple-07.n3 +7 -0
  55. data/test/n3_tests/n3p/simple-07.nt +6 -0
  56. data/test/perf_test/test.rb +11 -0
  57. data/test/perf_test/tommorris.rdf +2267 -0
  58. data/test/rdf_tests/cc197bad-dc9c-440d-a5b5-d52ba2e14234.nt +24 -0
  59. data/test/rdf_tests/cc197bad-dc9c-440d-a5b5-d52ba2e14234.rdf +46 -0
  60. data/test/rdf_tests/tm_001.nt +1 -0
  61. data/test/rdf_tests/tm_001.rdf +7 -0
  62. data/test/rdf_tests/xml-literal-mixed.nt +7 -0
  63. data/test/rdf_tests/xml-literal-mixed.rdf +15 -0
  64. data/test/ruby_fundamentals.spec.rb +17 -0
  65. data/test/test_helper.rb +2 -0
  66. data/test/test_reddy.rb +11 -0
  67. data/test/test_uris.rb +13 -0
  68. data/test/xml.rdf +6 -0
  69. metadata +198 -0
@@ -0,0 +1,95 @@
1
+ module Reddy
2
+ class Triple
3
+ class InvalidPredicate < StandardError
4
+ end
5
+
6
+ class InvalidSubject < StandardError
7
+ end
8
+
9
+ class InvalidObject < StandardError
10
+ end
11
+
12
+ attr_accessor :subject, :object, :predicate
13
+
14
+ ##
15
+ # Creates a new triple directly from the intended subject, predicate, and object.
16
+ #
17
+ # ==== Example
18
+ # Triple.new(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new) # => results in the creation of a new triple and returns it
19
+ #
20
+ # @param [URIRef, BNode] s the subject of the triple
21
+ # @param [URIRef] p the predicate of the triple
22
+ # @param [URIRef, BNode, Literal, TypedLiteral] o the object of the triple
23
+ #
24
+ # ==== Returns
25
+ #
26
+ # @return [Triple] An array of the triples (leaky abstraction? consider returning the graph instead)
27
+ #
28
+ # @raise [Error] Checks parameter types and raises if they are incorrect.
29
+ # @author Tom Morris
30
+ def initialize (subject, predicate, object)
31
+ @subject = self.class.coerce_subject(subject)
32
+ @predicate = self.class.coerce_predicate(predicate)
33
+ @object = self.class.coerce_object(object)
34
+ end
35
+
36
+ def to_ntriples
37
+ @subject.to_ntriples + " " + @predicate.to_ntriples + " " + @object.to_ntriples + " ."
38
+ end
39
+
40
+ def inspect
41
+ [@subject, @predicate, @object].inspect
42
+ end
43
+
44
+ def is_type?
45
+ @predicate.to_s == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
46
+ end
47
+
48
+ protected
49
+
50
+ def self.coerce_subject(subject)
51
+ case subject
52
+ when Addressable::URI
53
+ URIRef.new(subject.to_s)
54
+ when URIRef, BNode
55
+ subject
56
+ when String
57
+ if subject =~ /\S+\/\/\S+/ # does it smell like a URI?
58
+ URIRef.new subject
59
+ else
60
+ BNode.new subject
61
+ end
62
+ else
63
+ raise InvalidSubject, "Subject is not of a known class (#{subject.class}: #{subject.inspect})"
64
+ end
65
+ end
66
+
67
+ def self.coerce_predicate(uri_or_string)
68
+ case uri_or_string
69
+ when URIRef
70
+ uri_or_string
71
+ when String
72
+ URIRef.new uri_or_string
73
+ else
74
+ raise InvalidPredicate, "Predicate should be a URI"
75
+ end
76
+ rescue Reddy::UriRelativeException => e
77
+ raise InvalidPredicate, "Couldn't make a URIRef: #{e.message}"
78
+ end
79
+
80
+ def self.coerce_object(object)
81
+ case object
82
+ when Addressable::URI
83
+ URIRef.new(object.to_s)
84
+ when String, Integer, Float
85
+ Literal.untyped(object)
86
+ # when URIRef, BNode, Literal, TypedLiteral
87
+ # Literal.build_from(object)
88
+ when URIRef, BNode, Literal
89
+ object
90
+ else
91
+ raise InvalidObject, "#{object.class}: #{object.inspect} is not a valid object"
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,66 @@
1
+ require 'rubygems'
2
+ require 'addressable/uri'
3
+ require 'net/http'
4
+
5
+ module Reddy
6
+ class UriRelativeException < StandardError
7
+ end
8
+
9
+ class URIRef
10
+ attr_accessor :uri
11
+ def initialize (string)
12
+ self.test_string(string)
13
+ @uri = Addressable::URI.parse(string)
14
+ if @uri.relative?
15
+ raise UriRelativeException, "<" + @uri.to_s + ">"
16
+ end
17
+ if !@uri.to_s.match(/^javascript/).nil?
18
+ raise "Javascript pseudo-URIs are not acceptable"
19
+ end
20
+ end
21
+
22
+ def + (input)
23
+ if input.class == String
24
+ input_uri = Addressable::URI.parse(input)
25
+ else
26
+ input_uri = Addressable::URI.parse(input.to_s)
27
+ end
28
+ return URIRef.new((@uri + input_uri).to_s)
29
+ end
30
+
31
+ def short_name
32
+ if @uri.fragment()
33
+ return @uri.fragment()
34
+ elsif @uri.path.split("/").last.class == String and @uri.path.split("/").last.length > 0
35
+ return @uri.path.split("/").last
36
+ else
37
+ return false
38
+ end
39
+ end
40
+
41
+ def == (other)
42
+ return true if @uri == other.uri
43
+ end
44
+
45
+ def to_s
46
+ @uri.to_s
47
+ end
48
+
49
+ def to_ntriples
50
+ "<" + @uri.to_s + ">"
51
+ end
52
+
53
+ def test_string (string)
54
+ string.to_s.each_byte do |b|
55
+ if b >= 0 and b <= 31
56
+ raise "URI must not contain control characters"
57
+ end
58
+ end
59
+ end
60
+
61
+ def load_graph
62
+ get = Net::HTTP.start(@uri.host, @uri.port) {|http| [:xml, http.get(@uri.path)] }
63
+ return Reddy::RdfXmlParser.new(get[1].body, @uri.to_s).graph if get[0] == :xml
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{reddy}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Tom Morris"]
9
+ s.date = %q{2008-12-02}
10
+ s.description = %q{Reddy is an RDF library for Ruby.}
11
+ s.email = ["tom@tommorris.org"]
12
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc", "README.txt"]
13
+ s.files = ["HACKNOTES", "History.txt", "lib", "lib/reddy", "lib/reddy/libxml_hacks.rb", "lib/reddy/rexml_hacks.rb", "lib/reddy/n3_grammar.treetop", "lib/reddy/uriref.rb", "lib/reddy/rdfxmlparser.rb", "lib/reddy/n3parser.rb", "lib/reddy/exceptions", "lib/reddy/exceptions/about_each_exception.rb", "lib/reddy/exceptions/uri_relative_exception.rb", "lib/reddy/namespace.rb", "lib/reddy/graph.rb", "lib/reddy/bnode.rb", "lib/reddy/triple.rb", "lib/reddy/rdfaparser.rb", "lib/reddy/literal.rb", "lib/reddy.rb", "Manifest.txt", "Rakefile", "README.rdoc", "README.txt", "reddy.gemspec", "spec", "spec/uriref_spec.rb", "spec/rdfa_parser_spec.rb", "spec/n3parser_spec.rb", "spec/graph_spec.rb", "spec/literal_spec.rb", "spec/bnode_spec.rb", "spec/triple_spec.rb", "spec/parser_spec.rb", "spec/rexml_hacks_spec.rb", "spec/namespaces_spec.rb", "tasks", "test", "test/xml.rdf", "test/n3_tests", "test/n3_tests/misc", "test/n3_tests/misc/on_now-01.nt", "test/n3_tests/misc/on_now-01.n3", "test/n3_tests/n3p", "test/n3_tests/n3p/simple-07.nt", "test/n3_tests/n3p/simple-05.n3", "test/n3_tests/n3p/simple-06.n3", "test/n3_tests/n3p/simple-07.n3", "test/n3_tests/n3p/simple-06.nt", "test/n3_tests/n3p/simple-02.nt", "test/n3_tests/n3p/simple-04.nt", "test/n3_tests/n3p/simple-02.n3", "test/n3_tests/n3p/simple-03.nt", "test/n3_tests/n3p/simple-01.nt", "test/n3_tests/n3p/simple-01.n3", "test/n3_tests/n3p/simple-04.n3", "test/n3_tests/n3p/simple-03.n3", "test/n3_tests/n3p/simple-05.nt", "test/n3_tests/lcsh", "test/n3_tests/lcsh/sh85082139.nt", "test/n3_tests/lcsh/sh85118553.nt", "test/n3_tests/lcsh/sh85118553.n3", "test/n3_tests/lcsh/sh85062913.n3", "test/n3_tests/lcsh/sh85082139.n3", "test/n3_tests/lcsh/sh85062913.nt", "test/perf_test", "test/perf_test/tommorris.rdf", "test/perf_test/test.rb", "test/rdf_tests", "test/rdf_tests/xml-literal-mixed.rdf", "test/rdf_tests/xml-literal-mixed.nt", "test/rdf_tests/tm_001.nt", "test/rdf_tests/cc197bad-dc9c-440d-a5b5-d52ba2e14234.rdf", "test/rdf_tests/tm_001.rdf", "test/rdf_tests/cc197bad-dc9c-440d-a5b5-d52ba2e14234.nt", "test/test_helper.rb", "test/test_uris.rb", "test/test_reddy.rb", "test/longtests_spec.rb", "test/ruby_fundamentals.spec.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/tommorris/reddy}
16
+ s.rdoc_options = ["--main", "README.txt"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{reddy}
19
+ s.rubygems_version = %q{1.3.0}
20
+ s.summary = %q{Reddy is an RDF library for Ruby.}
21
+ s.test_files = ["test/test_helper.rb", "test/test_uris.rb", "test/test_reddy.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_runtime_dependency(%q<addressable>, [">= 2.0.0"])
29
+ s.add_runtime_dependency(%q<treetop>, [">= 1.2.4"])
30
+ s.add_runtime_dependency(%q<libxml-ruby>, [">= 0.8.3"])
31
+ s.add_runtime_dependency(%q<whatlanguage>, [">= 1.0.0"])
32
+ s.add_development_dependency(%q<newgem>, [">= 1.1.0"])
33
+ s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
34
+ else
35
+ s.add_dependency(%q<addressable>, [">= 2.0.0"])
36
+ s.add_dependency(%q<treetop>, [">= 1.2.4"])
37
+ s.add_dependency(%q<libxml-ruby>, [">= 0.8.3"])
38
+ s.add_dependency(%q<whatlanguage>, [">= 1.0.0"])
39
+ s.add_dependency(%q<newgem>, [">= 1.1.0"])
40
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
41
+ end
42
+ else
43
+ s.add_dependency(%q<addressable>, [">= 2.0.0"])
44
+ s.add_dependency(%q<treetop>, [">= 1.2.4"])
45
+ s.add_dependency(%q<libxml-ruby>, [">= 0.8.3"])
46
+ s.add_dependency(%q<whatlanguage>, [">= 1.0.0"])
47
+ s.add_dependency(%q<newgem>, [">= 1.1.0"])
48
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
49
+ end
50
+ end
@@ -0,0 +1,29 @@
1
+ require 'lib/reddy'
2
+ describe "Blank nodes" do
3
+ it "should accept a custom identifier" do
4
+ b = BNode.new('foo')
5
+ b.identifier.should == "foo"
6
+ b.to_s.should == "foo"
7
+ end
8
+
9
+ it "should reject custom identifiers if they are not acceptable" do
10
+ b = BNode.new("4cake")
11
+ b.identifier.should_not == "4cake"
12
+ end
13
+
14
+ it "should be expressible in N3 and NT syntax" do
15
+ b = BNode.new('test')
16
+ b.to_n3.should == "_:test"
17
+ b.to_ntriples.should == b.to_n3
18
+ end
19
+
20
+ it "should be able to determine equality" do
21
+ a = BNode.new('a')
22
+ a2 = BNode.new('a')
23
+ a.eql?(a2).should be_true
24
+
25
+ a3 = URIRef.new('http://somehost.com/wherever.xml')
26
+ a.eql?(a3).should be_false
27
+ end
28
+
29
+ end
@@ -0,0 +1,138 @@
1
+ require 'lib/reddy'
2
+
3
+ describe "Graphs" do
4
+ it "should allow you to add one or more triples" do
5
+ lambda do
6
+ f = Graph.new
7
+ f.add_triple(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new)
8
+ end.should_not raise_error
9
+ end
10
+
11
+ it "should tell you how large the graph is" do
12
+ f = Graph.new
13
+ 5.times do
14
+ f.add_triple BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new
15
+ end
16
+ f.size.should == 5
17
+ end
18
+
19
+ it "should support << as an alias for add_triple" do
20
+ lambda do
21
+ f = Graph.new
22
+ f << Triple.new(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new)
23
+ end.should_not raise_error
24
+ end
25
+
26
+ it "should output NTriple" do
27
+ f = Graph.new
28
+ ex = Namespace.new("http://example.org/", "ex")
29
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
30
+ f << Triple.new(ex.john, foaf.knows, ex.jane)
31
+ f << Triple.new(ex.jane, foaf.knows, ex.rick)
32
+ f << Triple.new(ex.rick, foaf.knows, ex.john)
33
+ nt = "<http://example.org/john> <http://xmlns.com/foaf/0.1/knows> <http://example.org/jane> .\n<http://example.org/jane> <http://xmlns.com/foaf/0.1/knows> <http://example.org/rick> .\n<http://example.org/rick> <http://xmlns.com/foaf/0.1/knows> <http://example.org/john> .\n"
34
+ f.to_ntriples.should == nt
35
+ end
36
+
37
+ it "should allow you to select one resource" do
38
+ f = Graph.new
39
+ ex = Namespace.new("http://example.org/", "ex")
40
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
41
+ f << Triple.new(ex.john, foaf.knows, ex.jane)
42
+ f << Triple.new(ex.jane, foaf.knows, ex.rick)
43
+ f << Triple.new(ex.rick, foaf.knows, ex.john)
44
+ f.get_resource(ex.john).size.should == 1
45
+ end
46
+
47
+ it "should allow iteration" do
48
+ f = Graph.new
49
+ ex = Namespace.new("http://example.org/", "ex")
50
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
51
+ f << Triple.new(ex.john, foaf.knows, ex.jane)
52
+ f << Triple.new(ex.jane, foaf.knows, ex.rick)
53
+ f << Triple.new(ex.rick, foaf.knows, ex.john)
54
+ count = 0
55
+ f.each do |t|
56
+ count = count + 1
57
+ t.class.should == Triple
58
+ end
59
+ count.should == 3
60
+ end
61
+
62
+ it "should allow iteration over a particular subject" do
63
+ f = Graph.new
64
+ ex = Namespace.new("http://example.org/", "ex")
65
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
66
+ f << Triple.new(ex.john, foaf.knows, ex.jane)
67
+ f << Triple.new(ex.jane, foaf.knows, ex.rick)
68
+ f << Triple.new(ex.rick, foaf.knows, ex.john)
69
+ count = 0
70
+ f.each_with_subject(ex.john) do |t|
71
+ count = count + 1
72
+ t.class.should == Triple
73
+ end
74
+ count.should == 1
75
+ end
76
+
77
+ it "should be able to determine whether or not it has existing BNodes" do
78
+ f = Graph.new
79
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
80
+ f << Triple.new(BNode.new('john'), foaf.knows, BNode.new('jane'))
81
+ f.has_bnode_identifier?('john').should == true
82
+ f.has_bnode_identifier?('jane').should == true
83
+ f.has_bnode_identifier?('jack').should == false
84
+ end
85
+
86
+ it "should be able to return BNodes on demand" do
87
+ f = Graph.new
88
+ john = BNode.new('john')
89
+ jane = BNode.new('jane')
90
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
91
+ f << Triple.new(john, foaf.knows, jane)
92
+ f.get_bnode_by_identifier('john').should == john
93
+ f.get_bnode_by_identifier('jane').should == jane
94
+ f.get_bnode_by_identifier('barny').should == false
95
+ end
96
+
97
+ it "should allow you to create and bind Namespace objects on-the-fly" do
98
+ f = Graph.new
99
+ f.namespace("http://xmlns.com/foaf/0.1/", "foaf")
100
+ f.nsbinding["foaf"].uri.should == "http://xmlns.com/foaf/0.1/"
101
+ end
102
+
103
+ it "should not allow you to bind things other than namespaces" do
104
+ lambda do
105
+ f = Graph.new
106
+ f.bind(false)
107
+ end.should raise_error
108
+ end
109
+
110
+ it "should follow the specification as to output identical triples" do
111
+ pending
112
+ end
113
+
114
+ it "should be able to integrate another graph" do
115
+ f = Graph.new
116
+ f.add_triple(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new)
117
+ g = Graph.new
118
+ g.join(f)
119
+ g.size.should == 1
120
+
121
+ h = Graph.new
122
+ lambda do
123
+ h.join("")
124
+ end.should raise_error
125
+ end
126
+
127
+ it "should give you a list of resources of a particular type" do
128
+ f = Graph.new
129
+ person = URIRef.new("http://xmlns.com/foaf/0.1/Person")
130
+ f.add_triple(URIRef.new("http://example.org/joe"), URIRef.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), URIRef.new("http://xmlns.com/foaf/0.1/Person"))
131
+ f.add_triple(URIRef.new("http://example.org/jane"), URIRef.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), URIRef.new("http://xmlns.com/foaf/0.1/Person"))
132
+ f.size.should == 2
133
+
134
+ f.get_by_type("http://xmlns.com/foaf/0.1/Person").size.should == 2
135
+ f.get_by_type("http://xmlns.com/foaf/0.1/Person")[0].to_s.should == "http://example.org/joe"
136
+ f.get_by_type("http://xmlns.com/foaf/0.1/Person")[1].to_s.should == "http://example.org/jane"
137
+ end
138
+ end
@@ -0,0 +1,142 @@
1
+ require 'lib/reddy'
2
+
3
+ describe "Literals" do
4
+ it "accept a language tag" do
5
+ f = Literal.untyped("tom", "en")
6
+ f.lang.should == "en"
7
+ end
8
+
9
+ it "accepts an encoding" do
10
+ f = Literal.typed("tom", "http://www.w3.org/2001/XMLSchema#string")
11
+ f.encoding.to_s.should == "http://www.w3.org/2001/XMLSchema#string"
12
+ end
13
+
14
+ it "should be equal if they have the same contents" do
15
+ f = Literal.untyped("tom")
16
+ g = Literal.untyped("tom")
17
+ f.should == g
18
+ end
19
+
20
+ it "should not be equal if they do not have the same contents" do
21
+ f = Literal.untyped("tom")
22
+ g = Literal.untyped("tim")
23
+ f.should_not == g
24
+ end
25
+
26
+ it "should be equal if they have the same contents and language" do
27
+ f = Literal.untyped("tom", "en")
28
+ g = Literal.untyped("tom", "en")
29
+ f.should == g
30
+ end
31
+
32
+ it "should return a string using to_s" do
33
+ f = Literal.untyped("tom")
34
+ f.to_s.should == "tom"
35
+ end
36
+
37
+ it "should not be equal if they do not have the same contents and language" do
38
+ f = Literal.untyped("tom", "en")
39
+ g = Literal.untyped("tim", "en")
40
+ f.should_not == g
41
+
42
+ lf = Literal.untyped("tom", "en")
43
+ lg = Literal.untyped("tom", "fr")
44
+ lf.should_not == lg
45
+ end
46
+
47
+ it "should be equal if they have the same contents and datatype" do
48
+ f = Literal.typed("tom", "http://www.w3.org/2001/XMLSchema#string")
49
+ g = Literal.typed("tom", "http://www.w3.org/2001/XMLSchema#string")
50
+ f.should == g
51
+ end
52
+
53
+ it "should not be equal if they do not have the same contents and datatype" do
54
+ f = Literal.typed("tom", "http://www.w3.org/2001/XMLSchema#string")
55
+ g = Literal.typed("tim", "http://www.w3.org/2001/XMLSchema#string")
56
+ f.should_not == g
57
+
58
+ dtf = Literal.typed("tom", "http://www.w3.org/2001/XMLSchema#string")
59
+ dtg = Literal.typed("tom", "http://www.w3.org/2001/XMLSchema#token")
60
+ dtf.should_not == dtg
61
+ end
62
+
63
+ it "should return valid N3/NTriples format strings" do
64
+ f = Literal.untyped("tom")
65
+ f.to_n3.should == "\"tom\""
66
+ f.to_ntriples.should == f.to_n3
67
+
68
+ g = Literal.untyped("tom", "en")
69
+ g.to_n3.should == "\"tom\"@en"
70
+ f.to_ntriples.should == f.to_n3
71
+
72
+ typed_int = Literal.typed(5, "http://www.w3.org/2001/XMLSchema#int")
73
+ typed_int.to_n3.should == "5^^<http://www.w3.org/2001/XMLSchema#int>"
74
+ typed_int.to_ntriples.should == typed_int.to_n3
75
+
76
+ typed_string = Literal.typed("foo", "http://www.w3.org/2001/XMLSchema#string")
77
+ typed_string.to_n3.should == "\"foo\"^^<http://www.w3.org/2001/XMLSchema#string>"
78
+ typed_string.to_ntriples.should == typed_string.to_n3
79
+ end
80
+
81
+ it "should normalize language tags to lower case" do
82
+ f = Literal.untyped("tom", "EN")
83
+ f.lang.should == "en"
84
+ end
85
+
86
+ it "should support TriX encoding" do
87
+ e = Literal.untyped("tom")
88
+ e.to_trix.should == "<plainLiteral>tom</plainLiteral>"
89
+
90
+ f = Literal.untyped("tom", "en")
91
+ f.to_trix.should == "<plainLiteral xml:lang=\"en\">tom</plainLiteral>"
92
+
93
+ g = Literal.typed("tom", "http://www.w3.org/2001/XMLSchema#string")
94
+ g.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#string\">tom</typedLiteral>"
95
+ end
96
+
97
+ it "should handle XML litearls" do
98
+ # first we should detect XML literals and mark them as such in the class
99
+ f = Literal.typed("foo <sup>bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
100
+ f.xmlliteral?.should == true
101
+ # pending "TODO: the thought of XML literals makes me want to wretch"
102
+ end
103
+
104
+ it "build_from should infer the type" do
105
+ int = Literal.build_from(15)
106
+ int.encoding.should == "http://www.w3.org/2001/XMLSchema#int"
107
+
108
+ float = Literal.build_from(15.4)
109
+ float.encoding.should == "http://www.w3.org/2001/XMLSchema#float"
110
+
111
+ other = Literal.build_from("foo")
112
+ other.encoding.should == "http://www.w3.org/2001/XMLSchema#string"
113
+ end
114
+
115
+ it "should support XML Literals" do
116
+ xml = Literal.typed("<b>foo</b>", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
117
+ xml.encoding.should == "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"
118
+ xml.to_n3.should == "\"<b>foo</b>\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>"
119
+ end
120
+
121
+ it "build_from_language" do
122
+ english = Literal.build_from_language("Have a nice day")
123
+ english.encoding.should == "en"
124
+
125
+ french = Literal.build_from_language("Bonjour, madame. Parlez vous francais?")
126
+ french.encoding.should == "fr"
127
+
128
+ german = Literal.build_from_language("Achtung")
129
+ german.encoding.should == "de"
130
+ end
131
+
132
+ # TODO: refactor based on new interface
133
+ # describe "Languages" do
134
+ # it "should be inspectable" do
135
+ # literal = Reddy::Literal.new("foo", "en")
136
+ # lang = literal.lang
137
+ # lang.to_s == "en"
138
+ # lang.hash.class.should == Fixnum
139
+ # end
140
+ # end
141
+
142
+ end