Skip to content

Commit 354abba

Browse files
gselzerctrueden
authored andcommitted
Rename opFromID to opFromSignature
1 parent c8d195a commit 354abba

3 files changed

Lines changed: 91 additions & 90 deletions

File tree

scijava/scijava-ops-api/src/main/java/org/scijava/ops/api/OpEnvironment.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -130,36 +130,15 @@ InfoChain infoChain(final String opName, final Nil<?> specialType,
130130

131131
<T> T opFromInfoChain(InfoChain chain, Nil<T> specialType);
132132

133-
/**
134-
* Returns an Op fitting the provided arguments. NB implementations of this
135-
* method likely depend on the {@link Hints} set by
136-
* {@link OpEnvironment#setDefaultHints(Hints)}, which provides no guarantee of
137-
* thread-safety. Users interested in parallel Op matching should consider
138-
* using {@link OpEnvironment#op(String, Nil, Nil[], Nil, Hints)} instead.
139-
*
140-
* @param <T> the {@link Type} of the Op
141-
* @param opID the name of the Op
142-
* @param specialType the generic {@link Type} of the Op
143-
* @param inTypes the arguments (inputs) to the Op
144-
* @param outType the return of the Op (note that it may also be an argument)
145-
* @return an instance of an Op aligning with the search parameters
146-
*/
147-
<T> T opFromID(final String opID, final Nil<T> specialType,
148-
final Nil<?>[] inTypes, final Nil<?> outType);
149-
150133
/**
151134
* Returns an Op fitting the provided arguments.
152135
*
153136
* @param <T> the {@link Type} of the Op
154-
* @param opName the name of the Op
137+
* @param signature the signature of the Op
155138
* @param specialType the generic {@link Type} of the Op
156-
* @param inTypes the arguments (inputs) to the Op
157-
* @param outType the return of the Op (note that it may also be an argument)
158-
* @param hints the {@link Hints} that should guide this matching call
159139
* @return an instance of an Op aligning with the search parameters
160140
*/
161-
<T> T opFromID(final String opID, final Nil<T> specialType,
162-
final Nil<?>[] inTypes, final Nil<?> outType, Hints hints);
141+
<T> T opFromSignature(final String signature, final Nil<T> specialType);
163142

164143
InfoChain chainFromID(final String signature);
165144

scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/impl/DefaultOpEnvironment.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,8 @@ public InfoChain chainFromInfo(OpInfo info, Nil<?> specialType) {
250250
}
251251

252252
@Override
253-
public <T> T opFromID(final String id, final Nil<T> specialType,
254-
final Nil<?>[] inTypes, final Nil<?> outType)
255-
{
256-
return opFromID(id, specialType, inTypes, outType, getDefaultHints());
257-
}
258-
259-
@Override
260-
public <T> T opFromID(final String id, final Nil<T> specialType, final Nil<?>[] inTypes, final Nil<?> outType, Hints hints) {
261-
InfoChain info = chainFromID(id);
253+
public <T> T opFromSignature(final String signature, final Nil<T> specialType) {
254+
InfoChain info = chainFromID(signature);
262255
return opFromInfoChain(info, specialType);
263256
}
264257

Lines changed: 87 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package org.scijava.ops.engine.impl;
23

34
import java.util.ArrayList;
@@ -7,7 +8,6 @@
78
import java.util.List;
89
import java.util.function.Function;
910

10-
import org.checkerframework.checker.units.qual.m;
1111
import org.junit.Assert;
1212
import org.junit.Test;
1313
import org.scijava.Priority;
@@ -38,20 +38,23 @@ public void testProvenance() {
3838
List<RichOp<?>> executionsUpon = ops.history().executionsUpon(s);
3939
Assert.assertEquals(1, executionsUpon.size());
4040
// Assert only one info in the execution hierarchy
41-
InfoChain executionHierarchy = ops.history().opExecutionChain(executionsUpon.get(0));
41+
InfoChain executionHierarchy = ops.history().opExecutionChain(executionsUpon
42+
.get(0));
4243
Assert.assertEquals(0, executionHierarchy.dependencies().size());
4344
OpInfo info = executionHierarchy.info();
44-
Assert.assertTrue(info.implementationName().contains(this.getClass().getPackageName()));
45+
Assert.assertTrue(info.implementationName().contains(this.getClass()
46+
.getPackageName()));
4547
}
4648

4749
@OpField(names = "test.provenance")
4850
public final Function<List<? extends Number>, Double> bar = l -> {
49-
return l.stream().map(n -> n.doubleValue()).reduce((d1, d2)-> d1 + d2).orElse(0.);
51+
return l.stream().map(n -> n.doubleValue()).reduce((d1, d2) -> d1 + d2)
52+
.orElse(0.);
5053
};
5154

5255
@OpField(names = "test.provenance", priority = Priority.HIGH)
5356
public final Function<List<Double>, Double> baz = l -> {
54-
return l.stream().reduce((d1, d2)-> d1 + d2).orElse(0.);
57+
return l.stream().reduce((d1, d2) -> d1 + d2).orElse(0.);
5558
};
5659

5760
@Test
@@ -61,48 +64,62 @@ public void testPriorityProvenance() {
6164
l1.add(2.0);
6265
l1.add(3.0);
6366
l1.add(4.0);
64-
Double out1 = ops.op("test.provenance").input(l1).outType(Double.class).apply();
67+
Double out1 = ops.op("test.provenance").input(l1).outType(Double.class)
68+
.apply();
6569

6670
List<Long> l2 = new ArrayList<>();
6771
l2.add(5l);
6872
l2.add(6l);
6973
l2.add(7l);
7074
l2.add(8l);
71-
Double out2 = ops.op("test.provenance").input(l2).outType(Double.class).apply();
75+
Double out2 = ops.op("test.provenance").input(l2).outType(Double.class)
76+
.apply();
7277

7378
List<RichOp<?>> history1 = ops.history().executionsUpon(out1);
7479
List<RichOp<?>> history2 = ops.history().executionsUpon(out2);
7580

7681
Assert.assertEquals(1, history1.size());
77-
InfoChain opExecutionChain = ops.history().opExecutionChain(history1.get(0));
82+
InfoChain opExecutionChain = ops.history().opExecutionChain(history1.get(
83+
0));
7884
Assert.assertEquals(0, opExecutionChain.dependencies().size());
79-
String expected = "public final java.util.function.Function org.scijava.ops.engine.impl.ProvenanceTest.baz";
80-
Assert.assertEquals(expected, opExecutionChain.info().getAnnotationBearer().toString());
85+
String expected =
86+
"public final java.util.function.Function org.scijava.ops.engine.impl.ProvenanceTest.baz";
87+
Assert.assertEquals(expected, opExecutionChain.info().getAnnotationBearer()
88+
.toString());
8189

8290
Assert.assertEquals(1, history2.size());
8391
opExecutionChain = ops.history().opExecutionChain(history2.get(0));
8492
Assert.assertEquals(0, opExecutionChain.dependencies().size());
85-
expected = "public final java.util.function.Function org.scijava.ops.engine.impl.ProvenanceTest.bar";
86-
Assert.assertEquals(expected, opExecutionChain.info().getAnnotationBearer().toString());
93+
expected =
94+
"public final java.util.function.Function org.scijava.ops.engine.impl.ProvenanceTest.bar";
95+
Assert.assertEquals(expected, opExecutionChain.info().getAnnotationBearer()
96+
.toString());
8797
}
8898

8999
@OpField(names = "test.provenanceMapped")
90100
public final Function<Double, Thing> mappedFunc = in -> new Thing(in);
91101

92102
@OpMethod(names = "test.provenanceMapper", type = Function.class)
93-
public static Thing mapperFunc(@OpDependency(name = "test.provenanceMapped") Function<Double, Thing> func, Double[] arr) {
94-
return Arrays.stream(arr).map(func).reduce((t1, t2) -> t1.append(t2)).orElseGet(() -> null);
103+
public static Thing mapperFunc(@OpDependency(
104+
name = "test.provenanceMapped") Function<Double, Thing> func, Double[] arr)
105+
{
106+
return Arrays.stream(arr).map(func).reduce((t1, t2) -> t1.append(t2))
107+
.orElseGet(() -> null);
95108
}
96109

97110
class Thing {
111+
98112
private Double d;
113+
99114
public Thing(Double d) {
100115
this.d = d;
101116
}
117+
102118
private Thing append(Thing other) {
103119
d += other.getDouble();
104120
return this;
105121
}
122+
106123
public Double getDouble() {
107124
return d;
108125
}
@@ -114,7 +131,8 @@ public void testMappingProvenance() {
114131
int length = 200;
115132
Double[] array = new Double[length];
116133
Arrays.fill(array, 1.);
117-
Thing out = ops.op("test.provenanceMapper").input(array).outType(Thing.class).apply();
134+
Thing out = ops.op("test.provenanceMapper").input(array).outType(
135+
Thing.class).apply();
118136

119137
// Assert only one execution upon this Object
120138
List<RichOp<?>> executionsUpon = ops.history().executionsUpon(out);
@@ -127,20 +145,25 @@ public void testMappingExecutionChain() {
127145
int length = 200;
128146
Double[] array = new Double[length];
129147
Arrays.fill(array, 1.);
130-
Function<Double[], Thing> mapper = ops.op("test.provenanceMapper").input(array).outType(Thing.class).function();
148+
Function<Double[], Thing> mapper = ops.op("test.provenanceMapper").input(
149+
array).outType(Thing.class).function();
131150

132151
// Get the Op execution chain associated with the above call
133152
InfoChain executionChain = ops.history().opExecutionChain(mapper);
134153

135154
// Assert the mapper is in the execution chain
136-
Iterator<OpInfo> mapperInfos = ops.env().infos("test.provenanceMapper").iterator();
155+
Iterator<OpInfo> mapperInfos = ops.env().infos("test.provenanceMapper")
156+
.iterator();
137157
OpInfo mapperInfo = mapperInfos.next();
138158
Assert.assertTrue(executionChain.info().equals(mapperInfo));
139159
// Assert mapped is in the execution chain
140-
Iterator<OpInfo> mappedInfos = ops.env().infos("test.provenanceMapped").iterator();
160+
Iterator<OpInfo> mappedInfos = ops.env().infos("test.provenanceMapped")
161+
.iterator();
141162
OpInfo mappedInfo = mappedInfos.next();
142-
Assert.assertEquals("Expected only one dependency of the mapper Op!", 1, executionChain.dependencies().size());
143-
Assert.assertTrue(executionChain.dependencies().get(0).info().equals(mappedInfo));
163+
Assert.assertEquals("Expected only one dependency of the mapper Op!", 1,
164+
executionChain.dependencies().size());
165+
Assert.assertTrue(executionChain.dependencies().get(0).info().equals(
166+
mappedInfo));
144167
}
145168

146169
@Test
@@ -150,14 +173,16 @@ public void testMappingProvenanceAndCaching() {
150173
int length = 200;
151174
Double[] array = new Double[length];
152175
Arrays.fill(array, 1.);
153-
Thing out = ops.op("test.provenanceMapper").input(array).outType(Thing.class).apply(hints);
176+
Thing out = ops.op("test.provenanceMapper").input(array).outType(
177+
Thing.class).apply(hints);
154178

155179
// Assert only one run of the Base Op
156180
List<RichOp<?>> history = ops.history().executionsUpon(out);
157181
Assert.assertEquals(1, history.size());
158182

159183
// Run the mapped Op, assert still one run on the mapper
160-
Thing out1 = ops.op("test.provenanceMapped").input(2.).outType(Thing.class).apply(hints);
184+
Thing out1 = ops.op("test.provenanceMapped").input(2.).outType(Thing.class)
185+
.apply(hints);
161186
history = ops.history().executionsUpon(out);
162187
Assert.assertEquals(1, history.size());
163188
// Assert one run on the mapped Op as well
@@ -169,14 +194,15 @@ public void testMappingProvenanceAndCaching() {
169194
@Test
170195
public void testDependencylessOpRecoveryFromString() {
171196
Hints hints = new DefaultHints();
172-
Function<Double, Thing> mapper = ops.op("test.provenanceMapped").input(5.0).outType(Thing.class).function(hints);
197+
Function<Double, Thing> mapper = ops.op("test.provenanceMapped").input(5.0)
198+
.outType(Thing.class).function(hints);
173199
InfoChain chain = ops.history().opExecutionChain(mapper);
174200
Assert.assertEquals(0, chain.dependencies().size());
175201
String signature = chain.signature();
176202
Nil<Function<Double, Thing>> special = new Nil<>() {};
177-
Nil<Double> inType = Nil.of(Double.class);
178-
Nil<Thing> outType = Nil.of(Thing.class);
179-
Function<Double, Thing> actual = ops.env().opFromID(signature, special, new Nil[] {inType}, outType);
203+
@SuppressWarnings("unused")
204+
Function<Double, Thing> actual = ops.env().opFromSignature(signature,
205+
special);
180206
}
181207

182208
@Test
@@ -187,60 +213,62 @@ public void testOpWithDependencyRecoveryFromString() {
187213
InfoChain chain = new InfoChain(info, Collections.singletonList(depChain));
188214
String signature = chain.signature();
189215
Nil<Function<Double[], Thing>> special = new Nil<>() {};
190-
Nil<Double[]> inType = Nil.of(Double[].class);
191-
Nil<Thing> outType = Nil.of(Thing.class);
192-
Function<Double[], Thing> actual = ops.env().opFromID(signature, special, new Nil[] {inType}, outType);
216+
@SuppressWarnings("unused")
217+
Function<Double[], Thing> actual = ops.env().opFromSignature(signature,
218+
special);
193219
}
194220

195221
@Test
196222
public void testAdaptationRecoveryFromString() {
197-
Function<Double[], Thing[]> f = ops.op("test.provenanceMapped").inType(Double[].class).outType(Thing[].class).function();
223+
Function<Double[], Thing[]> f = ops.op("test.provenanceMapped").inType(
224+
Double[].class).outType(Thing[].class).function();
198225
String signature = ops.history().signatureOf(f);
199-
Thing[] apply = f.apply(new Double[] {1., 2., 3.});
200226
Nil<Function<Double[], Thing[]>> special = new Nil<>() {};
201-
Nil<Double[]> inType = Nil.of(Double[].class);
202-
Nil<Thing[]> outType = Nil.of(Thing[].class);
203-
Function<Double[], Thing[]> actual = ops.env().opFromID(signature, special, new Nil[] {inType}, outType);
204-
apply = actual.apply(new Double[] {1., 2., 3.});
227+
Function<Double[], Thing[]> actual = ops.env().opFromSignature(signature,
228+
special);
229+
@SuppressWarnings("unused")
230+
Thing[] apply = actual.apply(new Double[] { 1., 2., 3. });
205231
}
206232

207233
@Test
208234
public void testAdaptedOpWithDependencies() {
209-
Function<Double[][], Thing[]> f = ops.op("test.provenanceMapper").inType(Double[][].class).outType(Thing[].class).function();
235+
Function<Double[][], Thing[]> f = ops.op("test.provenanceMapper").inType(
236+
Double[][].class).outType(Thing[].class).function();
210237
String signature = ops.history().signatureOf(f);
211238
Nil<Function<Double[][], Thing[]>> special = new Nil<>() {};
212-
Nil<Double[][]> inType = Nil.of(Double[][].class);
213-
Nil<Thing[]> outType = Nil.of(Thing[].class);
214-
Function<Double[][], Thing[]> actual = ops.env().opFromID(signature, special, new Nil[] {inType}, outType);
215-
Thing[] apply = actual.apply(new Double[][] {new Double[] {1., 2., 3.}});
239+
Function<Double[][], Thing[]> actual = ops.env().opFromSignature(signature,
240+
special);
241+
@SuppressWarnings("unused")
242+
Thing[] apply = actual.apply(new Double[][] { new Double[] { 1., 2.,
243+
3. } });
216244
}
217245

218246
@Test
219247
public void testSimplificationRecovery() {
220-
Computers.Arity1<Integer[], Integer[]> c = ops.op("test.provenanceComputer").inType(Integer[].class).outType(Integer[].class).computer();
248+
Computers.Arity1<Integer[], Integer[]> c = ops.op("test.provenanceComputer")
249+
.inType(Integer[].class).outType(Integer[].class).computer();
221250
String signature = ops.history().signatureOf(c);
222251
Nil<Computers.Arity1<Integer[], Integer[]>> special = new Nil<>() {};
223-
Nil<Integer[]> inType = Nil.of(Integer[].class);
224-
Nil<Integer[]> outType = Nil.of(Integer[].class);
225-
Computers.Arity1<Integer[], Integer[]> fromString = ops.env().opFromID(signature, special, new Nil[] {inType}, outType);
226-
Integer[] in = {1, 2, 3};
227-
Integer[] actual = {0, 0, 0};
252+
Computers.Arity1<Integer[], Integer[]> fromString = ops.env()
253+
.opFromSignature(signature, special);
254+
Integer[] in = { 1, 2, 3 };
255+
Integer[] actual = { 0, 0, 0 };
228256
fromString.compute(in, actual);
229-
Integer[] expected = {1, 2, 3};
257+
Integer[] expected = { 1, 2, 3 };
230258
Assert.assertArrayEquals(expected, actual);
231259
}
232260

233261
@Test
234262
public void testSimplificationAdaptationRecovery() {
235-
Function<Integer[], Integer[]> c = ops.op("test.provenanceComputer").inType(Integer[].class).outType(Integer[].class).function();
263+
Function<Integer[], Integer[]> c = ops.op("test.provenanceComputer").inType(
264+
Integer[].class).outType(Integer[].class).function();
236265
String signature = ops.history().signatureOf(c);
237266
Nil<Function<Integer[], Integer[]>> special = new Nil<>() {};
238-
Nil<Integer[]> inType = Nil.of(Integer[].class);
239-
Nil<Integer[]> outType = Nil.of(Integer[].class);
240-
Function<Integer[], Integer[]> fromString = ops.env().opFromID(signature, special, new Nil[] {inType}, outType);
241-
Integer[] in = {1, 2, 3};
267+
Function<Integer[], Integer[]> fromString = ops.env().opFromSignature(
268+
signature, special);
269+
Integer[] in = { 1, 2, 3 };
242270
Integer[] actual = fromString.apply(in);
243-
Integer[] expected = {1, 2, 3};
271+
Integer[] expected = { 1, 2, 3 };
244272
Assert.assertArrayEquals(expected, actual);
245273
}
246274

@@ -260,15 +288,16 @@ private OpInfo singularInfoOfName(String name) {
260288

261289
@Test
262290
public void testAdaptatorWithDependencies() {
263-
Function<Double[], Double[]> f = ops.op("test.provenanceComputer").inType(Double[].class).outType(Double[].class).function();
291+
Function<Double[], Double[]> f = ops.op("test.provenanceComputer").inType(
292+
Double[].class).outType(Double[].class).function();
264293
@SuppressWarnings("unchecked")
265294
InfoChain chain = ((RichOp<Function<Double[], Double[]>>) f).infoChain();
266295
String signature = chain.signature();
267296
Nil<Function<Double[], Double[]>> special = new Nil<>() {};
268-
Nil<Double[]> inType = Nil.of(Double[].class);
269-
Nil<Double[]> outType = Nil.of(Double[].class);
270-
Function<Double[], Double[]> actual = ops.env().opFromID(signature, special, new Nil[] {inType}, outType);
271-
Double[] apply = actual.apply(new Double[] {1., 2., 3.});
297+
Function<Double[], Double[]> actual = ops.env().opFromSignature(signature,
298+
special);
299+
@SuppressWarnings("unused")
300+
Double[] apply = actual.apply(new Double[] { 1., 2., 3. });
272301
}
273302

274303
}

0 commit comments

Comments
 (0)