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.OWLObjectProperty; 033import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; 034import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom; 035 036import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl; 037 038import org.apache.jena.query.ParameterizedSparqlString; 039import org.apache.jena.query.QuerySolution; 040import org.apache.jena.query.ResultSet; 041 042@ComponentAnn(name = "transitive object property axiom learner", shortName = "opltrans", version = 0.1, description="A learning algorithm for transitive object property axioms.") 043public class TransitiveObjectPropertyAxiomLearner extends ObjectPropertyCharacteristicsAxiomLearner<OWLTransitiveObjectPropertyAxiom> { 044 045 private static final ParameterizedSparqlString SAMPLE_QUERY = new ParameterizedSparqlString( 046 "CONSTRUCT {?s ?p ?o . ?o ?p ?o1 . ?s ?p ?o1 .} WHERE {?s ?p ?o . ?o ?p ?o1 . OPTIONAL {?s ?p ?o1 .}}"); 047 048 public TransitiveObjectPropertyAxiomLearner(SparqlEndpointKS ks) { 049 super(ks); 050 051 posExamplesQueryTemplate = new ParameterizedSparqlString( 052 "SELECT DISTINCT ?s ?o1 ?o2 WHERE {?s ?p ?o1. ?o1 ?p ?o2. ?s ?p ?o2}"); 053 negExamplesQueryTemplate = new ParameterizedSparqlString( 054 "SELECT DISTINCT ?s ?o1 ?o2 WHERE {?s ?p ?o1. ?o1 ?p ?o2. FILTER NOT EXISTS {?s ?p ?o2 }}"); 055 056 axiomType = AxiomType.TRANSITIVE_OBJECT_PROPERTY; 057 058 COUNT_QUERY = new ParameterizedSparqlString( 059 "SELECT (COUNT(*) AS ?cnt) WHERE {?s ?p ?o1. ?o1 ?p ?o2. }"); 060 061 POS_FREQUENCY_QUERY = new ParameterizedSparqlString( 062 "SELECT (COUNT(*) AS ?cnt) WHERE {?s ?p ?o1. ?o1 ?p ?o2. ?s ?p ?o2}"); 063 064 } 065 066 /* (non-Javadoc) 067 * @see org.dllearner.algorithms.properties.ObjectPropertyCharacteristicsAxiomLearner#getAxiom(org.semanticweb.owlapi.model.OWLObjectProperty) 068 */ 069 @Override 070 protected OWLTransitiveObjectPropertyAxiom getAxiom(OWLObjectProperty property) { 071 return df.getOWLTransitiveObjectPropertyAxiom(property); 072 } 073 074 /* (non-Javadoc) 075 * @see org.dllearner.algorithms.properties.PropertyAxiomLearner#getSampleQuery() 076 */ 077 @Override 078 protected ParameterizedSparqlString getSampleQuery() { 079 return SAMPLE_QUERY; 080 } 081 082 /* (non-Javadoc) 083 * @see org.dllearner.algorithms.properties.ObjectPropertyCharacteristicsAxiomLearner#getNegativeExamples(org.dllearner.core.EvaluatedAxiom) 084 */ 085 @Override 086 public Set<OWLObjectPropertyAssertionAxiom> getNegativeExamples( 087 EvaluatedAxiom<OWLTransitiveObjectPropertyAxiom> evaluatedAxiom) { 088 OWLTransitiveObjectPropertyAxiom axiom = evaluatedAxiom.getAxiom(); 089 negExamplesQueryTemplate.setIri("p", axiom.getProperty().asOWLObjectProperty().toStringID()); 090 091 Set<OWLObjectPropertyAssertionAxiom> negExamples = new TreeSet<>(); 092 093 ResultSet rs = executeSelectQuery(negExamplesQueryTemplate.toString()); 094 095 while (rs.hasNext()) { 096 QuerySolution qs = rs.next(); 097 // ?s 098 OWLIndividual subject = df.getOWLNamedIndividual(IRI.create(qs.getResource("s").getURI())); 099 // ?o1 100 OWLIndividual object1 = df.getOWLNamedIndividual(IRI.create(qs.getResource("o1").getURI())); 101 // ?o2 102 OWLIndividual object2 = df.getOWLNamedIndividual(IRI.create(qs.getResource("o2").getURI())); 103 104 // ?s -> ?o1 105 negExamples.add(df.getOWLObjectPropertyAssertionAxiom(entityToDescribe, subject, object1)); 106 // ?o1 -> ?o2 107 negExamples.add(df.getOWLObjectPropertyAssertionAxiom(entityToDescribe, object1, object2)); 108 } 109 110 return negExamples; 111 } 112 113 public static void main(String[] args) throws Exception { 114 TransitiveObjectPropertyAxiomLearner l = new TransitiveObjectPropertyAxiomLearner(new SparqlEndpointKS( 115 SparqlEndpoint.getEndpointDBpediaLiveAKSW())); 116 l.setEntityToDescribe(new OWLObjectPropertyImpl(IRI.create("http://dbpedia.org/ontology/birthPlace"))); 117 l.setMaxExecutionTimeInSeconds(5); 118 l.init(); 119 l.start(); 120 List<EvaluatedAxiom<OWLTransitiveObjectPropertyAxiom>> axioms = l.getCurrentlyBestEvaluatedAxioms(5, 0.0); 121 System.out.println(axioms); 122 123 for (EvaluatedAxiom<OWLTransitiveObjectPropertyAxiom> axiom : axioms) { 124 l.explainScore(axiom); 125 } 126 } 127 128}