001/** 002 * Copyright (C) 2007 - 2016, Jens Lehmann 003 * 004 * This file is part of DL-Learner. 005 * 006 * DL-Learner is free software; you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation; either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * DL-Learner is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package org.dllearner.kb.repository.tones; 020 021import java.io.BufferedReader; 022import java.io.IOException; 023import java.io.InputStreamReader; 024import java.net.URI; 025import java.net.URISyntaxException; 026import java.util.ArrayList; 027import java.util.Collection; 028import java.util.Collections; 029import java.util.List; 030 031import org.apache.log4j.Logger; 032import org.dllearner.kb.repository.OntologyRepository; 033import org.dllearner.kb.repository.OntologyRepositoryEntry; 034import org.semanticweb.owlapi.model.IRI; 035import org.semanticweb.owlapi.model.OWLOntology; 036import org.semanticweb.owlapi.model.OWLOntologyIRIMapper; 037import org.semanticweb.owlapi.util.OntologyIRIShortFormProvider; 038 039public class TONESRepository implements OntologyRepository{ 040 041 private static final Logger log = Logger.getLogger(TONESRepository.class); 042 043 private final String repositoryName = "TONES"; 044 045 private final URI repositoryLocation = URI.create("http://owl.cs.manchester.ac.uk/repository"); 046 047 private List<RepositoryEntry> entries; 048 049 private OWLOntologyIRIMapper iriMapper; 050 051 public TONESRepository() { 052 entries = new ArrayList<>(); 053 iriMapper = new RepositoryIRIMapper(); 054 } 055 056 @Override 057 public void initialize() { 058 refresh(); 059 } 060 061 @Override 062 public String getName() { 063 return repositoryName; 064 } 065 066 @Override 067 public String getLocation() { 068 return repositoryLocation.toString(); 069 } 070 071 @Override 072 public void refresh() { 073 fillRepository(); 074 } 075 076 @Override 077 public Collection<OntologyRepositoryEntry> getEntries() { 078 List<OntologyRepositoryEntry> ret = new ArrayList<>(); 079 ret.addAll(entries); 080 return ret; 081 } 082 083 @Override 084 public List<Object> getMetaDataKeys() { 085 return Collections.emptyList(); 086 } 087 088 @Override 089 public OWLOntology getOntology(OntologyRepositoryEntry entry) { 090 return null; 091 } 092 093 public void dispose() { 094 } 095 096 ///////////////////////////////////////////////////////////////////////////////////////////////// 097 // 098 // Implementation details 099 100 private void fillRepository() { 101 try { 102 entries.clear(); 103 URI listURI = URI.create(repositoryLocation + "/list"); 104 BufferedReader br = new BufferedReader(new InputStreamReader(listURI.toURL().openStream())); 105 String line; 106 while((line = br.readLine()) != null) { 107 try { 108 entries.add(new RepositoryEntry(new URI(line))); 109 } 110 catch (URISyntaxException e) { 111 e.printStackTrace(); 112 } 113 } 114 } 115 catch (IOException e) { 116 e.printStackTrace(); 117 } 118 log.info("Loaded " + entries.size() + " ontology entries from TONES."); 119 } 120 121 private class RepositoryEntry implements OntologyRepositoryEntry { 122 123 private String shortName; 124 125 private URI ontologyURI; 126 127 private URI physicalURI; 128 129 public RepositoryEntry(URI ontologyIRI) { 130 this.ontologyURI = ontologyIRI; 131 OntologyIRIShortFormProvider sfp = new OntologyIRIShortFormProvider(); 132 shortName = sfp.getShortForm(IRI.create(ontologyIRI)); 133 physicalURI = URI.create(repositoryLocation + "/download?ontology=" + ontologyIRI); 134 } 135 136 @Override 137 public String getOntologyShortName() { 138 return shortName; 139 } 140 141 @Override 142 public URI getOntologyURI() { 143 return ontologyURI; 144 } 145 146 @Override 147 public URI getPhysicalURI() { 148 return physicalURI; 149 } 150 151 @Override 152 public String getMetaData(Object key) { 153 return null; 154 } 155 156 } 157 158 private class RepositoryIRIMapper implements OWLOntologyIRIMapper { 159 160 @Override 161 public IRI getDocumentIRI(IRI iri) { 162 for(RepositoryEntry entry : entries) { 163 if(entry.getOntologyURI().equals(iri.toURI())) { 164 return IRI.create(entry.getPhysicalURI()); 165 } 166 } 167 return null; 168 } 169 } 170 171}