Strabon

changeset 835:c4989f4024a6 temporals

added StrabonTemporalElement class to handle temporal elements (periods and instants in a uniform way). Fixing incompatibilities between the jtemporal and postgres temporal implementations of temporal functions
author Konstantina Bereta <Konstantina.Bereta@di.uoa.gr>
date Fri Jan 04 13:12:18 2013 +0200 (2013-01-04)
parents 734773ad75bb
children c20732e3fc22
files evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/spatial/StrabonInstant.java evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/spatial/StrabonPeriod.java generaldb/src/main/java/org/openrdf/sail/generaldb/evaluation/GeneralDBEvaluation.java resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java
line diff
     1.1 --- a/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/spatial/StrabonInstant.java	Wed Jan 02 19:45:28 2013 +0200
     1.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/spatial/StrabonInstant.java	Fri Jan 04 13:12:18 2013 +0200
     1.3 @@ -13,13 +13,18 @@
     1.4  import java.text.SimpleDateFormat;
     1.5  import java.util.GregorianCalendar;
     1.6  
     1.7 +import org.openrdf.model.URI;
     1.8 +import org.openrdf.model.impl.URIImpl;
     1.9 +import org.openrdf.query.algebra.evaluation.function.temporal.stsparql.relation.TemporalConstants;
    1.10 +
    1.11  import net.sf.jtemporal.Instant;
    1.12 +import net.sf.jtemporal.Period;
    1.13  
    1.14  /**
    1.15   * @author Konstantina Bereta <Konstantina.Bereta@di.uoa.gr>
    1.16   *
    1.17   */
    1.18 -public class StrabonInstant implements Instant{
    1.19 +public class StrabonInstant extends StrabonTemporalElement implements Instant{
    1.20  
    1.21  	/* (non-Javadoc)
    1.22  	 * @see net.sf.jtemporal.Instant#compareTo(java.lang.Object)
    1.23 @@ -31,14 +36,26 @@
    1.24  		return value;
    1.25  	}
    1.26  
    1.27 -
    1.28 +	public String stringValue(){
    1.29 +		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD'T'HH:mm:ss");
    1.30 +		return sdf.format(this.value.getTime()).toString();
    1.31 +	}
    1.32 +	public static StrabonInstant read(String instant) throws ParseException{
    1.33 +		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD'T'HH:mm:ss");
    1.34 +		GregorianCalendar cal = new GregorianCalendar();
    1.35 +		cal.setTime(sdf.parse(instant.substring(instant.indexOf('"')+1,instant.lastIndexOf('"') )));
    1.36 +		return new StrabonInstant(cal);
    1.37 +	}
    1.38  
    1.39  	public StrabonInstant(GregorianCalendar value) throws ParseException{
    1.40  		this.value = value;
    1.41  	}
    1.42  	@Override
    1.43 -	public int compareTo(Object arg0) {
    1.44 -		return this.value.compareTo((GregorianCalendar)arg0);
    1.45 +	public int compareTo(Object arg0) 
    1.46 +	{
    1.47 +		if(arg0 instanceof StrabonInstant)
    1.48 +			return this.value.compareTo(((StrabonInstant)arg0).getValue());
    1.49 +		return 0;
    1.50  	}
    1.51  
    1.52  	/* (non-Javadoc)
    1.53 @@ -59,4 +76,13 @@
    1.54  		return false;
    1.55  	}
    1.56  
    1.57 +	/* (non-Javadoc)
    1.58 +	 * @see org.openrdf.query.algebra.evaluation.function.spatial.StrabonTemporalElement#setDatatype(org.openrdf.model.URI)
    1.59 +	 */
    1.60 +	@Override
    1.61 +	public void setDatatype(URI datatype) {
    1.62 +		
    1.63 +		this.setDatatype(new URIImpl(TemporalConstants.INSTANT));
    1.64 +	}
    1.65 +
    1.66  }
     2.1 --- a/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/spatial/StrabonPeriod.java	Wed Jan 02 19:45:28 2013 +0200
     2.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/spatial/StrabonPeriod.java	Fri Jan 04 13:12:18 2013 +0200
     2.3 @@ -17,9 +17,13 @@
     2.4  import java.util.GregorianCalendar;
     2.5  
     2.6  import net.sf.jtemporal.Instant;
     2.7 +
     2.8  import net.sf.jtemporal.Period;
     2.9  
    2.10 +import org.openrdf.model.URI;
    2.11  import org.openrdf.model.Value;
    2.12 +import org.openrdf.model.impl.URIImpl;
    2.13 +import org.openrdf.query.algebra.evaluation.function.temporal.stsparql.relation.TemporalConstants;
    2.14  
    2.15  
    2.16  /**
    2.17 @@ -29,7 +33,7 @@
    2.18   * @author Konstantina Bereta <Konstantina.Bereta@di.uoa.gr>
    2.19   *
    2.20   */
    2.21 -public class StrabonPeriod implements Value {
    2.22 +public class StrabonPeriod extends StrabonTemporalElement implements Value {
    2.23  	
    2.24  	private Period period;
    2.25  
    2.26 @@ -39,17 +43,41 @@
    2.27  	}
    2.28  	public StrabonPeriod(String period) throws ParseException
    2.29  	{
    2.30 -		int i = period.indexOf('[');
    2.31 -		int j = period.indexOf(')');
    2.32 -		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
    2.33 -		GregorianCalendar startCal = new GregorianCalendar();
    2.34 -		GregorianCalendar endCal = new GregorianCalendar();
    2.35 -		startCal.setTime(sdf.parse(period.substring(period.indexOf('[')+1,period.indexOf(',') )));
    2.36 -		endCal.setTime(sdf.parse(period.substring(period.indexOf(',')+1,period.indexOf(')') )));
    2.37 -		Instant start = new StrabonInstant(startCal);
    2.38 -		Instant end = new StrabonInstant(endCal);
    2.39 -		this.period = new Period(start, end);
    2.40 +		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD'T'HH:mm:ss");
    2.41 +		if(period.contains(","))
    2.42 +		{
    2.43 +			GregorianCalendar startCal = new GregorianCalendar();
    2.44 +			GregorianCalendar endCal = new GregorianCalendar();
    2.45 +			startCal.setTime(sdf.parse(period.substring(period.indexOf('[')+1,period.indexOf(',') )));
    2.46 +			endCal.setTime(sdf.parse(period.substring(period.indexOf(',')+1,period.indexOf(')') )));
    2.47 +			StrabonInstant start = new StrabonInstant(startCal);
    2.48 +			StrabonInstant end = new StrabonInstant(endCal);
    2.49 +			this.period = new Period(start, end);
    2.50 +
    2.51 +		}
    2.52 +	
    2.53  	}
    2.54 +	
    2.55 +	public StrabonPeriod(String period1, String period2) throws ParseException
    2.56 +	{
    2.57 +		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD'T'HH:mm:ss");
    2.58 +	
    2.59 +			GregorianCalendar startCal = new GregorianCalendar();
    2.60 +			GregorianCalendar endCal = new GregorianCalendar();
    2.61 +			startCal.setTime(sdf.parse(period1.substring(period1.indexOf('"')+1,period1.lastIndexOf('"') )));
    2.62 +			endCal.setTime(sdf.parse(period2.substring(period2.indexOf('"')+1,period2.lastIndexOf('"') )));
    2.63 +			StrabonInstant start = new StrabonInstant(startCal);
    2.64 +			StrabonInstant end = new StrabonInstant(endCal);
    2.65 +			this.period = new Period(start, end);
    2.66 +
    2.67 +	}
    2.68 +	
    2.69 +	public void setDatatype(URI datatype) {
    2.70 +		
    2.71 +		this.setDatatype(new URIImpl(TemporalConstants.PERIOD));
    2.72 +	}
    2.73 +	
    2.74 +	
    2.75  	public Period getPeriod() {
    2.76  		return period;
    2.77  	}
    2.78 @@ -69,8 +97,7 @@
    2.79  	@Override
    2.80  	public String stringValue() 
    2.81  	{
    2.82 -		// TODO Auto-generated method stub
    2.83 -		return period.toString();
    2.84 +		return period.toString().replace("Period:(","[");
    2.85  	}
    2.86  
    2.87  	public static StrabonPeriod union(StrabonPeriod A, StrabonPeriod B)
    2.88 @@ -107,7 +134,7 @@
    2.89  	{
    2.90  		return A.getPeriod().compareTo(B.getPeriod());
    2.91  	}
    2.92 -	public static boolean contains(StrabonPeriod A, Instant B)
    2.93 +	public static boolean contains(StrabonPeriod A, StrabonInstant B)
    2.94  	{
    2.95  		return A.getPeriod().contains(B);
    2.96  	}
    2.97 @@ -135,7 +162,7 @@
    2.98  	{
    2.99  		return A.getPeriod().overlaps(B.getPeriod());
   2.100  	}
   2.101 -	public static boolean precedes(StrabonPeriod A, Instant B)
   2.102 +	public static boolean precedes(StrabonPeriod A, StrabonInstant B)
   2.103  	{
   2.104  		return A.getPeriod().precedes(B);
   2.105  	}
     3.1 --- a/generaldb/src/main/java/org/openrdf/sail/generaldb/evaluation/GeneralDBEvaluation.java	Wed Jan 02 19:45:28 2013 +0200
     3.2 +++ b/generaldb/src/main/java/org/openrdf/sail/generaldb/evaluation/GeneralDBEvaluation.java	Fri Jan 04 13:12:18 2013 +0200
     3.3 @@ -7,6 +7,7 @@
     3.4  
     3.5  import info.aduna.iteration.CloseableIteration;
     3.6  
     3.7 +import java.text.ParseException;
     3.8  import java.util.ArrayList;
     3.9  import java.util.Collection;
    3.10  import java.util.HashMap;
    3.11 @@ -41,6 +42,7 @@
    3.12  import org.openrdf.query.algebra.evaluation.function.spatial.GeoConstants;
    3.13  import org.openrdf.query.algebra.evaluation.function.spatial.SpatialConstructFunc;
    3.14  import org.openrdf.query.algebra.evaluation.function.spatial.SpatialRelationshipFunc;
    3.15 +import org.openrdf.query.algebra.evaluation.function.spatial.StrabonInstant;
    3.16  import org.openrdf.query.algebra.evaluation.function.spatial.StrabonPeriod;
    3.17  import org.openrdf.query.algebra.evaluation.function.spatial.StrabonPolyhedron;
    3.18  import org.openrdf.query.algebra.evaluation.function.spatial.stsparql.relation.AboveFunc;
    3.19 @@ -517,29 +519,60 @@
    3.20  
    3.21  	}
    3.22  
    3.23 -	public StrabonPeriod temporalConstructPicker(Function function, Value left, Value right)
    3.24 +	public StrabonPeriod temporalConstructPicker(Function function, Value left, Value right) throws ParseException
    3.25  	{
    3.26  		if(function.getURI().equals(TemporalConstants.periodUnion))
    3.27  		{
    3.28 -			return StrabonPeriod.union((StrabonPeriod) left, (StrabonPeriod) right);
    3.29 +			System.out.println("left= "+left.toString());
    3.30 +			System.out.println("right= "+right.toString());
    3.31 +			if(!left.toString().contains(",") && right.toString().contains(","))
    3.32 +			{
    3.33 +				StrabonInstant leftArg = StrabonInstant.read(left.toString());
    3.34 +				StrabonPeriod rightArg= new StrabonPeriod(right.toString());
    3.35 +				if(rightArg.contains(rightArg, leftArg))
    3.36 +				{
    3.37 +					return rightArg;
    3.38 +				}
    3.39 +				else
    3.40 +					return null;
    3.41 +			}
    3.42 +			else if(left.toString().contains(",") && !right.toString().contains(","))
    3.43 +			{
    3.44 +				StrabonInstant rightArg = StrabonInstant.read(right.toString());
    3.45 +				StrabonPeriod leftArg= new StrabonPeriod(left.toString());
    3.46 +				if(leftArg.contains(leftArg, rightArg))
    3.47 +				{
    3.48 +					return leftArg;
    3.49 +				}
    3.50 +				else
    3.51 +					return null;
    3.52 +			}
    3.53 +			else if(!left.toString().contains(",") && !right.toString().contains(","))
    3.54 +			{
    3.55 +				return null;
    3.56 +			}
    3.57 +			else if(left.equals(right))
    3.58 +			{
    3.59 +				return new StrabonPeriod(right.toString());
    3.60 +			}
    3.61 +			
    3.62 +			return StrabonPeriod.union(new StrabonPeriod(left.toString()), new StrabonPeriod(right.toString()));
    3.63  		}
    3.64  		else if(function.getURI().equals(TemporalConstants.periodIntersection))
    3.65 -		{
    3.66 -			System.out.println("left= "+left.toString());
    3.67 -			System.out.println("right= "+right.toString());
    3.68 -			return StrabonPeriod.intersection((StrabonPeriod) left, (StrabonPeriod) right);
    3.69 +		{			
    3.70 +			return StrabonPeriod.intersection(new StrabonPeriod(left.toString()), new StrabonPeriod(right.toString()));
    3.71  		}
    3.72  		else if(function.getURI().equals(TemporalConstants.minusPeriod))
    3.73  		{
    3.74 -			return StrabonPeriod.union((StrabonPeriod) left, (StrabonPeriod) right);
    3.75 +			return StrabonPeriod.except(new StrabonPeriod(left.toString()), new StrabonPeriod(right.toString()));
    3.76  		}
    3.77  		else if(function.getURI().equals(TemporalConstants.precedingPeriod))
    3.78  		{
    3.79 -			return StrabonPeriod.precedingPeriod((StrabonPeriod) left, (StrabonPeriod) right);
    3.80 +			return StrabonPeriod.precedingPeriod(new StrabonPeriod(left.toString()), new StrabonPeriod(right.toString()));
    3.81  		}
    3.82  		else if(function.getURI().equals(TemporalConstants.succedingPeriod))
    3.83  		{
    3.84 -			return StrabonPeriod.succedingPeriod((StrabonPeriod) left, (StrabonPeriod) right);
    3.85 +			return StrabonPeriod.succedingPeriod(new StrabonPeriod(left.toString()), new StrabonPeriod(right.toString()));
    3.86  		}
    3.87  		else
    3.88  		{
    3.89 @@ -547,23 +580,23 @@
    3.90  		}
    3.91  	}
    3.92  	
    3.93 -	public boolean temporalRelationshipPicker (Function function, Value left, Value right)
    3.94 +	public boolean temporalRelationshipPicker (Function function, Value left, Value right) throws ParseException
    3.95  	{
    3.96  		if(function.getURI().equals(TemporalConstants.adjacent))
    3.97  		{
    3.98 -			return StrabonPeriod.meets((StrabonPeriod) left, (StrabonPeriod) right);
    3.99 +			return StrabonPeriod.meets(new StrabonPeriod(left.toString()), new StrabonPeriod(right.toString()));
   3.100  		}
   3.101  		else if(function.getURI().equals(TemporalConstants.after))
   3.102  		{
   3.103 -			return StrabonPeriod.succedes((StrabonPeriod) left, (StrabonPeriod) right);
   3.104 +			return StrabonPeriod.succedes(new StrabonPeriod(left.toString()), new StrabonPeriod(right.toString()));
   3.105  		}
   3.106  		else if(function.getURI().equals(TemporalConstants.meetsBefore))
   3.107  		{
   3.108 -			return StrabonPeriod.meetsBefore((StrabonPeriod) left, (StrabonPeriod) right);
   3.109 +			return StrabonPeriod.meetsBefore(new StrabonPeriod(left.toString()), new StrabonPeriod(right.toString()));
   3.110  		}
   3.111  		else if(function.getURI().equals(TemporalConstants.meetsAfter))
   3.112  		{
   3.113 -			return StrabonPeriod.meetsAfter((StrabonPeriod) left, (StrabonPeriod) right);
   3.114 +			return StrabonPeriod.meetsAfter(new StrabonPeriod(left.toString()), new StrabonPeriod(right.toString()));
   3.115  		}
   3.116  		else
   3.117  		{
     4.1 --- a/resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java	Wed Jan 02 19:45:28 2013 +0200
     4.2 +++ b/resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java	Fri Jan 04 13:12:18 2013 +0200
     4.3 @@ -14,11 +14,17 @@
     4.4  
     4.5  import org.openrdf.model.Value;
     4.6  import org.openrdf.model.impl.LiteralImpl;
     4.7 +import org.openrdf.model.impl.URIImpl;
     4.8 +import org.openrdf.query.algebra.evaluation.function.spatial.StrabonInstant;
     4.9 +import org.openrdf.query.algebra.evaluation.function.spatial.StrabonPeriod;
    4.10 +import org.openrdf.query.algebra.evaluation.function.spatial.StrabonTemporalElement;
    4.11 +import org.openrdf.query.algebra.evaluation.function.temporal.stsparql.relation.TemporalConstants;
    4.12  import org.openrdf.query.resultio.text.tsv.SPARQLResultsTSVWriter;
    4.13  import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron;
    4.14  
    4.15  /**
    4.16   * @author Charalampos Nikolaou <charnik@di.uoa.gr>
    4.17 + * @author Konstantina Bereta <Konstantina.Bereta@di.uoa.gr> (extended for the temporal case)
    4.18   * 
    4.19   */
    4.20  public class stSPARQLResultsTSVWriter extends SPARQLResultsTSVWriter {
    4.21 @@ -35,6 +41,10 @@
    4.22  			GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) val;
    4.23  			val = new LiteralImpl(dbpolyhedron.getPolyhedronStringRep(), dbpolyhedron.getDatatype());
    4.24  		}
    4.25 +		else if(val instanceof StrabonTemporalElement){
    4.26 +			val = new LiteralImpl(((StrabonTemporalElement)val).stringValue(), ((StrabonTemporalElement) val).getDatatype());
    4.27 +		}
    4.28 +
    4.29  		
    4.30  		// write value
    4.31  		super.writeValue(val);