<commit_before>package alma.acs.nc.sm;

import static alma.acs.nc.sm.generated.EventSubscriberAction.ConnectionCreator;
import static alma.acs.nc.sm.generated.EventSubscriberAction.ConnectionDestructor;
import static alma.acs.nc.sm.generated.EventSubscriberAction.ConnectionResumer;
import static alma.acs.nc.sm.generated.EventSubscriberAction.ConnectionSuspender;
import static alma.acs.nc.sm.generated.EventSubscriberAction.EnvironmentCreator;
import static alma.acs.nc.sm.generated.EventSubscriberAction.EnvironmentDestructor;

import java.util.logging.Logger;

import alma.acs.nc.AcsEventSubscriberImplBase;
import alma.acs.nc.sm.generated.EventSubscriberAction;
import alma.acs.nc.sm.generated.EventSubscriberSignal;
import alma.acs.nc.sm.generated.EventSubscriberSignalDispatcher;
import alma.acs.nc.sm.generic.AcsScxmlActionDispatcher;
import alma.acs.nc.sm.generic.AcsScxmlEngine;

/**
 * Adapts {@link AcsScxmlEngine} for the concrete SM type 'EventSubscriber',
 * including the action handlers.
 * <p>
 * This code could also be embedded somewhere else, 
 * e.g. directly in component code {@link AcsEventSubscriberImplBase}.
 * 
 * @author hsommer
 */
public class EventSubscriberStateMachine extends EventSubscriberSignalDispatcher
{
	private static final String scxmlFileName = "/alma/acs/nc/sm/generated/EventSubscriberSCXML.xml";
	private final Logger logger;
	
	private final AcsScxmlEngine<EventSubscriberSignal, EventSubscriberAction> stateMachine;

	public EventSubscriberStateMachine(Logger logger) {
		this.logger = logger;
		
		AcsScxmlActionDispatcher<EventSubscriberAction> actionDispatcher = createActionDispatcher();
		
		// The AcsScxmlEngine constructor loads the scxml file and starts the state machine
		stateMachine = new AcsScxmlEngine<EventSubscriberSignal, EventSubscriberAction>(scxmlFileName, logger, actionDispatcher);
	}

	/**
	 * Sets up action handlers.
	 * p:
	 * TODO: Modify action ctors to require notify service objects
	 * 
	 * @return
	 */
	protected AcsScxmlActionDispatcher<EventSubscriberAction> createActionDispatcher() {
		AcsScxmlActionDispatcher<EventSubscriberAction> actionDispatcher = 
				new AcsScxmlActionDispatcher<EventSubscriberAction>(logger, EventSubscriberAction.class);
		
		EnvironmentActionHandler environmentActionHandler = new EnvironmentActionHandler(logger);
		actionDispatcher.registerActionHandler(EnvironmentCreator, environmentActionHandler);
		actionDispatcher.registerActionHandler(EnvironmentDestructor, environmentActionHandler);
		
		ConnectionActionHandler connectionActionHandler = new ConnectionActionHandler(logger);
		actionDispatcher.registerActionHandler(ConnectionCreator, connectionActionHandler);
		actionDispatcher.registerActionHandler(ConnectionDestructor, connectionActionHandler);
		
		SuspendResumeActionHandler suspendResumeActionHandler = new SuspendResumeActionHandler(logger);
		actionDispatcher.registerActionHandler(ConnectionSuspender, suspendResumeActionHandler);
		actionDispatcher.registerActionHandler(ConnectionResumer, suspendResumeActionHandler);
		return actionDispatcher;
	}

	@Override
	protected AcsScxmlEngine<EventSubscriberSignal, EventSubscriberAction> getScxmlEngine() {
		return stateMachine;
	}
	
	/**
	 * Convenience method, delegates to
	 */
	public String getCurrentState() {
		return stateMachine.getCurrentState();
	}
}<commit_msg>Action handlers must be injected, which is better for the NC subscriber case where the SM gets used by base and subclasses that also override the action methods.<commit_after>package alma.acs.nc.sm;

import static alma.acs.nc.sm.generated.EventSubscriberAction.ConnectionCreator;
import static alma.acs.nc.sm.generated.EventSubscriberAction.ConnectionDestructor;
import static alma.acs.nc.sm.generated.EventSubscriberAction.ConnectionResumer;
import static alma.acs.nc.sm.generated.EventSubscriberAction.ConnectionSuspender;
import static alma.acs.nc.sm.generated.EventSubscriberAction.EnvironmentCreator;
import static alma.acs.nc.sm.generated.EventSubscriberAction.EnvironmentDestructor;

import java.util.logging.Logger;

import alma.acs.nc.AcsEventSubscriberImplBase;
import alma.acs.nc.sm.generated.EventSubscriberAction;
import alma.acs.nc.sm.generated.EventSubscriberSignal;
import alma.acs.nc.sm.generated.EventSubscriberSignalDispatcher;
import alma.acs.nc.sm.generic.AcsScxmlActionDispatcher;
import alma.acs.nc.sm.generic.AcsScxmlEngine;

/**
 * Adapts {@link AcsScxmlEngine} for the concrete SM type 'EventSubscriber',
 * including the action handlers.
 * <p>
 * This code could also be embedded somewhere else, 
 * e.g. directly in component code {@link AcsEventSubscriberImplBase}.
 * 
 * @author hsommer
 */
public class EventSubscriberStateMachine extends EventSubscriberSignalDispatcher
{
	private static final String scxmlFileName = "/alma/acs/nc/sm/generated/EventSubscriberSCXML.xml";
	private final Logger logger;
	
	private final AcsScxmlEngine<EventSubscriberSignal, EventSubscriberAction> stateMachine;

	public EventSubscriberStateMachine(Logger logger, 
										EnvironmentActionHandler environmentActionHandler, 
										ConnectionActionHandler connectionActionHandler,
										SuspendResumeActionHandler suspendResumeActionHandler ) {
		this.logger = logger;
	
		AcsScxmlActionDispatcher<EventSubscriberAction> actionDispatcher = 
				new AcsScxmlActionDispatcher<EventSubscriberAction>(logger, EventSubscriberAction.class);
		
		actionDispatcher.registerActionHandler(EnvironmentCreator, environmentActionHandler);
		actionDispatcher.registerActionHandler(EnvironmentDestructor, environmentActionHandler);
		
		actionDispatcher.registerActionHandler(ConnectionCreator, connectionActionHandler);
		actionDispatcher.registerActionHandler(ConnectionDestructor, connectionActionHandler);
		
		actionDispatcher.registerActionHandler(ConnectionSuspender, suspendResumeActionHandler);
		actionDispatcher.registerActionHandler(ConnectionResumer, suspendResumeActionHandler);
		
		// The AcsScxmlEngine constructor loads the scxml file and starts the state machine
		stateMachine = new AcsScxmlEngine<EventSubscriberSignal, EventSubscriberAction>(
				scxmlFileName, logger, actionDispatcher, EventSubscriberSignal.class);
	}

	@Override
	protected AcsScxmlEngine<EventSubscriberSignal, EventSubscriberAction> getScxmlEngine() {
		return stateMachine;
	}
	
	/**
	 * Convenience method, delegates to the underlying AcsScxmlEngine.
	 * @return The state name. Hierarchical states are separated by ":", with outer state first, e.g. "EnvironmentCreated::Connected::Suspended"
	 */
	public String getCurrentState() {
		return stateMachine.getCurrentState();
	}
	
}
{'fraction_non_alphanumeric': 0.07450331125827815, 'fraction_numerical': 0.0, 'mean_word_length': 6.726744186046512, 'pattern_counts': {'":': 1, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 31}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3115103', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1741, 'n_words': 370}