Changeset 683:580a09bb600c


Ignore:
Timestamp:
11/02/12 00:41:01 (5 years ago)
Author:
Babis Nikolaou <charnik@…>
Branch:
default
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/link/AddDateTimeFunc.java

    r682 r683  
     1/** 
     2 * This Source Code Form is subject to the terms of the Mozilla Public 
     3 * License, v. 2.0. If a copy of the MPL was not distributed with this 
     4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
     5 *  
     6 * Copyright (C) 2012, Pyravlos Team 
     7 *  
     8 * http://www.strabon.di.uoa.gr/ 
     9 */ 
    110package org.openrdf.query.algebra.evaluation.function.link; 
    211 
     
    1322import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; 
    1423import org.openrdf.query.algebra.evaluation.function.Function; 
    15 import org.openrdf.query.algebra.evaluation.function.spatial.GeoConstants; 
     24import org.slf4j.Logger; 
     25import org.slf4j.LoggerFactory; 
    1626 
     27/** 
     28 * Defines a function that advances a date-time value (first argument) 
     29 * by a given integer number representing minutes (second argument). 
     30 *   
     31 * @author Konstantina Bereta <Konstantina.Bereta@di.uoa.gr> 
     32 */ 
    1733public class AddDateTimeFunc implements Function { 
    1834         
     35        private static Logger logger = LoggerFactory.getLogger(org.openrdf.query.algebra.evaluation.function.link.AddDateTimeFunc.class); 
    1936 
    20          
    21         protected static String name = "addDatetime"; 
     37        protected static String name = "addDateTime"; 
    2238         
    2339        @Override 
    2440        public String getURI() { 
    25                 return "http://example.org/custom-function/addDateTime"; 
     41                return "http://example.org/custom-function/" + name; 
    2642                 
    2743        } 
    2844 
    29     @SuppressWarnings("deprecation") 
    30         public Value evaluate(ValueFactory valueFactory, Value... args) 
    31             throws ValueExprEvaluationException { 
     45        public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { 
    3246        if (args.length != 2) { 
    3347            throw new ValueExprEvaluationException("strdf:" + name 
    3448                    + " requires exactly 2 arguments, got " + args.length); 
    3549        } 
    36          
    37         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD'T'hh:mm:ss"); //the format of xsd:Datetime 
    38                 GregorianCalendar calendar = new GregorianCalendar(); 
     50 
     51        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss"); //the format of xsd:Datetime 
    3952                GregorianCalendar cal = new GregorianCalendar();  
    40             DatatypeFactory dafa = null; 
    41             XMLGregorianCalendar xmlcal =null; 
    42             XMLGregorianCalendar gxml1 =null; 
    4353 
    4454        try { 
    45                 String toParse = args[1].toString().replace("^^<http://www.w3.org/2001/XMLSchema#integer>", "").replace("\"", ""); 
    46                 System.out.println("TO PARSE:"+ toParse); 
    47                 int minutesToAdd = Integer.parseInt(toParse); 
     55                // get minutes, remove possible appearance of integer datatype, and strip double quotes 
     56                String minutes = args[1].toString().replace("^^<http://www.w3.org/2001/XMLSchema#integer>", "").replace("\"", ""); 
     57                System.out.println("TO PARSE:"+ minutes); 
     58                int minutesToAdd = Integer.parseInt(minutes); 
     59                 
    4860            String date = args[0].toString(); 
    49             date = args[0].toString().replace("^^<http://www.w3.org/2001/XMLSchema#dateTime>", "").replace("\"", ""); 
    50             //cal = sdf.getCalendar(); 
     61             
     62            // remove possible appearance of dateTime datatype and strip double quotes 
     63            date = date.replace("^^<http://www.w3.org/2001/XMLSchema#dateTime>", "").replace("\"", ""); 
     64             
     65            // set the time (according to 1st argument) 
    5166                cal.setTime(sdf.parse(date)); 
    5267                System.out.println("OLD TIME:"+cal.getTime()); 
     68                 
     69                // add the minutes (according to 2nd argument) 
    5370                cal.add(Calendar.MINUTE, minutesToAdd); 
     71 
    5472                System.out.println("NEW TIME:"+cal.getTime()); 
    5573            
    56                  
    57           // xmlcal =  dafa.newXMLGregorianCalendar(cal.toString()); 
    58  
    5974                System.out.println(cal.get(Calendar.YEAR)); 
    6075                System.out.println(cal.get(Calendar.MONTH)); 
     
    6580                 
    6681        } catch (java.text.ParseException e) { 
    67                         // TODO Auto-generated catch block 
    68                         e.printStackTrace(); 
     82                logger.error("[Strabon.AddDateTimeFunc] Error parsing the arguments of \"addDateTime\" extension function.", e); 
    6983                } 
    7084     
    71         //valueFactory.createLiteral(xmlmcal); 
    7285        XMLGregorianCalendar gxml=null; 
    7386                try { 
    7487                        gxml = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal); 
     88                         
    7589                } catch (DatatypeConfigurationException e) { 
    76                         // TODO Auto-generated catch block 
    77                         e.printStackTrace(); 
     90                        logger.error("[Strabon.AddDateTimeFunc] Error constructing a new Datetime value.", e); 
    7891                } 
    79                 //gxml = dafa.newXMLGregorianCalendar(cal); 
     92                 
    8093                Value value =  valueFactory.createLiteral(gxml); 
    8194                System.out.println("value="+value.toString()); 
    8295                return value; 
    8396    } 
    84  
    85  
    86  
    8797} 
Note: See TracChangeset for help on using the changeset viewer.