Package

com.eharmony.aloha.semantics

compiled

Permalink

package compiled

Visibility
  1. Public
  2. All

Type Members

  1. case class CompiledSemantics[A](compiler: ContainerReadable[Try], plugin: CompiledSemanticsPlugin[A], imports: Seq[String], provideSemanticsUdfException: Boolean = true)(implicit ec: ExecutionContext) extends CompiledSemanticsLike[A] with MorphableSemantics[CompiledSemantics, A] with ErrorEnrichingSemantics[A] with Logging with Product with Serializable

    Permalink

    A semantics that can interpret complicated expressions by compiling the expressions.

    A semantics that can interpret complicated expressions by compiling the expressions. This semantics constructs actual working scala code (see the format below).

    Notice from the code that is constructed that it calls a function in com.eharmony.aloha.semantics.func.GenFunc. The function that is called is based on the input arity. Aside from the f0 function, fi has i arguments in the first argument list and two arguments in the second argument list. The first argument list represents the definition of the accessor functions. These are the functions created to extract data from the domain object. com.eharmony.aloha.semantics.compiled.CompiledSemanticsPlugin objects are responsible for constructing the function bodies. For instance, (_:Map[String, Long]).get("user.match.TaxBrackets") in the first example. The CompiledSemantics class is responsible for wrapping this code appropriately, constructing the aggregation function and finally putting everything together and compiling it.

    Examples

    E.g. 1: Two variables are both optional (not necessarily in the input type passed to the model): - user.match.TaxBrackets - cand.taxBracket

    val spec1 = "Seq(${user.match.TaxBrackets}) contains ${cand.taxBracket}"
    val f1 = s.createFunction[Boolean](spec1, Some(false)).right.get
    
    // Equivalent to:
    //    val f1a =
    //      com.eharmony.aloha.semantics.compiled.GenFunc.f2(           // f2 b/c 2 variables.
    //        com.eharmony.aloha.semantics.compiled.GeneratedAccessor(  // Optional Accessor:
    //          "user.match.TaxBrackets",
    //          (_:Map[String, Long]).get("user.match.TaxBrackets"),             // <-- get produces Option[Long]
    //          Some("(_:Map[String, Long]).get(\"user.match.TaxBrackets\")")),
    //        com.eharmony.aloha.semantics.compiled.GeneratedAccessor(  // Optional Accessor:
    //          "cand.taxBracket",
    //          (_:Map[String, Long]).get("cand.taxBracket"),                    // <-- get produces Option[Long]
    //          Some("(_:Map[String, Long]).get(\"cand.taxBracket\")"))
    //      )(
    //        "Seq(${user.match.TaxBrackets}) contains ${cand.taxBracket}",
    //        (o0, o1) => for {
    //          _user__match__TaxBrackets <- o0                                  // Optional fields in for comprehension
    //          _cand__taxBracket <- o1
    //        } yield {
    //          Seq(_user__match__TaxBrackets) contains _cand__taxBracket        // Transformed function spec
    //        }
    //      )
    //
    //    val f1 = com.eharmony.aloha.semantics.compiled.OptionalFunc(f1a, false)

    E.g. 2: Two variables (one required, one optional): - user.inboundComm (required) - user.pageViews (optional)

    val spec2 = "${user.inboundComm} / ${user.pageViews}.toDouble"
    val f2 = s.createFunction[Double](spec2, Some(Double.NaN)).right.get
    
    // Equivalent to:
    //    val f2a =
    //      com.eharmony.aloha.semantics.compiled.GenFunc.f2(           // f2 b/c 2 variables.
    //        com.eharmony.aloha.semantics.compiled.GeneratedAccessor(  // Optional Accessor:
    //          "user.pageViews",
    //          (_:Map[String, Long]).get("user.pageViews"),                     // <-- get produces Option[Long]
    //          Some("(_:Map[String, Long]).get(\"user.pageViews\")")),
    //        com.eharmony.aloha.semantics.compiled.GeneratedAccessor(  // Required Accessor:
    //          "user.inboundComm",
    //          (_:Map[String, Long]).apply("user.inboundComm"),                 // <-- apply produces Long
    //          Some("(_:Map[String, Long]).apply(\"user.inboundComm\")"))
    //      )(
    //        "${user.inboundComm} / ${user.pageViews}.toDouble",
    //        (o0, _user__inboundComm) => for {
    //          _user__pageViews <- o0                                           // Optional field in for comprehension
    //        } yield {
    //          _user__inboundComm / _user__pageViews.toDouble                   // Transformed function spec
    //        }
    //      )
    //
    //    val f2 = com.eharmony.aloha.semantics.compiled.OptionalFunc(f2a, Double.NaN)

    E.g. 3: Three required variables (one, two, three):

    val spec3 = "List(${one}, ${two}, ${three}).sum.toInt"
    val f3 = s.createFunction[Int](spec3).right.get
    
    // Equivalent to:
    //    val f3 =
    //      com.eharmony.aloha.semantics.compiled.GenFunc.f3(           // f3 b/c 3 variables.
    //        com.eharmony.aloha.semantics.compiled.GeneratedAccessor(
    //          "one",
    //          (_:Map[String, Long]).apply("one"),
    //          Some("(_:Map[String, Long]).apply(\"one\")")),
    //        com.eharmony.aloha.semantics.compiled.GeneratedAccessor(
    //          "two",
    //          (_:Map[String, Long]).apply("two"),
    //          Some("(_:Map[String, Long]).apply(\"two\")")),
    //        com.eharmony.aloha.semantics.compiled.GeneratedAccessor(
    //          "three",
    //          (_:Map[String, Long]).apply("three"),
    //          Some("(_:Map[String, Long]).apply(\"three\")"))
    //      )(
    //        "List(${one}, ${two}, ${three}).sum.toInt",
    //        (_one, _two, _three) => {                  // Notice simplified expression when all params are required.
    //          List(_one, _two, _three).sum.toInt       // Variable names are in the param list so no need for temp
    //        }                                          // variables.
    //      )

    E.g. 4: Invariant function (not dependent on any input variables).

    val spec4 = "new util.Random(System.nanoTime).nextLong"
    val f = s.createFunction[Long](spec4).right.get         // f(null) is OK b/c not reliant on input value.
    
    // Equivalent to:
    //    val f4 =
    //      com.eharmony.aloha.semantics.compiled.GenFunc.f0( // f0 b/c input arity is 0
    //        "new util.Random(System.nanoTime).nextLong",
    //        (_:Any) => new util.Random(System.nanoTime).nextLong     // Notice input is Any
    //      )                                                          // OK b/c of Function1 1st arg contravariance.
    A

    the input type to the functions that are to be generated. Said another way, this is domain type of the models that will be generated by the ModelFactory to which this com.eharmony.aloha.semantics.Semantics will be passed.

    compiler

    a compiler capable of compiling real scala code and generating real instances that work at full speed.

    plugin

    a plugin that can make sense of the variable specifications and generate code to extract data from them.

    imports

    a list of imports.

    provideSemanticsUdfException

    whether we should provide com.eharmony.aloha.semantics.SemanticsUdfException instead of raw exceptions in the case that a feature produces an exception. The perceived benefit of this seems to drastically outweigh the performance hit, so this defaults to true (as this is suggested).

    ec

    an execution context in which to run the Futures that are used in the cache.

  2. sealed trait CompiledSemanticsLike[A] extends Semantics[A] with EitherHelpers with Logging

    Permalink
  3. trait CompiledSemanticsPlugin[A] extends AnyRef

    Permalink

    A semantics object is responsible for parsing a variable accessor specification inside a dollar sign-curly brace expression (that may be embedded in a larger expression).

    A semantics object is responsible for parsing a variable accessor specification inside a dollar sign-curly brace expression (that may be embedded in a larger expression). For instance, in the variable accessor specification: ${a.b[0].c}, the semantics is responsible for transforming the string a.b[0].c into Scala code for a function that pulls the data out of an object of type A. The output type of the function need not be stated explicitly because Scala's type inference mechanism will be used at the time the generated function is compiled.

    A

    the input type to a model

  4. case class OptionalAccessorCode(body: Seq[String]) extends VariableAccessorCode with Product with Serializable

    Permalink
  5. case class RequiredAccessorCode(body: Seq[String]) extends VariableAccessorCode with Product with Serializable

    Permalink
  6. sealed trait VariableAccessorCode extends AnyRef

    Permalink

Value Members

  1. object CompiledSemantics extends Serializable

    Permalink

    Companion class containing canonical class names for wrapper classes uses in compilation.

  2. package compiler

    Permalink
  3. package plugin

    Permalink

Ungrouped