Skip to content

Commit 34e5dea

Browse files
gselzerctrueden
authored andcommitted
Add new OpRunner Transformations
1 parent e47d6a4 commit 34e5dea

File tree

5 files changed

+130
-4
lines changed

5 files changed

+130
-4
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.scijava.ops.core.inplace;
2+
3+
import org.scijava.ops.core.Consumer7;
4+
import org.scijava.param.Mutable;
5+
6+
@FunctionalInterface
7+
public interface Inplace7Second<I1, IO, I3, I4, I5, I6, I7> extends Consumer7<I1, IO, I3, I4, I5, I6, I7> {
8+
void mutate(I1 in1, @Mutable IO io, I3 in3, I4 in4, I5 in5, I6 in6, I7 in7);
9+
10+
@Override
11+
default void accept(I1 in1, IO io, I3 in3, I4 in4, I5 in5, I6 in6, I7 in7) {
12+
mutate(in1, io, in3, in4, in5, in6, in7);
13+
}
14+
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,11 @@ public OpRef getRefTransformingTo(OpRef toRef) {
5555
// OpRunner) into one array so that we can use them to parameterize the
5656
// BiFunction.
5757
Type[] toParamTypes = getTransformedArgTypes(toRef);
58-
// Type[] toParamTypes = Stream
59-
// .concat(Arrays.stream(getTransformedArgTypes(toRef)), Arrays.stream(getTransformedOutputTypes(toRef)))
60-
// .toArray(Type[]::new);
6158

6259
// parameterize the OpRef types with the 3 BiFunction type parameters
6360
Type[] refTypes = Arrays.stream(toRef.getTypes())
6461
.map(refType -> Types.parameterize(Types.raw(refType), toParamTypes)).toArray(Type[]::new);
6562

66-
// from here it is the s
6763
boolean hit = TypeModUtils.replaceRawTypes(refTypes, targetClass(), srcClass());
6864
if (hit) {
6965
return OpRef.fromTypes(toRef.getName(), refTypes, getTransformedOutputTypes(toRef),
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.scijava.ops.transform.functional;
2+
3+
import java.lang.reflect.Type;
4+
import java.util.Arrays;
5+
6+
import org.scijava.ops.OpService;
7+
import org.scijava.ops.core.inplace.Inplace3Second;
8+
import org.scijava.ops.matcher.OpRef;
9+
import org.scijava.ops.transform.OpRunner;
10+
import org.scijava.ops.transform.OpTransformer;
11+
import org.scijava.ops.transform.TypeModUtils;
12+
import org.scijava.ops.types.TypeService;
13+
import org.scijava.ops.util.OpRunners;
14+
import org.scijava.plugin.Parameter;
15+
import org.scijava.plugin.Plugin;
16+
import org.scijava.util.Types;
17+
18+
@Plugin(type = OpTransformer.class)
19+
public class Inplace3SecondToOpRunnerTransformer implements FunctionalTypeTransformer {
20+
21+
@Parameter
22+
private TypeService typeService;
23+
24+
@SuppressWarnings("unchecked")
25+
@Override
26+
public Object transform(OpService opService, OpRef ref, Object src) {
27+
// Type[] outTypes = ref.getOutTypes();
28+
return OpRunners.Inplaces.toRunner((Inplace3Second) src);
29+
}
30+
31+
@Override
32+
public Class<?> srcClass() {
33+
return Inplace3Second.class;
34+
}
35+
36+
@Override
37+
public Class<?> targetClass() {
38+
return OpRunner.class;
39+
}
40+
41+
@Override
42+
public Type[] getTransformedArgTypes(OpRef toRef) {
43+
return toRef.getArgs();
44+
}
45+
46+
@Override
47+
public Type[] getTransformedOutputTypes(OpRef toRef) {
48+
return toRef.getOutTypes();
49+
}
50+
51+
@Override
52+
public OpRef getRefTransformingTo(OpRef toRef) {
53+
54+
// concatenate the input and output types of the BiFunction (as described by the
55+
// OpRunner) into one array so that we can use them to parameterize the
56+
// BiFunction.
57+
Type[] toParamTypes = getTransformedArgTypes(toRef);
58+
59+
// parameterize the OpRef types with the 3 BiFunction type parameters
60+
Type[] refTypes = Arrays.stream(toRef.getTypes())
61+
.map(refType -> Types.parameterize(Types.raw(refType), toParamTypes)).toArray(Type[]::new);
62+
63+
boolean hit = TypeModUtils.replaceRawTypes(refTypes, targetClass(), srcClass());
64+
if (hit) {
65+
return OpRef.fromTypes(toRef.getName(), refTypes, getTransformedOutputTypes(toRef),
66+
getTransformedArgTypes(toRef));
67+
}
68+
return null;
69+
}
70+
}

src/main/java/org/scijava/ops/util/Adapt.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.scijava.ops.core.inplace.BiInplaceSecond;
2020
import org.scijava.ops.core.inplace.Inplace;
2121
import org.scijava.ops.core.inplace.Inplace3First;
22+
import org.scijava.ops.core.inplace.Inplace3Second;
2223
import org.scijava.ops.core.inplace.Inplace4First;
2324
import org.scijava.ops.core.inplace.Inplace5First;
2425

@@ -232,6 +233,19 @@ public static <I1, I2, I3, O> BiComputer<I1, I2, O> asBiComputer(final Computer3
232233
}
233234

234235
public static class Inplaces {
236+
237+
public static <IO, I2> Inplace<IO> asInplace(BiInplaceFirst<IO, I2> inplace, I2 in2) {
238+
return (io) -> {
239+
inplace.mutate(io, in2);
240+
};
241+
}
242+
243+
public static <I1, IO> Inplace<IO> asInplace(BiInplaceSecond<I1, IO> inplace, I1 in1) {
244+
return (io) -> {
245+
inplace.mutate(in1, io);
246+
};
247+
}
248+
235249
public static <IO> Function<IO, IO> asFunction(Inplace<IO> inplace){
236250
return (io) -> {
237251
inplace.mutate(io);
@@ -260,6 +274,13 @@ public static <IO, I2, I3> Function3<IO, I2, I3, IO> asFunction3(Inplace3First<I
260274
};
261275
}
262276

277+
public static <I1, IO, I3> Function3<I1, IO, I3, IO> asFunction3(Inplace3Second<I1, IO, I3> inplace){
278+
return (in1, io, in3) -> {
279+
inplace.mutate(in1, io, in3);
280+
return io;
281+
};
282+
}
283+
263284
public static <IO, I2, I3, I4> Function4<IO, I2, I3, I4, IO> asFunction4(Inplace4First<IO, I2, I3, I4> inplace){
264285
return (io, in2, in3, in4) -> {
265286
inplace.mutate(io, in2, in3, in4);

src/main/java/org/scijava/ops/util/OpRunners.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.scijava.ops.core.inplace.BiInplaceSecond;
1717
import org.scijava.ops.core.inplace.Inplace;
1818
import org.scijava.ops.core.inplace.Inplace3First;
19+
import org.scijava.ops.core.inplace.Inplace3Second;
1920
import org.scijava.ops.core.inplace.Inplace4First;
2021
import org.scijava.ops.core.inplace.Inplace5First;
2122
import org.scijava.ops.transform.OpRunner;
@@ -381,6 +382,30 @@ public IO run(Object[] args) {
381382

382383
};
383384
}
385+
386+
public static <I1, IO, I3> OpRunner<IO> toRunner(Inplace3Second<I1, IO, I3> inplace) {
387+
return new OpRunner<IO>() {
388+
389+
@Override
390+
public Object getAdaptedOp() {
391+
return inplace;
392+
}
393+
394+
@Override
395+
public Nil<?>[] inTypes() {
396+
return new Nil<?>[] { new Nil<I1>() {
397+
}, new Nil<IO>() {
398+
}, new Nil<I3>() {
399+
} };
400+
}
401+
402+
@Override
403+
public IO run(Object[] args) {
404+
return Adapt.Inplaces.asFunction3(inplace).apply((I1) args[0], (IO) args[1], (I3) args[2]);
405+
}
406+
407+
};
408+
}
384409

385410
public static <IO, I2, I3, I4> OpRunner<IO> toRunner(Inplace4First<IO, I2, I3, I4> inplace) {
386411
return new OpRunner<IO>() {

0 commit comments

Comments
 (0)