acts_as_rdf 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/acts_as_rdf.rb +69 -49
  3. data/test/test_to_rdf.rb +7 -0
  4. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
data/lib/acts_as_rdf.rb CHANGED
@@ -12,12 +12,9 @@ module ActsAsRdf
12
12
  module ClassMethods
13
13
 
14
14
  def acts_as_rdf(options = {})
15
- configuration = { :prefixes => {
16
- 'rdf' => RDF,
17
- 'rdfs' => RDF::RDFS,
18
- 'xsd' => 'http://www.w3.org/2001/XMLSchema#',
19
- 'owl' => RDF::OWL
20
- }
15
+ configuration = {
16
+ :prefixes => {'rdf' => RDF, 'rdfs' => RDF::RDFS, 'xsd' => 'http://www.w3.org/2001/XMLSchema#', 'owl' => RDF::OWL},
17
+ :ontology_name => 'ontology'
21
18
  }
22
19
  configuration.update(options) if options.is_a?(Hash)
23
20
 
@@ -32,6 +29,13 @@ module ActsAsRdf
32
29
  '#{configuration[:prefixes]}'
33
30
  end
34
31
 
32
+ def get_ontology_name
33
+ '#{configuration[:ontology_name]}'
34
+ end
35
+
36
+ def self.to_rdfs(request = nil, format = :rdfxml)
37
+ new.to_rdfs(request, format)
38
+ end
35
39
  EOV
36
40
  end
37
41
  end
@@ -39,77 +43,93 @@ module ActsAsRdf
39
43
  # All the methods available to a record that has had <tt>acts_as_rdf</tt> specified.
40
44
  module InstanceMethods
41
45
 
42
- # current tested formats are rdfxml, turtle, ntriples
46
+ # format is for active_rdf library
43
47
  def to_rdf(request = nil, format = :rdfxml)
44
- graph = to_graph(request)
45
- return to_output(graph, request, format)
46
- end
47
-
48
- protected
49
-
50
- def to_graph(request, graph = RDF::Graph.new)
48
+ base_uri = RDF::URI.new(get_base_uri(request))
51
49
 
52
- if 'Array'.eql?(self.class.name) then
53
- self.each do |element|
54
- graph = self.to_graph(request, graph)
55
- end
56
- else
57
- graph << [to_uri(self, request), RDF.type, to_uri(self.class, request)]
50
+ RDF::Writer.for(format).buffer(:base_uri => base_uri, :default_namespace => base_uri, :prefixes => get_prefixes()) do |writer|
51
+ writer << [to_uri(self, base_uri), RDF.type, to_ontology_uri(self, base_uri)]
58
52
 
59
53
  self.attributes.each do |attr, value|
60
- graph << [to_uri(self, request), to_uri(attr, request), RDF::Literal.new(value)]
54
+ writer << [to_uri(self, base_uri), to_uri(attr, base_uri), RDF::Literal.new(value)]
61
55
  if ('name'.eql?(attr)) then
62
- graph << [to_uri(self, request), RDF::RDFS.label, RDF::Literal.new(value)]
56
+ writer << [to_uri(self, base_uri), RDF::RDFS.label, RDF::Literal.new(value)]
63
57
  end
64
58
  end
65
59
 
66
- socs = self.class.reflect_on_all_associations
67
- if (socs != nil) then
68
- socs.each do |soc|
69
- if self.respond_to? soc.name then
70
- association_objects = self.send soc.name # needs to handle plural case
71
- if association_objects != nil then
72
- if 'Array'.eql?(association_objects.class.name) then
73
- association_objects.each do |obj|
74
- graph << [to_uri(self, request), to_uri(soc.class_name, request), to_uri(obj, request)]
75
- end
76
- else
77
- graph << [to_uri(self, request), to_uri(soc.class_name, request), to_uri(association_objects, request)]
60
+ self.class.reflect_on_all_associations.each do |soc|
61
+ if self.respond_to? soc.name
62
+ association_objects = self.send soc.name
63
+ if association_objects != nil
64
+ if 'Array'.eql?(association_objects.class.name)
65
+ association_objects.each do |obj|
66
+ writer << [to_uri(self, base_uri), to_uri(soc.class_name, base_uri), to_uri(obj, base_uri)]
78
67
  end
68
+ else
69
+ writer << [to_uri(self, base_uri), to_uri(soc.class_name, base_uri), to_uri(association_objects, base_uri)]
79
70
  end
80
71
  end
81
72
  end
82
73
  end
83
74
  end
84
-
85
- return graph
86
75
  end
87
76
 
88
- def to_output(graph, request, format)
77
+ # this will return the rdfs schema for this model in owl.
78
+ def to_rdfs(request = nil, format = :rdfxml)
89
79
  base_uri = RDF::URI.new(get_base_uri(request))
90
80
 
91
- return RDF::Writer.for(format).buffer(:base_uri => base_uri, :default_namespace => base_uri, :prefixes => get_prefixes()) { |writer|
92
- graph.each_statement do |statement|
93
- writer << statement
81
+ RDF::Writer.for(format).buffer(:base_uri => base_uri, :default_namespace => base_uri, :prefixes => get_prefixes()) do |writer|
82
+ writer << [to_ontology_uri(self, base_uri), RDF::RDFS.label, RDF::Literal.new(self.class.name)]
83
+ writer << [to_uri(self, base_uri), RDF.type, RDF::OWL['Class']]
84
+
85
+ self.attributes.each do |attr, value|
86
+ writer << [to_uri(attr, base_uri), RDF.type, RDF::OWL['DatatypeProperty']]
87
+ writer << [to_uri(attr, base_uri), RDF::RDFS.label, RDF::Literal.new(attr)]
88
+ writer << [to_uri(attr, base_uri), RDF::RDFS.domain, to_ontology_uri(attr, base_uri)]
89
+ obj = RDF::Literal.new(value)
90
+ if obj.has_datatype? then
91
+ writer << [to_uri(attr, base_uri), RDF::RDFS.range, RDF::Literal.new(value).datatype]
92
+ end
94
93
  end
95
- }
94
+ socs = self.class.reflect_on_all_associations
95
+ socs.each do |soc|
96
+ writer << [to_uri(soc.class_name, base_uri), RDF.type, RDF::OWL['ObjectProperty']]
97
+ writer << [to_uri(soc.class_name, base_uri), RDF::RDFS.label, RDF::Literal.new(soc.class_name)]
98
+ writer << [to_uri(soc.class_name, base_uri), RDF::RDFS.domain, to_ontology_uri(soc, base_uri)]
99
+
100
+ # ranges
101
+ if self.respond_to? soc.name
102
+ association_objects = self.send soc.name # needs to handle plural case
103
+ if !association_objects.nil?
104
+ association_objects = association_objects[0]
105
+ end
106
+ if !association_objects.nil?
107
+ writer << [to_ontology_uri(soc.class_name, base_uri), RDF::RDFS.range, to_ontology_uri(association_objects, base_uri)]
108
+ end
109
+ end
110
+ end
111
+ end
96
112
  end
97
113
 
98
- def to_uri(thing, request)
99
- if (thing.class.name != nil)
100
- if ('String'.eql?(thing.class.name) || 'Class'.eql?(thing.class.name)) then
101
- return RDF::URI.new("#{get_base_uri(request)}/#{thing.to_s}")
102
- end
103
- return RDF::URI.new("#{get_base_uri(request)}/#{thing.class.name.tableize}/#{thing.id}")
114
+ protected
115
+
116
+ def to_uri(thing, base_uri)
117
+ if ('String'.eql?(thing.class.name) || 'Class'.eql?(thing.class.name))
118
+ RDF::URI.new("#{base_uri}/#{thing.to_s}")
119
+ else
120
+ RDF::URI.new("#{base_uri}/#{thing.class.name.tableize}/#{thing.id}")
104
121
  end
122
+ end
105
123
 
106
- return RDF::URI.new("#{get_base_uri(request)}##{thing.to_s}")
124
+ def to_ontology_uri(thing, base_uri)
125
+ RDF::URI.new("#{base_uri}/#{get_ontology_name()}/##{thing.class.name}")
107
126
  end
108
127
 
109
128
  def get_base_uri(request)
110
- request.nil? ? "http://locahost" : "#{request.protocol}://#{request.host}:#{request.port}"
129
+ request.nil? ? "http://localhost" : "#{request.protocol}://#{request.host}:#{request.port}"
111
130
  end
112
131
  end
132
+
113
133
  end
114
134
 
115
135
  ActiveRecord::Base.class_eval { include ActsAsRdf }
data/test/test_to_rdf.rb CHANGED
@@ -12,6 +12,13 @@ class RdfTest < Test::Unit::TestCase
12
12
  teardown_db
13
13
  end
14
14
 
15
+ def test_to_rdfs
16
+ puts '***** Mixin'
17
+ puts Mixin.to_rdfs
18
+ puts '***** MixinRelation'
19
+ puts MixinRelation.to_rdfs
20
+ end
21
+
15
22
  def test_to_rdf
16
23
  formats = [:rdfxml, :turtle, :ntriples]
17
24
  formats.each do |format|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_rdf
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 4
10
- version: 0.1.4
9
+ - 5
10
+ version: 0.1.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Sodt
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-28 00:00:00 -07:00
18
+ date: 2010-11-04 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency