Skip to content

Commit

Permalink
Use local name minter; Test localname_prefix; Add config examples
Browse files Browse the repository at this point in the history
* get active triples from new ActiveTriples repository in github
* add active_triples-local_name gem; add configuration for a local name minter function
* change id_prefix to localname_prefix to be more accurately descriptive
* write tests for localname_prefix
* add configuration examples to README
  • Loading branch information
elrayle committed Nov 26, 2014
1 parent 4280986 commit a3883b5
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 36 deletions.
7 changes: 4 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source 'https://rubygems.org'

gemspec

# GETTING FROM GEMFILE UNTIL MintLocalName CODE IS PUSHED INTO MASTER, THEN MOVE THIS BACK TO *.gemspec FILE
# Use active-triple for handling of triple persistence
gem "active-triples", :git => '[email protected]:no-reply/ActiveTriples.git', :branch => 'generate_id'
# GETTING FROM GEMFILE UNTIL ActiveTriples master CODE IS RELEASED (>0.4.0), THEN MOVE THIS BACK TO *.gemspec FILE
# Use active-triples for handling of triple persistence
gem 'active-triples', :git => '[email protected]:ActiveTriples/ActiveTriples.git', :branch => 'master'

112 changes: 86 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,128 @@

LD4L FOAF RDF provides tools for modeling person triples based on the FOAF ontology and persisting to a triplestore.


## Installation

Temporary get the gem from github until the gem is released publicly.

Add this line to your application's Gemfile:

gem 'ld4l-foaf_rdf'
<!-- gem 'ld4l-foaf_rdf' -->
gem 'ld4l-foaf_rdf', '~> 0.0.3', :git => '[email protected]:ld4l/foaf_rdf.git'


And then execute:

$ bundle install

<!--
Or install it yourself as:
$ gem install ld4l-foaf_rdf
-->


## Usage

**Caveat:** This gem is part of the LD4L Project and is being used in that context. There is no guarantee that the
code will work in a usable way outside of its use in LD4L Use Cases.

### Models
### Examples

The LD4L::FoafRDF gem provides model definitions using the
[ActiveTriples](https://github.com/no-reply/ActiveTriples) framework extension of
[ruby-rdf/rdf](https://github.com/ruby-rdf/rdf). The following models are provided:
Setup required for all examples.
```
require 'ld4l/foaf_rdf'
1. LD4L::FoafRDF::Person - Implements the Person class in the FOAF ontology.
# create an in-memory repository
ActiveTriples::Repositories.add_repository :default, RDF::Repository.new
```

### Ontologies
Example creating a person.
```
p = LD4L::FoafRDF::Person.new('p4')
The listed ontologies are used to represent the primary metadata about the person.
Other ontologies may also be used that aren't listed.

* [FOAF](http://xmlns.com/foaf/spec/)
* [RDF](http://www.w3.org/TR/rdf-syntax-grammar/)
puts p.dump :ttl
```

Example triples created for a person.
```
<http://localhost/p4> a <http://xmlns.com/foaf/0.1/Person> .
```


### Creating a Person
### Configurations

Start console in a terminal.
* base_uri - base URI used when new resources are created (default="http://localhost/")
* localname_minter - minter function to use for creating unique local names (default=nil which uses default minter in active_triples-local_name gem)

*Setup for all examples.*

* Restart your interactive session (e.g. irb, pry).
* Use the Setup for all examples in main Examples section above.

*Example usage using configured base_uri and default localname_minter.*
```
bundle console
LD4L::FoafRDF.configure do |config|
config.base_uri = "http://example.org/"
end
p = LD4L::FoafRDF::Person.new(ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::FoafRDF::Person, 10, {:prefix=>'p'} ))
puts p.dump :ttl
```
NOTE: If base_uri is not used, you need to restart your interactive environment (e.g. irb, pry). Once the
Person class is instantiated the first time, the base_uri for the class is set. If you ran
through the main Examples, the base_uri was set to the default base_uri.

In console, you can test creating a person.

*Example triples created for a person with configured base_uri and default minter.*
```
<http://example.org/p45c9c85b-25af-4c52-96a4-cf0d8b70a768> a <http://xmlns.com/foaf/0.1/Person> .
```
# Create an in-memory repository
ActiveTriples::Repositories.add_repository :default, RDF::Repository.new

# Configure a base_uri for all person objects
*Example usage using configured base_uri and configured localname_minter.*
```
LD4L::FoafRDF.configure do |config|
config.base_uri = "http://example.org/individual/"
config.base_uri = "http://example.org/"
config.localname_minter = lambda { |prefix=""| prefix+'_configured_'+SecureRandom.uuid }
end
# Create a person
p = LD4L::FoafRDF::Person.new('1')
p.rdf_subject
# => http://example.org/individual/pr1
p = LD4L::FoafRDF::Person.new(ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::FoafRDF::Person, 10, 'p',
&LD4L::FoafRDF.configuration.localname_minter ))
# View triples as tutle
puts p.dump :ttl
# => <http://example.org/individual/p1> a <http://xmlns.com/foaf/0.1/Person> .
```
NOTE: If base_uri is not used, you need to restart your interactive environment (e.g. irb, pry). Once the
Person class is instantiated the first time, the base_uri for the class is set. If you ran
through the main Examples, the base_uri was set to the default base_uri.


*Example triples created for a person with configured base_uri and configured minter.*
```
<http://example.org/p_configured_6498ba05-8b21-4e8c-b9d4-a6d5d2180966> a <http://xmlns.com/foaf/0.1/Person> .
```


### Models

The LD4L::FoafRDF gem provides model definitions using the
[ActiveTriples](https://github.com/ActiveTriples/ActiveTriples) framework extension of
[ruby-rdf/rdf](https://github.com/ruby-rdf/rdf). The following models are provided:

1. LD4L::FoafRDF::Person - Implements the Person class in the FOAF ontology. (extremely simple for holding rdf_subject only)


### Ontologies

The listed ontologies are used to represent the primary metadata about the person.
Other ontologies may also be used that aren't listed.

* [FOAF](http://xmlns.com/foaf/spec/)
* [RDF](http://www.w3.org/TR/rdf-syntax-grammar/)


## Contributing

Expand Down
3 changes: 3 additions & 0 deletions ld4l-foaf_rdf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Gem::Specification.new do |spec|
# GETTING FROM GEMFILE UNTIL MintLocalName CODE IS PUSHED INTO MASTER
# spec.add_dependency('active-triples', '~> 0.2')

spec.add_dependency('active_triples-local_name', '~> 0.1')

spec.add_development_dependency('pry')
spec.add_development_dependency('rdoc')
spec.add_development_dependency('rspec')
spec.add_development_dependency('guard-rspec')
Expand Down
2 changes: 2 additions & 0 deletions lib/ld4l/foaf_rdf.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'rdf'
require 'active_triples'
require 'active_triples/local_name'
require 'linkeddata'
require 'ld4l/foaf_rdf/version'


module LD4L
module FoafRDF

Expand Down
18 changes: 17 additions & 1 deletion lib/ld4l/foaf_rdf/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ module FoafRDF
class Configuration

attr_reader :base_uri
attr_reader :localname_minter

def self.default_base_uri
@default_base_uri = "http://localhost/".freeze
end
private_class_method :default_base_uri

def self.default_localname_minter
# by setting to nil, it will use the default minter in the minter gem
@default_localname_minter = nil
end
private_class_method :default_localname_minter

def initialize
@base_uri = self.class.send(:default_base_uri)
@base_uri = self.class.send(:default_base_uri)
@localname_minter = self.class.send(:default_localname_minter)
end

def base_uri=(new_base_uri)
Expand All @@ -20,6 +28,14 @@ def base_uri=(new_base_uri)
def reset_base_uri
@base_uri = self.class.send(:default_base_uri)
end

def localname_minter=(new_minter)
@localname_minter = new_minter
end

def reset_localname_minter
@localname_minter = self.class.send(:default_localname_minter)
end
end
end
end
3 changes: 2 additions & 1 deletion lib/ld4l/foaf_rdf/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module LD4L
module FoafRDF
class Person < ActiveTriples::Resource

@local_name_prefix="p"
class << self; attr_reader :localname_prefix end
@localname_prefix="p"

configure :type => RDF::FOAF.Person, :base_uri => LD4L::FoafRDF.configuration.base_uri, :repository => :default
end
Expand Down
66 changes: 66 additions & 0 deletions spec/ld4l/foaf_rdf/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,52 @@ class DummyPerson < LD4L::FoafRDF::Person
expect(LD4L::FoafRDF.configuration.base_uri).to eq "http://localhost/"
end
end

describe "localname_minter" do
context "when minter is nil" do
before do
class DummyPerson < LD4L::FoafRDF::Person
configure :type => RDF::FOAF.Person, :base_uri => LD4L::FoafRDF.configuration.base_uri, :repository => :default
end
end
after do
Object.send(:remove_const, "DummyPerson") if Object
end
it "should use default minter in minter gem" do
localname = ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::FoafRDF::Person, 10, {:prefix=>'default_'},
LD4L::FoafRDF.configuration.localname_minter )
expect(localname).to be_kind_of String
expect(localname.size).to eq 44
expect(localname).to match /default_[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/
end
end

context "when minter is configured" do
before do
LD4L::FoafRDF.configure do |config|
config.localname_minter = lambda { |prefix=""| prefix+'_configured_'+SecureRandom.uuid }
end
class DummyPerson < LD4L::FoafRDF::Person
configure :type => RDF::FOAF.Person, :base_uri => LD4L::FoafRDF.configuration.base_uri, :repository => :default
end
end
after do
Object.send(:remove_const, "DummyPerson") if Object
LD4L::FoafRDF.reset
end

it "should generate an Person URI using the configured localname_minter" do
localname = ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::FoafRDF::Person, 10,
LD4L::FoafRDF::Person.localname_prefix,
&LD4L::FoafRDF.configuration.localname_minter )
expect(localname).to be_kind_of String
expect(localname.size).to eq 49
expect(localname).to match /p_configured_[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/
end
end
end
end


Expand All @@ -104,5 +150,25 @@ class DummyPerson < LD4L::FoafRDF::Person
expect(config.base_uri).to eq "http://localhost/"
end
end

describe "#localname_minter" do
it "should default to nil" do
expect(LD4L::FoafRDF::Configuration.new.localname_minter).to eq nil
end

it "should be settable" do
config = LD4L::FoafRDF::Configuration.new
config.localname_minter = lambda { |prefix=""| prefix+'_configured_'+SecureRandom.uuid }
expect(config.localname_minter).to be_kind_of Proc
end

it "should be re-settable" do
config = LD4L::FoafRDF::Configuration.new
config.localname_minter = lambda { |prefix=""| prefix+'_configured_'+SecureRandom.uuid }
expect(config.localname_minter).to be_kind_of Proc
config.reset_localname_minter
expect(config.localname_minter).to eq nil
end
end
end
end
15 changes: 11 additions & 4 deletions spec/ld4l/foaf_rdf/person_rdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
end
end

describe '#localname_prefix' do
it "should return default prefix" do
prefix = LD4L::FoafRDF::Person.localname_prefix
expect(prefix).to eq "p"
end
end

# -----------------------------------------------
# END -- Test attributes specific to this model
# -----------------------------------------------
Expand Down Expand Up @@ -185,7 +192,7 @@
before do
class DummyPerson < ActiveTriples::Resource
configure :type => RDF::URI('http://example.org/Person')
property :name, :predicate => RDF::FOAF.name
property :foafname, :predicate => RDF::FOAF.name
property :publications, :predicate => RDF::FOAF.publications, :class_name => 'DummyDocument'
property :knows, :predicate => RDF::FOAF.knows, :class_name => DummyPerson
end
Expand Down Expand Up @@ -215,13 +222,13 @@ class DummyDocument < ActiveTriples::Resource

let (:person1) do
p = DummyPerson.new
p.name = 'Alice'
p.foafname = 'Alice'
p
end

let (:person2) do
p = DummyPerson.new
p.name = 'Bob'
p.foafname = 'Bob'
p
end

Expand Down Expand Up @@ -250,7 +257,7 @@ class DummyDocument < ActiveTriples::Resource
document2.creator = person1
person1.knows = person2
subject.item = [document1]
expect(subject.item.first.creator.first.knows.first.name).to eq ['Bob']
expect(subject.item.first.creator.first.knows.first.foafname).to eq ['Bob']
end
end
end
Loading

0 comments on commit a3883b5

Please sign in to comment.