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.SortedSet;
022
023import org.dllearner.core.ComponentAnn;
024import org.dllearner.core.EvaluatedAxiom;
025import org.dllearner.kb.SparqlEndpointKS;
026import org.dllearner.kb.sparql.SparqlEndpoint;
027import org.dllearner.learningproblems.AxiomScore;
028import org.semanticweb.owlapi.model.AxiomType;
029import org.semanticweb.owlapi.model.IRI;
030import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom;
031import org.semanticweb.owlapi.model.OWLObjectProperty;
032
033import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
034
035import org.apache.jena.query.ParameterizedSparqlString;
036import org.apache.jena.query.QuerySolution;
037import org.apache.jena.query.ResultSet;
038
039@ComponentAnn(name = "inverse object property axiom learner", shortName = "oplinv", version = 0.1, description="A learning algorithm for inverse object property axioms.")
040public class InverseObjectPropertyAxiomLearner extends
041                ObjectPropertyAxiomLearner<OWLInverseObjectPropertiesAxiom> {
042        
043        private static final ParameterizedSparqlString POS_EXAMPLES_QUERY = new ParameterizedSparqlString(
044                        "SELECT ?p_inv ?s ?o WHERE { ?s ?p ?o . ?o ?p_inv ?s . FILTER(!sameTerm(?p, ?p_inv))}");
045        
046        private static final ParameterizedSparqlString NEG_EXAMPLES_QUERY = new ParameterizedSparqlString(
047                        "SELECT ?p_inv ?s ?o WHERE { ?s ?p ?o . FILTER NOT EXISTS {?o ?p_inv ?s . FILTER(!sameTerm(?p, ?p_inv))}}");
048        
049        private static final ParameterizedSparqlString QUERY = new ParameterizedSparqlString(
050                        "SELECT ?p_inv (COUNT(*) AS ?cnt) WHERE { ?s ?p ?o . ?o ?p_inv ?s . FILTER(!sameTerm(?p, ?p_inv))} GROUP BY ?p_inv");
051        
052        private static final ParameterizedSparqlString SAMPLE_QUERY = new ParameterizedSparqlString(
053                        "CONSTRUCT {?s ?p ?o . ?o ?p_inv ?s . } WHERE {?s ?p ?o . OPTIONAL{ ?o ?p_inv ?s . FILTER(!sameTerm(?p, ?p_inv))}}");
054
055        public InverseObjectPropertyAxiomLearner(SparqlEndpointKS ks) {
056                super.ks = ks;
057                
058                super.posExamplesQueryTemplate = POS_EXAMPLES_QUERY;
059                super.negExamplesQueryTemplate = NEG_EXAMPLES_QUERY;
060                
061                axiomType = AxiomType.INVERSE_OBJECT_PROPERTIES;
062        }
063        
064        /* (non-Javadoc)
065         * @see org.dllearner.algorithms.properties.PropertyAxiomLearner#setEntityToDescribe(org.semanticweb.owlapi.model.OWLProperty)
066         */
067        @Override
068        public void setEntityToDescribe(OWLObjectProperty entityToDescribe) {
069                super.setEntityToDescribe(entityToDescribe);
070                
071                QUERY.setIri("p", entityToDescribe.toStringID());
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.core.AbstractAxiomLearningAlgorithm#getExistingAxioms()
084         */
085        @Override
086        protected void getExistingAxioms() {
087                SortedSet<OWLObjectProperty> existingInverseObjectProperties = reasoner
088                                .getInverseObjectProperties(entityToDescribe);
089                for (OWLObjectProperty invProp : existingInverseObjectProperties) {
090                        existingAxioms.add(df.getOWLInverseObjectPropertiesAxiom(invProp, entityToDescribe));
091                }
092        }
093        
094        /* (non-Javadoc)
095         * @see org.dllearner.algorithms.properties.PropertyAxiomLearner#run()
096         */
097        @Override
098        protected void run() {
099                ResultSet rs = executeSelectQuery(QUERY.toString());
100                QuerySolution qs;
101                while (rs.hasNext()) {
102                        qs = rs.next();
103                        
104                        // candidate
105                        OWLObjectProperty candidate = df.getOWLObjectProperty(IRI.create(qs.getResource("p_inv").getURI()));
106                        
107                        // frequency
108                        int frequency = qs.getLiteral("cnt").getInt();
109                        
110                        // score
111                        AxiomScore score = computeScore(popularity, frequency, useSampling);
112                        
113                        currentlyBestAxioms.add(new EvaluatedAxiom<>(df
114                                        .getOWLInverseObjectPropertiesAxiom(entityToDescribe, candidate), score
115                        ));
116                }
117        }
118
119        public static void main(String[] args) throws Exception {
120                SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia());
121
122                InverseObjectPropertyAxiomLearner l = new InverseObjectPropertyAxiomLearner(ks);
123                l.setEntityToDescribe(new OWLDataFactoryImpl().getOWLObjectProperty(IRI
124                                .create("http://dbpedia.org/ontology/routeEnd")));
125                l.setMaxExecutionTimeInSeconds(60);
126                //              l.setForceSPARQL_1_0_Mode(true);
127                //              l.setReturnOnlyNewAxioms(true);
128                l.init();
129                l.start();
130
131                System.out.println(l.getCurrentlyBestEvaluatedAxioms(10, 0.2));
132        }
133
134        
135
136}