Strabon

changeset 1148:57ee0bc32bb0 temporals

added utils class that contains method for rewriting update queries that contain quads
author Konstantina Bereta <Konstantina.Bereta@di.uoa.gr>
date Wed May 01 18:02:27 2013 +0300 (2013-05-01)
parents b739b7f99c4d
children ce263b0a53c4
files runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java runtime/src/main/java/eu/earthobservatory/runtime/generaldb/utils.java
line diff
     1.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Wed May 01 14:05:20 2013 +0300
     1.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Wed May 01 18:02:27 2013 +0300
     1.3 @@ -354,7 +354,18 @@
     1.4  				//tokenize quadruples and convert them to triples:
     1.5  				//s p o t  --becomes--> GRAPH ?g(numOfQuadruples) {s p o}
     1.6  				//                      ?g(numOfQuadruples) strdf:hasValidTime t
     1.7 +				
     1.8 +				//About to use rewriting mechanism in updates too: 
     1.9 +				/*example: insert data { s p o t} is translated into: 
    1.10 +				 * 
    1.11 +				 * insert data {graph <g> {s p o} }
    1.12 +				 * 
    1.13 +				 */
    1.14  				String[] token = quadruple.split("(\\s)+");
    1.15 +				System.out.println("subject:"+ token[0]);
    1.16 +				System.out.println("predicate"+ token[1]);
    1.17 +				System.out.println("object:"+ token[2]);
    1.18 +				System.out.println("graph:"+ token[3]);
    1.19  				newQueryString+="\n GRAPH "+graphVariable+numOfQuadruples+" { " +token[0]+" "+token[1]+" "+token[2]+" .}\n";
    1.20  				newQueryString+=graphVariable+numOfQuadruples+" <http://strdf.di.uoa.gr/ontology#hasValidTime>";
    1.21  				
    1.22 @@ -412,7 +423,11 @@
    1.23  		Update update = null;
    1.24  
    1.25  		try {
    1.26 -			update = con.prepareUpdate(QueryLanguage.SPARQL, updateString);
    1.27 +			
    1.28 +			String reWrittenUpdate = utils.updateRewriting(updateString);
    1.29 +			System.out.println("REWRITTEN UPDATE= "+ reWrittenUpdate);
    1.30 +			
    1.31 +			update = con.prepareUpdate(QueryLanguage.SPARQL, reWrittenUpdate);
    1.32  			
    1.33  		} catch (RepositoryException e) {
    1.34  			logger.error("[Strabon.update]", e);
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/utils.java	Wed May 01 18:02:27 2013 +0300
     2.3 @@ -0,0 +1,103 @@
     2.4 +package eu.earthobservatory.runtime.generaldb;
     2.5 +
     2.6 +import java.util.regex.Matcher;
     2.7 +import java.util.regex.Pattern;
     2.8 +
     2.9 +import org.openrdf.model.Resource;
    2.10 +import org.slf4j.Logger;
    2.11 +import org.slf4j.LoggerFactory;
    2.12 +
    2.13 +public class utils {
    2.14 +	
    2.15 +	private static Logger logger = LoggerFactory.getLogger(eu.earthobservatory.runtime.generaldb.Strabon.class);
    2.16 +	
    2.17 +	public static String updateRewriting(String queryString) 
    2.18 +	{
    2.19 +		String newQueryString="";
    2.20 +		int numOfQuadruples=0;
    2.21 +		int startIndex=0;
    2.22 +		
    2.23 +		NQuadsParser parser = new NQuadsParser();
    2.24 +		
    2.25 +		try
    2.26 +		{
    2.27 +			String graphVariable="?g"+(int)(Math.random()*10000);
    2.28 +			while (queryString.contains(graphVariable))
    2.29 +			{
    2.30 +				graphVariable+="1";
    2.31 +			}
    2.32 +			graphVariable+="_";
    2.33 +			
    2.34 +			//remove comments from query
    2.35 +			queryString=queryString.replaceAll("\\.#", ".\n#");
    2.36 +		    String REGEX = "((^(\\s)*#)|((\\s)+#)).*$";
    2.37 +			Pattern pattern = Pattern.compile(REGEX, Pattern.MULTILINE);							
    2.38 +			Matcher matcher = pattern.matcher(queryString);
    2.39 +			String oldQueryString=matcher.replaceAll("");
    2.40 +			
    2.41 +			//check whether the query contains quadruples	
    2.42 +			String Word="((\\w)|(\\p{InGreek}))+";
    2.43 +			String URI="(<([\\S])*>)|("+Word+":"+Word+")";
    2.44 +			String Literal="\".*\"(\\^\\^"+URI+")?";
    2.45 +			String Variable="\\?"+Word;
    2.46 +			
    2.47 +			String SPOT="(("+URI+")|("+Literal+")|("+Variable+"))";
    2.48 +			REGEX="("+SPOT+"(\\s)+){3}"+SPOT+"(\\s)*[\\}\\.]";
    2.49 +			
    2.50 +
    2.51 +			pattern = Pattern.compile(REGEX, Pattern.DOTALL);							
    2.52 +			matcher = pattern.matcher(oldQueryString);
    2.53 +			
    2.54 +			while(matcher.find())		
    2.55 +			{
    2.56 +				String quadruple=oldQueryString.substring(matcher.start(), matcher.end()).trim();
    2.57 +				numOfQuadruples++;
    2.58 +				
    2.59 +				newQueryString+=oldQueryString.substring(startIndex, matcher.start());
    2.60 +				startIndex=matcher.end();
    2.61 +	
    2.62 +				//tokenize quadruples and convert them to triples:
    2.63 +				//s p o t  --becomes--> GRAPH ?g(numOfQuadruples) {s p o}
    2.64 +				//                      ?g(numOfQuadruples) strdf:hasValidTime t
    2.65 +				
    2.66 +				//About to use rewriting mechanism in updates too: 
    2.67 +				/*example: insert data { s p o t} is translated into: 
    2.68 +				 * 
    2.69 +				 * insert data {graph <g> {s p o} }
    2.70 +				 * 
    2.71 +				 */
    2.72 +				String[] token = quadruple.split("(\\s)+");
    2.73 +				System.out.println("subject:"+ token[0]);
    2.74 +				System.out.println("predicate"+ token[1]);
    2.75 +				System.out.println("object:"+ token[2]);
    2.76 +				System.out.println("graph:"+ token[3]);
    2.77 +				String graph =  "<"+parser.createValidTimeURI(token[3]).toString()+">";
    2.78 +				System.out.println("The graph URI = "+ graph.toString());
    2.79 +				newQueryString+="\n GRAPH "+graph+" { " +token[0]+" "+token[1]+" "+token[2]+" .}\n";
    2.80 +				//newQueryString+=graphVariable+numOfQuadruples+" <http://strdf.di.uoa.gr/ontology#hasValidTime>";
    2.81 +				
    2.82 +				//add the rest tokens
    2.83 +				for( int i=5; i<token.length; i++)
    2.84 +					newQueryString+=" "+token[i];
    2.85 +			}
    2.86 +			
    2.87 +			if(numOfQuadruples==0)
    2.88 +			{
    2.89 +				newQueryString=queryString;
    2.90 +				logger.info("\n\nQuadruple not found\n\n");
    2.91 +			}
    2.92 +			else
    2.93 +			{
    2.94 +				newQueryString+=oldQueryString.substring(startIndex);
    2.95 +				logger.info("\n\nNew QueryString:\n {}\n\n", newQueryString);
    2.96 +			}
    2.97 +		}
    2.98 +		catch(Exception e)
    2.99 +		{
   2.100 +			logger.error("[Strabon.queryRewriting]", e);
   2.101 +		}
   2.102 +
   2.103 +		return newQueryString;
   2.104 +
   2.105 +}
   2.106 +}