Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Fixes up README.md for code correctness. Closes #209.
  • Loading branch information
Tom Johnson committed Aug 9, 2016
1 parent 3e11e8b commit 3922cd4
Showing 1 changed file with 72 additions and 39 deletions.
111 changes: 72 additions & 39 deletions README.md
Expand Up @@ -23,36 +23,47 @@ The core module of `ActiveTriples` is `ActiveTriples::RDFSource`. You can use th


```ruby
require 'rdf/vocab'

class Thing
include ActiveTriples::RDFSource
configure :type => RDF::OWL.Thing, :base_uri => 'http://example.org/things#'
property :title, :predicate => RDF::DC.title
property :description, :predicate => RDF::DC.description

configure type: RDF::OWL.Thing, base_uri: 'http://example.org/things#'

property :title, predicate: RDF::Vocab::DC.title
property :description, predicate: RDF::Vocab::DC.description
end

obj = Thing.new('123')
obj.title = 'Resource'
obj = Thing.new('123')
obj.title = 'Resource'
obj.description = 'A resource.'

obj.dump :ntriples # => "<http://example.org/things#123> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .\n<http://example.org/things#123> <http://purl.org/dc/terms/title> \"Resource\" .\n<http://example.org/things#123> <http://purl.org/dc/terms/description> \"A resource.\" .\n"
```

URI and bnode values are built out as generic Resources when accessed. A more specific model class can be configured on individual properties.

```ruby
Thing.property :creator, :predicate => RDF::DC.creator, :class_name => 'Person'
Thing.property :creator, predicate: RDF::Vocab::DC.creator, class_name: 'Person'

class Person
include ActiveTriples::RDFSource
configure :type => RDF::FOAF.Person, :base_uri => 'http://example.org/people#'
property :name, :predicate => RDF::FOAF.name

configure type: RDF::Vocab::FOAF.Person,
base_uri: 'http://example.org/people#'

property :name, predicate: RDF::Vocab::FOAF.name
end

obj_2 = Thing.new('2')
obj_2 = Thing.new('2')
obj_2.creator = Person.new

obj_2.creator
# => [#<Person:0x3fbe84ac9234(default)>]

obj_2.creator.first.name = 'Herman Melville'
obj_2.dump :ntriples # => "<http://example.org/things#2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .\n<http://example.org/things#2> <http://purl.org/dc/terms/creator> _:g70263220218800 .\n_:g70263220218800 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .\n_:g70263220218800 <http://xmlns.com/foaf/0.1/name> \"Herman Melville\" .\n"

obj_2.dump :ntriples # => "_:g47361345336040 <http://xmlns.com/foaf/0.1/name> \"Herman Melville\" .\n_:g47361345336040 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .\n<http://example.org/things#2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .\n<http://example.org/things#2> <http://purl.org/dc/terms/creator> _:g47361345336040 .\n"
```

Open Model
Expand All @@ -63,23 +74,33 @@ An RDFSource lets you handle data as a graph, independent of whether it is defin
```ruby
related = Thing.new

related << RDF::Statement(related, RDF::DC.relation, obj)
related << RDF::Statement(related, RDF::DC.subject, 'ActiveTriples')
related << RDF::Statement(related, RDF::Vocab::DC.relation, obj)
related << RDF::Statement(related, RDF::Vocab::DC.subject, 'ActiveTriples')

related.query(:subject => related, :predicate => RDF::DC.relation).each_statement {|s,p,o| puts o}
# => http://example.org/things#123
related.query(:subject => related, :predicate => RDF::DC.subject).each_statement {|s,p,o| puts o}
related.query(subject: related,
predicate: RDF::Vocab::DC.relation).each_statement do |s,p,o|
puts o
end
# => http://example.org/things#123

related.query(subject: related,
predicate: RDF::Vocab::DC.subject).each_statement do |s,p,o|
puts o
end
# => 'ActiveTriples'
```

Any operation you can run against an RDF::Graph works with RDFSources, too. Or you can use generic setters and getters with URI predicates:

```ruby
related.set_value(RDF::DC.relation, obj)
related.set_value(RDF::DC.subject, 'ActiveTriples')
related.set_value(RDF::Vocab::DC.relation, obj)
related.set_value(RDF::Vocab::DC.subject, 'ActiveTriples')

related.get_values(RDF::Vocab::DC.relation)
# => [#<Thing:0x3f949c6a2294(default)>]

related.get_values(RDF::DC.relation) # => [#<Thing:0x3f949c6a2294(default)>]
related.get_values(RDF::DC.subject) # => ["ActiveTriples"]
related.get_values(RDF::Vocab::DC.subject)
# => ["ActiveTriples"]
```

Some convienience methods provide support for handling data from web sources:
Expand All @@ -89,10 +110,13 @@ Some convienience methods provide support for handling data from web sources:
```ruby
require 'linkeddata' # to support various serializations

osu = ActiveTriples::Resource.new 'http://dbpedia.org/resource/Oregon_State_University'
uri = 'http://dbpedia.org/resource/Oregon_State_University'

osu = ActiveTriples::Resource.new uri
osu.fetch

osu.rdf_label # => ["Oregon State University", "Oregon State University", "Université d'État de l'Oregon", "Oregon State University", "Oregon State University", "オレゴン州立大学", "Universidad Estatal de Oregón", "Oregon State University", "俄勒岡州立大學", "Universidade do Estado do Oregon"]
osu.rdf_label
# => ["Oregon State University", "Oregon State University", "Université d'État de l'Oregon", "Oregon State University", "Oregon State University", "オレゴン州立大学", "Universidad Estatal de Oregón", "Oregon State University", "俄勒岡州立大學", "Universidade do Estado do Oregon"]
```

Typed Data
Expand All @@ -101,9 +125,9 @@ Typed Data
Typed literals are handled natively through Ruby types and [RDF::Literal](https://github.com/ruby-rdf/rdf/tree/develop/lib/rdf/model/literal). There is no need to register a specific type for a property, simply pass the setter the appropriate typed data. See the examples in the RDF::Literal documentation for futher information about supported datatypes.

```ruby
Thing.property :date, :predicate => RDF::DC.date
Thing.property :date, predicate: RDF::Vocab::DC.date

my_thing = Thing.new
my_thing = Thing.new
my_thing.date = Date.today

puts my_thing.dump :ntriples
Expand Down Expand Up @@ -143,38 +167,47 @@ Resources can persist to various databases and triplestores though integration w
# RDF::Repository support persistence to (e.g.) triplestores & NoSQL
# databases.
ActiveTriples::Repositories.add_repository :default, RDF::Repository.new
ActiveTriples::Repositories.add_repository :people, RDF::Repository.new
ActiveTriples::Repositories.add_repository :people, RDF::Repository.new

class Person
include ActiveTriples::RDFSource
configure :type => RDF::FOAF.Person, :base_uri => 'http://example.org/people#', :repository => :people
property :name, :predicate => RDF::FOAF.name

configure type: RDF::Vocab::FOAF.Person,
base_uri: 'http://example.org/people#',
repository: :people
property :name, predicate: RDF::Vocab::FOAF.name
end

class Thing
include ActiveTriples::RDFSource

configure :type => RDF::OWL.Thing, :base_uri => 'http://example.org/things#', :repository => :default
property :title, :predicate => RDF::DC.title
property :description, :predicate => RDF::DC.description
property :creator, :predicate => RDF::DC.creator, :class_name => 'Person'
configure type: RDF::OWL.Thing,
base_uri: 'http://example.org/things#',
repository: :default

property :title, predicate: RDF::Vocab::DC.title
property :description, predicate: RDF::Vocab::DC.description
property :creator, predicate: RDF::Vocab::DC.creator, class_name: 'Person'
end

t = Thing.new('1')
t.title = 'A Thing'
t = Thing.new('1')
t.title = 'A Thing'
t.creator = Person.new('1')

t.persisted? # => false
t.creator.first.name = 'Tove'
t.persist!

ActiveTriples::Repositories.repositories[:default].dump :ntriples
# => "<http://example.org/things#1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .\n<http://example.org/things#1> <http://purl.org/dc/terms/title> \"A Thing\" .\n<http://example.org/things#1> <http://purl.org/dc/terms/creator> <http://example.org/people#1> .\n"
# => ""

t.creator.first.persisted? # => false
t.creator.first.persist!
t.creator.first.name = 'Tove'
t.persist!

ActiveTriples::Repositories.repositories[:people].dump :ntriples
# => "<http://example.org/people#1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .\n<http://example.org/people#1> <http://xmlns.com/foaf/0.1/name> \"Tove\" .\n"
puts ActiveTriples::Repositories.repositories[:default].dump :ntriples
# <http://example.org/things#1> <http://purl.org/dc/terms/title> "A Thing" .
# <http://example.org/things#1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .
# <http://example.org/things#1> <http://purl.org/dc/terms/creator> <http://example.org/people#1> .
# <http://example.org/people#1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
# <http://example.org/people#1> <http://xmlns.com/foaf/0.1/name> "Tove" .
```

Contributing
Expand Down

0 comments on commit 3922cd4

Please sign in to comment.