simple rdf export of maps

This commit is contained in:
Devin Howard 2017-01-16 17:08:25 -05:00
parent dc8d274487
commit 2fedbc0787
6 changed files with 111 additions and 0 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.rdf { redirect_to action: :export, format: :rdf }
end
end
@ -94,6 +95,7 @@ class MapsController < ApplicationController
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,17 @@ 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[-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,17 @@ 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)
output += %( foaf:homepage <#{link}>;\n) if link.present?
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,16 @@ class User < ApplicationRecord
json
end
def as_rdf
output = ''
output += %(d:mapper_#{id} a foaf:OnlineAccount ;\n)
output += %( foaf:accountName "#{name}";\n)
output += %( foaf:accountServiceHomepage "https://metamaps.cc/mapper/#{id}";\n)
output[-2] = '.' # change last ; to a .
output += %(\n)
output
end
def all_accessible_maps
maps + shared_maps
end

View file

@ -22,6 +22,15 @@ class MapExportService
end
end
def rdf
output = ''
output += rdf_header
output += rdf_mappers
output += rdf_topics
output += rdf_synapses
output
end
private
def topic_headings
@ -87,4 +96,38 @@ class MapExportService
spreadsheet
end
def rdf_header
output = ''
output += "PREFIX d: <https://metamaps.cc/maps/#{map.id}>\n"
output += "PREFIX gr: <https://metamaps.cc/rdf/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"
output
end
def rdf_mappers
output = ''
map.contributors.each do |mapper|
output += mapper.as_rdf
end
output
end
def rdf_topics
output = ''
map.topics.each do |topic|
output += topic.as_rdf
end
output
end
def rdf_synapses
output = ''
map.synapses.each do |synapse|
output += synapse.as_rdf
end
output
end
end

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

@ -0,0 +1,34 @@
@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 concept 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:topic ;
rdfs:range owl:Thing .
mm:topic2 a owl:ObjectProperty ;
a owl:FunctionalProperty ;
rdfs:label "second topic of a synapse"@en ;
rdfs:domain mm:topic ;
rdfs:range owl:Thing .
mm:direction a owl:objectProprty ;
a owl:FunctionalProperty ;
rdfs:label "from-to, both, or none"@en ;
rdfs:domain mm:synapse ;
rdfs:range xsd:string .