Skip to content

Commit

Permalink
Merge pull request #5 from ld4l/add_casting_restrictions
Browse files Browse the repository at this point in the history
Add casting restrictions; Let Annotation.resume receive same parameters as .new
  • Loading branch information
elrayle committed Jan 13, 2015
2 parents 8e5048b + a5ccca5 commit 86a06ab
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 41 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,43 @@ puts stb.dump :ttl
<http://example.org/term/engineering> a <http://www.w3.org/ns/oa#SemanticTag> .
```

*Resume annotation of unknown type.*
```
## Using RDF::URI
# Create the annotations first using previous examples.
a1 = LD4L::OpenAnnotationRDF::Annotation.resume(RDF::URI('http://localhost/c10'))
# => #<LD4L::OpenAnnotationRDF::CommentAnnotation:0x3fdd8267adc8(default)>
a2 = LD4L::OpenAnnotationRDF::Annotation.resume(RDF::URI('http://localhost/t10'))
# => #<LD4L::OpenAnnotationRDF::TagAnnotation:0x3fdd826073f0(default)>
a3 = LD4L::OpenAnnotationRDF::Annotation.resume(RDF::URI('http://localhost/st10'))
# => #<LD4L::OpenAnnotationRDF::SemanticTagAnnotation:0x3fdd8259c7a8(default)>
## Using string URI
# Create the annotations first using previous examples.
a1 = LD4L::OpenAnnotationRDF::Annotation.resume('http://localhost/c10')
# => #<LD4L::OpenAnnotationRDF::CommentAnnotation:0x3fdd8267adc8(default)>
a2 = LD4L::OpenAnnotationRDF::Annotation.resume('http://localhost/t10')
# => #<LD4L::OpenAnnotationRDF::TagAnnotation:0x3fdd826073f0(default)>
a3 = LD4L::OpenAnnotationRDF::Annotation.resume('http://localhost/st10')
# => #<LD4L::OpenAnnotationRDF::SemanticTagAnnotation:0x3fdd8259c7a8(default)>
## Using localname expanded using configured base_uri
# Create the annotations first using previous examples.
a1 = LD4L::OpenAnnotationRDF::Annotation.resume('c10')
# => #<LD4L::OpenAnnotationRDF::CommentAnnotation:0x3fdd8267adc8(default)>
a2 = LD4L::OpenAnnotationRDF::Annotation.resume('t10')
# => #<LD4L::OpenAnnotationRDF::TagAnnotation:0x3fdd826073f0(default)>
a3 = LD4L::OpenAnnotationRDF::Annotation.resume('st10')
# => #<LD4L::OpenAnnotationRDF::SemanticTagAnnotation:0x3fdd8259c7a8(default)>
```

### Configurations

Expand Down
27 changes: 12 additions & 15 deletions lib/ld4l/open_annotation_rdf/annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,31 @@ class << self; attr_reader :localname_prefix end
property :hasTarget, :predicate => RDFVocabularies::OA.hasTarget # :type => URI
property :hasBody, :predicate => RDFVocabularies::OA.hasBody
property :annotatedBy, :predicate => RDFVocabularies::OA.annotatedBy, :class_name => LD4L::FoafRDF::Person
property :annotatedAt, :predicate => RDFVocabularies::OA.annotatedAt # :type => xsd:dateTime # the time Annotation was created
property :motivatedBy, :predicate => RDFVocabularies::OA.motivatedBy # comes from RDFVocabularies::OA ontology
property :annotatedAt, :predicate => RDFVocabularies::OA.annotatedAt, :cast => false # :type => xsd:dateTime # the time Annotation was created
property :motivatedBy, :predicate => RDFVocabularies::OA.motivatedBy, :cast => false # comes from RDFVocabularies::OA ontology

def self.resume(*args)
return nil unless args.kind_of?(Array) && args.size > 0 && args.first.kind_of?(RDF::URI)

rdf_subject = args.first
a = new(rdf_subject)
def self.resume(uri_or_str)
# Let ActiveTriples::Resource validate uri_or_str when creating new Annotation
a = new(uri_or_str)

# get motivatedBy
m = a.get_values(:motivatedBy)
return a unless m.kind_of?(Array) && m.size > 0 && m.first.kind_of?(ActiveTriples::Resource)
return a unless m.kind_of?(Array) && m.size > 0 && m.first.kind_of?(RDF::Vocabulary::Term)

# motivatedBy is set
m_uri = m.first.rdf_subject

m_uri = m.first
# currently only support commenting and tagging
return LD4L::OpenAnnotationRDF::CommentAnnotation.new(rdf_subject) if m_uri == RDFVocabularies::OA.commenting
return LD4L::OpenAnnotationRDF::CommentAnnotation.new(uri_or_str) if m_uri == RDFVocabularies::OA.commenting
return a unless m_uri == RDFVocabularies::OA.tagging

# Tagging can be TagAnnotation or SemanticTagAnnotation. Only way to tell is by checking type of body.
sta = LD4L::OpenAnnotationRDF::SemanticTagAnnotation.new(rdf_subject)
sta = LD4L::OpenAnnotationRDF::SemanticTagAnnotation.new(uri_or_str)
stb = sta.getBody
return sta if stb.type.include?(RDFVocabularies::OA.SemanticTag)
return sta if stb.type.include?(RDFVocabularies::OA.SemanticTag)

ta = LD4L::OpenAnnotationRDF::TagAnnotation.new(rdf_subject)
ta = LD4L::OpenAnnotationRDF::TagAnnotation.new(uri_or_str)
tb = ta.getBody
return ta if tb.type.include?(RDFVocabularies::OA.Tag)
return ta if tb.type.include?(RDFVocabularies::OA.Tag)

# can't match to a known annotation type, so return as generic annotation
return a
Expand Down
4 changes: 2 additions & 2 deletions lib/ld4l/open_annotation_rdf/comment_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class << self; attr_reader :localname_prefix end
:base_uri => LD4L::OpenAnnotationRDF.configuration.base_uri,
:repository => :default

property :content, :predicate => RDFVocabularies::CNT.chars # :type => XSD.string
property :format, :predicate => RDF::DC.format # :type => XSD.string
property :content, :predicate => RDFVocabularies::CNT.chars, :cast => true # :type => XSD.string
property :format, :predicate => RDF::DC.format, :cast => true # :type => XSD.string

def initialize(*args)
super(*args)
Expand Down
2 changes: 1 addition & 1 deletion lib/ld4l/open_annotation_rdf/tag_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class << self; attr_reader :localname_prefix end
:base_uri => LD4L::OpenAnnotationRDF.configuration.base_uri,
:repository => :default

property :tag, :predicate => RDFVocabularies::CNT.chars # :type => XSD.string
property :tag, :predicate => RDFVocabularies::CNT.chars, :cast => false # :type => XSD.string

##
# Get a list of annotations using the tag value.
Expand Down
2 changes: 1 addition & 1 deletion lib/ld4l/open_annotation_rdf/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module LD4L
module OpenAnnotationRDF
VERSION = "0.0.7"
VERSION = "0.0.8"
end
end
14 changes: 7 additions & 7 deletions spec/ld4l/open_annotation_rdf/annotation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@

it "should be settable" do
subject.motivatedBy = RDFVocabularies::OA.describing
expect(subject.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.describing
expect(subject.motivatedBy.first).to eq RDFVocabularies::OA.describing
end

it "should be changeable" do
subject.motivatedBy = RDFVocabularies::OA.describing
subject.motivatedBy = RDFVocabularies::OA.classifying
expect(subject.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.classifying
expect(subject.motivatedBy.first).to eq RDFVocabularies::OA.classifying
end
end

Expand Down Expand Up @@ -189,7 +189,7 @@
expect(a.hasTarget.first.rdf_subject.to_s).to eq "http://example.org/bibref/br3"
expect(a.annotatedBy.first).to eq a_person
expect(a.annotatedAt.first).to eq a_time
expect(a.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.commenting
expect(a.motivatedBy.first).to eq RDFVocabularies::OA.commenting

b = a.getBody
expect(b).to be_a_kind_of(LD4L::OpenAnnotationRDF::CommentBody)
Expand All @@ -215,7 +215,7 @@
expect(a.hasTarget.first.rdf_subject.to_s).to eq "http://example.org/bibref/br3"
expect(a.annotatedBy.first).to eq a_person
expect(a.annotatedAt.first).to eq a_time
expect(a.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.tagging
expect(a.motivatedBy.first).to eq RDFVocabularies::OA.tagging

b = a.getBody
expect(b).to be_a_kind_of(LD4L::OpenAnnotationRDF::TagBody)
Expand All @@ -240,7 +240,7 @@
expect(a.hasTarget.first.rdf_subject.to_s).to eq "http://example.org/bibref/br3"
expect(a.annotatedBy.first).to eq a_person
expect(a.annotatedAt.first).to eq a_time
expect(a.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.tagging
expect(a.motivatedBy.first).to eq RDFVocabularies::OA.tagging

b = a.getBody
expect(b).to be_a_kind_of(LD4L::OpenAnnotationRDF::SemanticTagBody)
Expand Down Expand Up @@ -293,7 +293,7 @@
end

it "should reset the motivatedBy" do
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
end

it "should be persisted" do
Expand Down Expand Up @@ -325,7 +325,7 @@

it "should delete from the repository" do
subject.reload
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
subject.motivatedBy = []
expect(subject.motivatedBy).to eq []
subject.persist!
Expand Down
10 changes: 5 additions & 5 deletions spec/ld4l/open_annotation_rdf/comment_annotation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,18 @@

describe 'motivatedBy' do
it "should be OA.commenting if we haven't set it" do
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting
end

it "should be settable" do
subject.motivatedBy = RDFVocabularies::OA.describing
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.describing
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.describing
end

it "should be changeable" do
subject.motivatedBy = RDFVocabularies::OA.describing
subject.motivatedBy = RDFVocabularies::OA.classifying
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.classifying
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.classifying
end
end

Expand Down Expand Up @@ -220,7 +220,7 @@
end

it "should reset the motivatedBy" do
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
end

it "should be persisted" do
Expand Down Expand Up @@ -252,7 +252,7 @@

it "should delete from the repository" do
subject.reload
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
subject.motivatedBy = []
expect(subject.motivatedBy).to eq []
subject.persist!
Expand Down
10 changes: 5 additions & 5 deletions spec/ld4l/open_annotation_rdf/semantic_tag_annotation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,18 @@

describe 'motivatedBy' do
it "should be OA.tagging if we haven't set it" do
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.tagging
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.tagging
end

it "should be settable" do
subject.motivatedBy = RDFVocabularies::OA.describing
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.describing
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.describing
end

it "should be changeable" do
subject.motivatedBy = RDFVocabularies::OA.describing
subject.motivatedBy = RDFVocabularies::OA.classifying
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.classifying
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.classifying
end
end

Expand Down Expand Up @@ -278,7 +278,7 @@
end

it "should reset the motivatedBy" do
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
end

it "should be persisted" do
Expand Down Expand Up @@ -310,7 +310,7 @@

it "should delete from the repository" do
subject.reload
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
subject.motivatedBy = []
expect(subject.motivatedBy).to eq []
subject.persist!
Expand Down
10 changes: 5 additions & 5 deletions spec/ld4l/open_annotation_rdf/tag_annotation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,18 @@

describe 'motivatedBy' do
it "should be OA.tagging if we haven't set it" do
expect(subject.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.tagging
expect(subject.motivatedBy.first).to eq RDFVocabularies::OA.tagging
end

it "should be settable" do
subject.motivatedBy = RDFVocabularies::OA.describing
expect(subject.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.describing
expect(subject.motivatedBy.first).to eq RDFVocabularies::OA.describing
end

it "should be changeable" do
subject.motivatedBy = RDFVocabularies::OA.describing
subject.motivatedBy = RDFVocabularies::OA.classifying
expect(subject.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.classifying
expect(subject.motivatedBy.first).to eq RDFVocabularies::OA.classifying
end
end

Expand Down Expand Up @@ -330,7 +330,7 @@
end

it "should reset the motivatedBy" do
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
end

it "should be persisted" do
Expand Down Expand Up @@ -362,7 +362,7 @@

it "should delete from the repository" do
subject.reload
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
subject.motivatedBy = []
expect(subject.motivatedBy).to eq []
subject.persist!
Expand Down

0 comments on commit 86a06ab

Please sign in to comment.