Skip to content

Commit 7e10ff0

Browse files
committed
Steps now only run when Input Socket are dirtied
1 parent b0217f2 commit 7e10ff0

File tree

5 files changed

+45
-18
lines changed

5 files changed

+45
-18
lines changed

core/src/main/java/edu/wpi/grip/core/Step.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,20 @@ private void resetOutputSockets() {
127127
* default values.
128128
*/
129129
protected final void runPerformIfPossible() {
130+
boolean anyDirty = false; // Keeps track of if there are sockets that are dirty
130131
for (InputSocket<?> inputSocket : inputSockets) {
131132
// If there is a socket that isn't present then we have a problem.
132133
if (!inputSocket.getValue().isPresent()) {
133134
witness.flagWarning(inputSocket.getSocketHint().getIdentifier() + MISSING_SOCKET_MESSAGE_END);
134135
resetOutputSockets();
135136
return; /* Only run the perform method if all of the input sockets are present. */
136137
}
138+
// If one value is true then this will stay true
139+
anyDirty |= inputSocket.dirtied();
140+
}
141+
if (!anyDirty) { // If there aren't any dirty inputs
142+
// Don't clear the exceptions just return
143+
return;
137144
}
138145

139146
try {

core/src/main/java/edu/wpi/grip/core/sockets/InputSocket.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
import com.thoughtworks.xstream.annotations.XStreamAlias;
55
import edu.wpi.grip.core.Operation;
66

7+
import java.util.concurrent.atomic.AtomicBoolean;
8+
79
/**
810
* Represents the input into an {@link Operation}.
911
*
1012
* @param <T> The type of the value that this socket stores
1113
*/
1214
@XStreamAlias(value = "grip:Input")
1315
public class InputSocket<T> extends Socket<T> {
14-
16+
private final AtomicBoolean dirty = new AtomicBoolean(false);
1517

1618
/**
1719
* @param eventBus The Guava {@link EventBus} used by the application.
@@ -21,6 +23,20 @@ public InputSocket(EventBus eventBus, SocketHint<T> socketHint) {
2123
super(eventBus, socketHint, Direction.INPUT);
2224
}
2325

26+
@Override
27+
protected void onValueChanged() {
28+
dirty.set(true);
29+
}
30+
31+
/**
32+
* Checks if the socket has been dirtied and rests it to false.
33+
*
34+
* @return True if the socket has been dirtied
35+
*/
36+
public boolean dirtied() {
37+
return dirty.compareAndSet(true, false);
38+
}
39+
2440
/**
2541
* {@inheritDoc}
2642
*/

core/src/main/java/edu/wpi/grip/core/sockets/OutputSocket.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
import edu.wpi.grip.core.Operation;
88
import edu.wpi.grip.core.events.SocketPreviewChangedEvent;
99

10-
import javax.annotation.Nullable;
11-
import java.util.Optional;
12-
1310
/**
1411
* Represents the output of an {@link Operation}.
1512
*
@@ -31,16 +28,6 @@ public OutputSocket(EventBus eventBus, SocketHint<T> socketHint) {
3128
super(eventBus, socketHint, Direction.OUTPUT);
3229
}
3330

34-
@Override
35-
public void setValueOptional(Optional<? extends T> optionalValue) {
36-
super.setValueOptional(optionalValue);
37-
}
38-
39-
@Override
40-
public void setValue(@Nullable T value) {
41-
super.setValue(value);
42-
}
43-
4431
/**
4532
* @param previewed If <code>true</code>, this socket will be shown in a preview in the GUI.
4633
*/

core/src/main/java/edu/wpi/grip/core/sockets/Socket.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ public SocketHint<T> getSocketHint() {
6161
*
6262
* @param optionalValue The optional value to assign this socket to.
6363
*/
64-
public synchronized void setValueOptional(Optional<? extends T> optionalValue) {
64+
public final synchronized void setValueOptional(Optional<? extends T> optionalValue) {
6565
checkNotNull(optionalValue, "The optional value can not be null");
6666
if (optionalValue.isPresent()) {
6767
getSocketHint().getType().cast(optionalValue.get());
6868
}
6969
this.value = optionalValue;
70+
onValueChanged();
7071
eventBus.post(new SocketChangedEvent(this));
7172
}
7273

@@ -75,9 +76,16 @@ public synchronized void setValueOptional(Optional<? extends T> optionalValue) {
7576
*
7677
* @param value The value to store in this socket. Nullable.
7778
*/
78-
public void setValue(@Nullable T value) {
79+
public final void setValue(@Nullable T value) {
7980
setValueOptional(Optional.ofNullable(this.getSocketHint().getType().cast(value)));
81+
}
8082

83+
/**
84+
* Called when the value for the socket is reassigned.
85+
* Can be used by an implementing class to change behaviour when the value is changed.
86+
*/
87+
protected void onValueChanged() {
88+
/* no-op */
8189
}
8290

8391
/**

core/src/test/java/edu/wpi/grip/core/PipelineRunnerTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import edu.wpi.grip.core.events.StopPipelineEvent;
1212
import edu.wpi.grip.core.sockets.InputSocket;
1313
import edu.wpi.grip.core.sockets.OutputSocket;
14+
import edu.wpi.grip.core.sockets.SocketHints;
1415
import edu.wpi.grip.core.util.MockExceptionWitness;
1516
import net.jodah.concurrentunit.Waiter;
1617
import org.junit.After;
@@ -98,10 +99,11 @@ public void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs) {
9899
class ExceptionEventReceiver {
99100
private int callCount = 0;
100101
private ExceptionEvent event;
102+
101103
@Subscribe
102104
public void onException(ExceptionEvent event) {
103105
this.event = event;
104-
callCount ++;
106+
callCount++;
105107
}
106108
}
107109
final ExceptionEventReceiver exceptionEventReceiver = new ExceptionEventReceiver();
@@ -334,7 +336,14 @@ default String getDescription() {
334336

335337
@Override
336338
default InputSocket<?>[] createInputSockets(EventBus eventBus) {
337-
return new InputSocket<?>[0];
339+
return new InputSocket<?>[]{
340+
new InputSocket<Boolean>(new EventBus(), SocketHints.createBooleanSocketHint("Test val", false)) {
341+
@Override
342+
public boolean dirtied() {
343+
return true;
344+
}
345+
}
346+
};
338347
}
339348

340349
@Override

0 commit comments

Comments
 (0)