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.lov;
020
021import org.aksw.jena_sparql_api.core.QueryExecutionFactory;
022import org.aksw.jena_sparql_api.http.QueryExecutionFactoryHttp;
023import org.apache.jena.query.QueryExecution;
024import org.apache.jena.query.QuerySolution;
025import org.apache.jena.query.ResultSet;
026import org.apache.log4j.Logger;
027import org.dllearner.kb.repository.OntologyRepository;
028import org.dllearner.kb.repository.OntologyRepositoryEntry;
029import org.semanticweb.owlapi.apibinding.OWLManager;
030import org.semanticweb.owlapi.model.IRI;
031import org.semanticweb.owlapi.model.OWLOntology;
032import org.semanticweb.owlapi.model.OWLOntologyIRIMapper;
033import org.semanticweb.owlapi.model.OWLOntologyManager;
034import org.semanticweb.owlapi.util.OntologyIRIShortFormProvider;
035
036import java.net.URI;
037import java.util.ArrayList;
038import java.util.Collection;
039import java.util.Collections;
040import java.util.List;
041
042public class LOVRepository implements OntologyRepository{
043
044        private static final Logger log = Logger.getLogger(LOVRepository.class);
045
046        private final String repositoryName = "LOV";
047
048    String endpointURL = "http://lov.okfn.org/dataset/lov/sparql";
049
050    private List<RepositoryEntry> entries;
051
052    private OWLOntologyIRIMapper iriMapper;
053
054    public LOVRepository() {
055        entries = new ArrayList<>();
056        iriMapper = new RepositoryIRIMapper();
057    }
058
059    @Override
060    public void initialize() {
061        refresh();
062    }
063
064    @Override
065    public String getName() {
066        return repositoryName;
067    }
068
069    @Override
070    public String getLocation() {
071        return endpointURL;
072    }
073
074    @Override
075    public void refresh() {
076        fillRepository();
077    }
078
079    @Override
080    public Collection<OntologyRepositoryEntry> getEntries() {
081        List<OntologyRepositoryEntry> ret = new ArrayList<>();
082        ret.addAll(entries);
083        return ret;
084    }
085
086    @Override
087    public List<Object> getMetaDataKeys() {
088        return Collections.emptyList();
089    }
090
091    @Override
092    public OWLOntology getOntology(OntologyRepositoryEntry entry) {
093        return null;
094    }
095
096    public void dispose() {
097    }
098
099    /////////////////////////////////////////////////////////////////////////////////////////////////
100    //
101    //  Implementation details
102
103    private void fillRepository() {
104        String query = "PREFIX vann:<http://purl.org/vocab/vann/>\n" +
105                "PREFIX voaf:<http://purl.org/vocommons/voaf#>\n" +
106                " \n" +
107                "### Vocabularies contained in LOV and their prefix\n" +
108                "SELECT DISTINCT ?vocabPrefix ?vocabURI {\n" +
109                " \tGRAPH <http://lov.okfn.org/dataset/lov>{\n" +
110                " \t \t?vocabURI a voaf:Vocabulary.\n" +
111                " \t \t?vocabURI vann:preferredNamespacePrefix ?vocabPrefix.\n" +
112                "}} ORDER BY ?vocabPrefix";
113
114        OWLOntologyManager man = OWLManager.createOWLOntologyManager();
115
116        QueryExecutionFactory qef = new QueryExecutionFactoryHttp(endpointURL);
117        try(QueryExecution qe = qef.createQueryExecution(query)) {
118            ResultSet rs = qe.execSelect();
119            while(rs.hasNext()) {
120                QuerySolution qs = rs.next();
121                String uri = qs.getResource("vocabURI").getURI();
122                System.out.println(uri);
123                try {
124                    OWLOntology ont = man.loadOntology(IRI.create(uri));
125                    entries.add(new RepositoryEntry(URI.create(uri)));
126                } catch (Exception e) {
127                    e.printStackTrace();
128                }
129            }
130        }
131        log.info("Loaded " + entries.size() + " ontology entries from LOV.");
132    }
133
134    private class RepositoryEntry implements OntologyRepositoryEntry {
135
136        private String shortName;
137
138        private URI ontologyURI;
139
140        private URI physicalURI;
141
142        public RepositoryEntry(URI ontologyIRI) {
143            this.ontologyURI = ontologyIRI;
144            OntologyIRIShortFormProvider sfp = new OntologyIRIShortFormProvider();
145            shortName = sfp.getShortForm(IRI.create(ontologyIRI));
146            physicalURI = URI.create(getLocation() + "/download?ontology=" + ontologyIRI);
147        }
148
149        @Override
150        public String getOntologyShortName() {
151            return shortName;
152        }
153
154        @Override
155        public URI getOntologyURI() {
156            return ontologyURI;
157        }
158
159        @Override
160        public URI getPhysicalURI() {
161            return physicalURI;
162        }
163
164        @Override
165        public String getMetaData(Object key) {
166            return null;
167        }
168
169    }
170
171    private class RepositoryIRIMapper implements OWLOntologyIRIMapper {
172
173        @Override
174        public IRI getDocumentIRI(IRI iri) {
175            for(RepositoryEntry entry : entries) {
176                if(entry.getOntologyURI().equals(iri.toURI())) {
177                    return IRI.create(entry.getPhysicalURI());
178                }
179            }
180            return null;
181        }
182    }
183
184    public static void main(String[] args) throws Exception {
185        new LOVRepository().initialize();
186    }
187
188}