Skip to content

Commit

Permalink
Merge pull request #7 from ld4l/upgrade_0.8
Browse files Browse the repository at this point in the history
Upgrade to support ActiveTriples 0.5, 0.6, and 0.8.2
  • Loading branch information
elrayle committed Apr 14, 2016
2 parents c5d6676 + 3259950 commit 0f5c1c7
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 184 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
source 'https://rubygems.org'

gemspec

10 changes: 3 additions & 7 deletions ld4l-open_annotation_rdf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ Gem::Specification.new do |spec|

spec.files = `git ls-files -z`.split("\x0")
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
# spec.test_files = `git ls-files -- {spec}/*`.split("\n") # FROM ActiveTriples gemspec file
# spec.require_paths = ["lib"] # NOT IN ActiveTriples gemspec file

# spec.add_dependency('ffi', '~> 1.9.5')
spec.add_dependency('rdf', '~> 1.1')

spec.add_dependency('active-triples', '~> 0.5')
spec.add_dependency('active_triples-local_name', '~> 0.1')
spec.add_dependency('active-triples', '0.6.1')
spec.add_dependency('active_triples-local_name')
spec.add_dependency('ld4l-foaf_rdf', '~> 0.0')

spec.add_development_dependency('pry')
# spec.add_development_dependency('pry-byebug') # Works with ruby > 2
# spec.add_development_dependency('pry-debugger') # Works with ruby < 2
spec.add_development_dependency('pry-byebug')
spec.add_development_dependency('rdoc')
spec.add_development_dependency('rspec')
spec.add_development_dependency('coveralls')
Expand Down
4 changes: 3 additions & 1 deletion lib/ld4l/open_annotation_rdf/annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def self.resume(uri_or_str)

# get motivatedBy
m = a.get_values(:motivatedBy)
m = m.to_a if Object.const_defined?("ActiveTriples::Relation") && m.kind_of?(ActiveTriples::Relation)

# TODO: Should m's class be validated? I've seen it be RDF::Vocabulary::Term and RDF::URI. For now, removing the validation.
return a unless m.kind_of?(Array) && m.size > 0
# return a unless m.kind_of?(Array) && m.size > 0 && (m.first.kind_of?(RDF::Vocabulary::Term) || m.first.kind_of?(RDF::URI)
Expand All @@ -36,7 +38,7 @@ def self.resume(uri_or_str)
# Tagging can be TagAnnotation or SemanticTagAnnotation. Only way to tell is by checking type of body.
sta = LD4L::OpenAnnotationRDF::SemanticTagAnnotation.new(uri_or_str)
stb = sta.getBody
return sta if stb.type.include?(RDFVocabularies::OA.SemanticTag)
return sta unless stb.type.include?(RDFVocabularies::OA.Tag) || !stb.type.include?(RDFVocabularies::OA.SemanticTag)

ta = LD4L::OpenAnnotationRDF::TagAnnotation.new(uri_or_str)
tb = ta.getBody
Expand Down
21 changes: 17 additions & 4 deletions lib/ld4l/open_annotation_rdf/comment_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class CommentAnnotation < LD4L::OpenAnnotationRDF::Annotation

@localname_prefix="ca"

configure :type => RDFVocabularies::OA.Annotation,
:base_uri => LD4L::OpenAnnotationRDF.configuration.base_uri,
:repository => :default

property :hasBody, :predicate => RDFVocabularies::OA.hasBody, :class_name => LD4L::OpenAnnotationRDF::CommentBody


Expand All @@ -14,10 +18,18 @@ class CommentAnnotation < LD4L::OpenAnnotationRDF::Annotation
#
# @return instance of CommentAnnotation
def setComment(comment)
@body = LD4L::OpenAnnotationRDF::CommentBody.new(
ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::OpenAnnotationRDF::CommentBody, 10, @localname_prefix,
LD4L::OpenAnnotationRDF.configuration.localname_minter ))
@body ||= hasBody.first if hasBody.size > 0
if self.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
@body ||= LD4L::OpenAnnotationRDF::CommentBody.new(
ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::OpenAnnotationRDF::CommentBody, 10, @localname_prefix,
LD4L::OpenAnnotationRDF.configuration.localname_minter),self)
else # < ActiveTriples 0.8
@body ||= LD4L::OpenAnnotationRDF::CommentBody.new(
ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::OpenAnnotationRDF::CommentBody, 10, @localname_prefix,
LD4L::OpenAnnotationRDF.configuration.localname_minter))
end
@body.content = comment
@body.format = "text/plain"
set_value(:hasBody, @body)
Expand All @@ -41,6 +53,7 @@ def initialize(*args)

# set motivatedBy
m = get_values(:motivatedBy)
m = m.to_a if Object.const_defined?("ActiveTriples::Relation") && m.kind_of?(ActiveTriples::Relation)
set_value(:motivatedBy, RDFVocabularies::OA.commenting) unless m.kind_of?(Array) && m.size > 0

# resume CommentBody if it exists
Expand Down
11 changes: 10 additions & 1 deletion lib/ld4l/open_annotation_rdf/semantic_tag_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class SemanticTagAnnotation < LD4L::OpenAnnotationRDF::Annotation

@localname_prefix = "sta"

configure :type => RDFVocabularies::OA.Annotation,
:base_uri => LD4L::OpenAnnotationRDF.configuration.base_uri,
:repository => :default

property :hasBody, :predicate => RDFVocabularies::OA.hasBody, :class_name => LD4L::OpenAnnotationRDF::SemanticTagBody


Expand Down Expand Up @@ -38,7 +42,11 @@ def setTerm(term_uri)
term_uri = RDF::URI(term_uri) unless term_uri.kind_of?(RDF::URI)
return @body if old_term_uri && old_term_uri == term_uri.to_s

@body = LD4L::OpenAnnotationRDF::SemanticTagBody.new(term_uri)
if self.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
@body = LD4L::OpenAnnotationRDF::SemanticTagBody.new(term_uri,self)
else # < ActiveTriples 0.8
@body = LD4L::OpenAnnotationRDF::SemanticTagBody.new(term_uri)
end
set_value(:hasBody, @body)
@body
end
Expand All @@ -51,6 +59,7 @@ def initialize(*args)

# set motivatedBy
m = get_values(:motivatedBy)
m = m.to_a if Object.const_defined?("ActiveTriples::Relation") && m.kind_of?(ActiveTriples::Relation)
set_value(:motivatedBy, RDFVocabularies::OA.tagging) unless m.kind_of?(Array) && m.size > 0

# resume SemanticTagBody if it exists
Expand Down
37 changes: 30 additions & 7 deletions lib/ld4l/open_annotation_rdf/tag_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class TagAnnotation < LD4L::OpenAnnotationRDF::Annotation

@localname_prefix="ta"

configure :type => RDFVocabularies::OA.Annotation,
:base_uri => LD4L::OpenAnnotationRDF.configuration.base_uri,
:repository => :default

property :hasBody, :predicate => RDFVocabularies::OA.hasBody, :class_name => LD4L::OpenAnnotationRDF::TagBody

# TODO: Should a tag be destroyed when the last annotation referencing the tag is destroyed?
Expand Down Expand Up @@ -40,12 +44,23 @@ def setTag(tag)
# ** If one found, set @body to this TagBody
# ** If multiple found, use the first one found
# ### the same one may not be the first one found each time the query executes
@body = LD4L::OpenAnnotationRDF.configuration.unique_tags ? LD4L::OpenAnnotationRDF::TagBody.fetch_by_tag_value(tag) : nil
if self.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
@body = LD4L::OpenAnnotationRDF.configuration.unique_tags ? LD4L::OpenAnnotationRDF::TagBody.fetch_by_tag_value(tag,self) : nil
else
@body = LD4L::OpenAnnotationRDF.configuration.unique_tags ? LD4L::OpenAnnotationRDF::TagBody.fetch_by_tag_value(tag) : nil
end
if @body == nil
@body = LD4L::OpenAnnotationRDF::TagBody.new(
if self.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
@body = LD4L::OpenAnnotationRDF::TagBody.new(
ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::OpenAnnotationRDF::TagBody, 10, @localname_prefix,
LD4L::OpenAnnotationRDF.configuration.localname_minter ))
LD4L::OpenAnnotationRDF.configuration.localname_minter ),self)
else # < ActiveTriples 0.8
@body = LD4L::OpenAnnotationRDF::TagBody.new(
ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::OpenAnnotationRDF::TagBody, 10, @localname_prefix,
LD4L::OpenAnnotationRDF.configuration.localname_minter ))
end
@body.tag = tag
end
else
Expand All @@ -62,10 +77,17 @@ def setTag(tag)
# * [CURRENT] Always create a new TagBody each time setTag is called and update @body
# ### This last options has the potential for orphaned TagBodys that no TagAnnotation references.
# TODO Rethink the current behavior which is always to create a new TagBody potentially leaving around orphans.
@body = LD4L::OpenAnnotationRDF::TagBody.new(
ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::OpenAnnotationRDF::TagBody, 10, @localname_prefix,
LD4L::OpenAnnotationRDF.configuration.localname_minter ))
if self.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
@body = LD4L::OpenAnnotationRDF::TagBody.new(
ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::OpenAnnotationRDF::TagBody, 10, @localname_prefix,
LD4L::OpenAnnotationRDF.configuration.localname_minter ),self)
else # < ActiveTriples 0.8
@body = LD4L::OpenAnnotationRDF::TagBody.new(
ActiveTriples::LocalName::Minter.generate_local_name(
LD4L::OpenAnnotationRDF::TagBody, 10, @localname_prefix,
LD4L::OpenAnnotationRDF.configuration.localname_minter ))
end
@body.tag = tag
end
set_value(:hasBody, @body)
Expand All @@ -80,6 +102,7 @@ def initialize(*args)

# set motivatedBy
m = get_values(:motivatedBy)
m = m.to_a if Object.const_defined?("ActiveTriples::Relation") && m.kind_of?(ActiveTriples::Relation)
set_value(:motivatedBy, RDFVocabularies::OA.tagging) unless m.kind_of?(Array) && m.size > 0

# resume TagBody if it exists
Expand Down
5 changes: 3 additions & 2 deletions lib/ld4l/open_annotation_rdf/tag_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def self::annotations_using( tag_value )
# @param [String] tag value
#
# @return instance of TagBody if found; otherwise, nil
def self::fetch_by_tag_value( tag_value )
def self::fetch_by_tag_value( tag_value, parent_annotation=nil )
raise ArgumentError, 'Argument must be a string with at least one character' unless
tag_value.kind_of?(String) && tag_value.size > 0

Expand All @@ -66,7 +66,8 @@ def self::fetch_by_tag_value( tag_value )
results = query.execute(repo)
unless( results.empty? )
tagbody_uri = results[0].to_hash[:tagbody]
tagbody = LD4L::OpenAnnotationRDF::TagBody.new(tagbody_uri)
tagbody = LD4L::OpenAnnotationRDF::TagBody.new(tagbody_uri) if parent_annotation.nil?
tagbody = LD4L::OpenAnnotationRDF::TagBody.new(tagbody_uri,parent_annotation) unless parent_annotation.nil?
end
tagbody
end
Expand Down
52 changes: 25 additions & 27 deletions spec/ld4l/open_annotation_rdf/annotation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
end

it 'should not be settable' do
expect{ subject.set_subject! RDF::URI('http://example.org/moomin2') }.to raise_error
expect{ subject.set_subject! RDF::URI('http://example.org/moomin2') }.to raise_error(RuntimeError, 'Refusing update URI when one is already assigned!')
end
end
end
Expand Down Expand Up @@ -368,14 +368,14 @@
anno_uri = RDF::URI.new(@anno_url)
@comment_anno = LD4L::OpenAnnotationRDF::Annotation.resume(anno_uri)
end
it "populates LD4L::OpenAnnotationRDF::TagAnnotation properly" do
it "populates LD4L::OpenAnnotationRDF::CommentAnnotation properly" do
expect(@comment_anno.rdf_subject.to_s).to eq @anno_url
expect(@comment_anno).to be_a LD4L::OpenAnnotationRDF::CommentAnnotation
expect(@comment_anno.type).to include(RDFVocabularies::OA.Annotation)
expect(@comment_anno.motivatedBy).to include(RDFVocabularies::OA.commenting)
expect(@comment_anno.hasTarget.first.rdf_subject).to eq RDF::URI.new("http://searchworks.stanford.edu/view/666")
end
it "populates Tag bodies properly" do
it "populates Comment bodies properly" do
body = @comment_anno.hasBody.first
expect(body).to be_a LD4L::OpenAnnotationRDF::CommentBody
expect(body.content.first).to eq @comment_value
Expand Down Expand Up @@ -515,14 +515,18 @@
context "and the item is not a blank node" do

subject {LD4L::OpenAnnotationRDF::Annotation.new("123")}
let(:result) { subject.persist! }

before do
# Create inmemory repository
@repo = RDF::Repository.new
allow(subject.class).to receive(:repository).and_return(nil)
allow(subject).to receive(:repository).and_return(@repo)
ActiveTriples::Repositories.repositories[:default] = @repo
subject.motivatedBy = RDFVocabularies::OA.commenting
subject.persist!
result
end

it "should return true" do
expect(result).to eq true
end

it "should persist to the repository" do
Expand All @@ -548,7 +552,7 @@
subject << RDF::Statement(RDF::DC.LicenseDocument, RDF::DC.title, 'LICENSE')
end

subject { LD4L::FoafRDF::Person.new('456')}
subject { LD4L::OpenAnnotationRDF::Annotation.new('123') }

it 'should return true' do
expect(subject.destroy!).to be true
Expand All @@ -562,22 +566,26 @@

context 'with a parent' do
before do
parent.annotatedBy = subject
subject.annotatedBy = child
end

let(:parent) do
LD4L::OpenAnnotationRDF::Annotation.new('123')
let(:child) do
if subject.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
LD4L::FoafRDF::Person.new('456',subject)
else # < ActiveTriples 0.8
LD4L::FoafRDF::Person.new('456')
end
end

it 'should empty the graph and remove it from the parent' do
subject.destroy
expect(parent.annotatedBy).to be_empty
child.destroy
expect(subject.annotatedBy).to be_empty
end

it 'should remove its whole graph from the parent' do
subject.destroy
subject.each_statement do |s|
expect(parent.statements).not_to include s
child.destroy
child.each_statement do |s|
expect(subject.statements).not_to include s
end
end
end
Expand Down Expand Up @@ -674,7 +682,8 @@

describe '#type' do
it 'should return the type configured on the parent class' do
expect(subject.type).to eq [LD4L::OpenAnnotationRDF::Annotation.type]
expected_result = LD4L::OpenAnnotationRDF::Annotation.type.kind_of?(Array) ? LD4L::OpenAnnotationRDF::Annotation.type : [LD4L::OpenAnnotationRDF::Annotation.type]
expect(subject.type).to eq expected_result
end

it 'should set the type' do
Expand Down Expand Up @@ -708,17 +717,6 @@
end
end

describe '#solrize' do
it 'should return a label for bnodes' do
expect(subject.solrize).to eq subject.rdf_label
end

it 'should return a string of the resource uri' do
subject.set_subject! 'http://example.org/moomin'
expect(subject.solrize).to eq 'http://example.org/moomin'
end
end

describe 'editing the graph' do
it 'should write properties when statements are added' do
subject << RDF::Statement.new(subject.rdf_subject, RDFVocabularies::OA.motivatedBy, 'commenting')
Expand Down
Loading

0 comments on commit 0f5c1c7

Please sign in to comment.