activerdf 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ == activerdf (1.2.3) Wed, 14 Feb 2007 16:07:41 +0000
2
+ * namespaced properties are first-class citizens: FOAF::name or RDFS::domain
3
+
1
4
  == activerdf (1.2.2) Tue, 13 Feb 2007 19:40:19 +0000
2
5
  * gems published automatically from Rakefile
3
6
  * added support for namespaced attributes:
data/README CHANGED
@@ -25,7 +25,7 @@ people found in the data source:
25
25
  people = FOAF::Person.find_all
26
26
 
27
27
  == License
28
- ActiveRDF is distributed under the LGPL[link:http://www.gnu.org/licenses/lgpl.html] license.
28
+ ActiveRDF is distributed under the LGPL[http://www.gnu.org/licenses/lgpl.html] license.
29
29
 
30
30
  == Authors
31
31
  * Eyal Oren
@@ -7,6 +7,7 @@ class FederationManager
7
7
  # add triple s,p,o to the currently selected write-adapter
8
8
  def FederationManager.add(s,p,o)
9
9
  # TODO: allow addition of full graphs
10
+ raise ActiveRdfError, "cannot write without a write-adapter" unless ConnectionPool.write_adapter
10
11
  ConnectionPool.write_adapter.add(s,p,o)
11
12
  end
12
13
 
@@ -13,6 +13,30 @@ class Namespace
13
13
  $activerdflog.info "Namespace: registering #{fullURI} to #{prefix}"
14
14
  @@namespaces[prefix.to_sym] = fullURI.to_s
15
15
  @@inverted_namespaces[fullURI.to_s] = prefix.to_sym
16
+
17
+ # enable namespace lookups through FOAF::name
18
+ # if FOAF defined, add to it
19
+ if Object.const_defined?(prefix.to_s.upcase)
20
+ ns = Object.const_get(prefix.to_s.upcase)
21
+ else
22
+ # otherwise create a new module for it
23
+ ns = Module.new
24
+ Object.const_set(prefix.to_s.upcase, ns)
25
+ end
26
+
27
+ # catch FOAF::name or all other lookups
28
+ class << ns
29
+ def method_missing(method, *args)
30
+ Namespace.lookup(self.to_s.downcase.to_sym, method)
31
+ end
32
+
33
+ # make some builtin methods private because lookup doesn't work otherwise
34
+ # on e.g. RDF::type and FOAF::name
35
+ [:type, :name].each {|m| private(m) }
36
+ end
37
+
38
+ # return the namespace proxy object
39
+ ns
16
40
  end
17
41
 
18
42
  # returns a resource whose URI is formed by concatenation of prefix and localname
@@ -0,0 +1,57 @@
1
+ # Author:: Eyal Oren
2
+ # Copyright:: (c) 2005-2006 Eyal Oren
3
+ # License:: LGPL
4
+ require 'active_rdf'
5
+ require 'uuidtools'
6
+
7
+ # ntriples parser
8
+ class NTriplesParser
9
+ # parses an input string of ntriples and returns a nested array of [s, p, o]
10
+ # (which are in turn ActiveRDF objects)
11
+ def self.parse(input)
12
+
13
+ # need unique identifier for this batch of triples (to detect occurence of
14
+ # same bnodes _:#1
15
+ uuid = UUID.random_create.to_s
16
+
17
+ input.collect do |triple|
18
+ nodes = triple.scan(Node)
19
+
20
+ # handle bnodes if necessary (bnodes need to have uri generated)
21
+ subject = case nodes[0]
22
+ when BNode
23
+ RDFS::Resource.new("http://www.activerdf.org/bnode/#{uuid}/#$1")
24
+ else
25
+ RDFS::Resource.new(nodes[0])
26
+ end
27
+
28
+ predicate = RDFS::Resource.new(nodes[1])
29
+
30
+ # handle bnodes and literals if necessary (literals need unicode fixing)
31
+ object = case nodes[2]
32
+ when BNode
33
+ RDFS::Resource.new("http://www.activerdf.org/bnode/#{uuid}/#$1")
34
+ when Literal
35
+ fix_unicode(nodes[2])
36
+ else
37
+ RDFS::Resourec.new(nodes[2])
38
+ end
39
+
40
+ # collect s, p, o into array to be returned
41
+ [subject, predicate, object]
42
+ end
43
+ end
44
+
45
+ private
46
+ # constants for extracting resources/literals from sql results
47
+ Node = Regexp.union(/_:\S*/,/<[^>]*>/,/"[^"]*"/)
48
+ BNode = /_:(\S*)/
49
+ Resource = /<([^>]*)>/
50
+ Literal = /"([^"]*)"/
51
+
52
+ # fixes unicode characters in literals (because we parse them wrongly somehow)
53
+ def self.fix_unicode(str)
54
+ tmp = str.gsub(/\\\u([0-9a-fA-F]{4,4})/u){ "U+#$1" }
55
+ tmp.gsub(/U\+([0-9a-fA-F]{4,4})/u){["#$1".hex ].pack('U*')}
56
+ end
57
+ end
@@ -42,12 +42,9 @@ class TestFederationManager < Test::Unit::TestCase
42
42
 
43
43
  def test_class_add_no_write_adapter
44
44
  # zero write, one read -> must raise error
45
-
46
45
  adapter = get_read_only_adapter
47
- assert(!(adapter.writes?))
48
- assert_raises NoMethodError do
49
- FederationManager.add(@@eyal, @@age, @@age_number)
50
- end
46
+ assert (not adapter.writes?)
47
+ assert_raises(ActiveRdfError) { FederationManager.add(@@eyal, @@age, @@age_number) }
51
48
  end
52
49
 
53
50
  def test_class_add_one_write_one_read
@@ -62,4 +62,16 @@ class TestNamespace < Test::Unit::TestCase
62
62
 
63
63
  assert_equal abc, Namespace.expand(:test, :abc)
64
64
  end
65
+
66
+ def test_attributes
67
+ assert_nothing_raised { RDFS::domain }
68
+ assert_nothing_raised { RDF::type }
69
+ assert_raise(NameError) { FOAF::type }
70
+
71
+ foaf = 'http://xmlns.com/foaf/0.1/'
72
+ Namespace.register :foaf, foaf
73
+
74
+ foafname = RDFS::Resource.new(foaf + 'name')
75
+ assert_equal foafname, FOAF::name
76
+ end
65
77
  end
@@ -12,6 +12,8 @@ class TestResourceReading < Test::Unit::TestCase
12
12
  ConnectionPool.clear
13
13
  @adapter = get_read_only_adapter
14
14
  Namespace.register(:ar, 'http://activerdf.org/test/')
15
+ ObjectManager.construct_classes
16
+
15
17
  @eyal = RDFS::Resource.new 'http://activerdf.org/test/eyal'
16
18
  end
17
19
 
@@ -75,9 +77,6 @@ class TestResourceReading < Test::Unit::TestCase
75
77
 
76
78
  # test for writing if no write adapter is defined (like only sparqls)
77
79
  def test_write_without_write_adapter
78
- assert_raises NoMethodError do
79
- @eyal.age = 18
80
- end
80
+ assert_raises(ActiveRdfError) { @eyal.age = 18 }
81
81
  end
82
-
83
82
  end
@@ -0,0 +1,31 @@
1
+ # Author:: Eyal Oren
2
+ # Copyright:: (c) 2005-2006
3
+ # License:: LGPL
4
+
5
+ require 'test/unit'
6
+ require 'active_rdf'
7
+ require 'queryengine/ntriples_parser'
8
+ require "#{File.dirname(__FILE__)}/../common"
9
+
10
+ class TestNTriplesParser < Test::Unit::TestCase
11
+ def setup
12
+ end
13
+
14
+ def teardown
15
+ end
16
+
17
+ def test_the_parser
18
+ str = <<EOF
19
+ <http://www.johnbreslin.com/blog/author/cloud/#foaf> <http://xmlns.com/foaf/0.1/surname> "Breslin" .
20
+ <http://www.johnbreslin.com/blog/author/cloud/#foaf> <http://xmlns.com/foaf/0.1/firstName> "John" .
21
+ <http://www.johnbreslin.com/blog/author/cloud/> <http://purl.org/dc/terms/created> "1999-11-30T00:00:00" .
22
+ EOF
23
+
24
+ results = NTriplesParser.parse(str)
25
+ assert_equal 9, results.flatten.size
26
+ assert_equal 3, results[0].size
27
+
28
+ assert_equal RDFS::Resource, results[0][0].class
29
+ assert_equal String, results[0][2].class
30
+ end
31
+ end
@@ -48,7 +48,7 @@ class TestQuery < Test::Unit::TestCase
48
48
  end
49
49
 
50
50
  def test_query_refuses_string_in_where_clause_subject_or_predicate
51
- assert_raises ActiveRdfError do
51
+ assert_raises(ActiveRdfError)do
52
52
  Query.new.select(:s).where("http://test.org/uri",:p, :o).execute
53
53
  end
54
54
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: activerdf
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.2.2
7
- date: 2007-02-13 00:00:00 +00:00
6
+ version: 1.2.3
7
+ date: 2007-02-14 00:00:00 +00:00
8
8
  summary: Offers object-oriented access to RDF (with adapters to several datastores).
9
9
  require_paths:
10
10
  - lib
@@ -48,6 +48,7 @@ files:
48
48
  - test/queryengine/test_query2jars2.rb
49
49
  - test/queryengine/test_query2sparql.rb
50
50
  - test/queryengine/test_query_engine.rb
51
+ - test/queryengine/test_ntriples_parser.rb
51
52
  - lib/active_rdf
52
53
  - lib/active_rdf.rb
53
54
  - lib/active_rdf_helpers.rb
@@ -64,6 +65,7 @@ files:
64
65
  - lib/active_rdf/queryengine/query.rb
65
66
  - lib/active_rdf/queryengine/query2jars2.rb
66
67
  - lib/active_rdf/queryengine/query2sparql.rb
68
+ - lib/active_rdf/queryengine/ntriples_parser.rb
67
69
  test_files: []
68
70
 
69
71
  rdoc_options: []