lightrdf 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.2.0 2011-03-03
2
+
3
+ * Instantiation of RDF data as Ruby objects (with methods and so on)
4
+
1
5
  === 0.1.9 2011-02-18
2
6
 
3
7
  * Simplified design with much better performance
data/Manifest CHANGED
@@ -6,6 +6,7 @@ bin/yarfp
6
6
  lib/lightrdf.rb
7
7
  lib/lightrdf/graph.rb
8
8
  lib/lightrdf/node.rb
9
+ lib/lightrdf/node_proxy.rb
9
10
  lib/lightrdf/parser.rb
10
11
  lib/lightrdf/repository.rb
11
12
  lib/lightrdf/id.rb
data/README.rdoc CHANGED
@@ -103,6 +103,52 @@ Also, queries for triples can be performed on a graph:
103
103
  # Find all predicates bob has
104
104
  g.find(Node('ex:bob'), nil, []) # => [Node('foaf:name'), Node('foaf:age')]
105
105
 
106
+ == INSTANTIATING OBJECTS:
107
+
108
+ LightRDF allows defining a class hierarchy that is instantiated using RDF data. Simply match
109
+ modules to prefix names and classes to RDF types and include RDF::NodeProxy:
110
+
111
+ module Foaf
112
+ class Person
113
+ include RDF::NodeProxy
114
+ # Optionally, you can specify the mapped URI:
115
+ # maps 'foaf:Person'
116
+ # or
117
+ # maps 'http://xmlns.com/foaf/0.1/Person'
118
+
119
+ # Adds 1 year to the person's age
120
+ def happy_birthday!
121
+ foaf::age = (foaf::age.first.to_i + 1).to_s
122
+ end
123
+ end
124
+ end
125
+
126
+ This class can be instantiated by passing an RDF node or a hash with predicates:
127
+
128
+ Foaf::Person.new.rdf::type # => [Node("http://xmlns.com/foaf/0.1/Person")]
129
+
130
+ person_node = Node(nil)
131
+ person_node.rdf::type = Node('foaf:Person')
132
+ person_node.foaf::age = "19"
133
+ person = Foaf::Person.new(person_node)
134
+ person.foaf::age # => ["19"]
135
+
136
+ person = Foaf::Person.new('foaf:age'=>"25")
137
+ person.happy_birthday!
138
+ person.foaf::age # => ["26"]
139
+
140
+ Also, you can use Graph#node to get instances of class Person out of an RDF graph:
141
+
142
+ graph = RDF::Graph.new
143
+ person_node = Node(nil)
144
+ person_node.rdf::type = Node('foaf:Person')
145
+ person_node.foaf::age = "25"
146
+ graph << person_node
147
+
148
+ person = graph.node(person_node)
149
+ person.happy_birthday!
150
+ person.foaf::age # => ["26"]
151
+
106
152
  == PARSING AND SERIALIZING:
107
153
 
108
154
  LightRDF uses Raptor library internally to support many RDF formats. It also introduces
@@ -41,6 +41,12 @@ module RDF
41
41
  values.select &block
42
42
  end
43
43
 
44
+ # This is equivalent to [], but tries to return a NodeProxy
45
+ def node id
46
+ node = self[id]
47
+ Node.classes[node.rdf::type.first].new node
48
+ end
49
+
44
50
  def find subject, predicate, object
45
51
  # Convert nodes into IDs
46
52
  subject = subject.id if subject.is_a?(Node)
data/lib/lightrdf/id.rb CHANGED
@@ -5,6 +5,7 @@ module RDF
5
5
  :rdfs => 'http://www.w3.org/2000/01/rdf-schema#',
6
6
  :dc => 'http://purl.org/dc/elements/1.1/',
7
7
  :owl => 'http://www.w3.org/2002/07/owl#' }
8
+
8
9
  def self.ns; @ns; end
9
10
  def self.count; @count; end
10
11
  def self.count=c; @count=c; end
data/lib/lightrdf/node.rb CHANGED
@@ -22,6 +22,9 @@ module RDF
22
22
 
23
23
  class Node < Hash
24
24
  include Parser
25
+ @classes = {}
26
+ def self.classes; @classes; end
27
+
25
28
  attr_accessor :graph, :id
26
29
 
27
30
  def initialize id=nil, graph=nil
@@ -0,0 +1,66 @@
1
+ module RDF
2
+ # This class allows instantiating RDF resources as Ruby objects
3
+ module NodeProxy
4
+ def self.included base
5
+ base.extend ClassMethods
6
+ base.send :attr_reader, :node
7
+ base.maps base.to_s.gsub("::",":").gsub(/\A.*:/) { |a| a.downcase }
8
+ end
9
+
10
+ module ClassMethods
11
+ def maps id
12
+ @rdf_type = Node(id)
13
+ Node.classes.delete Node.classes.invert[self]
14
+ Node.classes[@rdf_type] = self
15
+ end
16
+ end
17
+
18
+ # Constructor that allows
19
+ def initialize arg=nil
20
+ @node = if arg.is_a?(RDF::Node)
21
+ arg
22
+ elsif arg.is_a?(Hash)
23
+ new_node = Node('*')
24
+ new_node.rdf::type = rdf_type
25
+ arg.each { |k,v| new_node[k] = (new_node[k] + [v].flatten).uniq }
26
+ new_node
27
+ else
28
+ new_node = Node('*')
29
+ new_node.rdf::type = rdf_type
30
+ new_node
31
+ end
32
+ end
33
+
34
+ # Equality method delegated to node
35
+ def == other_node
36
+ @node.eql? other_node.node
37
+ end
38
+ # Equality method delegated to node
39
+ def eql? other_node
40
+ self.class == other_node.class and @node.id == other_node.node.id
41
+ end
42
+ # Hash method delegated to node
43
+ def hash # Hack for Ruby 1.8.6
44
+ @node.id.hash ^ self.class.hash
45
+ end
46
+
47
+ # to_s method delegated to node
48
+ def to_s
49
+ @node.to_s
50
+ end
51
+
52
+ # id method delegated to node
53
+ def id
54
+ @node.id
55
+ end
56
+
57
+ def rdf_type
58
+ self.class.instance_variable_get("@rdf_type")
59
+ end
60
+
61
+ # Any other method (including any predicate) delegated to node
62
+ def method_missing method, *args
63
+ @node.send method, args
64
+ end
65
+ end
66
+ end
data/lib/lightrdf.rb CHANGED
@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module RDF
5
- VERSION = '0.1.9'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
 
8
8
  require 'rubygems'
@@ -20,4 +20,5 @@ require "lightrdf/id"
20
20
  require "lightrdf/parser"
21
21
  require "lightrdf/graph"
22
22
  require "lightrdf/node"
23
+ require "lightrdf/node_proxy"
23
24
  require "lightrdf/repository"
data/lightrdf.gemspec CHANGED
@@ -2,17 +2,17 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{lightrdf}
5
- s.version = "0.1.9"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jose Ignacio"]
9
- s.date = %q{2011-02-18}
9
+ s.date = %q{2011-03-03}
10
10
  s.default_executable = %q{yarfp}
11
11
  s.description = %q{RDF library}
12
12
  s.email = %q{joseignacio.fernandez@gmail.com}
13
13
  s.executables = ["yarfp"]
14
- s.extra_rdoc_files = ["README.rdoc", "bin/yarfp", "lib/lightrdf.rb", "lib/lightrdf/graph.rb", "lib/lightrdf/node.rb", "lib/lightrdf/parser.rb", "lib/lightrdf/repository.rb", "lib/lightrdf/id.rb"]
15
- s.files = ["History.txt", "Manifest", "README.rdoc", "Rakefile", "bin/yarfp", "lib/lightrdf.rb", "lib/lightrdf/graph.rb", "lib/lightrdf/node.rb", "lib/lightrdf/parser.rb", "lib/lightrdf/repository.rb", "lib/lightrdf/id.rb", "test/test_helper.rb", "test/test_lightrdf.rb", "lightrdf.gemspec"]
14
+ s.extra_rdoc_files = ["README.rdoc", "bin/yarfp", "lib/lightrdf.rb", "lib/lightrdf/graph.rb", "lib/lightrdf/node.rb", "lib/lightrdf/node_proxy.rb", "lib/lightrdf/parser.rb", "lib/lightrdf/repository.rb", "lib/lightrdf/id.rb"]
15
+ s.files = ["History.txt", "Manifest", "README.rdoc", "Rakefile", "bin/yarfp", "lib/lightrdf.rb", "lib/lightrdf/graph.rb", "lib/lightrdf/node.rb", "lib/lightrdf/node_proxy.rb", "lib/lightrdf/parser.rb", "lib/lightrdf/repository.rb", "lib/lightrdf/id.rb", "test/test_helper.rb", "test/test_lightrdf.rb", "lightrdf.gemspec"]
16
16
  s.homepage = %q{http://github.com/josei/lighrdf}
17
17
  s.post_install_message = %q{**Remember to install raptor RDF tools and (optionally for RDF PNG output) Graphviz**}
18
18
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Lightrdf", "--main", "README.rdoc"]
@@ -1,12 +1,27 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
- class TestLightRDF < Test::Unit::TestCase
3
+ Namespace :ex, 'http://www.example.com/ontology#'
4
+ Namespace :foaf, 'http://xmlns.com/foaf/0.1/'
5
+
6
+ module Foaf
7
+ class Person
8
+ include RDF::NodeProxy
9
+
10
+ # Adds 1 year to the person's age
11
+ def happy_birthday!
12
+ foaf::age = (foaf::age.first.to_i + 1).to_s
13
+ end
14
+ end
15
+ end
4
16
 
5
- def setup
6
- Namespace :ex, 'http://www.example.com/ontology#'
7
- Namespace :foaf, 'http://xmlns.com/foaf/0.1/'
8
- end
9
-
17
+ module Foaf
18
+ class Thing
19
+ include RDF::NodeProxy
20
+ maps 'http://xmlns.com/foaf/0.1/Agent'
21
+ end
22
+ end
23
+
24
+ class TestLightRDF < Test::Unit::TestCase
10
25
  def test_equality
11
26
  assert Node('_:something') == Node(Node('_:something'))
12
27
  assert ID('_:2') == ID('_:2')
@@ -167,4 +182,39 @@ foaf: http://xmlns.com/foaf/0.1/
167
182
  # Check if the added context is there
168
183
  assert contexts.include?("http://test_repository_contexts.org")
169
184
  end
185
+
186
+ def test_instantiation
187
+ assert_equal Node("foaf:Person"), Foaf::Person.new.rdf::type.first
188
+ assert_equal Node("foaf:Agent"), Foaf::Thing.new.rdf::type.first
189
+
190
+ person_node = Node(nil)
191
+ person_node.foaf::age = "19"
192
+ person = Foaf::Person.new(person_node)
193
+ assert_equal ["19"], person.foaf::age
194
+
195
+ person = Foaf::Person.new('foaf:age'=>"25", 'rdf:type'=>Node('rdf:Resource'))
196
+ person.happy_birthday!
197
+ assert_equal ["26"], person.foaf::age
198
+ assert_equal Node("foaf:Person"), person.rdf::type.first
199
+ end
200
+
201
+ def test_graph_instantiation
202
+ graph = RDF::Graph.new
203
+ person_node = Node(nil)
204
+ person_node.rdf::type = Node('foaf:Person')
205
+ person_node.foaf::age = "25"
206
+ graph << person_node
207
+
208
+ person = graph.node(person_node)
209
+ person.happy_birthday!
210
+ assert_equal ["26"], person.foaf::age
211
+ end
212
+
213
+ def test_instance_equality
214
+ node = Node(nil)
215
+ node.foaf::age = "25"
216
+ person1 = Foaf::Person.new(node)
217
+ person2 = Foaf::Person.new(node)
218
+ assert_equal person1.id, person2.id
219
+ end
170
220
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 9
9
- version: 0.1.9
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jose Ignacio
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-18 00:00:00 +01:00
17
+ date: 2011-03-03 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -71,6 +71,7 @@ extra_rdoc_files:
71
71
  - lib/lightrdf.rb
72
72
  - lib/lightrdf/graph.rb
73
73
  - lib/lightrdf/node.rb
74
+ - lib/lightrdf/node_proxy.rb
74
75
  - lib/lightrdf/parser.rb
75
76
  - lib/lightrdf/repository.rb
76
77
  - lib/lightrdf/id.rb
@@ -83,6 +84,7 @@ files:
83
84
  - lib/lightrdf.rb
84
85
  - lib/lightrdf/graph.rb
85
86
  - lib/lightrdf/node.rb
87
+ - lib/lightrdf/node_proxy.rb
86
88
  - lib/lightrdf/parser.rb
87
89
  - lib/lightrdf/repository.rb
88
90
  - lib/lightrdf/id.rb