equivalent-exchange-3/common/com/pahimar/ee3/emc/DynEMC.java
2013-09-28 15:00:28 -04:00

109 lines
3.8 KiB
Java

package com.pahimar.ee3.emc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.google.common.collect.Multimap;
import com.pahimar.ee3.core.util.LogHelper;
import com.pahimar.ee3.graph.Node;
import com.pahimar.ee3.graph.WeightedDirectedGraph;
import com.pahimar.ee3.graph.WeightedDirectedEdge;
import com.pahimar.ee3.item.CustomWrappedStack;
import com.pahimar.ee3.item.crafting.RecipeRegistry;
public class DynEMC {
private static DynEMC dynEMC = null;
private RecipeRegistry recipeRegistry;
public final WeightedDirectedGraph<CustomWrappedStack> graph;
private DynEMC() {
recipeRegistry = RecipeRegistry.getInstance();
graph = new WeightedDirectedGraph<CustomWrappedStack>();
init();
}
public static DynEMC getInstance() {
if (dynEMC == null) {
dynEMC = new DynEMC();
}
return dynEMC;
}
private void init() {
populateGraph();
}
private void populateGraph() {
for (CustomWrappedStack discoveredStack : recipeRegistry.getDiscoveredStacks()) {
graph.addNode(discoveredStack);
}
Multimap<CustomWrappedStack, List<CustomWrappedStack>> recipeMappings = recipeRegistry.getRecipeMappings();
Set<CustomWrappedStack> recipeKeySet = recipeMappings.keySet();
Iterator<CustomWrappedStack> recipeKeySetIterator = recipeKeySet.iterator();
while (recipeKeySetIterator.hasNext()) {
CustomWrappedStack recipeOutput = recipeKeySetIterator.next();
for (List<CustomWrappedStack> recipeInputs : recipeMappings.get(recipeOutput)) {
CustomWrappedStack unWrappedRecipeOutput = new CustomWrappedStack(recipeOutput.getWrappedStack());
if (graph.containsNode(unWrappedRecipeOutput)) {
for (CustomWrappedStack recipeInput : recipeInputs) {
// Unwrapped the wrapped stacks so that we actually find them in the graph
CustomWrappedStack unWrappedRecipeInput = new CustomWrappedStack(recipeInput.getWrappedStack());
if (graph.containsNode(unWrappedRecipeInput)) {
if (recipeOutput.getStackSize() != 0) {
graph.addEdge(unWrappedRecipeOutput, unWrappedRecipeInput, (recipeInput.getStackSize() * 1.0f) / recipeOutput.getStackSize());
}
}
}
}
}
}
}
public void printDebugDump() {
LogHelper.debug("Total node count: " + graph.getAllNodes().size());
LogHelper.debug("Leaf node count: " + graph.getLeafNodes().size());
LogHelper.debug("Orphan node count: " + graph.getOrphanNodes().size());
LogHelper.debug("Compound node count: " + graph.getCompoundNodes().size());
LogHelper.debug("'Critical' node count: " + (graph.getLeafNodes().size() - graph.getOrphanNodes().size()));
LogHelper.debug("");
List<Node<CustomWrappedStack>> nodes = new ArrayList<Node<CustomWrappedStack>>();
nodes.addAll(graph.getAllNodes());
Collections.sort(nodes);
for (Node<CustomWrappedStack> node : nodes) {
LogHelper.debug("Node: " + node);
LogHelper.debug("Edges FROM this Node:");
for (WeightedDirectedEdge<CustomWrappedStack> fromEdge : graph.edgesFrom(node)) {
LogHelper.debug(" * " + fromEdge);
}
LogHelper.debug("Edges TO this Node:");
for (WeightedDirectedEdge<CustomWrappedStack> toEdge : graph.edgesTo(node)) {
LogHelper.debug(" * " + toEdge);
}
LogHelper.debug("");
}
}
}