Fork me on GitHub
  • API

    Show / Hide Table of Contents

    Class JavascriptCompiler

    An expression compiler for javascript expressions.

    Inheritance
    object
    JavascriptCompiler
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Lucene.Net.Expressions.JS
    Assembly: Lucene.Net.Expressions.dll
    Syntax
    public class JavascriptCompiler
    Remarks

    An expression compiler for javascript expressions.

    Example:
    Expression foo = JavascriptCompiler.Compile("((0.3*popularity)/10.0)+(0.7*score)");

    See the package documentation for the supported syntax and default functions.

    You can compile with an alternate set of functions via Compile(string, IDictionary<string, MethodInfo>). For example:

    // instantiate and add all the default functions
    IDictionary<string, MethodInfo> functions = new Dictionary<string, MethodInfo>(JavascriptCompiler.DEFAULT_FUNCTIONS);
    // add sqrt()
    functions["sqrt"] = (typeof(Math)).GetMethod("Sqrt", new Type[] { typeof(double) });
    // call compile with customized function map
    Expression foo = JavascriptCompiler.Compile("sqrt(score)+ln(popularity)", functions);
    Note

    This API is experimental and might change in incompatible ways in the next release.

    Fields

    DEFAULT_FUNCTIONS

    The default set of functions available to expressions.

    Declaration
    public static readonly IDictionary<string, MethodInfo> DEFAULT_FUNCTIONS
    Field Value
    Type Description
    IDictionary<string, MethodInfo>
    Remarks

    The default set of functions available to expressions.

    See the package documentation for a list.

    Methods

    Compile(string)

    Compiles the given expression.

    Declaration
    public static Expression Compile(string sourceText)
    Parameters
    Type Name Description
    string sourceText

    The expression to compile

    Returns
    Type Description
    Expression

    A new compiled expression

    Remarks

    An expression compiler for javascript expressions.

    Example:
    Expression foo = JavascriptCompiler.Compile("((0.3*popularity)/10.0)+(0.7*score)");

    See the package documentation for the supported syntax and default functions.

    You can compile with an alternate set of functions via Compile(string, IDictionary<string, MethodInfo>). For example:

    // instantiate and add all the default functions
    IDictionary<string, MethodInfo> functions = new Dictionary<string, MethodInfo>(JavascriptCompiler.DEFAULT_FUNCTIONS);
    // add sqrt()
    functions["sqrt"] = (typeof(Math)).GetMethod("Sqrt", new Type[] { typeof(double) });
    // call compile with customized function map
    Expression foo = JavascriptCompiler.Compile("sqrt(score)+ln(popularity)", functions);
    Note

    This API is experimental and might change in incompatible ways in the next release.

    Exceptions
    Type Condition
    ParseException

    on failure to compile

    Compile(string, IDictionary<string, MethodInfo>)

    Compiles the given expression with the supplied custom functions.

    Declaration
    public static Expression Compile(string sourceText, IDictionary<string, MethodInfo> functions)
    Parameters
    Type Name Description
    string sourceText

    The expression to compile

    IDictionary<string, MethodInfo> functions

    map of string names to functions

    Returns
    Type Description
    Expression

    A new compiled expression

    Remarks

    Compiles the given expression with the supplied custom functions.

    Functions must be public static, return double and can take from zero to 256 double parameters.
    Exceptions
    Type Condition
    ParseException

    on failure to compile

    Back to top Copyright © 2024 The Apache Software Foundation, Licensed under the Apache License, Version 2.0
    Apache Lucene.Net, Lucene.Net, Apache, the Apache feather logo, and the Apache Lucene.Net project logo are trademarks of The Apache Software Foundation.
    All other marks mentioned may be trademarks or registered trademarks of their respective owners.