001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.text.nlp.namedentity; 031 032import java.util.ArrayList; 033 034import com.hp.hpl.jena.query.Query; 035import com.hp.hpl.jena.query.QueryExecution; 036import com.hp.hpl.jena.query.QueryExecutionFactory; 037import com.hp.hpl.jena.query.QueryFactory; 038import com.hp.hpl.jena.query.QuerySolution; 039import com.hp.hpl.jena.query.ResultSet; 040 041/** 042 * A class to handle the page iteration through a set of results from a sparql endpoint query. 043 * Will increase the page size until failure, then resume on last successful page size. 044 * 045 * @author Laurence Willmore (lgw1e10@ecs.soton.ac.uk) 046 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 047 */ 048public class SparqlQueryPager { 049 private String endPoint; 050 private int increaseFactor = 5; 051 052 /** 053 * @param endPoint = URL string of endpoint 054 */ 055 public SparqlQueryPager(String endPoint){ 056 this.endPoint=endPoint; 057 } 058 059 /** 060 * Process a query in the form of a string. 061 * @param queryString = SELECT with no LIMIT or OFFSET clauses. 062 * @return List of com.hp.hpl.jena.query.QuerySolution. 063 */ 064 public ArrayList<QuerySolution> pageQuery(String queryString){ 065 int rollBacks=0; 066 ArrayList<QuerySolution> result = new ArrayList<QuerySolution>(); 067 int currentOffset = 0; 068 int currentChunkSize = 500; 069 while(true){ 070 String pageQueryString = queryString+" OFFSET "+currentOffset+" LIMIT "+currentChunkSize; 071 ArrayList<QuerySolution> subResult = fetch(pageQueryString); 072 if(subResult==null){ 073 if(rollBacks>2)return result; 074 currentChunkSize=currentChunkSize/increaseFactor; 075 rollBacks++; 076 continue; 077 } 078 else if(subResult.size()<currentChunkSize){ 079 result.addAll(subResult); 080 break; 081 } 082 else{ 083 result.addAll(subResult); 084 currentOffset+=currentChunkSize; 085 currentChunkSize*=increaseFactor; 086 } 087 } 088 return result; 089 } 090 091 private ArrayList<QuerySolution> fetch(String pageQueryString) { 092 ArrayList<QuerySolution> result = new ArrayList<QuerySolution>(); 093 Query q = QueryFactory.create(pageQueryString); 094 QueryExecution qexec =QueryExecutionFactory.sparqlService(endPoint, q); 095 ResultSet results; 096 try { 097 results = qexec.execSelect(); 098 for (; results.hasNext();) { 099 QuerySolution soln = results.nextSolution(); 100 result.add(soln); 101 } 102 } 103 catch(Exception e){ 104 e.printStackTrace(); 105 return null; 106 } 107 finally { 108 qexec.close(); 109 } 110 return result; 111 } 112 113}