Strabon

changeset 1149:ce263b0a53c4 temporals

initial version of the modified rewritter to support temporal updates
author Konstantina Bereta <Konstantina.Bereta@di.uoa.gr>
date Tue May 07 00:45:39 2013 +0300 (2013-05-07)
parents 57ee0bc32bb0
children dc7f1c630fb5
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 18:02:27 2013 +0300
     1.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Tue May 07 00:45:39 2013 +0300
     1.3 @@ -355,17 +355,9 @@
     1.4  				//s p o t  --becomes--> GRAPH ?g(numOfQuadruples) {s p o}
     1.5  				//                      ?g(numOfQuadruples) strdf:hasValidTime t
     1.6  				
     1.7 -				//About to use rewriting mechanism in updates too: 
     1.8 -				/*example: insert data { s p o t} is translated into: 
     1.9 -				 * 
    1.10 -				 * insert data {graph <g> {s p o} }
    1.11 -				 * 
    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 +	
    1.20  				newQueryString+="\n GRAPH "+graphVariable+numOfQuadruples+" { " +token[0]+" "+token[1]+" "+token[2]+" .}\n";
    1.21  				newQueryString+=graphVariable+numOfQuadruples+" <http://strdf.di.uoa.gr/ontology#hasValidTime>";
    1.22  				
     2.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/utils.java	Wed May 01 18:02:27 2013 +0300
     2.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/utils.java	Tue May 07 00:45:39 2013 +0300
     2.3 @@ -17,6 +17,7 @@
     2.4  		int numOfQuadruples=0;
     2.5  		int startIndex=0;
     2.6  		
     2.7 +		StringBuffer whereClauses = new StringBuffer(2048);
     2.8  		NQuadsParser parser = new NQuadsParser();
     2.9  		
    2.10  		try
    2.11 @@ -35,6 +36,8 @@
    2.12  			Matcher matcher = pattern.matcher(queryString);
    2.13  			String oldQueryString=matcher.replaceAll("");
    2.14  			
    2.15 +			String updateREGEX= "(where([\\s])*\\{(.)*\\})";
    2.16 +	
    2.17  			//check whether the query contains quadruples	
    2.18  			String Word="((\\w)|(\\p{InGreek}))+";
    2.19  			String URI="(<([\\S])*>)|("+Word+":"+Word+")";
    2.20 @@ -48,9 +51,31 @@
    2.21  			pattern = Pattern.compile(REGEX, Pattern.DOTALL);							
    2.22  			matcher = pattern.matcher(oldQueryString);
    2.23  			
    2.24 +			Pattern updatePattern = Pattern.compile(updateREGEX, Pattern.DOTALL);
    2.25 +			Matcher updateMatcher =  updatePattern.matcher(queryString);
    2.26 +			
    2.27 +			System.out.println("oldQuerySting "+oldQueryString);
    2.28 +			while(updateMatcher.find())
    2.29 +			{
    2.30 +				String clause=oldQueryString.substring(updateMatcher.start(), updateMatcher.end()).trim();
    2.31 +				System.out.println("Clause is: "+ clause);
    2.32 +				whereClauses.append(clause);
    2.33 +			}
    2.34 +			
    2.35 +			
    2.36  			while(matcher.find())		
    2.37  			{
    2.38  				String quadruple=oldQueryString.substring(matcher.start(), matcher.end()).trim();
    2.39 +				
    2.40 +				boolean inWhere;
    2.41 +				if( whereClauses.indexOf(quadruple) <0 )
    2.42 +				{
    2.43 +					inWhere=false;
    2.44 +				}
    2.45 +				else
    2.46 +				{
    2.47 +					inWhere = true;
    2.48 +				}
    2.49  				numOfQuadruples++;
    2.50  				
    2.51  				newQueryString+=oldQueryString.substring(startIndex, matcher.start());
    2.52 @@ -71,15 +96,38 @@
    2.53  				System.out.println("predicate"+ token[1]);
    2.54  				System.out.println("object:"+ token[2]);
    2.55  				System.out.println("graph:"+ token[3]);
    2.56 -				String graph =  "<"+parser.createValidTimeURI(token[3]).toString()+">";
    2.57 -				System.out.println("The graph URI = "+ graph.toString());
    2.58 -				newQueryString+="\n GRAPH "+graph+" { " +token[0]+" "+token[1]+" "+token[2]+" .}\n";
    2.59 -				//newQueryString+=graphVariable+numOfQuadruples+" <http://strdf.di.uoa.gr/ontology#hasValidTime>";
    2.60 +				
    2.61 +				int i=3;
    2.62 +				if(!isVar(token[3]) && inWhere==false) //the forth element is a literal representation of valid time
    2.63 +				{
    2.64 +					System.out.println("quad constant in where clause");
    2.65 +					String graph =  "<"+parser.createValidTimeURI(token[3]).toString()+">";
    2.66 +					System.out.println("The graph URI = "+ graph.toString());
    2.67 +					newQueryString+="\n GRAPH "+graph+" { " +token[0]+" "+token[1]+" "+token[2]+" .}\n";
    2.68 +					i=5;
    2.69 +	
    2.70 +				}
    2.71 +				else if(isVar(token[3]) && inWhere==false) //the fourth element is a temporal variable in an update clause
    2.72 +				{
    2.73 +					//String addedPattern = graphVariable+numOfQuadruples+ " <http://strdf.di.uoa.gr/ontology#hasValidTime>"+ token[3];
    2.74 +					newQueryString+="\n GRAPH "+graphVariable+numOfQuadruples+" { " +token[0]+" "+token[1]+" "+token[2]+" .}\n";
    2.75 +					i=5;
    2.76 +				}
    2.77 +				else if(isVar(token[3]) && inWhere==true) //temporal variable in where clause
    2.78 +				{
    2.79 +					newQueryString+="\n GRAPH "+graphVariable+numOfQuadruples+" { " +token[0]+" "+token[1]+" "+token[2]+" .}\n";
    2.80 +					newQueryString+=graphVariable+numOfQuadruples+" <http://strdf.di.uoa.gr/ontology#hasValidTime>";
    2.81 +					i=3;
    2.82 +				}
    2.83 +				
    2.84  				
    2.85  				//add the rest tokens
    2.86 -				for( int i=5; i<token.length; i++)
    2.87 +				while( i<token.length)
    2.88 +				{
    2.89  					newQueryString+=" "+token[i];
    2.90 -			}
    2.91 +					i++;
    2.92 +					
    2.93 +				}
    2.94  			
    2.95  			if(numOfQuadruples==0)
    2.96  			{
    2.97 @@ -92,12 +140,23 @@
    2.98  				logger.info("\n\nNew QueryString:\n {}\n\n", newQueryString);
    2.99  			}
   2.100  		}
   2.101 +		}
   2.102  		catch(Exception e)
   2.103 -		{
   2.104 +		{ 
   2.105  			logger.error("[Strabon.queryRewriting]", e);
   2.106  		}
   2.107  
   2.108  		return newQueryString;
   2.109  
   2.110  }
   2.111 +	/*
   2.112 +	 * checks whether an element of a statement pattern is a variable or not
   2.113 +	 */
   2.114 +	public static boolean isVar (String element)
   2.115 +	{
   2.116 +		if(element.startsWith("?"))
   2.117 +			return true;
   2.118 +		else
   2.119 +			return false;
   2.120 +	}
   2.121  }