Skip to content

Commit b0217f2

Browse files
committed
Merge pull request #547 from JLLeitschuh/feat/dynamicTypedOperations
Adds Dynamic Typed Operations Valve & Switch
2 parents 3ae952b + 0ef2e39 commit b0217f2

File tree

120 files changed

+939
-324
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

+939
-324
lines changed

buildSrc/src/main/java/edu/wpi/gripgenerator/templates/Operation.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525

2626
public class Operation {
2727
private static final ImportDeclaration OPERATION_IMPORT = new ImportDeclaration(new NameExpr("edu.wpi.grip.core.operations.opencv.CVOperation"), false, false);
28-
private static final ImportDeclaration INPUT_SOCKET_IMPORT = new ImportDeclaration(new NameExpr("edu.wpi.grip.core.InputSocket"), false, false);
29-
private static final ImportDeclaration OUTPUT_SOCKET_IMPORT = new ImportDeclaration(new NameExpr("edu.wpi.grip.core.OutputSocket"), false, false);
28+
private static final ImportDeclaration SOCKETS_IMPORT = new ImportDeclaration(new NameExpr("edu.wpi.grip.core.sockets"), false, true);
3029
private static final ImportDeclaration EVENT_BUS_IMPORT = new ImportDeclaration(new NameExpr("com.google.common.eventbus.EventBus"), false, false);
3130
private static final ImportDeclaration CV_CORE_IMPORT = new ImportDeclaration(new NameExpr("org.bytedeco.javacpp.opencv_core"), true, true);
3231
private static final ClassOrInterfaceType iOperation = new ClassOrInterfaceType("CVOperation");
@@ -309,11 +308,9 @@ private ClassOrInterfaceDeclaration getClassDeclaration() {
309308
*/
310309
public CompilationUnit getDeclaration() {
311310
Set<ImportDeclaration> importList = Sets.newHashSet(
312-
SocketHintDeclaration.SOCKET_IMPORT,
313311
OPERATION_IMPORT,
314312
CV_CORE_IMPORT,
315-
INPUT_SOCKET_IMPORT,
316-
OUTPUT_SOCKET_IMPORT,
313+
SOCKETS_IMPORT,
317314
EVENT_BUS_IMPORT
318315
);
319316
importList.addAll(getAdditionalImports());

buildSrc/src/main/java/edu/wpi/gripgenerator/templates/SocketHintDeclaration.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package edu.wpi.gripgenerator.templates;
22

33
import com.github.javaparser.ASTHelper;
4-
import com.github.javaparser.ast.ImportDeclaration;
54
import com.github.javaparser.ast.body.*;
65
import com.github.javaparser.ast.expr.*;
76
import com.github.javaparser.ast.type.ClassOrInterfaceType;
@@ -32,8 +31,6 @@
3231
public class SocketHintDeclaration {
3332
public static final String SOCKET_HINT_CLASS_NAME = "SocketHint";
3433
public static final String SOCKET_HINT_BUILDER_CLASS_NAME = "Builder";
35-
public static final ImportDeclaration SOCKET_IMPORT = new ImportDeclaration(
36-
new NameExpr("edu.wpi.grip.core." + SOCKET_HINT_CLASS_NAME), false, false);
3734
public static final String HINT_POSTFIX = "Hint";
3835
public static final String INPUT_POSTFIX = "Input";
3936
public static final String OUTPUT_POSTFIX = "Output";

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.google.inject.assistedinject.Assisted;
77
import com.thoughtworks.xstream.annotations.XStreamAlias;
88
import edu.wpi.grip.core.events.*;
9+
import edu.wpi.grip.core.sockets.InputSocket;
10+
import edu.wpi.grip.core.sockets.OutputSocket;
911

1012
import static com.google.common.base.Preconditions.checkArgument;
1113

@@ -20,21 +22,21 @@ public class Connection<T> {
2022
private final InputSocket<T> inputSocket;
2123

2224

23-
public interface Factory <T> {
25+
public interface Factory<T> {
2426
Connection<T> create(OutputSocket<? extends T> outputSocket, InputSocket<T> inputSocket);
2527
}
2628

2729
/**
28-
* @param pipeline The pipeline to create the connection inside of.
29-
* @param outputSocket The socket to listen for changes in.
30-
* @param inputSocket A different socket to update when a change occurs in the first.
30+
* @param connectionValidator An object to validate that the connection can be made
31+
* @param outputSocket The socket to listen for changes in.
32+
* @param inputSocket A different socket to update when a change occurs in the first.
3133
*/
3234
@Inject
33-
Connection(EventBus eventBus, Pipeline pipeline, @Assisted OutputSocket<? extends T> outputSocket, @Assisted InputSocket<T> inputSocket) {
35+
Connection(EventBus eventBus, ConnectionValidator connectionValidator, @Assisted OutputSocket<? extends T> outputSocket, @Assisted InputSocket<T> inputSocket) {
3436
this.eventBus = eventBus;
3537
this.outputSocket = outputSocket;
3638
this.inputSocket = inputSocket;
37-
checkArgument(pipeline.canConnect(outputSocket, inputSocket), "Cannot connect sockets");
39+
checkArgument(connectionValidator.canConnect(outputSocket, inputSocket), "Cannot connect sockets");
3840
}
3941

4042
public OutputSocket<? extends T> getOutputSocket() {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package edu.wpi.grip.core;
2+
3+
import edu.wpi.grip.core.sockets.InputSocket;
4+
import edu.wpi.grip.core.sockets.OutputSocket;
5+
import edu.wpi.grip.core.sockets.Socket;
6+
7+
/**
8+
* Allows {@link Connection Connections} to be validated to ensure that the pipeline will remain valid.
9+
*/
10+
public interface ConnectionValidator {
11+
12+
/**
13+
* Resolves which {@link Socket} is the {@link OutputSocket} and which is the {@link InputSocket}
14+
* and calls {@link #canConnect(OutputSocket, InputSocket)}
15+
* @param socket1 The first socket
16+
* @param socket2 The second socket
17+
* @return The return value of {@link #canConnect(OutputSocket, InputSocket)}
18+
*/
19+
default boolean canConnect(Socket socket1, Socket socket2) {
20+
final OutputSocket<?> outputSocket;
21+
final InputSocket<?> inputSocket;
22+
23+
// One socket must be an input and one must be an output
24+
if (socket1.getDirection() == socket2.getDirection()) {
25+
return false;
26+
}
27+
28+
if (socket1.getDirection().equals(Socket.Direction.OUTPUT)) {
29+
outputSocket = (OutputSocket) socket1;
30+
inputSocket = (InputSocket) socket2;
31+
} else {
32+
inputSocket = (InputSocket) socket1;
33+
outputSocket = (OutputSocket) socket2;
34+
}
35+
36+
// DO NOT DO ANY OTHER SORT OF VALIDATION HERE. We just want to resolve the types.
37+
38+
return canConnect(outputSocket, inputSocket);
39+
}
40+
41+
/**
42+
* Determines if an output socket can be connected to an input socket
43+
* @param outputSocket The output socket to connect to the input socket
44+
* @param inputSocket The input socket to accept the output value of the output socket
45+
* @return True if a valid connection can be made from these two Sockets
46+
*/
47+
boolean canConnect(OutputSocket<?> outputSocket, InputSocket<?> inputSocket);
48+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
103103
install(new FactoryModuleBuilder().build(new TypeLiteral<Connection.Factory<Object>>() {
104104
}));
105105

106-
106+
bind(ConnectionValidator.class).to(Pipeline.class);
107107
bind(Source.SourceFactory.class).to(Source.SourceFactoryImpl.class);
108108
bind(CameraSource.FrameGrabberFactory.class).to(CameraSource.FrameGrabberFactoryImpl.class);
109109
install(new FactoryModuleBuilder()

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.google.common.collect.ImmutableSet;
44
import com.google.common.eventbus.EventBus;
5+
import edu.wpi.grip.core.sockets.InputSocket;
6+
import edu.wpi.grip.core.sockets.OutputSocket;
7+
import edu.wpi.grip.core.sockets.SocketsProvider;
58

69
import java.io.InputStream;
710
import java.util.Optional;
@@ -16,6 +19,7 @@ enum Category {
1619
IMAGE_PROCESSING,
1720
FEATURE_DETECTION,
1821
NETWORK,
22+
LOGICAL,
1923
OPENCV,
2024
MISCELLANEOUS,
2125
}
@@ -53,6 +57,9 @@ default Optional<InputStream> getIcon() {
5357
return Optional.empty();
5458
}
5559

60+
default SocketsProvider createSockets(EventBus eventBus) {
61+
return new SocketsProvider(createInputSockets(eventBus), createOutputSockets(eventBus));
62+
}
5663
/**
5764
* @param eventBus The Guava {@link EventBus} used by the application.
5865
* @return An array of sockets for the inputs that the operation expects.

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import com.thoughtworks.xstream.annotations.XStreamOmitField;
99
import edu.wpi.grip.core.events.*;
1010
import edu.wpi.grip.core.settings.ProjectSettings;
11+
import edu.wpi.grip.core.sockets.InputSocket;
12+
import edu.wpi.grip.core.sockets.OutputSocket;
13+
import edu.wpi.grip.core.sockets.SocketHint;
1114

1215
import javax.inject.Inject;
1316
import java.util.*;
@@ -30,7 +33,7 @@
3033
*/
3134
@Singleton
3235
@XStreamAlias(value = "grip:Pipeline")
33-
public class Pipeline {
36+
public class Pipeline implements ConnectionValidator {
3437

3538
@Inject
3639
@XStreamOmitField
@@ -176,28 +179,13 @@ public ProjectSettings getProjectSettings() {
176179
* @return true if a connection can be made from the given output socket to the given input socket
177180
*/
178181
@SuppressWarnings("unchecked")
179-
public boolean canConnect(Socket socket1, Socket socket2) {
180-
final OutputSocket<?> outputSocket;
181-
final InputSocket<?> inputSocket;
182-
183-
// One socket must be an input and one must be an output
184-
if (socket1.getDirection() == socket2.getDirection()) {
185-
return false;
186-
}
187-
188-
if (socket1.getDirection().equals(Socket.Direction.OUTPUT)) {
189-
outputSocket = (OutputSocket) socket1;
190-
inputSocket = (InputSocket) socket2;
191-
} else {
192-
inputSocket = (InputSocket) socket1;
193-
outputSocket = (OutputSocket) socket2;
194-
}
195-
196-
final SocketHint outputHint = socket1.getSocketHint();
197-
final SocketHint inputHint = socket2.getSocketHint();
182+
@Override
183+
public boolean canConnect(OutputSocket<?> outputSocket, InputSocket<?> inputSocket) {
184+
final SocketHint outputHint = outputSocket.getSocketHint();
185+
final SocketHint inputHint = inputSocket.getSocketHint();
198186

199187
// The input socket must be able to hold the type of value that the output socket contains
200-
if (!inputHint.getType().isAssignableFrom(outputHint.getType())) {
188+
if (!inputHint.isCompatibleWith(outputHint)) {
201189
return false;
202190
}
203191

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

Lines changed: 0 additions & 144 deletions
This file was deleted.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package edu.wpi.grip.core;
22

33
import com.google.inject.Inject;
4+
import edu.wpi.grip.core.sockets.OutputSocket;
45
import edu.wpi.grip.core.sources.CameraSource;
56
import edu.wpi.grip.core.sources.ImageFileSource;
67
import edu.wpi.grip.core.sources.MultiImageFileSource;

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import com.google.inject.Inject;
55
import com.google.inject.Singleton;
66
import com.thoughtworks.xstream.annotations.XStreamAlias;
7+
import edu.wpi.grip.core.sockets.InputSocket;
8+
import edu.wpi.grip.core.sockets.OutputSocket;
9+
import edu.wpi.grip.core.sockets.Socket;
10+
import edu.wpi.grip.core.sockets.SocketsProvider;
711
import edu.wpi.grip.core.util.ExceptionWitness;
812

913
import java.util.Optional;
@@ -44,8 +48,9 @@ public Factory(EventBus eventBus, ExceptionWitness.Factory exceptionWitnessFacto
4448
public Step create(Operation operation) {
4549
checkNotNull(operation, "The operation can not be null");
4650
// Create the list of input and output sockets, and mark this step as their owner.
47-
final InputSocket<?>[] inputSockets = operation.createInputSockets(eventBus);
48-
final OutputSocket<?>[] outputSockets = operation.createOutputSockets(eventBus);
51+
final SocketsProvider socketsProvider = operation.createSockets(eventBus);
52+
final InputSocket<?>[] inputSockets = socketsProvider.inputSockets();
53+
final OutputSocket<?>[] outputSockets = socketsProvider.outputSockets();
4954

5055
final Step step = new Step(
5156
operation,

0 commit comments

Comments
 (0)