activesesame 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,107 @@
1
+ module ActiveSesame
2
+ module Testing
3
+ require 'rest-open-uri'
4
+ require 'uri'
5
+
6
+ @@repository_uri = "http://localhost:8112/sesame/repositories"
7
+ @@triple_store_id = "go-for-ryan"
8
+ @@location = @@repository_uri + "/" + @@triple_store_id
9
+ @@prefixes = ["PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>",
10
+ "PREFIX ss: <http://study-stash.radiology.umm.edu/ontologies/study-stash.owl#>",
11
+ "PREFIX owl: <http://www.w3.org/2002/07/owl#>",
12
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>",
13
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"]
14
+
15
+ def self.find_by_sparql(query, include_prefixes=true)
16
+ #puts self.base_uri + " " + @@prefixes.join(" ") + " " + query
17
+ rest_call("", :method => :get, :body => {:infer => "false", :queryLn => "SPARQL", :query => @@base + " " + @@prefixes.join(" ") + " " + query})
18
+ end
19
+
20
+ def self.group_save(xml)
21
+ xml = "<transaction>#{xml}</transaction>"
22
+ #puts xml
23
+ rest_call("statements", {:method => :post, "content-type" => "application/x-rdftransaction", :body => xml})
24
+ end
25
+
26
+ def self.save_triple(subject, predicate, object)
27
+ rest_call("statements", {:method => :put, :body => {:subj => subject, :pred => predicate, :obj => object}})
28
+ end
29
+
30
+ def self.delete_by_pattern(subject, predicate, object)
31
+ rest_call("statements", {:method => :delete, :body => {:subj => subject, :pred => predicate, :obj => object}})
32
+ end
33
+
34
+ def self.triple_store_id
35
+ @@triple_store_id
36
+ end
37
+
38
+ def self.set_triple_store_id(new_id)
39
+ @@triple_store_id = new_id.to_s
40
+ end
41
+
42
+ def self.repository_uri
43
+ @@repository_uri
44
+ end
45
+
46
+ def self.set_repository_uri(new_repository)
47
+ @@repository_uri = new_repository
48
+ end
49
+
50
+ def self.prefixes
51
+ @@prefixes
52
+ end
53
+
54
+ def self.add_prefix(prefix)
55
+ @@prefixes << prefix
56
+ end
57
+
58
+ def self.base_uri
59
+ uri_with_quacks = @@base.split(" ").last
60
+ uri_with_quacks.slice(1,uri_with_quacks.length-2)
61
+ end
62
+
63
+ # def self.base_uri
64
+ # uri_with_quacks = @@base.split(" ").last
65
+ # uri_with_quacks.slice(1,uri_with_quacks.length-2)
66
+ # end
67
+
68
+ def self.set_base_uri(base_uri)
69
+ @@base = "BASE <#{base_uri}>"
70
+ end
71
+
72
+ def self.simple_rest_methods(*method_names)
73
+ method_names.each do |name|
74
+ new_name = "self.#{name.to_s}"
75
+ define_method(new_name) { return rest_call(name) }
76
+ end
77
+ end
78
+
79
+ self.simple_rest_methods :size, :contexts, :namespaces
80
+ self.set_base_uri "http://www.geneontology.org/formats/oboInOwl"
81
+
82
+ def self.rest_call(method_name, args={})
83
+ args[:body][:query] = encode_sparql(args[:body][:query]) if args[:body][:query] if args[:body]
84
+ args[:body][:subj] = encode_sparql(args[:body][:subj]) if args[:body][:subj] if args[:body]
85
+ args[:body][:pred] = encode_sparql(args[:body][:pred]) if args[:body][:pred] if args[:body]
86
+ args[:body][:obj] = encode_sparql(args[:body][:obj]) if args[:body][:obj] if args[:body]
87
+ [:get, :put, :delete].include?(args[:method]) ? vars_if_get = hash_to_get(args[:body]) : vars_if_get = ""
88
+ method_name == "" ? slash = "" : slash = "/"
89
+ puts @@location + slash + method_name.to_s + vars_if_get
90
+ puts self.repository_uri + "/" + self.triple_store_id + slash + method_name.to_s + vars_if_get
91
+ return open(self.repository_uri + "/" + self.triple_store_id + slash + method_name.to_s + vars_if_get, args).read
92
+ end
93
+
94
+ def self.encode_sparql(query)
95
+ URI.encode(query).gsub("?","%3f").gsub("/","%2f").gsub(":","%3a").gsub("\\","5C")
96
+ end
97
+
98
+ def self.hash_to_get(hash)
99
+ (hash.inject("?") {|total,tuple| total += "#{tuple[0]}=#{tuple[1]}&"}).chop
100
+ end
101
+
102
+
103
+ end
104
+
105
+
106
+
107
+ end
@@ -0,0 +1,28 @@
1
+ module ActiveSesame::TransactionBuilder
2
+
3
+ def self.object_to_triples(object)
4
+ base_class = object.class.base_uri_location.gsub(/<|>/,"") + object.class.rdf_class
5
+ full_transaction = self.build_triple(object.instance, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", base_class)
6
+ object.class.simple_attributes.keys.inject(full_transaction) do |transaction,attribute|
7
+ transaction += self.build_triple(object.instance, attribute, object.send(Support.uri_to_sym(attribute)))
8
+ end
9
+ end
10
+
11
+ def self.build_triples(list_of_triples, trans_type='add')
12
+ xml_for_transation = list_of_triples.inject("<transaction>") do |xml, triple|
13
+ xml += "<#{trans_type}>" + type_cast(triple[:subject]) + type_cast(triple[:predicate]) + type_cast(triple[:object]) + "</#{trans_type}>"
14
+ xml
15
+ end
16
+ xml_for_transation += "</transaction>"
17
+ end
18
+
19
+ def self.type_cast(uri_or_literal)
20
+ if uri_or_literal =~ /http:\/\//
21
+ "<uri>#{uri_or_literal}</uri>"
22
+ else
23
+ "<literal datatype=\"#{ActiveSesame::RDFConstants.class_to_literal[uri_or_literal.class]}\">#{uri_or_literal}</literal>"
24
+ end
25
+ end
26
+
27
+ end
28
+
@@ -0,0 +1,9 @@
1
+ module ActiveSesame #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'active_sesame', 'active_sesame.rb')
@@ -0,0 +1,131 @@
1
+ require File.join(File.dirname(__FILE__), "..","lib","activesesame.rb")
2
+ require File.join(File.dirname(__FILE__), 'spec_helpers')
3
+
4
+ describe ActiveSesame do
5
+
6
+ it "should load all it's libraries" do
7
+ ActiveSesame.const_defined?("Base").should be_true
8
+ ActiveSesame.const_defined?("Repository").should be_true
9
+ ActiveSesame.const_defined?("ResultParser").should be_true
10
+ ActiveSesame.const_defined?("TransactionBuilder").should be_true
11
+ ActiveSesame.const_defined?("RDFConstants").should be_true
12
+ ActiveSesame.const_defined?("MonkeyPatches").should be_true
13
+ ActiveSesame.const_defined?("Support").should be_true
14
+ ActiveSesame.const_defined?("Ontology").should be_true
15
+ ActiveSesame.const_defined?("Behaviors").should be_true
16
+ end
17
+
18
+ describe ActiveSesame::Behaviors do
19
+ describe ActiveSesame::Behaviors::Ontology do
20
+ before(:all) do
21
+ @repo = SpecHelpers.repository
22
+ SpecHelpers.populate_triple_store
23
+ end
24
+
25
+ after(:all) do
26
+ SpecHelpers.clear_triple_store
27
+ end
28
+
29
+ before(:each) do
30
+ class TermTest
31
+ attr_accessor :name
32
+ ActiveSesame::Behaviors::Ontology.mimic(self, :repository => SpecHelpers.repository)
33
+ end
34
+ @term = TermTest.new
35
+ @term.name = "http://www.fakeontology.org/ontology.owl#Enders_Game"
36
+ end
37
+
38
+ it "should bootstrap the class" do
39
+ @term.respond_to?(:ontology).should be_true
40
+ @term.ontology.term.should be_equal(@term.send(@term.class.ontology_attribute))
41
+ end
42
+
43
+ it "should have relationships" do
44
+ @term.ontology.relationships.keys.size.should_not be_equal(0)
45
+ end
46
+
47
+ it "'s relationships should include an ontology term" do
48
+ @term.ontology.base_author.class.should be_equal(ActiveSesame::Ontology::Term)
49
+ end
50
+
51
+ end
52
+
53
+ it "should make a sparql query" do
54
+ #ActiveSesame::Behaviors::FuzzyOntology.include_fuzzy_ontology(self, :bogus).should_not be_equal(nil)
55
+ end
56
+ end
57
+
58
+ describe ActiveSesame::ResultParser do
59
+ end
60
+
61
+
62
+ describe ActiveSesame::Base do
63
+ end
64
+ end
65
+
66
+ describe ActiveSesame::Repository do
67
+ before do
68
+ @repo = SpecHelpers.repository
69
+ end
70
+
71
+ it "should add triples to the store" do
72
+ @repo.group_save(ActiveSesame::TransactionBuilder.build_triples(SpecHelpers.triples_to_add, "add"))
73
+ @repo.size.to_i.should_not be_equal(0)
74
+ end
75
+
76
+ it "should remove triples from the store" do
77
+ @repo.group_save(ActiveSesame::TransactionBuilder.build_triples(SpecHelpers.triples_to_add, "remove"))
78
+ @repo.size.to_i.should be_equal(0)
79
+ end
80
+
81
+ it "should be able to connect to the test triple store" do
82
+ results = open("http://localhost:8111/sesame/repositories", :method => :get).read
83
+ results.include?("http://localhost:8111/sesame/repositories/test").should be_true
84
+ end
85
+ end
86
+
87
+ describe ActiveSesame::OwlThing do
88
+ before do
89
+ class Book < ActiveSesame::OwlThing
90
+ end
91
+ end
92
+
93
+ it "should have a term" do
94
+ ActiveSesame::OwlThing.term.term
95
+ end
96
+
97
+ it "should create a new thing with a generated uri" do
98
+ thing = ActiveSesame::OwlThing.new()
99
+ thing.term.term.should =~ /#{ActiveSesame::OwlThing.repository.base_uri}\#.*/
100
+ end
101
+
102
+ it "should build a class with the correct relationships" do
103
+ class Author < ActiveSesame::OwlThing
104
+ end
105
+ Author.term.class.should be_equal(ActiveSesame::Ontology::Term)
106
+ Author.term.unsaved_triples.first[:predicate].should == "http://www.w3.org/2000/01/rdf-schema#subClassOf"
107
+ end
108
+
109
+ it "should pass unknown methods to term" do
110
+ Book.respond_to?(:unsaved_triples).should be_equal(false)
111
+ Book.unsaved_triples.class.should be_equal(Array)
112
+ end
113
+
114
+ it "should have the correct uri and repository when setters are called" do
115
+ class Author < ActiveSesame::OwlThing
116
+ set_repository SpecHelpers.repository
117
+ end
118
+ Author.repository.base_uri.should == SpecHelpers.repository.base_uri
119
+ Author.unsaved_triples.first[:subject].should == "http://www.fakeontology.org/ontology.owl#Author"
120
+ Author.set_uri("http://bogus.org/ontology.owl#JimBob")
121
+ Author.repository.base_uri.should == SpecHelpers.repository.base_uri
122
+ Author.unsaved_triples.first[:subject].should == "http://bogus.org/ontology.owl#JimBob"
123
+ Author.term.term.should == "http://bogus.org/ontology.owl#JimBob"
124
+ end
125
+
126
+ it "should create a new instance with the correct rdf_type" do
127
+ nb = Book.new
128
+ nb.unsaved_triples.first[:predicate].should == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
129
+ nb.unsaved_triples.first[:object].should == Book.term.term
130
+ end
131
+ end
@@ -0,0 +1,49 @@
1
+ module SpecHelpers
2
+ def self.repository
3
+ ActiveSesame::Repository.new(ActiveSesame::SesameProtocol, {
4
+ :repository_uri => "http://localhost:8111/sesame/repositories",
5
+ :triple_store_id => "test",
6
+ :location => "http://localhost:8111/sesame/repositories/test",
7
+ :query_language => "SPARQL",
8
+ :base_uri => "http://www.fakeontology.org/ontology.owl"
9
+ })
10
+ end
11
+
12
+ def self.populate_triple_store
13
+ self.repository.group_save(ActiveSesame::TransactionBuilder.build_triples(SpecHelpers.triples_to_add, "add"))
14
+ end
15
+
16
+ def self.clear_triple_store
17
+ self.repository.group_save(ActiveSesame::TransactionBuilder.build_triples(SpecHelpers.triples_to_add, "remove"))
18
+ end
19
+
20
+ def self.triples_to_add
21
+ [
22
+ {:subject => "base:Book", :predicate => "rdfs:subClassOf", :object => "owl:Thing"},
23
+ {:subject => "base:Enders_Game", :predicate => "rdfs:subClassOf", :object => "base:Book"},
24
+ {:subject => "base:Enders_Game", :predicate => "base:author", :object => "base:Orson_Scott_Card"},
25
+ {:subject => "base:Enders_Game", :predicate => "base:title", :object => "Ender's Game"},
26
+ {:subject => "base:Speaker_For_The_Dead", :predicate => "base:title", :object => "Speaker for the Dead"},
27
+ {:subject => "base:Speaker_For_The_Dead", :predicate => "base:author", :object => "base:Orson_Scott_Card"},
28
+ {:subject => "base:Speaker_For_The_Dead", :predicate => "base:title", :object => "base:Book"},
29
+ {:subject => "base:Christmas_Carol", :predicate => "base:title", :object => "A Christmas Carol"},
30
+ {:subject => "base:Christmas_Carol", :predicate => "base:author", :object => "base:Charles_Dickens"},
31
+ {:subject => "base:Christmas_Carol", :predicate => "rdfs:subClassOf", :object => "base:Book"}
32
+ ].collect {|triple| triple.keys.inject(Hash.new) {|hash,key| hash[key] = expand_term(triple[key]); hash } }
33
+ end
34
+
35
+ def self.expand_term(full_term)
36
+ prefix, term = full_term.split(":")
37
+ term != nil ? expand_prefix(prefix) + "#" + term : prefix
38
+ end
39
+
40
+ def self.expand_prefix(prefix)
41
+ {
42
+ :xsd => "http://www.w3.org/2001/XMLSchema",
43
+ :owl => "http://www.w3.org/2002/07/owl",
44
+ :rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns",
45
+ :rdfs => "http://www.w3.org/2000/01/rdf-schema",
46
+ :base => "http://www.fakeontology.org/ontology.owl"
47
+ }[prefix.to_sym]
48
+ end
49
+ end
File without changes
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activesesame
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Max Warnock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-04 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Ruby Gem for interacting with RDF/OWL stored in the AllegroGraph Triple Store via Seasame 2 HTTP protocol. Go to http://wiki.github.com/mwarnock/activesesame
17
+ email: circuitshaman@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.txt
26
+ - CHANGELOG.txt
27
+ - Rakefile
28
+ - init.rb
29
+ - install.rb
30
+ - ./lib/activesesame.rb
31
+ - ./lib/active_sesame/active_sesame.rb
32
+ - ./lib/active_sesame/base.rb
33
+ - ./lib/active_sesame/behaviors.rb
34
+ - ./lib/active_sesame/general.rb
35
+ - ./lib/active_sesame/monkey_patches.rb
36
+ - ./lib/active_sesame/ontology.rb
37
+ - ./lib/active_sesame/owl_thing.rb
38
+ - ./lib/active_sesame/rdf_class.rb
39
+ - ./lib/active_sesame/rdf_constants.rb
40
+ - ./lib/active_sesame/rdf_property.rb
41
+ - ./lib/active_sesame/repository.rb
42
+ - ./lib/active_sesame/result_parser.rb
43
+ - ./lib/active_sesame/support.rb
44
+ - ./lib/active_sesame/testing.rb
45
+ - ./lib/active_sesame/transaction_builder.rb
46
+ - ./lib/active_sesame/version.rb
47
+ - ./spec/spec_active_sesame.rb
48
+ - ./spec/spec_helpers.rb
49
+ - ./tasks/default.rake
50
+ has_rdoc: true
51
+ homepage: http://github.com/mwarnock/activesesame
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.5
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: A Ruby Gem for interacting with RDF/OWL stored in the AllegroGraph Triple Store via Seasame 2 HTTP protocol
78
+ test_files: []
79
+