Skip to content

Commit f4eba19

Browse files
gselzerctrueden
authored andcommitted
Retain RichOp when resolving dependencies
This allows the dependencies to update the progress of their parent Task This commit also fixes some expected answers within ProvenanceTest. Previously, these tests asserted that only the outside (mapper) function altered the output. But, in this case, the output was directly created by the mapped function. Thus there were really two Ops that directly altered the output. Now, the test reflects that.
1 parent 2c534d5 commit f4eba19

4 files changed

Lines changed: 50 additions & 15 deletions

File tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,9 @@ private OpInstance<?> instantiateOp(final OpCandidate candidate,
477477
Hints hints)
478478
{
479479
final List<RichOp<?>> conditions = resolveOpDependencies(candidate, hints);
480-
final List<InfoChain> depChains = conditions.stream().map(op -> op
481-
.metadata().info()).collect(Collectors.toList());
482-
final InfoChain chain = new InfoChain(candidate.opInfo(), depChains);
483-
return chain.op();
480+
InfoChain adaptorChain = new DependencyInstantiatedInfoChain(candidate
481+
.opInfo(), conditions);
482+
return adaptorChain.op();
484483
}
485484

486485
/**

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ public DependencyInstantiatedInfoChain(OpInfo info, List<RichOp<?>> dependencies
2525

2626
@Override
2727
protected Object generateOp() {
28-
List<Object> dependencyInstances = dependencies.stream() //
29-
.map(d -> d.op()) //
30-
.collect(Collectors.toList());
31-
return info().createOpInstance(dependencyInstances).object();
28+
return info().createOpInstance(dependencies).object();
3229
}
3330

3431
public static List<InfoChain> chainsFrom(List<RichOp<?>> dependencies) {

scijava/scijava-ops-engine/src/test/java/org/scijava/ops/engine/impl/ProvenanceTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ public void testMappingProvenance() {
134134
Thing out = ops.op("test.provenanceMapper").input(array).outType(
135135
Thing.class).apply();
136136

137-
// Assert only one execution upon this Object
137+
// Assert two executions upon this Object, once from the mapped function, once from the mapper
138138
List<RichOp<?>> executionsUpon = ops.history().executionsUpon(out);
139-
Assert.assertEquals(1, executionsUpon.size());
139+
Assert.assertEquals(2, executionsUpon.size());
140140
}
141141

142142
@Test
@@ -176,15 +176,15 @@ public void testMappingProvenanceAndCaching() {
176176
Thing out = ops.op("test.provenanceMapper").input(array).outType(
177177
Thing.class).apply(hints);
178178

179-
// Assert only one run of the Base Op
179+
// Assert that two Ops operated on the return.
180180
List<RichOp<?>> history = ops.history().executionsUpon(out);
181-
Assert.assertEquals(1, history.size());
181+
Assert.assertEquals(2, history.size());
182182

183-
// Run the mapped Op, assert still one run on the mapper
183+
// Run the mapped Op, assert still two runs on the mapper
184184
Thing out1 = ops.op("test.provenanceMapped").input(2.).outType(Thing.class)
185185
.apply(hints);
186186
history = ops.history().executionsUpon(out);
187-
Assert.assertEquals(1, history.size());
187+
Assert.assertEquals(2, history.size());
188188
// Assert one run on the mapped Op as well
189189
history = ops.history().executionsUpon(out1);
190190
Assert.assertEquals(1, history.size());

scijava/scijava-ops-engine/src/test/java/org/scijava/ops/engine/progress/DefaultProgressTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import org.junit.Assert;
88
import org.junit.Test;
99
import org.scijava.ops.engine.AbstractTestEnvironment;
10+
import org.scijava.ops.spi.Op;
1011
import org.scijava.ops.spi.OpCollection;
12+
import org.scijava.ops.spi.OpDependency;
1113
import org.scijava.ops.spi.OpField;
1214
import org.scijava.plugin.Plugin;
1315

@@ -64,7 +66,7 @@ public void testSimpleReporter() throws InterruptedException {
6466
@Test
6567
public void testMultiStageReporter() throws InterruptedException {
6668
// obtain the Op
67-
BiFunction<Integer,Integer, Integer> op = //
69+
BiFunction<Integer, Integer, Integer> op = //
6870
ops.op("test.progressReporter") //
6971
.inType(Integer.class, Integer.class) //
7072
.outType(Integer.class) //
@@ -82,10 +84,47 @@ public void testMultiStageReporter() throws InterruptedException {
8284

8385
}
8486

87+
@Test
88+
public void testDependentReporter() throws InterruptedException {
89+
// obtain the Op
90+
Function<Integer, Integer> op = //
91+
ops.op("test.dependentProgressReporter") //
92+
.inType(Integer.class) //
93+
.outType(Integer.class) //
94+
.function();
95+
96+
int numIterations = 100;
97+
Progress.addListener(op, (t) -> {
98+
testProgress(t.progress(), 3);
99+
});
100+
Thread t = new Thread(() -> op.apply(numIterations));
101+
t.start();
102+
t.join();
103+
Assert.assertEquals(3, this.numUpdates);
104+
105+
}
106+
85107
private int numUpdates = 0;
86108

87109
private void testProgress(double progress, int numIterations) {
88110
Assert.assertEquals((double) ++numUpdates / numIterations, progress, 1e-6);
89111
}
90112

91113
}
114+
115+
@Plugin(type = Op.class, name = "test.dependentProgressReporter")
116+
class DependentProgressReportingOp implements Function<Integer, Integer> {
117+
118+
@OpDependency(name = "test.progressReporter")
119+
private Function<Integer, Integer> dependency;
120+
121+
@Override
122+
public Integer apply(Integer t) {
123+
Progress.defineTotalProgress(0, 3);
124+
for (int i = 0; i < 3; i++) {
125+
dependency.apply(t);
126+
}
127+
return 3 * t;
128+
}
129+
130+
}

0 commit comments

Comments
 (0)