map rdf export (fixes #1015) (#1036)

* simple rdf export of maps

* register ttl mime type

* owl

* mm

* fix up export service

* implement base url thing whoo

* add more rdf fields

* fix rdf syntax errors

* hide unused fields in rdf

* some code climate fixes

* update ontology a bit more

* syntax fix

* typo
This commit is contained in:
Devin Howard 2017-01-22 16:42:04 -05:00 committed by GitHub
parent 2652d53e9b
commit d11278b63b
7 changed files with 106 additions and 3 deletions

View file

@ -20,6 +20,7 @@ class MapsController < ApplicationController
end
format.json { render json: @map }
format.csv { redirect_to action: :export, format: :csv }
format.ttl { redirect_to action: :export, format: :ttl }
end
end
@ -90,10 +91,12 @@ class MapsController < ApplicationController
# GET maps/:id/export
def export
exporter = MapExportService.new(current_user, @map)
exporter = MapExportService.new(current_user, @map, base_url: request.base_url)
respond_to do |format|
format.json { render json: exporter.json }
format.csv { send_data exporter.csv }
format.ttl { render text: exporter.rdf }
end
end

View file

@ -51,6 +51,18 @@ class Synapse < ApplicationRecord
super(methods: [:user_name, :user_image, :collaborator_ids])
end
def as_rdf
output = ''
output += %(d:synapse_#{id} a mm:Synapse ;\n)
output += %( mm:topic1 d:topic_#{topic1_id} ;\n)
output += %( mm:topic2 d:topic_#{topic2_id} ;\n)
output += %( mm:direction "#{category}" ;\n)
output += %( rdfs:comment "#{desc}" ;\n) if desc.present?
output[-2] = '.'
output += %(\n)
output
end
def after_updated
attrs = ['desc', 'category', 'permission', 'defer_to_map_id']
if attrs.any? {|k| changed_attributes.key?(k)}

View file

@ -82,6 +82,19 @@ class Topic < ApplicationRecord
map_count: map_count(options[:user]), synapse_count: synapse_count(options[:user]))
end
def as_rdf
output = ''
output += %(d:topic_#{id} a mm:Topic ;\n)
output += %( rdfs:label "#{name}" ;\n)
output += %( rdfs:comment "#{desc}" ;\n) if desc.present?
output += %( foaf:homepage <#{link}> ;\n) if link.present?
output += %( mm:mapper d:mapper_#{user_id} ;\n)
output += %( mm:metacode "#{metacode.name}" ;\n)
output[-2] = '.' # change last ; to a .
output += %(\n)
output
end
def collaborator_ids
if defer_to_map
defer_to_map.editors.select { |mapper| mapper != user }.map(&:id)

View file

@ -67,6 +67,17 @@ class User < ApplicationRecord
json
end
def as_rdf(opts = {})
base_url = opts[:base_url] || 'https://metamaps.cc'
output = ''
output += %(d:mapper_#{id} a foaf:OnlineAccount ;\n)
output += %( foaf:accountName "#{name}" ;\n)
output += %( foaf:accountServiceHomepage "#{base_url}/mapper/#{id}" ;\n)
output[-2] = '.' # change last ; to a .
output += %(\n)
output
end
def all_accessible_maps
maps + shared_maps
end

View file

@ -1,9 +1,11 @@
# frozen_string_literal: true
class MapExportService
attr_reader :user, :map
def initialize(user, map)
attr_reader :user, :map, :base_url
def initialize(user, map, opts = {})
@user = user
@map = map
@base_url = opts[:base_url] || 'https://metamaps.cc'
end
def json
@ -22,6 +24,25 @@ class MapExportService
end
end
def rdf
output = ''
output += "PREFIX d: <#{base_url}/maps/#{map.id}>\n"
output += "PREFIX mm: <#{base_url}/owl/map.owl.ttl>\n"
output += "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
output += "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
output += "\n"
map.contributors.each do |mapper|
output += mapper.as_rdf(base_url: base_url)
end
map.topics.each do |topic|
output += topic.as_rdf
end
map.synapses.each do |synapse|
output += synapse.as_rdf
end
output
end
private
def topic_headings

View file

@ -3,3 +3,6 @@
# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# RDF export
Mime::Type.register 'text/turtle', :ttl

40
public/owl/map.owl.ttl Normal file
View file

@ -0,0 +1,40 @@
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix : <http://metamaps.cc/owl/map.owl.ttl#> .
@prefix mm: <http://metamaps.cc/owl/map.owl.ttl#> .
: a owl:Ontology ;
rdfs:label "Metamaps Map"@en .
mm:Topic a owl:Class ;
rdfs:label "One topic on a metamap"@en .
mm:Synapse a owl:Class ;
rdfs:label "Link between two topics on a metamap"@en .
mm:topic1 a owl:ObjectProperty ;
a owl:FunctionalProperty ;
rdfs:label "first topic of a synapse"@en ;
rdfs:domain mm:Synapse ;
rdfs:range mm:Topic .
mm:topic2 a owl:ObjectProperty ;
a owl:FunctionalProperty ;
rdfs:label "second topic of a synapse"@en ;
rdfs:domain mm:Topic ;
rdfs:range mm:Topic .
mm:mapper a owl:ObjectProperty ;
a owl:FunctionalProperty ;
rdfs:label "Metamaps user who created this topic"@en ;
rdfs:domain mm:Topic ;
rdfs:range foaf:OnlineAccount .
mm:direction a owl:ObjectProperty ;
a owl:FunctionalProperty ;
rdfs:label "from-to, both, or none"@en ;
rdfs:domain mm:Synapse ;
rdfs:range [ owl:oneOf mm:from-to, mm:both, mm:none ] .