![JAR search and dependency download from the Maven repository](/logo.png)
com.github.dakusui.jcunit8.factorspace.fsm.FsmComposer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jcunit Show documentation
Show all versions of jcunit Show documentation
Automated combinatorial testing framework on top of JUnit
The newest version!
package com.github.dakusui.jcunit8.factorspace.fsm;
import com.github.dakusui.jcunit.core.tuples.Tuple;
import com.github.dakusui.jcunit.fsm.FiniteStateMachine;
import com.github.dakusui.jcunit.fsm.State;
import java.util.*;
import java.util.stream.IntStream;
import static com.github.dakusui.jcunit8.exceptions.TestDefinitionException.fsmDoesNotHaveRouteToSpecifiedState;
import static java.util.stream.Collectors.toList;
public class FsmComposer extends FsmTupleAccessor {
public FsmComposer(String name, FiniteStateMachine model, int scenarioLength) {
super(name, model, scenarioLength);
}
public Scenario composeValueFrom(Tuple tuple) {
Sequence main = composeScenarioFromTuple(tuple);
return new Scenario.Impl<>(
this.name,
composeScenarioToBringUpFsmTo(main.get(0).from),
main
);
}
Sequence composeScenarioToBringUpFsmTo(State destination) {
if (Objects.equals(this.model.initialState(), destination)) {
return new Sequence.Builder().build();
}
return new Sequence.Builder().addAll(
findRoute(
this.model.initialState(),
destination,
new LinkedList<>(),
new ArrayList<>(this.model.states())
)
).build();
}
private List> findRoute(State from, State to, List> work, List> notVisited) {
Optional> edgeOptional = allPossibleEdges(from::equals, sutAction -> true, to::equals).findFirst();
if (edgeOptional.isPresent()) {
work.add(edgeOptional.get());
return work;
} else {
for (State eachState : notVisited) {
Optional> cur = allPossibleEdges(from::equals, sutAction -> true, eachState::equals).findFirst();
if (cur.isPresent()) {
return findRoute(
eachState,
to,
new ArrayList>(work) {{
add(cur.get());
}},
new ArrayList>(notVisited) {{
remove(eachState);
}});
}
}
}
throw fsmDoesNotHaveRouteToSpecifiedState(to, this.name, this.model);
}
private Sequence composeScenarioFromTuple(Tuple tuple) {
return new Sequence.Builder()
.addAll(
IntStream.range(0, scenarioLength)
.mapToObj(i -> Edge.Builder.from(getStateFromTuple(tuple, i))
.with(getActionFromTuple(tuple, i), getActionArgsFromTuple(tuple, i))
.to(getStateFromTuple(tuple, i)
.expectation(
getActionFromTuple(tuple, i),
getActionArgsFromTuple(tuple, i)
).state)
.build())
.collect(toList())
)
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy