4444import org .scijava .function .Computers ;
4545import org .scijava .function .Container ;
4646import org .scijava .function .Mutable ;
47- import org .scijava .ops .api .Hints ;
48- import org .scijava .ops .api .OpEnvironment ;
49- import org .scijava .ops .api .OpInfo ;
50- import org .scijava .ops .api .OpMatchingException ;
51- import org .scijava .ops .api .OpRequest ;
52- import org .scijava .ops .api .RichOp ;
47+ import org .scijava .ops .api .*;
5348import org .scijava .ops .engine .BaseOpHints ;
5449import org .scijava .ops .engine .util .Infos ;
5550import org .scijava .ops .engine .util .internal .AnnotationUtils ;
@@ -203,19 +198,61 @@ public static SimplifiedOpInfo simplifyInfo(OpEnvironment env, OpInfo info) {
203198 outSimplifier = org .scijava .ops .api .Ops .rich (identity );
204199 }
205200
206- RichOp <Computers .Arity1 <?, ?>> copyOp = null ;
201+ RichOp <Computers .Arity1 <?, ?>> copyOp = getCopyOp (env , info , inFocusers ,
202+ outSimplifier , h );
203+ return new SimplifiedOpInfo (info , inFocusers , outSimplifier , copyOp );
204+ }
205+
206+ /**
207+ * Helper method that finds the {@code engine.copy} Op needed for a
208+ * {@link SimplifiedOpInfo}
209+ *
210+ * @param env the {@link OpEnvironment} containing {@code engine.copy} Ops.
211+ * @param info the original {@link OpInfo}.
212+ * @param inFocusers the {@code engine.focus} Ops used to focus the inputs to
213+ * the simplified Op
214+ * @param outSimplifier the {@code engine.simplify} Op used to simplify the
215+ * output for the simplified Op
216+ * @param h {@link Hints} to be used in matching the copy Op.
217+ * @return a {@code engine.copy} Op
218+ */
219+ private static RichOp <Computers .Arity1 <?, ?>> getCopyOp ( //
220+ OpEnvironment env , //
221+ OpInfo info , //
222+ List <RichOp <Function <?, ?>>> inFocusers , //
223+ RichOp <Function <?, ?>> outSimplifier , //
224+ Hints h //
225+ ) {
207226 int ioIndex = SimplificationUtils .findMutableArgIndex (Types .raw (info
208227 .opType ()));
209- if (ioIndex > -1 ) {
210- var nil = Nil .of (outType (info .outputType (), outSimplifier ));
211- var copier = env .unary ("engine.copy" , h ).inType (nil ).outType (nil )
212- .computer ();
213- copyOp = org .scijava .ops .api .Ops .rich (copier );
228+ // If IO index is -1, output is returned - no need to copy.
229+ if (ioIndex == -1 ) {
230+ return null ;
214231 }
215- return new SimplifiedOpInfo (info , inFocusers , outSimplifier , copyOp );
232+ // Otherwise, we need an Op to convert the simple output back into the
233+ // pre-focused input
234+ // Determine simple output type.
235+ var simpleOut = outType (info .outputType (), outSimplifier );
236+ // Determine unfocused input type.
237+ var focuserInfo = Ops .info (inFocusers .get (ioIndex ));
238+ Map <TypeVariable <?>, Type > typeAssigns = new HashMap <>();
239+ GenericAssignability .inferTypeVariables ( //
240+ new Type [] { focuserInfo .outputType () }, //
241+ new Type [] { info .inputTypes ().get (ioIndex ) }, //
242+ typeAssigns //
243+ );
244+ var unfocusedInput = Nil .of (Types .mapVarToTypes ( //
245+ focuserInfo .inputTypes ().get (0 ), //
246+ typeAssigns //
247+ ));
248+ // Match a copier
249+ return Ops .rich (env .unary ("engine.copy" , h ) //
250+ .inType (simpleOut ) //
251+ .outType (unfocusedInput ) //
252+ .computer ());
216253 }
217254
218- private static Type outType (Type originalOutput ,
255+ private static Nil <?> outType (Type originalOutput ,
219256 RichOp <Function <?, ?>> outputSimplifier )
220257 {
221258 Map <TypeVariable <?>, Type > typeAssigns = new HashMap <>();
@@ -225,8 +262,9 @@ private static Type outType(Type originalOutput,
225262 new Type [] { originalOutput }, //
226263 typeAssigns //
227264 );
228- return Types .mapVarToTypes (org .scijava .ops .api .Ops .info (outputSimplifier )
229- .outputType (), typeAssigns );
265+ Type outType = Types .mapVarToTypes (org .scijava .ops .api .Ops .info (
266+ outputSimplifier ).outputType (), typeAssigns );
267+ return Nil .of (outType );
230268 }
231269
232270 /**
0 commit comments