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 org.dllearner.core.ComponentAnn;
022import org.dllearner.core.EvaluatedAxiom;
023import org.dllearner.kb.SparqlEndpointKS;
024import org.semanticweb.owlapi.model.AxiomType;
025import org.semanticweb.owlapi.model.OWLDataProperty;
026import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom;
027
028import org.apache.jena.query.ParameterizedSparqlString;
029
030@ComponentAnn(name="functional data property axiom learner", shortName="dplfunc", version=0.1, description="A learning algorithm for functional data property axioms.")
031public class FunctionalDataPropertyAxiomLearner extends DataPropertyAxiomLearner<OWLFunctionalDataPropertyAxiom> {
032        
033        private final ParameterizedSparqlString GET_SAMPLE_QUERY = new ParameterizedSparqlString(
034                        "CONSTRUCT {?s ?p ?o.} WHERE {?s ?p ?o}");
035        
036        private final ParameterizedSparqlString POS_FREQUENCY_QUERY = new ParameterizedSparqlString(
037                        "SELECT (COUNT(DISTINCT(?s)) AS ?cnt) WHERE {?s ?p ?o1. FILTER NOT EXISTS {?s ?p ?o2. FILTER(?o1 != ?o2)} }");
038        
039        private boolean declaredAsFunctional;
040
041        public FunctionalDataPropertyAxiomLearner(SparqlEndpointKS ks){
042                this.ks = ks;
043                
044                posExamplesQueryTemplate = new ParameterizedSparqlString("SELECT ?s WHERE {?s ?p ?o1. FILTER NOT EXISTS {?s ?p ?o2. FILTER(?o1 != ?o2)} }");
045                negExamplesQueryTemplate = new ParameterizedSparqlString("SELECT ?s WHERE {?s ?p ?o1. ?s ?p ?o2. FILTER(?o1 != ?o2)}");
046                
047                COUNT_QUERY = DISTINCT_SUBJECTS_COUNT_QUERY;
048                
049                axiomType = AxiomType.FUNCTIONAL_DATA_PROPERTY;
050        }
051        
052        /* (non-Javadoc)
053         * @see org.dllearner.core.AbstractAxiomLearningAlgorithm#getExistingAxioms()
054         */
055        @Override
056        protected void getExistingAxioms() {
057                declaredAsFunctional = reasoner.isFunctional(entityToDescribe);
058                if(declaredAsFunctional) {
059                        existingAxioms.add(df.getOWLFunctionalDataPropertyAxiom(entityToDescribe));
060                        logger.warn("Data property " + entityToDescribe + " is already declared as functional in knowledge base.");
061                }
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(OWLDataProperty entityToDescribe) {
069                super.setEntityToDescribe(entityToDescribe);
070                
071                POS_FREQUENCY_QUERY.setIri("p", entityToDescribe.toStringID());
072                GET_SAMPLE_QUERY.setIri("p", entityToDescribe.toStringID());
073        }
074        
075        /* (non-Javadoc)
076         * @see org.dllearner.algorithms.properties.DataPropertyAxiomLearner#run()
077         */
078        @Override
079        protected void run() {
080                boolean declared = !existingAxioms.isEmpty();
081                
082                int frequency = getCountValue(POS_FREQUENCY_QUERY.toString());
083
084                currentlyBestAxioms.add(new EvaluatedAxiom<>(
085                                df.getOWLFunctionalDataPropertyAxiom(entityToDescribe),
086                                computeScore(popularity, frequency, useSampling),
087                                declared));
088        }
089}