Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code enhances #7

Merged
merged 8 commits into from
Aug 15, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
*
* @author pavl_g.
*/
public final class SerialAdder extends Thread implements TransitionListener {
public final class SerialAdder extends Thread implements TransitionListener<BitsAdder, Integer> {

private final TransitionalManager transitionalManager = new TransitionalManager();
private final TransitionalManager<BitsAdder, Integer> transitionalManager = new TransitionalManager<>();
private final List<BitsAdder> adders = new ArrayList<>();
private final List<AutoState<BitsAdder, Integer>> states = new ArrayList<>();

Expand Down Expand Up @@ -117,18 +117,6 @@ public void run() {
Logger.getLogger(getName()).info("Finite state finishes !");
}

@Override
@SuppressWarnings("unchecked")
public <I, O> void onTransition(AutoState<I, O> presentState) {
/* assigns the state on transition */
final AutoState<BitsAdder, Integer> autoState = (AutoState<BitsAdder, Integer>) presentState;
if (!hasNextCarryState(autoState)) {
transitionalManager.assignNextState(states.get(0));
} else {
transitionalManager.assignNextState(states.get(1));
}
}

/**
* Tests whether the current state has a next {@link CarryState}.
*
Expand All @@ -138,4 +126,14 @@ public <I, O> void onTransition(AutoState<I, O> presentState) {
private boolean hasNextCarryState(AutoState<BitsAdder, Integer> state) {
return state.getStateTracer() == 1;
}

@Override
public void onTransition(AutoState<BitsAdder, Integer> presentState) {
/* assigns the state on transition */
if (!hasNextCarryState(presentState)) {
transitionalManager.assignNextState(states.get(0));
} else {
transitionalManager.assignNextState(states.get(1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@
import com.avrsandbox.fsa.core.TransitionalManager;
import com.avrsandbox.fsa.core.deterministic.DeterministicManager;
import com.avrsandbox.fsa.core.state.AutoState;
import com.avrsandbox.fsa.core.state.TransitionListener;
import com.avrsandbox.fsa.util.AutomataLogger;
import com.avrsandbox.fsa.util.TransitionPath;
import com.avrsandbox.fsa.core.TransitionPath;

/**
* Tests the Deterministic Finite-State-Automaton Pattern through using {@link DeterministicManager}
Expand All @@ -53,19 +52,16 @@ public static void main(String[] args) {
final ArmatureState walkingState = new ArmatureState();
walkingState.setInput("Walking");

final TransitionPath<AutoState<String, String>> transitionPath =
final TransitionPath<String, String> transitionPath =
new TransitionPath<>("Armature-Mover-Map");
transitionPath.assignPresentState(idleState);
transitionPath.assignNextState(walkingState);

final TransitionalManager transitionalManager = new DeterministicManager();
final TransitionalManager<String, String> transitionalManager = new DeterministicManager<>();
/* repeat the transition path to assert the TransitionPathNotUniqueException */
transitionalManager.transit(transitionPath, new TransitionListener() {
@Override
public <I, O> void onTransition(AutoState<I, O> presentState) {
transitionalManager.transit(null);
transitionalManager.transit(transitionPath, null);
}
transitionalManager.transit(transitionPath, presentState -> {
transitionalManager.transit(null);
transitionalManager.transit(transitionPath, null);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2023, The AvrSandbox Project, Automata4j
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.avrsandbox.fsa.core;

import com.avrsandbox.fsa.core.state.AutoState;
import com.avrsandbox.fsa.core.state.TransitionListener;

/**
* Assigns a next state {@link AutoState} when the
* transition from a present state from a transition path finishes.
*
* @param <I> the input type
* @param <O> the tracer object (output) type
* @author pavl_g
*/
class NextStateAssigner<I, O> implements TransitionListener<I, O> {

private final TransitionalManager<I, O> transitionalManager;
private final TransitionPath<I, O> transitionPath;
private final TransitionListener<I, O> delegator;

public NextStateAssigner(TransitionalManager<I, O> transitionalManager,
TransitionPath<I, O> transitionPath,
TransitionListener<I, O> delegator) {
this.transitionalManager = transitionalManager;
this.transitionPath = transitionPath;
this.delegator = delegator;
}

@Override
public void onTransition(AutoState<I, O> presentState) {
assert (transitionalManager != null);
assert (transitionPath != null);

transitionalManager.assignNextState(transitionPath.getNextState());
/* incremental dispatch */
if (delegator != null) {
delegator.onTransition(presentState);
}
}
}
19 changes: 11 additions & 8 deletions automata4j/src/main/java/com/avrsandbox/fsa/core/Transition.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
/**
* Represents a machine system transition with one next-state {@link Transition#nextState}.
*
* @param <S> a type of {@link AutoState}
* @param <I> a class-generic representing the input value type
* @param <O> a class-generic representing the tracer object (output) value type
* @author pavl_g
*/
@SuppressWarnings("rawtypes")
public final class Transition<S extends AutoState> {

private S nextState;
public class Transition<I, O> {

/**
* The state of the transition.
*/
protected AutoState<I, O> nextState;

/**
* Instantiates a transition with an empty next-state.
Expand All @@ -56,7 +59,7 @@ public Transition() {
*
* @param nextState the next-state object to assign
*/
public Transition(S nextState) {
public Transition(AutoState<I, O> nextState) {
this.nextState = nextState;
}

Expand All @@ -67,7 +70,7 @@ public Transition(S nextState) {
* @param nextState the next-state object to assign
* @throws NextStateNotFoundException thrown if the next-state is null
*/
public void setNextState(S nextState) {
public void assignNextState(AutoState<I, O> nextState) {
/* a business exception if there is no next-state assigned */
if (nextState == null) {
throw new NextStateNotFoundException();
Expand All @@ -80,7 +83,7 @@ public void setNextState(S nextState) {
*
* @return the next state of the transition system
*/
public S getNextState() throws NextStateNotFoundException {
public AutoState<I, O> getNextState() throws NextStateNotFoundException {
return nextState;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,35 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.avrsandbox.fsa.util;
package com.avrsandbox.fsa.core;

import com.avrsandbox.fsa.core.deterministic.DeterministicManager;
import com.avrsandbox.fsa.core.state.AutoState;

/**
* Provides a transition path composed of two states, a present-state and a next-state.
*
* @param <S> a type of {@link AutoState}
* @param <I> a class-generic representing the input value type
* @param <O> a class-generic representing the tracer object (output) value type
* @author pavl_g
* @see DeterministicManager#transit(TransitionPath, com.avrsandbox.fsa.core.state.TransitionListener)
*/
@SuppressWarnings("rawtypes")
public final class TransitionPath<S extends AutoState> {
public class TransitionPath<I, O> extends Transition<I, O> {

private String name;
private S presentState;
private S nextState;
/**
* The name of this path.
*/
protected String name;

/**
* The starting auto state.
*/
protected AutoState<I, O> presentState;

/**
* The next state (accepting/terminating) in this path.
*/
protected AutoState<I, O> nextState;

/**
* Instantiates a new transition path with empty states.
Expand All @@ -64,45 +75,27 @@ public TransitionPath(String name) {
* @param presentState a present-state to transit to
* @param nextState a next-state to assign for the next transition
*/
public TransitionPath(String name, S presentState, S nextState) {
public TransitionPath(String name, AutoState<I, O> presentState, AutoState<I, O> nextState) {
super(nextState);
this.name = name;
this.presentState = presentState;
this.nextState = nextState;
}

/**
* Assigns the present state to this transition path object.
*
* @param presentState the present state object
*/
public void assignPresentState(S presentState) {
public void assignPresentState(AutoState<I, O> presentState) {
this.presentState = presentState;
}

/**
* Assigns the next state to this transition path object.
*
* @param nextState the next state object
*/
public void assignNextState(S nextState) {
this.nextState = nextState;
}

/**
* Retains the next-state object from the heap memory.
*
* @return the next-state object
*/
public S getNextState() {
return nextState;
}

/**
* Retains the present-state object from the heap memory.
*
* @return the present-state object
*/
public S getPresentState() {
public AutoState<I, O> getPresentState() {
return presentState;
}

Expand Down Expand Up @@ -150,4 +143,12 @@ public void removeAll() {
removePresentState();
removeNextState();
}

/**
* @deprecated use {@link TransitionPath#removeNextState()}.
*/
@Override
@Deprecated
public void remove() {
}
}
Loading