lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rm...@apache.org
Subject svn commit: r1522767 [1/5] - in /lucene/dev/branches/lucene5207: dev-tools/scripts/ lucene/ lucene/core/src/java/org/apache/lucene/util/ lucene/core/src/test/org/apache/lucene/util/ lucene/expressions/ lucene/expressions/src/ lucene/expressions/src/jav...
Date Fri, 13 Sep 2013 03:11:18 GMT
Author: rmuir
Date: Fri Sep 13 03:11:17 2013
New Revision: 1522767

URL: http://svn.apache.org/r1522767
Log:
LUCENE-5207: initial patch from ryan

Added:
    lucene/dev/branches/lucene5207/lucene/expressions/
    lucene/dev/branches/lucene5207/lucene/expressions/build.xml   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/ivy.xml   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/Bindings.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/Expression.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionComparator.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ScoreFunctionValues.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ScoreValueSource.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/SimpleBindings.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/Javascript.g   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/Javascript.tokens
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptFunction.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptLexer.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptParser.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/package.html   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/package.html   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/overview.html   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/TestDemoExpressions.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionValidation.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/js/
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestJavascriptCompiler.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestJavascriptFunction.java   (with props)
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestJavascriptOperations.java   (with props)
    lucene/dev/branches/lucene5207/lucene/licenses/antlr-3.4.jar.sha1   (with props)
    lucene/dev/branches/lucene5207/lucene/licenses/antlr-LICENSE-BSD_LIKE.txt   (with props)
    lucene/dev/branches/lucene5207/lucene/licenses/antlr-NOTICE.txt   (with props)
    lucene/dev/branches/lucene5207/lucene/licenses/asm-4.1.jar.sha1   (with props)
    lucene/dev/branches/lucene5207/lucene/licenses/asm-LICENSE-BSD_LIKE.txt   (with props)
    lucene/dev/branches/lucene5207/lucene/licenses/asm-NOTICE.txt   (with props)
Modified:
    lucene/dev/branches/lucene5207/dev-tools/scripts/smokeTestRelease.py
    lucene/dev/branches/lucene5207/lucene/build.xml
    lucene/dev/branches/lucene5207/lucene/common-build.xml
    lucene/dev/branches/lucene5207/lucene/core/src/java/org/apache/lucene/util/MathUtil.java
    lucene/dev/branches/lucene5207/lucene/core/src/test/org/apache/lucene/util/TestMathUtil.java
    lucene/dev/branches/lucene5207/lucene/module-build.xml

Modified: lucene/dev/branches/lucene5207/dev-tools/scripts/smokeTestRelease.py
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/dev-tools/scripts/smokeTestRelease.py?rev=1522767&r1=1522766&r2=1522767&view=diff
==============================================================================
--- lucene/dev/branches/lucene5207/dev-tools/scripts/smokeTestRelease.py (original)
+++ lucene/dev/branches/lucene5207/dev-tools/scripts/smokeTestRelease.py Fri Sep 13 03:11:17 2013
@@ -651,7 +651,7 @@ def verifyUnpacked(project, artifact, un
 
   if project == 'lucene':
     # TODO: clean this up to not be a list of modules that we must maintain
-    extras = ('analysis', 'benchmark', 'classification', 'codecs', 'core', 'demo', 'docs', 'facet', 'grouping', 'highlighter', 'join', 'memory', 'misc', 'queries', 'queryparser', 'replicator', 'sandbox', 'spatial', 'suggest', 'test-framework', 'licenses')
+    extras = ('analysis', 'benchmark', 'classification', 'codecs', 'core', 'demo', 'docs', 'expressions', 'facet', 'grouping', 'highlighter', 'join', 'memory', 'misc', 'queries', 'queryparser', 'replicator', 'sandbox', 'spatial', 'suggest', 'test-framework', 'licenses')
     if isSrc:
       extras += ('build.xml', 'common-build.xml', 'module-build.xml', 'ivy-settings.xml', 'backwards', 'tools', 'site')
   else:

Modified: lucene/dev/branches/lucene5207/lucene/build.xml
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/build.xml?rev=1522767&r1=1522766&r2=1522767&view=diff
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/build.xml (original)
+++ lucene/dev/branches/lucene5207/lucene/build.xml Fri Sep 13 03:11:17 2013
@@ -283,6 +283,7 @@
     <!-- codecs: problems -->
     <!-- core: problems -->
     <check-missing-javadocs dir="build/docs/demo" level="method"/>
+    <check-missing-javadocs dir="build/docs/expressions" level="method"/>
     <!-- facet: problems -->
     <!-- grouping: problems -->
     <!-- highlighter: problems -->

Modified: lucene/dev/branches/lucene5207/lucene/common-build.xml
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/common-build.xml?rev=1522767&r1=1522766&r2=1522767&view=diff
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/common-build.xml (original)
+++ lucene/dev/branches/lucene5207/lucene/common-build.xml Fri Sep 13 03:11:17 2013
@@ -1567,6 +1567,8 @@ ${tests-output}/junit4-*.suites     - pe
         <pattern substring="This file was generated automatically by the Snowball to Java compiler"/>
       <!-- uima tests generated by JCasGen -->
         <pattern substring="First created by JCasGen"/>
+      <!-- parsers generated by antlr -->
+        <pattern substring="ANTLR GENERATED CODE"/>
       </rat:substringMatcher>
 
       <!-- built in approved licenses -->

Modified: lucene/dev/branches/lucene5207/lucene/core/src/java/org/apache/lucene/util/MathUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/core/src/java/org/apache/lucene/util/MathUtil.java?rev=1522767&r1=1522766&r2=1522767&view=diff
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/core/src/java/org/apache/lucene/util/MathUtil.java (original)
+++ lucene/dev/branches/lucene5207/lucene/core/src/java/org/apache/lucene/util/MathUtil.java Fri Sep 13 03:11:17 2013
@@ -44,6 +44,13 @@ public final class MathUtil {
     return ret;
   }
 
+  /**
+   * Calculates logarithm in a given base with doubles.
+   */
+  public static double log(double base, double x) {
+    return Math.log(x) / Math.log(base);
+  }
+
   /** Return the greatest common divisor of <code>a</code> and <code>b</code>,
    *  consistently with {@link BigInteger#gcd(BigInteger)}.
    *  <p><b>NOTE</b>: A greatest common divisor must be positive, but
@@ -78,4 +85,69 @@ public final class MathUtil {
     }
     return a << commonTrailingZeros;
   }
+
+
+  /**
+   * Calculates inverse hyperbolic sine of a {@code double} value.
+   * <p>
+   * Special cases:
+   * <ul>
+   *    <li>If the argument is NaN, then the result is NaN.
+   *    <li>If the argument is zero, then the result is a zero with the same sign as the argument.
+   *    <li>If the argument is infinite, then the result is infinity with the same sign as the argument.
+   * </ul>
+   */
+  public static double asinh(double a) {
+    final double sign;
+    // check the sign bit of the raw representation to handle -0
+    if (Double.doubleToRawLongBits(a) < 0) {
+      a = Math.abs(a);
+      sign = -1.0d;
+    } else {
+      sign = 1.0d;
+    }
+
+    return sign * Math.log(Math.sqrt(a * a + 1.0d) + a);
+  }
+
+  /**
+   * Calculates inverse hyperbolic cosine of a {@code double} value.
+   * <p>
+   * Special cases:
+   * <ul>
+   *    <li>If the argument is NaN, then the result is NaN.
+   *    <li>If the argument is +1, then the result is a zero.
+   *    <li>If the argument is positive infinity, then the result is positive infinity.
+   *    <li>If the argument is less than 1, then the result is NaN.
+   * </ul>
+   */
+  public static double acosh(double a) {
+    return Math.log(Math.sqrt(a * a - 1.0d) + a);
+  }
+
+  /**
+   * Calculates inverse hyperbolic tangent of a {@code double} value.
+   * <p>
+   * Special cases:
+   * <ul>
+   *    <li>If the argument is NaN, then the result is NaN.
+   *    <li>If the argument is zero, then the result is a zero with the same sign as the argument.
+   *    <li>If the argument is +1, then the result is positive infinity.
+   *    <li>If the argument is -1, then the result is negative infinity.
+   *    <li>If the argument's absolute value is greater than 1, then the result is NaN.
+   * </ul>
+   */
+  public static double atanh(double a) {
+    final double mult;
+    // check the sign bit of the raw representation to handle -0
+    if (Double.doubleToRawLongBits(a) < 0) {
+      a = Math.abs(a);
+      mult = -0.5d;
+    } else {
+      mult = 0.5d;
+    }
+    return mult * Math.log((1.0d + a) / (1.0d - a));
+  }
+
+
 }

Modified: lucene/dev/branches/lucene5207/lucene/core/src/test/org/apache/lucene/util/TestMathUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/core/src/test/org/apache/lucene/util/TestMathUtil.java?rev=1522767&r1=1522766&r2=1522767&view=diff
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/core/src/test/org/apache/lucene/util/TestMathUtil.java (original)
+++ lucene/dev/branches/lucene5207/lucene/core/src/test/org/apache/lucene/util/TestMathUtil.java Fri Sep 13 03:11:17 2013
@@ -102,4 +102,80 @@ public class TestMathUtil extends Lucene
     assertEquals(Long.MIN_VALUE, MathUtil.gcd(Long.MIN_VALUE, Long.MIN_VALUE));
   }
 
+  public void testAcoshMethod() {
+    // acosh(NaN) == NaN
+    assertTrue(Double.isNaN(MathUtil.acosh(Double.NaN)));
+    // acosh(1) == +0
+    assertEquals(0, Double.doubleToLongBits(MathUtil.acosh(1D)));
+    // acosh(POSITIVE_INFINITY) == POSITIVE_INFINITY
+    assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY),
+        Double.doubleToLongBits(MathUtil.acosh(Double.POSITIVE_INFINITY)));
+    // acosh(x) : x < 1 == NaN
+    assertTrue(Double.isNaN(MathUtil.acosh(0.9D)));                      // x < 1
+    assertTrue(Double.isNaN(MathUtil.acosh(0D)));                        // x == 0
+    assertTrue(Double.isNaN(MathUtil.acosh(-0D)));                       // x == -0
+    assertTrue(Double.isNaN(MathUtil.acosh(-0.9D)));                     // x < 0
+    assertTrue(Double.isNaN(MathUtil.acosh(-1D)));                       // x == -1
+    assertTrue(Double.isNaN(MathUtil.acosh(-10D)));                      // x < -1
+    assertTrue(Double.isNaN(MathUtil.acosh(Double.NEGATIVE_INFINITY)));  // x == -Inf
+
+    double epsilon = 0.000001;
+    assertEquals(0, MathUtil.acosh(1), epsilon);
+    assertEquals(1.5667992369724109, MathUtil.acosh(2.5), epsilon);
+    assertEquals(14.719378760739708, MathUtil.acosh(1234567.89), epsilon);
+  }
+
+  public void testAsinhMethod() {
+
+    // asinh(NaN) == NaN
+    assertTrue(Double.isNaN(MathUtil.asinh(Double.NaN)));
+    // asinh(+0) == +0
+    assertEquals(0, Double.doubleToLongBits(MathUtil.asinh(0D)));
+    // asinh(-0) == -0
+    assertEquals(Double.doubleToLongBits(-0D), Double.doubleToLongBits(MathUtil.asinh(-0D)));
+    // asinh(POSITIVE_INFINITY) == POSITIVE_INFINITY
+    assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY),
+        Double.doubleToLongBits(MathUtil.asinh(Double.POSITIVE_INFINITY)));
+    // asinh(NEGATIVE_INFINITY) == NEGATIVE_INFINITY
+    assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY),
+        Double.doubleToLongBits(MathUtil.asinh(Double.NEGATIVE_INFINITY)));
+
+    double epsilon = 0.000001;
+    assertEquals(-14.719378760740035, MathUtil.asinh(-1234567.89), epsilon);
+    assertEquals(-1.6472311463710958, MathUtil.asinh(-2.5), epsilon);
+    assertEquals(-0.8813735870195429, MathUtil.asinh(-1), epsilon);
+    assertEquals(0, MathUtil.asinh(0), 0);
+    assertEquals(0.8813735870195429, MathUtil.asinh(1), epsilon);
+    assertEquals(1.6472311463710958, MathUtil.asinh(2.5), epsilon);
+    assertEquals(14.719378760740035, MathUtil.asinh(1234567.89), epsilon  );
+  }
+
+  public void testAtanhMethod() {
+    // atanh(NaN) == NaN
+    assertTrue(Double.isNaN(MathUtil.atanh(Double.NaN)));
+    // atanh(+0) == +0
+    assertEquals(0, Double.doubleToLongBits(MathUtil.atanh(0D)));
+    // atanh(-0) == -0
+    assertEquals(Double.doubleToLongBits(-0D),
+        Double.doubleToLongBits(MathUtil.atanh(-0D)));
+    // atanh(1) == POSITIVE_INFINITY
+    assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY),
+        Double.doubleToLongBits(MathUtil.atanh(1D)));
+    // atanh(-1) == NEGATIVE_INFINITY
+    assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY),
+        Double.doubleToLongBits(MathUtil.atanh(-1D)));
+    // atanh(x) : Math.abs(x) > 1 == NaN
+    assertTrue(Double.isNaN(MathUtil.atanh(1.1D)));                      // x > 1
+    assertTrue(Double.isNaN(MathUtil.atanh(Double.POSITIVE_INFINITY)));  // x == Inf
+    assertTrue(Double.isNaN(MathUtil.atanh(-1.1D)));                     // x < -1
+    assertTrue(Double.isNaN(MathUtil.atanh(Double.NEGATIVE_INFINITY)));  // x == -Inf
+
+    double epsilon = 0.000001;
+    assertEquals(Double.NEGATIVE_INFINITY, MathUtil.atanh(-1), 0);
+    assertEquals(-0.5493061443340549, MathUtil.atanh(-0.5), epsilon);
+    assertEquals(0, MathUtil.atanh(0), 0);
+    assertEquals(0.5493061443340549, MathUtil.atanh(0.5), epsilon);
+    assertEquals(Double.POSITIVE_INFINITY, MathUtil.atanh(1), 0);
+  }
+
 }

Added: lucene/dev/branches/lucene5207/lucene/expressions/build.xml
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/build.xml?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/build.xml (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/build.xml Fri Sep 13 03:11:17 2013
@@ -0,0 +1,103 @@
+<?xml version="1.0"?>
+<!--
+    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.
+ -->
+<project name="expressions" default="default">
+
+  <description>
+    Dynamically computed values to sort/facet/search on based on a pluggable grammar.
+  </description>
+
+  <!-- some files for testing that do not have license headers -->
+  <property name="rat.excludes" value="**/*.tokens"/>
+
+  <import file="../module-build.xml"/>
+
+  <path id="classpath">
+    <path refid="base.classpath"/>
+    <fileset dir="lib"/>
+    <pathelement path="${queries.jar}"/>
+  </path>
+
+  <path id="test.classpath">
+    <path refid="test.base.classpath"/>
+    <fileset dir="lib"/>
+    <pathelement path="src/test-files"/>
+  </path>
+
+  <target name="compile-core" depends="jar-queries,common.compile-core" />
+
+  <target name="javadocs" depends="javadocs-queries,compile-core">
+    <invoke-module-javadoc>
+      <links>
+        <link href="../queries"/>
+      </links>
+    </invoke-module-javadoc>
+  </target>
+
+  <target name="regenerate" depends="run-antlr"/>
+
+  <target name="resolve-antlr" xmlns:ivy="antlib:org.apache.ivy.ant">
+    <ivy:cachepath organisation="org.antlr" module="antlr" revision="3.4"
+                  inline="true" conf="default" type="jar" pathid="antlr.classpath"/>
+  </target>
+
+  <target name="run-antlr" depends="resolve-antlr">
+    <regen-grammar package="js" grammar="Javascript"/>
+  </target>
+
+  <macrodef name="regen-grammar">
+    <attribute name="package" />
+    <attribute name="grammar" />
+    <sequential>
+      <!-- delete parser and lexer so files will be generated -->
+      <delete>
+        <fileset dir="src/java/org/apache/lucene/expressions/@{package}">
+          <include name="@{grammar}Lexer.java" />
+          <include name="@{grammar}Parser.java" />
+        </fileset>
+      </delete>
+      <local name="grammar.path"/>
+      <local name="grammar.relative.path"/>
+      <property name="grammar.path" location="src/java/org/apache/lucene/expressions/@{package}"/>
+      <property name="grammar.relative.path" location="${grammar.path}" relative="true"/>
+      <java classname="org.antlr.Tool" fork="true" failonerror="true" classpathref="antlr.classpath">
+        <arg value="-verbose"/>
+        <arg value="-make"/>
+        <arg value="-o"/>
+        <arg path="${grammar.path}"/>
+        <arg path="${grammar.path}/@{grammar}.g"/>
+      </java>
+      <!-- prevent warnings from generated code -->
+      <replaceregexp file="${grammar.path}/@{grammar}Parser.java"
+                     encoding="UTF-8"
+                     byline="true">
+
+        <regexp pattern="@SuppressWarnings(.*)" />
+        <substitution expression="@SuppressWarnings({&quot;all&quot;, &quot;warnings&quot;, &quot;unchecked&quot;, &quot;cast&quot;})" />
+      </replaceregexp>
+      <!-- replace absolute paths by relative ones -->
+      <replace file="${grammar.path}/@{grammar}Parser.java" token="${grammar.path}" value="${grammar.relative.path}" encoding="UTF-8"/>
+      <replace file="${grammar.path}/@{grammar}Lexer.java" token="${grammar.path}" value="${grammar.relative.path}" encoding="UTF-8"/>
+      <!-- make the generated classes package private (it's an antlr option with 4.0) -->
+      <replace file="${grammar.path}/@{grammar}Parser.java" token="public class @{grammar}Parser" value="class @{grammar}Parser" encoding="UTF-8"/>
+      <replace file="${grammar.path}/@{grammar}Lexer.java" token="public class @{grammar}Lexer" value="class @{grammar}Lexer" encoding="UTF-8"/>
+      <!-- nuke timestamps in generated files -->
+      <replaceregexp file="${grammar.path}/@{grammar}Parser.java" match=".*" replace="\/\/ ANTLR GENERATED CODE: DO NOT EDIT" encoding="UTF-8"/>
+      <replaceregexp file="${grammar.path}/@{grammar}Lexer.java" match=".*" replace="\/\/ ANTLR GENERATED CODE: DO NOT EDIT" encoding="UTF-8"/>
+    </sequential>
+  </macrodef>
+</project>

Added: lucene/dev/branches/lucene5207/lucene/expressions/ivy.xml
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/ivy.xml?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/ivy.xml (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/ivy.xml Fri Sep 13 03:11:17 2013
@@ -0,0 +1,26 @@
+<!--
+   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.    
+-->
+<ivy-module version="2.0">
+    <info organisation="org.apache.lucene" module="expressions"/>
+    <dependencies>
+      <dependency org="org.antlr" name="antlr-runtime" rev="3.4" transitive="false"/>
+      <dependency org="org.ow2.asm" name="asm" rev="4.1" transitive="false"/>
+      <exclude org="*" ext="*" matcher="regexp" type="${ivy.exclude.types}"/>
+    </dependencies>
+</ivy-module>

Added: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/Bindings.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/Bindings.java?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/Bindings.java (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/Bindings.java Fri Sep 13 03:11:17 2013
@@ -0,0 +1,87 @@
+package org.apache.lucene.expressions;
+/*
+ * 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.
+ */
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.lucene.queries.function.ValueSource;
+
+/**
+ * Binds variable names in expressions to actual data.
+ * <p>
+ * These are typically DocValues fields/FieldCache, the document's 
+ * relevance score, or other ValueSources.
+ * 
+ * @lucene.experimental
+ */
+public abstract class Bindings implements Iterable<String> {
+
+  /** Sole constructor. (For invocation by subclass 
+   *  constructors, typically implicit.) */
+  protected Bindings() {}
+  
+  /**
+   * Returns a ValueSource bound to the variable name.
+   */
+  public abstract ValueSource getValueSource(String name);
+  
+  /** Returns an <code>Iterator</code> over the variable names in this binding */
+  @Override
+  public abstract Iterator<String> iterator();
+
+  /** 
+   * Traverses the graph of bindings, checking there are no cycles or missing references 
+   * @throws IllegalArgumentException if the bindings is inconsistent 
+   */
+  public final void validate() {
+    Set<String> marked = new HashSet<String>();
+    Set<String> chain = new HashSet<String>();
+    
+    for (String name : this) {
+      if (!marked.contains(name)) {
+        chain.add(name);
+        validate(name, marked, chain);
+        chain.remove(name);
+      }
+    }
+  }
+
+  private void validate(String name, Set<String> marked, Set<String> chain) {        
+    ValueSource vs = getValueSource(name);
+    if (vs == null) {
+      throw new IllegalArgumentException("Invalid reference '" + name + "'");
+    }
+    
+    if (vs instanceof ExpressionValueSource) {
+      Expression expr = ((ExpressionValueSource)vs).expression;
+      for (String external : expr.externals) {
+        if (chain.contains(external)) {
+          throw new IllegalArgumentException("Recursion Error: Cycle detected originating in (" + external + ")");
+        }
+        if (!marked.contains(external)) {
+          chain.add(external);
+          validate(external, marked, chain);
+          chain.remove(external);
+        }
+      }
+    }
+    
+    marked.add(name);
+  }
+}

Added: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/Expression.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/Expression.java?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/Expression.java (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/Expression.java Fri Sep 13 03:11:17 2013
@@ -0,0 +1,84 @@
+package org.apache.lucene.expressions;
+/*
+ * 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.
+ */
+
+import org.apache.lucene.queries.function.FunctionValues;
+import org.apache.lucene.queries.function.ValueSource;
+import org.apache.lucene.search.SortField;
+
+/**
+ * Base class that computes the value of an expression for a document.
+ * <p>
+ * Example usage:
+ * <pre class="prettyprint">
+ *   // compile an expression:
+ *   Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)");
+ *   
+ *   // SimpleBindings just maps variables to SortField instances
+ *   SimpleBindings bindings = new SimpleBindings();    
+ *   bindings.add(new SortField("_score", SortField.Type.SCORE));
+ *   bindings.add(new SortField("popularity", SortField.Type.INT));
+ *   
+ *   // create a sort field and sort by it (reverse order)
+ *   Sort sort = new Sort(expr.getSortField(bindings, true));
+ *   Query query = new TermQuery(new Term("body", "contents"));
+ *   searcher.search(query, null, 10, sort);
+ * </pre>
+ * @lucene.experimental
+ */
+public abstract class Expression {
+
+  /** The original {@link String} expression, before it was compiled */
+  public final String expression;
+
+  /** The names of external references found in the expression */
+  public final String[] externals;
+
+  /**
+   * Creates a new {@code CompiledExpression}.
+   *
+   * @param expression The expression that was compiled
+   * @param externals Names of external references found in the expression
+   */
+  public Expression(String expression, String[] externals) {
+    this.expression = expression;
+    this.externals = externals;
+  }
+
+  /**
+   * Evaluates the expression for the given document.
+   *
+   * @param document <code>docId</code> of the document to compute a value for
+   * @param functionValues {@link FunctionValues} for each element of {@link #externals}.
+   * @return The computed value of the expression for the given document.
+   */
+  public abstract double evaluate(int document, FunctionValues[] functionValues);
+
+  /**
+   * Get a value source which can compute the value of this expression in the context of the given bindings.
+   * @param bindings Bindings to use for external values in this expression
+   * @return A value source which will evaluate this expression when used
+   */
+  public ValueSource getValueSource(Bindings bindings) {
+    return new ExpressionValueSource(bindings, this);
+  }
+  
+  /** Get a sort field which can be used to rank documents by this expression. */
+  public SortField getSortField(Bindings bindings, boolean reverse) {
+    return getValueSource(bindings).getSortField(reverse);
+  }
+}

Added: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionComparator.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionComparator.java?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionComparator.java (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionComparator.java Fri Sep 13 03:11:17 2013
@@ -0,0 +1,95 @@
+package org.apache.lucene.expressions;
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.lucene.index.AtomicReaderContext;
+import org.apache.lucene.queries.function.FunctionValues;
+import org.apache.lucene.queries.function.ValueSource;
+import org.apache.lucene.search.FieldComparator;
+import org.apache.lucene.search.Scorer;
+
+/** A custom comparator for sorting documents by an expression */
+class ExpressionComparator extends FieldComparator<Double> {
+  private final double[] values;
+  private double bottom;
+  
+  private ValueSource source;
+  private FunctionValues scores;
+  private AtomicReaderContext readerContext;
+  
+  public ExpressionComparator(ValueSource source, int numHits) {
+    values = new double[numHits];
+    this.source = source;
+  }
+  
+  // TODO: change FieldComparator.setScorer to throw IOException and remove this try-catch
+  @Override
+  public void setScorer(Scorer scorer) {
+    super.setScorer(scorer);
+    // TODO: might be cleaner to lazy-init 'source' and set scorer after?
+    assert readerContext != null;
+    try {
+      Map<String,Object> context = new HashMap<String,Object>();
+      assert scorer != null;
+      context.put("scorer", new ScoreFunctionValues(scorer));
+      context.put("valuesCache", new HashMap<String, FunctionValues>());
+      scores = source.getValues(context, readerContext);
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+  
+  @Override
+  public int compare(int slot1, int slot2) {
+    return Double.compare(values[slot1], values[slot2]);
+  }
+  
+  @Override
+  public void setBottom(int slot) {
+    bottom = values[slot];
+  }
+  
+  @Override
+  public int compareBottom(int doc) throws IOException {
+    return Double.compare(bottom, scores.doubleVal(doc));
+  }
+  
+  @Override
+  public void copy(int slot, int doc) throws IOException {
+    values[slot] = scores.doubleVal(doc);
+  }
+  
+  @Override
+  public FieldComparator<Double> setNextReader(AtomicReaderContext context) throws IOException {
+    this.readerContext = context;
+    return this;
+  }
+  
+  @Override
+  public Double value(int slot) {
+    return Double.valueOf(values[slot]);
+  }
+  
+  @Override
+  public int compareDocToValue(int doc, Double valueObj) throws IOException {
+    return Double.compare(scores.doubleVal(doc), valueObj.doubleValue());
+  }
+}

Added: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java Fri Sep 13 03:11:17 2013
@@ -0,0 +1,64 @@
+package org.apache.lucene.expressions;
+/*
+ * 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.
+ */
+
+import org.apache.lucene.queries.function.FunctionValues;
+
+/** A {@link FunctionValues} which evaluates an expression */
+class ExpressionFunctionValues extends FunctionValues {
+  final Expression expression;
+  final FunctionValues[] functionValues;
+  
+  int currentDocument = -1;
+  double currentValue;
+  
+  public ExpressionFunctionValues(Expression expression, FunctionValues[] functionValues) {
+    if (expression == null) {
+      throw new NullPointerException();
+    }
+    if (functionValues == null) {
+      throw new NullPointerException();
+    }
+    this.expression = expression;
+    this.functionValues = functionValues;
+  }
+  
+  @Override
+  public double doubleVal(int document) {
+    if (currentDocument != document) {
+      currentDocument = document;
+      currentValue = expression.evaluate(document, functionValues);
+    }
+    
+    return currentValue;
+  }
+  
+  @Override
+  public Object objectVal(int doc) {
+    return doubleVal(doc);
+  }
+  
+  @Override
+  public String toString(int document) {
+    if (currentDocument != document) {
+      currentDocument = document;
+      currentValue = expression.evaluate(document, functionValues);
+    }
+    
+    return "ExpressionFunctionValues(" + document + ": " + currentValue + ")";
+  }
+}

Added: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionSortField.java Fri Sep 13 03:11:17 2013
@@ -0,0 +1,38 @@
+package org.apache.lucene.expressions;
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+
+import org.apache.lucene.queries.function.ValueSource;
+import org.apache.lucene.search.FieldComparator;
+import org.apache.lucene.search.SortField;
+
+/** A {@link SortField} which sorts documents by the evaluated value of an expression for each document */
+class ExpressionSortField extends SortField {
+  private final ValueSource source;
+
+  ExpressionSortField(String name, ValueSource source, boolean reverse) {
+    super(name, Type.CUSTOM, reverse);
+    this.source = source;
+  }
+  
+  @Override
+  public FieldComparator<?> getComparator(final int numHits, final int sortPos) throws IOException {
+    return new ExpressionComparator(source, numHits);
+  }
+}
\ No newline at end of file

Added: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java Fri Sep 13 03:11:17 2013
@@ -0,0 +1,88 @@
+package org.apache.lucene.expressions;
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.lucene.index.AtomicReaderContext;
+import org.apache.lucene.queries.function.FunctionValues;
+import org.apache.lucene.queries.function.ValueSource;
+import org.apache.lucene.search.SortField;
+
+/**
+ * A {@link ValueSource} which evaluates a {@link Expression} given the context of an {@link Bindings}.
+ */
+@SuppressWarnings({"rawtypes", "unchecked"})
+final class ExpressionValueSource extends ValueSource {
+  private final Bindings bindings;
+  final Expression expression;
+
+  public ExpressionValueSource(Bindings bindings, Expression expression) {
+    if (bindings == null) throw new NullPointerException();
+    if (expression == null) throw new NullPointerException();
+    this.bindings = bindings;
+    this.expression = expression;
+  }
+
+  /** <code>context</code> must contain a key <code>"valuesCache"</code> which is a <code>Map&lt;String,FunctionValues&gt;</code>. */
+  @Override
+  public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
+    ValueSource source;
+    Map<String, FunctionValues> valuesCache = (Map<String, FunctionValues>)context.get("valuesCache");
+    if (valuesCache == null) {
+      throw new NullPointerException();
+    }
+    FunctionValues[] externalValues = new FunctionValues[expression.externals.length];
+
+    for (int i = 0; i < expression.externals.length; ++i) {
+      String externalName = expression.externals[i];
+      FunctionValues values = valuesCache.get(externalName);
+      if (values == null) {
+        source = bindings.getValueSource(externalName);
+        values = source.getValues(context, readerContext);
+        if (values == null) {
+          throw new RuntimeException("Internal error. External (" + externalName + ") does not exist.");
+        }
+        valuesCache.put(externalName, values);
+      }
+      externalValues[i] = values;
+    }
+
+    return new ExpressionFunctionValues(expression, externalValues);
+  }
+
+  @Override
+  public SortField getSortField(boolean reverse) {
+    return new ExpressionSortField(expression.expression, this, reverse);
+  }
+
+  @Override
+  public String description() {
+    return "ExpressionValueSource(" + expression.expression + ")";
+  }
+
+  @Override
+  public int hashCode() {
+    return System.identityHashCode(this);
+  }
+  
+  @Override
+  public boolean equals(Object obj) {
+    return obj == this;
+  }
+}

Added: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ScoreFunctionValues.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ScoreFunctionValues.java?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ScoreFunctionValues.java (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ScoreFunctionValues.java Fri Sep 13 03:11:17 2013
@@ -0,0 +1,48 @@
+package org.apache.lucene.expressions;
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+
+import org.apache.lucene.queries.function.FunctionValues;
+import org.apache.lucene.search.Scorer;
+
+/**
+ * A utility class to allow expressions to access the score as a {@link FunctionValues}.
+ */
+class ScoreFunctionValues extends FunctionValues {
+  final Scorer scorer;
+
+  ScoreFunctionValues(Scorer scorer) {
+    this.scorer = scorer;
+  }
+  
+  @Override
+  public double doubleVal(int document) {
+    try {
+      assert document == scorer.docID();
+      return scorer.score();
+    } catch (IOException exception) {
+      throw new RuntimeException(exception);
+    }
+  }
+  
+  @Override
+  public String toString(int document) {
+    return "ComputedScorerValues(" + document + ": " + doubleVal(document) + ")";
+  }
+}

Added: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ScoreValueSource.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ScoreValueSource.java?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ScoreValueSource.java (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/ScoreValueSource.java Fri Sep 13 03:11:17 2013
@@ -0,0 +1,60 @@
+package org.apache.lucene.expressions;
+
+/*
+ * 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.
+ */
+
+import org.apache.lucene.index.AtomicReaderContext;
+import org.apache.lucene.queries.function.FunctionValues;
+import org.apache.lucene.queries.function.ValueSource;
+
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * A {@link ValueSource} which uses the {@link ScoreFunctionValues} passed through
+ * the context map by {@link ExpressionComparator}.
+ */
+@SuppressWarnings({"rawtypes"})
+class ScoreValueSource extends ValueSource {
+
+  /**
+   * <code>context</code> must contain a key "scorer" which is a {@link FunctionValues}.
+   */
+  @Override
+  public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
+    FunctionValues v = (FunctionValues) context.get("scorer");
+    if (v == null) {
+      throw new NullPointerException();
+    }
+    return v;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    return o == this;
+  }
+
+  @Override
+  public int hashCode() {
+    return System.identityHashCode(this);
+  }
+
+  @Override
+  public String description() {
+    return "ValueSource to expose scorer passed by ExpressionComparator";
+  }
+}

Added: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/SimpleBindings.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/SimpleBindings.java?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/SimpleBindings.java (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/SimpleBindings.java Fri Sep 13 03:11:17 2013
@@ -0,0 +1,94 @@
+package org.apache.lucene.expressions;
+
+/*
+ * 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.
+ */
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.lucene.queries.function.ValueSource;
+import org.apache.lucene.queries.function.valuesource.DoubleFieldSource;
+import org.apache.lucene.queries.function.valuesource.FloatFieldSource;
+import org.apache.lucene.queries.function.valuesource.IntFieldSource;
+import org.apache.lucene.queries.function.valuesource.LongFieldSource;
+import org.apache.lucene.search.FieldCache.DoubleParser;
+import org.apache.lucene.search.FieldCache.FloatParser;
+import org.apache.lucene.search.FieldCache.IntParser;
+import org.apache.lucene.search.FieldCache.LongParser;
+import org.apache.lucene.search.SortField;
+
+/**
+ * Simple class that binds expression variable names to {@link SortField}s.
+ *
+ * @lucene.experimental
+ */
+public final class SimpleBindings extends Bindings {
+  final Map<String,Object> map = new HashMap<String,Object>();
+  
+  /** Creates a new empty Bindings */
+  public SimpleBindings() {}
+  
+  /** 
+   * Adds a SortField to the bindings.
+   * <p>
+   * This can be used to reference a DocValuesField, a field from
+   * FieldCache, the document's score, etc. 
+   */
+  public void add(SortField sortField) {
+    map.put(sortField.getField(), sortField);
+  }
+  
+  /** 
+   * Adds an Expression to the bindings.
+   * <p>
+   * This can be used to reference expressions from other expressions. 
+   */
+  public void add(String name, Expression expression) {
+    map.put(name, expression);
+  }
+  
+  @Override
+  public ValueSource getValueSource(String name) {
+    Object o = map.get(name);
+    if (o == null) {
+      throw new IllegalArgumentException("Invalid reference '" + name + "'");
+    } else if (o instanceof Expression) {
+      return ((Expression)o).getValueSource(this);
+    }
+    SortField field = (SortField) o;
+    switch(field.getType()) {
+      case INT:
+        return new IntFieldSource(field.getField(), (IntParser) field.getParser());
+      case LONG:
+        return new LongFieldSource(field.getField(), (LongParser) field.getParser());
+      case FLOAT:
+        return new FloatFieldSource(field.getField(), (FloatParser) field.getParser());
+      case DOUBLE:
+        return new DoubleFieldSource(field.getField(), (DoubleParser) field.getParser());
+      case SCORE:
+        return new ScoreValueSource();
+      default:
+        throw new UnsupportedOperationException(); 
+    }
+  }
+
+  @Override
+  public Iterator<String> iterator() {
+    return map.keySet().iterator();
+  }
+}

Added: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/Javascript.g
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/Javascript.g?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/Javascript.g (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/Javascript.g Fri Sep 13 03:11:17 2013
@@ -0,0 +1,381 @@
+/*
+    Javascript.g
+    An expression syntax based on ECMAScript/Javascript.
+ 
+    This file was adapted from a general ECMAScript language definition at http://research.xebic.com/es3.
+    The major changes are the following:
+        * Stripped grammar of all parts not relevant for expression syntax.
+        * Stripped grammar of unicode character support.
+        * Added override function for customized error handling.
+        * Renaming of many grammar rules.
+        * Removal of annotations no longer relevant for stripped pieces.
+     
+    The Original Copyright Notice is the following: 
+    
+        Copyrights 2008-2009 Xebic Reasearch BV. All rights reserved..
+        Original work by Patrick Hulsmeijer.
+ 
+        This ANTLR 3 LL(*) grammar is based on Ecma-262 3rd edition (JavaScript 1.5, JScript 5.5). 
+        The annotations refer to the "A Grammar Summary" section (e.g. A.1 Lexical Grammar)
+        and the numbers in parenthesis to the paragraph numbers (e.g. (7.8) ).
+        This document is best viewed with ANTLRWorks (www.antlr.org).
+ 
+        Software License Agreement (BSD License)
+ 
+        Copyright (c) 2008-2010, Xebic Research B.V.
+        All rights reserved.
+ 
+        Redistribution and use of this software in source and binary forms, with or without modification, are
+        permitted provided that the following conditions are met:
+ 
+            * Redistributions of source code must retain the above
+              copyright notice, this list of conditions and the
+              following disclaimer.
+ 
+            * Redistributions in binary form must reproduce the above
+              copyright notice, this list of conditions and the
+              following disclaimer in the documentation and/or other
+              materials provided with the distribution.
+ 
+            * Neither the name of Xebic Research B.V. nor the names of its
+              contributors may be used to endorse or promote products
+              derived from this software without specific prior
+              written permission of Xebic Research B.V.
+ 
+        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+        WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+        PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+        ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+        LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+        TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+        ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// ***********************************************************************
+// * ANTLRv3 grammar for A9 rank expression language.
+// ***********************************************************************
+ 
+grammar Javascript;
+
+options {
+    language = Java;
+    output = AST;
+    ASTLabelType=CommonTree;
+}
+
+tokens
+{
+    AT_LPAREN          = '('  ;
+    AT_RPAREN          = ')'  ;
+    AT_DOT             = '.'  ;
+    AT_COMMA           = ','  ;
+    AT_COLON           = ':'  ;
+
+    AT_COMP_LT         = '<'  ;
+    AT_COMP_LTE        = '<=' ;
+    AT_COMP_EQ         = '==' ;
+    AT_COMP_NEQ        = '!=' ;
+    AT_COMP_GTE        = '>=' ;
+    AT_COMP_GT         = '>'  ;
+
+    AT_BOOL_NOT        = '!'  ;
+    AT_BOOL_AND        = '&&' ;
+    AT_BOOL_OR         = '||' ;
+    AT_COND_QUE        = '?'  ;
+
+    AT_NEGATE                 ;
+    AT_ADD             = '+'  ;
+    AT_SUBTRACT        = '-'  ;
+    AT_MULTIPLY        = '*'  ;
+    AT_DIVIDE          = '/'  ;
+    AT_MODULO          = '%'  ;
+
+    AT_BIT_SHL         = '<<' ;
+    AT_BIT_SHR         = '>>' ;
+    AT_BIT_SHU         = '>>>';
+    AT_BIT_AND         = '&'  ;
+    AT_BIT_OR          = '|'  ;
+    AT_BIT_XOR         = '^'  ;
+    AT_BIT_NOT         = '~'  ;
+    
+    AT_CALL                   ;
+}
+
+// ***********************************************************************
+// * Java Package
+// ***********************************************************************
+
+@lexer::header {
+package org.apache.lucene.expressions.js;
+
+import java.text.ParseException;
+}
+
+@parser::header {
+package org.apache.lucene.expressions.js;
+    
+import java.text.ParseException;
+}
+ 
+// ***********************************************************************
+// * Error Handling
+// ***********************************************************************
+
+@lexer::members {
+
+@Override
+public void displayRecognitionError(String[] tokenNames, RecognitionException re) {  
+    String message = " unexpected character '" + (char)re.c 
+                   + "' at position (" + re.charPositionInLine + ").";
+    ParseException parseException = new ParseException(message, re.charPositionInLine);
+    parseException.initCause(re);
+    throw new RuntimeException(parseException);
+}
+
+}
+
+@parser::members {
+
+@Override
+public void displayRecognitionError(String[] tokenNames, RecognitionException re) {
+    String message;
+
+    if (re.token == null) {
+        message = " unknown error (missing token).";
+    }
+    else if (re instanceof UnwantedTokenException) {
+        message = " extraneous " + getReadableTokenString(re.token)
+                +  " at position (" + re.charPositionInLine + ").";
+    }
+    else if (re instanceof MissingTokenException) {
+        message = " missing " + getReadableTokenString(re.token)
+                +  " at position (" + re.charPositionInLine + ").";
+    }
+    else if (re instanceof NoViableAltException) {
+        switch (re.token.getType()) {
+        case EOF:
+            message = " unexpected end of expression.";
+            break;
+        default:
+            message = " invalid sequence of tokens near " + getReadableTokenString(re.token)
+                    +  " at position (" + re.charPositionInLine + ").";
+            break;
+        }
+    }
+    else {
+        message = " unexpected token " + getReadableTokenString(re.token) 
+                +  " at position (" + re.charPositionInLine + ").";
+    }
+    ParseException parseException = new ParseException(message, re.charPositionInLine);
+    parseException.initCause(re);
+    throw new RuntimeException(parseException);
+}
+
+public static String getReadableTokenString(Token token) {
+    if (token == null) {
+        return "unknown token";
+    }
+
+    switch (token.getType()) {
+    case AT_LPAREN:
+        return "open parenthesis '('";
+    case AT_RPAREN:
+        return "close parenthesis ')'";
+    case AT_COMP_LT:
+        return "less than '<'";
+    case AT_COMP_LTE:
+        return "less than or equal '<='";
+    case AT_COMP_GT:
+        return "greater than '>'";
+    case AT_COMP_GTE:
+        return "greater than or equal '>='";
+    case AT_COMP_EQ:
+        return "equal '=='";
+    case AT_NEGATE:
+        return "negate '!='";
+    case AT_BOOL_NOT:
+        return "boolean not '!'";
+    case AT_BOOL_AND:
+        return "boolean and '&&'";
+    case AT_BOOL_OR:
+        return "boolean or '||'";
+    case AT_COND_QUE:
+        return "conditional '?'";
+    case AT_ADD:
+        return "addition '+'";
+    case AT_SUBTRACT:
+        return "subtraction '-'";
+    case AT_MULTIPLY:
+        return "multiplication '*'";
+    case AT_DIVIDE:
+        return "division '/'";
+    case AT_MODULO:
+        return "modulo '\%'";
+    case AT_BIT_SHL:
+        return "bit shift left '<<'";
+    case AT_BIT_SHR:
+        return "bit shift right '>>'";
+    case AT_BIT_SHU:
+        return "unsigned bit shift right '>>>'";
+    case AT_BIT_AND:
+        return "bitwise and '&'";
+    case AT_BIT_OR:
+        return "bitwise or '|'";
+    case AT_BIT_XOR:
+        return "bitwise xor '^'";
+    case AT_BIT_NOT:
+        return "bitwise not '~'";
+    case ID:
+        return "identifier '" + token.getText() + "'";
+    case DECIMAL:
+        return "decimal '" + token.getText() + "'";
+    case OCTAL:
+        return "octal '" + token.getText() + "'";
+    case HEX:
+        return "hex '" + token.getText() + "'";
+    case EOF:
+        return "end of expression";
+    default:
+        return "'" + token.getText() + "'";
+    }
+}
+
+}
+
+// ***********************************************************************
+// * Parser Rules
+// ***********************************************************************
+
+expression
+    : conditional EOF!
+    ;
+
+conditional
+    : logical_or (AT_COND_QUE^ conditional AT_COLON! conditional)?
+    ;
+
+logical_or
+    : logical_and (AT_BOOL_OR^ logical_and)*
+    ;
+
+logical_and
+    : bitwise_or (AT_BOOL_AND^ bitwise_or)*
+    ;
+
+bitwise_or
+    : bitwise_xor (AT_BIT_OR^ bitwise_xor)*
+    ;
+
+bitwise_xor
+    : bitwise_and (AT_BIT_XOR^ bitwise_and)*
+    ;
+
+bitwise_and
+    :  equality (AT_BIT_AND^ equality)*
+    ;
+
+equality
+    : relational ((AT_COMP_EQ | AT_COMP_NEQ)^ relational)*
+    ;
+
+relational
+    : shift ((AT_COMP_LT | AT_COMP_GT | AT_COMP_LTE | AT_COMP_GTE)^ shift)*
+    ;
+
+shift
+    : additive ((AT_BIT_SHL | AT_BIT_SHR | AT_BIT_SHU)^ additive)*
+    ;
+
+additive
+    : multiplicative ((AT_ADD | AT_SUBTRACT)^ multiplicative)*
+    ;
+
+multiplicative
+    : unary ((AT_MULTIPLY | AT_DIVIDE | AT_MODULO)^ unary)*
+    ;
+
+unary
+    : postfix
+    | AT_ADD! unary
+    | unary_operator^ unary
+    ;
+
+unary_operator
+    : AT_SUBTRACT -> AT_NEGATE
+    | AT_BIT_NOT
+    | AT_BOOL_NOT
+    ;
+
+postfix
+    : primary
+    | ID arguments -> ^(AT_CALL ID arguments?)
+    ;
+
+primary
+    : ID
+    | numeric
+    | AT_LPAREN! conditional AT_RPAREN!
+    ;
+
+arguments
+    : AT_LPAREN! (conditional (AT_COMMA! conditional)*)? AT_RPAREN!
+    ;
+
+numeric
+    : HEX | OCTAL | DECIMAL
+    ;
+
+// ***********************************************************************
+// * Lexer Rules
+// ***********************************************************************
+
+ID
+    : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
+    ;
+    
+WS
+    : (' '|'\t'|'\n'|'\r')+ {skip();}
+    ;
+
+DECIMAL
+    : DECIMALINTEGER AT_DOT DECIMALDIGIT* EXPONENT?
+    | AT_DOT DECIMALDIGIT+ EXPONENT?
+    | DECIMALINTEGER EXPONENT?
+    ;
+
+OCTAL
+    : '0' OCTALDIGIT+
+    ;
+
+HEX
+    : ('0x'|'0X') HEXDIGIT+
+    ;
+
+fragment 
+DECIMALINTEGER
+    : '0'
+    | '1'..'9' DECIMALDIGIT*
+    ;
+
+fragment 
+EXPONENT
+    : ('e'|'E') ('+'|'-')? DECIMALDIGIT+
+    ;
+
+fragment
+DECIMALDIGIT
+    : '0'..'9'
+    ;
+
+fragment 
+HEXDIGIT
+    : DECIMALDIGIT 
+    | 'a'..'f' 
+    | 'A'..'F'
+    ;
+
+fragment 
+OCTALDIGIT
+    : '0'..'7'
+    ;

Added: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/Javascript.tokens
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/Javascript.tokens?rev=1522767&view=auto
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/Javascript.tokens (added)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/Javascript.tokens Fri Sep 13 03:11:17 2013
@@ -0,0 +1,66 @@
+AT_ADD=4
+AT_BIT_AND=5
+AT_BIT_NOT=6
+AT_BIT_OR=7
+AT_BIT_SHL=8
+AT_BIT_SHR=9
+AT_BIT_SHU=10
+AT_BIT_XOR=11
+AT_BOOL_AND=12
+AT_BOOL_NOT=13
+AT_BOOL_OR=14
+AT_CALL=15
+AT_COLON=16
+AT_COMMA=17
+AT_COMP_EQ=18
+AT_COMP_GT=19
+AT_COMP_GTE=20
+AT_COMP_LT=21
+AT_COMP_LTE=22
+AT_COMP_NEQ=23
+AT_COND_QUE=24
+AT_DIVIDE=25
+AT_DOT=26
+AT_LPAREN=27
+AT_MODULO=28
+AT_MULTIPLY=29
+AT_NEGATE=30
+AT_RPAREN=31
+AT_SUBTRACT=32
+DECIMAL=33
+DECIMALDIGIT=34
+DECIMALINTEGER=35
+EXPONENT=36
+HEX=37
+HEXDIGIT=38
+ID=39
+OCTAL=40
+OCTALDIGIT=41
+WS=42
+'!'=13
+'!='=23
+'%'=28
+'&&'=12
+'&'=5
+'('=27
+')'=31
+'*'=29
+'+'=4
+','=17
+'-'=32
+'.'=26
+'/'=25
+':'=16
+'<'=21
+'<<'=8
+'<='=22
+'=='=18
+'>'=19
+'>='=20
+'>>'=9
+'>>>'=10
+'?'=24
+'^'=11
+'|'=7
+'||'=14
+'~'=6



Mime
View raw message