-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Patrick Sinclair
committed
Oct 24, 2008
0 parents
commit 20acb7c
Showing
17 changed files
with
518 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require File.dirname(__FILE__) + '/../lib/ldontospec' | ||
|
||
command = ARGV[0] | ||
directory = ARGV[1] | ||
prefix = ARGV[2] | ||
uri = ARGV[3] | ||
|
||
if !command || !directory | ||
puts "Usage: #{$0} <build|setup|preview> <directory> <prefix> <uri>" | ||
exit | ||
end | ||
|
||
configuration = StaticMatic::Configuration.new | ||
|
||
config_file = "#{directory}/src/configuration.rb" | ||
|
||
if File.exists?(config_file) | ||
config = File.read(config_file) | ||
eval(config) | ||
end | ||
|
||
ldontospec = LDOntoSpec::Base.new(directory, configuration) | ||
ldontospec.ontology_prefix = prefix | ||
ldontospec.ontology_uri = uri | ||
ldontospec.run(command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'rubygems' | ||
require 'staticmatic' | ||
require 'rena' | ||
require 'activerdf_reddy' | ||
require 'activerdf_rules' | ||
|
||
require File.dirname(__FILE__) + '/ldontospec/base' | ||
require File.dirname(__FILE__) + '/ldontospec/activerdf_hacks' | ||
require File.dirname(__FILE__) + '/ldontospec/helpers' | ||
|
||
Haml::Helpers.class_eval("include LDOntoSpec::Helpers") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
RDFS::Resource.class_eval do | ||
define_method(:title) do | ||
label = self.rdfs::label | ||
label = self.dc::title if label.nil? | ||
label = self.dcterms::title if label.nil? | ||
return (label.nil? ? '' : label) | ||
end | ||
|
||
define_method(:search_for_property) do |property| | ||
results = Query.new.select(:s).where(self, property, :s).execute | ||
results.uniq! | ||
return results.delete_if { |r| r.uri =~ %r[http://www.activerdf.org/bnode/] } | ||
end | ||
|
||
define_method(:search_for_property_of) do |property| | ||
results = Query.new.select(:s).where(:s, property, self).execute | ||
results.uniq! | ||
return results.delete_if { |r| r.uri =~ %r[http://www.activerdf.org/bnode/] } | ||
end | ||
end | ||
|
||
OWL::Ontology.class_eval do | ||
define_method(:classes) do | ||
all_classes = OWL::Class.find_all | ||
return all_classes.find_all { |c| c.uri.index(self.uri) }.sort | ||
end | ||
|
||
define_method(:properties) do | ||
all_properties = OWL::ObjectProperty.find_all | ||
all_properties << OWL::DatatypeProperty.find_all | ||
all_properties << OWL::TransitiveProperty.find_all | ||
all_properties.flatten!.uniq! | ||
return all_properties.find_all { |p| p.uri.index(self.uri) }.sort | ||
end | ||
end | ||
|
||
OWL::Class.class_eval do | ||
define_method(:funky_properties) do | ||
properties = { | ||
'subClassOf' => self.search_for_property(RDFS::subClassOf), | ||
'inDomainOf' => self.search_for_property_of(RDFS::domain), | ||
'inRangeOf' => self.search_for_property_of(RDFS::range) | ||
} | ||
return properties.delete_if {|prop, classes | classes.nil? or classes.empty? } | ||
end | ||
end | ||
|
||
[OWL::ObjectProperty, OWL::TransitiveProperty, OWL::DatatypeProperty].each do |c| | ||
c.class_eval do | ||
define_method(:funky_properties) do | ||
properties = { | ||
'subPropertyOf' => self.search_for_property(RDFS::subPropertyOf), | ||
'domain' =>self.search_for_property(RDFS::domain), | ||
'range' => self.search_for_property(RDFS::range), | ||
} | ||
return properties.delete_if {|prop, classes | classes.nil? or classes.empty? } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
require 'pp' | ||
module LDOntoSpec | ||
|
||
class Scope | ||
PROTECTED = ['@ontology', '@authors', '@version', '@namespaces'] | ||
def initialize(object) | ||
@staticmatic = object.instance_variable_get('@staticmatic') | ||
end | ||
|
||
def instance_variable_set(name, value) | ||
# don't reset special variables | ||
super unless (PROTECTED.include?(name) and instance_variable_get(name)) | ||
end | ||
end | ||
|
||
class Base < StaticMatic::Base | ||
attr_accessor :ontology_uri, :ontology_prefix | ||
|
||
def initialize(base_dir, configuration = Configuration.new) | ||
super | ||
@templates_dir = File.join(File.dirname(__FILE__), 'templates') | ||
@settings_path = File.join(@base_dir, 'settings.yml') | ||
@scope = Scope.new(@scope) | ||
end | ||
|
||
def setup | ||
super | ||
setup_ontology(@ontology_uri, @ontology_prefix) | ||
end | ||
|
||
def build | ||
build_ontology | ||
super | ||
end | ||
|
||
def preview | ||
build_ontology | ||
super | ||
end | ||
|
||
# broken in staticmatic? | ||
def generate_html_from_template_source(source, options = {}) | ||
html = Haml::Engine.new(source, options) | ||
locals = options[:locals] || {} | ||
html.render(@scope, locals) { yield } | ||
end | ||
|
||
def generate_partial(name, options = {}) | ||
begin | ||
super | ||
rescue StaticMatic::Error => e | ||
generate_ldontospec_partial(name, options) if e.message =~ /Partial not found/ | ||
end | ||
end | ||
|
||
def generate_ldontospec_partial(name, options = {}) | ||
partial_path = File.join(@templates_dir, 'partials', "#{name}.haml") | ||
if File.exists?(partial_path) | ||
partial_rel_path = partial_path.gsub(/\/+/, '/') | ||
@current_file_stack.unshift(partial_rel_path) | ||
begin | ||
generate_html_from_template_source(File.read(partial_path), options) | ||
rescue Haml::Error => haml_error | ||
raise StaticMatic::Error.new(haml_error.haml_line, "Partial: #{partial_rel_path[0,partial_rel_path.length-5]}", haml_error.message) | ||
ensure | ||
@current_file_stack.shift | ||
end | ||
else | ||
raise StaticMatic::Error.new("", name, "Partial not found") | ||
end | ||
end | ||
|
||
protected | ||
|
||
def build_ontology | ||
config = YAML.load_file(@settings_path) | ||
|
||
triples_cache = File.join(@base_dir, 'cache.sqlite3') | ||
adapter = ConnectionPool.add_data_source(:type => :reddy, :location => triples_cache) | ||
|
||
ontology = OWL::Ontology.new(config[:uri]) | ||
namespaces = config[:namespaces] | ||
namespaces.keys.each { |key| Namespace.register key, namespaces[key] } | ||
|
||
# Register some standard ontologies | ||
Namespace.register 'dc', 'http://purl.org/dc/elements/1.1/' | ||
Namespace.register 'dcterms', 'http://purl.org/dc/terms/' | ||
Namespace.register 'foaf', 'http://xmlns.com/foaf/0.1/' | ||
|
||
ObjectManager.construct_classes | ||
namespaces.each_key { |key| namespaces[key] = RDFS::Resource.new(namespaces[key]) } | ||
|
||
authors = [ ontology.foaf::maker, ontology.dc::contributor ].flatten.compact.uniq | ||
authors.flatten! | ||
|
||
if ontology.dc::date =~ %r[Date: (\d+/\d+/\d+) \d+:\d+:\d+ ] | ||
date = $1 | ||
version = date.gsub('/','-') unless date.blank? | ||
else | ||
version = Date.today.strftime('%Y-%m-%d') | ||
end | ||
|
||
@scope.instance_variable_set("@ontology", ontology) | ||
@scope.instance_variable_set("@authors", authors) | ||
@scope.instance_variable_set("@version", version) | ||
@scope.instance_variable_set("@namespaces", namespaces) | ||
end | ||
|
||
def setup_ontology(uri, prefix) | ||
triples_cache = File.join(@base_dir, 'cache.sqlite3') | ||
adapter = ConnectionPool.add_data_source(:type => :reddy, :location => triples_cache) | ||
# FIXME: remove this once Rena supports more RDF | ||
adapter_rapper = ConnectionPool.add_data_source(:type => :fetching, :location => triples_cache) | ||
|
||
puts "Fetching Ontology: #{uri}" | ||
adapter.fetch(uri) | ||
|
||
Namespace.register prefix, uri | ||
Namespace.register 'foaf', 'http://xmlns.com/foaf/0.1/' | ||
Namespace.register 'dc', 'http://purl.org/dc/elements/1.1/' | ||
ObjectManager.construct_classes | ||
|
||
rules = RuleBase.new('MyRuleBase') { | ||
rule "unionOf" do | ||
condition :s, :p, :o | ||
condition :o, OWL::unionOf, :list | ||
condition :list, RDF::first, :thing | ||
|
||
conclusion :s, :p, :thing | ||
end | ||
rule "unionOf-rest" do | ||
condition :s, :p, :o | ||
condition :o, OWL::unionOf, :list | ||
# FIXME: what if there is a longer list??? | ||
condition :list, RDF::rest, :rest | ||
condition :rest, RDF::first, :thing | ||
|
||
conclusion :s, :p, :thing | ||
end | ||
} | ||
|
||
puts "Applying inference rules" | ||
re = RuleEngine.new | ||
re.rule_bases << RuleEngine::RDFSRuleBase | ||
re.rule_bases << RuleEngine::OWLRuleBase | ||
re.rule_bases << rules | ||
while re.process_rules; end | ||
|
||
# Cache namespaced ontologies | ||
adapter.namespaces.values.each do |ns| | ||
next if ns==uri | ||
puts "Fetching linked data #{ns}" | ||
adapter_rapper.fetch ns | ||
end | ||
|
||
# Cache authors | ||
ontology = RDFS::Resource.new(uri) | ||
[ ontology.foaf::maker, ontology.dc::contributor ].flatten.compact.uniq.each do |author| | ||
adapter_rapper.fetch author.uri | ||
end | ||
|
||
settings = { | ||
:uri => uri, | ||
:prefix => prefix, | ||
:namespaces => adapter.namespaces | ||
} | ||
File.open(@settings_path, 'w') { |f| f.puts(settings.to_yaml) } | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module LDOntoSpec | ||
module Helpers | ||
def link_to_ontology_rdf() | ||
%[<link rel="meta" type="application/rdf+xml" title="#{@ontology.title} RDF Representation" href="#{@ontology.uri}" />] | ||
end | ||
|
||
def name_for_resource(resource) | ||
name = resource.localname | ||
name << " - #{resource.vs::term_status }" if Namespace.abbreviations.include? :vs | ||
name | ||
end | ||
|
||
def comments_paragraphs(resource) | ||
return '' if resource.rdfs::comment.nil? | ||
comments = resource.rdfs::comment.map { |c| c.strip }.find_all { |c| !c.blank? } | ||
comments.map { |c| "<p>#{c}</p>" }.join("\n") | ||
end | ||
|
||
def link_to_author(author) | ||
if (author.nil?) | ||
'' | ||
elsif (author and author.foaf::name and author.foaf::homepage) | ||
link_to author.foaf::name, author.foaf::homepage.uri | ||
elsif (author and author.foaf::name) | ||
link_to author.foaf::name, author.uri | ||
else | ||
link_to author.uri, author.uri | ||
end | ||
end | ||
|
||
def link_to_term(term) | ||
prefix = @namespaces.keys.find { |k| k if (term.uri.index(@namespaces[k].uri)==0) } | ||
if term.uri.index(@ontology.uri)==0 | ||
link = "##{term.localname}" | ||
else | ||
link = term.uri | ||
end | ||
link_to("#{prefix}:#{term.localname}", link) | ||
end | ||
|
||
def setup_ontology(uri, prefix) | ||
if ConnectionPool.adapters.empty? | ||
ConnectionPool.add_data_source( | ||
:type => :fetching, | ||
:location => "#{prefix}.sqlite3") | ||
end | ||
ldontospec = LDOntoSpec.new(prefix, uri) | ||
|
||
@namespaces = ldontospec.namespaces | ||
@namespaces[prefix] = uri | ||
@namespaces.keys.each { |key| Namespace.register key, @namespaces[key] } | ||
ObjectManager.construct_classes | ||
|
||
@namespaces.each_key { |key| @namespaces[key] = RDFS::Resource.new(@namespaces[key]) } | ||
@ontology = OWL::Ontology.new(ldontospec.ontology_uri) | ||
|
||
@authors = [ @ontology.foaf::maker ] | ||
@authors << [ @ontology.dc::contributor ] | ||
@authors.flatten! | ||
@authors.each { |author| ConnectionPool.read_adapters.first.fetch author.uri } | ||
|
||
@version = Date.today.strftime('%Y-%m-%d') | ||
if @ontology.dc::date =~ %r[Date: (\d+/\d+/\d+) \d+:\d+:\d+ ] | ||
date = $1 | ||
@version = date.gsub('/','-') unless date.blank? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
!!! XML | ||
!!! Strict | ||
%html{ :xmlns => 'http://www.w3.org/1999/xhtml', 'xml:lang' => 'en', :lang => 'en' } | ||
%head | ||
%title= @ontology.title | ||
= link_to_ontology_rdf | ||
%meta | ||
= stylesheets | ||
%body | ||
= yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
body | ||
:font | ||
:family Verdana | ||
:size 10pt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
%h1= @ontology.title | ||
|
||
- versions = {} | ||
= partial('authors', :locals => { :versions => versions }) | ||
|
||
= partial('namespaces') | ||
= partial('terms_overview') | ||
= partial('terms') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
%dl | ||
%dt This Version | ||
%dd= versions[:this] | ||
%dt Latest Version | ||
%dd= versions[:latest] | ||
%dt Previous Version | ||
%dd= versions[:previous] | ||
|
||
%dt Published | ||
%dd= @version | ||
|
||
%dt Authors of this document | ||
- for author in @authors | ||
%dd= link_to_author(author) |
Oops, something went wrong.