001/**
002 * Copyright (C) 2007-2011, 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 */
019
020package org.dllearner.scripts.analyse;
021
022import java.net.URL;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.dllearner.kb.sparql.Cache;
027import org.dllearner.kb.sparql.SPARQLTasks;
028import org.dllearner.kb.sparql.SparqlEndpoint;
029
030import org.apache.jena.query.QuerySolution;
031import org.apache.jena.query.ResultSetFormatter;
032import org.apache.jena.query.ResultSetRewindable;
033import org.apache.jena.rdf.model.Literal;
034import org.apache.jena.rdf.model.Resource;
035
036public class CountInstances {
037        
038                SPARQLTasks t;
039                
040                public CountInstances( String url , List<String> defaultGraphs){
041                        try{
042                         t = new SPARQLTasks(Cache.getDefaultCache(), 
043                                         new SparqlEndpoint(new URL(url), defaultGraphs, new ArrayList<>()));
044                        }catch (Exception e) {
045                                e.printStackTrace();    
046                        }
047                        
048                }
049                
050                public class Count implements Comparable<Count>{
051                        public Count(String uri, int count) {
052                                super();
053                                this.uri = uri;
054                                this.count = count;
055                        }
056                        String uri;
057                        int count;
058                        @Override
059                        public String toString(){
060                                return count+"\t"+uri;
061                        }
062                        @Override
063                        public int compareTo(Count o) {
064                                if(this.count == o.count) {
065                                        return this.uri.compareTo(o.uri);
066                                }
067                                return o.count -this.count ;
068                        }
069
070                }
071                
072                
073
074                public static void main(String[] args) {
075                }
076
077                @SuppressWarnings("unchecked")
078                public List<Count> countProperties() {
079                        List<Count> ret = new ArrayList<>();
080                        String query = "SELECT ?p count(?p) as ?count {" + 
081                                                "?s ?p ?o" +
082                                                "} GROUP BY ?p " +
083                                                "ORDER by DESC(?count)" + "";
084                        ResultSetRewindable rsw = t.queryAsResultSet(query);
085                        final List<QuerySolution> l = ResultSetFormatter.toList(rsw);
086                        for (QuerySolution resultBinding : l) {
087                                Resource res = (Resource)resultBinding.get("p");
088                                Literal lit =  (Literal) resultBinding.get("count");
089                                
090                                ret.add(new Count(res.getURI(), lit.getInt()));
091                        }
092                        return ret;
093                }
094                
095                @SuppressWarnings("unchecked")
096                public List<Count> countInstances(String property, String namespace) {
097                        List<Count> ret = new ArrayList<>();
098                        String query =  "SELECT ?class count(?instance) as ?count {" 
099                                + "?instance <"+property+"> ?class ." +
100                                "FILTER (?class LIKE <"+namespace+"%>) " 
101                                + "}" +
102                                "GROUP BY ?class "
103                                + "ORDER by DESC(?count)" + "";
104                        ResultSetRewindable rsw = t.queryAsResultSet(query);
105                        final List<QuerySolution> l = ResultSetFormatter.toList(rsw);
106                        for (QuerySolution resultBinding : l) {
107                                String res = resultBinding.get("class").toString();
108                                Literal lit =  (Literal) resultBinding.get("count");
109                                
110                                ret.add(new Count(res, lit.getInt()));
111                        }
112                        System.out.println("retrieved: "+ret.size());
113                        return ret;
114                }
115        }
116