Skip to content

Commit

Permalink
Merge 9abdee4 into e9eab19
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Aug 2, 2016
2 parents e9eab19 + 9abdee4 commit 32cb734
Show file tree
Hide file tree
Showing 27 changed files with 390 additions and 431 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ script: "bundle exec rspec spec"
sudo: false
cache: bundler
rvm:
- 2.0
- 2.1
- 2.2.2
- 2.2.4
- 2.3.0
- 2.3.1
- jruby-9.0.4.0
- rbx-2
- rbx
matrix:
allow_failures:
- rvm: jruby-9.0.4.0
- rvm: rbx-2
- rvm: rbx
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
source 'https://rubygems.org'

gemspec

gem 'activesupport', '< 5.0.0' if RUBY_VERSION =~ /2\.1\..*/
gem 'pry-byebug' unless ENV["CI"]
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ Or install it yourself as:
$ gem install ld4l-open_annotation_rdf


## Primary Dependencies

* [ActiveTriples 0.10.0](https://github.com/ActiveTriples/ActiveTriples/tree/v0.10.0)
* [RDF 2.0.2](https://github.com/ruby-rdf/rdf/tree/2.0.2)

## Usage

**Caveat:** This gem is part of the LD4L Project and is being used in that context. There is no guarantee that the
Expand Down
7 changes: 2 additions & 5 deletions ld4l-open_annotation_rdf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ Gem::Specification.new do |spec|
spec.description = %q{LD4L Open Annotation RDF provides tools for modeling a annotations based on the Open Annotation ontology and persisting to a triplestore.}
spec.homepage = "https://github.com/ld4l/open_annotation_rdf"
spec.license = "APACHE2"
spec.required_ruby_version = '>= 1.9.3'
spec.required_ruby_version = '>= 2.1'

spec.files = `git ls-files -z`.split("\x0")
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})

spec.add_dependency('rdf', '= 1.99.0')
spec.add_dependency('rdf')
spec.add_dependency('active-triples')
spec.add_dependency('active_triples-local_name')
spec.add_dependency('ld4l-foaf_rdf')

spec.add_development_dependency('pry')
spec.add_development_dependency('pry-byebug')
spec.add_development_dependency('rdoc')
spec.add_development_dependency('rspec')
spec.add_development_dependency('coveralls')
spec.add_development_dependency('guard-rspec')
spec.add_development_dependency('webmock')

spec.extra_rdoc_files = [
Expand Down
27 changes: 1 addition & 26 deletions lib/ld4l/open_annotation_rdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
require 'linkeddata'
require 'ld4l/foaf_rdf'
require 'ld4l/open_annotation_rdf/version'
require 'ld4l/open_annotation_rdf/vocab/oa'
require 'ld4l/open_annotation_rdf/vocab/cnt'
require 'ld4l/open_annotation_rdf/vocab/dctypes'


module LD4L
Expand All @@ -29,38 +26,16 @@ def self.configure
yield(configuration)
end


# RDF vocabularies
autoload :OA, 'ld4l/open_annotation_rdf/vocab/oa'
autoload :CNT, 'ld4l/open_annotation_rdf/vocab/cnt'
autoload :DCTYPES, 'ld4l/open_annotation_rdf/vocab/dctypes'


# autoload classes
autoload :Configuration, 'ld4l/open_annotation_rdf/configuration'
autoload :Annotation, 'ld4l/open_annotation_rdf/annotation'
autoload :AnnotationGenericBody, 'ld4l/open_annotation_rdf/annotation_generic_body'
autoload :CommentAnnotation, 'ld4l/open_annotation_rdf/comment_annotation'
autoload :CommentBody, 'ld4l/open_annotation_rdf/comment_body'
autoload :TagAnnotation, 'ld4l/open_annotation_rdf/tag_annotation'
autoload :TagBody, 'ld4l/open_annotation_rdf/tag_body'
autoload :SemanticTagAnnotation, 'ld4l/open_annotation_rdf/semantic_tag_annotation'
autoload :SemanticTagBody, 'ld4l/open_annotation_rdf/semantic_tag_body'

def self.class_from_string(class_name, container_class=Kernel)
container_class = container_class.name if container_class.is_a? Module
container_parts = container_class.split('::')
(container_parts + class_name.split('::')).flatten.inject(Kernel) do |mod, class_name|
if mod == Kernel
Object.const_get(class_name)
elsif mod.const_defined? class_name.to_sym
mod.const_get(class_name)
else
container_parts.pop
class_from_string(class_name, container_parts.join('::'))
end
end
end

end
end

58 changes: 28 additions & 30 deletions lib/ld4l/open_annotation_rdf/annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,30 @@ class << self; attr_reader :localname_prefix end

@body = nil

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

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, :cast => false # :type => xsd:dateTime # the time Annotation was created
property :motivatedBy, :predicate => RDFVocabularies::OA.motivatedBy, :cast => false # comes from RDFVocabularies::OA ontology
property :hasTarget, :predicate => RDF::Vocab::OA.hasTarget # :type => URI
property :hasBody, :predicate => RDF::Vocab::OA.hasBody
property :annotatedBy, :predicate => RDF::Vocab::OA.annotatedBy, :class_name => LD4L::FoafRDF::Person
property :annotatedAt, :predicate => RDF::Vocab::OA.annotatedAt, :cast => false # :type => xsd:dateTime # the time Annotation was created
property :motivatedBy, :predicate => RDF::Vocab::OA.motivatedBy, :cast => false # comes from RDF::Vocab::OA ontology

def self.resume(uri_or_str)
# Let ActiveTriples::Resource validate uri_or_str when creating new Annotation
a = new(uri_or_str)
return nil if a.nil?

# get motivatedBy
m = a.get_values(:motivatedBy)
m = m.to_a if Object::ActiveTriples.const_defined?("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)

# motivatedBy is set
m_uri = m.first
# currently only support commenting and tagging
return LD4L::OpenAnnotationRDF::CommentAnnotation.new(uri_or_str) if m_uri == RDFVocabularies::OA.commenting
return a unless m_uri == RDFVocabularies::OA.tagging
m = motivated_by a
return LD4L::OpenAnnotationRDF::CommentAnnotation.new(uri_or_str) if m.include? RDF::Vocab::OA.commenting

# 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 unless stb.type.include?(RDFVocabularies::OA.Tag) || !stb.type.include?(RDFVocabularies::OA.SemanticTag)

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

# can't match to a known annotation type, so return as generic annotation
return a
return a unless m.include? RDF::Vocab::OA.tagging
b = body_resource a
return LD4L::OpenAnnotationRDF::TagAnnotation.new(uri_or_str) if b.type.include? RDF::Vocab::OA.Tag
LD4L::OpenAnnotationRDF::SemanticTagAnnotation.new(uri_or_str)
end

##
Expand Down Expand Up @@ -98,15 +83,28 @@ def self::find_by_target(target_uri)
repo = ActiveTriples::Repositories.repositories[repository]
query = RDF::Query.new({
:annotation => {
RDF.type => RDFVocabularies::OA.Annotation,
RDFVocabularies::OA.hasTarget => target_uri,
RDF.type => RDF::Vocab::OA.Annotation,
RDF::Vocab::OA.hasTarget => target_uri,
}
})
annotations = []
results = query.execute(repo)
results.each { |r| annotations << r.to_hash[:annotation] }
annotations
end

private
def self::motivated_by annotation
m = annotation.get_values(:motivatedBy).to_a
return nil unless m.kind_of?(Array) && (m.size > 0)
m
end

def self::body_resource annotation
body_uris = annotation.hasBody
return nil if body_uris.nil? || body_uris.size < 1
AnnotationGenericBody.new(body_uris.first) # TODO: a full implementation of OA could have multiple bodies
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/ld4l/open_annotation_rdf/annotation_generic_body.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module LD4L
module OpenAnnotationRDF
class AnnotationGenericBody < ActiveTriples::Resource

class << self; attr_reader :localname_prefix end
@localname_prefix="a"

configure :base_uri => LD4L::OpenAnnotationRDF.configuration.base_uri,
:repository => :default
end
end
end
7 changes: 2 additions & 5 deletions lib/ld4l/open_annotation_rdf/comment_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ class CommentAnnotation < LD4L::OpenAnnotationRDF::Annotation

@localname_prefix="ca"

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

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


##
# Create a comment annotation body and set the hasBody property to it.
#
Expand Down Expand Up @@ -54,7 +51,7 @@ def initialize(*args)
# set motivatedBy
m = get_values(:motivatedBy)
m = m.to_a if Object::ActiveTriples.const_defined?("Relation") && m.kind_of?(ActiveTriples::Relation)
set_value(:motivatedBy, RDFVocabularies::OA.commenting) unless m.kind_of?(Array) && m.size > 0
set_value(:motivatedBy, RDF::Vocab::OA.commenting) unless m.kind_of?(Array) && m.size > 0

# resume CommentBody if it exists
comment_uri = get_values(:hasBody).first
Expand Down
8 changes: 4 additions & 4 deletions lib/ld4l/open_annotation_rdf/comment_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ class CommentBody < ActiveTriples::Resource
class << self; attr_reader :localname_prefix end
@localname_prefix="cb"

configure :type => RDFVocabularies::CNT.ContentAsText,
configure :type => RDF::Vocab::CNT.ContentAsText,
:base_uri => LD4L::OpenAnnotationRDF.configuration.base_uri,
:repository => :default

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

def initialize(*args)
super(*args)

t = get_values(:type)
t << RDFVocabularies::DCTYPES.Text
t << RDF::Vocab::DCMIType.Text
set_value(:type,t)
end
end
Expand Down
7 changes: 2 additions & 5 deletions lib/ld4l/open_annotation_rdf/semantic_tag_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ class SemanticTagAnnotation < LD4L::OpenAnnotationRDF::Annotation

@localname_prefix = "sta"

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

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


# USAGE: Use setTerm to set the hasBody property to be the URI of the controlled vocabulary term that
# is the annotation.

Expand Down Expand Up @@ -60,7 +57,7 @@ def initialize(*args)
# set motivatedBy
m = get_values(:motivatedBy)
m = m.to_a if Object::ActiveTriples.const_defined?("Relation") && m.kind_of?(ActiveTriples::Relation)
set_value(:motivatedBy, RDFVocabularies::OA.tagging) unless m.kind_of?(Array) && m.size > 0
set_value(:motivatedBy, RDF::Vocab::OA.tagging) unless m.kind_of?(Array) && m.size > 0

# resume SemanticTagBody if it exists
term_uri = get_values(:hasBody).first
Expand Down
6 changes: 3 additions & 3 deletions lib/ld4l/open_annotation_rdf/semantic_tag_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class << self; attr_reader :localname_prefix end
# USAGE: When creating a semantic tag body, use the URI of the controlled vocabulary term as the RDF Subject URI
# for an instance of this class.

configure :type => RDFVocabularies::OA.SemanticTag,
configure :type => RDF::Vocab::OA.SemanticTag,
:base_uri => LD4L::OpenAnnotationRDF.configuration.base_uri,
:repository => :default

Expand All @@ -30,8 +30,8 @@ def self::annotations_using( term_uri )
repo = ActiveTriples::Repositories.repositories[repository]
query = RDF::Query.new({
:annotation => {
RDF.type => RDFVocabularies::OA.Annotation,
RDFVocabularies::OA.hasBody => term_uri,
RDF.type => RDF::Vocab::OA.Annotation,
RDF::Vocab::OA.hasBody => term_uri,
}
})
annotations = []
Expand Down
6 changes: 2 additions & 4 deletions lib/ld4l/open_annotation_rdf/tag_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ class TagAnnotation < LD4L::OpenAnnotationRDF::Annotation

@localname_prefix="ta"

configure :type => RDFVocabularies::OA.Annotation,
configure :type => RDF::Vocab::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 @@ -103,7 +101,7 @@ def initialize(*args)
# set motivatedBy
m = get_values(:motivatedBy)
m = m.to_a if Object::ActiveTriples.const_defined?("Relation") && m.kind_of?(ActiveTriples::Relation)
set_value(:motivatedBy, RDFVocabularies::OA.tagging) unless m.kind_of?(Array) && m.size > 0
set_value(:motivatedBy, RDF::Vocab::OA.tagging) unless m.kind_of?(Array) && m.size > 0

# resume TagBody if it exists
tag_uri = get_values(:hasBody).first
Expand Down
14 changes: 7 additions & 7 deletions lib/ld4l/open_annotation_rdf/tag_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class TagBody < ActiveTriples::Resource
class << self; attr_reader :localname_prefix end
@localname_prefix="tb"

configure :type => RDFVocabularies::OA.Tag,
configure :type => RDF::Vocab::OA.Tag,
:base_uri => LD4L::OpenAnnotationRDF.configuration.base_uri,
:repository => :default

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

##
# Get a list of annotations using the tag value.
Expand All @@ -31,8 +31,8 @@ def self::annotations_using( tag_value )
repo = ActiveTriples::Repositories.repositories[repository]
query = RDF::Query.new({
:annotation => {
RDF.type => RDFVocabularies::OA.Annotation,
RDFVocabularies::OA.hasBody => tag_uri,
RDF.type => RDF::Vocab::OA.Annotation,
RDF::Vocab::OA.hasBody => tag_uri,
}
})
results = query.execute(repo)
Expand All @@ -57,8 +57,8 @@ def self::fetch_by_tag_value( tag_value, parent_annotation=nil )
repo = ActiveTriples::Repositories.repositories[repository]
query = RDF::Query.new({
:tagbody => {
RDF.type => RDFVocabularies::OA.Tag,
RDFVocabularies::CNT.chars => tag_value,
RDF.type => RDF::Vocab::OA.Tag,
RDF::Vocab::CNT.chars => tag_value,
}
})

Expand All @@ -76,7 +76,7 @@ def initialize(*args)
super(*args)

t = get_values(:type)
t << RDFVocabularies::CNT.ContentAsText
t << RDF::Vocab::CNT.ContentAsText
set_value(:type,t)
end
end
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.1.0"
VERSION = "0.2.0"
end
end
6 changes: 0 additions & 6 deletions lib/ld4l/open_annotation_rdf/vocab/cnt.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/ld4l/open_annotation_rdf/vocab/dctypes.rb

This file was deleted.

0 comments on commit 32cb734

Please sign in to comment.