Move around some stuff

This commit is contained in:
pahimar 2013-07-03 20:31:07 -04:00
parent c1efbe06a7
commit b04b88f4d4
2 changed files with 46 additions and 6 deletions

View file

@ -1,8 +1,10 @@
package com.pahimar.ee3.emc; package com.pahimar.ee3.emc;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Set; import java.util.Set;
import com.google.common.collect.Multimap;
import com.pahimar.ee3.core.util.LogHelper; import com.pahimar.ee3.core.util.LogHelper;
import com.pahimar.ee3.emc.graph.WeightedDirectedGraph; import com.pahimar.ee3.emc.graph.WeightedDirectedGraph;
import com.pahimar.ee3.emc.graph.WeightedEdge; import com.pahimar.ee3.emc.graph.WeightedEdge;
@ -13,10 +15,12 @@ public class DynEMC {
private static DynEMC dynEMC = null; private static DynEMC dynEMC = null;
private RecipeRegistry recipeRegistry;
private WeightedDirectedGraph<CustomWrappedStack> graph; private WeightedDirectedGraph<CustomWrappedStack> graph;
private DynEMC() { private DynEMC() {
recipeRegistry = RecipeRegistry.getInstance();
graph = new WeightedDirectedGraph<CustomWrappedStack>(); graph = new WeightedDirectedGraph<CustomWrappedStack>();
init(); init();
@ -32,15 +36,40 @@ public class DynEMC {
} }
private void init() { private void init() {
@SuppressWarnings("unused")
RecipeRegistry recipeManager = RecipeRegistry.getInstance();
populateGraph(); populateGraph();
//printDebugDump();
} }
private void 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();
CustomWrappedStack recipeOutput = null;
while (recipeKeySetIterator.hasNext()) {
recipeOutput = recipeKeySetIterator.next();
for (List<CustomWrappedStack> recipeInputs : recipeMappings.get(recipeOutput)) {
for (CustomWrappedStack recipeInput : recipeInputs) {
// Unwrapped the wrapped stacks so that we actually find them in the graph
CustomWrappedStack unWrappedRecipeOutput = new CustomWrappedStack(recipeOutput.getWrappedStack());
CustomWrappedStack unWrappedRecipeInput = new CustomWrappedStack(recipeInput.getWrappedStack());
if (recipeOutput.getStackSize() != 0) {
graph.addEdge(unWrappedRecipeOutput, unWrappedRecipeInput, (recipeInput.getStackSize() * 1.0f) / recipeOutput.getStackSize());
}
}
}
}
} }
public int size() { public int size() {
@ -50,6 +79,15 @@ public class DynEMC {
public void printDebugDump() { public void printDebugDump() {
LogHelper.debug("Total node count: " + graph.getAllNodes().size());
LogHelper.debug("Critical node count: " + graph.getCriticalNodes().size());
LogHelper.debug("Orphan node count: " + graph.getOrphanNodes().size());
List<CustomWrappedStack> critsMinusOrphans = graph.getCriticalNodes();
critsMinusOrphans.removeAll(graph.getOrphanNodes());
LogHelper.debug("[Critical - Orphans] node count: " + critsMinusOrphans.size());
LogHelper.debug("***** START NODES *****"); LogHelper.debug("***** START NODES *****");
Iterator<CustomWrappedStack> nodeIter = graph.iterator(); Iterator<CustomWrappedStack> nodeIter = graph.iterator();
while (nodeIter.hasNext()) { while (nodeIter.hasNext()) {

View file

@ -13,6 +13,8 @@ import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.logging.Logger; import java.util.logging.Logger;
import com.pahimar.ee3.core.util.LogHelper;
public class WeightedDirectedGraph<T> implements Iterable<T> { public class WeightedDirectedGraph<T> implements Iterable<T> {
private final Map<T, SortedSet<WeightedEdge<T>>> graph = new HashMap<T, SortedSet<WeightedEdge<T>>>(); private final Map<T, SortedSet<WeightedEdge<T>>> graph = new HashMap<T, SortedSet<WeightedEdge<T>>>();
@ -46,10 +48,10 @@ public class WeightedDirectedGraph<T> implements Iterable<T> {
if (!(graph.containsKey(from) && graph.containsKey(to))) { if (!(graph.containsKey(from) && graph.containsKey(to))) {
if (!graph.containsKey(from)) { if (!graph.containsKey(from)) {
LOGGER.severe("From node doesn't exist: " + from.toString()); LogHelper.severe("From node doesn't exist: " + from.toString());
} }
if (!graph.containsKey(to)) { if (!graph.containsKey(to)) {
LOGGER.severe("To node doesn't exist: " + to.toString()); LogHelper.severe("To node doesn't exist: " + to.toString());
} }
throw new NoSuchElementException("Missing nodes from graph"); throw new NoSuchElementException("Missing nodes from graph");
} }