1515 */
1616package io .serverlessworkflow .impl .expressions .func ;
1717
18+ import io .cloudevents .CloudEventData ;
1819import io .serverlessworkflow .api .types .TaskBase ;
1920import io .serverlessworkflow .api .types .TaskMetadata ;
2021import io .serverlessworkflow .api .types .func .TypedFunction ;
2122import io .serverlessworkflow .api .types .func .TypedPredicate ;
22- import io .serverlessworkflow .impl .TaskContext ;
23- import io .serverlessworkflow .impl .WorkflowContext ;
24- import io .serverlessworkflow .impl .WorkflowFilter ;
25- import io .serverlessworkflow .impl .WorkflowModel ;
2623import io .serverlessworkflow .impl .WorkflowModelFactory ;
27- import io .serverlessworkflow .impl .expressions .Expression ;
28- import io .serverlessworkflow .impl .expressions .ExpressionFactory ;
24+ import io .serverlessworkflow .impl .WorkflowPredicate ;
25+ import io .serverlessworkflow .impl .expressions .AbstractExpressionFactory ;
26+ import io .serverlessworkflow .impl .expressions .ExpressionDescriptor ;
27+ import io .serverlessworkflow .impl .expressions .ObjectExpression ;
2928import io .serverlessworkflow .impl .expressions .TaskMetadataKeys ;
29+ import java .time .OffsetDateTime ;
30+ import java .util .Collection ;
31+ import java .util .Map ;
3032import java .util .Optional ;
31- import java .util .function .BiFunction ;
32- import java .util .function .BiPredicate ;
3333import java .util .function .Function ;
3434import java .util .function .Predicate ;
3535
36- public class JavaExpressionFactory implements ExpressionFactory {
36+ public class JavaExpressionFactory extends AbstractExpressionFactory {
3737
3838 private final WorkflowModelFactory modelFactory = new JavaModelFactory ();
39- private final Expression dummyExpression =
40- new Expression () {
41- @ Override
42- public WorkflowModel eval (
43- WorkflowContext workflowContext , TaskContext context , WorkflowModel model ) {
44- return model ;
45- }
46- };
4739
4840 @ Override
49- public Expression buildExpression (String expression ) {
50- return dummyExpression ;
51- }
52-
53- @ Override
54- public WorkflowFilter buildFilter (String expr , Object value ) {
41+ public ObjectExpression buildExpression (ExpressionDescriptor descriptor ) {
42+ Object value = descriptor .asObject ();
5543 if (value instanceof Function func ) {
56- return (w , t , n ) -> modelFactory . fromAny ( func .apply (n .asJavaObject () ));
44+ return (w , t , n ) -> func .apply (n .asJavaObject ());
5745 } else if (value instanceof TypedFunction func ) {
58- return (w , t , n ) ->
59- modelFactory .fromAny (func .function ().apply (n .as (func .argClass ()).orElseThrow ()));
60- } else if (value instanceof Predicate pred ) {
61- return fromPredicate (pred );
62- } else if (value instanceof TypedPredicate pred ) {
63- return fromPredicate (pred );
64- } else if (value instanceof BiPredicate pred ) {
65- return (w , t , n ) -> modelFactory .from (pred .test (w , t ));
66- } else if (value instanceof BiFunction func ) {
67- return (w , t , n ) -> modelFactory .fromAny (func .apply (w , t ));
68- } else if (value instanceof WorkflowFilter filter ) {
69- return filter ;
46+ return (w , t , n ) -> func .function ().apply (n .as (func .argClass ()).orElseThrow ());
7047 } else {
7148 return (w , t , n ) -> modelFactory .fromAny (value );
7249 }
7350 }
7451
7552 @ SuppressWarnings ({"rawtypes" , "unchecked" })
76- private WorkflowFilter fromPredicate (Predicate pred ) {
77- return (w , t , n ) -> modelFactory . from ( pred .test (n .asJavaObject () ));
53+ private WorkflowPredicate fromPredicate (Predicate pred ) {
54+ return (w , t , n ) -> pred .test (n .asJavaObject ());
7855 }
7956
8057 @ SuppressWarnings ({"rawtypes" , "unchecked" })
81- private WorkflowFilter fromPredicate (TypedPredicate pred ) {
82- return (w , t , n ) -> modelFactory . from ( pred .pred ().test (n .as (pred .argClass ()).orElseThrow () ));
58+ private WorkflowPredicate fromPredicate (TypedPredicate pred ) {
59+ return (w , t , n ) -> pred .pred ().test (n .as (pred .argClass ()).orElseThrow ());
8360 }
8461
8562 @ Override
86- public Optional <WorkflowFilter > buildIfFilter (TaskBase task ) {
63+ public Optional <WorkflowPredicate > buildIfFilter (TaskBase task ) {
8764 TaskMetadata metadata = task .getMetadata ();
8865 if (metadata != null ) {
8966 Object obj = metadata .getAdditionalProperties ().get (TaskMetadataKeys .IF_PREDICATE );
@@ -93,11 +70,54 @@ public Optional<WorkflowFilter> buildIfFilter(TaskBase task) {
9370 return Optional .of (fromPredicate (pred ));
9471 }
9572 }
96- return ExpressionFactory . super .buildIfFilter (task );
73+ return super .buildIfFilter (task );
9774 }
9875
9976 @ Override
10077 public WorkflowModelFactory modelFactory () {
10178 return modelFactory ;
10279 }
80+
81+ @ Override
82+ public WorkflowPredicate buildPredicate (ExpressionDescriptor desc ) {
83+ Object value = desc .asObject ();
84+ if (value instanceof Predicate pred ) {
85+ return fromPredicate (pred );
86+ } else if (value instanceof TypedPredicate pred ) {
87+ return fromPredicate (pred );
88+ } else if (value instanceof Boolean bool ) {
89+ return (w , f , n ) -> bool ;
90+ } else {
91+ throw new IllegalArgumentException ("value should be a predicate or a boolean" );
92+ }
93+ }
94+
95+ @ Override
96+ protected String toString (Object eval ) {
97+ return asClass (eval , String .class );
98+ }
99+
100+ @ Override
101+ protected CloudEventData toCloudEventData (Object eval ) {
102+ return asClass (eval , CloudEventData .class );
103+ }
104+
105+ @ Override
106+ protected OffsetDateTime toDate (Object eval ) {
107+ return asClass (eval , OffsetDateTime .class );
108+ }
109+
110+ @ Override
111+ protected Map <String , Object > toMap (Object eval ) {
112+ return asClass (eval , Map .class );
113+ }
114+
115+ @ Override
116+ protected Collection <?> toCollection (Object obj ) {
117+ return asClass (obj , Collection .class );
118+ }
119+
120+ private <T > T asClass (Object obj , Class <T > clazz ) {
121+ return clazz .cast (obj );
122+ }
103123}
0 commit comments