summaryrefslogtreecommitdiff
path: root/opendc-simulator/opendc-simulator-flow/src/main
diff options
context:
space:
mode:
authorDante Niewenhuis <d.niewenhuis@hotmail.com>2024-11-26 11:09:21 +0100
committerGitHub <noreply@github.com>2024-11-26 11:09:21 +0100
commitec73210b675fd90568c5193e6ae6ef82ce81be6c (patch)
tree89b530b53898752f7800e5109548412b84fcd375 /opendc-simulator/opendc-simulator-flow/src/main
parent698a64615d0eef8994fc1eaf0a3b71da194e1dcd (diff)
Streamlined the FlowNetwork for better performance (#273)
Diffstat (limited to 'opendc-simulator/opendc-simulator-flow/src/main')
-rw-r--r--opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/Multiplexer.java24
-rw-r--r--opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/engine/FlowEdge.java8
2 files changed, 23 insertions, 9 deletions
diff --git a/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/Multiplexer.java b/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/Multiplexer.java
index 25dc564f..dd4c2d11 100644
--- a/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/Multiplexer.java
+++ b/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/Multiplexer.java
@@ -31,11 +31,11 @@ import org.opendc.simulator.engine.FlowNode;
import org.opendc.simulator.engine.FlowSupplier;
public class Multiplexer extends FlowNode implements FlowSupplier, FlowConsumer {
- private ArrayList<FlowEdge> consumerEdges = new ArrayList<>();
+ private final ArrayList<FlowEdge> consumerEdges = new ArrayList<>();
private FlowEdge supplierEdge;
- private ArrayList<Double> demands = new ArrayList<>(); // What is demanded by the consumers
- private ArrayList<Double> supplies = new ArrayList<>(); // What is supplied to the consumers
+ private final ArrayList<Double> demands = new ArrayList<>(); // What is demanded by the consumers
+ private final ArrayList<Double> supplies = new ArrayList<>(); // What is supplied to the consumers
private double totalDemand; // The total demand of all the consumers
private double totalSupply; // The total supply from the supplier
@@ -180,13 +180,19 @@ public class Multiplexer extends FlowNode implements FlowSupplier, FlowConsumer
demands.set(idx, newDemand);
this.totalDemand += (newDemand - prevDemand);
- this.invalidate();
+
+ if (this.totalDemand <= this.capacity) {
+
+ this.totalSupply = this.totalDemand;
+ this.pushDemand(this.supplierEdge, this.totalSupply);
+
+ this.pushSupply(consumerEdge, newDemand);
+ }
+ // TODO: add behaviour if capacity is reached
}
@Override
- public void handleSupply(FlowEdge supplierEdge, double newSupply) {
- this.invalidate();
- }
+ public void handleSupply(FlowEdge supplierEdge, double newSupply) {}
@Override
public void pushDemand(FlowEdge supplierEdge, double newDemand) {
@@ -201,6 +207,10 @@ public class Multiplexer extends FlowNode implements FlowSupplier, FlowConsumer
System.out.println("Error (Multiplexer): pushing supply to an unknown consumer");
}
+ if (supplies.get(idx) == newSupply) {
+ return;
+ }
+
supplies.set(idx, newSupply);
consumerEdge.pushSupply(newSupply);
}
diff --git a/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/engine/FlowEdge.java b/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/engine/FlowEdge.java
index d89740a2..d99cd78e 100644
--- a/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/engine/FlowEdge.java
+++ b/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/engine/FlowEdge.java
@@ -90,19 +90,23 @@ public class FlowEdge {
* Push new demand from the Consumer to the Supplier
*/
public void pushDemand(double newDemand) {
+ if (newDemand == this.demand) {
+ return;
+ }
this.demand = newDemand;
this.supplier.handleDemand(this, newDemand);
- ((FlowNode) this.supplier).invalidate();
}
/**
* Push new supply from the Supplier to the Consumer
*/
public void pushSupply(double newSupply) {
+ if (newSupply == this.supply) {
+ return;
+ }
this.supply = newSupply;
this.consumer.handleSupply(this, newSupply);
- ((FlowNode) this.consumer).invalidate();
}
}