Skip to content

Commit

Permalink
Changed descendancy pattern for ActorPlan
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnhatch committed Jun 27, 2024
1 parent 3c78031 commit c6acdc8
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void subscribeToSimulationClose(Consumer<ActorContext> consumer) {
* </ul>
*/
public void addPlan(final Consumer<ActorContext> consumer, final double planTime) {
simulation.addActorPlan(new ActorPlan(planTime, consumer));
simulation.addActorPlan(new ConsumerActorPlan(planTime, consumer));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,45 @@
package gov.hhs.aspr.ms.gcm.simulation.nucleus;

import java.util.function.Consumer;

import gov.hhs.aspr.ms.util.errors.ContractException;

public class ActorPlan extends Plan {
public abstract class ActorPlan extends Plan {

// The actor id is used by the simulation via package access
ActorId actorId;

private final Consumer<ActorContext> consumer;


/**
* Constructs the plan scheduled for the given time active status arrivalId and
* consumer
*
* @throw {@link ContractException}
* <ul>
* <li>{@linkplain NucleusError#NULL_PLAN_CONSUMER} if the consumer is
* null</li>
* </ul>
* Constructs the plan scheduled for the given time, active status and arrivalId.
*
*
*/
public ActorPlan(double time, boolean active, long arrivalId, Consumer<ActorContext> consumer) {
public ActorPlan(double time, boolean active, long arrivalId) {
super(time, active, arrivalId, Planner.ACTOR);
if (consumer == null) {
throw new ContractException(NucleusError.NULL_PLAN_CONSUMER);
}
this.consumer = consumer;
}

/**
* Constructs the plan scheduled for the given time and consumer. The plan will
* Constructs the plan scheduled for the given time. The plan will
* be active.The arrival id is set to -1L indicating that this is a new,
* non-deserialized plan.
*
* @throw {@link ContractException}
* <ul>
* <li>{@linkplain NucleusError#NULL_PLAN_CONSUMER} if the consumer is
* null</li>
* </ul>
*
*/
public ActorPlan(double time, Consumer<ActorContext> consumer) {
public ActorPlan(double time) {
super(time, true, -1L, Planner.ACTOR);
if (consumer == null) {
throw new ContractException(NucleusError.NULL_PLAN_CONSUMER);
}
this.consumer = consumer;
}

/**
* Constructs the plan scheduled for the given time, active status and consumer.
* Constructs the plan scheduled for the given time and active status.
* The arrival id is set to -1L indicating that this is a new, non-deserialized
* plan.
*
* @throw {@link ContractException}
* <ul>
* <li>{@linkplain NucleusError#NULL_PLAN_CONSUMER} if the consumer is
* null</li>
* </ul>
*
*
*/
public ActorPlan(double time, boolean active, Consumer<ActorContext> consumer) {
public ActorPlan(double time, boolean active) {
super(time, active, -1L, Planner.ACTOR);
if (consumer == null) {
throw new ContractException(NucleusError.NULL_PLAN_CONSUMER);
}
this.consumer = consumer;
}

protected void execute(ActorContext context) {
this.consumer.accept(context);
}
/**
* Executes the actor logic associated with the plan.
*/
protected abstract void execute(ActorContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package gov.hhs.aspr.ms.gcm.simulation.nucleus;

import java.util.function.Consumer;

import gov.hhs.aspr.ms.util.errors.ContractException;

public final class ConsumerActorPlan extends ActorPlan {

private final Consumer<ActorContext> consumer;

/**
* Constructs the plan scheduled for the given time active status arrivalId and
* consumer
*
* @throw {@link ContractException}
* <ul>
* <li>{@linkplain NucleusError#NULL_PLAN_CONSUMER} if the consumer is
* null</li>
* </ul>
*
*/
public ConsumerActorPlan(double time, boolean active, long arrivalId, Consumer<ActorContext> consumer) {
super(time, active, arrivalId);
if (consumer == null) {
throw new ContractException(NucleusError.NULL_PLAN_CONSUMER);
}
this.consumer = consumer;
}

/**
* Constructs the plan scheduled for the given time and consumer. The plan will
* be active.The arrival id is set to -1L indicating that this is a new,
* non-deserialized plan.
*
* @throw {@link ContractException}
* <ul>
* <li>{@linkplain NucleusError#NULL_PLAN_CONSUMER} if the consumer is
* null</li>
* </ul>
*
*/
public ConsumerActorPlan(double time, Consumer<ActorContext> consumer) {
super(time, true, -1L);
if (consumer == null) {
throw new ContractException(NucleusError.NULL_PLAN_CONSUMER);
}
this.consumer = consumer;
}

/**
* Constructs the plan scheduled for the given time, active status and consumer.
* The arrival id is set to -1L indicating that this is a new, non-deserialized
* plan.
*
* @throw {@link ContractException}
* <ul>
* <li>{@linkplain NucleusError#NULL_PLAN_CONSUMER} if the consumer is
* null</li>
* </ul>
*
*/
public ConsumerActorPlan(double time, boolean active, Consumer<ActorContext> consumer) {
super(time, active, -1L);
if (consumer == null) {
throw new ContractException(NucleusError.NULL_PLAN_CONSUMER);
}
this.consumer = consumer;
}

@Override
protected final void execute(ActorContext context) {
this.consumer.accept(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import gov.hhs.aspr.ms.gcm.simulation.nucleus.ActorContext;
import gov.hhs.aspr.ms.gcm.simulation.nucleus.ActorPlan;
import gov.hhs.aspr.ms.gcm.simulation.nucleus.ConsumerActorPlan;

public class RunContinuityActor implements Consumer<ActorContext> {
private final RunContinuityPluginData runContinuityPluginData;
Expand All @@ -29,7 +30,7 @@ public void accept(ActorContext actorContext) {
double time = pair.getFirst();
Consumer<ActorContext> consumer = pair.getSecond();

ActorPlan actorPlan = new ActorPlan(time, (c) -> {
ActorPlan actorPlan = new ConsumerActorPlan(time, (c) -> {
planMap.remove(i);
consumer.accept(actorContext);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class TestActorPlan extends ActorPlan {
* null
*/
public TestActorPlan(final double scheduledTime, Consumer<ActorContext> consumer) {
super(scheduledTime, consumer);
super(scheduledTime);
if (consumer == null) {
throw new ContractException(TestError.NULL_PLAN);
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public boolean equals(Object obj) {
* Constructs a test actor plan from another test actor plan.
*/
public TestActorPlan(TestActorPlan testActorPlan) {
super(testActorPlan.getTime(), testActorPlan.consumer);
super(testActorPlan.getTime());
executed = testActorPlan.executed;
consumer = testActorPlan.consumer;
}
Expand All @@ -87,7 +87,7 @@ public boolean executed() {
@Override
protected void execute(final ActorContext actorContext) {
try {
super.execute(actorContext);
consumer.accept(actorContext);
} finally {
executed = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void testAddPlan_Plan() {
*/

MutableBoolean planExecuted = new MutableBoolean();
ActorPlan actorPlan = new ActorPlan(5, (c) -> {
ActorPlan actorPlan = new ConsumerActorPlan(5, (c) -> {
planExecuted.setValue(true);
});

Expand Down Expand Up @@ -315,7 +315,7 @@ public void testAddPlan_Plan() {
Simulation.builder()//
.addPlugin(TestPlugin.getTestPlugin(
TestPluginData.builder().addTestActorPlan("actor", new TestActorPlan(0, (c) -> {
c.addPlan(new ActorPlan(-10, (c3)->{}));
c.addPlan(new ConsumerActorPlan(-10, (c3)->{}));
})).build()))//
.build()//
.execute();//
Expand All @@ -329,7 +329,7 @@ public void testAddPlan_Plan() {
.addPlugin(TestPlugin.getTestPlugin(
TestPluginData.builder().addTestActorPlan("actor", new TestActorPlan(0, (c) -> {
c.subscribeToSimulationClose((c2)->{
c2.addPlan(new ActorPlan(0, (c3)->{}));
c2.addPlan(new ConsumerActorPlan(0, (c3)->{}));
});

})).build()))//
Expand Down Expand Up @@ -985,7 +985,7 @@ public void testRetrievePlans() {
double haltTime = 50;

for (int i = 1; i <= 100; i++) {
ActorPlan actorPlan = new ActorPlan(i, (c) -> {
ActorPlan actorPlan = new ConsumerActorPlan(i, (c) -> {
});
if (i > 50) {
expectedPlans.add(actorPlan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ public SpecialActor(int id) {
public void accept(ActorContext actorContext) {
switch (id) {
case 0:
actorContext.addPlan(new ActorPlan(2, true, 5L, c -> c.releaseOutput(1)));
actorContext.addPlan(new ActorPlan(7, true, 14L, c -> c.releaseOutput(4)));
actorContext.addPlan(new ActorPlan(14, true, 12L, c -> c.releaseOutput(9)));
actorContext.addPlan(new ActorPlan(2, true, -1L, c -> c.releaseOutput(2)));
actorContext.addPlan(new ActorPlan(7, true, -1L, c -> c.releaseOutput(6)));
actorContext.addPlan(new ActorPlan(14, true, -1L, c -> c.releaseOutput(10)));
actorContext.addPlan(new ConsumerActorPlan(2, true, 5L, c -> c.releaseOutput(1)));
actorContext.addPlan(new ConsumerActorPlan(7, true, 14L, c -> c.releaseOutput(4)));
actorContext.addPlan(new ConsumerActorPlan(14, true, 12L, c -> c.releaseOutput(9)));
actorContext.addPlan(new ConsumerActorPlan(2, true, -1L, c -> c.releaseOutput(2)));
actorContext.addPlan(new ConsumerActorPlan(7, true, -1L, c -> c.releaseOutput(6)));
actorContext.addPlan(new ConsumerActorPlan(14, true, -1L, c -> c.releaseOutput(10)));

break;
case 1:
actorContext.addPlan(new ActorPlan(2, true, 4L, c -> c.releaseOutput(0)));
actorContext.addPlan(new ActorPlan(7, true, 16L, c -> c.releaseOutput(5)));
actorContext.addPlan(new ActorPlan(14, true, 11L, c -> c.releaseOutput(8)));
actorContext.addPlan(new ActorPlan(2, true, -1L, c -> c.releaseOutput(3)));
actorContext.addPlan(new ActorPlan(7, true, -1L, c -> c.releaseOutput(7)));
actorContext.addPlan(new ActorPlan(14, true, -1L, c -> c.releaseOutput(11)));
actorContext.addPlan(new ConsumerActorPlan(2, true, 4L, c -> c.releaseOutput(0)));
actorContext.addPlan(new ConsumerActorPlan(7, true, 16L, c -> c.releaseOutput(5)));
actorContext.addPlan(new ConsumerActorPlan(14, true, 11L, c -> c.releaseOutput(8)));
actorContext.addPlan(new ConsumerActorPlan(2, true, -1L, c -> c.releaseOutput(3)));
actorContext.addPlan(new ConsumerActorPlan(7, true, -1L, c -> c.releaseOutput(7)));
actorContext.addPlan(new ConsumerActorPlan(14, true, -1L, c -> c.releaseOutput(11)));

break;
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,26 @@
package gov.hhs.aspr.ms.gcm.simulation.nucleus;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.function.Consumer;

import org.junit.jupiter.api.Test;

import gov.hhs.aspr.ms.util.annotations.UnitTestConstructor;
import gov.hhs.aspr.ms.util.errors.ContractException;

public class AT_ActorPlan {

@Test
@UnitTestConstructor(target = ActorPlan.class, args = { double.class, Consumer.class })
@UnitTestConstructor(target = ActorPlan.class, args = { double.class })
public void testActorPlan() {
//precondition test: if the consumer is null
ContractException contractException = assertThrows(ContractException.class, ()->{
new ActorPlan(0, null);
});
assertEquals(NucleusError.NULL_PLAN_CONSUMER, contractException.getErrorType());
//nothing to test
}

@Test
@UnitTestConstructor(target = ActorPlan.class, args = { double.class, boolean.class, Consumer.class })
@UnitTestConstructor(target = ActorPlan.class, args = { double.class, boolean.class})
public void testConstructor_Active() {
for (int i = 0; i < 10; i++) {
ActorPlan actorPlan = new ActorPlan(i, i % 2 == 0, (c) -> {
});

assertNotNull(actorPlan);
}
//nothing to test
}

@Test
@UnitTestConstructor(target = ActorPlan.class, args = { double.class, boolean.class, long.class, Consumer.class })
@UnitTestConstructor(target = ActorPlan.class, args = { double.class, boolean.class, long.class})
public void testConstructor_Active_Arrival() {
for (int i = 0; i < 10; i++) {
ActorPlan actorPlan = new ActorPlan(i, i % 2 == 0, (long) i, (c) -> {
});

assertNotNull(actorPlan);
}
//nothing to test
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package gov.hhs.aspr.ms.gcm.simulation.nucleus;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.function.Consumer;

import org.junit.jupiter.api.Test;

import gov.hhs.aspr.ms.util.annotations.UnitTestConstructor;
import gov.hhs.aspr.ms.util.errors.ContractException;

public class AT_ConsumerActorPlan {

@Test
@UnitTestConstructor(target = ConsumerActorPlan.class, args = { double.class, Consumer.class })
public void testActorPlan() {
//precondition test: if the consumer is null
ContractException contractException = assertThrows(ContractException.class, ()->{
new ConsumerActorPlan(0, null);
});
assertEquals(NucleusError.NULL_PLAN_CONSUMER, contractException.getErrorType());
}

@Test
@UnitTestConstructor(target = ConsumerActorPlan.class, args = { double.class, boolean.class, Consumer.class })
public void testConstructor_Active() {
//precondition test: if the consumer is null
ContractException contractException = assertThrows(ContractException.class, ()->{
new ConsumerActorPlan(0,true, null);
});
assertEquals(NucleusError.NULL_PLAN_CONSUMER, contractException.getErrorType());
}

@Test
@UnitTestConstructor(target = ConsumerActorPlan.class, args = { double.class, boolean.class, long.class, Consumer.class })
public void testConstructor_Active_Arrival() {
//precondition test: if the consumer is null
ContractException contractException = assertThrows(ContractException.class, ()->{
new ConsumerActorPlan(0,true,2345345L, null);
});
assertEquals(NucleusError.NULL_PLAN_CONSUMER, contractException.getErrorType());
}
}

0 comments on commit c6acdc8

Please sign in to comment.