introduce testing, first wokring chain with screws
parent
f8c17d351f
commit
4cb7b821a6
|
|
@ -14,7 +14,13 @@ sourceSets {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
dependencies{
|
||||
implementation 'org.junit.jupiter:junit-jupiter:5.7.0'
|
||||
compile 'com.fasterxml.jackson.core:jackson-core:2.12.2'
|
||||
compile 'com.fasterxml.jackson.core:jackson-databind:2.12.2'
|
||||
compile 'org.jgrapht:jgrapht-io:1.5.1'
|
||||
|
|
|
|||
|
|
@ -58,5 +58,17 @@ public class Test {
|
|||
|
||||
Utils.fixSums(ACU, x);
|
||||
de.exportGraph(x, new File("acu3.dot"));
|
||||
|
||||
plot(Database.Screw, "screws", 100, de);
|
||||
plot(Database.ReinforcedIronPlate, "ReinforcedIronPlate", 100, de);
|
||||
}
|
||||
|
||||
private static void plot(Item target, String name, int amount, DOTExporter<Item, DefaultWeightedEdge> de) {
|
||||
Graph<Item, DefaultWeightedEdge> screws = target.productionHierarchy(amount);
|
||||
de.exportGraph(screws, new File(name + ".dot"));
|
||||
System.out.println(name);
|
||||
Item.production(screws).forEach((item, rate) -> {
|
||||
System.out.println("\t" + item.getName() + "\t" + rate);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
package items;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.jgrapht.Graph;
|
||||
import org.jgrapht.graph.DefaultWeightedEdge;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class Item {
|
||||
public abstract class Item {
|
||||
protected boolean isRaw = false;
|
||||
private String name;
|
||||
private Set<Recipe> recipes;
|
||||
|
|
@ -61,11 +62,12 @@ public class Item {
|
|||
public Recipe getRecipe() {
|
||||
Recipe recipe = preference;
|
||||
if (recipe == null) {
|
||||
recipe = recipes.stream().findFirst().orElse(null);
|
||||
if (Database.preferences.containsKey(this)) {
|
||||
recipe = Database.preferences.get(this);
|
||||
}
|
||||
if (recipe == null) {
|
||||
if (Database.preferences.containsKey(this)) {
|
||||
recipe = Database.preferences.get(this);
|
||||
}
|
||||
recipe = recipes.stream().findFirst().orElse(null);
|
||||
|
||||
}
|
||||
}
|
||||
return recipe;
|
||||
|
|
@ -83,4 +85,30 @@ public class Item {
|
|||
this.preference = preference;
|
||||
}
|
||||
|
||||
public Graph<Item, DefaultWeightedEdge> productionHierarchy() {
|
||||
return productionHierarchy(1);
|
||||
}
|
||||
|
||||
public Graph<Item, DefaultWeightedEdge> productionHierarchy(int amount) {
|
||||
|
||||
Graph<Item, DefaultWeightedEdge> graph = getRecipe().buildGraph(this, amount);
|
||||
graph.addEdge(this,this);
|
||||
graph.setEdgeWeight(this,this,amount);
|
||||
return graph;
|
||||
}
|
||||
|
||||
public Map<Item, Double> production(int amount){
|
||||
Graph<Item, DefaultWeightedEdge> graph = productionHierarchy(amount);
|
||||
return production(graph);
|
||||
}
|
||||
|
||||
public static Map<Item, Double> production(Graph<Item, DefaultWeightedEdge> graph){
|
||||
Map<Item,Double> map = new HashMap<>();
|
||||
graph.vertexSet().forEach(item -> {
|
||||
double rate = graph.outgoingEdgesOf(item).stream().mapToDouble(graph::getEdgeWeight).sum();
|
||||
map.put(item, rate);
|
||||
});
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,7 +92,6 @@ public class Recipe {
|
|||
public Map<Item, Integer> getTotalRequirements() {
|
||||
return new TotalAccumulator(inputs).accumulate();
|
||||
}
|
||||
|
||||
public float getProductionRate(Item item) {
|
||||
Integer integer = outputs.get(item);
|
||||
float production = integer;
|
||||
|
|
@ -133,6 +132,46 @@ public class Recipe {
|
|||
});
|
||||
|
||||
|
||||
return graph;
|
||||
}
|
||||
|
||||
private float getLoad(Item target, float amount) {
|
||||
return getUsage(target, amount) * duration;
|
||||
}
|
||||
|
||||
private float getUsage(Item target, float amount) {
|
||||
return amount / outputs.get(target);
|
||||
}
|
||||
|
||||
public Graph<Item, DefaultWeightedEdge> buildGraph(Item target, float amountPerSecond) {
|
||||
Graph<Item, DefaultWeightedEdge> graph = new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class);
|
||||
graph.addVertex(target);
|
||||
target.sum += 1;
|
||||
float load = getLoad(target, amountPerSecond);
|
||||
// byproducts
|
||||
Map<Item, Integer> output = outputs.entrySet().stream()
|
||||
.filter(itemIntegerEntry -> itemIntegerEntry.getKey() != target)
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
output.forEach((item, integer) -> {
|
||||
graph.addVertex(item);
|
||||
graph.addEdge(target, item);
|
||||
graph.setEdgeWeight(target, item, integer * load);
|
||||
});
|
||||
// inputs
|
||||
inputs.forEach((item, integer) -> {
|
||||
graph.addVertex(item);
|
||||
graph.addEdge(item, target);
|
||||
float rate = (amountPerSecond / outputs.get(target))/integer;
|
||||
graph.setEdgeWeight(item, target, rate);
|
||||
|
||||
Recipe recipe = item.getRecipe();
|
||||
if (recipe != null) {
|
||||
Graph<Item, DefaultWeightedEdge> g = recipe.buildGraph(item, rate);
|
||||
Graphs.addGraph(graph, g);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return graph;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ public class Utils {
|
|||
}
|
||||
|
||||
public static String dotID(Item item){
|
||||
String name = item.getName();// +"_"+item.sum+"__"+item.getProductionRate();
|
||||
String name = item.getName() +"_"+item.sum+"__"+(String.valueOf(item.getProductionRate()).replace(".","_"));
|
||||
name = name.replace(" ","").replace(".","");
|
||||
return name;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
package items;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ItemTest {
|
||||
|
||||
@Test
|
||||
void productionScrews() {
|
||||
Map<Item, Double> production = Database.Screw.production(100);
|
||||
assertEquals(100, production.get(Database.Screw), "Screws (output)");
|
||||
assertEquals(25, production.get(Database.IronRod), "IronRod");
|
||||
assertEquals(25, production.get(Database.IronIngot), "IronIngot");
|
||||
assertEquals(25, production.get(Database.IronOre), "IronOre");
|
||||
}
|
||||
|
||||
@Test
|
||||
void productionReinforcedIronPlates() {
|
||||
Map<Item, Double> production = Database.ReinforcedIronPlate.production(100);
|
||||
assertEquals(100, production.get(Database.ReinforcedIronPlate), "output");
|
||||
assertEquals(1200, production.get(Database.Screw), "Screws");
|
||||
assertEquals(300, production.get(Database.IronRod), "IronRod");
|
||||
assertEquals(1200, production.get(Database.IronIngot), "IronIngot");
|
||||
assertEquals(1200, production.get(Database.IronOre), "IronOre");
|
||||
assertEquals(600, production.get(Database.IronPlate), "IronPlate");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue