pius-dm-semantic 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,82 @@
1
+ DM-Semantic Release 0.0.2 (January 8th 2009)
2
+ ===================================
3
+
4
+ **Git**: [http://github.com/pius/dm-semantic](http://github.com/pius/dm-semantic)
5
+ **Author**: Pius Uzamere, The Uyiosa Corporation
6
+
7
+ **Copyright**: 2009
8
+
9
+
10
+ SYNOPSIS
11
+ --------
12
+
13
+ DM-Semantic is a plugin for DataMapper that will allow you to seamlessly export your data models as OWL ontologies and expose your data as RDF.
14
+
15
+
16
+ FEATURE LIST
17
+ ------------
18
+
19
+ 1. **Import and Export RDF Data Directly From Your Models**: DM-Semantic adds a special DataMapper Type called RDFGraph. You can add properties with this type to your DataMapper models and transparently load and dump RDF triples to your models (in Ntriples form.) Let your data do more for you by manipulating it as RDF so that you can mash it up with other data sources and inference across the data.
20
+
21
+ USAGE
22
+ -----
23
+
24
+ 1. **Install the Gem**
25
+
26
+ Make sure you've upgraded to at least RubyGems 1.2. Then, if you've never installed a gem from GitHub before then do this:
27
+
28
+ > gem sources -a http://gems.github.com (you only have to do this once)
29
+
30
+ Then:
31
+
32
+ > sudo gem install pius-dm-semantic
33
+
34
+ 2. **Make Sure You've Got the Dependencies installed**
35
+
36
+ DM-Semantic depends on Reddy (http://github.com/tommorris/reddy).
37
+
38
+ > sudo gem install reddy
39
+
40
+ 3. **Require the gem and include it in your DataMapper models**
41
+
42
+ To use the special RDFGraph type, create a property with the type in your model. For example:
43
+
44
+ > property :graph, RDFGraph
45
+
46
+ 4. Now, you can use RDF in your model. For example:
47
+
48
+ >> c = Concept.new(:slug => "pius", :graph => "<http://pius.github.com#me> <http://xmlns.com/foaf/0.1/name> \"Pius Uzamere\" .
49
+ <http://pius.github.com#me> <http://xmlns.com/foaf/0.1/homepage> \"Pius Uzamere\" .")
50
+ => #<Concept id=nil slug="pius" graph="<http://pius.github.com#me> <http://xmlns.com/foaf/0.1/name> \"Pius Uzamere\" . \n<http://pius.github.com#me> <http://xmlns.com/foaf/0.1/homepage> \"Pius Uzamere\" .">
51
+ >> c.save
52
+ ~ (0.000087) SELECT "id", "slug" FROM "concepts" WHERE ("slug" = 'pius') ORDER BY "id", "slug" LIMIT 1
53
+ ~ (0.002364) INSERT INTO "concepts" ("graph", "slug") VALUES ('<http://pius.github.com#me> <http://xmlns.com/foaf/0.1/name> "Pius Uzamere" .
54
+ <http://pius.github.com#me> <http://xmlns.com/foaf/0.1/homepage> "Pius Uzamere" .', 'pius')
55
+ => true
56
+ >> exit
57
+
58
+ Then, later:
59
+
60
+ >> c = Concept.first
61
+ ~ (0.000086) SELECT "id", "slug" FROM "concepts" ORDER BY "id", "slug" LIMIT 1
62
+ => #<Concept id=4 slug="pius" graph=<not loaded>>
63
+ >> c.graph
64
+ ~ (0.000084) SELECT "graph", "id", "slug" FROM "concepts" WHERE ("id" = 4) AND ("slug" = 'pius') ORDER BY "id", "slug"
65
+ => #<Reddy::Graph:0x2742a3c @nsbinding={}, @triples=[[#<Reddy::URIRef:0x274262c @uri=#<Addressable::URI:0x13a0d1c URI:http://pius.github.com#me>, #<Reddy::URIRef:0x27415b0 @uri=#<Addressable::URI:0x13a03da URI:http://xmlns.com/foaf/0.1/name>, #<Reddy::Literal:0x27404f8 @encoding=<theReddy::TypeLiteral::Encoding::Null>, contents"Pius Uzamere"], [#<Reddy::URIRef:0x2742690 @uri=#<Addressable::URI:0x13a0024 URI:http://pius.github.com#me>, #<Reddy::URIRef:0x273fc10 @uri=#<Addressable::URI:0x139fc5a URI:http://xmlns.com/foaf/0.1/homepage>, #<Reddy::Literal:0x273f670 @encoding=<theReddy::TypeLiteral::Encoding::Null>, contents"Pius Uzamere"]]
66
+ >>
67
+
68
+
69
+ 4. **Read the documentation**
70
+
71
+ It's YARD. It's sexy.
72
+
73
+ 5. **Contribute!**
74
+
75
+ Fork my repository (http://github.com/pius/dm-semantic), make some changes, and send along a pull request!
76
+
77
+
78
+ COPYRIGHT
79
+ ---------
80
+
81
+ DM-Semantic was created in 2008 by Pius Uzamere (pius -AT- alum -DOT- mit -DOT- edu) and is
82
+ licensed under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'rake/clean'
4
+ require 'spec/rake/spectask'
5
+ require 'pathname'
6
+
7
+ task :default => [ :spec ]
8
+
9
+ desc 'Run specifications'
10
+ Spec::Rake::SpecTask.new(:spec) do |t|
11
+ t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
12
+ t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
13
+
14
+ begin
15
+ t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
16
+ t.rcov_opts << '--exclude' << 'spec'
17
+ t.rcov_opts << '--text-summary'
18
+ t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
19
+ rescue Exception
20
+ # rcov not installed
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "dm-semantic"
3
+ s.version = "0.0.2"
4
+ s.date = "2009-01-08"
5
+ s.summary = "DM-Semantic is a DataMapper plugin for adding semantic capabilities to models."
6
+ s.email = "pius+github@alum.mit.edu"
7
+ s.homepage = "http://github.com/pius/dm-semantic"
8
+ s.description = "DM-Semantic is a DataMapper plugin for adding semantic capabilities to models."
9
+ s.has_rdoc = true
10
+ s.authors = ["Pius Uzamere"]
11
+ s.files = ["README.markdown", "Rakefile", "dm-semantic.gemspec", "lib/dm-semantic.rb", "lib/dm-semantic/functions.rb", "lib/dm-semantic/model.rb", "lib/dm-semantic/types.rb"]
12
+ s.test_files = ["spec/spec_helper.rb"]
13
+ #s.rdoc_options = ["--main", "README.txt"]
14
+ #s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
15
+ s.add_dependency("dm-types", ["> 0.9.6"])
16
+ s.add_dependency("reddy", ["> 0.0.1"])
17
+ end
@@ -0,0 +1,26 @@
1
+ require 'pathname'
2
+ require 'rubygems'
3
+
4
+ gem 'dm-core', '~>0.9.8'
5
+ require 'dm-core'
6
+
7
+ gem 'reddy'
8
+ require 'reddy'
9
+
10
+ class Pathname
11
+ def /(path)
12
+ (self + path).expand_path
13
+ end
14
+ end # class Pathname
15
+
16
+ dir = Pathname(__FILE__).dirname.expand_path / 'dm-semantic'
17
+
18
+ require dir / 'functions'
19
+ require dir / 'model'
20
+
21
+ module DataMapper
22
+ module Types
23
+ dir = (Pathname(__FILE__).dirname.expand_path / 'dm-semantic/types' ).to_s
24
+ autoload :RDFGraph, dir / 'rdf_graph'
25
+ end
26
+ end
@@ -0,0 +1,173 @@
1
+ module DataMapper
2
+ module Semantic
3
+ module ClassMethods
4
+
5
+ # property :id, Serial
6
+ # property :login, String, :nullable => false, :length => 3..40, :unique => true
7
+ # property :email, String, :nullable => false, :unique => true
8
+ # property :created_at, DateTime
9
+ # property :updated_at, DateTime
10
+ # property :activated_at, DateTime
11
+ # property :activation_code, String
12
+ # property :crypted_password, String
13
+ # property :salt, String
14
+ # property :remember_token_expires_at, DateTime
15
+ # property :remember_token, String
16
+ # property :password_reset_key, String, :writer => :protected
17
+
18
+ # Triple.new(:User, is_a, FOAF:Person)
19
+ # Triple.new(:id, rdf:datatype, :integer)
20
+ # Triple.new(:login, rdf:datatype, :string)
21
+
22
+ ##
23
+ # Returns a hash of the ontologies to which the model is mapped, along with their authoritative URIs.
24
+ #
25
+ # ==== Example
26
+ # Person.ontologies # returns {:foaf => "http://xmlns.com/foaf/spec/"}
27
+ # Person.ontologies(:rdf) # returns {:foaf => "http://xmlns.com/foaf/spec/index.rdf"}
28
+ #
29
+ # @param [Symbol] format format of the ontology to which the URI points (:rdf, :turtle, or :json). Defaults to :rdf
30
+ #
31
+ # ==== Returns
32
+ # @return [String] the OWL ontology in the requested format.
33
+ #
34
+ # @author Pius Uzamere
35
+ #---
36
+ # @public
37
+
38
+ def ontologies(format = :rdf)
39
+ raise "Not Yet Implemented"
40
+ end
41
+
42
+ ##
43
+ # Export the entire class definition as an OWL ontology.
44
+ #
45
+ # ==== Example
46
+ # Person.to_owl # returns string representation of the OWL ontology in Turtle.
47
+ # Person.to_owl(:json) # returns string representation of the OWL ontology in RDF-JSON.
48
+ #
49
+ # @param [Symbol] format format of the OWL-RDF output (:xml, :turtle, or :json). Defaults to :turtle
50
+ # ==== Rules for creating the ontology
51
+ # The class itself will be represented by a b-node.
52
+ #
53
+ # ==== Returns
54
+ # @return [String] the OWL ontology in the requested format.
55
+ #
56
+ # @author Pius Uzamere
57
+ #---
58
+ # @public
59
+
60
+ def to_owl(*args)
61
+ g = Graph.new
62
+ owl = Namespace.new('http://www.w3.org/2002/07/owl', 'owl', true)
63
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
64
+ rdf = Namespace.new("http://www.w3.org/1999/02/22-rdf-syntax-ns", "rdf", true)
65
+ rdfs = Namespace.new("http://www.w3.org/2000/01/rdf-schema", 'rdfs', true)
66
+ xsd = Namespace.new('http://www.w3.org/2001/XMLSchema', 'xsd', true)
67
+
68
+ for property in properties
69
+ g << Triple.new(BNode.new('john'), foaf.knows, BNode.new('jane'))
70
+ end
71
+ return g
72
+ end
73
+
74
+ ##
75
+ # Create the basic OWL triples for the class, without the properties.
76
+ #
77
+ # ==== Example
78
+ # Person.triple_for_class_definition # returns a graph of the triples representing the bare class in OWL.
79
+ #
80
+ # ==== Rules for creating the ontology
81
+ # The class itself will be represented by a b-node.
82
+ #
83
+ # ==== Returns
84
+ # @return [Graph] graph of the OWL triples for the bare class.
85
+ #
86
+ # @author Pius Uzamere
87
+ #---
88
+ # @public
89
+
90
+ def triples_for_class_definition
91
+ declare_namespaces
92
+ g = Graph.new
93
+ b = BNode.new(self.name)
94
+ g << Triple.new(b, URIRef.new('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), URIRef.new('http://www.w3.org/2002/07/owl#Class'))
95
+ return g
96
+ end
97
+
98
+ ##
99
+ # Create the OWL triples for a DataMapper property.
100
+ #
101
+ # ==== Example
102
+ # Person.triples_for_property(Person.properties[:id]) # returns a graph of the triples representing the id property on Person in OWL.
103
+ #
104
+ #
105
+ # ==== Returns
106
+ # @return [Graph] graph of the OWL triples for the property.
107
+ #
108
+ # @author Pius Uzamere
109
+ #---
110
+ # @public
111
+
112
+ def triples_for_property(property)
113
+ g = Graph.new
114
+ b = BNode.new(property.field)
115
+ t = Triple.new(b, URIRef.new('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), URIRef.new('http://www.w3.org/2002/07/owl#DatatypeProperty'))
116
+ g << t
117
+ return g
118
+ end
119
+
120
+ protected
121
+
122
+ ##
123
+ # Utility method to declare a bunch of useful namespaces.
124
+ #
125
+ # ==== Example
126
+ # Person.declare_namespaces # instantiates some useful namespaces.
127
+ #
128
+ # @author Pius Uzamere
129
+ #---
130
+ # @protected
131
+
132
+ def declare_namespaces
133
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
134
+ rdf = Namespace.new("http://www.w3.org/1999/02/22-rdf-syntax-ns", "rdf", true)
135
+ rdfs = Namespace.new("http://www.w3.org/2000/01/rdf-schema", 'rdfs', true)
136
+ xsd = Namespace.new('http://www.w3.org/2001/XMLSchema', 'xsd', true)
137
+ owl = Namespace.new('http://www.w3.org/2002/07/owl', 'owl', true)
138
+ end
139
+
140
+ end
141
+
142
+
143
+ module InstanceMethods
144
+
145
+ ##
146
+ # Export a representation of the instance in RDF.
147
+ #
148
+ # ==== Example
149
+ # Friend.first.to_rdf # returns the first Friend model instance in RDF using its ontology
150
+ #
151
+ # @param [Symbol] format format of the OWL-RDF output (:xml, :turtle, or :json). Defaults to :turtle
152
+ #
153
+ # ==== Returns
154
+ # @return [String] the RDF representation in the requested serialization format.
155
+ #
156
+ # @author Pius Uzamere
157
+ #---
158
+ # @public
159
+
160
+ def to_rdf(format = :xml)
161
+ raise "not yet implemented"
162
+ end
163
+ end
164
+
165
+ private
166
+
167
+ def self.included(receiver)
168
+ receiver.extend(ClassMethods)
169
+ receiver.send :include, InstanceMethods
170
+ end
171
+
172
+ end
173
+ end
@@ -0,0 +1,9 @@
1
+ module DataMapper
2
+ module Model
3
+ include DataMapper::Semantic
4
+ #
5
+ # private
6
+ #
7
+ # sample function here
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ require 'pathname'
3
+
4
+ gem 'dm-core', '=0.9.3'
5
+ require 'dm-core'
6
+
7
+ spec_dir_path = Pathname(__FILE__).dirname.expand_path
8
+ require spec_dir_path.parent + 'lib/dm-semantic'
9
+
10
+ def load_driver(name, default_uri)
11
+ return false if ENV['ADAPTER'] != name.to_s
12
+
13
+ lib = "do_#{name}"
14
+
15
+ begin
16
+ gem lib, '=0.9.3'
17
+ require lib
18
+ DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
19
+ DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
20
+ true
21
+ rescue Gem::LoadError => e
22
+ warn "Could not load #{lib}: #{e}"
23
+ false
24
+ end
25
+ end
26
+
27
+ ENV['ADAPTER'] ||= 'sqlite3'
28
+
29
+ HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
30
+ HAS_MYSQL = load_driver(:mysql, 'mysql://localhost/dm_core_test')
31
+ HAS_POSTGRES = load_driver(:postgres, 'postgres://postgres@localhost/dm_core_test')
32
+
33
+
34
+ # require fixture resources
35
+ Dir[spec_dir_path + "fixtures/*.rb"].each do |fixture_file|
36
+ require fixture_file
37
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pius-dm-semantic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Pius Uzamere
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-08 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dm-types
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">"
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.6
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: reddy
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">"
30
+ - !ruby/object:Gem::Version
31
+ version: 0.0.1
32
+ version:
33
+ description: DM-Semantic is a DataMapper plugin for adding semantic capabilities to models.
34
+ email: pius+github@alum.mit.edu
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - README.markdown
43
+ - Rakefile
44
+ - dm-semantic.gemspec
45
+ - lib/dm-semantic.rb
46
+ - lib/dm-semantic/functions.rb
47
+ - lib/dm-semantic/model.rb
48
+ - lib/dm-semantic/types.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/pius/dm-semantic
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: DM-Semantic is a DataMapper plugin for adding semantic capabilities to models.
75
+ test_files:
76
+ - spec/spec_helper.rb