001    package org.dllearner.test;
002    
003    import com.hp.hpl.jena.query.ResultSet;
004    import com.hp.hpl.jena.query.ResultSetFormatter;
005    import com.hp.hpl.jena.sparql.engine.http.HttpQuery;
006    import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP;
007    
008    
009    
010    /**
011     * this class was submitted to Jena Bug Tracker
012     * 
013     */
014    public class JenaLongQueryTest {
015    
016            public static void main(String[] args) {
017                    String url = "http://dbpedia.openlinksw.com:8890/sparql";
018                    String defaultgraph = "http://dbpedia.org";
019                    String shortQuery = "SELECT ?predicate ?object " +
020                                    "WHERE { " +
021                                    "<http://dbpedia.org/resource/Philolaus> ?predicate ?object. "+
022                                    "FILTER( "+
023                                    "(!isLiteral(?object)) "+
024                                    "&&( !regex(str(?predicate), 'http://dbpedia.org/property/relatedInstance') ) "+
025                                    "&&( !regex(str(?predicate), 'http://dbpedia.org/property/wikiPageUsesTemplate') ) "+
026                                    "&&( !regex(str(?predicate), 'http://www.w3.org/2002/07/owl#sameAs') ) "+
027                                    "&&( !regex(str(?predicate), 'http://xmlns.com/foaf/0.1/') ) "+
028                                    "&&( !regex(str(?predicate), 'http://www.w3.org/2004/02/skos/core') ) "+
029                                    "&&( !regex(str(?object), 'http://xmlns.com/foaf/0.1/') ) "+
030                                    "&&( !regex(str(?object), 'http://upload.wikimedia.org/wikipedia') ) "+
031                                    "&&( !regex(str(?object), 'http://www4.wiwiss.fu-berlin.de/flickrwrappr') ) "+
032                                    "&&( !regex(str(?object), 'http://dbpedia.org/resource/Template') ) "+
033                                    "&&( !regex(str(?object), 'http://upload.wikimedia.org/wikipedia/commons') ) "+
034                                    "&&( !regex(str(?object), 'http://www.w3.org/2006/03/wn/wn20/instances/synset') ) "+
035                                    "&&( !regex(str(?object), 'http://dbpedia.org/resource/Category:') ) "+
036                                    "&&( !regex(str(?object), 'http://www.w3.org/2004/02/skos/core') ) "+
037                                    "&&( !regex(str(?object), 'http://www.geonames.org') )).} ";
038                    
039                    String longQuery="SELECT ?predicate ?object " +
040                                    "WHERE { " +
041                                    "<http://dbpedia.org/resource/Philolaus> ?predicate ?object. "+
042                                    "FILTER( "+
043                                    "(!isLiteral(?object)) "+
044                                    "&&( !regex(str(?predicate), 'http://dbpedia.org/property/relatedInstance') ) "+
045                                    "&&( !regex(str(?predicate), 'http://dbpedia.org/property/wikiPageUsesTemplate') ) "+
046                                    "&&( !regex(str(?predicate), 'http://www.w3.org/2002/07/owl#sameAs') ) "+
047                                    "&&( !regex(str(?predicate), 'http://xmlns.com/foaf/0.1/') ) "+
048                                    "&&( !regex(str(?predicate), 'http://www.w3.org/2004/02/skos/core') ) "+
049                                    "&&( !regex(str(?object), 'http://xmlns.com/foaf/0.1/') ) "+
050                                    "&&( !regex(str(?object), 'http://upload.wikimedia.org/wikipedia') ) "+
051                                    "&&( !regex(str(?object), 'http://www4.wiwiss.fu-berlin.de/flickrwrappr') ) "+
052                                    "&&( !regex(str(?object), 'http://dbpedia.org/resource/Template') ) "+
053                                    "&&( !regex(str(?object), 'http://upload.wikimedia.org/wikipedia/commons') ) "+
054                                    "&&( !regex(str(?object), 'http://www.w3.org/2006/03/wn/wn20/instances/synset') ) "+
055                                    "&&( !regex(str(?object), 'http://www.w3.org/2006/03/wn/wn20/instances/synset') ) "+
056                                    "&&( !regex(str(?object), 'http://www.w3.org/2006/03/wn/wn20/instances/synset') ) "+
057                                    "&&( !regex(str(?object), 'http://www.w3.org/2006/03/wn/wn20/instances/synset') ) "+
058                                    "&&( !regex(str(?object), 'http://www.w3.org/2006/03/wn/wn20/instances/synset') ) "+
059                                    "&&( !regex(str(?object), 'http://www.w3.org/2006/03/wn/wn20/instances/synset') ) "+
060                                    "&&( !regex(str(?object), 'http://dbpedia.org/resource/Category:') ) "+
061                                    "&&( !regex(str(?object), 'http://www.w3.org/2004/02/skos/core') ) "+
062                                    "&&( !regex(str(?object), 'http://www.geonames.org') )).} ";
063                    
064                    ResultSet rs;
065                    String xml;
066                    QueryEngineHTTP queryExecution;
067                    HttpQuery.urlLimit = 3*1024 ;
068                    queryExecution = new QueryEngineHTTP(url, shortQuery);
069                    queryExecution.addDefaultGraph(defaultgraph);
070                    rs = queryExecution.execSelect();
071                    xml = ResultSetFormatter.asXMLString(rs);
072                    System.out.println("Short Query ResultSet length: "+xml.length());
073                    
074                    try{
075                    queryExecution=new QueryEngineHTTP(url,longQuery);
076                    queryExecution.addDefaultGraph(defaultgraph);
077                    
078                    //queryExecution.getContext().
079                    rs = queryExecution.execSelect();
080                    xml = ResultSetFormatter.asXMLString(rs);
081                    System.out.println("Long Query ResultSet length: "+xml.length()+"\n");
082                    System.out.println("Long query XML: "+xml);
083                    }catch (Exception e) {e.printStackTrace();}
084                    //
085                    
086                    String queryWithIncreasingLength="";
087                    for (int i = 0; i < 100; i++) {
088                            queryWithIncreasingLength = makeQueryString ( i);
089                            queryExecution=new QueryEngineHTTP(url,queryWithIncreasingLength);
090                            queryExecution.addDefaultGraph(defaultgraph);
091                            rs = queryExecution.execSelect();
092                            System.out.println("Query with length: "+queryWithIncreasingLength.length()
093                                            +" produces ResultSet length: "+ResultSetFormatter.toList(rs).size());
094                    }
095                    
096            }
097            
098            public static String makeQueryString ( int charsToAdd){
099                    String add="";
100                    
101                    for (int i = 0; i <charsToAdd; i++) {
102                            add+="0123456789"+"0123456789";
103                    }
104                    // NOTE THE LAST LINE
105                    String query = "SELECT ?predicate ?object " +
106                    "WHERE { " +
107                    "<http://dbpedia.org/resource/Philolaus> ?predicate ?object. "+
108                    "FILTER( "+
109                    "(!isLiteral(?object)) "+
110                    "&&( !regex(str(?predicate), 'http://dbpedia.org/property/relatedInstance') ) "+
111                    "&&( !regex(str(?predicate), 'http://dbpedia.org/property/wikiPageUsesTemplate') ) "+
112                    "&&( !regex(str(?predicate), 'http://www.w3.org/2002/07/owl#sameAs') ) "+
113                    "&&( !regex(str(?predicate), 'http://xmlns.com/foaf/0.1/') ) "+
114                    "&&( !regex(str(?predicate), 'http://www.w3.org/2004/02/skos/core') ) "+
115                    "&&( !regex(str(?object), 'http://xmlns.com/foaf/0.1/') ) "+
116                    "&&( !regex(str(?object), 'http://upload.wikimedia.org/wikipedia') ) "+
117                    "&&( !regex(str(?object), 'http://www4.wiwiss.fu-berlin.de/flickrwrappr') ) "+
118                    "&&( !regex(str(?object), 'http://dbpedia.org/resource/Template') ) "+
119                    "&&( !regex(str(?object), 'http://upload.wikimedia.org/wikipedia/commons') ) "+
120                    "&&( !regex(str(?object), 'http://www.w3.org/2006/03/wn/wn20/instances/synset') ) "+
121                    "&&( !regex(str(?object), 'http://dbpedia.org/resource/Category:') ) "+
122                    "&&( !regex(str(?object), 'http://www.w3.org/2004/02/skos/core') ) "+
123                    "&&( !regex(str(?object), 'http://www.geonames.org"+add+"') )).} ";
124                    
125                    
126                    return query;
127            }
128            
129            
130    
131    }