Skip to content

Commit

Permalink
Merge pull request #73 from FIUS/feature/program-runner-refactor
Browse files Browse the repository at this point in the history
Refactor program runner to use CompletableFuture
  • Loading branch information
buehlefs authored Oct 4, 2019
2 parents e953a41 + ade2986 commit bed2734
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public interface EntityProgramRunner {
/**
* Check whether the given program can be run to the given entity.
* <p>
* First checks {@link #canRunProgram(String)} and then {@link EntityProgram#canRunOn(Entity)}.
* First checks {@link #canRunProgram(String)} and then {@link EntityProgram#canRunOn(Entity)}. If the entity was
* running a program also checks if that program finished succesfully.
* </p>
*
* @param program
Expand Down Expand Up @@ -86,6 +87,19 @@ public interface EntityProgramRunner {
*/
void run(String program, Entity entity);

/**
* Get the running program of a given entity.
*
* @param entity
* The entity to run the program on
* @return the information object for the running program
* @throws IllegalArgumentException
* if an argument is null
* @throws NoSuchElementException
* if no running program is found for the entity
*/
RunningProgramInfo getRunningProgramInfo(Entity entity);

/**
* Force stop all running entity programs.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @author Tim Neumann
*/
public enum EntityProgramState {
/** When the program has a program factory that creates new instances of this program. */
IS_FACTORY,
/** When the entity program is new and was not started yet. */
NEW,
/** When the entity program is currently running. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This source file is part of the FIUS ICGE project.
* For more information see github.com/FIUS/ICGE2
*
* Copyright (c) 2019 the ICGE project authors.
*
* This software is available under the MIT license.
* SPDX-License-Identifier: MIT
*/
package de.unistuttgart.informatik.fius.icge.simulation.entity.program;

import java.util.concurrent.CompletableFuture;


/**
* Info object holding a running program.
*
* @author Fabian Bühler
*/
public interface RunningProgramInfo {

/**
* @return the state of this object; cannot be null
*/
public EntityProgramState getState();

/**
* @return the future running the program
*/
public CompletableFuture<Void> getFuture();

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class EntityProgramRegistryEntry {
private final String name;
private final boolean single;
private final boolean isSingleInstance;
private final EntityProgram program;
private final Supplier<EntityProgram> programGenerator;

Expand All @@ -39,7 +39,7 @@ public class EntityProgramRegistryEntry {
public EntityProgramRegistryEntry(final String name, final EntityProgram program) {
if ((name == null) || (program == null)) throw new IllegalArgumentException("Argument cannot be null.");
this.name = name;
this.single = true;
this.isSingleInstance = true;
this.program = program;
this.programGenerator = null;
}
Expand All @@ -57,7 +57,7 @@ public EntityProgramRegistryEntry(final String name, final EntityProgram program
public EntityProgramRegistryEntry(final String name, final Supplier<EntityProgram> programGenerator) {
if ((name == null) || (programGenerator == null)) throw new IllegalArgumentException("Argument cannot be null.");
this.name = name;
this.single = false;
this.isSingleInstance = false;
this.program = null;
this.programGenerator = programGenerator;
}
Expand All @@ -69,6 +69,13 @@ public String getName() {
return this.name;
}

/**
* @return false iff the program has a factory to create new instances
*/
public boolean isSingle() {
return this.isSingleInstance;
}

/**
* Get the program instance of this info.
* <p>
Expand All @@ -78,25 +85,25 @@ public String getName() {
* @return the program instance
*/
public EntityProgram getProgram() {
if (this.single) return this.program;
if (this.isSingleInstance) return this.program;
final EntityProgram prog = this.programGenerator.get();
if (prog == null) throw new IllegalStateException("Program Generator returned null.");
return prog;
}

@Override
public int hashCode() {
if (this.single) return this.name.hashCode() + this.program.hashCode();
if (this.isSingleInstance) return this.name.hashCode() + this.program.hashCode();
return this.name.hashCode() + this.programGenerator.hashCode();
}

@Override
public boolean equals(final Object obj) {
if (!(obj instanceof EntityProgramRegistryEntry)) return false;
final EntityProgramRegistryEntry other = (EntityProgramRegistryEntry) obj;
if (this.single != other.single) return false;
if (this.isSingleInstance != other.isSingleInstance) return false;
if (!this.name.equals(other.name)) return false;
if (this.single) return this.program.equals(other.program);
if (this.isSingleInstance) return this.program.equals(other.program);
return this.programGenerator.equals(other.programGenerator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@
*/
package de.unistuttgart.informatik.fius.icge.simulation.internal.entity.program;

import java.util.concurrent.CompletableFuture;

import de.unistuttgart.informatik.fius.icge.simulation.entity.program.EntityProgram;
import de.unistuttgart.informatik.fius.icge.simulation.entity.program.EntityProgramState;
import de.unistuttgart.informatik.fius.icge.simulation.entity.program.RunningProgramInfo;


/**
* An object holding information about the execution of a entity program.
*
* @author Tim Neumann
*/
public class EntityProgramRunningInfo {
private EntityProgramState state;
private final EntityProgram program;
private Thread thread;
public class EntityProgramRunningInfo implements RunningProgramInfo {

private EntityProgramState state;
private final EntityProgram program;
private CompletableFuture<Void> future;

/**
* Initialize
Expand All @@ -38,9 +42,7 @@ public EntityProgramRunningInfo(final EntityProgram program) {
this.state = EntityProgramState.NEW;
}

/**
* @return the state of this object; cannot be null
*/
@Override
public EntityProgramState getState() {
return this.state;
}
Expand All @@ -67,23 +69,17 @@ public EntityProgram getProgram() {
return this.program;
}

/**
* Get the thread of this object
*
* @return the thread of this object; can be null
*/
public Thread getThread() {
return this.thread;
@Override
public CompletableFuture<Void> getFuture() {
return this.future;
}

/**
* Set the thread of this object
*
* @param thread
* the new thread; may be null
* @param future
* the future to set; may be null
*/
public void setThread(final Thread thread) {
this.thread = thread;
public void setFuture(CompletableFuture<Void> future) {
this.future = future;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,17 @@ public EntityProgram getEntityProgram(final String name) {
return this.programs.get(name).getProgram();
}

/**
* Get the full program entry.
*
* @param name
* the program name
* @return iff the program has a factory to create new instances
*/
public boolean checkIfProgramHasFactory(final String name) {
if (name == null) throw new IllegalArgumentException("An argument is null.");
if (!this.programs.containsKey(name)) throw new NoSuchElementException();
return !this.programs.get(name).isSingle();
}

}
Loading

0 comments on commit bed2734

Please sign in to comment.