001    package org.dllearner.test;
002    
003    import java.io.BufferedInputStream;
004    import java.io.IOException;
005    import java.io.InputStream;
006    import java.net.InetAddress;
007    import java.net.Socket;
008    import java.net.URLEncoder;
009    
010    import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP;
011    
012    public class JenaHTTPTest {
013            
014            static String query1="SELECT DISTINCT ?object\n"+
015            "FROM <http://dbpedia.org>\n"+
016            "WHERE { <http://dbpedia.org/resource/Leipzig> <http://www.w3.org/2000/01/rdf-schema#label> ?object}\n";
017    
018                    
019            static String query2 = "SELECT * WHERE { \n"+
020                    "<http://dbpedia.org/resource/County_Route_G7_%28California%29> ?predicate ?object. \n"+
021                    "       FILTER( \n"+
022                    "       (!isLiteral(?object))\n"+
023                    "       &&( !regex(str(?predicate), 'http://dbpedia.org/property/relatedInstance') )\n"+
024                    "       &&( !regex(str(?predicate), 'http://dbpedia.org/property/website') )\n"+
025                    "       &&( !regex(str(?predicate), 'http://dbpedia.org/property/owner') )\n"+
026                    "       &&( !regex(str(?predicate), 'http://dbpedia.org/property/wikiPageUsesTemplate') )\n"+
027                    "       &&( !regex(str(?predicate), 'http://www.w3.org/2002/07/owl#sameAs') )\n"+
028                    "       &&( !regex(str(?predicate), 'http://xmlns.com/foaf/0.1/') )\n"+
029                    "       &&( !regex(str(?predicate), 'http://dbpedia.org/property/standard') )\n"+
030                    "   &&( !regex(str(?predicate), 'http://dbpedia.org/property/wikipage') )\n"+
031                    "       &&( !regex(str(?predicate), 'http://dbpedia.org/property/reference') )\n"+
032                    "       &&( !regex(str(?predicate), 'http://www.w3.org/2004/02/skos/core') )\n"+
033                    "       &&( !regex(str(?object), 'http://xmlns.com/foaf/0.1/') )\n"+
034                    "       &&( !regex(str(?object), 'http://upload.wikimedia.org/wikipedia') )\n"+
035                    "       &&( !regex(str(?object), 'http://www4.wiwiss.fu-berlin.de/flickrwrappr') )\n"+
036                    "       &&( !regex(str(?object), 'http://dbpedia.org/resource/Template') )\n"+
037                    "       &&( !regex(str(?object), 'http://upload.wikimedia.org/wikipedia/commons') )\n"+
038                    "       &&( !regex(str(?object), 'http://www.w3.org/2006/03/wn/wn20/instances/synset') )\n"+
039                    "       &&( !regex(str(?object), 'http://dbpedia.org/resource/Category:') )\n"+
040                    "       &&( !regex(str(?object), 'http://www.w3.org/2004/02/skos/core') )\n"+
041                    "       &&( !regex(str(?object), 'http://www.geonames.org') )).}\n";
042            
043            public static void main(String[] args) throws Exception{
044                    
045                    String query=query2;
046                    double time=0;
047                    /*for (int i=0; i<101; i++)
048                    {
049                            if (i!=0) time+=JenaHTTPTest.httpQuery(query);
050                    }
051                    time=time/100;
052                    System.out.println("Durchschnittliche Zeit f�r eine Anfrage per Http-Methode: "+time);
053                    */
054                    time=0;
055                    for (int i=0; i<101; i++)
056                    {
057                            if (i%20 ==0)System.out.println(i);
058                            if (i!=0) time+=JenaHTTPTest.jenaQuery(query);
059                    }
060                    time=time/100;
061                    System.out.println("Durchschnittliche Zeit f�r eine Anfrage DBpedia: "+time);
062                    
063                    time=0;
064                    for (int i=0; i<101; i++)
065                    {
066                            if (i%20 ==0)System.out.println(i);
067                            if (i!=0) time+=JenaHTTPTest.jenaLocalQuery(query);
068                    }
069                    time=time/100;
070                    System.out.println("Durchschnittliche Zeit f�r eine Anfrage per DBpedia LOCAL: "+time);
071                    
072                    time=0;
073                    for (int i=0; i<101; i++)
074                    {
075                            if (i%20 ==0)System.out.println(i);
076                            if (i!=0) time+=JenaHTTPTest.jenaQuery(query);
077                    }
078                    time=time/100;
079                    System.out.println("Durchschnittliche Zeit f�r eine Anfrage DBpedia: "+time);
080            }
081            
082            private static double jenaQuery(String query)
083            {
084                    double start=System.currentTimeMillis();
085                    QueryEngineHTTP queryExecution=new QueryEngineHTTP("http://dbpedia.openlinksw.com:8890/sparql",query);
086                    queryExecution.addDefaultGraph("http://dbpedia.org");
087                    // Jena access to DBpedia SPARQL endpoint
088                    // ResultSet rs = 
089                    queryExecution.execSelect();
090                    double end=System.currentTimeMillis();
091                    return ((end-start)/1000);
092            }
093            
094            private static double jenaLocalQuery(String query)
095            {
096                    double start=System.currentTimeMillis();
097                    QueryEngineHTTP queryExecution=new QueryEngineHTTP("http://139.18.2.37:8890/sparql",query);
098                    queryExecution.addDefaultGraph("http://dbpedia.org");
099                    // Jena access to DBpedia SPARQL endpoint
100                    // ResultSet rs = 
101                    queryExecution.execSelect();
102                    double end=System.currentTimeMillis();
103                    return ((end-start)/1000);
104            }
105            
106            @Deprecated
107            @SuppressWarnings("all")
108            private static double httpQuery(String query) throws Exception
109            {
110                    char value[]={13,10};
111                    String cut=new String(value);
112                    String test="GET /sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=" +
113                    URLEncoder.encode(query, "UTF-8")+
114                    "&format=application%2Fsparql-results%2Bxml HTTP/1.1"+cut+
115                    "Host: localhost"+cut+
116                "Connection: close"+cut+
117                "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"+cut+
118                "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"+cut+
119                "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"+cut+
120                "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24"+cut+
121                cut;
122                    String retval="";
123                    //
124                    double start=System.currentTimeMillis();
125                    byte resp[]=null;
126                    
127                    try{
128                            Socket SparqlServer=new Socket(InetAddress.getByName("localhost"),8890);
129                            String request=test;
130                            // send request
131                            (SparqlServer.getOutputStream()).write(request.getBytes());
132            
133                            //get Response
134                            resp=readBuffer(new BufferedInputStream(SparqlServer.getInputStream()));
135                            retval=new String(resp);
136                            retval=retval.substring(retval.indexOf(cut+""+cut)+4);
137                                            
138                            SparqlServer.close();
139                    } catch (Exception e){}
140                    double end=System.currentTimeMillis();
141                    return (end-start)/1000;
142            }
143            
144            private static byte[] readBuffer(InputStream IS)
145            throws IOException{
146                    byte  buffer[] = new byte[0xffff];
147                    int nbytes=0;
148                    byte resp[]=new byte[0];
149                    while ((nbytes=IS.read(buffer))!=-1)    {
150                            byte tmp[]=new byte[resp.length+nbytes];
151                            int i=0;
152                            for (;i<resp.length;i++){
153                                    tmp[i]=resp[i];
154                            }
155                            for(int a=0;a<nbytes;a++,i++){
156                                    tmp[i]=buffer[a];
157                            }
158                            resp=tmp;
159                    }
160                    return resp;
161            }
162    }