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.qtl.util.filters;
020
021import com.google.common.base.Charsets;
022import com.google.common.io.Files;
023import org.apache.jena.graph.Node;
024import org.apache.jena.graph.NodeFactory;
025import org.apache.jena.query.QueryExecution;
026import org.apache.jena.query.QuerySolution;
027import org.apache.jena.query.ResultSet;
028import org.apache.jena.rdf.model.Resource;
029import org.apache.jena.sparql.util.NodeComparator;
030import org.dllearner.kb.SparqlEndpointKS;
031
032import java.io.File;
033import java.io.IOException;
034import java.net.URISyntaxException;
035import java.util.List;
036import java.util.Set;
037import java.util.TreeSet;
038
039/**
040 * @author Lorenz Buehmann
041 *
042 */
043public class PredicateExistenceFilterDBpedia extends PredicateExistenceFilter{
044        
045        private String PATH = "org/dllearner/algorithms/qtl/dbpedia_meaningless_properties.txt";
046        private SparqlEndpointKS ks;
047        
048        public PredicateExistenceFilterDBpedia(SparqlEndpointKS ks) {
049                this.ks = ks;
050                Set<Node> existentialMeaninglessProperties = new TreeSet<>(new NodeComparator());
051                
052                try {
053                        List<String> lines = Files.readLines(new File(this.getClass().getClassLoader().getResource(PATH).toURI()), Charsets.UTF_8);
054                        for (String line : lines) {
055                                if(!line.trim().isEmpty() && !line.startsWith("#")) {
056                                        existentialMeaninglessProperties.add(NodeFactory.createURI(line.trim()));
057                                }
058                        }
059                } catch (IOException | URISyntaxException e) {
060                        e.printStackTrace();
061                }
062                setExistentialMeaninglessProperties(existentialMeaninglessProperties);
063        }
064        
065        private void analyze() {
066                Set<Node> existentialMeaninglessProperties = new TreeSet<>(new NodeComparator());
067                
068                StringBuilder sb = new StringBuilder();
069                // check data properties
070                String query = "SELECT ?p ?range WHERE {?p a owl:DatatypeProperty . ?p rdfs:range ?range .}";
071                
072                QueryExecution qe = ks.getQueryExecutionFactory().createQueryExecution(query);
073                ResultSet rs = qe.execSelect();
074                while (rs.hasNext()) {
075                        QuerySolution qs = rs.next();
076                        
077                        Resource property = qs.getResource("p");
078                        Resource range = qs.getResource("range");
079                        
080//                      if(range.equals(XSD.xdouble)) {
081                                existentialMeaninglessProperties.add(property.asNode());
082//                      }
083                }
084                qe.close();
085                for (Node p : existentialMeaninglessProperties) {
086                        sb.append(p).append("\n");
087                }
088                existentialMeaninglessProperties.clear();
089                sb.append("\n\n");
090                
091                // check object properties
092                query = "SELECT ?p WHERE {?p a owl:ObjectProperty .}";
093
094                qe = ks.getQueryExecutionFactory().createQueryExecution(query);
095                rs = qe.execSelect();
096                while (rs.hasNext()) {
097                        QuerySolution qs = rs.next();
098
099                        Resource property = qs.getResource("p");
100                        existentialMeaninglessProperties.add(property.asNode());
101                        
102                }
103                qe.close();
104                
105                for (Node p : existentialMeaninglessProperties) {
106                        sb.append(p).append("\n");
107                }
108                try {
109                        Files.asCharSink(new File("dbpedia_meaningless_properties.txt"), Charsets.UTF_8).write(sb.toString());
110                } catch (IOException e) {
111                        e.printStackTrace();
112                }
113        }
114
115}