Skip to content

Commit 19d1fc3

Browse files
hielsnoppeNiels Hoppe
authored andcommitted
Implement named transitions
- Backport #850 - Fixes #989 Co-authored-by: Niels Hoppe <[email protected]>
1 parent 6cf3851 commit 19d1fc3

20 files changed

+252
-43
lines changed

spring-statemachine-core/src/main/java/org/springframework/statemachine/config/AbstractStateMachineFactory.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2020 the original author or authors.
2+
* Copyright 2015-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -864,7 +864,7 @@ private StateMachine<S, E> buildMachine(Map<Object, StateMachine<S, E>> machineM
864864
}
865865
DefaultExternalTransition<S, E> transition = new DefaultExternalTransition<S, E>(stateMap.get(source),
866866
stateMap.get(target), transitionData.getActions(), event, transitionData.getGuard(), trigger,
867-
transitionData.getSecurityRule());
867+
transitionData.getSecurityRule(), transitionData.getName());
868868
transitions.add(transition);
869869

870870
} else if (transitionData.getKind() == TransitionKind.LOCAL) {
@@ -874,12 +874,12 @@ private StateMachine<S, E> buildMachine(Map<Object, StateMachine<S, E>> machineM
874874
}
875875
DefaultLocalTransition<S, E> transition = new DefaultLocalTransition<S, E>(stateMap.get(source),
876876
stateMap.get(target), transitionData.getActions(), event, transitionData.getGuard(), trigger,
877-
transitionData.getSecurityRule());
877+
transitionData.getSecurityRule(), transitionData.getName());
878878
transitions.add(transition);
879879
} else if (transitionData.getKind() == TransitionKind.INTERNAL) {
880880
DefaultInternalTransition<S, E> transition = new DefaultInternalTransition<S, E>(stateMap.get(source),
881881
transitionData.getActions(), event, transitionData.getGuard(), trigger,
882-
transitionData.getSecurityRule());
882+
transitionData.getSecurityRule(), transitionData.getName());
883883
transitions.add(transition);
884884
}
885885
}

spring-statemachine-core/src/main/java/org/springframework/statemachine/config/builders/StateMachineTransitionBuilder.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -176,17 +176,19 @@ public HistoryTransitionConfigurer<S, E> withHistory() throws Exception {
176176
* @param guard the guard
177177
* @param kind the kind
178178
* @param securityRule the security rule
179+
* @param name the name
179180
*/
180181
public void addTransition(S source, S target, S state, E event, Long period, Integer count,
181182
Collection<Function<StateContext<S, E>, Mono<Void>>> actions,
182-
Function<StateContext<S, E>, Mono<Boolean>> guard, TransitionKind kind, SecurityRule securityRule) {
183+
Function<StateContext<S, E>, Mono<Boolean>> guard, TransitionKind kind, SecurityRule securityRule,
184+
String name) {
183185
// if rule not given, get it from global
184186
if (securityRule == null) {
185187
@SuppressWarnings("unchecked")
186188
ConfigurationData<S, E> config = getSharedObject(ConfigurationData.class);
187189
securityRule = config.getTransitionSecurityRule();
188190
}
189-
transitionData.add(new TransitionData<>(source, target, state, event, period, count, actions, guard, kind, securityRule));
191+
transitionData.add(new TransitionData<>(source, target, state, event, period, count, actions, guard, kind, securityRule, name));
190192
}
191193

192194
/**

spring-statemachine-core/src/main/java/org/springframework/statemachine/config/configurers/AbstractTransitionConfigurer.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,7 @@ public abstract class AbstractTransitionConfigurer<S, E> extends
5353
private final Collection<Function<StateContext<S, E>, Mono<Void>>> actions = new ArrayList<>();
5454
private Function<StateContext<S, E>, Mono<Boolean>> guard;
5555
private SecurityRule securityRule;
56+
private String name;
5657

5758
protected S getSource() {
5859
return source;
@@ -94,6 +95,10 @@ protected SecurityRule getSecurityRule() {
9495
return securityRule;
9596
}
9697

98+
protected String getName() {
99+
return name;
100+
}
101+
97102
protected void setSource(S source) {
98103
this.source = source;
99104
}
@@ -156,4 +161,8 @@ protected void setSecurityRule(String expression) {
156161
securityRule.setExpression(expression);
157162
}
158163

164+
protected void setName(String name) {
165+
this.name = name;
166+
}
167+
159168
}

spring-statemachine-core/src/main/java/org/springframework/statemachine/config/configurers/DefaultExternalTransitionConfigurer.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,7 +44,7 @@ public class DefaultExternalTransitionConfigurer<S, E> extends AbstractTransitio
4444
@Override
4545
public void configure(StateMachineTransitionBuilder<S, E> builder) throws Exception {
4646
builder.addTransition(getSource(), getTarget(), getState(), getEvent(), getPeriod(), getCount(), getActions(), getGuard(), TransitionKind.EXTERNAL,
47-
getSecurityRule());
47+
getSecurityRule(), getName());
4848
}
4949

5050
@Override
@@ -127,4 +127,10 @@ public ExternalTransitionConfigurer<S, E> secured(String expression) {
127127
return this;
128128
}
129129

130+
@Override
131+
public ExternalTransitionConfigurer<S, E> name(String name) {
132+
setName(name);
133+
return this;
134+
}
135+
130136
}

spring-statemachine-core/src/main/java/org/springframework/statemachine/config/configurers/DefaultInternalTransitionConfigurer.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,7 +44,7 @@ public class DefaultInternalTransitionConfigurer<S, E> extends AbstractTransitio
4444
@Override
4545
public void configure(StateMachineTransitionBuilder<S, E> builder) throws Exception {
4646
builder.addTransition(getSource(), getTarget(), getState(), getEvent(), getPeriod(), getCount(), getActions(), getGuard(), TransitionKind.INTERNAL,
47-
getSecurityRule());
47+
getSecurityRule(), getName());
4848
}
4949

5050
@Override
@@ -121,4 +121,10 @@ public InternalTransitionConfigurer<S, E> secured(String expression) {
121121
return this;
122122
}
123123

124+
@Override
125+
public InternalTransitionConfigurer<S, E> name(String name) {
126+
setName(name);
127+
return this;
128+
}
129+
124130
}

spring-statemachine-core/src/main/java/org/springframework/statemachine/config/configurers/DefaultLocalTransitionConfigurer.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ public class DefaultLocalTransitionConfigurer<S, E> extends AbstractTransitionCo
4343
@Override
4444
public void configure(StateMachineTransitionBuilder<S, E> builder) throws Exception {
4545
builder.addTransition(getSource(), getTarget(), getState(), getEvent(), getPeriod(), getCount(), getActions(), getGuard(), TransitionKind.LOCAL,
46-
getSecurityRule());
46+
getSecurityRule(), getName());
4747
}
4848

4949
@Override
@@ -126,4 +126,10 @@ public LocalTransitionConfigurer<S, E> secured(String expression) {
126126
return this;
127127
}
128128

129+
@Override
130+
public LocalTransitionConfigurer<S, E> name(String name) {
131+
setName(name);
132+
return this;
133+
}
134+
129135
}

spring-statemachine-core/src/main/java/org/springframework/statemachine/config/configurers/TransitionConfigurer.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -138,4 +138,13 @@ public interface TransitionConfigurer<T, S, E> extends
138138
*/
139139
T secured(String expression);
140140

141+
/**
142+
* Specify a name for this {@link Transition}.
143+
*
144+
* @param name the name
145+
* @return configurer for chaining
146+
* @param name
147+
* @return
148+
*/
149+
T name(String name);
141150
}

spring-statemachine-core/src/main/java/org/springframework/statemachine/config/model/TransitionData.java

+51-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ public class TransitionData<S, E> {
4141
private final Function<StateContext<S, E>, Mono<Boolean>> guard;
4242
private final TransitionKind kind;
4343
private final SecurityRule securityRule;
44+
private final String name;
4445

4546
/**
4647
* Instantiates a new transition data.
@@ -50,7 +51,7 @@ public class TransitionData<S, E> {
5051
* @param event the event
5152
*/
5253
public TransitionData(S source, S target, E event) {
53-
this(source, target, null, event, null, null, null, null, TransitionKind.EXTERNAL, null);
54+
this(source, target, null, event, null, null, null, null, TransitionKind.EXTERNAL, null, null);
5455
}
5556

5657
/**
@@ -65,7 +66,23 @@ public TransitionData(S source, S target, E event) {
6566
*/
6667
public TransitionData(S source, S target, E event, Collection<Function<StateContext<S, E>, Mono<Void>>> actions,
6768
Function<StateContext<S, E>, Mono<Boolean>> guard, TransitionKind kind) {
68-
this(source, target, null, event, null, null, actions, guard, kind, null);
69+
this(source, target, null, event, null, null, actions, guard, kind, null, null);
70+
}
71+
72+
/**
73+
* Instantiates a new transition data.
74+
*
75+
* @param source the source
76+
* @param target the target
77+
* @param event the event
78+
* @param actions the actions
79+
* @param guard the guard
80+
* @param kind the kind
81+
* @param name the name
82+
*/
83+
public TransitionData(S source, S target, E event, Collection<Function<StateContext<S, E>, Mono<Void>>> actions,
84+
Function<StateContext<S, E>, Mono<Boolean>> guard, TransitionKind kind, String name) {
85+
this(source, target, null, event, null, null, actions, guard, kind, null, name);
6986
}
7087

7188
/**
@@ -82,7 +99,25 @@ public TransitionData(S source, S target, E event, Collection<Function<StateCont
8299
public TransitionData(S source, S target, Long period, Integer count,
83100
Collection<Function<StateContext<S, E>, Mono<Void>>> actions,
84101
Function<StateContext<S, E>, Mono<Boolean>> guard, TransitionKind kind) {
85-
this(source, target, null, null, period, count, actions, guard, kind, null);
102+
this(source, target, null, null, period, count, actions, guard, kind, null, null);
103+
}
104+
105+
/**
106+
* Instantiates a new transition data.
107+
*
108+
* @param source the source
109+
* @param target the target
110+
* @param period the period
111+
* @param count the count
112+
* @param actions the actions
113+
* @param guard the guard
114+
* @param kind the kind
115+
* @param name the name
116+
*/
117+
public TransitionData(S source, S target, Long period, Integer count,
118+
Collection<Function<StateContext<S, E>, Mono<Void>>> actions,
119+
Function<StateContext<S, E>, Mono<Boolean>> guard, TransitionKind kind, String name) {
120+
this(source, target, null, null, period, count, actions, guard, kind, null, name);
86121
}
87122

88123
/**
@@ -98,10 +133,11 @@ public TransitionData(S source, S target, Long period, Integer count,
98133
* @param guard the guard
99134
* @param kind the kind
100135
* @param securityRule the security rule
136+
* @param name the name
101137
*/
102138
public TransitionData(S source, S target, S state, E event, Long period, Integer count,
103139
Collection<Function<StateContext<S, E>, Mono<Void>>> actions,
104-
Function<StateContext<S, E>, Mono<Boolean>> guard, TransitionKind kind, SecurityRule securityRule) {
140+
Function<StateContext<S, E>, Mono<Boolean>> guard, TransitionKind kind, SecurityRule securityRule, String name) {
105141
this.source = source;
106142
this.target = target;
107143
this.state = state;
@@ -112,6 +148,7 @@ public TransitionData(S source, S target, S state, E event, Long period, Integer
112148
this.guard = guard;
113149
this.kind = kind;
114150
this.securityRule = securityRule;
151+
this.name = (name == null) ? "" : name;
115152
}
116153

117154
/**
@@ -203,4 +240,13 @@ public TransitionKind getKind() {
203240
public SecurityRule getSecurityRule() {
204241
return securityRule;
205242
}
243+
244+
/**
245+
* Gets the name,
246+
*
247+
* @return the name
248+
*/
249+
public String getName() {
250+
return name;
251+
}
206252
}

spring-statemachine-core/src/main/java/org/springframework/statemachine/transition/AbstractExternalTransition.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2020 the original author or authors.
2+
* Copyright 2015-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,24 @@
2727

2828
public abstract class AbstractExternalTransition<S, E> extends AbstractTransition<S, E> {
2929

30+
/**
31+
* Instantiates a new abstract external transition.
32+
*
33+
* @param source the source
34+
* @param target the target
35+
* @param actions the actions
36+
* @param event the event
37+
* @param guard the guard
38+
* @param trigger the trigger
39+
* @param securityRule the security rule
40+
* @param name the name
41+
*/
42+
public AbstractExternalTransition(State<S, E> source, State<S, E> target,
43+
Collection<Function<StateContext<S, E>, Mono<Void>>> actions, E event,
44+
Function<StateContext<S, E>, Mono<Boolean>> guard, Trigger<S, E> trigger, SecurityRule securityRule, String name) {
45+
super(source, target, actions, event, TransitionKind.EXTERNAL, guard, trigger, securityRule, name);
46+
}
47+
3048
/**
3149
* Instantiates a new abstract external transition.
3250
*
@@ -41,7 +59,7 @@ public abstract class AbstractExternalTransition<S, E> extends AbstractTransitio
4159
public AbstractExternalTransition(State<S, E> source, State<S, E> target,
4260
Collection<Function<StateContext<S, E>, Mono<Void>>> actions, E event,
4361
Function<StateContext<S, E>, Mono<Boolean>> guard, Trigger<S, E> trigger, SecurityRule securityRule) {
44-
super(source, target, actions, event, TransitionKind.EXTERNAL, guard, trigger, securityRule);
62+
super(source, target, actions, event, TransitionKind.EXTERNAL, guard, trigger, securityRule, null);
4563
}
4664

4765
/**

spring-statemachine-core/src/main/java/org/springframework/statemachine/transition/AbstractInternalTransition.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2020 the original author or authors.
2+
* Copyright 2015-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,23 @@ public AbstractInternalTransition(State<S, E> source, Collection<Function<StateC
5454
public AbstractInternalTransition(State<S, E> source, Collection<Function<StateContext<S, E>, Mono<Void>>> actions,
5555
E event, Function<StateContext<S, E>, Mono<Boolean>> guard, Trigger<S, E> trigger,
5656
SecurityRule securityRule) {
57-
super(source, source, actions, event, TransitionKind.INTERNAL, guard, trigger, securityRule);
57+
super(source, source, actions, event, TransitionKind.INTERNAL, guard, trigger, securityRule, null);
58+
}
59+
60+
/**
61+
* Instantiates a new abstract internal transition.
62+
*
63+
* @param source the source
64+
* @param actions the actions
65+
* @param event the event
66+
* @param guard the guard
67+
* @param trigger the trigger
68+
* @param securityRule the security rule
69+
* @param name the name
70+
*/
71+
public AbstractInternalTransition(State<S, E> source, Collection<Function<StateContext<S, E>, Mono<Void>>> actions,
72+
E event, Function<StateContext<S, E>, Mono<Boolean>> guard, Trigger<S, E> trigger,
73+
SecurityRule securityRule, String name) {
74+
super(source, source, actions, event, TransitionKind.INTERNAL, guard, trigger, securityRule, name);
5875
}
5976
}

0 commit comments

Comments
 (0)