Class HeadlessApplication<S extends org.eclipse.emf.ecore.EObject,M extends Model>
- All Implemented Interfaces:
IHeadlessApplication
Java API:
1. Submit a new task: submitTask(ShortId, String, int).
2. Gracefully stop a running task / prevent a submitted but not yet running
task from running: stopTask(ShortId).
3. Get task progress and status: getProgressAndStatus(),
hasTasksInProgress() .
4. Stop a single task or all tasks:
stopTask(ShortId),stopAll().
5. Remove a task from the list of known tasks:
deleteTask(ShortId).
For more details see IHeadlessApplication interface javadocs.
HTTP API is provided by an instance of the HeadlessHTTPServer class.
This class is designed to be agnostic of any concrete Model/Scenario implementations. Scenario file reading, model creation, and simulation result extraction logic are all passed as class constructor parameters.
A typical use case for this class is as follows:
new HeadlessApplication<>(
OpenpitDataTransform.transform::createLoader,
(theScenario, theSeed, engine) -> new Model(theScenario, engine, new Mapping(), true, theSeed),
OpenpitResult::new,
httpPort());
Here, a HeadlessApplication instance receives an object to read a scenario
file, a model creator, and a simulation result extractor. The last parameter
denotes the HTTP port number the application will listen.-
Constructor Summary
ConstructorsConstructorDescriptionHeadlessApplication(Function<String, com.amalgamasimulation.emf.excel.EMFExcelLoader<S>> scenarioLoaderSupplier, ModelCreator<S, M> modelCreator, Function<M, SimulationRunResult> resultGenerator, int httpPort) Creates a new instance of HeadlessApplication. -
Method Summary
Modifier and TypeMethodDescriptionbooleandeleteTask(ShortId runId) Delete a task.Return the current completed fraction andstatusof allknown tasks.Return the result of the task execution.booleanReport if there are any tasks currently in progress.voidstopAll()Gracefully stop/cancel all known tasks, if any.voidGracefully stop a started task, or cancel a submitted but not yet started task.booleansubmitTask(ShortId runId, String scenarioPath, int seed) Submit a new simulation task.booleantaskIsKnown(ShortId runId) Returns true if the application has a task with this runId.
-
Constructor Details
-
HeadlessApplication
public HeadlessApplication(Function<String, com.amalgamasimulation.emf.excel.EMFExcelLoader<S>> scenarioLoaderSupplier, ModelCreator<S, M> modelCreator, Function<M, SimulationRunResult> resultGenerator, int httpPort) Creates a new instance of HeadlessApplication. Use this constructor when creating a SimulationRunResult from the model object is expected to be fast (within seconds). Otherwise, consider using another constructor which allows reporting the progress of SimulationRunResult generation.- Parameters:
scenarioLoaderSupplier- function that accepts path to the scenario file (as String) and produces a EMFExcelLoader object to load the scenariomodelCreator- function that accepts a Scenario object and creates a Model objectresultGenerator- function that created SimulationRunResult from the Model object after the simulation has completedhttpPort- HTTP port that this headless application will listen to
-
-
Method Details
-
submitTask
Description copied from interface:IHeadlessApplicationSubmit a new simulation task. If there is a known task with the same runId, a new task does not get submitted, false is returned, no exception is thrown.- Specified by:
submitTaskin interfaceIHeadlessApplication- Parameters:
runId- unique id of the taskscenarioPath- full path to a local file that contains a scenario to be used for the taskseed- random seed (a task parameter)- Returns:
- true if there is no known task with the same runId; false otherwise
-
stopTask
Description copied from interface:IHeadlessApplicationGracefully stop a started task, or cancel a submitted but not yet started task.For a task whose state is already
final, nothing is done.If the task is not known, nothing is done, no exception is thrown.
This method is idempotent - it may be called multiple times for the same task id.
- Specified by:
stopTaskin interfaceIHeadlessApplication- Parameters:
runId- unique id of the task
-
stopAll
public void stopAll()Description copied from interface:IHeadlessApplicationGracefully stop/cancel all known tasks, if any. This is equivalent to callingIHeadlessApplication.stopTask(ShortId)for each known task.- Specified by:
stopAllin interfaceIHeadlessApplication
-
taskIsKnown
Description copied from interface:IHeadlessApplicationReturns true if the application has a task with this runId. The task may be in any state. A task is known since it issubmittedand until it isdeleted.- Specified by:
taskIsKnownin interfaceIHeadlessApplication
-
hasTasksInProgress
public boolean hasTasksInProgress()Description copied from interface:IHeadlessApplicationReport if there are any tasks currently in progress. This method is used during application shutdown to check that all tasks have been stopped.- Specified by:
hasTasksInProgressin interfaceIHeadlessApplication
-
getProgressAndStatus
Description copied from interface:IHeadlessApplicationReturn the current completed fraction andstatusof allknown tasks.- Specified by:
getProgressAndStatusin interfaceIHeadlessApplication
-
getResult
Description copied from interface:IHeadlessApplicationReturn the result of the task execution.- Specified by:
getResultin interfaceIHeadlessApplication- Parameters:
runId- task id- Returns:
- a non-null
Resultobject if the task is found by its id and is in afinal state; null otherwise
-
deleteTask
Description copied from interface:IHeadlessApplicationDelete a task.A deleted task is not included in the
IHeadlessApplication.getProgressAndStatus()result, nor can its result be fetched byIHeadlessApplication.getResult(ShortId).Only a
known taskfor which one of the following holds true may be deleted:
1. The task has been canceled, or
2. There were scenario loading errors, or
3. The task is in afinal state, or- Specified by:
deleteTaskin interfaceIHeadlessApplication- Parameters:
runId- task id- Returns:
- true if the task is in a correct state for deletion; false otherwise
-