Skip to content
Merged

Dev2 #329

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,5 @@ Temporary Items
###################################

# VSCode
.vscode
.vscode
docgen/renv
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To use this project in your project, simply add the following dependency to your
<dependency>
<groupId>gov.hhs.aspr.ms.gcm</groupId>
<artifactId>simulation</artifactId>
<version>4.2.0</version>
<version>4.4.0</version>
</dependency>
```

Expand Down Expand Up @@ -53,7 +53,7 @@ Data managers and actors are organized into plugins. A GCM model is thus compose
### Requirements
- Maven 3.8.x
- Java 17
- Your favroite IDE for developing Java projects
- Your favorite IDE for developing Java projects
- Modeling Utilities located [here](https://github.com/HHS/ASPR-ms-util)

*Note that Modeling Utilities is in Maven Central, so there is no need to clone and build it.
Expand All @@ -69,7 +69,7 @@ To build this project:
The documentation can be found at [https://hhs.github.io/ASPR-8/](https://hhs.github.io/ASPR-8/)

## Lessons
The documentation contains lessons which the code can be found in [lessons](lessons).
Code for all lessons in the documentation above can be found in [lessons](lessons).

<!-- MARKDOWN LINKS & IMAGES -->
[contributors-shield]: https://img.shields.io/github/contributors/HHS/ASPR-8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* managers to actors. It is supplied by the engine each time it interacts with
* an actor. Actors are defined by this context. If this context is passed to a
* method invocation, then that method is an actor method.
*
*/
public final class ActorContext {

Expand Down Expand Up @@ -251,5 +252,31 @@ public List<ActorPlan> retrievePlans() {
public Optional<Double> getSimulationHaltTime() {
return simulation.getSimulationHaltTime();
}

/**
* Returns the plugin data object associated with the given class reference
*
* @throws ContractException
* <ul>
* <li>{@linkplain NucleusError#NULL_PLUGIN_DATA_CLASS}
* if the class reference is null</li>
* <li>{@linkplain NucleusError#AMBIGUOUS_PLUGIN_DATA_CLASS}
* if more than one plugin data object matches the
* class reference</li>
* </ul>
*/
public <T extends PluginData> Optional<T> getPluginData(Class<T> pluginDataClass) {
return simulation.getPluginData(pluginDataClass);
}

/**
* Returns the plugin data objects associated with the given class reference
*
* @throws ContractException {@linkplain NucleusError#NULL_PLUGIN_DATA_CLASS} if
* the class reference is null
*/
public <T extends PluginData> List<T> getPluginDatas(Class<T> pluginDataClass) {
return simulation.getPluginDatas(pluginDataClass);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -315,5 +315,31 @@ public DataManagerId getDataManagerId() {
public Optional<Double> getSimulationHaltTime() {
return simulation.getSimulationHaltTime();
}

/**
* Returns the plugin data object associated with the given class reference
*
* @throws ContractException
* <ul>
* <li>{@linkplain NucleusError#NULL_PLUGIN_DATA_CLASS}
* if the class reference is null</li>
* <li>{@linkplain NucleusError#AMBIGUOUS_PLUGIN_DATA_CLASS}
* if more than one plugin data object matches the
* class reference</li>
* </ul>
*/
public <T extends PluginData> Optional<T> getPluginData(Class<T> pluginDataClass) {
return simulation.getPluginData(pluginDataClass);
}

/**
* Returns the plugin data objects associated with the given class reference
*
* @throws ContractException {@linkplain NucleusError#NULL_PLUGIN_DATA_CLASS} if
* the class reference is null
*/
public <T extends PluginData> List<T> getPluginDatas(Class<T> pluginDataClass) {
return simulation.getPluginDatas(pluginDataClass);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,31 @@ public List<ReportPlan> retrievePlans() {
public Optional<Double> getSimulationHaltTime() {
return simulation.getSimulationHaltTime();
}

/**
* Returns the plugin data object associated with the given class reference
*
* @throws ContractException
* <ul>
* <li>{@linkplain NucleusError#NULL_PLUGIN_DATA_CLASS}
* if the class reference is null</li>
* <li>{@linkplain NucleusError#AMBIGUOUS_PLUGIN_DATA_CLASS}
* if more than one plugin data object matches the
* class reference</li>
* </ul>
*/
public <T extends PluginData> Optional<T> getPluginData(Class<T> pluginDataClass) {
return simulation.getPluginData(pluginDataClass);
}

/**
* Returns the plugin data objects associated with the given class reference
*
* @throws ContractException {@linkplain NucleusError#NULL_PLUGIN_DATA_CLASS} if
* the class reference is null
*/
public <T extends PluginData> List<T> getPluginDatas(Class<T> pluginDataClass) {
return simulation.getPluginDatas(pluginDataClass);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand Down Expand Up @@ -1333,4 +1334,163 @@ public void testGetDataManagerId() {
.execute();//

}

private static class LocalPluginData implements PluginData {

public static class Builder implements PluginDataBuilder {
@Override
public LocalPluginData build() {
return new LocalPluginData();
}
}

@Override
public PluginDataBuilder toBuilder() {
return new Builder();
}
}

private static class LocalPluginData2 implements PluginData {

public static class Builder implements PluginDataBuilder {
@Override
public LocalPluginData2 build() {
return new LocalPluginData2();
}
}

@Override
public PluginDataBuilder toBuilder() {
return new Builder();
}
}

@Test
@UnitTestMethod(target = DimensionContext.class, name = "getPluginData", args = { Class.class })
public void testGetPluginData() {

TestPluginData.Builder pluginDataBuilder = TestPluginData.builder();
pluginDataBuilder.addTestDataManager("dm", ()->new TestDataManager());

pluginDataBuilder.addTestDataManagerPlan("dm", new TestDataManagerPlan(0, (c) -> {
Optional<LocalPluginData> optional1 = c.getPluginData(LocalPluginData.class);
assertTrue(optional1.isPresent());
Optional<LocalPluginData2> optional2 = c.getPluginData(LocalPluginData2.class);
assertFalse(optional2.isPresent());

}));

// build the plugin
TestPluginData testPluginData = pluginDataBuilder.build();
Plugin testPlugin = TestPlugin.getTestPlugin(testPluginData);

Plugin localPlugin = Plugin.builder()//
.setPluginId(new SimplePluginId("localPlugin"))//
.addPluginData(new LocalPluginData())//
.build();

// run the simulation
Simulation.builder()//
.addPlugin(testPlugin)//
.addPlugin(localPlugin)//
.build()//
.execute();//

// precondition test: if more than one plugin data object matches the class
// reference
ContractException contractException = assertThrows(ContractException.class, () -> {
Plugin lp = Plugin.builder()//
.setPluginId(new SimplePluginId("localPlugin"))//
.addPluginData(new LocalPluginData())//
.addPluginData(new LocalPluginData())//
.build();

Simulation.builder()//
.addPlugin(lp)//
.addPlugin(TestPlugin.getTestPlugin(

TestPluginData.builder()//
.addTestDataManager("dm", ()->new TestDataManager())//
.addTestActorPlan("dm", new TestActorPlan(0, (c) -> {
c.getPluginData(LocalPluginData.class);
})).build()))//
.build()//
.execute();//
});
assertEquals(NucleusError.AMBIGUOUS_PLUGIN_DATA_CLASS, contractException.getErrorType());

// precondition test: if the class reference is null
contractException = assertThrows(ContractException.class, () -> {
Simulation.builder()//
.addPlugin(TestPlugin.getTestPlugin(
TestPluginData.builder()//
.addTestDataManager("dm", ()->new TestDataManager())
.addTestActorPlan("dm", new TestActorPlan(0, (c) -> {
c.getPluginData(null);
})).build()))//
.build()//
.execute();//
});
assertEquals(NucleusError.NULL_PLUGIN_DATA_CLASS, contractException.getErrorType());

}

@Test
@UnitTestMethod(target = DimensionContext.class, name = "getPluginDatas", args = { Class.class })
public void testGetPluginDatas() {
/*
* Returns the plugin data objects associated with the given class reference
*
* @throws ContractException {@linkplain NucleusError#NULL_PLUGIN_DATA_CLASS} if
* the class reference is null
*/
TestPluginData.Builder pluginDataBuilder = TestPluginData.builder();
pluginDataBuilder.addTestDataManager("dm",()->new TestDataManager());
pluginDataBuilder.addTestDataManagerPlan("dm", new TestDataManagerPlan(0, (c) -> {
List<LocalPluginData> localPluginDatas = c.getPluginDatas(LocalPluginData.class);
assertNotNull(localPluginDatas);
assertEquals(2,localPluginDatas.size());

List<LocalPluginData2> localPluginData2s = c.getPluginDatas(LocalPluginData2.class);
assertNotNull(localPluginData2s);
assertEquals(0,localPluginData2s.size());

List<PluginData> pluginDatas = c.getPluginDatas(PluginData.class);
assertNotNull(pluginDatas);
assertEquals(3,pluginDatas.size());

}));

// build the plugin
TestPluginData testPluginData = pluginDataBuilder.build();
Plugin testPlugin = TestPlugin.getTestPlugin(testPluginData);

Plugin localPlugin = Plugin.builder()//
.setPluginId(new SimplePluginId("localPlugin"))//
.addPluginData(new LocalPluginData())//
.addPluginData(new LocalPluginData())//
.build();

// run the simulation
Simulation.builder()//
.addPlugin(testPlugin)//
.addPlugin(localPlugin)//
.build()//
.execute();//

// precondition test: if the class reference is null
ContractException contractException = assertThrows(ContractException.class, () -> {
Simulation.builder()//
.addPlugin(TestPlugin.getTestPlugin(
TestPluginData.builder()//
.addTestDataManager("dm", ()->new TestDataManager())//
.addTestActorPlan("dm", new TestActorPlan(0, (c) -> {
c.getPluginDatas(null);
})).build()))//
.build()//
.execute();//
});
assertEquals(NucleusError.NULL_PLUGIN_DATA_CLASS, contractException.getErrorType());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,6 @@ public void testBuild() {
assertEquals(NucleusError.NULL_SIMULATION_STATE, contractException.getErrorType());

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ private void testSetHaltOnException() {
testSetHaltOnException_Implicit();
}

@Test

private void testSetHaltOnException_Explicit_True() {
// This test will run the experiment two times in single thread mode

Expand Down
Loading