You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2012/04/29 21:04:46 UTC
svn commit: r1331976 - in
/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue: ./
Dispatch.java NV2.java
Author: andy
Date: Sun Apr 29 19:04:46 2012
New Revision: 1331976
URL: http://svn.apache.org/viewvc?rev=1331976&view=rev
Log: (empty)
Added:
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/Dispatch.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/NV2.java
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/Dispatch.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/Dispatch.java?rev=1331976&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/Dispatch.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/Dispatch.java Sun Apr 29 19:04:46 2012
@@ -0,0 +1,95 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package projects.nodevalue;
+
+import java.util.HashMap ;
+import java.util.Map ;
+
+import com.hp.hpl.jena.sparql.expr.ExprEvalException ;
+import com.hp.hpl.jena.sparql.expr.NodeValue ;
+import com.hp.hpl.jena.sparql.expr.ValueSpaceClassification ;
+import static com.hp.hpl.jena.sparql.expr.ValueSpaceClassification.* ;
+import com.hp.hpl.jena.sparql.expr.nodevalue.XSDFuncOp ;
+
+public class Dispatch
+{
+ public Dispatch() {}
+
+ interface Function2 { NodeValue eval(NodeValue nv1, NodeValue nv2) ; }
+
+ static Function2 vsExplode = new Function2(){
+ @Override public NodeValue eval(NodeValue x, NodeValue y) {
+ throw new ExprEvalException("No such value space") ;
+ }
+ } ;
+
+ /** Multimode operations
+ * +, -
+ * = < etc
+ */
+
+ /*
+ * NodeValueDuration.isDayTime
+ * NodeValueDuration.isYearMonth
+ */
+
+ public static void main(String... argv)
+ {
+ Map<ValueSpaceClassification, Function2> table = new HashMap<ValueSpaceClassification, Function2>() ;
+
+ Function2 vsExplode = new Function2(){
+ @Override public NodeValue eval(NodeValue x, NodeValue y) {
+ throw new ExprEvalException("No such value space") ;
+ }
+ } ;
+
+ Function2 vsNumberAdd = new Function2(){
+ @Override public NodeValue eval(NodeValue x, NodeValue y) {
+ return XSDFuncOp.add(x, y) ;
+ }
+ } ;
+ Function2 vsStringAdd = new Function2(){
+ @Override
+ public NodeValue eval(NodeValue x, NodeValue y)
+ {
+ return NodeValue.makeString(x.asString()+y.asString()) ;
+ }
+ } ;
+ table.put(VSPACE_NUM, vsNumberAdd) ;
+ table.put(VSPACE_STRING, vsStringAdd) ;
+
+ NodeValue nv1 = NodeValue.makeDecimal(2) ;
+ NodeValue nv2 = NodeValue.makeDecimal(3) ;
+
+ ValueSpaceClassification vs = NodeValue.classifyValueOp(nv1, nv2) ;
+ NodeValue nv3 = choose(table, nv1, nv2).eval(nv1, nv2) ;
+ System.out.println(nv3) ;
+ //ValueSpaceClassification
+ }
+
+ static Function2 choose(Map<ValueSpaceClassification, Function2> table, NodeValue nv1, NodeValue nv2)
+ {
+ ValueSpaceClassification vs = NodeValue.classifyValueOp(nv1, nv2) ;
+ Function2 f2 = table.get(vs) ;
+ if ( f2 == null ) return vsExplode ;
+ return f2 ;
+ }
+
+}
+
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/NV2.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/NV2.java?rev=1331976&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/NV2.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/nodevalue/NV2.java Sun Apr 29 19:04:46 2012
@@ -0,0 +1,216 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package projects.nodevalue;
+
+import static com.hp.hpl.jena.sparql.expr.ValueSpaceClassification.VSPACE_DURATION ;
+import static com.hp.hpl.jena.sparql.expr.ValueSpaceClassification.VSPACE_NUM ;
+import static com.hp.hpl.jena.sparql.expr.ValueSpaceClassification.VSPACE_STRING ;
+import static javax.xml.datatype.DatatypeConstants.DAYS ;
+import static javax.xml.datatype.DatatypeConstants.HOURS ;
+import static javax.xml.datatype.DatatypeConstants.MINUTES ;
+import static javax.xml.datatype.DatatypeConstants.MONTHS ;
+import static javax.xml.datatype.DatatypeConstants.SECONDS ;
+import static javax.xml.datatype.DatatypeConstants.YEARS ;
+
+import java.util.GregorianCalendar ;
+
+import javax.xml.datatype.DatatypeConfigurationException ;
+import javax.xml.datatype.DatatypeFactory ;
+import javax.xml.datatype.Duration ;
+import javax.xml.datatype.XMLGregorianCalendar ;
+import javax.xml.namespace.QName ;
+
+import com.hp.hpl.jena.sparql.ARQInternalErrorException ;
+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 ;
+
+public class NV2
+{
+ // Convert NodeValue to use javax.xml.datatype. XMLGregorianCalendar and Duration
+
+ /*
+ NodeFunctions.java
+ NodeValueBoolean.java
+* NodeValueDate.java
+* NodeValueDateTime.java
+ NodeValueDecimal.java
+ NodeValueDouble.java
+* NodeValueDuration.java
+ NodeValueFloat.java
+* NodeValueGDay.java
+* NodeValueGMonth.java
+* NodeValueGMonthDay.java
+* NodeValueGYear.java
+* NodeValueGYearMonth.java
+ NodeValueInteger.java
+ NodeValueNode.java
+ NodeValueString.java
+* NodeValueTime.java
+ NodeValueVisitor.java
+ NumericType.java
+ XSDFuncOp.java
+
+ */
+
+ private static DatatypeFactory datatypefactory = null ;
+ static
+ {
+ try { datatypefactory = DatatypeFactory.newInstance() ; }
+ catch (DatatypeConfigurationException ex)
+ { throw new ARQInternalErrorException("Can't create a javax.xml DatatypeFactory") ; }
+ }
+
+ public static void main(String... argv)
+ {
+ NodeValue nv = NodeValue.makeDate("2012-04-29") ;
+ //System.out.println(nv.getDateTime().getHours()) ;
+
+ //XSDFuncOp.add(nv, nv)
+
+ Duration dur = datatypefactory.newDuration("P10Y10MT2H") ;
+ XMLGregorianCalendar cal = datatypefactory.newXMLGregorianCalendar("2012-04-28T21:06:00Z") ;
+ XMLGregorianCalendar cal2 = datatypefactory.newXMLGregorianCalendar("2012-04-29") ;
+
+ NodeValue nv1 = NodeValue.parse("'2012-04-28T21:06:00Z'^^xsd:dateTime") ;
+ NodeValue nv2 = NodeValue.makeDuration("P10Y10MT2H") ;
+
+ System.out.println(addNV(nv1, nv2)) ;
+ System.out.println(addNV(nv2, nv1)) ;
+
+ NodeValue nv3 = NodeValue.makeDuration("P1Y1MT2H") ;
+ System.out.println(NodeValue.compare(nv2, nv3)) ;
+ }
+
+ static NodeValue addNV(NodeValue nv1, NodeValue nv2)
+ {
+ ValueSpaceClassification vs1 = nv1.getValueSpace() ;
+ ValueSpaceClassification vs2 = nv2.getValueSpace() ;
+
+ if ( vs1.equals(VSPACE_NUM) && vs2.equals(VSPACE_NUM) )
+ return XSDFuncOp.add(nv1, nv2) ;
+ if ( vs1.equals(VSPACE_STRING) && vs2.equals(VSPACE_STRING) )
+ return NodeValue.makeString(nv1.asString()+nv2.asString()) ;
+
+ if ( isDT(vs1) && vs2.equals(VSPACE_DURATION) )
+ {
+ // Temp fakery.
+ XMLGregorianCalendar cal = datatypefactory.newXMLGregorianCalendar((GregorianCalendar)nv1.getDateTime().asCalendar()) ;
+ XMLGregorianCalendar result = add(cal, nv2.getDuration()) ;
+ // Unfakery.
+ NodeValue r = NodeValue.makeDateTime(result.toGregorianCalendar()) ;
+ return r ;
+ }
+
+ if ( isDT(vs2) && vs1.equals(VSPACE_DURATION) )
+ {
+ // Temp fakery.
+ XMLGregorianCalendar cal = datatypefactory.newXMLGregorianCalendar((GregorianCalendar)nv2.getDateTime().asCalendar()) ;
+ XMLGregorianCalendar result = add(cal, nv1.getDuration()) ;
+ // Unfakery.
+ NodeValue r = NodeValue.makeDateTime(result.toGregorianCalendar()) ;
+ return r ;
+ }
+
+ throw new ExprEvalTypeException("Operator '+' : Undefined addition: "+nv1+" and "+nv2) ;
+ }
+
+ private static XMLGregorianCalendar add(XMLGregorianCalendar cal, Duration duration)
+ {
+ XMLGregorianCalendar result = (XMLGregorianCalendar)cal.clone() ;
+ result.add(duration) ;
+ return result ;
+ }
+
+ static boolean isDT(ValueSpaceClassification vs)
+ {
+ switch (vs)
+ {
+ case VSPACE_DATETIME:
+ case VSPACE_DATE:
+ case VSPACE_TIME:
+ //case VSPACE_DURATION:
+ case VSPACE_G_YEAR:
+ case VSPACE_G_YEARMONTH: case VSPACE_G_MONTHDAY:
+ case VSPACE_G_MONTH: case VSPACE_G_DAY:
+ return true ;
+ default:
+ return false ;
+ }
+
+ }
+
+
+ static XMLGregorianCalendar parsecal(String lex)
+ {
+ XMLGregorianCalendar cal = datatypefactory.newXMLGregorianCalendar(lex) ;
+ QName qname = cal.getXMLSchemaType() ;
+ String uri = qname.getNamespaceURI()+"#"+qname.getLocalPart() ;
+ return cal ;
+ }
+
+ static XMLGregorianCalendar add(String lex1, String lex2)
+ {
+ System.out.println(lex1+" + "+lex2) ;
+ XMLGregorianCalendar cal = datatypefactory.newXMLGregorianCalendar(lex1) ;
+ Duration dur = datatypefactory.newDuration(lex2) ;
+ cal.add(dur) ;
+ System.out.println(str(cal)) ;
+ return cal ;
+ }
+
+ private static String str(XMLGregorianCalendar cal)
+ {
+ QName qname = cal.getXMLSchemaType() ;
+ return "'"+cal.toXMLFormat()+"'^^xs:"+qname.getLocalPart() ;
+ }
+
+
+ static Duration parsedur(String lex)
+ {
+ Duration dur = datatypefactory.newDuration(lex) ;
+ String dt = "duration" ;
+ if ( isYearMonth(dur) )
+ dt = "yearMonth" ;
+ if ( isDayTime(dur) )
+ dt = "dayTime" ;
+// System.out.println("'"+dur+"'^^xs:"+dt) ;
+ return dur ;
+ }
+
+ // Spec is slightly unclear.
+ // It defines it as PnDTnHnMnS
+ // but also says "restrict to D/H/M/S i.e. not necessary required"
+ // The examples include the shorter forms.
+
+ static boolean isYearMonth(Duration dur)
+ {
+ return ( dur.isSet(YEARS) || dur.isSet(MONTHS) ) &&
+ ! dur.isSet(DAYS) && ! dur.isSet(HOURS) && ! dur.isSet(MINUTES) && ! dur.isSet(SECONDS) ;
+ }
+
+ static boolean isDayTime(Duration dur)
+ {
+ return !dur.isSet(YEARS) && ! dur.isSet(MONTHS) &&
+ ( dur.isSet(DAYS) || dur.isSet(HOURS) || dur.isSet(MINUTES) || dur.isSet(SECONDS) );
+ }
+
+}
+