Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change visibility of fields in EvaluableLazyParameter and SpelExpression to allow ast ops in Service #7125

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pl.touk.nussknacker.engine.api

import pl.touk.nussknacker.engine.api.LazyParameter.{Evaluate, MappedLazyParameter, ProductLazyParameter}
import pl.touk.nussknacker.engine.api.parameter.ParameterName
import pl.touk.nussknacker.engine.api.typed.typing.{Typed, TypingResult}
import pl.touk.nussknacker.engine.graph.expression.Expression.Language

import scala.reflect.runtime.universe.TypeTag

Expand Down Expand Up @@ -66,7 +68,15 @@ object LazyParameter {
): LazyParameter[Y] =
new MappedLazyParameter[T, Y](lazyParameter, fun, transformTypingResult)

trait CustomLazyParameter[+T <: AnyRef] extends LazyParameter[T]
trait CustomLazyParameter[T <: AnyRef] extends LazyParameter[T]

trait CustomLazyParamterWithCustomizableEvaluationLogic[T <: AnyRef] extends CustomLazyParameter[T] {

def withCustomEvaluationLogic(
customEvaluate: (BaseCompiledParameter, BaseExpressionEvaluator, NodeId, JobData, Context) => T
): CustomLazyParamterWithCustomizableEvaluationLogic[T]

}

final class ProductLazyParameter[T <: AnyRef, Y <: AnyRef](
val arg1: LazyParameter[T],
Expand Down Expand Up @@ -122,6 +132,27 @@ object LazyParameter {

}

trait BaseCompiledParameter {
def name: ParameterName
def expression: CompiledExpression
def shouldBeWrappedWithScalaOption: Boolean
def shouldBeWrappedWithJavaOptional: Boolean
}

trait CompiledExpression {
def language: Language
def original: String
def evaluate[T](ctx: Context, globals: Map[String, Any]): T
}

trait BaseExpressionEvaluator {

def evaluate[R](expr: CompiledExpression, expressionId: String, nodeId: String, ctx: Context)(
implicit jobData: JobData
): ValueWithContext[R]

}

// This class is Flink-specific. It allows to evaluate value of lazy parameter in case when LazyParameter isn't
// a ready to evaluation function. In Flink case, LazyParameters are passed into Flink's operators so they
// need to be Serializable. Because of that they can't hold heavy objects like ExpressionCompiler or ExpressionEvaluator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pl.touk.nussknacker.engine.api.definition

import pl.touk.nussknacker.engine.api.{BaseCompiledParameter, BaseExpressionEvaluator, Context, JobData, NodeId}
import pl.touk.nussknacker.engine.api.context.transformation.{
NodeDependencyValue,
OutputVariableNameValue,
Expand Down Expand Up @@ -134,6 +135,7 @@ case class Parameter(
javaOptionalParameter: Boolean,
hintText: Option[String],
labelOpt: Option[String],
customEvaluate: Option[(BaseCompiledParameter, BaseExpressionEvaluator, NodeId, JobData, Context) => AnyRef] = None
) extends NodeDependency {

def copy(
Expand Down Expand Up @@ -180,6 +182,8 @@ case class Parameter(
javaOptionalParameter: Boolean = this.javaOptionalParameter,
hintText: Option[String] = this.hintText,
labelOpt: Option[String] = this.labelOpt,
customEvaluate: Option[(BaseCompiledParameter, BaseExpressionEvaluator, NodeId, JobData, Context) => AnyRef] =
this.customEvaluate
): Parameter = {
new Parameter(
name,
Expand All @@ -194,7 +198,8 @@ case class Parameter(
scalaOptionParameter,
javaOptionalParameter,
hintText,
labelOpt
labelOpt,
customEvaluate
)
}

Expand All @@ -211,7 +216,9 @@ case class Parameter(
scalaOptionParameter: Boolean,
javaOptionalParameter: Boolean,
hintText: Option[String],
labelOpt: Option[String]
labelOpt: Option[String],
customEvaluate: Option[(BaseCompiledParameter, BaseExpressionEvaluator, NodeId, JobData, Context) => AnyRef] =
None
): Parameter = {
new Parameter(
name,
Expand All @@ -226,7 +233,8 @@ case class Parameter(
scalaOptionParameter,
javaOptionalParameter,
hintText,
labelOpt
labelOpt,
customEvaluate
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import pl.touk.nussknacker.engine.compiledgraph.node._
import pl.touk.nussknacker.engine.compiledgraph.service._
import pl.touk.nussknacker.engine.compiledgraph.variable._
import pl.touk.nussknacker.engine.expression.ExpressionEvaluator
import pl.touk.nussknacker.engine.expression.parse.CompiledExpression
import pl.touk.nussknacker.engine.node.NodeComponentInfoExtractor
import pl.touk.nussknacker.engine.util.SynchronousExecutionContextAndIORuntime

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cats.data.Validated.{Invalid, Valid, invalid, invalidNel, valid}
import cats.data.{Ior, IorNel, NonEmptyList, Validated, ValidatedNel}
import cats.instances.list._
import pl.touk.nussknacker.engine.ModelData
import pl.touk.nussknacker.engine.api.{JobData, MetaData, NodeId}
import pl.touk.nussknacker.engine.api.{CompiledExpression, JobData, MetaData, NodeId}
import pl.touk.nussknacker.engine.api.context.ProcessCompilationError._
import pl.touk.nussknacker.engine.api.context.{PartSubGraphCompilationError, ProcessCompilationError, ValidationContext}
import pl.touk.nussknacker.engine.api.definition._
Expand All @@ -17,12 +17,7 @@ import pl.touk.nussknacker.engine.definition.clazz.ClassDefinitionSet
import pl.touk.nussknacker.engine.definition.component.parameter.validator.ValidationExpressionParameterValidator
import pl.touk.nussknacker.engine.definition.globalvariables.ExpressionConfigDefinition
import pl.touk.nussknacker.engine.expression.{ExpressionEvaluator, NullExpression}
import pl.touk.nussknacker.engine.expression.parse.{
CompiledExpression,
ExpressionParser,
TypedExpression,
TypedExpressionMap
}
import pl.touk.nussknacker.engine.expression.parse.{ExpressionParser, TypedExpression, TypedExpressionMap}
import pl.touk.nussknacker.engine.graph.evaluatedparam.{BranchParameters, Parameter => NodeParameter}
import pl.touk.nussknacker.engine.graph.expression.Expression
import pl.touk.nussknacker.engine.graph.expression.Expression.Language
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cats.data.Validated.{Invalid, Valid}
import cats.data.ValidatedNel
import cats.implicits.{catsSyntaxTuple2Semigroupal, toFoldableOps, toTraverseOps}
import cats.instances.list._
import pl.touk.nussknacker.engine.api.NodeId
import pl.touk.nussknacker.engine.api.{CompiledExpression, NodeId}
import pl.touk.nussknacker.engine.api.context._
import pl.touk.nussknacker.engine.api.expression.ExpressionTypingInfo
import pl.touk.nussknacker.engine.api.parameter.ParameterName
Expand All @@ -14,7 +14,7 @@ import pl.touk.nussknacker.engine.compile.nodecompilation.BaseComponentValidatio
import pl.touk.nussknacker.engine.compile.nodecompilation.BuiltInNodeCompiler._
import pl.touk.nussknacker.engine.compile.nodecompilation.NodeCompiler.NodeCompilationResult
import pl.touk.nussknacker.engine.compiledgraph
import pl.touk.nussknacker.engine.expression.parse.{CompiledExpression, TypedExpression}
import pl.touk.nussknacker.engine.expression.parse.TypedExpression
import pl.touk.nussknacker.engine.graph.expression.NodeExpressionId.DefaultExpressionIdParamName
import pl.touk.nussknacker.engine.graph.expression._
import pl.touk.nussknacker.engine.graph.node
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,62 @@
package pl.touk.nussknacker.engine.compile.nodecompilation

import pl.touk.nussknacker.engine.api.LazyParameter.{CustomLazyParameter, Evaluate}
import pl.touk.nussknacker.engine.api.LazyParameter.{
CustomLazyParameter,
CustomLazyParamterWithCustomizableEvaluationLogic,
Evaluate
}
import pl.touk.nussknacker.engine.api.typed.typing.TypingResult
import pl.touk.nussknacker.engine.api.{Context, JobData, MetaData, NodeId}
import pl.touk.nussknacker.engine.compiledgraph.{BaseCompiledParameter, CompiledParameter}
import pl.touk.nussknacker.engine.api.{BaseCompiledParameter, BaseExpressionEvaluator, Context, JobData, NodeId}
import pl.touk.nussknacker.engine.compiledgraph.CompiledParameter
import pl.touk.nussknacker.engine.expression.ExpressionEvaluator

class EvaluableLazyParameter[T <: AnyRef](
compiledParameter: BaseCompiledParameter,
expressionEvaluator: ExpressionEvaluator,
val compiledParameter: BaseCompiledParameter,
val expressionEvaluator: ExpressionEvaluator,
nodeId: NodeId,
jobData: JobData,
override val returnType: TypingResult
) extends CustomLazyParameter[T] {
val jobData: JobData,
override val returnType: TypingResult,
val customEvaluate: Option[(BaseCompiledParameter, ExpressionEvaluator, NodeId, JobData, Context) => T]
) extends CustomLazyParamterWithCustomizableEvaluationLogic[T] {

def this(
compiledParameter: CompiledParameter,
expressionEvaluator: ExpressionEvaluator,
nodeId: NodeId,
jobData: JobData
jobData: JobData,
customEvaluate: Option[(BaseCompiledParameter, ExpressionEvaluator, NodeId, JobData, Context) => T] = None
) =
this(compiledParameter, expressionEvaluator, nodeId, jobData, compiledParameter.typingInfo.typingResult)
this(
compiledParameter,
expressionEvaluator,
nodeId,
jobData,
compiledParameter.typingInfo.typingResult,
customEvaluate
)

override val evaluate: Evaluate[T] = { ctx: Context =>
expressionEvaluator
.evaluateParameter(compiledParameter, ctx)(nodeId, jobData)
.value
.asInstanceOf[T]
customEvaluate
.map(evaluate => evaluate(compiledParameter, expressionEvaluator, nodeId, jobData, ctx))
.getOrElse {
expressionEvaluator
.evaluateParameter(compiledParameter, ctx)(nodeId, jobData)
.value
.asInstanceOf[T]
}
}

def withCustomEvaluationLogic(
customEvaluate: (BaseCompiledParameter, BaseExpressionEvaluator, NodeId, JobData, Context) => T
): EvaluableLazyParameter[T] = {
new EvaluableLazyParameter(
compiledParameter,
expressionEvaluator,
nodeId,
jobData,
returnType,
Some(customEvaluate)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import pl.touk.nussknacker.engine.api._
import pl.touk.nussknacker.engine.api.parameter.ParameterName
import pl.touk.nussknacker.engine.api.typed.typing._
import pl.touk.nussknacker.engine.compile.ExpressionCompiler
import pl.touk.nussknacker.engine.compiledgraph.BaseCompiledParameter
import pl.touk.nussknacker.engine.expression.ExpressionEvaluator
import pl.touk.nussknacker.engine.expression.parse.CompiledExpression
import pl.touk.nussknacker.engine.graph.expression.Expression

// This class looks like a LazyParameter but actually it's not - it's a creator of the LazyParameter.
Expand Down Expand Up @@ -47,7 +45,8 @@ final class EvaluableLazyParameterCreator[T <: AnyRef](
deps.expressionEvaluator,
nodeId,
deps.jobData,
returnType
returnType,
None
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package pl.touk.nussknacker.engine.compile.nodecompilation
import cats.data.Validated.{Invalid, Valid, invalid, valid}
import cats.data.{NonEmptyList, ValidatedNel, Writer}
import cats.implicits._
import pl.touk.nussknacker.engine.api._
import pl.touk.nussknacker.engine.api.{CompiledExpression, _}
import pl.touk.nussknacker.engine.api.component.ComponentType
import pl.touk.nussknacker.engine.api.context.ProcessCompilationError._
import pl.touk.nussknacker.engine.api.context._
Expand Down Expand Up @@ -32,7 +32,7 @@ import pl.touk.nussknacker.engine.definition.component.methodbased.MethodBasedCo
import pl.touk.nussknacker.engine.definition.fragment.FragmentParametersDefinitionExtractor
import pl.touk.nussknacker.engine.definition.globalvariables.ExpressionConfigDefinition
import pl.touk.nussknacker.engine.definition.model.ModelDefinition
import pl.touk.nussknacker.engine.expression.parse.{CompiledExpression, TypedExpression, TypedExpressionMap}
import pl.touk.nussknacker.engine.expression.parse.{TypedExpression, TypedExpressionMap}
import pl.touk.nussknacker.engine.graph.evaluatedparam.{BranchParameters, Parameter => NodeParameter}
import pl.touk.nussknacker.engine.graph.expression.NodeExpressionId.branchParameterExpressionId
import pl.touk.nussknacker.engine.graph.expression._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ParameterEvaluator(
.toMap
}

private def prepareLazyParameter[T](param: TypedParameter, definition: ParameterDef)(
private def prepareLazyParameter(param: TypedParameter, definition: ParameterDef)(
implicit jobData: JobData,
nodeId: NodeId,
lazyParameterCreationStrategy: LazyParameterCreationStrategy
Expand Down Expand Up @@ -88,19 +88,21 @@ class ParameterEvaluator(
}
}

private def prepareLazyParameterExpression[T](definition: ParameterDef, exprValue: TypedExpression)(
private def prepareLazyParameterExpression(definition: ParameterDef, exprValue: TypedExpression)(
implicit jobData: JobData,
nodeId: NodeId,
lazyParameterCreationStrategy: LazyParameterCreationStrategy
): LazyParameter[Nothing] = {
): LazyParameter[AnyRef] = {
lazyParameterCreationStrategy match {
case EvaluableLazyParameterStrategy =>
new EvaluableLazyParameter(
CompiledParameter(exprValue, definition),
runtimeExpressionEvaluator,
nodeId,
jobData
compiledParameter = CompiledParameter(exprValue, definition),
expressionEvaluator = runtimeExpressionEvaluator,
nodeId = nodeId,
jobData = jobData,
customEvaluate = definition.customEvaluate
)
// TODO: add customEvaluate here
case PostponedEvaluatorLazyParameterStrategy =>
new EvaluableLazyParameterCreator(
nodeId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package pl.touk.nussknacker.engine.compiledgraph

import pl.touk.nussknacker.engine.api.{BaseCompiledParameter, CompiledExpression}
import pl.touk.nussknacker.engine.api.definition.Parameter
import pl.touk.nussknacker.engine.api.expression.ExpressionTypingInfo
import pl.touk.nussknacker.engine.api.parameter.ParameterName
import pl.touk.nussknacker.engine.expression.parse.{CompiledExpression, TypedExpression}
import pl.touk.nussknacker.engine.expression.parse.TypedExpression

object CompiledParameter {

Expand All @@ -29,10 +30,3 @@ final case class CompiledParameter(
override val shouldBeWrappedWithJavaOptional: Boolean,
typingInfo: ExpressionTypingInfo
) extends BaseCompiledParameter

trait BaseCompiledParameter {
def name: ParameterName
def expression: CompiledExpression
def shouldBeWrappedWithScalaOption: Boolean
def shouldBeWrappedWithJavaOptional: Boolean
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package pl.touk.nussknacker.engine.compiledgraph

import pl.touk.nussknacker.engine.api.CompiledExpression
import pl.touk.nussknacker.engine.compiledgraph.service.ServiceRef
import pl.touk.nussknacker.engine.compiledgraph.variable.Field
import pl.touk.nussknacker.engine.expression.parse.{CompiledExpression, TypedExpression}
import pl.touk.nussknacker.engine.expression.parse.TypedExpression
import pl.touk.nussknacker.engine.graph.node.BranchEndDefinition

object node {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pl.touk.nussknacker.engine.compiledgraph

import pl.touk.nussknacker.engine.expression.parse.CompiledExpression
import pl.touk.nussknacker.engine.api.CompiledExpression

object variable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import pl.touk.nussknacker.engine.api.context.PartSubGraphCompilationError
import pl.touk.nussknacker.engine.api.context.ProcessCompilationError._
import pl.touk.nussknacker.engine.api.definition.{CompileTimeEvaluableValueValidator, Validator}
import pl.touk.nussknacker.engine.api.parameter.ParameterName
import pl.touk.nussknacker.engine.api.{Context, CustomMetaData, JobData, MetaData, NodeId}
import pl.touk.nussknacker.engine.api.{CompiledExpression, Context, CustomMetaData, JobData, MetaData, NodeId}
import pl.touk.nussknacker.engine.definition.component.parameter.validator.ValidationExpressionParameterValidator.variableName
import pl.touk.nussknacker.engine.expression.ExpressionEvaluator
import pl.touk.nussknacker.engine.expression.parse.CompiledExpression
import pl.touk.nussknacker.engine.graph.expression.Expression

import scala.util.Try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package pl.touk.nussknacker.engine.expression
import pl.touk.nussknacker.engine.api.typed.CustomNodeValidationException
import pl.touk.nussknacker.engine.api._
import pl.touk.nussknacker.engine.api.parameter.ParameterName
import pl.touk.nussknacker.engine.compiledgraph.{BaseCompiledParameter, CompiledParameter}
import pl.touk.nussknacker.engine.expression.parse.CompiledExpression
import pl.touk.nussknacker.engine.compiledgraph.CompiledParameter
import pl.touk.nussknacker.engine.util.Implicits.RichScalaMap
import pl.touk.nussknacker.engine.variables.GlobalVariablesPreparer

Expand Down Expand Up @@ -39,7 +38,7 @@ class ExpressionEvaluator(
globalVariablesPreparer: GlobalVariablesPreparer,
listeners: Seq[ProcessListener],
cacheGlobalVariables: Boolean
) {
) extends BaseExpressionEvaluator {
private def prepareGlobals(jobData: JobData): Map[String, Any] =
globalVariablesPreparer.prepareGlobalVariables(jobData).mapValuesNow(_.obj)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package pl.touk.nussknacker.engine.expression

import pl.touk.nussknacker.engine.api.Context
import pl.touk.nussknacker.engine.api.{CompiledExpression, Context}
import pl.touk.nussknacker.engine.graph.expression.Expression.Language
import pl.touk.nussknacker.engine.expression.parse.CompiledExpression
import pl.touk.nussknacker.engine.spel.SpelExpressionParser.Flavour

case class NullExpression(original: String, flavour: Flavour) extends CompiledExpression {
Expand Down
Loading
Loading