Skip to content

Commit bcc516f

Browse files
hielsnoppeNiels Hoppe
and
Niels Hoppe
authored
Implement named transitions
- Fixes #850 Co-authored-by: Niels Hoppe <[email protected]>
1 parent 4b3c19d commit bcc516f

19 files changed

+267
-39
lines changed

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

Lines changed: 4 additions & 4 deletions
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.
@@ -886,7 +886,7 @@ private StateMachine<S, E> buildMachine(Map<Object, StateMachine<S, E>> machineM
886886
}
887887
DefaultExternalTransition<S, E> transition = new DefaultExternalTransition<S, E>(stateMap.get(source),
888888
stateMap.get(target), transitionData.getActions(), event, transitionData.getGuard(), trigger,
889-
transitionData.getSecurityRule());
889+
transitionData.getSecurityRule(), transitionData.getName());
890890
transitions.add(transition);
891891

892892
} else if (transitionData.getKind() == TransitionKind.LOCAL) {
@@ -896,12 +896,12 @@ private StateMachine<S, E> buildMachine(Map<Object, StateMachine<S, E>> machineM
896896
}
897897
DefaultLocalTransition<S, E> transition = new DefaultLocalTransition<S, E>(stateMap.get(source),
898898
stateMap.get(target), transitionData.getActions(), event, transitionData.getGuard(), trigger,
899-
transitionData.getSecurityRule());
899+
transitionData.getSecurityRule(), transitionData.getName());
900900
transitions.add(transition);
901901
} else if (transitionData.getKind() == TransitionKind.INTERNAL) {
902902
DefaultInternalTransition<S, E> transition = new DefaultInternalTransition<S, E>(stateMap.get(source),
903903
transitionData.getActions(), event, transitionData.getGuard(), trigger,
904-
transitionData.getSecurityRule());
904+
transitionData.getSecurityRule(), transitionData.getName());
905905
transitions.add(transition);
906906
}
907907
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 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,14 +176,14 @@ public HistoryTransitionConfigurer<S, E> withHistory() throws Exception {
176176
* @param securityRule the security rule
177177
*/
178178
public void addTransition(S source, S target, S state, E event, Long period, Integer count, Collection<Action<S, E>> actions,
179-
Guard<S, E> guard, TransitionKind kind, SecurityRule securityRule) {
179+
Guard<S, E> guard, TransitionKind kind, SecurityRule securityRule, String name) {
180180
// if rule not given, get it from global
181181
if (securityRule == null) {
182182
@SuppressWarnings("unchecked")
183183
ConfigurationData<S, E> config = getSharedObject(ConfigurationData.class);
184184
securityRule = config.getTransitionSecurityRule();
185185
}
186-
transitionData.add(new TransitionData<>(source, target, state, event, period, count, actions, guard, kind, securityRule));
186+
transitionData.add(new TransitionData<>(source, target, state, event, period, count, actions, guard, kind, securityRule, name));
187187
}
188188

189189
/**

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 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.
@@ -48,6 +48,7 @@ public abstract class AbstractTransitionConfigurer<S, E> extends
4848
private final Collection<Action<S, E>> actions = new ArrayList<>();
4949
private Guard<S, E> guard;
5050
private SecurityRule securityRule;
51+
private String name;
5152

5253
protected S getSource() {
5354
return source;
@@ -88,6 +89,10 @@ protected Guard<S, E> getGuard() {
8889
protected SecurityRule getSecurityRule() {
8990
return securityRule;
9091
}
92+
93+
protected String getName() {
94+
return name;
95+
}
9196

9297
protected void setSource(S source) {
9398
this.source = source;
@@ -142,5 +147,9 @@ protected void setSecurityRule(String expression) {
142147
}
143148
securityRule.setExpression(expression);
144149
}
150+
151+
protected void setName(String name) {
152+
this.name = name;
153+
}
145154

146155
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 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.
@@ -39,7 +39,7 @@ public class DefaultExternalTransitionConfigurer<S, E> extends AbstractTransitio
3939
@Override
4040
public void configure(StateMachineTransitionBuilder<S, E> builder) throws Exception {
4141
builder.addTransition(getSource(), getTarget(), getState(), getEvent(), getPeriod(), getCount(), getActions(), getGuard(), TransitionKind.EXTERNAL,
42-
getSecurityRule());
42+
getSecurityRule(), getName());
4343
}
4444

4545
@Override
@@ -116,4 +116,10 @@ public ExternalTransitionConfigurer<S, E> secured(String expression) {
116116
return this;
117117
}
118118

119+
@Override
120+
public ExternalTransitionConfigurer<S, E> name(String name) {
121+
setName(name);
122+
return this;
123+
}
124+
119125
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 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.
@@ -39,7 +39,7 @@ public class DefaultInternalTransitionConfigurer<S, E> extends AbstractTransitio
3939
@Override
4040
public void configure(StateMachineTransitionBuilder<S, E> builder) throws Exception {
4141
builder.addTransition(getSource(), getTarget(), getState(), getEvent(), getPeriod(), getCount(), getActions(), getGuard(), TransitionKind.INTERNAL,
42-
getSecurityRule());
42+
getSecurityRule(), getName());
4343
}
4444

4545
@Override
@@ -110,4 +110,10 @@ public InternalTransitionConfigurer<S, E> secured(String expression) {
110110
return this;
111111
}
112112

113+
@Override
114+
public InternalTransitionConfigurer<S, E> name(String name) {
115+
setName(name);
116+
return this;
117+
}
118+
113119
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 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.
@@ -38,7 +38,7 @@ public class DefaultLocalTransitionConfigurer<S, E> extends AbstractTransitionCo
3838
@Override
3939
public void configure(StateMachineTransitionBuilder<S, E> builder) throws Exception {
4040
builder.addTransition(getSource(), getTarget(), getState(), getEvent(), getPeriod(), getCount(), getActions(), getGuard(), TransitionKind.LOCAL,
41-
getSecurityRule());
41+
getSecurityRule(), getName());
4242
}
4343

4444
@Override
@@ -114,5 +114,11 @@ public LocalTransitionConfigurer<S, E> secured(String expression) {
114114
setSecurityRule(expression);
115115
return this;
116116
}
117+
118+
@Override
119+
public LocalTransitionConfigurer<S, E> name(String name) {
120+
setName(name);
121+
return this;
122+
}
117123

118124
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 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.
@@ -126,4 +126,13 @@ public interface TransitionConfigurer<T, S, E> extends
126126
*/
127127
T secured(String expression);
128128

129+
/**
130+
* Specify a name for this {@link Transition}.
131+
*
132+
* @param name the name
133+
* @return configurer for chaining
134+
* @param name
135+
* @return
136+
*/
137+
T name(String name);
129138
}

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

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 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.
@@ -39,6 +39,7 @@ public class TransitionData<S, E> {
3939
private final Guard<S, E> guard;
4040
private final TransitionKind kind;
4141
private final SecurityRule securityRule;
42+
private final String name;
4243

4344
/**
4445
* Instantiates a new transition data.
@@ -48,7 +49,7 @@ public class TransitionData<S, E> {
4849
* @param event the event
4950
*/
5051
public TransitionData(S source, S target, E event) {
51-
this(source, target, null, event, null, null, null, null, TransitionKind.EXTERNAL, null);
52+
this(source, target, null, event, null, null, null, null, TransitionKind.EXTERNAL, null, null);
5253
}
5354

5455
/**
@@ -63,7 +64,23 @@ public TransitionData(S source, S target, E event) {
6364
*/
6465
public TransitionData(S source, S target, E event, Collection<Action<S, E>> actions,
6566
Guard<S, E> guard, TransitionKind kind) {
66-
this(source, target, null, event, null, null, actions, guard, kind, null);
67+
this(source, target, null, event, null, null, actions, guard, kind, null, null);
68+
}
69+
70+
/**
71+
* Instantiates a new transition data.
72+
*
73+
* @param source the source
74+
* @param target the target
75+
* @param event the event
76+
* @param actions the actions
77+
* @param guard the guard
78+
* @param kind the kind
79+
* @param name the name
80+
*/
81+
public TransitionData(S source, S target, E event, Collection<Action<S, E>> actions,
82+
Guard<S, E> guard, TransitionKind kind, String name) {
83+
this(source, target, null, event, null, null, actions, guard, kind, null, name);
6784
}
6885

6986
/**
@@ -79,7 +96,41 @@ public TransitionData(S source, S target, E event, Collection<Action<S, E>> acti
7996
*/
8097
public TransitionData(S source, S target, Long period, Integer count, Collection<Action<S, E>> actions,
8198
Guard<S, E> guard, TransitionKind kind) {
82-
this(source, target, null, null, period, count, actions, guard, kind, null);
99+
this(source, target, null, null, period, count, actions, guard, kind, null, null);
100+
}
101+
102+
/**
103+
* Instantiates a new transition data.
104+
*
105+
* @param source the source
106+
* @param target the target
107+
* @param period the period
108+
* @param count the count
109+
* @param actions the actions
110+
* @param guard the guard
111+
* @param kind the kind
112+
* @param name the name
113+
*/
114+
public TransitionData(S source, S target, Long period, Integer count, Collection<Action<S, E>> actions,
115+
Guard<S, E> guard, TransitionKind kind, String name) {
116+
this(source, target, null, null, period, count, actions, guard, kind, null, name);
117+
}
118+
119+
/**
120+
* Instantiates a new transition data.
121+
*
122+
* @param source the source
123+
* @param target the target
124+
* @param period the period
125+
* @param count the count
126+
* @param actions the actions
127+
* @param guard the guard
128+
* @param kind the kind
129+
* @param securityRule the security rule
130+
*/
131+
public TransitionData(S source, S target, Long period, Integer count, Collection<Action<S, E>> actions,
132+
Guard<S, E> guard, TransitionKind kind, SecurityRule securityRule) {
133+
this(source, target, null, null, period, count, actions, guard, kind, securityRule, null);
83134
}
84135

85136
/**
@@ -95,9 +146,10 @@ public TransitionData(S source, S target, Long period, Integer count, Collection
95146
* @param guard the guard
96147
* @param kind the kind
97148
* @param securityRule the security rule
149+
* @param name the name
98150
*/
99151
public TransitionData(S source, S target, S state, E event, Long period, Integer count, Collection<Action<S, E>> actions,
100-
Guard<S, E> guard, TransitionKind kind, SecurityRule securityRule) {
152+
Guard<S, E> guard, TransitionKind kind, SecurityRule securityRule, String name) {
101153
this.source = source;
102154
this.target = target;
103155
this.state = state;
@@ -108,6 +160,7 @@ public TransitionData(S source, S target, S state, E event, Long period, Integer
108160
this.guard = guard;
109161
this.kind = kind;
110162
this.securityRule = securityRule;
163+
this.name = (name == null) ? "" : name;
111164
}
112165

113166
/**
@@ -199,4 +252,8 @@ public TransitionKind getKind() {
199252
public SecurityRule getSecurityRule() {
200253
return securityRule;
201254
}
255+
256+
public String getName() {
257+
return name;
258+
}
202259
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 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.
@@ -25,6 +25,23 @@
2525

2626
public abstract class AbstractExternalTransition<S, E> extends AbstractTransition<S, E> implements Transition<S, E> {
2727

28+
/**
29+
* Instantiates a new abstract external transition.
30+
*
31+
* @param source the source
32+
* @param target the target
33+
* @param actions the actions
34+
* @param event the event
35+
* @param guard the guard
36+
* @param trigger the trigger
37+
* @param securityRule the security rule
38+
* @param name the name
39+
*/
40+
public AbstractExternalTransition(State<S, E> source, State<S, E> target, Collection<Action<S, E>> actions,
41+
E event, Guard<S, E> guard, Trigger<S, E> trigger, SecurityRule securityRule, String name) {
42+
super(source, target, actions, event, TransitionKind.EXTERNAL, guard, trigger, securityRule, name);
43+
}
44+
2845
/**
2946
* Instantiates a new abstract external transition.
3047
*

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 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,4 +53,20 @@ public AbstractInternalTransition(State<S, E> source, Collection<Action<S, E>> a
5353
Trigger<S, E> trigger, SecurityRule securityRule) {
5454
super(source, source, actions, event, TransitionKind.INTERNAL, guard, trigger, securityRule);
5555
}
56+
57+
/**
58+
* Instantiates a new abstract internal transition.
59+
*
60+
* @param source the source
61+
* @param actions the actions
62+
* @param event the event
63+
* @param guard the guard
64+
* @param trigger the trigger
65+
* @param securityRule the security rule
66+
* @param name the name
67+
*/
68+
public AbstractInternalTransition(State<S, E> source, Collection<Action<S, E>> actions, E event, Guard<S, E> guard,
69+
Trigger<S, E> trigger, SecurityRule securityRule, String name) {
70+
super(source, source, actions, event, TransitionKind.INTERNAL, guard, trigger, securityRule, name);
71+
}
5672
}

0 commit comments

Comments
 (0)