|
32 | 32 | import io.serverlessworkflow.fluent.spec.configurers.AuthenticationConfigurer; |
33 | 33 | import io.serverlessworkflow.impl.TaskContextData; |
34 | 34 | import io.serverlessworkflow.impl.WorkflowContextData; |
| 35 | +import io.serverlessworkflow.impl.WorkflowModel; |
35 | 36 | import java.net.URI; |
36 | 37 | import java.util.Collection; |
37 | 38 | import java.util.List; |
@@ -279,6 +280,176 @@ public static FuncPredicateEventConfigurer event(String type) { |
279 | 280 | return OPS.event(type); |
280 | 281 | } |
281 | 282 |
|
| 283 | + /** |
| 284 | + * Create an input transformation that enriches the typed last state with the root workflow input |
| 285 | + * as WorkflowModel. |
| 286 | + * |
| 287 | + * <p>This is useful when you want to combine the last task output (with its actual type) with the |
| 288 | + * original workflow input as WorkflowModel. |
| 289 | + * |
| 290 | + * <p>Example usage: |
| 291 | + * |
| 292 | + * <pre>{@code |
| 293 | + * function("processData", (Long input) -> input + 5, Long.class), |
| 294 | + * function("combineData", (Long enrichedValue) -> enrichedValue, Long.class) |
| 295 | + * .inputFrom(enrich((lastState, rootInputModel) -> { |
| 296 | + * Long rootInput = rootInputModel.as(Long.class).orElse(0L); |
| 297 | + * return lastState + rootInput; |
| 298 | + * }, Long.class)) |
| 299 | + * }</pre> |
| 300 | + * |
| 301 | + * @param fn the enrichment function that receives typed lastState and WorkflowModel rootInput |
| 302 | + * @param lastStateClass the class of the last state type |
| 303 | + * @param <T> the type of the last state |
| 304 | + * @param <R> the type of the enriched result |
| 305 | + * @return a JavaContextFunction that can be used with inputFrom |
| 306 | + */ |
| 307 | + public static <T, R> JavaContextFunction<T, R> enrich( |
| 308 | + EnrichWithModelBiFunction<T, R> fn, Class<T> lastStateClass) { |
| 309 | + return (lastState, workflowContext) -> { |
| 310 | + Objects.requireNonNull(lastStateClass, "lastStateClass must not be null"); |
| 311 | + final WorkflowModel rootInput = workflowContext.instanceData().input(); |
| 312 | + final WorkflowModel lastStateModel = |
| 313 | + workflowContext.definition().application().modelFactory().fromAny(lastState); |
| 314 | + final T typedLastState = |
| 315 | + lastStateModel |
| 316 | + .as(lastStateClass) |
| 317 | + .orElseThrow( |
| 318 | + () -> |
| 319 | + new IllegalArgumentException( |
| 320 | + "Cannot convert lastState of type " |
| 321 | + + (lastState != null ? lastState.getClass().getName() : "null") |
| 322 | + + " to " |
| 323 | + + lastStateClass.getName())); |
| 324 | + return fn.apply(typedLastState, rootInput); |
| 325 | + }; |
| 326 | + } |
| 327 | + |
| 328 | + /** |
| 329 | + * Create an input transformation that uses only the root workflow input as WorkflowModel. |
| 330 | + * |
| 331 | + * <p>This is useful when you want to transform the task input based solely on the original |
| 332 | + * workflow input, ignoring the last state. |
| 333 | + * |
| 334 | + * <p>Example usage: |
| 335 | + * |
| 336 | + * <pre>{@code |
| 337 | + * function("processData", (Long input) -> input * 2, Long.class) |
| 338 | + * .inputFrom(enrich(rootInput -> rootInput.asNumber().orElseThrow())) |
| 339 | + * }</pre> |
| 340 | + * |
| 341 | + * @param fn the function that receives the root workflow input and returns the enriched result |
| 342 | + * @param <R> the type of the enriched result |
| 343 | + * @return a JavaContextFunction that can be used with inputFrom |
| 344 | + */ |
| 345 | + public static <R> JavaContextFunction<Object, R> enrich(Function<WorkflowModel, R> fn) { |
| 346 | + return (lastState, workflowContext) -> { |
| 347 | + final WorkflowModel rootInput = workflowContext.instanceData().input(); |
| 348 | + return fn.apply(rootInput); |
| 349 | + }; |
| 350 | + } |
| 351 | + |
| 352 | + /** |
| 353 | + * Create an output transformation that enriches the typed task output with the root workflow |
| 354 | + * input as WorkflowModel. |
| 355 | + * |
| 356 | + * <p>This is useful when you want to combine the task output (with its actual type) with the |
| 357 | + * original workflow input as WorkflowModel. |
| 358 | + * |
| 359 | + * <p>Example usage: |
| 360 | + * |
| 361 | + * <pre>{@code |
| 362 | + * function("processData", (Long input) -> input + 5, Long.class) |
| 363 | + * .outputAs(FuncDSL.enrichOutput((taskOutput, rootInputModel) -> { |
| 364 | + * Long rootInput = rootInputModel.as(Long.class).orElse(0L); |
| 365 | + * return taskOutput + rootInput; |
| 366 | + * }, Long.class)) |
| 367 | + * }</pre> |
| 368 | + * |
| 369 | + * @param fn the enrichment function that receives typed taskOutput and WorkflowModel rootInput |
| 370 | + * @param taskOutputClass the class of the task output type |
| 371 | + * @param <T> the type of the task output |
| 372 | + * @param <R> the type of the enriched result |
| 373 | + * @return a JavaContextFunction that can be used with outputAs |
| 374 | + */ |
| 375 | + public static <T, R> JavaContextFunction<T, R> enrichOutput( |
| 376 | + EnrichWithModelBiFunction<T, R> fn, Class<T> taskOutputClass) { |
| 377 | + return (taskOutput, workflowContext) -> { |
| 378 | + Objects.requireNonNull(taskOutputClass, "taskOutputClass must not be null"); |
| 379 | + final WorkflowModel rootInput = workflowContext.instanceData().input(); |
| 380 | + final T typedTaskOutput = convertTaskOutput(taskOutput, taskOutputClass); |
| 381 | + return fn.apply(typedTaskOutput, rootInput); |
| 382 | + }; |
| 383 | + } |
| 384 | + |
| 385 | + private static <T> T convertTaskOutput(Object taskOutput, Class<T> taskOutputClass) { |
| 386 | + if (taskOutput == null) { |
| 387 | + return null; |
| 388 | + } |
| 389 | + if (taskOutputClass.isInstance(taskOutput)) { |
| 390 | + return taskOutputClass.cast(taskOutput); |
| 391 | + } |
| 392 | + if (Number.class.isAssignableFrom(taskOutputClass) && taskOutput instanceof Number) { |
| 393 | + Number number = (Number) taskOutput; |
| 394 | + if (taskOutputClass == Long.class) { |
| 395 | + @SuppressWarnings("unchecked") |
| 396 | + T converted = (T) Long.valueOf(number.longValue()); |
| 397 | + return converted; |
| 398 | + } |
| 399 | + if (taskOutputClass == Integer.class) { |
| 400 | + @SuppressWarnings("unchecked") |
| 401 | + T converted = (T) Integer.valueOf(number.intValue()); |
| 402 | + return converted; |
| 403 | + } |
| 404 | + if (taskOutputClass == Short.class) { |
| 405 | + @SuppressWarnings("unchecked") |
| 406 | + T converted = (T) Short.valueOf(number.shortValue()); |
| 407 | + return converted; |
| 408 | + } |
| 409 | + if (taskOutputClass == Byte.class) { |
| 410 | + @SuppressWarnings("unchecked") |
| 411 | + T converted = (T) Byte.valueOf(number.byteValue()); |
| 412 | + return converted; |
| 413 | + } |
| 414 | + if (taskOutputClass == Double.class) { |
| 415 | + @SuppressWarnings("unchecked") |
| 416 | + T converted = (T) Double.valueOf(number.doubleValue()); |
| 417 | + return converted; |
| 418 | + } |
| 419 | + if (taskOutputClass == Float.class) { |
| 420 | + @SuppressWarnings("unchecked") |
| 421 | + T converted = (T) Float.valueOf(number.floatValue()); |
| 422 | + return converted; |
| 423 | + } |
| 424 | + } |
| 425 | + // Fallback to the original behavior for incompatible types. |
| 426 | + return taskOutputClass.cast(taskOutput); |
| 427 | + } |
| 428 | + |
| 429 | + /** |
| 430 | + * Create an output transformation that uses only the root workflow input as WorkflowModel. |
| 431 | + * |
| 432 | + * <p>This is useful when you want to transform the task output based solely on the original |
| 433 | + * workflow input, ignoring the actual task output. |
| 434 | + * |
| 435 | + * <p>Example usage: |
| 436 | + * |
| 437 | + * <pre>{@code |
| 438 | + * function("processData", (Long input) -> input * 2, Long.class) |
| 439 | + * .outputAs(FuncDSL.enrichOutput(rootInput -> rootInput.asNumber().orElseThrow())) |
| 440 | + * }</pre> |
| 441 | + * |
| 442 | + * @param fn the function that receives the root workflow input and returns the enriched result |
| 443 | + * @param <R> the type of the enriched result |
| 444 | + * @return a JavaContextFunction that can be used with outputAs |
| 445 | + */ |
| 446 | + public static <R> JavaContextFunction<Object, R> enrichOutput(Function<WorkflowModel, R> fn) { |
| 447 | + return (taskOutput, workflowContext) -> { |
| 448 | + final WorkflowModel rootInput = workflowContext.instanceData().input(); |
| 449 | + return fn.apply(rootInput); |
| 450 | + }; |
| 451 | + } |
| 452 | + |
282 | 453 | /** |
283 | 454 | * Create a {@link FuncCallStep} that calls a simple Java {@link Function} with explicit input |
284 | 455 | * type. |
|
0 commit comments