Skip to content

Commit f5ea08e

Browse files
authored
Merge pull request #132 from scijava/scijava-ops-api/history-hint
Add Hint to control writing to Op history
2 parents c264a05 + 86829ca commit f5ea08e

File tree

19 files changed

+197
-204
lines changed

19 files changed

+197
-204
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ InfoTree infoTree(final String opName, final Nil<?> specialType,
183183
InfoTree infoTree(final String opName, final Nil<?> specialType,
184184
final Nil<?>[] inTypes, final Nil<?> outType, Hints hints);
185185

186-
<T> T opFromInfoChain(InfoTree tree, Nil<T> specialType);
186+
default <T> T opFromInfoChain(InfoTree tree, Nil<T> specialType) {
187+
return opFromInfoChain(tree, specialType, getDefaultHints());
188+
}
189+
190+
<T> T opFromInfoChain(InfoTree tree, Nil<T> specialType, Hints hints);
187191

188192
/**
189193
* Returns an Op fitting the provided arguments.

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,8 @@ static OpHistory getOpHistory() {
7777
*/
7878
List<RichOp<?>> executionsUpon(Object o);
7979

80-
/**
81-
* Returns the hierarchy of {@link OpInfo}s describing the dependency tree of
82-
* the {@link Object} {@code op}.
83-
*
84-
* @param op the {@link Object} returned by a matching call. NB {@code op}
85-
* <b>must</b> be the {@link Object} returned by the outermost
86-
* matching call, as the dependency {@link Object}s are not recorded.
87-
* @return the {@link InfoTree} describing the dependency tree
88-
*/
89-
InfoTree infoTree(Object op);
90-
91-
default String signatureOf(Object op) {
92-
return infoTree(op).signature();
93-
}
94-
9580
// -- HISTORY MAINTENANCE API -- //
9681

97-
/**
98-
* Logs the creation of {@link RichOp}
99-
*
100-
* @param op the {@link RichOp} containing relevant information
101-
*/
102-
void logOp(RichOp<?> op);
103-
10482
/**
10583
* Logs the {@link Object} output of the {@link RichOp} {@code op}.
10684
*

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,38 +67,37 @@ public static <T> RichOp<T> rich(T op) {
6767
}
6868

6969
/**
70-
* Convenience function for getting the {@link OpInfo} of {@code op}
70+
* Convenience function for getting the {@link InfoTree} behind {@code op}
7171
*
7272
* @param op the Op
73-
* @return the {@link OpInfo} that generated {@code op}
74-
* @param <T> the type of {@code op}
73+
* @return the {@link InfoTree} of {@code op}
7574
* @throws IllegalArgumentException if {@code op} is not an Op
7675
*/
77-
public static <T> OpInfo info(T op) {
78-
return rich(op).instance().infoTree().info();
76+
public static InfoTree infoTree(Object op) {
77+
return rich(op).instance().infoTree();
7978
}
8079

8180
/**
82-
* Convenience function for accessing {@link RichOp#recordExecutions(boolean)}
81+
* Convenience function for getting the {@link OpInfo} of {@code op}
8382
*
8483
* @param op the Op
85-
* @param record true iff {@code op} should record its executions
86-
* @param <T> the type of the Op
84+
* @return the {@link OpInfo} that generated {@code op}
8785
* @throws IllegalArgumentException if {@code op} is not an Op
8886
*/
89-
public static <T> void recordExecutions(T op, boolean record) {
90-
rich(op).recordExecutions(record);
87+
public static OpInfo info(Object op) {
88+
return infoTree(op).info();
9189
}
9290

9391
/**
94-
* Convenience function for accessing {@link RichOp#isRecordingExecutions()}
92+
* Convenience function for getting the signature of {@code op}
9593
*
9694
* @param op the Op
97-
* @param <T> the type of the Op
98-
* @return true iff Op is recording its executions
95+
* @return the signature of {@code op}, which can be used to completely
96+
* restore {@code op}
97+
* @throws IllegalArgumentException if {@code op} is not an Op
9998
*/
100-
public static <T> boolean isRecordingExecutions(T op) {
101-
return isRich(op) && rich(op).isRecordingExecutions();
99+
public static String signature(Object op) {
100+
return infoTree(op).signature();
102101
}
103102

104103
}

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,4 @@ default void preprocess(Object... inputs) {
107107
default void postprocess(Object output) {
108108
// By default, do nothing
109109
}
110-
111-
/**
112-
* Returns true iff this {@link RichOp} is logging its executions in its
113-
* {@link OpHistory}
114-
*
115-
* @return true iff it is recording its executions
116-
*/
117-
boolean isRecordingExecutions();
118-
119-
/**
120-
* Designates whether this {@link RichOp} should log its executions to its
121-
* {@link OpHistory}
122-
*
123-
* @param record tells this {@link RichOp} to record its executions iff true.
124-
*/
125-
void recordExecutions(boolean record);
126110
}

scijava-ops-engine/src/main/java/org/scijava/ops/engine/BaseOpHints.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,15 @@ private Progress() {
9797

9898
}
9999

100+
public static final class History {
101+
102+
private History() {
103+
// Prevent instantiation of static utility class
104+
}
105+
106+
public static final String PREFIX = "history";
107+
public static final String IGNORE = PREFIX + ".IGNORE";
108+
109+
}
110+
100111
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,17 @@ public <T> T opFromSignature(final String signature,
238238
}
239239

240240
@Override
241-
public <T> T opFromInfoChain(final InfoTree tree, final Nil<T> specialType) {
241+
public <T> T opFromInfoChain(final InfoTree tree, final Nil<T> specialType,
242+
Hints hints)
243+
{
242244
if (!(specialType.getType() instanceof ParameterizedType))
243245
throw new IllegalArgumentException("TODO");
244246
@SuppressWarnings("unchecked")
245247
OpInstance<T> instance = (OpInstance<T>) tree.newInstance(specialType
246248
.getType());
247249
var conditions = MatchingConditions.from( //
248250
new InfoMatchingOpRequest(tree.info(), Nil.of(tree.info().opType())), //
249-
getDefaultHints() //
251+
hints //
250252
);
251253
RichOp<T> wrappedOp = wrapOp(instance, conditions);
252254
return wrappedOp.asOpType();
@@ -623,7 +625,8 @@ private List<RichOp<?>> resolveOpDependencies(OpInfo info,
623625
Hints baseDepHints = hints //
624626
.plus( //
625627
BaseOpHints.DependencyMatching.IN_PROGRESS, //
626-
BaseOpHints.Simplification.FORBIDDEN //
628+
BaseOpHints.Simplification.FORBIDDEN, //
629+
BaseOpHints.History.IGNORE //
627630
).minus(BaseOpHints.Progress.TRACK);
628631
// Then, match dependencies
629632
final List<RichOp<?>> dependencyChains = new ArrayList<>();
@@ -769,7 +772,7 @@ public void setDefaultHints(Hints hints) {
769772
@Override
770773
public Hints getDefaultHints() {
771774
if (environmentHints != null) return environmentHints.copy();
772-
return new Hints(BaseOpHints.Progress.TRACK);
775+
return new Hints();
773776
}
774777

775778
@Override

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ public class DefaultOpHistory implements OpHistory {
6464

6565
// -- DATA STRCUTURES -- //
6666

67-
/**
68-
* {@link Map} responsible for recording the {@link InfoTree} of
69-
* {@link OpInfo}s involved to produce the result of a particular matching
70-
* call
71-
*/
72-
private final Map<RichOp<?>, InfoTree> dependencyChain = new WeakHashMap<>();
73-
7467
private final Map<Object, List<RichOp<?>>> mutationMap = new WeakHashMap<>();
7568

7669
// -- USER API -- //
@@ -89,22 +82,8 @@ public List<RichOp<?>> executionsUpon(Object o) {
8982
return mutationMap.getOrDefault(o, Collections.emptyList());
9083
}
9184

92-
@Override
93-
public InfoTree infoTree(Object op) {
94-
if (op instanceof RichOp<?>) {
95-
return dependencyChain.get(op);
96-
}
97-
throw new IllegalArgumentException("Object " + op +
98-
" is not an Op known to this OpHistory!");
99-
}
100-
10185
// -- HISTORY MAINTENANCE API -- //
10286

103-
@Override
104-
public void logOp(RichOp<?> op) {
105-
dependencyChain.put(op, op.infoTree());
106-
}
107-
10887
@Override
10988
public void logOutput(RichOp<?> op, Object output) {
11089
if (!mutationMap.containsKey(output)) updateList(output);

scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/adapt/AdaptationMatchingRoutine.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,11 @@
4545
import org.scijava.ops.api.Hints;
4646
import org.scijava.ops.api.InfoTree;
4747
import org.scijava.ops.api.OpMatchingException;
48-
import org.scijava.ops.engine.OpCandidate;
48+
import org.scijava.ops.engine.*;
4949
import org.scijava.ops.engine.OpCandidate.StatusCode;
50-
import org.scijava.ops.engine.OpDependencyMember;
5150
import org.scijava.ops.api.OpEnvironment;
5251
import org.scijava.ops.api.OpInfo;
5352
import org.scijava.ops.api.OpRequest;
54-
import org.scijava.ops.engine.BaseOpHints.Adaptation;
55-
import org.scijava.ops.engine.DependencyMatchingException;
56-
import org.scijava.ops.engine.MatchingConditions;
5753
import org.scijava.ops.engine.matcher.MatchingRoutine;
5854
import org.scijava.ops.engine.matcher.OpMatcher;
5955
import org.scijava.ops.engine.matcher.impl.DefaultOpRequest;
@@ -73,8 +69,10 @@ public class AdaptationMatchingRoutine implements MatchingRoutine {
7369
public void checkSuitability(MatchingConditions conditions)
7470
throws OpMatchingException
7571
{
76-
if (conditions.hints().containsAny(Adaptation.IN_PROGRESS,
77-
Adaptation.FORBIDDEN)) //
72+
if (conditions.hints().containsAny( //
73+
BaseOpHints.Adaptation.IN_PROGRESS, //
74+
BaseOpHints.Adaptation.FORBIDDEN //
75+
)) //
7876
throw new OpMatchingException(
7977
"Adaptation is not suitable: Adaptation is disabled");
8078
}
@@ -103,7 +101,10 @@ public void checkSuitability(MatchingConditions conditions)
103101
public OpCandidate findMatch(MatchingConditions conditions, OpMatcher matcher,
104102
OpEnvironment env) throws OpMatchingException
105103
{
106-
Hints adaptationHints = conditions.hints().plus(Adaptation.IN_PROGRESS);
104+
Hints adaptationHints = conditions.hints().plus( //
105+
BaseOpHints.Adaptation.IN_PROGRESS, //
106+
BaseOpHints.History.IGNORE //
107+
);
107108
List<Exception> matchingExceptions = new ArrayList<>();
108109
List<DependencyMatchingException> depExceptions = new ArrayList<>();
109110
for (final OpInfo adaptor : env.infos("engine.adapt")) {

scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/AbstractRichOp.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ public AbstractRichOp(final OpInstance<T> instance, final OpEnvironment env,
6363
this.instance = instance;
6464
this.env = env;
6565
this.conditions = conditions;
66-
67-
this.env.history().logOp(this);
6866
}
6967

7068
@Override
@@ -99,22 +97,12 @@ public void preprocess(Object... inputs) {
9997

10098
@Override
10199
public void postprocess(Object output) {
102-
if (record) {
100+
if (!hints().contains(BaseOpHints.History.IGNORE)) {
103101
env.history().logOutput(this, output);
104102
}
105103
Progress.complete();
106104
}
107105

108-
@Override
109-
public boolean isRecordingExecutions() {
110-
return record;
111-
}
112-
113-
@Override
114-
public void recordExecutions(boolean record) {
115-
this.record = record;
116-
}
117-
118106
@Override
119107
public String toString() {
120108
return instance().toString();

scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/simplify/FocusedInfoTreeGenerator.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,8 @@
3737
import java.util.function.Function;
3838

3939
import org.scijava.function.Computers;
40-
import org.scijava.ops.api.InfoTree;
41-
import org.scijava.ops.api.OpEnvironment;
42-
import org.scijava.ops.api.OpInfo;
43-
import org.scijava.ops.api.Ops;
44-
import org.scijava.ops.api.RichOp;
40+
import org.scijava.ops.api.*;
41+
import org.scijava.ops.engine.BaseOpHints;
4542
import org.scijava.ops.engine.InfoTreeGenerator;
4643
import org.scijava.types.Nil;
4744
import org.scijava.types.Types;
@@ -66,14 +63,15 @@ public InfoTree generate(OpEnvironment env, String signature,
6663
// Proceed to input simplifiers
6764
List<RichOp<Function<?, ?>>> reqSimplifiers = new ArrayList<>();
6865
String reqSimpComp = components.get(compIndex);
66+
Hints dependencyHints = new Hints(BaseOpHints.History.IGNORE);
6967
while (reqSimpComp.startsWith(FocusedOpInfo.INPUT_SIMPLIFIER_DELIMITER)) {
7068
String reqSimpSignature = reqSimpComp.substring(
7169
FocusedOpInfo.INPUT_SIMPLIFIER_DELIMITER.length());
7270
InfoTree reqSimpChain = InfoTreeGenerator.generateDependencyTree(env,
7371
reqSimpSignature, idMap, generators);
7472
reqSimplifiers.add(Ops.rich(env.opFromInfoChain(reqSimpChain,
7573
new Nil<>()
76-
{})));
74+
{}, dependencyHints)));
7775
reqSimpComp = components.get(++compIndex);
7876
}
7977

@@ -89,7 +87,7 @@ public InfoTree generate(OpEnvironment env, String signature,
8987
outFocuserSignature, idMap, generators);
9088
RichOp<Function<?, ?>> outputFocuser = Ops.rich(env.opFromInfoChain(
9189
outputFocuserTree, new Nil<>()
92-
{}));
90+
{}, dependencyHints));
9391

9492
// Proceed to output copier
9593
RichOp<Computers.Arity1<?, ?>> copier = null;
@@ -103,7 +101,8 @@ public InfoTree generate(OpEnvironment env, String signature,
103101
if (!outCopySignature.isEmpty()) {
104102
InfoTree copierTree = InfoTreeGenerator.generateDependencyTree(env,
105103
outCopySignature, idMap, generators);
106-
copier = Ops.rich(env.opFromInfoChain(copierTree, new Nil<>() {}));
104+
copier = Ops.rich(env.opFromInfoChain(copierTree, new Nil<>() {},
105+
dependencyHints));
107106
}
108107

109108
// Proceed to original info

0 commit comments

Comments
 (0)