Skip to content

Commit 0458b28

Browse files
authored
Merge pull request #60 from scijava/scijava/scijava-ops-engine/progress-reporting-revisions
Revisions to scijava/incubator#43
2 parents 94f6a42 + 47de342 commit 0458b28

2 files changed

Lines changed: 126 additions & 94 deletions

File tree

‎scijava/scijava-ops-api/src/main/java/org/scijava/ops/api/RichOp.java‎

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,48 @@
3535

3636
/**
3737
* An {@link OpInstance} with state
38-
* <p>
39-
* Each {@link RichOp} has <b>one</b> {@link OpInstance}, and <b>one</b>
40-
* </p>
41-
*
38+
*
4239
* @author Gabriel Selzer
4340
* @param <T> the functional {@link Type} of the wrapped Op
4441
*/
4542
public interface RichOp<T> extends GenericTyped {
4643

44+
/**
45+
* Gets the {@link OpInstance} of this {@link RichOp}
46+
*
47+
* @return the {@link OpInstance} of this {@link RichOp}
48+
*/
4749
OpInstance<T> instance();
4850

4951
OpEnvironment env();
5052

5153
Hints hints();
5254

55+
/**
56+
* Getter for this {@link RichOp}'s <b>raw</b> Op instance
57+
*
58+
* @return the raw Op instance of this {@link RichOp}
59+
*/
5360
default T op() {
5461
return instance().op();
5562
}
5663

64+
/**
65+
* Getter for this {@link RichOp}'s functional {@link Type}
66+
*
67+
* @return the {@link Type} of this Op
68+
*/
69+
5770
@Override
5871
default Type getType() {
5972
return instance().getType();
6073
}
6174

75+
/**
76+
* Getter for this {@link RichOp}'s {@link InfoTree}
77+
*
78+
* @return the {@link InfoTree} describing the construction of this Op
79+
*/
6280
default InfoTree infoTree() {
6381
return instance().infoTree();
6482
}
@@ -70,11 +88,37 @@ default InfoTree infoTree() {
7088
*/
7189
T asOpType();
7290

73-
void preprocess(Object... inputs);
91+
/**
92+
* Defines behavior performed by the Op before <em>each</em> execution.
93+
*
94+
* @param inputs the inputs to the Op's functional method
95+
*/
96+
default void preprocess(Object... inputs) {
97+
// By default, do nothing
98+
}
7499

75-
void postprocess(Object output);
100+
/**
101+
* Defines behavior performed by the Op after <em>each</em> execution.
102+
*
103+
* @param output the output of the Op's functional method
104+
*/
105+
default void postprocess(Object output) {
106+
// By default, do nothing
107+
}
76108

109+
/**
110+
* Returns true iff this {@link RichOp} is logging its executions in its
111+
* {@link OpHistory}
112+
*
113+
* @return true iff it is recording its executions
114+
*/
77115
boolean isRecordingExecutions();
78116

117+
/**
118+
* Designates whether this {@link RichOp} should log its executions to its
119+
* {@link OpHistory}
120+
*
121+
* @param record tells this {@link RichOp} to record its executions iff true.
122+
*/
79123
void recordExecutions(boolean record);
80124
}

‎scijava/scijava-progress/src/test/java/org/scijava/progress/ImproperReportingTest.java‎

Lines changed: 76 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -56,87 +56,24 @@ public class ImproperReportingTest {
5656
return arr;
5757
};
5858

59-
/**
60-
* A progressible Task that defines fewer stages than it completes
61-
*/
62-
public final Supplier<Integer> tooFewStageTask = () -> {
63-
Progress.defineTotalProgress(2);
64-
int totalStages = 3;
65-
for (int i = 0; i < totalStages; i++) {
66-
Progress.setStageMax(1);
67-
Progress.update();
68-
}
69-
return totalStages;
70-
};
71-
72-
/**
73-
* A progressible task that defines more stages than it completes
74-
*/
75-
public final Supplier<Integer> tooManyStageTask = () -> {
76-
Progress.defineTotalProgress(3);
77-
int totalStages = 2;
78-
for (int i = 0; i < totalStages; i++) {
79-
Progress.setStageMax(1);
80-
Progress.update();
81-
}
82-
return totalStages;
83-
};
84-
85-
/**
86-
* A progressible task that defines fewer subtasks than it runs
87-
*
88-
* @param subtask the subtask being run
89-
* @return the summation of each subtask
90-
*/
91-
public static Integer tooFewSubTaskTask(Function<Integer, Integer> subtask) {
92-
Progress.defineTotalProgress(0, 2);
93-
return IntStream.range(0, 3) //
94-
.map(i -> subtask.apply(4)) //
95-
.sum();
96-
}
97-
98-
/**
99-
* A progressible task that defines more subtasks than it runs
100-
*
101-
* @param subtask the subtask being run
102-
* @return the summation of each subtask
103-
*/
104-
public static Integer tooManySubTaskTask(Function<Integer, Integer> subtask) {
105-
Progress.defineTotalProgress(0, 3);
106-
return IntStream.range(0, 2) //
107-
.map(i -> subtask.apply(4)) //
108-
.sum();
109-
}
110-
111-
/**
112-
* A testing subtask
113-
*/
114-
private final Function<Integer, Integer> subtask = (in) -> in;
115-
116-
/**
117-
* Tests that tasks who update progress without defining total progress result
118-
* in a thrown error.
119-
*/
120-
@Test
121-
public void testUpdateWithoutSetMax() {
122-
Function<Integer, int[]> task = arrayCreator;
123-
Progress.register(task);
124-
Assertions.assertThrows(IllegalStateException.class, () -> task.apply(3));
125-
Progress.complete();
126-
}
127-
12859
/**
12960
* Tests that tasks who updated progress past the defined maximum result in a
13061
* thrown error.
13162
*/
13263
@Test
13364
public void testDefineTooFewStages() {
134-
Supplier<Integer> task = tooFewStageTask;
135-
Assertions.assertThrows(IllegalStateException.class, () -> {
136-
Progress.register(task);
137-
task.get();
138-
Progress.complete();
139-
});
65+
assertISEFromTask( //
66+
// task
67+
() -> {
68+
// defines 2 stages
69+
Progress.defineTotalProgress(2);
70+
// but completes 3 stages
71+
for (int i = 0; i < 3; i++) {
72+
Progress.setStageMax(1);
73+
Progress.update();
74+
}
75+
return "All done";
76+
});
14077
}
14178

14279
/**
@@ -145,26 +82,47 @@ public void testDefineTooFewStages() {
14582
*/
14683
@Test
14784
public void testDefineTooManyStages() {
148-
Supplier<Integer> task = tooManyStageTask;
149-
Assertions.assertThrows(IllegalStateException.class, () -> {
150-
Progress.register(task);
151-
task.get();
152-
Progress.complete();
153-
});
85+
assertISEFromTask( //
86+
// task
87+
() -> {
88+
// defines 3 stages
89+
Progress.defineTotalProgress(3);
90+
// but completes 2 stages
91+
for (int i = 0; i < 2; i++) {
92+
Progress.setStageMax(1);
93+
Progress.update();
94+
}
95+
return "All done!";
96+
});
15497
}
15598

99+
/**
100+
* A testing subtask
101+
*/
102+
private final Function<Integer, Integer> subtask = (in) -> {
103+
return in;
104+
};
105+
156106
/**
157107
* Tests that tasks who updated progress past the defined maximum result in a
158108
* thrown error.
159109
*/
160110
@Test
161111
public void testDefineTooFewSubTasks() {
162-
Supplier<Integer> task = () -> tooFewSubTaskTask(subtask);
163-
Assertions.assertThrows(IllegalStateException.class, () -> {
164-
Progress.register(task);
165-
task.get();
166-
Progress.complete();
167-
});
112+
assertISEFromTask( //
113+
// task
114+
() -> {
115+
// define 2 subtasks
116+
Progress.defineTotalProgress(0, 2);
117+
// but complete 3 subtasks
118+
for( int i = 0; i < 3; i++) {
119+
// Call subtask
120+
Progress.register(subtask);
121+
subtask.apply(4);
122+
Progress.complete();
123+
}
124+
return "All done!";
125+
});
168126
}
169127

170128
/**
@@ -173,7 +131,37 @@ public void testDefineTooFewSubTasks() {
173131
*/
174132
@Test
175133
public void testDefineTooManySubTasks() {
176-
Supplier<Integer> task = () -> tooManySubTaskTask(subtask);
134+
assertISEFromTask( //
135+
// task
136+
() -> {
137+
// define 3 subtasks
138+
Progress.defineTotalProgress(0, 3);
139+
// but run 2 subtasks
140+
for(int i = 0; i < 2; i++) {
141+
// Call subtask
142+
Progress.register(subtask);
143+
subtask.apply(4);
144+
Progress.complete();
145+
}
146+
return "All done!";
147+
});
148+
}
149+
150+
/**
151+
* Tests that tasks who update progress without defining total progress result
152+
* in a thrown error.
153+
*/
154+
@Test
155+
public void testUpdateWithoutSetMax() {
156+
Function<Integer, int[]> task = arrayCreator;
157+
Progress.register(task);
158+
Assertions.assertThrows(IllegalStateException.class, () -> task.apply(3));
159+
Progress.complete();
160+
}
161+
162+
// -- Helper methods -- //
163+
164+
private void assertISEFromTask(Supplier<?> task) {
177165
Assertions.assertThrows(IllegalStateException.class, () -> {
178166
Progress.register(task);
179167
task.get();

0 commit comments

Comments
 (0)