Strabon

changeset 683:580a09bb600c

added logging in addDateTime function. Corrected the date format to use "dd" (day of month) instead of "DD" (day of year).
Used the following query to test it:
PREFIX time: <http://example.org/custom-function/>

SELECT (time:addDateTime("2010-08-21T19:50:00", "10") as ?t) WHERE {?s ?p ?o. } LIMIT 1

The new date time is returned correctly, but then I get an OutOfMemory exception
author Babis Nikolaou <charnik@di.uoa.gr>
date Fri Nov 02 00:41:01 2012 +0200 (2012-11-02)
parents a49d35fba425
children 5221af947637
files evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/link/AddDateTimeFunc.java
line diff
     1.1 --- a/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/link/AddDateTimeFunc.java	Thu Nov 01 19:52:56 2012 +0200
     1.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/link/AddDateTimeFunc.java	Fri Nov 02 00:41:01 2012 +0200
     1.3 @@ -1,3 +1,12 @@
     1.4 +/**
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.8 + * 
     1.9 + * Copyright (C) 2012, Pyravlos Team
    1.10 + * 
    1.11 + * http://www.strabon.di.uoa.gr/
    1.12 + */
    1.13  package org.openrdf.query.algebra.evaluation.function.link;
    1.14  
    1.15  import java.text.SimpleDateFormat;
    1.16 @@ -12,50 +21,56 @@
    1.17  import org.openrdf.model.ValueFactory;
    1.18  import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException;
    1.19  import org.openrdf.query.algebra.evaluation.function.Function;
    1.20 -import org.openrdf.query.algebra.evaluation.function.spatial.GeoConstants;
    1.21 +import org.slf4j.Logger;
    1.22 +import org.slf4j.LoggerFactory;
    1.23  
    1.24 +/**
    1.25 + * Defines a function that advances a date-time value (first argument)
    1.26 + * by a given integer number representing minutes (second argument).
    1.27 + *  
    1.28 + * @author Konstantina Bereta <Konstantina.Bereta@di.uoa.gr>
    1.29 + */
    1.30  public class AddDateTimeFunc implements Function {
    1.31  	
    1.32 +	private static Logger logger = LoggerFactory.getLogger(org.openrdf.query.algebra.evaluation.function.link.AddDateTimeFunc.class);
    1.33  
    1.34 -	
    1.35 -	protected static String name = "addDatetime";
    1.36 +	protected static String name = "addDateTime";
    1.37  	
    1.38  	@Override
    1.39  	public String getURI() {
    1.40 -		return "http://example.org/custom-function/addDateTime";
    1.41 +		return "http://example.org/custom-function/" + name;
    1.42  		
    1.43  	}
    1.44  
    1.45 -    @SuppressWarnings("deprecation")
    1.46 -	public Value evaluate(ValueFactory valueFactory, Value... args)
    1.47 -            throws ValueExprEvaluationException {
    1.48 +	public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
    1.49          if (args.length != 2) {
    1.50              throw new ValueExprEvaluationException("strdf:" + name
    1.51                      + " requires exactly 2 arguments, got " + args.length);
    1.52          }
    1.53 -        
    1.54 -        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD'T'hh:mm:ss"); //the format of xsd:Datetime
    1.55 -		GregorianCalendar calendar = new GregorianCalendar();
    1.56 +
    1.57 +        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss"); //the format of xsd:Datetime
    1.58  		GregorianCalendar cal = new GregorianCalendar(); 
    1.59 -	    DatatypeFactory dafa = null;
    1.60 -	    XMLGregorianCalendar xmlcal =null;
    1.61 -	    XMLGregorianCalendar gxml1 =null;
    1.62  
    1.63      	try {
    1.64 -    		String toParse = args[1].toString().replace("^^<http://www.w3.org/2001/XMLSchema#integer>", "").replace("\"", "");
    1.65 -    		System.out.println("TO PARSE:"+ toParse);
    1.66 -    		int minutesToAdd = Integer.parseInt(toParse);
    1.67 +    		// get minutes, remove possible appearance of integer datatype, and strip double quotes
    1.68 +    		String minutes = args[1].toString().replace("^^<http://www.w3.org/2001/XMLSchema#integer>", "").replace("\"", "");
    1.69 +    		System.out.println("TO PARSE:"+ minutes);
    1.70 +    		int minutesToAdd = Integer.parseInt(minutes);
    1.71 +    		
    1.72      	    String date = args[0].toString();
    1.73 -    	    date = args[0].toString().replace("^^<http://www.w3.org/2001/XMLSchema#dateTime>", "").replace("\"", "");
    1.74 -    	    //cal = sdf.getCalendar();
    1.75 +    	    
    1.76 +    	    // remove possible appearance of dateTime datatype and strip double quotes
    1.77 +    	    date = date.replace("^^<http://www.w3.org/2001/XMLSchema#dateTime>", "").replace("\"", "");
    1.78 +    	    
    1.79 +    	    // set the time (according to 1st argument)
    1.80      		cal.setTime(sdf.parse(date));
    1.81      		System.out.println("OLD TIME:"+cal.getTime());
    1.82 +    		
    1.83 +    		// add the minutes (according to 2nd argument)
    1.84      		cal.add(Calendar.MINUTE, minutesToAdd);
    1.85 +
    1.86      		System.out.println("NEW TIME:"+cal.getTime());
    1.87      	   
    1.88 -    		
    1.89 -          // xmlcal =  dafa.newXMLGregorianCalendar(cal.toString());
    1.90 -
    1.91      		System.out.println(cal.get(Calendar.YEAR));
    1.92      		System.out.println(cal.get(Calendar.MONTH));
    1.93      		System.out.println(cal.get(Calendar.DAY_OF_MONTH));
    1.94 @@ -64,24 +79,19 @@
    1.95      		System.out.println(cal.get(Calendar.MILLISECOND));
    1.96      		
    1.97      	} catch (java.text.ParseException e) {
    1.98 -			// TODO Auto-generated catch block
    1.99 -			e.printStackTrace();
   1.100 +    		logger.error("[Strabon.AddDateTimeFunc] Error parsing the arguments of \"addDateTime\" extension function.", e);
   1.101  		}
   1.102      
   1.103 -        //valueFactory.createLiteral(xmlmcal);
   1.104      	XMLGregorianCalendar gxml=null;
   1.105  		try {
   1.106  			gxml = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
   1.107 +			
   1.108  		} catch (DatatypeConfigurationException e) {
   1.109 -			// TODO Auto-generated catch block
   1.110 -			e.printStackTrace();
   1.111 +			logger.error("[Strabon.AddDateTimeFunc] Error constructing a new Datetime value.", e);
   1.112  		}
   1.113 -		//gxml = dafa.newXMLGregorianCalendar(cal);
   1.114 +		
   1.115  		Value value =  valueFactory.createLiteral(gxml);
   1.116  		System.out.println("value="+value.toString());
   1.117  		return value;
   1.118      }
   1.119 -
   1.120 -
   1.121 -
   1.122  }