Skip to content

Commit

Permalink
Merge pull request #16 from FIUS/feature/entityProgram
Browse files Browse the repository at this point in the history
Add entity programs and necessary stuff.
  • Loading branch information
neumantm authored Sep 18, 2019
2 parents 899a296 + 3f31193 commit 6a8edf0
Show file tree
Hide file tree
Showing 20 changed files with 980 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
package de.unistuttgart.informatik.fius.icge.simulation;

import de.unistuttgart.informatik.fius.icge.simulation.entity.program.EntityProgramRegistry;
import de.unistuttgart.informatik.fius.icge.simulation.entity.program.EntityProgramRunner;
import de.unistuttgart.informatik.fius.icge.ui.UiManager;


Expand Down Expand Up @@ -39,6 +41,20 @@ public interface Simulation {
*/
SimulationClock getSimulationClock();

/**
* Get the entity program registry for this simulation.
*
* @return the entity program registry used by this simulation
*/
EntityProgramRegistry getEntityProgramRegistry();

/**
* Get the entity program runner for this simulation.
*
* @return the entity program runner used by this simulation
*/
EntityProgramRunner getEntityProgramRunner();

/**
* Initialize the simulation and all its submodules.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public interface SimulationClock {
* @param tick
* The absolute number of the tick at which the operation will be run
* @param endOfOperation
* Tick processing will be halted until this future is completed
* Tick processing will be halted until this future is completed; must not complete exceptionally
* @throws IllegalStateException
* if the end of operation completes exceptionally
*/
void scheduleOperationAtTick(long tick, CompletableFuture<Void> endOfOperation);

Expand All @@ -117,7 +119,9 @@ public interface SimulationClock {
* @param ticks
* The number of ticks until the tick, for which to schedule the operation
* @param endOfOperation
* Tick processing will be halted until this future is completed
* Tick processing will be halted until this future is completed; must not complete exceptionally
* @throws IllegalStateException
* if the end of operation completes exceptionally
*/
void scheduleOperationInTicks(long ticks, CompletableFuture<Void> endOfOperation);

Expand All @@ -126,7 +130,9 @@ public interface SimulationClock {
* processing will halt until the given end of operation is completed.
*
* @param endOfOperation
* Tick processing will be halted until this future is completed
* Tick processing will be halted until this future is completed; must not complete exceptionally
* @throws IllegalStateException
* if the end of operation completes exceptionally
*/
void scheduleOperationAtNextTick(CompletableFuture<Void> endOfOperation);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import de.unistuttgart.informatik.fius.icge.simulation.internal.StandardSimulation;
import de.unistuttgart.informatik.fius.icge.simulation.internal.StandardSimulationClock;
import de.unistuttgart.informatik.fius.icge.simulation.internal.entity.program.StandardEntityProgramRegistry;
import de.unistuttgart.informatik.fius.icge.simulation.internal.entity.program.StandardEntityProgramRunner;
import de.unistuttgart.informatik.fius.icge.simulation.internal.playfield.StandardPlayfield;
import de.unistuttgart.informatik.fius.icge.ui.UiManager;
import de.unistuttgart.informatik.fius.icge.ui.UiManagerFactory;
Expand All @@ -32,7 +34,9 @@ public static Simulation createSimulation() {

StandardPlayfield playfield = new StandardPlayfield();
StandardSimulationClock tickManager = new StandardSimulationClock();
StandardEntityProgramRegistry entityProgramRegistry = new StandardEntityProgramRegistry();
StandardEntityProgramRunner entityProgramRunner = new StandardEntityProgramRunner(entityProgramRegistry);

return new StandardSimulation(uiManager, playfield, tickManager);
return new StandardSimulation(uiManager, playfield, tickManager, entityProgramRegistry, entityProgramRunner);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 de.unistuttgart.informatik.fius.icge.simulation.entity.Entity;


/**
* A program to be executed by an entity
*
* @author Tim Neumann
*/
public interface EntityProgram {

/**
* Run this program on the given entity.
*
* @param entity
* The entity to run this program on
* @throws IllegalArgumentException
* if this program cannot run on the given entity
*/
void run(Entity entity);

/**
* Check whether this program could run on the given entity.
*
* @param entity
* The entity to check
* @return true if this program could run on that entity
*/
boolean canRunOn(Entity entity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.NoSuchElementException;
import java.util.Set;
import java.util.function.Supplier;

import de.unistuttgart.informatik.fius.icge.simulation.entity.Entity;
import de.unistuttgart.informatik.fius.icge.simulation.exception.ElementExistsException;


/**
* The registry for all {@link EntityProgram}s.
*
* @author Tim Neumann
*/
public interface EntityProgramRegistry {
/**
* Register an entity program with the given name.
*
* @param name
* the name of the program; must not be null; must be unique
*
* @param program
* the program to register; must not be null
*
* @throws IllegalArgumentException
* if an argument is null
* @throws ElementExistsException
* if the name is already used
*/
void registerEntityProgram(String name, EntityProgram program);

/**
* Register many entity programs of the same type with the given name.
*
* @param name
* the name for these programs; must not be null; must be unique
*
* @param programGenerator
* a generator for the type of program; must not be null; must provide non null programs
*
* @throws IllegalArgumentException
* if an argument is null
*
* @throws ElementExistsException
* if the name is already used
*/
void registerManyEntityProgram(String name, Supplier<EntityProgram> programGenerator);

/**
* Get the names of all registered entity programs.
*
* @return a set of the names of all registered entity programs
*/
Set<String> getPrograms();

/**
* Get the names of all entity programs, which could run on the given entity.
*
* @param entity
* the entity to get the programs for; must not be null
* @return a set of the names of all entity programs which could be run on this entity
*
* @throws IllegalArgumentException
* if an argument is null
*/
Set<String> getProgramsForEntity(Entity entity);

/**
* Get the entity program for a given name.
*
* @param name
* The name to get the program for; must not be null
* @return The program for the given name
* @throws IllegalArgumentException
* if an argument is null
* @throws NoSuchElementException
* if the given name is not registered
*/
EntityProgram getEntityProgram(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.NoSuchElementException;

import de.unistuttgart.informatik.fius.icge.simulation.entity.Entity;
import de.unistuttgart.informatik.fius.icge.simulation.exception.CannotRunProgramException;


/**
* A class to run {@link EntityProgram}s.
*
* @author Tim Neumann
*/
public interface EntityProgramRunner {

/**
* Get the state of an entity program.
*
* @param program
* The name of program to get the state of; must be a registered program in the {@link EntityProgramRegistry}
* @return The state of the given program
* @throws NoSuchElementException
* if the program is not registered in the {@link EntityProgramRegistry}
* @throws IllegalArgumentException
* if an argument is null
*/
public EntityProgramState getState(String program);

/**
* Check whether the given program can run.
*
* @param program
* The name of program to check; must be a registered program in the {@link EntityProgramRegistry}
*
* @return true if the program can run
* @throws NoSuchElementException
* if the program is not registered in the {@link EntityProgramRegistry}
* @throws IllegalArgumentException
* if an argument is null
*/
public boolean canRunProgram(String program);

/**
* Check whether the given program can be run to the given entity.
* <p>
* First checks {@link #canRunProgram(String)} and then {@link EntityProgram#canRunOn(Entity)}.
* </p>
*
* @param program
* The name of the program to check; must be a registered program in the {@link EntityProgramRegistry}
* @param entity
* The entity to check; must not be null
* @return true if the program can run on that entity
* @throws NoSuchElementException
* if the program is not registered in the {@link EntityProgramRegistry}
* @throws IllegalArgumentException
* if an argument is null
*/
public boolean canRunProgramOn(String program, Entity entity);

/**
* Run an entity program on an entity.
*
* @param program
* The program to run; must be a registered program in the {@link EntityProgramRegistry}; must be able to run on
* the entity now
* @param entity
* The entity to run the program on
* @throws IllegalArgumentException
* if an argument is null
* @throws NoSuchElementException
* if the program is not registered in the {@link EntityProgramRegistry}
* @throws CannotRunProgramException
* if the program is not able to run on the entity now
* @throws RuntimeException
* if an exception occurred in the entity program
*/
public void run(String program, Entity entity);

/**
* Force stop all running entity programs.
* <p>
* This does not block until successful shutdown
* </p>
*/
public void forceStop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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;

/**
* The state of a entity program
*
* @author Tim Neumann
*/
public enum EntityProgramState {
/** When the entity program is new and was not started yet. */
NEW,
/** When the entity program is currently running. */
RUNNING,
/** When the entity program finished nominally. */
FINISHED,
/** When the entity program was killed by the {@link EntityProgramRunner}. */
KILLED
}
Loading

0 comments on commit 6a8edf0

Please sign in to comment.