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.algorithms.schema; 020 021import java.util.HashSet; 022import java.util.List; 023import java.util.Set; 024import java.util.SortedSet; 025 026import org.aksw.jena_sparql_api.core.QueryExecutionFactory; 027import org.dllearner.algorithms.properties.AxiomAlgorithms; 028import org.dllearner.utilities.OwlApiJenaUtils; 029import org.semanticweb.owlapi.model.AxiomType; 030import org.semanticweb.owlapi.model.OWLAxiom; 031import org.semanticweb.owlapi.model.OWLEntity; 032import org.semanticweb.owlapi.model.OWLOntology; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036import com.google.common.collect.Sets; 037import com.google.common.collect.Sets.SetView; 038import org.apache.jena.rdf.model.Model; 039 040/** 041 * {@inheritDoc} 042 * <p> 043 * This is a very simple implementation of a schema generator which 044 * iterates over all entities and all axiom types in a specific order 045 * and adds axioms as long as the knowledge base remains consistent 046 * and coherent. 047 * 048 * @author Lorenz Buehmann 049 * 050 */ 051public class SimpleSchemaGenerator extends AbstractSchemaGenerator{ 052 053 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleSchemaGenerator.class); 054 055 // how often the enrichment process is executed 056 private int nrOfIterations = 1; 057 058 public SimpleSchemaGenerator(QueryExecutionFactory qef) { 059 super(qef); 060 } 061 062 public SimpleSchemaGenerator(OWLOntology ontology) { 063 super(OwlApiJenaUtils.getModel(ontology)); 064 } 065 066 public SimpleSchemaGenerator(Model model) { 067 super(model); 068 } 069 070 /* (non-Javadoc) 071 * @see org.dllearner.algorithms.schema.SchemaGenerator#generateSchema() 072 */ 073 @Override 074 public Set<OWLAxiom> generateSchema() { 075 Set<OWLAxiom> generatedAxiomsTotal = new HashSet<>(); 076 077 // get the entities 078 SortedSet<OWLEntity> entities = getEntities(); 079 080 // we repeat the whole process 081 for (int i = 0; i < nrOfIterations; i++) { 082 LOGGER.trace("Iteration " + (i+1) + " ..."); 083 Set<OWLAxiom> generatedAxioms = new HashSet<>(); 084 085 // iterate over the entities 086 for (OWLEntity entity : entities) { 087 // get the applicable axiom types 088 SetView<AxiomType<? extends OWLAxiom>> applicableAxiomTypes = Sets.intersection(AxiomAlgorithms.getAxiomTypes(entity.getEntityType()), axiomTypes); 089 090 // iterate over the axiom types 091 for (AxiomType<? extends OWLAxiom> axiomType : applicableAxiomTypes) { 092 // apply the appropriate learning algorithm 093 try { 094 Set<OWLAxiom> axioms = applyLearningAlgorithm(entity, axiomType); 095 generatedAxioms.addAll(axioms); 096 } catch (Exception e) { 097 LOGGER.error("Exception occured for axiom type " 098 + axiomType.getName() + " and entity " + entity + ".", e); 099 //TODO handle exception despite logging 100 } 101 } 102 } 103 104 // add the generated axioms to the knowledge base 105 addToKnowledgebase(generatedAxioms); 106 107 // new axioms 108 SetView<OWLAxiom> newAxioms = Sets.difference(generatedAxioms, generatedAxiomsTotal); 109 110 LOGGER.trace(newAxioms.isEmpty() ? "Got no new axioms." : ("Got " + newAxioms.size() + " new axioms:")); 111 if(newAxioms.isEmpty()){ // terminate if iteration lead to no new axioms 112 if((i+1) < nrOfIterations) 113 LOGGER.trace("Early termination. Ignoring further iterations."); 114 break; 115 } 116 LOGGER.trace(newAxioms.toString()); 117 118 // add to total set 119 generatedAxiomsTotal.addAll(generatedAxioms); 120 } 121 return generatedAxiomsTotal; 122 } 123 124 /** 125 * @param nrOfIterations the nrOfIterations to set 126 */ 127 public void setNrOfIterations(int nrOfIterations) { 128 this.nrOfIterations = nrOfIterations; 129 } 130}