All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentcs.kernel.KernelStateOnline Maven / Gradle / Ivy

There is a newer version: 6.1.2
Show newest version
// SPDX-FileCopyrightText: The openTCS Authors
// SPDX-License-Identifier: MIT
package org.opentcs.kernel;

import org.opentcs.kernel.persistence.ModelPersister;
import org.opentcs.kernel.workingset.PlantModelManager;

/**
 * The base class for the kernel's online states.
 */
public abstract class KernelStateOnline
    extends
      KernelState {

  /**
   * Whether to save the model when this state is terminated.
   */
  private final boolean saveModelOnTerminate;

  /**
   * Creates a new instance.
   *
   * @param globalSyncObject The kernel threads' global synchronization object.
   * @param plantModelManager The plant model manager to be used.
   * @param modelPersister The model persister to be used.
   * @param saveModelOnTerminate Whether to save the model when this state is terminated.
   */
  public KernelStateOnline(
      Object globalSyncObject,
      PlantModelManager plantModelManager,
      ModelPersister modelPersister,
      boolean saveModelOnTerminate
  ) {
    super(globalSyncObject, plantModelManager, modelPersister);
    this.saveModelOnTerminate = saveModelOnTerminate;
  }

  @Override
  public void terminate() {
    if (saveModelOnTerminate) {
      savePlantModel();
    }
  }

  private void savePlantModel()
      throws IllegalStateException {
    synchronized (getGlobalSyncObject()) {
      getModelPersister().saveModel(getPlantModelManager().createPlantModelCreationTO());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy