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.sparql.simple; 020 021import java.util.HashSet; 022import java.util.List; 023import java.util.Set; 024 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028import org.apache.jena.ontology.OntModel; 029import org.apache.jena.rdf.model.ModelFactory; 030import com.jamonapi.Monitor; 031import com.jamonapi.MonitorFactory; 032 033public class SchemaIndexer { 034 private static Logger log = LoggerFactory.getLogger(SchemaIndexer.class); 035 private OntModel model; 036 037 //static instantiation 038 private static ClassIndexer classIndexer = new ClassIndexer(); 039 //remember ontologies 040 private static Set<String> alreadyIndexed = new HashSet<>(); 041 //set or list of urls for the ontologies 042 private List<String> ontologySchemaUrls; 043 044 public SchemaIndexer() { 045 } 046 047 public synchronized void init() { 048 049 for (String url : ontologySchemaUrls) { 050 log.info("Testing, if indexed: " + url); 051 if (alreadyIndexed.add(url)) { 052 log.info("Ontology not found, start indexing"); 053 try { 054 Monitor m0 = MonitorFactory.start("Indexer parsing ontology"); 055 model = ModelFactory.createOntologyModel(); 056 model.read(url, null); 057 classIndexer.index(model); 058 m0.stop(); 059 log.info("indexed ontology in ms: " + m0.getTotal()); 060 } catch (Exception e) { 061 log.error(e.getMessage(), e); 062 } 063 } else { 064 //not so important output 065 log.debug("Already indexed: " + url + " " + alreadyIndexed); 066 } 067 } 068 069 } 070 071 public OntModel getHierarchyForURI(String classUri) { 072 if (classIndexer == null) { 073 this.init(); 074 } 075 return classIndexer.getHierarchyForClassURI(classUri); 076 } 077 078 public static void main(String... args) { 079 SchemaIndexer i = new SchemaIndexer(); 080 System.out.println(i.getHierarchyForURI("http://dbpedia.org/ontology/Software")); 081 } 082 083 public void setOntologySchemaUrls(List<String> ontologySchemaUrls) { 084 this.ontologySchemaUrls = ontologySchemaUrls; 085 } 086}