Add repository config actions

- Add action to RepositoryTransition, state/entry/exit
  actions to RepositoryState.
- New ActionsRepository.
- Relates to #262
This commit is contained in:
Janne Valkealahti
2016-10-02 14:42:12 +01:00
parent de556f0d54
commit 0b6f67ece4
12 changed files with 466 additions and 13 deletions

View File

@@ -2,17 +2,35 @@
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
"initial": true,
"state": "S1"
"state": "S1",
"exitActions": [
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryAction",
"spel": "T(System).out.println('hello exit S1')"
}
]
},
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
"initial": false,
"state": "S2"
"state": "S2",
"entryActions": [
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryAction",
"spel": "T(System).out.println('hello entry S2')"
}
]
},
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
"initial": false,
"state": "S3"
"state": "S3",
"stateActions": [
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryAction",
"spel": "T(System).out.println('hello state S3')"
}
]
},
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
@@ -24,6 +42,12 @@
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
"source": "S2",
"target": "S3",
"event": "E2"
"event": "E2",
"actions": [
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryAction",
"spel": "T(System).out.println('hello')"
}
]
}
]

View File

@@ -0,0 +1,12 @@
package org.springframework.statemachine.data.jpa;
import org.springframework.statemachine.data.ActionRepository;
/**
* A {@link ActionRepository} interface for JPA used for actions.
*
* @author Janne Valkealahti
*
*/
public interface JpaActionRepository extends ActionRepository<JpaRepositoryAction> {
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.statemachine.data.jpa;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.springframework.statemachine.data.RepositoryAction;
/**
* JPA entity for actions.
*
* @author Janne Valkealahti
*
*/
@Entity
public class JpaRepositoryAction implements RepositoryAction {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String spel;
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getSpel() {
return spel;
}
public void setSpel(String spel) {
this.spel = spel;
}
}

View File

@@ -15,10 +15,15 @@
*/
package org.springframework.statemachine.data.jpa;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.springframework.statemachine.data.RepositoryState;
@@ -40,6 +45,15 @@ public class JpaRepositoryState implements RepositoryState {
private String state;
private boolean initial;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<JpaRepositoryAction> stateActions;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<JpaRepositoryAction> entryActions;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<JpaRepositoryAction> exitActions;
/**
* Instantiates a new jpa repository state.
*/
@@ -85,10 +99,29 @@ public class JpaRepositoryState implements RepositoryState {
* @param initial the initial
*/
public JpaRepositoryState(String machineId, String parentState, String state, boolean initial) {
this(machineId, parentState, state, initial, null, null, null);
}
/**
* Instantiates a new jpa repository state.
*
* @param machineId the machine id
* @param parentState the parent state
* @param state the state
* @param initial the initial
* @param stateActions the state actions
* @param entryActions the entry actions
* @param exitActions the exit actions
*/
public JpaRepositoryState(String machineId, String parentState, String state, boolean initial, Set<JpaRepositoryAction> stateActions,
Set<JpaRepositoryAction> entryActions, Set<JpaRepositoryAction> exitActions) {
this.machineId = machineId;
this.parentState = parentState;
this.state = state;
this.initial = initial;
this.stateActions = stateActions;
this.entryActions = entryActions;
this.exitActions = exitActions;
}
@Override
@@ -126,4 +159,31 @@ public class JpaRepositoryState implements RepositoryState {
public void setInitial(boolean initial) {
this.initial = initial;
}
@Override
public Set<JpaRepositoryAction> getStateActions() {
return stateActions;
}
public void setStateActions(Set<JpaRepositoryAction> stateActions) {
this.stateActions = stateActions;
}
@Override
public Set<JpaRepositoryAction> getEntryActions() {
return entryActions;
}
public void setEntryActions(Set<JpaRepositoryAction> entryActions) {
this.entryActions = entryActions;
}
@Override
public Set<JpaRepositoryAction> getExitActions() {
return exitActions;
}
public void setExitActions(Set<JpaRepositoryAction> exitActions) {
this.exitActions = exitActions;
}
}

View File

@@ -15,10 +15,15 @@
*/
package org.springframework.statemachine.data.jpa;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.springframework.statemachine.data.RepositoryTransition;
@@ -40,6 +45,9 @@ public class JpaRepositoryTransition implements RepositoryTransition {
private String target;
private String event;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<JpaRepositoryAction> actions;
/**
* Instantiates a new jpa repository transition.
*/
@@ -67,10 +75,24 @@ public class JpaRepositoryTransition implements RepositoryTransition {
* @param event the event
*/
public JpaRepositoryTransition(String machineId, String source, String target, String event) {
this(machineId, source, target, event, null);
}
/**
* Instantiates a new jpa repository transition.
*
* @param machineId the machine id
* @param source the source
* @param target the target
* @param event the event
* @param actions the actions
*/
public JpaRepositoryTransition(String machineId, String source, String target, String event, Set<JpaRepositoryAction> actions) {
this.machineId = machineId;
this.source = source;
this.target = target;
this.event = event;
this.actions = actions;
}
@Override
@@ -108,4 +130,13 @@ public class JpaRepositoryTransition implements RepositoryTransition {
public void setEvent(String event) {
this.event = event;
}
@Override
public Set<JpaRepositoryAction> getActions() {
return actions;
}
public void setActions(Set<JpaRepositoryAction> actions) {
this.actions = actions;
}
}

View File

@@ -18,7 +18,10 @@ package org.springframework.statemachine.data.jpa;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -133,6 +136,53 @@ public class JpaRepositoryTests extends AbstractJpaRepositoryTests {
context.close();
}
@Test
public void testRepository4() {
context.register(Config.class);
context.refresh();
JpaActionRepository actionsRepository = context.getBean(JpaActionRepository.class);
JpaRepositoryAction action1 = new JpaRepositoryAction();
action1.setSpel("spel1");
action1.setName("action1");
actionsRepository.save(action1);
assertThat(actionsRepository.count(), is(1l));
JpaRepositoryAction action11 = actionsRepository.findAll().iterator().next();
assertThat(action1.getSpel(), is(action11.getSpel()));
assertThat(action1.getName(), is(action11.getName()));
}
@Test
public void testRepository5() {
context.register(Config.class);
context.refresh();
JpaActionRepository actionsRepository = context.getBean(JpaActionRepository.class);
JpaTransitionRepository transitionsRepository = context.getBean(JpaTransitionRepository.class);
JpaRepositoryTransition transition = new JpaRepositoryTransition("S1", "S2", "E1");
JpaRepositoryAction action1 = new JpaRepositoryAction();
action1.setName("action1");
Set<JpaRepositoryAction> actions = new HashSet<>(Arrays.asList(action1));
transition.setActions(actions);
transitionsRepository.save(transition);
JpaRepositoryTransition transition2 = transitionsRepository.findAll().iterator().next();
assertThat(transition2.getSource(), is("S1"));
assertThat(transition2.getTarget(), is("S2"));
assertThat(transition2.getEvent(), is("E1"));
assertThat(actionsRepository.count(), is(1l));
JpaRepositoryAction action11 = actionsRepository.findAll().iterator().next();
assertThat(action1.getName(), is(action11.getName()));
assertThat(transition2.getActions().size(), is(1));
}
@SuppressWarnings("unchecked")
@Test
public void testMachine2() throws Exception {

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.statemachine.data;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
/**
* Generic {@link Repository} interface for actions.
*
* @author Janne Valkealahti
*
* @param <T> the transition entity type
*/
@NoRepositoryBean
public interface ActionRepository <T extends RepositoryAction> extends CrudRepository<T, Long> {
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.statemachine.data;
/**
* Generic interface representing action entity.
*
* @author Janne Valkealahti
*
*/
public interface RepositoryAction {
/**
* Gets the name.
*
* @return the name
*/
String getName();
/**
* Gets the spel.
*
* @return the spel
*/
String getSpel();
}

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.statemachine.data;
import java.util.Set;
/**
* Generic interface representing state entity.
*
@@ -50,4 +52,25 @@ public interface RepositoryState {
* @return true, if is initial
*/
boolean isInitial();
/**
* Gets the state actions.
*
* @return the state actions
*/
Set<? extends RepositoryAction> getStateActions();
/**
* Gets the entry actions.
*
* @return the entry actions
*/
Set<? extends RepositoryAction> getEntryActions();
/**
* Gets the exit actions.
*
* @return the exit actions
*/
Set<? extends RepositoryAction> getExitActions();
}

View File

@@ -17,7 +17,14 @@ package org.springframework.statemachine.data;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.action.SpelExpressionAction;
import org.springframework.statemachine.config.model.AbstractStateMachineModelFactory;
import org.springframework.statemachine.config.model.ConfigurationData;
import org.springframework.statemachine.config.model.DefaultStateMachineModel;
import org.springframework.statemachine.config.model.StateData;
@@ -26,6 +33,8 @@ import org.springframework.statemachine.config.model.StateMachineModelFactory;
import org.springframework.statemachine.config.model.StatesData;
import org.springframework.statemachine.config.model.TransitionData;
import org.springframework.statemachine.config.model.TransitionsData;
import org.springframework.statemachine.transition.TransitionKind;
import org.springframework.util.StringUtils;
/**
* A generic {@link StateMachineModelFactory} which is backed by a Spring Data
@@ -34,7 +43,8 @@ import org.springframework.statemachine.config.model.TransitionsData;
* @author Janne Valkealahti
*
*/
public class RepositoryStateMachineModelFactory implements StateMachineModelFactory<String, String> {
public class RepositoryStateMachineModelFactory extends AbstractStateMachineModelFactory<String, String>
implements StateMachineModelFactory<String, String> {
private final StateRepository<? extends RepositoryState> stateRepository;
private final TransitionRepository<? extends RepositoryTransition> transitionRepository;
@@ -60,15 +70,97 @@ public class RepositoryStateMachineModelFactory implements StateMachineModelFact
public StateMachineModel<String, String> build(String machineId) {
ConfigurationData<String, String> configurationData = new ConfigurationData<>();
Collection<StateData<String, String>> stateData = new ArrayList<>();
Collection<StateData<String, String>> stateDatas = new ArrayList<>();
for (RepositoryState s : stateRepository.findByMachineId(machineId)) {
stateData.add(new StateData<String, String>(s.getParentState(), null, s.getState(), s.isInitial()));
Collection<Action<String, String>> stateActions = new ArrayList<Action<String, String>>();
Set<? extends RepositoryAction> repositoryStateActions = s.getStateActions();
if (repositoryStateActions != null) {
for (RepositoryAction repositoryAction : repositoryStateActions) {
Action<String, String> action = null;
if (StringUtils.hasText(repositoryAction.getName())) {
action = resolveAction(repositoryAction.getName());
} else if (StringUtils.hasText(repositoryAction.getSpel())) {
SpelExpressionParser parser = new SpelExpressionParser(
new SpelParserConfiguration(SpelCompilerMode.MIXED, null));
action = new SpelExpressionAction<String, String>(parser.parseExpression(repositoryAction.getSpel()));
}
if (action != null) {
stateActions.add(action);
}
}
}
Collection<Action<String, String>> entryActions = new ArrayList<Action<String, String>>();
Set<? extends RepositoryAction> repositoryEntryActions = s.getEntryActions();
if (repositoryEntryActions != null) {
for (RepositoryAction repositoryAction : repositoryEntryActions) {
Action<String, String> action = null;
if (StringUtils.hasText(repositoryAction.getName())) {
action = resolveAction(repositoryAction.getName());
} else if (StringUtils.hasText(repositoryAction.getSpel())) {
SpelExpressionParser parser = new SpelExpressionParser(
new SpelParserConfiguration(SpelCompilerMode.MIXED, null));
action = new SpelExpressionAction<String, String>(parser.parseExpression(repositoryAction.getSpel()));
}
if (action != null) {
stateActions.add(action);
}
}
}
Collection<Action<String, String>> exitActions = new ArrayList<Action<String, String>>();
Set<? extends RepositoryAction> repositoryExitActions = s.getExitActions();
if (repositoryExitActions != null) {
for (RepositoryAction repositoryAction : repositoryExitActions) {
Action<String, String> action = null;
if (StringUtils.hasText(repositoryAction.getName())) {
action = resolveAction(repositoryAction.getName());
} else if (StringUtils.hasText(repositoryAction.getSpel())) {
SpelExpressionParser parser = new SpelExpressionParser(
new SpelParserConfiguration(SpelCompilerMode.MIXED, null));
action = new SpelExpressionAction<String, String>(parser.parseExpression(repositoryAction.getSpel()));
}
if (action != null) {
stateActions.add(action);
}
}
}
StateData<String,String> stateData = new StateData<String, String>(s.getParentState(), null, s.getState(), s.isInitial());
stateData.setStateActions(stateActions);
stateData.setEntryActions(entryActions);
stateData.setExitActions(exitActions);
stateDatas.add(stateData);
}
StatesData<String, String> statesData = new StatesData<>(stateData);
StatesData<String, String> statesData = new StatesData<>(stateDatas);
Collection<TransitionData<String, String>> transitionData = new ArrayList<>();
for (RepositoryTransition t : transitionRepository.findByMachineId(machineId)) {
transitionData.add(new TransitionData<String, String>(t.getSource(), t.getTarget(), t.getEvent()));
Collection<Action<String, String>> actions = new ArrayList<Action<String, String>>();
Set<? extends RepositoryAction> repositoryActions = t.getActions();
if (repositoryActions != null) {
for (RepositoryAction repositoryAction : repositoryActions) {
Action<String, String> action = null;
if (StringUtils.hasText(repositoryAction.getName())) {
action = resolveAction(repositoryAction.getName());
} else if (StringUtils.hasText(repositoryAction.getSpel())) {
SpelExpressionParser parser = new SpelExpressionParser(
new SpelParserConfiguration(SpelCompilerMode.MIXED, null));
action = new SpelExpressionAction<String, String>(parser.parseExpression(repositoryAction.getSpel()));
}
if (action != null) {
actions.add(action);
}
}
}
transitionData.add(new TransitionData<>(t.getSource(), t.getTarget(), t.getEvent(), actions, null, TransitionKind.EXTERNAL));
}
TransitionsData<String, String> transitionsData = new TransitionsData<>(transitionData);

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.statemachine.data;
import java.util.Set;
/**
* Generic interface representing transition entity.
*
@@ -50,4 +52,11 @@ public interface RepositoryTransition {
* @return the event
*/
String getEvent();
/**
* Gets the actions.
*
* @return the actions
*/
Set<? extends RepositoryAction> getActions();
}

View File

@@ -2,17 +2,35 @@
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
"initial": true,
"state": "S1"
"state": "S1",
"exitActions": [
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryAction",
"spel": "T(System).out.println('hello exit S1')"
}
]
},
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
"initial": false,
"state": "S2"
"state": "S2",
"entryActions": [
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryAction",
"spel": "T(System).out.println('hello entry S2')"
}
]
},
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
"initial": false,
"state": "S3"
"state": "S3",
"stateActions": [
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryAction",
"spel": "T(System).out.println('hello state S3')"
}
]
},
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
@@ -24,6 +42,12 @@
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
"source": "S2",
"target": "S3",
"event": "E2"
"event": "E2",
"actions": [
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryAction",
"spel": "T(System).out.println('hello')"
}
]
}
]