support graph merging
parent
4ffc73e56d
commit
db063345c5
|
|
@ -63,10 +63,16 @@ public class Test {
|
||||||
System.out.println("\nSUM_reif");
|
System.out.println("\nSUM_reif");
|
||||||
Database.ReinforcedIronPlate.getRecipe().sum(Database.ReinforcedIronPlate, 100);
|
Database.ReinforcedIronPlate.getRecipe().sum(Database.ReinforcedIronPlate, 100);
|
||||||
System.out.println("\nSUM_screw");
|
System.out.println("\nSUM_screw");
|
||||||
plot2(Database.Screw.getRecipe().sum(Database.Screw, 100), "screw_sum");
|
Graph<Item, ProductionEdge> screws100 = Database.Screw.getRecipe().sum(Database.Screw, 100);
|
||||||
|
Graph<Item, ProductionEdge> computers100 = Database.Computer.getRecipe().sum(Database.Computer, 100);
|
||||||
|
|
||||||
|
plot2(screws100, "screw_sum");
|
||||||
System.out.println("\nSUM_ACU");
|
System.out.println("\nSUM_ACU");
|
||||||
plot2(Database.AdaptiveControlUnit.getRecipe().sum(Database.AdaptiveControlUnit, 1), "acu4");
|
plot2(Database.AdaptiveControlUnit.getRecipe().sum(Database.AdaptiveControlUnit, 1), "acu4");
|
||||||
|
|
||||||
|
|
||||||
|
plot2(Utils.merge(screws100, computers100), "merged");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void plot2(Graph<Item, ProductionEdge> sum, String filename) {
|
private static void plot2(Graph<Item, ProductionEdge> sum, String filename) {
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,26 @@ public class ProductionEdge {
|
||||||
return "(%.2f||%.2f)".formatted(totalRequired,instances);
|
return "(%.2f||%.2f)".formatted(totalRequired,instances);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getTotalRequired() {
|
||||||
|
return totalRequired;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTotalRequired(double totalRequired) {
|
||||||
|
this.totalRequired = totalRequired;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getInstances() {
|
||||||
|
return instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInstances(double instances) {
|
||||||
|
this.instances = instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasTarget(Item t){
|
||||||
|
return source.equals(t);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
|
|
@ -38,4 +58,7 @@ public class ProductionEdge {
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Item getTarget() {
|
||||||
|
return source;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,14 @@ import org.jgrapht.event.ConnectedComponentTraversalEvent;
|
||||||
import org.jgrapht.event.EdgeTraversalEvent;
|
import org.jgrapht.event.EdgeTraversalEvent;
|
||||||
import org.jgrapht.event.TraversalListener;
|
import org.jgrapht.event.TraversalListener;
|
||||||
import org.jgrapht.event.VertexTraversalEvent;
|
import org.jgrapht.event.VertexTraversalEvent;
|
||||||
|
import org.jgrapht.graph.DefaultDirectedWeightedGraph;
|
||||||
import org.jgrapht.graph.DefaultWeightedEdge;
|
import org.jgrapht.graph.DefaultWeightedEdge;
|
||||||
import org.jgrapht.graph.EdgeReversedGraph;
|
import org.jgrapht.graph.EdgeReversedGraph;
|
||||||
import org.jgrapht.traverse.DepthFirstIterator;
|
import org.jgrapht.traverse.DepthFirstIterator;
|
||||||
import org.jgrapht.traverse.GraphIterator;
|
import org.jgrapht.traverse.GraphIterator;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
@ -76,4 +78,30 @@ public class Utils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Graph<Item, ProductionEdge> merge(Graph<Item, ProductionEdge> graph0, Graph<Item, ProductionEdge> graph1) {
|
||||||
|
Graph<Item, ProductionEdge> result = new DefaultDirectedWeightedGraph<>(ProductionEdge.class);
|
||||||
|
|
||||||
|
graph0.vertexSet().forEach(result::addVertex);
|
||||||
|
graph0.edgeSet().forEach(productionEdge -> result.addEdge(graph0.getEdgeSource(productionEdge), graph0.getEdgeTarget(productionEdge), productionEdge));
|
||||||
|
|
||||||
|
graph1.vertexSet().forEach(result::addVertex);
|
||||||
|
graph1.edgeSet().forEach(productionEdge -> {
|
||||||
|
List<ProductionEdge> collect = result.edgeSet().stream().filter(productionEdge1 -> productionEdge1.hasTarget(productionEdge.getTarget())).collect(Collectors.toList());
|
||||||
|
collect.forEach(edge -> {
|
||||||
|
Item src = result.getEdgeSource(edge);
|
||||||
|
Item target = result.getEdgeTarget(edge);
|
||||||
|
Item target2 = graph1.getEdgeTarget(productionEdge);
|
||||||
|
if (target != target2) {
|
||||||
|
result.addEdge(src, target2, productionEdge);
|
||||||
|
} else {
|
||||||
|
result.removeEdge(edge);
|
||||||
|
result.addEdge(src, target, edge);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (collect.isEmpty()) {
|
||||||
|
result.addEdge(graph1.getEdgeSource(productionEdge), graph1.getEdgeTarget(productionEdge), productionEdge);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue