Skip to content

Commit

Permalink
Added next batch and stage id values used by the data manager to add …
Browse files Browse the repository at this point in the history
…batches as stages in support of run continuity (#108)

Co-authored-by: Shawn <[email protected]>
  • Loading branch information
shawnhatch and humbledaisy authored Apr 21, 2023
1 parent 9c2553a commit 80407f8
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 306 deletions.
107 changes: 107 additions & 0 deletions gcm4/src/main/java/plugins/materials/MaterialsPluginData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.*;

import org.apache.commons.math3.util.FastMath;

import net.jcip.annotations.Immutable;
import nucleus.PluginData;
import nucleus.PluginDataBuilder;
Expand Down Expand Up @@ -244,6 +246,16 @@ public Builder addStage(final StageId stageId, final boolean offered, final Mate
* <li>{@linkplain MaterialsError#BATCH_STAGED_TO_DIFFERENT_OWNER}
* if a batch is associated with a stage that is not owned
* by the same materials producer as the batch</li>
*
* <li>{@linkplain MaterialsError#NEXT_BATCH_ID_TOO_SMALL}
* if a batch is greater than or equal to the next batch id
* assigned for the entire plugin data</li>
*
* <li>{@linkplain MaterialsError#NEXT_STAGE_ID_TOO_SMALL}
* if a stage is greater than or equal to the next stage id
* assigned for the entire plugin data</li>
*
*
*/

public MaterialsPluginData build() {
Expand Down Expand Up @@ -382,6 +394,42 @@ public Builder setMaterialsProducerResourceLevel(final MaterialsProducerId mater
return this;
}

/**
* Sets the next available batch id. This value needs to exceed all
* extant batch ids. If the nextBatchRecordId is not set explicitly, the
* nextBatchRecordId is assigned to either zero or the next integer
* value that exceeds the highest valued batch added to this builder.
*
* @throws ContractException
* <li>{@linkplain MaterialsError#NEGATIVE_BATCH_ID} if the
* next batch record id is negative</li>
*
*/
public Builder setNextBatchRecordId(int nextBatchRecordId) {
ensureDataMutability();
validateBatchIdValue(nextBatchRecordId);
data.nextBatchRecordId = nextBatchRecordId;
return this;
}

/**
* Sets the next available stage id. This value needs to exceed all
* extant stage ids. If the nextStageRecordId is not set explicitly, the
* nextStageRecordId is assigned to either zero or the next integer
* value that exceeds the highest valued batch added to this builder.
*
* @throws ContractException
* <li>{@linkplain MaterialsError#NEGATIVE_BATCH_ID} if the
* next stage record id is negative</li>
*
*/
public Builder setNextStageRecordId(int nextStageRecordId) {
ensureDataMutability();
validateStageIdValue(nextStageRecordId);
data.nextStageRecordId = nextStageRecordId;
return this;
}

private void validateData() {

for (final MaterialId materialId : data.batchPropertyDefinitions.keySet()) {
Expand Down Expand Up @@ -502,7 +550,22 @@ private void validateData() {
* Ensure that each batch has property value assignments for every
* relevant property definition that does not have a default value
*/

if (data.nextBatchRecordId < 0) {
for (final BatchId batchId : data.batchIds) {
data.nextBatchRecordId = FastMath.max(data.nextBatchRecordId, batchId.getValue());
}
data.nextBatchRecordId++;
} else {
for (final BatchId batchId : data.batchIds) {
if (batchId.getValue() >= data.nextBatchRecordId) {
throw new ContractException(MaterialsError.NEXT_BATCH_ID_TOO_SMALL);
}
}
}

for (final BatchId batchId : data.batchIds) {

final MaterialId materialId = data.batchMaterials.get(batchId);
Map<BatchPropertyId, Integer> propertyIndexMap = nonDefaultBatchPropertiesMap.get(materialId);
boolean[] checkArray = nonDefaultBatchCheckArrayMap.get(materialId);
Expand All @@ -529,7 +592,21 @@ private void validateData() {
}
}

if (data.nextStageRecordId < 0) {
for (final StageId stageId : data.stageIds) {
data.nextStageRecordId = FastMath.max(data.nextStageRecordId, stageId.getValue());
}
data.nextStageRecordId++;
} else {
for (final StageId stageId : data.stageIds) {
if (stageId.getValue() >= data.nextStageRecordId) {
throw new ContractException(MaterialsError.NEXT_STAGE_ID_TOO_SMALL);
}
}
}

for (final StageId stageId : data.stageMaterialsProducers.keySet()) {

final MaterialsProducerId materialsProducerId = data.stageMaterialsProducers.get(stageId);
if (!data.materialsProducerIds.contains(materialsProducerId)) {
throw new ContractException(MaterialsError.UNKNOWN_MATERIALS_PRODUCER_ID, stageId + " in stage additions");
Expand Down Expand Up @@ -620,6 +697,10 @@ private static class Data {

private final Map<BatchId, StageId> batchStages;

private int nextBatchRecordId = -1;

private int nextStageRecordId = -1;

private boolean locked;

private boolean compareBatchPropertyValues(Data other) {
Expand Down Expand Up @@ -1177,6 +1258,18 @@ private static void validateStageExists(final Data data, final StageId stageId)
}
}

private static void validateBatchIdValue(int batchIdValue) {
if (batchIdValue < 0) {
throw new ContractException(MaterialsError.NEGATIVE_BATCH_ID, batchIdValue);
}
}

private static void validateStageIdValue(int stageIdValue) {
if (stageIdValue < 0) {
throw new ContractException(MaterialsError.NEGATIVE_STAGE_ID, stageIdValue);
}
}

private static void validateStageIdNotNull(final StageId stageId) {
if (stageId == null) {
throw new ContractException(MaterialsError.NULL_STAGE_ID);
Expand Down Expand Up @@ -1488,6 +1581,20 @@ public PluginDataBuilder getCloneBuilder() {
return new Builder(data);
}

/**
* Returns the next available batch id.
*/
public int getNextBatchRecordId() {
return data.nextBatchRecordId;
}

/**
* Returns the next available stage id.
*/
public int getNextStageRecordId() {
return data.nextStageRecordId;
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Optional;
import java.util.Set;

import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.Pair;

import nucleus.DataManager;
Expand Down Expand Up @@ -309,28 +308,26 @@ public void init(DataManagerContext dataManagerContext) {
}
}

nextStageRecordId = -1;

for (final StageId stageId : materialsPluginData.getStageIds()) {
final MaterialsProducerId materialsProducerId = materialsPluginData.getStageMaterialsProducer(stageId);
final MaterialsProducerRecord materialsProducerRecord = materialsProducerMap.get(materialsProducerId);
final StageRecord stageRecord = new StageRecord(stageId);
nextStageRecordId = FastMath.max(nextStageRecordId, stageRecord.stageId.getValue());
final StageRecord stageRecord = new StageRecord(stageId);
stageRecord.materialsProducerRecord = materialsProducerRecord;
stageRecord.offered = materialsPluginData.isStageOffered(stageId);
materialsProducerRecord.stageRecords.add(stageRecord);
stageRecords.put(stageRecord.stageId, stageRecord);
}
nextStageRecordId++;
nextStageRecordId = materialsPluginData.getNextStageRecordId();

nextBatchRecordId = -1;

for (final BatchId batchId : materialsPluginData.getBatchIds()) {
final MaterialsProducerId materialsProducerId = materialsPluginData.getBatchMaterialsProducer(batchId);
final MaterialId materialId = materialsPluginData.getBatchMaterial(batchId);
final double amount = materialsPluginData.getBatchAmount(batchId);
final MaterialsProducerRecord materialsProducerRecord = materialsProducerMap.get(materialsProducerId);

final BatchRecord batchRecord = new BatchRecord(batchId);
nextBatchRecordId = FastMath.max(nextBatchRecordId, batchRecord.batchId.getValue());
final BatchRecord batchRecord = new BatchRecord(batchId);
batchRecord.amount = amount;
batchRecord.creationTime = dataManagerContext.getTime();
batchRecord.materialId = materialId;
Expand All @@ -349,7 +346,7 @@ public void init(DataManagerContext dataManagerContext) {
}

}
nextBatchRecordId++;
nextBatchRecordId = materialsPluginData.getNextBatchRecordId();

for (final StageId stageId : materialsPluginData.getStageIds()) {
final Set<BatchId> batches = materialsPluginData.getStageBatches(stageId);
Expand Down Expand Up @@ -385,6 +382,9 @@ public void init(DataManagerContext dataManagerContext) {
private void recordSimulationState(DataManagerContext dataManagerContext) {

MaterialsPluginData.Builder builder = MaterialsPluginData.builder();

builder.setNextBatchRecordId(nextBatchRecordId);
builder.setNextStageRecordId(nextStageRecordId);

Set<MaterialsProducerPropertyId> materialsProducerPropertyIds = getMaterialsProducerPropertyIds();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public enum MaterialsError implements ContractError {
DUPLICATE_MATERIALS_PRODUCER_PROPERTY_ID("Duplicate materials producer property id"),
RESOURCE_LOADING_ORDER("Resources must be added before materials producers are added"),
MATERIALS_PRODUCER_PROPERTY_LOADING_ORDER("Material procuders must be added before materials producer properties are added"),
NEXT_BATCH_ID_TOO_SMALL("The next batch id must exceed all extant batch ids"),
NEXT_STAGE_ID_TOO_SMALL("The next stage id must exceed all extant stage ids"),
BATCH_ALREADY_STAGED("Batch is already staged"),
DUPLICATE_MATERIAL("Duplicate material addition"),
BATCH_NOT_STAGED("Batch is not currently staged"),
Expand All @@ -24,6 +26,8 @@ public enum MaterialsError implements ContractError {
INSUFFICIENT_MATERIAL_AVAILABLE("Material level is insufficient for transaction amount"),
MATERIAL_ARITHMETIC_EXCEPTION("Material arithmetic error due to non finite sum"),
MATERIAL_TYPE_MISMATCH("Material identifiers do not match"),
NEGATIVE_BATCH_ID("Negative batch id"),
NEGATIVE_STAGE_ID("Negative stage id"),
NEGATIVE_MATERIAL_AMOUNT("Material amount is negative"),
NON_FINITE_MATERIAL_AMOUNT("Material amount is not finite"),
NULL_BATCH_CONSTRUCTION_INFO("Null batch construction info"),
Expand Down
19 changes: 3 additions & 16 deletions gcm4/src/test/java/plugins/materials/AT_MaterialsPluginData.java
Original file line number Diff line number Diff line change
Expand Up @@ -582,16 +582,8 @@ public void testAddStage() {

MaterialsPluginData materialsInitialData = MaterialsPluginData .builder()//
.addStage(stageId, offered, materialsProducerId)//
// adding
// duplicate
// data
// to
// show
// that
// the
// value
// persists
.addStage(stageId, offered, materialsProducerId).addMaterialsProducerId(materialsProducerId)//
.addStage(stageId, offered, materialsProducerId)//
.addMaterialsProducerId(materialsProducerId)//
.build();//
assertTrue(materialsInitialData.getStageIds().contains(stageId));
assertEquals(offered, materialsInitialData.isStageOffered(stageId));
Expand All @@ -600,9 +592,6 @@ public void testAddStage() {
offered = false;
materialsInitialData = MaterialsPluginData .builder()//
.addStage(stageId, offered, materialsProducerId)//
// adding duplicate data to
// show that the value
// persists
.addStage(stageId, offered, materialsProducerId).addMaterialsProducerId(materialsProducerId)//
.build();//

Expand Down Expand Up @@ -1737,15 +1726,13 @@ public void testIsStageOffered() {
@UnitTestMethod(target = MaterialsPluginData.class, name = "getResourceIds", args = {})
public void testGetResourceIds() {



MaterialsPluginData materialsInitialData = MaterialsPluginData.builder().build();//

assertTrue(materialsInitialData.getResourceIds().isEmpty());

TestMaterialsProducerId testMaterialsProducerId = TestMaterialsProducerId.MATERIALS_PRODUCER_1;
long amount = 45L;

MaterialsPluginData.Builder builder = MaterialsPluginData.builder();
for (TestResourceId testResourceId : TestResourceId.values()) {
builder.setMaterialsProducerResourceLevel(testMaterialsProducerId, testResourceId, amount++);
Expand Down
Loading

0 comments on commit 80407f8

Please sign in to comment.