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.OWLFunctionalObjectPropertyAxiom; 032import org.semanticweb.owlapi.model.OWLIndividual; 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 = "functional object property axiom learner", shortName = "oplfunc", version = 0.1, description="A learning algorithm for functional object property axioms.") 043public class FunctionalObjectPropertyAxiomLearner extends 044 ObjectPropertyCharacteristicsAxiomLearner<OWLFunctionalObjectPropertyAxiom> { 045 046 public FunctionalObjectPropertyAxiomLearner(SparqlEndpointKS ks) { 047 super(ks); 048 049 super.posExamplesQueryTemplate = new ParameterizedSparqlString( 050 "SELECT ?s (?o1 AS ?o) ?WHERE {?s ?p ?o1. FILTER NOT EXISTS {?s ?p ?o2. FILTER(?o1 != ?o2)}}"); 051 super.negExamplesQueryTemplate = new ParameterizedSparqlString( 052 "SELECT ?s ?o1 ?o2 WHERE {?s ?p ?o1. ?s ?p ?o2. FILTER(?o1 != ?o2)}"); 053 054 super.POS_FREQUENCY_QUERY = new ParameterizedSparqlString( 055 "SELECT (COUNT(DISTINCT(?s)) AS ?cnt) WHERE {?s ?p ?o1. FILTER NOT EXISTS {?s ?p ?o2. FILTER(?o1 != ?o2)}}"); 056 057 COUNT_QUERY = DISTINCT_SUBJECTS_COUNT_QUERY; 058 059 axiomType = AxiomType.FUNCTIONAL_OBJECT_PROPERTY; 060 061 } 062 063 /* (non-Javadoc) 064 * @see org.dllearner.algorithms.properties.ObjectPropertyCharacteristicsAxiomLearner#getAxiom(org.semanticweb.owlapi.model.OWLObjectProperty) 065 */ 066 @Override 067 protected OWLFunctionalObjectPropertyAxiom getAxiom(OWLObjectProperty property) { 068 return df.getOWLFunctionalObjectPropertyAxiom(property); 069 } 070 071 /* (non-Javadoc) 072 * @see org.dllearner.algorithms.properties.ObjectPropertyCharacteristicsAxiomLearner#getNegativeExamples(org.dllearner.core.EvaluatedAxiom) 073 */ 074 @Override 075 public Set<OWLObjectPropertyAssertionAxiom> getNegativeExamples( 076 EvaluatedAxiom<OWLFunctionalObjectPropertyAxiom> evaluatedAxiom) { 077 OWLFunctionalObjectPropertyAxiom axiom = evaluatedAxiom.getAxiom(); 078 negExamplesQueryTemplate.setIri("p", axiom.getProperty().asOWLObjectProperty().toStringID()); 079 080 Set<OWLObjectPropertyAssertionAxiom> negExamples = new TreeSet<>(); 081 082 ResultSet rs = executeSelectQuery(negExamplesQueryTemplate.toString()); 083 084 while (rs.hasNext()) { 085 QuerySolution qs = rs.next(); 086 OWLIndividual subject = df.getOWLNamedIndividual(IRI.create(qs.getResource("s").getURI())); 087 // ?o1 088 OWLIndividual object = df.getOWLNamedIndividual(IRI.create(qs.getResource("o1").getURI())); 089 negExamples.add(df.getOWLObjectPropertyAssertionAxiom(entityToDescribe, subject, object)); 090 // ?o2 091 object = df.getOWLNamedIndividual(IRI.create(qs.getResource("o2").getURI())); 092 negExamples.add(df.getOWLObjectPropertyAssertionAxiom(entityToDescribe, subject, object)); 093 } 094 095 return negExamples; 096 } 097 098 public static void main(String[] args) throws Exception { 099 FunctionalObjectPropertyAxiomLearner l = new FunctionalObjectPropertyAxiomLearner(new SparqlEndpointKS( 100 SparqlEndpoint.getEndpointDBpedia())); 101 l.setEntityToDescribe(new OWLDataFactoryImpl().getOWLObjectProperty(IRI 102 .create("http://dbpedia.org/ontology/birthPlace"))); 103 l.setMaxExecutionTimeInSeconds(20); 104 l.setForceSPARQL_1_0_Mode(true); 105 l.init(); 106 l.start(); 107 List<EvaluatedAxiom<OWLFunctionalObjectPropertyAxiom>> axioms = l.getCurrentlyBestEvaluatedAxioms(5); 108 System.out.println(axioms); 109 110 for (EvaluatedAxiom<OWLFunctionalObjectPropertyAxiom> axiom : axioms) { 111 printSubset(l.getPositiveExamples(axiom), 10); 112 printSubset(l.getNegativeExamples(axiom), 10); 113 l.explainScore(axiom); 114 } 115 } 116}