Strabon

changeset 734:413b712d1ea3 temporals

Fixed a bug in queryRewriting.
author Panayiotis Smeros <psmeros@di.uoa.gr>
date Fri Nov 23 16:15:15 2012 +0200 (2012-11-23)
parents 09fc776e43be
children 7c5ed324d065
files runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java runtime/src/test/java/eu/earthobservatory/runtime/postgis/temporals/QueryRewritingTests.java runtime/src/test/java/eu/earthobservatory/runtime/postgis/temporals/TemporalSelectionTests.java
line diff
     1.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Fri Nov 23 16:08:50 2012 +0200
     1.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Fri Nov 23 16:15:15 2012 +0200
     1.3 @@ -287,7 +287,6 @@
     1.4  		
     1.5  		try
     1.6  		{
     1.7 -
     1.8  			String graphVariable="?g"+(int)(Math.random()*10000);
     1.9  			while (queryString.contains(graphVariable))
    1.10  			{
    1.11 @@ -300,44 +299,35 @@
    1.12  			Pattern pattern = Pattern.compile(REGEX, Pattern.MULTILINE);							
    1.13  			Matcher matcher = pattern.matcher(queryString);
    1.14  			String oldQueryString=matcher.replaceAll("");
    1.15 +					
    1.16 +			//check whether the query contains quadruples	
    1.17 +			String Word="((\\w)|(\\p{InGreek}))+";
    1.18 +			String URI="(<([\\S])*>)|("+Word+":"+Word+")";
    1.19 +			String Literal="\".*\"(\\^\\^"+URI+")?";
    1.20 +			String Variable="\\?"+Word;
    1.21  			
    1.22 -			//escape prefixes
    1.23 -			startIndex=oldQueryString.indexOf('{');
    1.24 -			newQueryString+=oldQueryString.substring(0, startIndex);
    1.25 -			oldQueryString=oldQueryString.substring(startIndex);
    1.26 -			startIndex=0;
    1.27 -					
    1.28 -			//check whether the query contains quadruples
    1.29 -			String SPO = "([[a-z][A-Z][?:<\\p{InGreek}]]([\\S])*)";
    1.30 -			String C = "((\"\\[.*\\]\"\\^\\^strdf:validTime)|"+SPO+")";
    1.31 -			REGEX = "("+SPO+"(\\s)+){3}"+C+"(\\s)*[\\}\\.]";
    1.32 -	
    1.33 +			String SPOT="(("+URI+")|("+Literal+")|("+Variable+"))";
    1.34 +			REGEX="("+SPOT+"(\\s)+){3}"+SPOT+"(\\s)*[\\}\\.]";
    1.35 +			
    1.36 +
    1.37  			pattern = Pattern.compile(REGEX, Pattern.DOTALL);							
    1.38  			matcher = pattern.matcher(oldQueryString);
    1.39  			
    1.40  			while(matcher.find())		
    1.41  			{
    1.42  				String quadruple=oldQueryString.substring(matcher.start(), matcher.end()).trim();
    1.43 -				
    1.44 -				REGEX = ".*[.[\\s+]](FILTER).*";
    1.45 -				pattern = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);							
    1.46 -				Matcher matcher1 = pattern.matcher(quadruple);
    1.47 -				if(matcher1.find())
    1.48 -					continue;
    1.49 -
    1.50  				numOfQuadruples++;
    1.51  				
    1.52  				newQueryString+=oldQueryString.substring(startIndex, matcher.start());
    1.53  				startIndex=matcher.end();
    1.54  	
    1.55  				//tokenize quadruples and convert them to triples:
    1.56 -				//s p o c  --becomes--> GRAPH ?g(numOfQuadruples) {s p o}
    1.57 -				//                      ?g(numOfQuadruples) strdf:hasValidTime c
    1.58 +				//s p o t  --becomes--> GRAPH ?g(numOfQuadruples) {s p o}
    1.59 +				//                      ?g(numOfQuadruples) strdf:hasValidTime t
    1.60  				String[] token = quadruple.split("(\\s)+");
    1.61  				newQueryString+="\n GRAPH "+graphVariable+numOfQuadruples+" { " +token[0]+" "+token[1]+" "+token[2]+" .}\n";
    1.62  				newQueryString+=graphVariable+numOfQuadruples+" <http://strdf.di.uoa.gr/ontology#hasValidTime>";
    1.63  				
    1.64 -	
    1.65  				//add the rest tokens
    1.66  				for( int i=3; i<token.length; i++)
    1.67  					newQueryString+=" "+token[i];
    1.68 @@ -362,12 +352,23 @@
    1.69  		return newQueryString;
    1.70  		
    1.71  		//backup
    1.72 -		//REGEX = ".*(PREFIX)|(SELECT).*";
    1.73 -		//pattern = Pattern.compile(REGEX, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);							
    1.74 -		//matcher1 = pattern.matcher(quadruple);
    1.75  		
    1.76 +		//escape prefixes
    1.77 +		//startIndex=oldQueryString.indexOf('{');
    1.78 +		//newQueryString+=oldQueryString.substring(0, startIndex);
    1.79 +		//oldQueryString=oldQueryString.substring(startIndex);
    1.80 +		//startIndex=0;
    1.81 +		
    1.82 +		//REGEX = ".*[.[\\s+]](FILTER).*";
    1.83 +		//pattern = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);							
    1.84 +		//Matcher matcher1 = pattern.matcher(quadruple);
    1.85  		//if(matcher1.find())
    1.86  		//	continue;
    1.87 +				
    1.88 +		//String SPO = "([[a-z][A-Z][?:<]]([\\S])*)";
    1.89 +		//String C = "((\"\\[.*\\]\"\\^\\^strdf:validTime)|\\?([\\S])*)";
    1.90 +		//REGEX = "("+SPO+"(\\s)+){3}"+C+"(\\s)*[\\}\\.]{1}";
    1.91 +
    1.92  		
    1.93  		//String URI = "[[a-z][A-Z][?:<\\p{InGreek}]][[a-z][A-Z][0-9][\\p{InGreek}@#$%^<>/:;\"\'.]]*";
    1.94  		//String SPO = "([[a-z][A-Z][?:<\\p{InGreek}]][\\S]*)&&(^(.PREFIX.))";
     2.1 --- a/runtime/src/test/java/eu/earthobservatory/runtime/postgis/temporals/QueryRewritingTests.java	Fri Nov 23 16:08:50 2012 +0200
     2.2 +++ b/runtime/src/test/java/eu/earthobservatory/runtime/postgis/temporals/QueryRewritingTests.java	Fri Nov 23 16:15:15 2012 +0200
     2.3 @@ -11,6 +11,8 @@
     2.4  
     2.5  
     2.6  
     2.7 +import static org.junit.Assert.assertEquals;
     2.8 +
     2.9  import java.io.IOException;
    2.10  import java.sql.SQLException;
    2.11  import org.junit.AfterClass;
    2.12 @@ -73,7 +75,8 @@
    2.13  				"FILTER(strdf:intersects(?geo1, ?geo2))."+
    2.14  				"}";
    2.15  		
    2.16 -		System.out.println(strabon.queryRewriting(query));
    2.17 +		String [] querySplit=strabon.queryRewriting(query).split("GRAPH");		
    2.18 +		assertEquals(1, querySplit.length);
    2.19  	}
    2.20  	
    2.21  
    2.22 @@ -92,8 +95,27 @@
    2.23  			"FILTER(strdf:intersects(?geo1,?geo2))." +
    2.24  			"   FILTER(strdf:afterPeriod(?t1,?t2))}";	
    2.25  		
    2.26 -		System.out.println(strabon.queryRewriting(query));
    2.27 +		String [] querySplit=strabon.queryRewriting(query).split("GRAPH");		
    2.28 +		assertEquals(3, querySplit.length);
    2.29  	}
    2.30  
    2.31  
    2.32 +	@Test
    2.33 +	public void testQueryRewriting3() throws MalformedQueryException, QueryEvaluationException, TupleQueryResultHandlerException, IOException, QueryEvaluationException
    2.34 +	{
    2.35 +	
    2.36 +		String query = 
    2.37 +			prefixes+
    2.38 +			"select ?s1 " +
    2.39 +			"where { " +
    2.40 +			"?s1 ?p1 ?o1 ?t1 ." +
    2.41 +			"?s2 ?p2 ?o2 ?t2 ." +
    2.42 +			"?x1 <http://teleios.di.uoa.gr/ontologies/noaOntology.owl#hasGeometry>    ?geo1. " +
    2.43 +			"?x2 <http://teleios.di.uoa.gr/ontologies/noaOntology.owl#hasGeometry>    ?geo2. " +
    2.44 +			"FILTER(strdf:disjoint(?geo1,?geo2))" +
    2.45 +			"FILTER(strdf:afterPeriod(?t1, ?t2)).";
    2.46 +		
    2.47 +		String [] querySplit=strabon.queryRewriting(query).split("GRAPH");		
    2.48 +		assertEquals(3, querySplit.length);
    2.49 +	}
    2.50  }
     3.1 --- a/runtime/src/test/java/eu/earthobservatory/runtime/postgis/temporals/TemporalSelectionTests.java	Fri Nov 23 16:08:50 2012 +0200
     3.2 +++ b/runtime/src/test/java/eu/earthobservatory/runtime/postgis/temporals/TemporalSelectionTests.java	Fri Nov 23 16:15:15 2012 +0200
     3.3 @@ -92,7 +92,6 @@
     3.4  			"}";
     3.5  		
     3.6  		ArrayList<String> bindings = (ArrayList<String>) strabon.query(strabon.queryRewriting(query),strabon.getSailRepoConnection());
     3.7 -		System.out.println(bindings);
     3.8  		assertEquals(9, bindings.size());
     3.9  	}
    3.10  }