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.properties; 020 021import java.util.List; 022import java.util.Set; 023import java.util.TreeSet; 024 025import org.dllearner.core.ComponentAnn; 026import org.dllearner.core.EvaluatedAxiom; 027import org.dllearner.kb.SparqlEndpointKS; 028import org.dllearner.kb.sparql.SparqlEndpoint; 029import org.semanticweb.owlapi.model.AxiomType; 030import org.semanticweb.owlapi.model.IRI; 031import org.semanticweb.owlapi.model.OWLIndividual; 032import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom; 033import org.semanticweb.owlapi.model.OWLObjectProperty; 034import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; 035 036import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; 037 038import org.apache.jena.query.ParameterizedSparqlString; 039import org.apache.jena.query.QuerySolution; 040import org.apache.jena.query.ResultSet; 041 042@ComponentAnn(name = "inverse functional object property axiom learner", shortName = "oplinvfunc", version = 0.1, description="A learning algorithm for inverse functional object property axioms.") 043public class InverseFunctionalObjectPropertyAxiomLearner extends 044 ObjectPropertyCharacteristicsAxiomLearner<OWLInverseFunctionalObjectPropertyAxiom> { 045 046 public InverseFunctionalObjectPropertyAxiomLearner(SparqlEndpointKS ks) { 047 super(ks); 048 049 super.posExamplesQueryTemplate = new ParameterizedSparqlString( 050 "SELECT ?s ?o ?WHERE {?s ?p ?o. FILTER NOT EXISTS {?s2 ?p ?o. FILTER(?s != ?s2)}}"); 051 super.negExamplesQueryTemplate = new ParameterizedSparqlString( 052 "SELECT ?s ?s2 ?o WHERE {?s ?p ?o. ?s2 ?p ?o. FILTER(?s != ?s2)}"); 053 054 super.POS_FREQUENCY_QUERY = new ParameterizedSparqlString( 055 "SELECT (COUNT(DISTINCT(?o)) AS ?cnt) WHERE {?s ?p ?o. FILTER NOT EXISTS {?s2 ?p ?o. FILTER(?s != ?s2)}}"); 056// "SELECT (COUNT(DISTINCT(?s)) AS ?cnt) WHERE {?o1 ?p ?s. ?o2 ?p ?s. FILTER(?o1 != ?o2)}"); 057 058 axiomType = AxiomType.INVERSE_FUNCTIONAL_OBJECT_PROPERTY; 059 COUNT_QUERY = DISTINCT_OBJECTS_COUNT_QUERY; 060 } 061 062 /* (non-Javadoc) 063 * @see org.dllearner.algorithms.properties.ObjectPropertyCharacteristicsAxiomLearner#getAxiom(org.semanticweb.owlapi.model.OWLObjectProperty) 064 */ 065 @Override 066 protected OWLInverseFunctionalObjectPropertyAxiom getAxiom(OWLObjectProperty property) { 067 return df.getOWLInverseFunctionalObjectPropertyAxiom(property); 068 } 069 070 /* (non-Javadoc) 071 * @see org.dllearner.algorithms.properties.ObjectPropertyCharacteristicsAxiomLearner#getNegativeExamples(org.dllearner.core.EvaluatedAxiom) 072 */ 073 @Override 074 public Set<OWLObjectPropertyAssertionAxiom> getNegativeExamples( 075 EvaluatedAxiom<OWLInverseFunctionalObjectPropertyAxiom> evaluatedAxiom) { 076 OWLInverseFunctionalObjectPropertyAxiom axiom = evaluatedAxiom.getAxiom(); 077 negExamplesQueryTemplate.setIri("p", axiom.getProperty().asOWLObjectProperty().toStringID()); 078 079 Set<OWLObjectPropertyAssertionAxiom> negExamples = new TreeSet<>(); 080 081 ResultSet rs = executeSelectQuery(negExamplesQueryTemplate.toString()); 082 083 while (rs.hasNext()) { 084 QuerySolution qs = rs.next(); 085 // ?o 086 OWLIndividual object = df.getOWLNamedIndividual(IRI.create(qs.getResource("o").getURI())); 087 // ?s 088 OWLIndividual subject1 = df.getOWLNamedIndividual(IRI.create(qs.getResource("s").getURI())); 089 // ?s2 090 OWLIndividual subject2 = df.getOWLNamedIndividual(IRI.create(qs.getResource("s2").getURI())); 091 // ?s -> ?o 092 negExamples.add(df.getOWLObjectPropertyAssertionAxiom(entityToDescribe, subject1, object)); 093 // ?s2 -> ?o 094 negExamples.add(df.getOWLObjectPropertyAssertionAxiom(entityToDescribe, subject2, object)); 095 } 096 097 return negExamples; 098 } 099 100 public static void main(String[] args) throws Exception { 101 InverseFunctionalObjectPropertyAxiomLearner l = new InverseFunctionalObjectPropertyAxiomLearner(new SparqlEndpointKS( 102 SparqlEndpoint.getEndpointDBpediaLiveAKSW())); 103 l.setEntityToDescribe(new OWLDataFactoryImpl().getOWLObjectProperty(IRI 104 .create("http://dbpedia.org/ontology/birthPlace"))); 105 l.setMaxExecutionTimeInSeconds(5); 106 l.init(); 107 l.start(); 108 List<EvaluatedAxiom<OWLInverseFunctionalObjectPropertyAxiom>> axioms = l.getCurrentlyBestEvaluatedAxioms(5); 109 System.out.println(axioms); 110 111 for (EvaluatedAxiom<OWLInverseFunctionalObjectPropertyAxiom> axiom : axioms) { 112 l.explainScore(axiom); 113 } 114 } 115}