1616package io .serverlessworkflow .impl ;
1717
1818import static io .serverlessworkflow .impl .WorkflowUtils .*;
19- import static io .serverlessworkflow .impl .json .JsonUtils .*;
2019
21- import com .fasterxml .jackson .databind .JsonNode ;
2220import io .serverlessworkflow .api .types .Input ;
2321import io .serverlessworkflow .api .types .Output ;
2422import io .serverlessworkflow .api .types .TaskBase ;
25- import io .serverlessworkflow .api .types .TaskItem ;
2623import io .serverlessworkflow .api .types .Workflow ;
2724import io .serverlessworkflow .impl .executors .DefaultTaskExecutorFactory ;
2825import io .serverlessworkflow .impl .executors .TaskExecutor ;
3431import io .serverlessworkflow .impl .jsonschema .SchemaValidator ;
3532import io .serverlessworkflow .impl .jsonschema .SchemaValidatorFactory ;
3633import io .serverlessworkflow .resources .DefaultResourceLoaderFactory ;
34+ import io .serverlessworkflow .resources .ResourceLoader ;
3735import io .serverlessworkflow .resources .ResourceLoaderFactory ;
3836import java .nio .file .Path ;
3937import java .util .Collection ;
4038import java .util .Collections ;
4139import java .util .HashSet ;
42- import java .util .List ;
4340import java .util .Map ;
4441import java .util .Optional ;
4542import java .util .concurrent .ConcurrentHashMap ;
4643
4744public class WorkflowDefinition {
4845
46+ private final Workflow workflow ;
47+ private final Collection <WorkflowExecutionListener > listeners ;
48+ private Optional <SchemaValidator > inputSchemaValidator = Optional .empty ();
49+ private Optional <SchemaValidator > outputSchemaValidator = Optional .empty ();
50+ private Optional <WorkflowFilter > inputFilter = Optional .empty ();
51+ private Optional <WorkflowFilter > outputFilter = Optional .empty ();
52+ private final TaskExecutorFactory taskFactory ;
53+ private final ExpressionFactory exprFactory ;
54+ private final ResourceLoader resourceLoader ;
55+ private final SchemaValidatorFactory schemaValidatorFactory ;
56+ private final Map <String , TaskExecutor <? extends TaskBase >> taskExecutors =
57+ new ConcurrentHashMap <>();
58+
4959 private WorkflowDefinition (
5060 Workflow workflow ,
5161 Collection <WorkflowExecutionListener > listeners ,
52- WorkflowFactories factories ) {
62+ TaskExecutorFactory taskFactory ,
63+ ResourceLoader resourceLoader ,
64+ ExpressionFactory exprFactory ,
65+ SchemaValidatorFactory schemaValidatorFactory ) {
5366 this .workflow = workflow ;
5467 this .listeners = listeners ;
55- this .factories = factories ;
68+ this .taskFactory = taskFactory ;
69+ this .exprFactory = exprFactory ;
70+ this .schemaValidatorFactory = schemaValidatorFactory ;
71+ this .resourceLoader = resourceLoader ;
5672 if (workflow .getInput () != null ) {
5773 Input input = workflow .getInput ();
5874 this .inputSchemaValidator =
5975 getSchemaValidator (
60- factories . getValidatorFactory () , schemaToNode (factories , input .getSchema ()));
61- this .inputFilter = buildWorkflowFilter (factories . getExpressionFactory () , input .getFrom ());
76+ schemaValidatorFactory , schemaToNode (resourceLoader , input .getSchema ()));
77+ this .inputFilter = buildWorkflowFilter (exprFactory , input .getFrom ());
6278 }
6379 if (workflow .getOutput () != null ) {
6480 Output output = workflow .getOutput ();
6581 this .outputSchemaValidator =
6682 getSchemaValidator (
67- factories . getValidatorFactory () , schemaToNode (factories , output .getSchema ()));
68- this .outputFilter = buildWorkflowFilter (factories . getExpressionFactory () , output .getAs ());
83+ schemaValidatorFactory , schemaToNode (resourceLoader , output .getSchema ()));
84+ this .outputFilter = buildWorkflowFilter (exprFactory , output .getAs ());
6985 }
7086 }
7187
72- private final Workflow workflow ;
73- private final Collection <WorkflowExecutionListener > listeners ;
74- private final WorkflowFactories factories ;
75- private Optional <SchemaValidator > inputSchemaValidator = Optional .empty ();
76- private Optional <SchemaValidator > outputSchemaValidator = Optional .empty ();
77- private Optional <WorkflowFilter > inputFilter = Optional .empty ();
78- private Optional <WorkflowFilter > outputFilter = Optional .empty ();
79-
80- private final Map <String , TaskExecutor <? extends TaskBase >> taskExecutors =
81- new ConcurrentHashMap <>();
82-
8388 public static class Builder {
8489 private final Workflow workflow ;
8590 private TaskExecutorFactory taskFactory = DefaultTaskExecutorFactory .get ();
@@ -127,18 +132,15 @@ public Builder withSchemaValidatorFactory(SchemaValidatorFactory factory) {
127132 }
128133
129134 public WorkflowDefinition build () {
130- WorkflowDefinition def =
131- new WorkflowDefinition (
132- workflow ,
133- listeners == null
134- ? Collections .emptySet ()
135- : Collections .unmodifiableCollection (listeners ),
136- new WorkflowFactories (
137- taskFactory ,
138- resourceLoaderFactory .getResourceLoader (path ),
139- exprFactory ,
140- schemaValidatorFactory ));
141- return def ;
135+ return new WorkflowDefinition (
136+ workflow ,
137+ listeners == null
138+ ? Collections .emptySet ()
139+ : Collections .unmodifiableCollection (listeners ),
140+ taskFactory ,
141+ resourceLoaderFactory .getResourceLoader (path ),
142+ exprFactory ,
143+ schemaValidatorFactory );
142144 }
143145 }
144146
@@ -147,7 +149,7 @@ public static Builder builder(Workflow workflow) {
147149 }
148150
149151 public WorkflowInstance execute (Object input ) {
150- return new WorkflowInstance (JsonUtils .fromValue (input ));
152+ return new WorkflowInstance (this , JsonUtils .fromValue (input ));
151153 }
152154
153155 enum State {
@@ -156,50 +158,48 @@ enum State {
156158 FINISHED
157159 };
158160
159- public class WorkflowInstance {
160-
161- private JsonNode output ;
162- private State state ;
163- private WorkflowContext context ;
164-
165- private WorkflowInstance (JsonNode input ) {
166- this .output = input ;
167- inputSchemaValidator .ifPresent (v -> v .validate (input ));
168- this .context = WorkflowContext .builder (input ).build ();
169- inputFilter .ifPresent (f -> output = f .apply (context , Optional .empty (), output ));
170- this .state = State .STARTED ;
171- processDo (workflow .getDo ());
172- outputFilter .ifPresent (f -> output = f .apply (context , Optional .empty (), output ));
173- outputSchemaValidator .ifPresent (v -> v .validate (output ));
174- }
161+ public Optional <SchemaValidator > inputSchemaValidator () {
162+ return inputSchemaValidator ;
163+ }
175164
176- private void processDo (List <TaskItem > tasks ) {
177- context .position ().addProperty ("do" );
178- int index = 0 ;
179- for (TaskItem task : tasks ) {
180- context .position ().addIndex (++index ).addProperty (task .getName ());
181- listeners .forEach (l -> l .onTaskStarted (context .position (), task .getTask ()));
182- this .output =
183- taskExecutors
184- .computeIfAbsent (
185- context .position ().jsonPointer (),
186- k -> factories .getTaskFactory ().getTaskExecutor (task .getTask (), factories ))
187- .apply (context , output );
188- listeners .forEach (l -> l .onTaskEnded (context .position (), task .getTask ()));
189- context .position ().back ().back ();
190- }
191- }
165+ public Optional <WorkflowFilter > inputFilter () {
166+ return inputFilter ;
167+ }
192168
193- public State state () {
194- return state ;
195- }
169+ public Workflow workflow () {
170+ return workflow ;
171+ }
196172
197- public Object output () {
198- return toJavaValue ( output ) ;
199- }
173+ public Collection < WorkflowExecutionListener > listeners () {
174+ return listeners ;
175+ }
200176
201- public Object outputAsJsonNode () {
202- return output ;
203- }
177+ public Map <String , TaskExecutor <? extends TaskBase >> taskExecutors () {
178+ return taskExecutors ;
179+ }
180+
181+ public TaskExecutorFactory taskFactory () {
182+ return taskFactory ;
183+ }
184+
185+ public Optional <WorkflowFilter > outputFilter () {
186+ return outputFilter ;
187+ }
188+
189+ public Optional <SchemaValidator > outputSchemaValidator () {
190+ return outputSchemaValidator ;
191+ }
192+
193+ public ExpressionFactory expressionFactory () {
194+ return exprFactory ;
195+ }
196+
197+ public SchemaValidatorFactory validatorFactory () {
198+ return schemaValidatorFactory ;
199+ }
200+
201+ public ResourceLoader resourceLoader () {
202+
203+ return resourceLoader ;
204204 }
205205}
0 commit comments