Strabon

changeset 1345:4b7dbc54ae9d

solve Bug #62
author Dimitrianos Savva <dimis@di.uoa.gr>
date Tue Jul 01 18:43:44 2014 +0300 (2014-07-01)
parents 168b89aaa51b
children cd3fec856743
files evaluation/src/main/java/org/openrdf/query/algebra/evaluation/util/StSPARQLOrderComparator.java generaldb/src/main/java/org/openrdf/sail/generaldb/evaluation/GeneralDBEvaluation.java testsuite/src/test/java/eu/earthobservatory/testsuite/utils/TemplateTest.java testsuite/src/test/java/eu/earthobservatory/testsuite/utils/Utils.java
line diff
     1.1 --- a/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/util/StSPARQLOrderComparator.java	Fri Jun 27 18:55:57 2014 +0300
     1.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/util/StSPARQLOrderComparator.java	Tue Jul 01 18:43:44 2014 +0300
     1.3 @@ -14,12 +14,14 @@
     1.4  import org.openrdf.model.Value;
     1.5  import org.openrdf.query.BindingSet;
     1.6  import org.openrdf.query.QueryEvaluationException;
     1.7 +import org.openrdf.query.algebra.BindingSetAssignment;
     1.8  import org.openrdf.query.algebra.FunctionCall;
     1.9  import org.openrdf.query.algebra.Order;
    1.10  import org.openrdf.query.algebra.OrderElem;
    1.11  import org.openrdf.query.algebra.ValueExpr;
    1.12  import org.openrdf.query.algebra.Var;
    1.13  import org.openrdf.query.algebra.evaluation.EvaluationStrategy;
    1.14 +import org.openrdf.query.algebra.evaluation.QueryBindingSet;
    1.15  import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException;
    1.16  import org.slf4j.Logger;
    1.17  import org.slf4j.LoggerFactory;
    1.18 @@ -28,6 +30,7 @@
    1.19  
    1.20  /**
    1.21   * @author Manos Karpathiotakis <mk@di.uoa.gr>
    1.22 + * @author Dimitrianos Savva <dimis@di.uoa.gr>
    1.23   */
    1.24  public class StSPARQLOrderComparator implements Comparator<BindingSet> {
    1.25  
    1.26 @@ -63,10 +66,23 @@
    1.27  						//I know it is a var cause I 'planted' it earlier
    1.28  						Var lastArg = (Var) fc.getArgs().get(1);
    1.29  						String bindingName = lastArg.getName();
    1.30 -
    1.31 -						v1 = o1.getValue(bindingName);
    1.32 -						v2 = o2.getValue(bindingName);
    1.33 +						
    1.34 +						//avoid function encapsulation @see GeneralDBSelectQueryOptimizer meet(Order)
    1.35 +						if(bindingName.startsWith("-mbb-"))
    1.36 +						{
    1.37 +							//get the encapsulated function 
    1.38 +							v1=evaluate(expr.getArgs().get(0), o1);
    1.39 +							v2=evaluate(expr.getArgs().get(0), o2);
    1.40 +						}
    1.41 +						else
    1.42 +						{
    1.43 +							v1 = o1.getValue(bindingName);
    1.44 +							v2 = o2.getValue(bindingName);
    1.45 +						}
    1.46 +						
    1.47 +						
    1.48  						//XXX unfinished
    1.49 +						
    1.50  						int compare = cmp.compare(v1, v2);
    1.51  
    1.52  						if (compare != 0) {
     2.1 --- a/generaldb/src/main/java/org/openrdf/sail/generaldb/evaluation/GeneralDBEvaluation.java	Fri Jun 27 18:55:57 2014 +0300
     2.2 +++ b/generaldb/src/main/java/org/openrdf/sail/generaldb/evaluation/GeneralDBEvaluation.java	Tue Jul 01 18:43:44 2014 +0300
     2.3 @@ -22,6 +22,7 @@
     2.4  import org.openrdf.model.Value;
     2.5  import org.openrdf.model.impl.BooleanLiteralImpl;
     2.6  import org.openrdf.model.impl.LiteralImpl;
     2.7 +import org.openrdf.model.impl.NumericLiteralImpl;
     2.8  import org.openrdf.model.impl.URIImpl;
     2.9  import org.openrdf.query.BindingSet;
    2.10  import org.openrdf.query.Dataset;
    2.11 @@ -43,8 +44,11 @@
    2.12  import org.openrdf.query.algebra.evaluation.function.Function;
    2.13  import org.openrdf.query.algebra.evaluation.function.FunctionRegistry;
    2.14  import org.openrdf.query.algebra.evaluation.function.spatial.SpatialConstructFunc;
    2.15 +import org.openrdf.query.algebra.evaluation.function.spatial.SpatialMetricFunc;
    2.16  import org.openrdf.query.algebra.evaluation.function.spatial.SpatialRelationshipFunc;
    2.17  import org.openrdf.query.algebra.evaluation.function.spatial.StrabonPolyhedron;
    2.18 +import org.openrdf.query.algebra.evaluation.function.spatial.stsparql.metric.AreaFunc;
    2.19 +import org.openrdf.query.algebra.evaluation.function.spatial.stsparql.metric.DistanceFunc;
    2.20  import org.openrdf.query.algebra.evaluation.function.spatial.stsparql.relation.AboveFunc;
    2.21  import org.openrdf.query.algebra.evaluation.function.spatial.stsparql.relation.BelowFunc;
    2.22  import org.openrdf.query.algebra.evaluation.function.spatial.stsparql.relation.ContainsFunc;
    2.23 @@ -198,7 +202,8 @@
    2.24  		//		{
    2.25  		//			var.setName(var.getName().replace("-mbbVar-",""));
    2.26  		//		}
    2.27 -
    2.28 +		
    2.29 +		
    2.30  		//Case met when evaluating a construct function inside an aggregate 
    2.31  		if(var.getName().endsWith("?spatial"))
    2.32  		{
    2.33 @@ -268,8 +273,9 @@
    2.34  
    2.35  		// evaluate first argument
    2.36  		leftResult = evaluate(left, bindings);
    2.37 -
    2.38 +		
    2.39  		// function call with 2 arguments, evaluate the second one now
    2.40 +		// see distance function as example
    2.41  		if ( fc.getArgs().size() == 2 )
    2.42  		{
    2.43  			ValueExpr right = fc.getArgs().get(1);
    2.44 @@ -278,7 +284,101 @@
    2.45  
    2.46  		// having evaluated the arguments of the function, evaluate the function
    2.47  		try {
    2.48 -			if ( function instanceof SpatialConstructFunc ) {
    2.49 +			if(function instanceof SpatialMetricFunc)
    2.50 +			{
    2.51 +				double funcResult = 0;
    2.52 +				Geometry leftGeom = null;
    2.53 +				Geometry rightGeom = null;
    2.54 +				if(leftResult instanceof StrabonPolyhedron)
    2.55 +				{
    2.56 +					leftGeom = ((StrabonPolyhedron) leftResult).getGeometry();
    2.57 +				}
    2.58 +				else if(leftResult instanceof GeneralDBPolyhedron)
    2.59 +				{
    2.60 +					leftGeom = ((GeneralDBPolyhedron) leftResult).getPolyhedron().getGeometry();
    2.61 +				}
    2.62 +				else if(leftResult instanceof Literal)
    2.63 +				{	
    2.64 +					/**
    2.65 +					 * Duplicate work done here in order to retain the literal's datatype...
    2.66 +					 * Instead of only utilizing StrabonPolyhedron items, I converted them to Literals
    2.67 +					 * in order to have them appear in Select Clause along with the appropriate datatype.
    2.68 +					 */
    2.69 +					leftGeom = new StrabonPolyhedron(((Literal) leftResult).getLabel()).getGeometry();
    2.70 +					int sridPosition = ((Literal) leftResult).getLabel().indexOf(';');
    2.71 +					//Default case
    2.72 +					if(sridPosition == -1)
    2.73 +					{
    2.74 +						leftGeom.setSRID(GeoConstants.defaultSRID);
    2.75 +					}
    2.76 +					else
    2.77 +					{
    2.78 +						sridPosition = ((Literal) leftResult).getLabel().lastIndexOf('/');
    2.79 +						int srid = Integer.parseInt(((Literal) leftResult).getLabel().substring(sridPosition+1));
    2.80 +						leftGeom.setSRID(srid);
    2.81 +					}
    2.82 +				}
    2.83 +				else
    2.84 +				{	//SHOULD NEVER REACH THIS CASE!
    2.85 +					return null;
    2.86 +				}
    2.87 +				if(rightResult != null)
    2.88 +				{
    2.89 +				if(rightResult instanceof StrabonPolyhedron)
    2.90 +				{
    2.91 +					rightGeom = ((StrabonPolyhedron) rightResult).getGeometry();
    2.92 +				}
    2.93 +				else if(rightResult instanceof GeneralDBPolyhedron)
    2.94 +				{
    2.95 +					rightGeom = ((GeneralDBPolyhedron) rightResult).getPolyhedron().getGeometry();
    2.96 +				}
    2.97 +				else if(rightResult instanceof Literal)
    2.98 +				{	
    2.99 +					/**
   2.100 +					 * Duplicate work done here in order to retain the literal's datatype...
   2.101 +					 * Instead of only utilizing StrabonPolyhedron items, I converted them to Literals
   2.102 +					 * in order to have them appear in Select Clause along with the appropriate datatype.
   2.103 +					 */
   2.104 +					rightGeom = new StrabonPolyhedron(((Literal) rightResult).getLabel()).getGeometry();
   2.105 +					int sridPosition = ((Literal) rightResult).getLabel().indexOf(';');
   2.106 +					//Default case
   2.107 +					if(sridPosition == -1)
   2.108 +					{
   2.109 +						rightGeom.setSRID(GeoConstants.defaultSRID);
   2.110 +					}
   2.111 +					else
   2.112 +					{
   2.113 +						sridPosition = ((Literal) rightResult).getLabel().lastIndexOf('/');
   2.114 +						int srid = Integer.parseInt(((Literal) rightResult).getLabel().substring(sridPosition+1));
   2.115 +						rightGeom.setSRID(srid);
   2.116 +					}
   2.117 +				}
   2.118 +				else
   2.119 +				{	//SHOULD NEVER REACH THIS CASE!
   2.120 +					return null;
   2.121 +				}
   2.122 +				}
   2.123 +				else
   2.124 +				{
   2.125 +					//it's ok, maybe this is a non-binary function!
   2.126 +				}
   2.127 +				
   2.128 +				
   2.129 +				if(function instanceof AreaFunc)
   2.130 +				{
   2.131 +					funcResult = leftGeom.getArea();
   2.132 +				}
   2.133 +				if(function instanceof DistanceFunc)
   2.134 +				{
   2.135 +					int targetSRID = leftGeom.getSRID();
   2.136 +					int sourceSRID = rightGeom.getSRID();
   2.137 +					Geometry rightConverted = JTSWrapper.getInstance().transform(rightGeom, sourceSRID, targetSRID);
   2.138 +					funcResult = leftGeom.distance(rightConverted);
   2.139 +				}
   2.140 +				return org.openrdf.model.impl.ValueFactoryImpl.getInstance().createLiteral(funcResult);
   2.141 +				
   2.142 +			}
   2.143 +			else if ( function instanceof SpatialConstructFunc ) {
   2.144  				return spatialConstructPicker(function, leftResult, rightResult);
   2.145  
   2.146  			}  else if(function instanceof SpatialRelationshipFunc)	{
     3.1 --- a/testsuite/src/test/java/eu/earthobservatory/testsuite/utils/TemplateTest.java	Fri Jun 27 18:55:57 2014 +0300
     3.2 +++ b/testsuite/src/test/java/eu/earthobservatory/testsuite/utils/TemplateTest.java	Tue Jul 01 18:43:44 2014 +0300
     3.3 @@ -25,9 +25,10 @@
     3.4   * 2) stores a dataset
     3.5   * 3) poses a query
     3.6   * 4) checks if the results of the query are the expected 
     3.7 + * 	  If you use ORDER BY do NOT use this class.  
     3.8   * 5) drops the database
     3.9   * 
    3.10 - * @author Panayiotis Smeros <psmeros@di.uoa.gr
    3.11 + * @author Panayiotis Smeros <psmeros@di.uoa.gr>
    3.12   */
    3.13  public abstract class TemplateTest
    3.14  {	
     4.1 --- a/testsuite/src/test/java/eu/earthobservatory/testsuite/utils/Utils.java	Fri Jun 27 18:55:57 2014 +0300
     4.2 +++ b/testsuite/src/test/java/eu/earthobservatory/testsuite/utils/Utils.java	Tue Jul 01 18:43:44 2014 +0300
     4.3 @@ -49,6 +49,7 @@
     4.4   * A class with useful methods for the tests.
     4.5   * 
     4.6   * @author Panayiotis Smeros <psmeros@di.uoa.gr>
     4.7 + * @author Dimitrianos Savva <dimis@di.uoa.gr>
     4.8   */
     4.9  public class Utils
    4.10  {
    4.11 @@ -141,8 +142,13 @@
    4.12  	    	strabon.storeInRepo(datasetFile, "NQUADS", inference);
    4.13  	}
    4.14  	
    4.15 +	
    4.16  	public static void testQuery(String queryFile, String resultsFile) throws IOException, MalformedQueryException, QueryEvaluationException, TupleQueryResultHandlerException, URISyntaxException, QueryResultParseException, UnsupportedQueryResultFormatException
    4.17  	{
    4.18 +		testQuery(queryFile, resultsFile, false); //dimis
    4.19 +	}
    4.20 +	public static void testQuery(String queryFile, String resultsFile,boolean orderOn) throws IOException, MalformedQueryException, QueryEvaluationException, TupleQueryResultHandlerException, URISyntaxException, QueryResultParseException, UnsupportedQueryResultFormatException
    4.21 +	{
    4.22  		ByteArrayOutputStream resultsStream = new ByteArrayOutputStream();
    4.23  		String query = FileUtils.readFileToString(new File(Utils.class.getResource(prefixesFile).toURI()))+"\n"+FileUtils.readFileToString(new File(Utils.class.getResource(queryFile).toURI()));
    4.24  		
    4.25 @@ -180,17 +186,19 @@
    4.26  		
    4.27  		assertFalse("Results are not the expected. QueryFile: "+queryFile, expectedResults.hasNext() || actualResults.hasNext());
    4.28  		
    4.29 -		//Sort bindings alphabetically
    4.30 -		Collections.sort(eBindingList);
    4.31 -		Collections.sort(aBindingList);
    4.32 -		
    4.33 +		if(!orderOn)
    4.34 +		{
    4.35 +			//Sort bindings alphabetically
    4.36 +			Collections.sort(eBindingList);
    4.37 +			Collections.sort(aBindingList);
    4.38 +		}
    4.39  		//Check bindings one by one
    4.40  		Iterator<String> eBindingListIterator = eBindingList.iterator();
    4.41  		Iterator<String> aBindingListIterator = aBindingList.iterator();
    4.42  
    4.43  		while(eBindingListIterator.hasNext() && aBindingListIterator.hasNext())
    4.44  		{
    4.45 -			assertEquals("Results are not the expected. QueryFile: "+queryFile, eBindingListIterator.next(), aBindingListIterator.next());
    4.46 +			assertEquals("Results are not the expected. QueryFile: "+queryFile,eBindingListIterator.next(), aBindingListIterator.next() );
    4.47  		}
    4.48  		
    4.49  		actualResults.close();