Skip to content

Commit 9f4cbe2

Browse files
committed
WIP: Substitute Any types in Op transformers
Pretty hacky at the moment since we only implemented this substitution for the first link in the transformation chain and also only for computer->function conversion (and there, only rudimentary). Also add AnyTest.testRunAnyBiFunctionFromBiComputer to cover further work. Note the fix in DefaultOpTypeMatchingService (logical not added).
1 parent a3f8cd1 commit 9f4cbe2

11 files changed

Lines changed: 115 additions & 6 deletions

File tree

src/main/java/net/imagej/ops/create/Creators.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import net.imglib2.util.Util;
4545
import net.imglib2.view.Views;
4646

47+
import org.joml.Vector3d;
48+
import org.joml.Vector3f;
4749
import org.scijava.core.Priority;
4850
import org.scijava.ops.OpField;
4951
import org.scijava.ops.core.OpCollection;
@@ -524,4 +526,11 @@ public class Creators<N extends NativeType<N>, L, I extends IntegerType<I>, T ex
524526
@Parameter(key = "nativeType", type = ItemIO.OUTPUT)
525527
public final Function<Class<N>, N> nativeTypeFromClass = (clazz) -> (N) object.apply((Class<L>) clazz);
526528

529+
@OpField(names = "create.vector")
530+
@Parameter(key = "vector3d", type = ItemIO.OUTPUT)
531+
public final Source<Vector3d> defaultVector3d = () -> new Vector3d();
532+
533+
@OpField(names = "create.vector")
534+
@Parameter(key = "vector3f", type = ItemIO.OUTPUT)
535+
public final Source<Vector3f> defaultVector3f = () -> new Vector3f();
527536
}

src/main/java/org/scijava/ops/matcher/DefaultOpTypeMatchingService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ private boolean typesPerfectMatch(final OpCandidate candidate) {
241241
}
242242

243243
final Type outputType = candidate.getRef().getOutType();
244-
if (Objects.equals(outputType, OpUtils.outputType(candidate)))
244+
if (!Objects.equals(outputType, OpUtils.outputType(candidate)))
245245
return false;
246246

247247
candidate.setStatus(StatusCode.MATCH);

src/main/java/org/scijava/ops/transform/FunctionalToOpRunnerTransformer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ private static OpTransformationException createInvalidSourceOpException(final Ob
170170
problemArgs));
171171
}
172172

173+
@Override
174+
public OpRef substituteAnyInTargetRef(OpRef srcRef, OpRef targetRef) {
175+
throw new UnsupportedOperationException("not yet implemented");
176+
}
177+
173178
@Override
174179
public Collection<OpRef> getRefsTransformingTo(final OpRef targetRef) {
175180
if (!isOpRunner(targetRef)) {

src/main/java/org/scijava/ops/transform/OpMapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ default Object transform(final OpService opService, final Object src, final OpRe
8484
" is not of required class: " + srcClass().getName());
8585
}
8686

87+
@Override
88+
default OpRef substituteAnyInTargetRef(OpRef srcRef, OpRef targetRef) {
89+
throw new UnsupportedOperationException("not yet implemented");
90+
}
91+
8792
/**
8893
* One-to-one specialization of {@link #getRefsTransformingTo(OpRef)}. Returns
8994
* a non-{@code null} source if one could be found and {@code null} otherwise.

src/main/java/org/scijava/ops/transform/OpTransformation.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,21 @@ public OpTransformation getChild() {
6666
* be executed.
6767
*
6868
* @param obj
69+
* @param targetRef2
6970
* @param opService
7071
* @return
7172
* @throws OpTransformationException
7273
*/
73-
public Object execute(Object obj, OpService opService) throws OpTransformationException {
74+
public Object execute(Object obj, OpRef targetRef, OpService opService) throws OpTransformationException {
7475
Object candidate = obj;
7576
OpTransformation c = this;
77+
OpRef tr = targetRef;
7678
do {
77-
candidate = c.getTransformer().transform(opService, candidate, c.targetRef);
79+
candidate = c.getTransformer().transform(opService, candidate, tr);
7880
c = c.getChild();
81+
if (c != null) {
82+
tr = c.getTarget();
83+
}
7984
} while (c != null);
8085

8186
return candidate;

src/main/java/org/scijava/ops/transform/OpTransformationCandidate.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
12
package org.scijava.ops.transform;
23

4+
import java.lang.reflect.Type;
35
import java.util.List;
6+
import java.util.stream.Stream;
47

58
import org.scijava.ops.OpService;
69
import org.scijava.ops.matcher.OpCandidate;
10+
import org.scijava.ops.matcher.OpInfo;
711
import org.scijava.ops.matcher.OpMatchingException;
12+
import org.scijava.ops.matcher.OpRef;
13+
import org.scijava.struct.Member;
814

915
/**
1016
* Wrapper class to match a {@link OpTransformation} with a matching
@@ -21,11 +27,11 @@ public OpTransformationCandidate(OpCandidate scrOp, OpTransformation transformat
2127
this.srcOp = scrOp;
2228
this.transformation = transformation;
2329
}
24-
30+
2531
public OpCandidate getSourceOp() {
2632
return srcOp;
2733
}
28-
34+
2935
// private void substituteAnyInTransformation() {
3036
// // obtain the type info from the matched Op
3137
// OpInfo srcInfo = srcOp.opInfo();
@@ -43,7 +49,13 @@ public Object exceute(OpService opService, List<?> dependencies, Object... secon
4349
{
4450
Object op = srcOp.createOp(dependencies, secondaryArgs);
4551
// substituteAnyInTransformation();
46-
return transformation.execute(op, opService);
52+
53+
OpInfo srcInfo = srcOp.opInfo();
54+
OpRef srcRef = new OpRef(srcOp.getRef().getName(), new Type[] { srcInfo.opType() }, srcInfo.output().getType(),
55+
srcInfo.inputs().stream().map(Member::getType).toArray(Type[]::new));
56+
OpRef targetRef = transformation.getTransformer().substituteAnyInTargetRef(srcRef, transformation.getTarget());
57+
58+
return transformation.execute(op, targetRef, opService);
4759
}
4860

4961
@Override

src/main/java/org/scijava/ops/transform/OpTransformer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public interface OpTransformer extends SingletonPlugin {
6262
*/
6363
Object transform(OpService opService, Object src, OpRef targetRef) throws OpTransformationException;
6464

65+
66+
OpRef substituteAnyInTargetRef(OpRef srcRef, OpRef targetRef);
67+
6568
/**
6669
* Returns a collection that contains the {@link OpRef}s of all Ops which can
6770
* be transformed into the Op described by the given {@code targetRef} using

src/main/java/org/scijava/ops/transform/functional/ComputerToFunctionTransformer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ private static <I1, I2, I3, I4, I5, O> Function5<I1, I2, I3, I4, I5, O> computer
193193
: Adapt.Computers.asFunction5(src, (Source<O>) create);
194194
}
195195

196+
@Override
197+
public OpRef substituteAnyInTargetRef(OpRef srcRef, OpRef targetRef) {
198+
final Type[] targetTypes = srcRef.getTypes();
199+
TypeModUtils.replaceRawTypes(targetTypes, Types.raw(srcRef.getTypes()[0]), Types.raw(targetRef.getTypes()[0]));
200+
return new OpRef(targetRef.getName(), targetTypes, srcRef.getOutType(), targetRef.getArgs());
201+
}
202+
196203
@Override
197204
public Integer getTargetArity(final Class<?> targetFunctionalRawType) {
198205
return Functions.ALL_FUNCTIONS.get(targetFunctionalRawType);

src/main/java/org/scijava/ops/transform/functional/FunctionToComputerTransformer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ private static <I1, I2, O> BiComputer<I1, I2, O> functionToComputer(final BiFunc
128128
return Adapt.Functions.asBiComputer(src, (Computer<O, O>) copy);
129129
}
130130

131+
@Override
132+
public OpRef substituteAnyInTargetRef(OpRef srcRef, OpRef targetRef) {
133+
throw new UnsupportedOperationException("not yet implemented");
134+
}
135+
131136
@Override
132137
public Integer getTargetArity(final Class<?> targetFunctionalRawType) {
133138
return Computers.ALL_COMPUTERS.get(targetFunctionalRawType);

src/main/java/org/scijava/ops/transform/functional/InplaceToFunctionTransformer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ else if (targetFunctionalRawType == null) {
114114
}
115115
}
116116

117+
@Override
118+
public OpRef substituteAnyInTargetRef(OpRef srcRef, OpRef targetRef) {
119+
throw new UnsupportedOperationException("not yet implemented");
120+
}
121+
117122
@Override
118123
public Integer getTargetArity(final Class<?> targetFunctionalRawType) {
119124
return Functions.ALL_FUNCTIONS.get(targetFunctionalRawType);

0 commit comments

Comments
 (0)