jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r1335203 - in /jena/Jena2/ARQ/trunk/src: main/java/com/hp/hpl/jena/sparql/expr/ main/java/com/hp/hpl/jena/sparql/expr/nodevalue/ test/java/com/hp/hpl/jena/sparql/expr/
Date Mon, 07 May 2012 19:12:26 GMT
Author: andy
Date: Mon May  7 19:12:26 2012
New Revision: 1335203

URL: http://svn.apache.org/viewvc?rev=1335203&view=rev
Log:
Algorithms for XSD date/time/duration operations.

Added:
    jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/nodevalue/NodeValueOps.java
      - copied, changed from r1335123, jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/NodeValueOps.java
    jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TestNodeValueOps.java
      - copied, changed from r1335123, jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/TestNodeValueOps.java
Modified:
    jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/NodeValue.java
    jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TS_Expr.java

Modified: jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/NodeValue.java
URL: http://svn.apache.org/viewvc/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/NodeValue.java?rev=1335203&r1=1335202&r2=1335203&view=diff
==============================================================================
--- jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/NodeValue.java (original)
+++ jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/NodeValue.java Mon May
 7 19:12:26 2012
@@ -169,10 +169,10 @@ public abstract class NodeValue extends 
     
     public static final String xsdNamespace = XSD+"#" ; 
     
-    private static DatatypeFactory datatypefactory = null ;
+    public static DatatypeFactory xmlDatatypeFactory = null ;
     static
     {
-        try { datatypefactory = DatatypeFactory.newInstance() ; }
+        try { xmlDatatypeFactory = DatatypeFactory.newInstance() ; }
         catch (DatatypeConfigurationException ex)
         { throw new ARQInternalErrorException("Can't create a javax.xml DatatypeFactory")
; }
     }
@@ -1129,7 +1129,7 @@ public abstract class NodeValue extends 
                    dtXSDyearMonthDuration.equals(datatypeURI) ) &&
                    XSDduration.isValid(lex) ) // use lex
             {
-                Duration duration = datatypefactory.newDuration(lex) ;
+                Duration duration = xmlDatatypeFactory.newDuration(lex) ;
                 
                 if ( dtXSDdayTimeDuration.equals(datatypeURI) && ! XSDFuncOp.isDayTime(duration)
)
                     return null ;

Copied: jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/nodevalue/NodeValueOps.java
(from r1335123, jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/NodeValueOps.java)
URL: http://svn.apache.org/viewvc/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/nodevalue/NodeValueOps.java?p2=jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/nodevalue/NodeValueOps.java&p1=jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/NodeValueOps.java&r1=1335123&r2=1335203&rev=1335203&view=diff
==============================================================================
--- jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/NodeValueOps.java (original)
+++ jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/nodevalue/NodeValueOps.java
Mon May  7 19:12:26 2012
@@ -16,7 +16,7 @@
  * limitations under the License.
  */
 
-package projects.nodevalue;
+package com.hp.hpl.jena.sparql.expr.nodevalue;
 
 import static com.hp.hpl.jena.datatypes.xsd.XSDDatatype.XSD ;
 import static com.hp.hpl.jena.sparql.expr.ValueSpaceClassification.VSPACE_DATE ;
@@ -28,6 +28,7 @@ import static com.hp.hpl.jena.sparql.exp
 import static javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED ;
 
 import java.math.BigDecimal ;
+import java.util.GregorianCalendar ;
 
 import javax.xml.datatype.Duration ;
 import javax.xml.datatype.XMLGregorianCalendar ;
@@ -38,11 +39,69 @@ import com.hp.hpl.jena.query.ARQ ;
 import com.hp.hpl.jena.sparql.expr.ExprEvalTypeException ;
 import com.hp.hpl.jena.sparql.expr.NodeValue ;
 import com.hp.hpl.jena.sparql.expr.ValueSpaceClassification ;
-import com.hp.hpl.jena.sparql.expr.nodevalue.XSDFuncOp ;
 import com.hp.hpl.jena.sparql.util.NodeFactory ;
 
+/** The code parts of arithmetic opperations on Nodevlaues.
+ */
 public class NodeValueOps
 {
+    /* Date/time/duration coverage
+     * add, subtract (duration, duration)
+     * 
+(xsd:duration, xsd:duration) -> xsd:duration
+        10.6.1 op:add-yearMonthDurations
+        10.6.2 op:subtract-yearMonthDurations
+(xsd:duration, xsd:duration) -> xsd:duration
+        10.6.6 op:add-dayTimeDurations
+        10.6.7 op:subtract-dayTimeDurations
+** Java has general duration subtract 
+
+    * Subtract two date/times
+
+(xsd:dateTime, xsd:dateTime) -> xsd:duration
+        10.8.1 op:subtract-dateTimes -> xsd:dayTimeDuration
+(xsd:date, xsd:date) -> xsd:duration
+        10.8.2 op:subtract-dates
+(xsd:time, xsd:time) -> xsd:duration
+        10.8.3 op:subtract-times
+
+    * Date/time and duration
+** Java has general duration subtract (error needed?)
+
+(xsd:dateTime, xsd:duration) -> xsd:dateTime
+        10.8.4 op:add-yearMonthDuration-to-dateTime
+        10.8.5 op:add-dayTimeDuration-to-dateTime
+        10.8.6 op:subtract-yearMonthDuration-from-dateTime
+        10.8.7 op:subtract-dayTimeDuration-from-dateTime
+
+(xsd:date, xsd:duration) -> xsd:date
+        10.8.8 op:add-yearMonthDuration-to-date
+        10.8.9 op:add-dayTimeDuration-to-date
+        10.8.10 op:subtract-yearMonthDuration-from-date
+        10.8.11 op:subtract-dayTimeDuration-from-date
+
+(xsd:time, xsd:duration) -> xsd:time
+        10.8.12 op:add-dayTimeDuration-to-time
+        10.8.13 op:subtract-dayTimeDuration-from-time      
+
+(xsd:duration, xsd:double) -> xsd:duration
+        10.6.8 op:multiply-dayTimeDuration
+        */
+    
+        /* Missing:
+(xsd:duration, xsd:double) -> xsd:duration
+        10.6.9 op:divide-dayTimeDuration
+        
+(xsd:duration, xsd:duration) -> xsd:decimal
+        10.6.10 op:divide-dayTimeDuration-by-dayTimeDuration 
+         */
+    /* Notes:
+     * Does not consider whether a duration is dayTime or yearMonth
+     * for addition and subtraction.
+     * As per the Java (Xerces) implementation, it just work on durations.    
+     */
+    
+    // Until part of Jena datatypes.
     private static final String dtXSDdateTimeStamp      = XSD+"#dateTimeStamp" ;
     private static final String dtXSDdayTimeDuration    = XSD+"#dayTimeDuration" ;
     private static final String dtXSDyearMonthDuration  = XSD+"#yearMonthDuration" ;
@@ -82,7 +141,7 @@ public class NodeValueOps
         if ( vs1.equals(VSPACE_DATETIME) && vs2.equals(VSPACE_DURATION) )
         {
             XMLGregorianCalendar cal = nv1.getDateTime() ;
-            XMLGregorianCalendar result = NV2.xsd_add(cal, nv2.getDuration()) ;
+            XMLGregorianCalendar result = xsd_add(cal, nv2.getDuration()) ;
             NodeValue r = NodeValue.makeDateTime(result) ;
             return r ;
         }
@@ -90,7 +149,7 @@ public class NodeValueOps
         if ( vs1.equals(VSPACE_DATE) && vs2.equals(VSPACE_DURATION) )
         {
             XMLGregorianCalendar cal = nv1.getDateTime() ;
-            XMLGregorianCalendar result = NV2.xsd_add(cal, nv2.getDuration()) ;
+            XMLGregorianCalendar result = xsd_add(cal, nv2.getDuration()) ;
             NodeValue r = NodeValue.makeDate(result) ;
             return r ;
         }
@@ -99,7 +158,7 @@ public class NodeValueOps
         {
             // ONLY dayTime.
             XMLGregorianCalendar cal = nv1.getDateTime() ;
-            XMLGregorianCalendar result = NV2.xsd_add(cal, nv2.getDuration()) ;
+            XMLGregorianCalendar result = xsd_add(cal, nv2.getDuration()) ;
             NodeValue r = NodeValue.makeNode(result.toXMLFormat(), XSDDatatype.XSDtime) ;

             return r ;
         }
@@ -149,7 +208,7 @@ public class NodeValueOps
             if ( ( isDef1 && !isDef2 ) || ( !isDef1 && isDef2 ) )
                 throw new ExprEvalTypeException("Operator '-': can't substract timezone/non-timezone
values") ;
             // Inspect duration and force to better type? xsd:dayTimeDuration
-            return NodeValue.makeDuration(NV2.xsd_substract(cal1, cal2));
+            return NodeValue.makeDuration(xsd_substract(cal1, cal2));
         }
         
         // Loose style. Subtract any duration to any date or time value.
@@ -157,7 +216,7 @@ public class NodeValueOps
         {
             XMLGregorianCalendar cal = nv1.getDateTime() ;
             // add-negation
-            XMLGregorianCalendar result = NV2.xsd_subtract(cal, nv2.getDuration()) ;
+            XMLGregorianCalendar result = xsd_subtract(cal, nv2.getDuration()) ;
             NodeValue r = NodeValue.makeDateTime(result) ;
             return r ;
         }
@@ -165,7 +224,7 @@ public class NodeValueOps
         {
             XMLGregorianCalendar cal = nv1.getDateTime() ;
             // add-negation
-            XMLGregorianCalendar result = NV2.xsd_subtract(cal, nv2.getDuration()) ;
+            XMLGregorianCalendar result = xsd_subtract(cal, nv2.getDuration()) ;
             NodeValue r = NodeValue.makeDate(result) ;
             return r ;
         }
@@ -173,7 +232,7 @@ public class NodeValueOps
         {
             XMLGregorianCalendar cal = nv1.getDateTime() ;
             // add-negation
-            XMLGregorianCalendar result = NV2.xsd_subtract(cal, nv2.getDuration()) ;
+            XMLGregorianCalendar result = xsd_subtract(cal, nv2.getDuration()) ;
             NodeValue r = NodeValue.makeNode(result.toXMLFormat(), XSDDatatype.XSDtime) ;

             return r ;
         }
@@ -218,7 +277,6 @@ public class NodeValueOps
     }
 
 
-    // XSDFuncOps
     private static boolean isDT(ValueSpaceClassification vs)
     {
         switch (vs)
@@ -237,5 +295,30 @@ public class NodeValueOps
         }
     }
     
+    private static XMLGregorianCalendar xsd_add(XMLGregorianCalendar cal, Duration duration)
+    {
+        //if ( ! isYearMonth(duration) && ! isDayTime(duration) )
+
+        XMLGregorianCalendar result = (XMLGregorianCalendar)cal.clone() ;
+        result.add(duration) ;
+        return result ;
+    }
+
+    private static XMLGregorianCalendar xsd_subtract(XMLGregorianCalendar cal, Duration duration)
+    {
+        return xsd_add(cal, duration.negate()) ;
+    }
+
+    private static Duration xsd_substract(XMLGregorianCalendar cal1, XMLGregorianCalendar
cal2)
+    {
+        GregorianCalendar gcal1 = cal1.toGregorianCalendar() ;
+        GregorianCalendar gcal2 = cal2.toGregorianCalendar() ;
+        long x1 = gcal1.getTimeInMillis() ;
+        long x2 = gcal2.getTimeInMillis() ;
+        //return datatypefactory.newDurationDayTime(x1-x2) ;
+        return NodeValue.xmlDatatypeFactory.newDuration(x1-x2) ;
+    }
+
+    
 }
 

Modified: jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TS_Expr.java
URL: http://svn.apache.org/viewvc/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TS_Expr.java?rev=1335203&r1=1335202&r2=1335203&view=diff
==============================================================================
--- jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TS_Expr.java (original)
+++ jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TS_Expr.java Mon May  7
19:12:26 2012
@@ -32,6 +32,7 @@ import org.junit.runners.Suite.SuiteClas
     , TestExpressions2.class
     , TestFunctions.class
     , TestFunctions2.class
+    , TestNodeValueOps.class
     , TestOrdering.class 
     , TestRegex.class
     , TestXSDFuncOp.class

Copied: jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TestNodeValueOps.java
(from r1335123, jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/TestNodeValueOps.java)
URL: http://svn.apache.org/viewvc/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TestNodeValueOps.java?p2=jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TestNodeValueOps.java&p1=jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/TestNodeValueOps.java&r1=1335123&r2=1335203&rev=1335203&view=diff
==============================================================================
--- jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/TestNodeValueOps.java (original)
+++ jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/expr/TestNodeValueOps.java Mon
May  7 19:12:26 2012
@@ -16,10 +16,11 @@
  * limitations under the License.
  */
 
-package projects.nodevalue;
+package com.hp.hpl.jena.sparql.expr;
 
 import com.hp.hpl.jena.sparql.expr.ExprEvalException ;
 import com.hp.hpl.jena.sparql.expr.NodeValue ;
+import com.hp.hpl.jena.sparql.expr.nodevalue.NodeValueOps ;
 
 import org.junit.Test ;
 import org.openjena.atlas.junit.BaseTest ;



Mime
View raw message