I have created a feeder that creates objects after pulling data from a JSON file.
public class FruitFeeder{
public static Iterator<Map<String, Object>> CreateFeeder() {
Gson gson = new Gson();
try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("data/Fruit.json")) {
if (in == null) {
throw new RuntimeException("InputStream is null, maybe couldn't find the Fruit.json file");
}
String jsonString = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));
Fruit[] fruits = gson.fromJson(jsonString, Fruit[].class);
return Stream.generate((Supplier<Map<String, Object>>) () -> {
ArrayList<Map<String,Object>> fruitList = new ArrayList<>();
for (Fruit fruit: fruits) {
int randomSize = ThreadLocalRandom.current().nextInt(0, 500);
// Create a fruit map
Map<String, Object> fruitMap = Map.of(
"name", fruit.name,
"color", fruit.color,
"size", randomSize);
//Add to array
fruitList.add(fruitMap)
}
return Map.of("fruits", fruitList);
}).iterator();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
When using this feeder in a ChainBuilder
it sometimes works. Sometimes I am given the error Feeder is now empty, stopping the engine
.
Currently the simulations that depend on this feeder only inject 1 user. My intention is that this feeder creates a new array of fruit mappings everytime it is called. It creates the maps by a combination of data it pulls from JSON and data it randomly generates.
What am I missing?