Skip to content

Commit d71b5bb

Browse files
gselzerctrueden
authored andcommitted
Begin implementing transformers
1 parent f3fc619 commit d71b5bb

4 files changed

Lines changed: 244 additions & 10 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.scijava.ops.transform;
2+
3+
import org.scijava.ops.types.Nil;
4+
5+
public interface KnowsTypes {
6+
Nil<?>[] inTypes();
7+
8+
Nil<?>[] outTypes();
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.scijava.ops.transform;
2+
3+
import org.scijava.ops.types.Nil;
4+
5+
public interface OpRunner<O> extends KnowsTypes {
6+
O run(Object[] args);
7+
8+
@Override
9+
default Nil<?>[] outTypes() {
10+
return new Nil<?>[] { new Nil<O>() {
11+
} };
12+
}
13+
}

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

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.scijava.ops.util;
22

3+
import java.util.concurrent.Callable;
34
import java.util.function.BiFunction;
45
import java.util.function.Function;
56

@@ -9,7 +10,9 @@
910
import org.scijava.ops.core.computer.BiComputer;
1011
import org.scijava.ops.core.computer.Computer;
1112
import org.scijava.ops.core.computer.Computer3;
13+
import org.scijava.ops.core.computer.Computer4;
1214
import org.scijava.ops.core.function.Function3;
15+
import org.scijava.ops.core.function.Function4;
1316

1417
/**
1518
* Utility providing adaptation between {@link Op} types.
@@ -42,16 +45,13 @@ public static <I1, I2, O> BiComputer<I1, I2, O> asBiComputer(final BiFunction<I1
4245
};
4346
}
4447

45-
public static <I, O> OneToOneCommand<I, O> asCommand(final Function<I, O> function, I input) {
46-
OneToOneCommand<I, O> command = new OneToOneCommand<I, O>() {
47-
@Override
48-
public void run() {
49-
output = function.apply(input);
50-
}
51-
};
52-
// Populate the input member of the function command
53-
Inject.Commands.inputs(command, input);
54-
return command;
48+
public static <I, O> Callable<O> asNullaryFunction(final Function<I, O> function, I input) {
49+
return () -> function.apply(input);
50+
}
51+
52+
public static <I1, I2, O> Callable<O> asNullaryFunction(final BiFunction<I1, I2, O> function, I1 input1,
53+
I2 input2) {
54+
return () -> function.apply(input1, input2);
5555
}
5656

5757
/**
@@ -85,6 +85,7 @@ public static <I1, I2, I3, O> BiFunction<I1, I2, O> asBiFunction(final Function3
8585
return function3.apply(in1, in2, in3);
8686
};
8787
}
88+
8889
}
8990

9091
/**
@@ -111,6 +112,11 @@ public static <I, O> Function<I, O> asFunction(final Computer<I, O> computer,
111112
};
112113
}
113114

115+
public static <I, O> Callable<O> asNullaryFunction(final Computer<I, O> computer, final I input,
116+
final Function<I, O> inputAwareSource) {
117+
return Functions.asNullaryFunction(asFunction(computer, inputAwareSource), input);
118+
}
119+
114120
public static <I1, I2, O> BiFunction<I1, I2, O> asBiFunction(final BiComputer<I1, I2, O> computer,
115121
final Source<O> source) {
116122
return (in1, in2) -> {
@@ -129,6 +135,42 @@ public static <I1, I2, O> BiFunction<I1, I2, O> asBiFunction(final BiComputer<I1
129135
};
130136
}
131137

138+
public static <I1, I2, I3, O> Function3<I1, I2, I3, O> asFunction3(final Computer3<I1, I2, I3, O> computer,
139+
final Source<O> source) {
140+
return (in1, in2, in3) -> {
141+
O out = source.create();
142+
computer.compute(in1, in2, in3, out);
143+
return out;
144+
};
145+
}
146+
147+
public static <I1, I2, I3, O> Function3<I1, I2, I3, O> asFunction3(Computer3<I1, I2, I3, O> computer,
148+
Function3<I1, I2, I3, O> inputAwareSource) {
149+
return (in1, in2, in3) -> {
150+
O out = inputAwareSource.apply(in1, in2, in3);
151+
computer.compute(in1, in2, in3, out);
152+
return out;
153+
};
154+
}
155+
156+
public static <I1, I2, I3, I4, O> Function4<I1, I2, I3, I4, O> asFunction4(
157+
final Computer4<I1, I2, I3, I4, O> computer, final Source<O> source) {
158+
return (in1, in2, in3, in4) -> {
159+
O out = source.create();
160+
computer.compute(in1, in2, in3, in4, out);
161+
return out;
162+
};
163+
}
164+
165+
public static <I1, I2, I3, I4, O> Function4<I1, I2, I3, I4, O> asFunction4(
166+
Computer4<I1, I2, I3, I4, O> computer, Function4<I1, I2, I3, I4, O> inputAwareSource) {
167+
return (in1, in2, in3, in4) -> {
168+
O out = inputAwareSource.apply(in1, in2, in3, in4);
169+
computer.compute(in1, in2, in3, in4, out);
170+
return out;
171+
};
172+
}
173+
132174
public static <I, O> OneToOneCommand<I, O> asCommand(final Computer<I, O> computer, I input, O output) {
133175
OneToOneCommand<I, O> command = new OneToOneCommand<I, O>() {
134176
@Override
@@ -162,4 +204,5 @@ public static <I1, I2, I3, O> BiComputer<I1, I2, O> asBiComputer(final Computer3
162204
}
163205

164206
}
207+
165208
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package org.scijava.ops.util;
2+
3+
import java.util.function.BiFunction;
4+
import java.util.function.Function;
5+
6+
import org.scijava.ops.core.computer.BiComputer;
7+
import org.scijava.ops.core.computer.Computer;
8+
import org.scijava.ops.core.computer.Computer3;
9+
import org.scijava.ops.core.computer.Computer4;
10+
import org.scijava.ops.core.function.Function3;
11+
import org.scijava.ops.core.function.Function4;
12+
import org.scijava.ops.transform.OpRunner;
13+
import org.scijava.ops.types.Nil;
14+
15+
// 1. Implement all of the transformers for all types that need to be runnable
16+
// 2. Improve the matcher to respect the ops that implement KnowsTypes
17+
public class OpRunners {
18+
public static class Functions {
19+
20+
public static <I, O> OpRunner<O> toRunner(Function<I, O> function) {
21+
return new OpRunner<O>() {
22+
23+
@Override
24+
public Nil<?>[] inTypes() {
25+
return new Nil<?>[] { new Nil<I>() {
26+
} };
27+
}
28+
29+
@SuppressWarnings("unchecked")
30+
@Override
31+
public O run(Object[] args) {
32+
return function.apply((I) args[0]);
33+
}
34+
35+
};
36+
}
37+
38+
public static <I1, I2, O> OpRunner<O> toRunner(BiFunction<I1, I2, O> function) {
39+
return new OpRunner<O>() {
40+
41+
@Override
42+
public Nil<?>[] inTypes() {
43+
return new Nil<?>[] { new Nil<I1>() {
44+
}, new Nil<I2>() {
45+
} };
46+
}
47+
48+
@SuppressWarnings("unchecked")
49+
@Override
50+
public O run(Object[] args) {
51+
return function.apply((I1) args[0], (I2) args[1]);
52+
}
53+
54+
};
55+
}
56+
57+
public static <I1, I2, I3, O> OpRunner<O> toRunner(Function3<I1, I2, I3, O> function) {
58+
return new OpRunner<O>() {
59+
60+
@Override
61+
public Nil<?>[] inTypes() {
62+
return new Nil<?>[] { new Nil<I1>() {
63+
}, new Nil<I2>() {
64+
}, new Nil<I3>() {
65+
} };
66+
}
67+
68+
@SuppressWarnings("unchecked")
69+
@Override
70+
public O run(Object[] args) {
71+
return function.apply((I1) args[0], (I2) args[1], (I3) args[2]);
72+
}
73+
74+
};
75+
}
76+
77+
public static <I1, I2, I3, I4, O> OpRunner<O> toRunner(Function4<I1, I2, I3, I4, O> function) {
78+
return new OpRunner<O>() {
79+
80+
@Override
81+
public Nil<?>[] inTypes() {
82+
return new Nil<?>[] { new Nil<I1>() {
83+
}, new Nil<I2>() {
84+
}, new Nil<I3>() {
85+
}, new Nil<I4>() {
86+
} };
87+
}
88+
89+
@SuppressWarnings("unchecked")
90+
@Override
91+
public O run(Object[] args) {
92+
return function.apply((I1) args[0], (I2) args[1], (I3) args[2], (I4) args[3]);
93+
}
94+
95+
};
96+
}
97+
}
98+
99+
public static class Computers {
100+
public static <I, O> OpRunner<O> toRunner(Computer<I, O> computer, Function<I, O> inputAwareSource){
101+
return new OpRunner<O>() {
102+
103+
@Override
104+
public Nil<?>[] inTypes() {
105+
return new Nil<?>[] { new Nil<I>() {}};
106+
}
107+
108+
@SuppressWarnings("unchecked")
109+
@Override
110+
public O run(Object[] args) {
111+
return Adapt.Computers.asFunction(computer, inputAwareSource).apply((I) args[0]);
112+
}
113+
114+
};
115+
}
116+
117+
public static <I1, I2, O> OpRunner<O> toRunner(BiComputer<I1, I2, O> computer, BiFunction<I1, I2, O> inputAwareSource){
118+
return new OpRunner<O>() {
119+
120+
@Override
121+
public Nil<?>[] inTypes() {
122+
return new Nil<?>[] { new Nil<I1>() {}, new Nil<I2>() {}};
123+
}
124+
125+
@SuppressWarnings("unchecked")
126+
@Override
127+
public O run(Object[] args) {
128+
return Adapt.Computers.asBiFunction(computer, inputAwareSource).apply((I1) args[0], (I2) args[1]);
129+
}
130+
131+
};
132+
}
133+
134+
public static <I1, I2, I3, O> OpRunner<O> toRunner(Computer3<I1, I2, I3, O> computer, Function3<I1, I2, I3, O> inputAwareSource){
135+
return new OpRunner<O>() {
136+
137+
@Override
138+
public Nil<?>[] inTypes() {
139+
return new Nil<?>[] { new Nil<I1>() {}, new Nil<I2>() {}, new Nil<I3>() {}};
140+
}
141+
142+
@SuppressWarnings("unchecked")
143+
@Override
144+
public O run(Object[] args) {
145+
return Adapt.Computers.asFunction3(computer, inputAwareSource).apply((I1) args[0], (I2) args[1], (I3) args[2]);
146+
}
147+
148+
};
149+
}
150+
151+
public static <I1, I2, I3, I4, O> OpRunner<O> toRunner(Computer4<I1, I2, I3, I4, O> computer, Function4<I1, I2, I3, I4, O> inputAwareSource){
152+
return new OpRunner<O>() {
153+
154+
@Override
155+
public Nil<?>[] inTypes() {
156+
return new Nil<?>[] { new Nil<I1>() {}, new Nil<I2>() {}, new Nil<I3>() {}, new Nil<I4>() {}};
157+
}
158+
159+
@SuppressWarnings("unchecked")
160+
@Override
161+
public O run(Object[] args) {
162+
return Adapt.Computers.asFunction4(computer, inputAwareSource).apply((I1) args[0], (I2) args[1], (I3) args[2], (I4) args[3]);
163+
}
164+
165+
};
166+
}
167+
}
168+
169+
}

0 commit comments

Comments
 (0)