Skip to content

Commit

Permalink
WIP: Better conversion output types
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Mar 22, 2024
1 parent 83253bc commit 14a20c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,11 @@ public OpCandidate findMatch(MatchingConditions conditions, OpMatcher matcher,
.hints()))
{
Conversions.tryConvert(env, info, request).ifPresent(converted -> {
Map<TypeVariable<?>, Type> map = new HashMap<>();
GenericAssignability.inferTypeVariables( //
new Type[] { converted.opType() }, //
new Type[] { request.getType() }, //
map //
);
candidates.add(new OpCandidate( //
env, //
request, //
converted, //
map //
converted.typeVarAssigns() //
));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.scijava.function.Mutable;
import org.scijava.ops.api.*;
import org.scijava.ops.engine.BaseOpHints;
import org.scijava.types.Any;
import org.scijava.types.Nil;
import org.scijava.types.Types;
import org.scijava.types.inference.FunctionalInterfaces;
Expand Down Expand Up @@ -136,7 +137,7 @@ private static ConvertedOpInfo convert(OpEnvironment env, OpInfo info,
return opt.get();
}
// Attempt 2: Computer with identity mutable output
opt = postprocessIdentity(info, request, preConverters, env);
opt = postprocessIdentity(info, request, preConverters, vars, env);
if (opt.isPresent()) {
return opt.get();
}
Expand Down Expand Up @@ -209,7 +210,8 @@ private static Optional<ConvertedOpInfo> postprocessFunction(OpInfo info,
postConverter, //
request.getOutType(), //
null, //
env //
env, //
vars //
));
}

Expand All @@ -231,7 +233,7 @@ private static Optional<ConvertedOpInfo> postprocessFunction(OpInfo info,
*/
private static Optional<ConvertedOpInfo> postprocessIdentity(OpInfo info,
OpRequest request, List<RichOp<Function<?, ?>>> preConverters,
OpEnvironment env)
Map<TypeVariable<?>, Type> vars, OpEnvironment env)
{
// This procedure only applies to Ops with mutable outputs
int ioIndex = mutableIndexOf(request.getType());
Expand All @@ -252,7 +254,8 @@ private static Optional<ConvertedOpInfo> postprocessIdentity(OpInfo info,
null, //
request.getOutType(), //
null, //
env //
env, //
vars //
));
}
return Optional.empty();
Expand Down Expand Up @@ -312,7 +315,8 @@ private static Optional<ConvertedOpInfo> postprocessConvertAndCopy(
postConverter, //
request.getOutType(), //
copyOp, //
env //
env, //
vars //
));
}
catch (OpMatchingException e) {
Expand Down Expand Up @@ -375,7 +379,8 @@ private static Optional<ConvertedOpInfo> postprocessCopy(OpInfo info,
postConverter, //
request.getOutType(), //
copyOp, //
env //
env, //
vars //
));
}
catch (OpMatchingException e) {
Expand Down Expand Up @@ -443,13 +448,17 @@ private static void resolveTypes(Type source, Type dest,
*/
private static Nil<?> wildcardVacuousTypeVars(final Type t) {
Type[] typeParams = Types.typeParamsAgainstClass(t, Types.raw(t));
if (t instanceof TypeVariable<?>) {
TypeVariable<?> tv = (TypeVariable<?>) t;
return Nil.of(new Any(tv.getBounds()));
}
var vars = new HashMap<TypeVariable<?>, Type>();
for (Type typeParam : typeParams) {
if (typeParam instanceof TypeVariable<?>) {
// Get the type variable
TypeVariable<?> from = (TypeVariable<?>) typeParam;
// Create a wildcard type with the type variable bounds
Type to = Types.wildcard(from.getBounds(), null);
Type to = new Any(from.getBounds());
vars.put(from, to);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public class ConvertedOpInfo implements OpInfo {

private final OpInfo info;
private final OpEnvironment env;
private final Map<TypeVariable<?>, Type> typeVarAssigns;
final List<RichOp<Function<?, ?>>> preconverters;
final List<Type> inTypes;
final RichOp<Function<?, ?>> postconverter;
Expand All @@ -116,7 +117,9 @@ public ConvertedOpInfo(OpInfo info,
Arrays.asList(inTypes(info.inputTypes(), preconverters)), //
postconverter, //
outType(info.outputType(), postconverter), copyOp, //
env //
env, //
// TODO: Fix?
Collections.emptyMap() //
);
}

Expand All @@ -138,7 +141,8 @@ public ConvertedOpInfo( //
RichOp<Function<?, ?>> postconverter, //
Type reqOutput, //
final RichOp<Computers.Arity1<?, ?>> copyOp, //
OpEnvironment env //
OpEnvironment env, //
Map<TypeVariable<?>, Type> typeVarAssigns //
) {
this.info = info;
this.opType = mapAnys(opType, info);
Expand All @@ -153,6 +157,7 @@ public ConvertedOpInfo( //
BaseOpHints.Conversion.FORBIDDEN, //
"converted" //
);
this.typeVarAssigns = typeVarAssigns;
}

/**
Expand Down Expand Up @@ -943,4 +948,7 @@ private static String fMethodPreprocessing(
return sb.toString();
}

public Map<TypeVariable<?>, Type> typeVarAssigns() {
return this.typeVarAssigns;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* @author Gabriel Selzer
* @param <T>
*/
public class IdentityCollection<T, U extends T> implements OpCollection {
public class IdentityCollection<T> implements OpCollection {

/**
* @input t the object to be converted
Expand All @@ -55,7 +55,7 @@ public class IdentityCollection<T, U extends T> implements OpCollection {
@OpHints(hints = { Conversion.FORBIDDEN,
BaseOpHints.DependencyMatching.FORBIDDEN })
@OpField(names = "engine.convert, engine.identity", priority = Priority.FIRST)
public final Function<U, T> identity = (t) -> t;
public final Function<T, T> identity = (t) -> t;

/**
* @mutable t the object to be "mutated"
Expand Down

0 comments on commit 14a20c0

Please sign in to comment.