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.pattern; 020 021import org.aksw.jena_sparql_api.core.QueryExecutionFactory; 022import org.apache.jena.query.QueryExecution; 023import org.apache.jena.query.QuerySolution; 024import org.apache.jena.query.ResultSet; 025import org.apache.jena.rdf.model.Model; 026import org.apache.jena.rdf.model.ModelFactory; 027import org.dllearner.kb.SparqlEndpointKS; 028import org.dllearner.kb.sparql.ConciseBoundedDescriptionGenerator; 029import org.dllearner.kb.sparql.ConciseBoundedDescriptionGeneratorImpl; 030import org.semanticweb.owlapi.model.IRI; 031import org.semanticweb.owlapi.model.OWLClass; 032import org.semanticweb.owlapi.model.OWLDataFactory; 033import org.semanticweb.owlapi.model.OWLIndividual; 034import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; 035 036import java.util.HashSet; 037import java.util.Set; 038 039/** 040 * @author Lorenz Buehmann 041 * 042 */ 043public class IndividualBasedFragmentExtractor implements FragmentExtractor{ 044 045 public static final FragmentExtractionStrategy extractionStrategy = FragmentExtractionStrategy.INDIVIDUALS; 046 private QueryExecutionFactory qef; 047 048 private long maxNrOfIndividuals; 049 private long startTime; 050 051 private ConciseBoundedDescriptionGenerator cbdGen; 052 053 private OWLDataFactory df = new OWLDataFactoryImpl(); 054 055 public IndividualBasedFragmentExtractor(SparqlEndpointKS ks, int maxNrOfIndividuals) { 056 this.maxNrOfIndividuals = maxNrOfIndividuals; 057 cbdGen = new ConciseBoundedDescriptionGeneratorImpl(ks.getQueryExecutionFactory()); 058 } 059 060 /* (non-Javadoc) 061 * @see org.dllearner.algorithms.pattern.FragmentExtractor#extractFragment(org.dllearner.core.owl.NamedClass) 062 */ 063 @Override 064 public Model extractFragment(OWLClass cls, int maxFragmentDepth) { 065 startTime = System.currentTimeMillis(); 066 Model fragment = ModelFactory.createDefaultModel(); 067 068 //get some random individuals 069 Set<OWLIndividual> individuals = getRandomIndividuals(cls); 070 071 //get for each individual the CBD 072 Model cbd; 073 for (OWLIndividual ind : individuals) { 074 cbd = cbdGen.getConciseBoundedDescription(ind.toStringID(), maxFragmentDepth); 075 fragment.add(cbd); 076 } 077 return fragment; 078 } 079 080 private Set<OWLIndividual> getRandomIndividuals(OWLClass cls){ 081 Set<OWLIndividual> individuals = new HashSet<>(); 082 083 String query = "SELECT ?s WHERE {?s a <" + cls.toStringID() + ">} LIMIT " + maxNrOfIndividuals; 084 QueryExecution qe = qef.createQueryExecution(query); 085 ResultSet rs = qe.execSelect(); 086 QuerySolution qs; 087 while(rs.hasNext()){ 088 qs = rs.next(); 089 if(qs.get("s").isURIResource()){ 090 individuals.add(df.getOWLNamedIndividual(IRI.create(qs.getResource("s").getURI()))); 091 } 092 } 093 qe.close(); 094 095 return individuals; 096 } 097}