[Mulgara-svn] r693 - in branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter: . arithmetic value

pag at mulgara.org pag at mulgara.org
Tue Mar 18 21:49:56 UTC 2008


Author: pag
Date: 2008-03-18 14:49:52 -0700 (Tue, 18 Mar 2008)
New Revision: 693

Added:
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/LangMatches.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFn.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/SameTerm.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractAccessorFn.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFn.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/StrFn.java
Modified:
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/BinaryTestFilter.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractNumericOperation.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparable.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparableLiteral.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BlankNodeValue.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/IRI.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/SimpleLiteral.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/ValueLiteral.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Var.java
Log:
The last of the functions to be implemented, along with some refactoring to reduce duplication. The equals() method was also updated on literals to return errors for differing types, and to handle the semantics for different numeric types.

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/BinaryTestFilter.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/BinaryTestFilter.java	2008-03-18 11:07:25 UTC (rev 692)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/BinaryTestFilter.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -16,7 +16,7 @@
 
 
 /**
- * A test operation for equality or inequality.
+ * A test operation for general RDF Terms, specifically equality or inequality.
  *
  * @created Mar 14, 2008
  * @author Paul Gearon

Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/LangMatches.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/LangMatches.java	                        (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/LangMatches.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -0,0 +1,42 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter;
+
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.value.ValueLiteral;
+
+
+/**
+ * The lang matches comparison for Values.
+ *
+ * @created Mar 8, 2008
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class LangMatches extends BinaryTestFilter {
+
+  /**
+   * Creates an equality test operation
+   * @param lhs The first term to compare
+   * @param rhs The second term to compare
+   */
+  LangMatches(ValueLiteral lhs, ValueLiteral rhs) {
+    super(lhs, rhs);
+  }
+
+  /** @see org.mulgara.query.filter.BinaryTestFilter#testCmp() */
+  boolean testCmp() throws QueryException {
+    return lhs.equals(rhs);
+  }
+
+}

Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFn.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFn.java	                        (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFn.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -0,0 +1,118 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter;
+
+import java.util.regex.Pattern;
+import static java.util.regex.Pattern.*;
+
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.value.ValueLiteral;
+
+
+/**
+ * The regular expression test for values.
+ *
+ * @created Mar 8, 2008
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class RegexFn extends BinaryTestFilter {
+
+  /** a cache of the last pattern */ 
+  private Pattern pattern = null;
+
+  /** The expression that resolves flags */
+  private ValueLiteral flagExpression = null;
+
+  /** a cache of the last flag string */
+  private String flagsStr = null;
+
+  /** a cache of the last flags */
+  private int flags = 0;
+
+  /**
+   * Creates an equality test operation with default flags
+   * @param lhs The first term to compare
+   * @param rhs The second term to compare
+   */
+  RegexFn(ValueLiteral str, ValueLiteral patternStr) {
+    super(str, patternStr);
+  }
+
+  /**
+   * Creates an equality test operation with default flags
+   * @param lhs The first term to compare
+   * @param rhs The second term to compare
+   */
+  RegexFn(ValueLiteral str, ValueLiteral patternStr, ValueLiteral flagExpression) {
+    super(str, patternStr);
+    flagExpression.setContextOwner(this);
+  }
+
+  /** @see org.mulgara.query.filter.BinaryTestFilter#testCmp() */
+  boolean testCmp() throws QueryException {
+    return pattern().matcher(str()).matches();
+  }
+
+  /**
+   * Gets the string to be matched in this regular expression.
+   * @return The string to be matched against.
+   * @throws QueryException If the expression for the string cannot be resolved.
+   */
+  private String str() throws QueryException {
+    if (!lhs.isLiteral()) throw new QueryException("Invalid type in regular expression. Need string, got: " + lhs.getClass().getSimpleName());
+    return ((ValueLiteral)lhs).getLexical();
+  }
+
+  /**
+   * Gets the Pattern to use for the current variable bindings. This will calculate a new pattern
+   * and flags if either change for the current variable bindings.
+   * @return A Pattern for regex matching, using the existing pattern if there was no update.
+   * @throws QueryException If the pattern string or flags string cannot be resolved.
+   */
+  private Pattern pattern() throws QueryException {
+    if (!rhs.isLiteral()) throw new QueryException("Invalid pattern type in regular expression. Need string, got: " + rhs.getClass().getSimpleName());
+    String patternStr = ((ValueLiteral)rhs).getLexical();
+    int oldFlags = flags;
+    if (pattern == null || !patternStr.equals(pattern.pattern()) || oldFlags != flags()) {
+      pattern = Pattern.compile(patternStr, flags);
+    }
+    return pattern;
+  }
+
+  /** Characters used for regex flags */
+  private static final String optionChars = "smix";
+  /** Regex flags that correspond to the optionChars */
+  private static final int[] optionFlags = new int[] { DOTALL, MULTILINE, CASE_INSENSITIVE, COMMENTS };
+
+  /**
+   * Gets the flags to use for this regex call. This will calculate new flags is the expression
+   * the flags come from is updated.
+   * @return An int with the flags for the current binding. Returns 0 if no flags are to be used.
+   * @throws QueryException The expression the flags are built on cannot be resolved.
+   */
+  private int flags() throws QueryException {
+    if (flagExpression == null) return 0;
+    String currentFlagStr = flagExpression.getLexical();
+    if (flagsStr == null || !flagsStr.equals(currentFlagStr)) {
+      flagsStr = currentFlagStr;
+      // calculate the new flags
+      flags = 0;
+      for (int i = 0; i < optionChars.length(); i++) {
+        if (flagsStr.indexOf(optionChars.charAt(i)) != -1) flags |= optionFlags[i];
+      }
+    }
+    return flags;
+  }
+  
+}

Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/SameTerm.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/SameTerm.java	                        (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/SameTerm.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -0,0 +1,42 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter;
+
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.value.RDFTerm;
+
+
+/**
+ * The sameterm comparison for Values.
+ *
+ * @created Mar 17, 2008
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class SameTerm extends BinaryTestFilter {
+
+  /**
+   * Creates a sameterm test operation
+   * @param lhs The first term to compare
+   * @param rhs The second term to compare
+   */
+  SameTerm(RDFTerm lhs, RDFTerm rhs) {
+    super(lhs, rhs);
+  }
+
+  /** @see org.mulgara.query.filter.BinaryTestFilter#testCmp() */
+  boolean testCmp() throws QueryException {
+    return lhs.sameTerm(rhs);
+  }
+
+}

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractNumericOperation.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractNumericOperation.java	2008-03-18 11:07:25 UTC (rev 692)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractNumericOperation.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -22,7 +22,7 @@
   public Object getValue() throws QueryException { return getNumber(); }
 
   /** @see org.mulgara.query.filter.value.RDFTerm#equals() */
-  public boolean equals(RDFTerm v) throws QueryException { return getNumber().equals(v.getValue()); }
+  public boolean equals(RDFTerm v) throws QueryException { return compare(getNumber(), v.getValue()) == 0; }
 
   /** @see org.mulgara.query.filter.value.RDFTerm#isBlank() */
   public boolean isBlank() throws QueryException { return false; }
@@ -37,10 +37,10 @@
   public boolean isURI() throws QueryException { return false; }
 
   /** @see org.mulgara.query.filter.value.RDFTerm#notEquals() */
-  public boolean notEquals(RDFTerm v) throws QueryException { return !getNumber().equals(v.getValue()); }
+  public boolean notEquals(RDFTerm v) throws QueryException { return compare(getNumber(), v.getValue()) != 0; }
 
   /** @see org.mulgara.query.filter.value.RDFTerm#sameTerm() */
-  public boolean sameTerm(RDFTerm v) throws QueryException { return equals(v); }
+  public boolean sameTerm(RDFTerm v) throws QueryException { return getNumber().equals(v.getValue()); }
 
   /** @see org.mulgara.query.filter.Filter#test() */
   public boolean test(Context context) throws QueryException {

Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractAccessorFn.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractAccessorFn.java	                        (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractAccessorFn.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -0,0 +1,162 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.value;
+
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.Context;
+import org.mulgara.query.filter.ContextOwner;
+import org.mulgara.query.filter.Filter;
+
+
+/**
+ * Represents a function that accesses an RDF value.
+ * This node acts as an RDFTerm and as a ValueLiteral, so that it can act as
+ * either a Literal (Simple or Typed) or an IRI.
+ * By default, this object will at as a literal.
+ *
+ * @created Mar 17, 2008
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public abstract class AbstractAccessorFn implements ValueLiteral {
+
+  /** The operand for the function */
+  RDFTerm operand;
+
+  /** The owner of the current context */
+  private ContextOwner contextOwner = null;
+
+  public AbstractAccessorFn(RDFTerm operand) {
+    this.operand = operand;
+    operand.setContextOwner(this);
+  }
+
+  /**
+   * @see org.mulgara.query.filter.value.ValueLiteral#getLang()
+   * @throws QueryException if this function does not resolve to a literal
+   */
+  public SimpleLiteral getLang() throws QueryException {
+    return SimpleLiteral.EMPTY;
+  }
+
+  /** @see org.mulgara.query.filter.value.ValueLiteral#getType() */
+  public IRI getType() throws QueryException {
+    return SimpleLiteral.STRING_TYPE;
+  }
+
+  /**
+   * @see org.mulgara.query.filter.value.ValueLiteral#getLexical()
+   * @throws QueryException if this function does not resolve to a literal
+   */
+  public String getLexical() throws QueryException {
+    return (String)getValue();
+  }
+
+  /**
+   * @see org.mulgara.query.filter.value.ValueLiteral#getValue()
+   * @return a string, for the result.  Never null.
+   * @throws QueryException if this function does not resolve to a literal
+   */
+  public Object getValue() throws QueryException {
+    return resolve().getValue();
+  }
+
+  /** @see org.mulgara.query.filter.value.ValueLiteral#test(org.mulgara.query.filter.Context) */
+  public boolean test(Context context) throws QueryException {
+    setCurrentContext(context);
+    RDFTerm term = resolve();
+    if (term.isLiteral()) {
+      return ((ValueLiteral)term).test(context);
+    } else if (term instanceof Filter) {
+      return ((Filter)term).test(context);
+    }
+    throw new QueryException("Type error. Cannot get a boolean from a: " + term.getClass().getSimpleName());
+  }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#equals(org.mulgara.query.filter.value.RDFTerm) */
+  public boolean equals(RDFTerm v) throws QueryException {
+    return resolve().equals(v);
+  }
+
+  /**
+   * @see org.mulgara.query.filter.value.RDFTerm#isBlank()
+   * May be overridden.
+   */
+  public boolean isBlank() throws QueryException {
+    return false;
+  }
+
+  /**
+   * @see org.mulgara.query.filter.value.RDFTerm#isIRI()
+   * May be overridden.
+   */
+  public boolean isIRI() throws QueryException {
+    return false;
+  }
+
+  /**
+   * @see org.mulgara.query.filter.value.RDFTerm#isLiteral()
+   * May be overridden.
+   */
+  public boolean isLiteral() throws QueryException {
+    return true;
+  }
+
+  /**
+   * @see org.mulgara.query.filter.value.RDFTerm#isURI()
+   * May be overridden.
+   */
+  public boolean isURI() throws QueryException {
+    return isIRI();
+  }
+
+  /**
+   * @see org.mulgara.query.filter.value.RDFTerm#notEquals(org.mulgara.query.filter.value.RDFTerm)
+   * May be overridden.
+   */
+  public boolean notEquals(RDFTerm v) throws QueryException {
+    return !resolve().equals(v);
+  }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#sameTerm(org.mulgara.query.filter.value.RDFTerm) */
+  public boolean sameTerm(RDFTerm v) throws QueryException {
+    return resolve().sameTerm(v);
+  }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#getContextOwner() */
+  public ContextOwner getContextOwner() {
+    return contextOwner;
+  }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#setContextOwner(org.mulgara.query.filter.ContextOwner) */
+  public void setContextOwner(ContextOwner owner) {
+    contextOwner = owner;
+  }
+
+  /** @see org.mulgara.query.filter.ContextOwner#getCurrentContext() */
+  public Context getCurrentContext() {
+    return contextOwner.getCurrentContext();
+  }
+
+  /** @see org.mulgara.query.filter.ContextOwner#setCurrentContext(org.mulgara.query.filter.Context) */
+  public void setCurrentContext(Context context) {
+    if (!(context.equals(contextOwner.getCurrentContext()))) throw new AssertionError("Function context being set differently to initial calling context.");
+  }
+
+  /**
+   * Resolves this function into whatever the return type should be (Literal or URI).
+   * @return The resolved value for the function.
+   * @throws QueryException There was an error resolving the value against the context.
+   */
+  abstract RDFTerm resolve() throws QueryException;
+}

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparable.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparable.java	2008-03-18 11:07:25 UTC (rev 692)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparable.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -25,6 +25,16 @@
     return !lessThan(v);
   }
 
+  /** {@inheritDoc} */
+  public boolean equals(ComparableExpression v) throws QueryException {
+    return compare(getValue(), v.getValue()) == 0;
+  }
+
+  /** {@inheritDoc} */
+  public boolean notEquals(ComparableExpression v) throws QueryException {
+    return compare(getValue(), v.getValue()) != 0;
+  }
+
   /**
    * Compares elements of the type handled by the implementing class.
    * @param left The LHS of the comparison

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparableLiteral.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparableLiteral.java	2008-03-18 11:07:25 UTC (rev 692)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparableLiteral.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -70,24 +70,61 @@
   /** {@inheritDoc} */
   public boolean sameTerm(RDFTerm v) {
     try {
-      return value.equals(v.getValue());
+      if (!(v instanceof ValueLiteral)) return false;
+      IRI type = ((ValueLiteral)v).getType();
+      if (type == null || !type.equals(getType())) return false;
+      return getValue().equals(v.getValue());
     } catch (QueryException qe) {
       return false;
     }
   }
 
 
-  /** {@inheritDoc} */
+  /**
+   * {@inheritDoc}
+   * This method will only return <code>true</code> when the elements are identical,
+   * or if they have differing numeric types compare equal in number space.
+   * Since this object is a literal, then an incorrect comparison will throw an exception.
+   * {@link http://www.w3.org/TR/rdf-sparql-query/#func-RDFterm-equal}
+   */
   public boolean equals(RDFTerm v) throws QueryException {
-    return value.equals(v.getValue());
+    if (!(v instanceof ValueLiteral)) return false;
+    IRI type = ((ValueLiteral)v).getType();
+    // check for differing types
+    if (type == null || !type.equals(getType())) {
+      // only handle equivalent numbers
+      return numberCompare(v);
+    }
+    // terms are the same type
+    if (getValue().equals(v.getValue())) return true;
+    throw new QueryException("Terms are not equal");
   }
   
-  /** {@inheritDoc} */
+  /**
+   * {@inheritDoc}
+   * Not the inverse of #equals().  This method returns true when the elements connot be determined
+   * to be equal.
+   */
   public boolean notEquals(RDFTerm v) throws QueryException {
-    return !value.equals(v.getValue());
+    try {
+      return !equals(v);
+    } catch (QueryException qe) {
+      return true;
+    }
   }
 
   /**
+   * Extended numerical comparison function, for use with equals.
+   * @param v The term to compare against.
+   * @return <code>true</code> if this compares against v with semantic equivalence, regardless of lexical equivalence
+   * @throws QueryException Thrown when a value cannot be resolved, or if the types are no numbers.
+   */
+  private boolean numberCompare(RDFTerm v) throws QueryException {
+    if (!(value instanceof Number) || !(v.getValue() instanceof Number)) throw new QueryException("Terms are not equal");
+    return compare(value, v) == 0;
+  }
+
+  /**
    * Type-based switching to handle comparison of things that may not be directly comparable.
    * @param left The first thing to compare
    * @param right The second thing to compare

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BlankNodeValue.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BlankNodeValue.java	2008-03-18 11:07:25 UTC (rev 692)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BlankNodeValue.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -53,8 +53,8 @@
   }
 
   /** {@inheritDoc} */
-  public boolean sameTerm(RDFTerm v) {
-    return v instanceof BlankNodeValue && node.equals(((BlankNodeValue)v).node);
+  public boolean sameTerm(RDFTerm v) throws QueryException {
+    return equals(v);
   }
 
   /** {@inheritDoc} */

Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFn.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFn.java	                        (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFn.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -0,0 +1,75 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.value;
+
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.Context;
+
+/**
+ * Represents a datatype() function that accesses the datatype on a literal.
+ * This operation is made to look like an IRI.
+ *
+ * @created Mar 17, 2008
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class DataTypeFn extends AbstractAccessorFn {
+
+  /**
+   * Create a new language extractor function.
+   * @param operand The operand for the function.
+   */
+  public DataTypeFn(RDFTerm operand) {
+    super(operand);
+  }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#isLiteral() */
+  public boolean isLiteral() throws QueryException {
+    return false;
+  }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#isIri() */
+  public boolean isIri() throws QueryException {
+    return true;
+  }
+
+  /** @see org.mulgara.query.filter.value.AbstractAccessorFn#getLexical() */
+  public String getLexical() throws QueryException {
+    throw new QueryException("Invalid to get lexical information on an IRI.");
+  }
+
+  /** @see org.mulgara.query.filter.value.AbstractAccessorFn#getLang() */
+  public SimpleLiteral getLang() throws QueryException {
+    throw new QueryException("Invalid to get a language on an IRI.");
+  }
+
+  /** @see org.mulgara.query.filter.value.AbstractAccessorFn#getType() */
+  public IRI getType() throws QueryException {
+    throw new QueryException("Invalid to get a type IRI on an IRI.");
+  }
+
+  /** @see org.mulgara.query.filter.value.AbstractAccessorFn#test(org.mulgara.query.filter.Context) */
+  public boolean test(Context context) throws QueryException {
+    throw new QueryException("Invalid to get an effective boolean value on an IRI.");
+  }
+
+  /**
+   * Get the type from the underlying operand
+   * @return The operands type IRI
+   * @throws QueryException If there was an error resolving the operand 
+   */
+  RDFTerm resolve() throws QueryException {
+    if (!operand.isLiteral()) throw new QueryException("Disallowed type in DATATYPE function. Expected a Literal. Got a : " + operand.getClass().getSimpleName());
+    return ((ValueLiteral)operand).getType();
+  }
+}

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/IRI.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/IRI.java	2008-03-18 11:07:25 UTC (rev 692)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/IRI.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -51,7 +51,7 @@
 
   /** {@inheritDoc} */
   public boolean notEquals(RDFTerm v) throws QueryException {
-    return !v.isIRI() || !value.equals(((IRI)v).getValue());
+    return !equals(v);
   }
 
   /** {@inheritDoc} */

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/SimpleLiteral.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/SimpleLiteral.java	2008-03-18 11:07:25 UTC (rev 692)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/SimpleLiteral.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -29,8 +29,12 @@
   /** An empty simple literal */
   public static final SimpleLiteral EMPTY = new SimpleLiteral("");
 
+  /** The language tag for a simple literal */
   private SimpleLiteral lang = EMPTY;
 
+  /** The language tag for a simple literal */
+  static final IRI STRING_TYPE = new IRI(new TypedLiteral.XSDString().getTypeURI());
+
   /**
    * Creates the value to wrap the string
    * @param s The string to wrap
@@ -63,6 +67,14 @@
   }
 
   /**
+   * Gets the type of this literal
+   * @return Always null.
+   */
+  public IRI getType() {
+    return STRING_TYPE;
+  }
+
+  /**
    * @see org.mulgara.query.filter.value.ComparableExpression#test()
    */
   public boolean test(Context context) throws QueryException {

Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/StrFn.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/StrFn.java	                        (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/StrFn.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -0,0 +1,59 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.value;
+
+import org.mulgara.query.QueryException;
+
+/**
+ * Represents a str() function that stringizes any underlying data.
+ * This operation is made to look like a SimpleLiteral.
+ *
+ * @created Mar 17, 2008
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class StrFn extends AbstractAccessorFn {
+
+  /**
+   * Create a new string extractor function.
+   * @param operand The operand for the function.
+   */
+  public StrFn(RDFTerm operand) {
+    super(operand);
+  }
+
+  /**
+   * @see org.mulgara.query.filter.value.ValueLiteral#getLang()
+   * @return Always empty, as the result of this operation is a simple literal, which has no type.
+   */
+  public SimpleLiteral getLang() throws QueryException {
+    return SimpleLiteral.EMPTY;
+  }
+
+  /**
+   * @see org.mulgara.query.filter.value.ValueLiteral#getLexical()
+   * @throws QueryException if the operand does not resolve
+   */
+  public String getLexical() throws QueryException {
+    return operand.getValue().toString();
+  }
+
+  /**
+   * Resolve the value of the operand, and create a literal for it.
+   * @return The operand
+   * @throws QueryException if the operand does not resolve
+   */
+  RDFTerm resolve() throws QueryException {
+    return new SimpleLiteral(getLexical());
+  }
+}

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java	2008-03-18 11:07:25 UTC (rev 692)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -189,7 +189,7 @@
   // Implementing classes
   //////////////////////////////////////////////////////////////////
 
-  private static class XSDString extends AbstractXSD {
+  static class XSDString extends AbstractXSD {
     XSDString() { super("string"); }
     public boolean ebv(String data) { return data != null && data.length() != 0; }
     public Object toData(String r) { return r; }

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/ValueLiteral.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/ValueLiteral.java	2008-03-18 11:07:25 UTC (rev 692)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/ValueLiteral.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -46,10 +46,19 @@
   /**
    * Gets the language code for this literal, if it exists.
    * @return a language code, or an empty string if none is present
+   * @throws QueryException Resolving the data for this value leads to an error.
    */
   public SimpleLiteral getLang() throws QueryException;
-  
+
+
   /**
+   * Gets the type of this literal
+   * @return The URI for this literals type
+   * @throws QueryException Resolving the data for this value leads to an error.
+   */
+  public IRI getType() throws QueryException;
+
+  /**
    * Calculates the Effective Boolean Value (EBV) of this literal.
    * @param context The context to determine this value in. Ignored because this is a literal.
    * @return <code>true</code> when the EBV conditions are met.

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Var.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Var.java	2008-03-18 11:07:25 UTC (rev 692)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Var.java	2008-03-18 21:49:52 UTC (rev 693)
@@ -102,6 +102,12 @@
     return e.getValue().toString();
   }
 
+  public IRI getType() throws QueryException {
+    ComparableExpression e = resolveComparable();
+    if (!e.isLiteral()) throw new QueryException("Only literals are typed");
+    return ((ValueLiteral)e).getType();
+  }
+
   /** {@inheritDoc} */
   public boolean equals(RDFTerm v) throws QueryException {
     return resolveComparable().equals(v);




More information about the Mulgara-svn mailing list