Skip to content

Commit 347db13

Browse files
SamCarlbergJLLeitschuh
authored andcommitted
Refactor Operation Interface
- Operations now use local state variables for input and output sockets rather than regenerating them on the fly - Moved name, description, category, icon, and aliases to a separate description object - Socket classes are now interfaces - Refactors Generated Operations to be in source code - Changed socket get methods to return List<FooSocket> instead of FooSocket<?>[]
1 parent 0a5446b commit 347db13

File tree

120 files changed

+4017
-2457
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+4017
-2457
lines changed

buildSrc/src/main/java/edu/wpi/gripgenerator/FileParser.java

Lines changed: 117 additions & 121 deletions
Large diffs are not rendered by default.

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ public void removeConnection(StepRemovedEvent e) {
7676
// Remove this connection if one of the steps it was connected to was removed
7777
for (OutputSocket socket : e.getStep().getOutputSockets()) {
7878
if (socket == this.outputSocket) {
79-
this.eventBus.post(new ConnectionRemovedEvent(this));
79+
this.remove();
8080
return;
8181
}
8282
}
8383

8484
for (InputSocket socket : e.getStep().getInputSockets()) {
8585
if (socket == this.inputSocket) {
86-
this.eventBus.post(new ConnectionRemovedEvent(this));
86+
this.remove();
8787
return;
8888
}
8989
}
@@ -94,9 +94,16 @@ public void removeConnection(SourceRemovedEvent e) {
9494
// Remove this connection if it's from a source that was removed
9595
for (OutputSocket socket : e.getSource().getOutputSockets()) {
9696
if (socket == this.outputSocket) {
97-
this.eventBus.post(new ConnectionRemovedEvent(this));
97+
this.remove();
9898
return;
9999
}
100100
}
101101
}
102+
103+
/**
104+
* Removes this connection from the pipeline.
105+
*/
106+
public void remove() {
107+
eventBus.post(new ConnectionRemovedEvent(this));
108+
}
102109
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
import edu.wpi.grip.core.events.UnexpectedThrowableEvent;
1313
import edu.wpi.grip.core.serialization.Project;
1414
import edu.wpi.grip.core.settings.SettingsProvider;
15+
import edu.wpi.grip.core.sockets.InputSocket;
16+
import edu.wpi.grip.core.sockets.InputSocketImpl;
17+
import edu.wpi.grip.core.sockets.OutputSocket;
18+
import edu.wpi.grip.core.sockets.OutputSocketImpl;
1519
import edu.wpi.grip.core.sources.CameraSource;
1620
import edu.wpi.grip.core.sources.ImageFileSource;
1721
import edu.wpi.grip.core.sources.MultiImageFileSource;
@@ -109,6 +113,9 @@ public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
109113

110114
bind(ConnectionValidator.class).to(Pipeline.class);
111115
bind(Source.SourceFactory.class).to(Source.SourceFactoryImpl.class);
116+
117+
bind(InputSocket.Factory.class).to(InputSocketImpl.FactoryImpl.class);
118+
bind(OutputSocket.Factory.class).to(OutputSocketImpl.FactoryImpl.class);
112119
install(new FactoryModuleBuilder()
113120
.implement(CameraSource.class, CameraSource.class)
114121
.build(CameraSource.Factory.class));

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
import com.google.inject.Injector;
77
import edu.wpi.grip.core.events.ExceptionClearedEvent;
88
import edu.wpi.grip.core.events.ExceptionEvent;
9+
import edu.wpi.grip.core.operations.CVOperations;
910
import edu.wpi.grip.core.operations.Operations;
1011
import edu.wpi.grip.core.operations.network.GRIPNetworkModule;
1112
import edu.wpi.grip.core.serialization.Project;
1213
import edu.wpi.grip.core.sources.GRIPSourcesHardwareModule;
13-
import edu.wpi.grip.generated.CVOperations;
1414

1515
import javax.inject.Inject;
1616
import java.io.File;
1717
import java.io.IOException;
1818
import java.util.logging.Level;
1919
import java.util.logging.Logger;
2020

21+
2122
/**
2223
* Main driver class for headless mode
2324
*/
@@ -27,6 +28,7 @@ public class Main {
2728
@Inject private PipelineRunner pipelineRunner;
2829
@Inject private EventBus eventBus;
2930
@Inject private Operations operations;
31+
@Inject private CVOperations cvOperations;
3032
@Inject private Logger logger;
3133

3234
@SuppressWarnings("PMD.SystemPrintln")
@@ -45,7 +47,7 @@ public void start(String[] args) throws IOException, InterruptedException {
4547
}
4648

4749
operations.addOperations();
48-
CVOperations.addOperations(eventBus);
50+
cvOperations.addOperations();
4951

5052
final String projectPath = args[0];
5153

Lines changed: 14 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,40 @@
11
package edu.wpi.grip.core;
22

3-
import com.google.common.collect.ImmutableSet;
4-
import com.google.common.eventbus.EventBus;
53
import edu.wpi.grip.core.sockets.InputSocket;
64
import edu.wpi.grip.core.sockets.OutputSocket;
7-
import edu.wpi.grip.core.sockets.SocketsProvider;
85

9-
import java.io.InputStream;
10-
import java.util.Optional;
6+
import java.util.List;
117

128
/**
13-
* The common interface used by <code>Step</code>s in a pipeline to call various operations. There is usually only one
14-
* instance of any class that implements <code>Operation</code>, which is called whenever that operation is used.
9+
* The common interface used by <code>Step</code>s in a pipeline to call various operations. Each instance of an
10+
* operation in the pipeline is handled by a unique instance of that {@code Operation} class.
1511
*/
1612
public interface Operation {
1713

18-
enum Category {
19-
IMAGE_PROCESSING,
20-
FEATURE_DETECTION,
21-
NETWORK,
22-
LOGICAL,
23-
OPENCV,
24-
MISCELLANEOUS,
25-
}
26-
27-
/**
28-
* @return The unique user-facing name of the operation, such as "Gaussian Blur"
29-
*/
30-
String getName();
31-
32-
/**
33-
* @return Any old unique user-facing names of the operation. This is used to preserve compatibility with
34-
* old versions of GRIP if the operation name changes.
35-
*/
36-
default ImmutableSet<String> getAliases() {
37-
return ImmutableSet.of();
38-
}
39-
40-
41-
/**
42-
* @return A description of the operation.
43-
*/
44-
String getDescription();
45-
46-
/**
47-
* @return What category the operation falls under. This is used to organize them in the GUI
48-
*/
49-
default Category getCategory() {
50-
return Category.MISCELLANEOUS;
51-
}
52-
5314
/**
54-
* @return An {@link InputStream} of a 128x128 image to show the user as a representation of the operation.
55-
*/
56-
default Optional<InputStream> getIcon() {
57-
return Optional.empty();
58-
}
59-
60-
default SocketsProvider createSockets(EventBus eventBus) {
61-
return new SocketsProvider(createInputSockets(eventBus), createOutputSockets(eventBus));
62-
}
63-
/**
64-
* @param eventBus The Guava {@link EventBus} used by the application.
65-
* @return An array of sockets for the inputs that the operation expects.
66-
*/
67-
InputSocket<?>[] createInputSockets(EventBus eventBus);
68-
69-
/**
70-
* @param eventBus The Guava {@link EventBus} used by the application.
71-
* @return An array of sockets for the outputs that the operation produces.
15+
* @return A list of sockets for the inputs that the operation expects.
16+
*
17+
* @implNote The returned list should be immutable (i.e. read-only)
7218
*/
73-
OutputSocket<?>[] createOutputSockets(EventBus eventBus);
19+
List<InputSocket> getInputSockets();
7420

7521
/**
76-
* Override this to provide persistent per-step data
22+
* @return A list of sockets for the outputs that the operation produces.
23+
*
24+
* @implNote The returned list should be immutable (i.e. read-only)
7725
*/
78-
default Optional<?> createData() {
79-
return Optional.empty();
80-
}
26+
List<OutputSocket> getOutputSockets();
8127

8228
/**
83-
* Perform the operation on the specified inputs, storing the results in the specified outputs.
84-
*
85-
* @param inputs An array obtained from {@link #createInputSockets(EventBus)}. The caller can set the value of
86-
* each socket to an actual parameter for the operation.
87-
* @param outputs An array obtained from {@link #createOutputSockets(EventBus)}. The outputs of the operation will
88-
* be stored in these sockets.
89-
* @param data Optional data to be passed to the operation
29+
* Performs this {@code Operation}.
9030
*/
91-
default void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs, Optional<?> data) {
92-
perform(inputs, outputs);
93-
}
94-
95-
default void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs) {
96-
throw new UnsupportedOperationException("Perform was not overridden");
97-
}
31+
void perform();
9832

9933
/**
10034
* Allows the step to clean itself up when removed from the pipeline.
10135
* This should only be called by {@link Step#setRemoved()} to ensure correct synchronization.
102-
*
103-
* @param inputs An array obtained from {@link #createInputSockets(EventBus)}. The caller can set the value of
104-
* each socket to an actual parameter for the operation.
105-
* @param outputs An array obtained from {@link #createOutputSockets(EventBus)}. The outputs of the operation will
106-
* be stored in these sockets.
107-
* @param data Optional data to be passed to the operation
10836
*/
109-
default void cleanUp(InputSocket<?>[] inputs, OutputSocket<?>[] outputs, Optional<?> data) {
37+
default void cleanUp() {
11038
/* no-op */
11139
}
11240
}

0 commit comments

Comments
 (0)