66import com .google .inject .Singleton ;
77import com .thoughtworks .xstream .annotations .XStreamAlias ;
88import edu .wpi .grip .core .events .SocketChangedEvent ;
9+ import edu .wpi .grip .core .util .ExceptionWitness ;
910
10- import java .util .NoSuchElementException ;
1111import java .util .Optional ;
1212import java .util .logging .Level ;
1313import java .util .logging .Logger ;
2020 */
2121@ XStreamAlias (value = "grip:Step" )
2222public class Step {
23+ private static final Logger logger = Logger .getLogger (Step .class .getName ());
24+ private static final String MISSING_SOCKET_MESSAGE_END = " must have a value to run this step." ;
2325
24- private final Logger logger = Logger . getLogger ( Step . class . getName ()) ;
26+ private final ExceptionWitness witness ;
2527
2628 private final Operation operation ;
2729 private final InputSocket <?>[] inputSockets ;
2830 private final OutputSocket <?>[] outputSockets ;
2931 private final Optional <?> data ;
3032
33+
3134 @ Singleton
3235 public static class Factory {
3336 private final EventBus eventBus ;
37+ private final ExceptionWitness .Factory exceptionWitnessFactory ;
3438
3539 @ Inject
36- public Factory (EventBus eventBus ) {
40+ public Factory (EventBus eventBus , ExceptionWitness . Factory exceptionWitnessFactory ) {
3741 this .eventBus = eventBus ;
42+ this .exceptionWitnessFactory = exceptionWitnessFactory ;
3843 }
3944
4045 public Step create (Operation operation ) {
@@ -51,7 +56,13 @@ public Step create(Operation operation) {
5156 eventBus .register (socket );
5257 }
5358
54- final Step step = new Step (operation , inputSockets , outputSockets , operation .createData ());
59+ final Step step = new Step (
60+ operation ,
61+ inputSockets ,
62+ outputSockets ,
63+ operation .createData (),
64+ exceptionWitnessFactory
65+ );
5566 eventBus .register (step );
5667 for (Socket <?> socket : inputSockets ) {
5768 socket .setStep (Optional .of (step ));
@@ -66,16 +77,22 @@ public Step create(Operation operation) {
6677 }
6778
6879 /**
69- * @param operation The operation that is performed at this step.
70- * @param inputSockets The input sockets from the operation.
71- * @param outputSockets The output sockets provided by the operation.
72- * @param data The data provided by the operation.
80+ * @param operation The operation that is performed at this step.
81+ * @param inputSockets The input sockets from the operation.
82+ * @param outputSockets The output sockets provided by the operation.
83+ * @param data The data provided by the operation.
84+ * @param exceptionWitnessFactory A factory used to create an {@link ExceptionWitness}
7385 */
74- Step (Operation operation , InputSocket <?>[] inputSockets , OutputSocket <?>[] outputSockets , Optional <?> data ) {
86+ Step (Operation operation ,
87+ InputSocket <?>[] inputSockets ,
88+ OutputSocket <?>[] outputSockets ,
89+ Optional <?> data ,
90+ ExceptionWitness .Factory exceptionWitnessFactory ) {
7591 this .operation = operation ;
7692 this .inputSockets = inputSockets ;
7793 this .outputSockets = outputSockets ;
7894 this .data = data ;
95+ this .witness = exceptionWitnessFactory .create (this );
7996 }
8097
8198 /**
@@ -115,25 +132,25 @@ private void resetOutputSockets() {
115132 * default values.
116133 */
117134 private synchronized void runPerformIfPossible () {
118- try {
119- for ( InputSocket <?> inputSocket : inputSockets ) {
120- inputSocket .getValue ()
121- . orElseThrow (() -> new NoSuchElementException (
122- inputSocket . getSocketHint (). getIdentifier () + " must have a value to run this step."
123- ));
135+ for ( InputSocket <?> inputSocket : inputSockets ) {
136+ // If there is a socket that isn't present then we have a problem.
137+ if (! inputSocket .getValue (). isPresent ()) {
138+ witness . flagWarning ( inputSocket . getSocketHint (). getIdentifier () + MISSING_SOCKET_MESSAGE_END );
139+ resetOutputSockets ();
140+ return ; /* Only run the perform method if all of the input sockets are present. */
124141 }
125- } catch (NoSuchElementException e ) {
126- //TODO: show warning icon
127- resetOutputSockets ();
128- return ; /* Only run the perform method if all of the input sockets are present. */
129142 }
130143
131144 try {
132145 this .operation .perform (inputSockets , outputSockets , data );
133146 } catch (Exception e ) {
134- logger .log (Level .WARNING , e .getMessage (), e );
147+ final String operationFailedMessage = "The " + operation .getName () + " operation did not perform correctly." ;
148+ logger .log (Level .WARNING , operationFailedMessage , e );
149+ witness .flagException (e , operationFailedMessage );
135150 resetOutputSockets ();
151+ return ;
136152 }
153+ witness .clearException ();
137154 }
138155
139156 @ Subscribe
0 commit comments