Skip to content

Commit c3db7f1

Browse files
committed
Merge branch 'scijava/scijava-ops/hints'
This PR provides a two-way mechanism to deliver matching preferences (called hints) to the matcher. Hints can be declared by: * Ops, using the OpHints annotation. This data is stored within the OpInfo. * Users, using the Hints interface. A Hints object can be created by the user or can be obtained through an OpEnvironment These hints can then be used to indicate their matching preferences. For example, Ops can use the hint Simplification.FORBIDDEN to declare that it should not be simplified. Conversely, a user can pass the same hint to the matcher via a Hints object to specify that they do not want to match simplified Ops. This PR also moves the simplification matching logic (SimplifiedOpRef/SimplifiedOpInfo construction, etc.) to DefaultOpMatcher (solves part of Closes #43. See scijava/incubator#26
2 parents 784eba8 + 1532ded commit c3db7f1

41 files changed

Lines changed: 4247 additions & 354 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎scijava/scijava-ops/src/main/java/module-info.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
exports org.scijava.ops.function; // contains functional inferfaces
66
exports org.scijava.ops.core; // contains OpCollection, Op interfaces
77
exports org.scijava.ops.core.builder; // contains OpBuilder classes
8+
exports org.scijava.ops.hints;
89
exports org.scijava.ops.matcher;
910
exports org.scijava.ops.simplify;
1011
exports org.scijava.ops.conversionLoss;

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

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import java.lang.reflect.Type;
3333

3434
import org.scijava.ops.core.builder.OpBuilder;
35-
import org.scijava.ops.matcher.OpMatcher;
35+
import org.scijava.ops.hints.Hints;
3636
import org.scijava.ops.matcher.OpRef;
3737
import org.scijava.types.Nil;
3838

@@ -56,6 +56,7 @@
5656
* </ul>
5757
*
5858
* @author Curtis Rueden
59+
* @author Gabriel Selzer
5960
*/
6061
public interface OpEnvironment {
6162

@@ -64,12 +65,73 @@ public interface OpEnvironment {
6465

6566
Iterable<OpInfo> infos(String name);
6667

68+
Iterable<OpInfo> infos(Hints hints);
69+
70+
Iterable<OpInfo> infos(String name, Hints hints);
71+
6772
// TODO: Add interface method: OpInfo info(final String opName, final Nil<T> specialType, final Nil<?>[] inTypes, final Nil<?> outType);
6873

69-
<T> T op(final String opName, final Nil<T> specialType, final Nil<?>[] inTypes, final Nil<?> outType);
74+
/**
75+
* Returns an Op fitting the provided arguments. NB implementations of this
76+
* method likely depend on the {@link Hints} set by
77+
* {@link OpEnvironment#setHints(Hints)}, which provides no guarantee of
78+
* thread-safety. Users interested in parallel Op matching should consider
79+
* using {@link OpEnvironment#op(String, Nil, Nil[], Nil, Hints)} instead.
80+
*
81+
* @param <T> the {@link Type} of the Op
82+
* @param opName the name of the Op
83+
* @param specialType the generic {@link Type} of the Op
84+
* @param inTypes the arguments (inputs) to the Op
85+
* @param outType the return of the Op (note that it may also be an argument)
86+
* @return an instance of an Op aligning with the search parameters
87+
*/
88+
<T> T op(final String opName, final Nil<T> specialType,
89+
final Nil<?>[] inTypes, final Nil<?> outType);
7090

91+
/**
92+
* Returns an Op fitting the provided arguments. NB implementations of this
93+
* method likely depend on the {@link Hints} set by
94+
* {@link OpEnvironment#setHints(Hints)}, which provides no guarantee of
95+
* thread-safety. Users interested in parallel Op matching should consider
96+
* using {@link OpEnvironment#op(String, Nil, Nil[], Nil, Hints)} instead.
97+
*
98+
* @param <T> the {@link Type} of the Op
99+
* @param info the {@link OpInfo}
100+
* @param specialType the generic {@link Type} of the Op
101+
* @param inTypes the arguments (inputs) to the Op
102+
* @param outType the return of the Op (note that it may also be an argument)
103+
* @return an instance of an Op aligning with the search parameters
104+
*/
71105
<T> T op(OpInfo info, Nil<T> specialType, Nil<?>[] inTypes, Nil<?> outType);
72106

107+
/**
108+
* Returns an Op fitting the provided arguments.
109+
*
110+
* @param <T> the {@link Type} of the Op
111+
* @param opName the name of the Op
112+
* @param specialType the generic {@link Type} of the Op
113+
* @param inTypes the arguments (inputs) to the Op
114+
* @param outType the return of the Op (note that it may also be an argument)
115+
* @param hints the {@link Hints} that should guide this matching call
116+
* @return an instance of an Op aligning with the search parameters
117+
*/
118+
<T> T op(final String opName, final Nil<T> specialType,
119+
final Nil<?>[] inTypes, final Nil<?> outType, Hints hints);
120+
121+
/**
122+
* Returns an Op fitting the provided arguments.
123+
*
124+
* @param <T> the {@link Type} of the Op
125+
* @param info the {@link OpInfo}
126+
* @param specialType the generic {@link Type} of the Op
127+
* @param inTypes the arguments (inputs) to the Op
128+
* @param outType the return of the Op (note that it may also be an argument)
129+
* @param hints the {@link Hints} that should guide this matching call
130+
* @return an instance of an Op aligning with the search parameters
131+
*/
132+
<T> T op(OpInfo info, Nil<T> specialType, Nil<?>[] inTypes, Nil<?> outType,
133+
Hints hints);
134+
73135
default OpBuilder op(final String opName) {
74136
return new OpBuilder(this, opName);
75137
}
@@ -139,4 +201,22 @@ default OpBuilder op(final String opName) {
139201
* @param name
140202
*/
141203
void register(OpInfo info, String name);
204+
205+
/**
206+
* Sets the {@link Hints} for the {@link OpEnvironment}. Every Call to
207+
* {@link #op} that <b>does not</b> pass a {@link Hints} will <b>copy</b> the
208+
* Hints passed here (to prevent multiple Op calls from accessing/changing the
209+
* same {@link Hints}). In the case that <b>no {@link Hints} have been
210+
* given</b> to the Environment before {@code op} is called, the
211+
* implementation will create a suitable stand-in.
212+
* <p>
213+
* Note that this method does not inherently provide <b>any</b> guarantees
214+
* pertaining to thread-safety; this API is provided purely for convenience.
215+
* Any calls to {@link OpEnvironment#op(String, Nil, Nil[], Nil)} that require
216+
* a specific {@Hints} should <not> use this method, instead opting for
217+
* {@link OpEnvironment#op(String, Nil, Nil[], Nil, Hints)}.
218+
*
219+
* @param hints
220+
*/
221+
void setHints(Hints hints);
142222
}

‎scijava/scijava-ops/src/main/java/org/scijava/ops/OpInfo.java‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
import java.lang.reflect.AnnotatedElement;
55
import java.lang.reflect.Type;
66
import java.lang.reflect.TypeVariable;
7+
import java.util.Arrays;
8+
import java.util.Collections;
79
import java.util.List;
810
import java.util.Map;
911

1012
import org.scijava.log.Logger;
13+
import org.scijava.ops.hints.Hints;
14+
import org.scijava.ops.hints.OpHints;
15+
import org.scijava.ops.hints.impl.ImmutableHints;
1116
import org.scijava.ops.matcher.OpCandidate;
1217
import org.scijava.ops.matcher.OpRef;
1318
import org.scijava.param.ValidityException;
@@ -29,9 +34,14 @@ public interface OpInfo extends Comparable<OpInfo> {
2934

3035
/** Gets the associated {@link Struct} metadata. */
3136
Struct struct();
32-
33-
/** Describes whether this Op can be simplified. */
34-
boolean isSimplifiable();
37+
38+
/** Gets the hints declared in the {@link OpHints} annotation */
39+
Hints declaredHints();
40+
41+
default Hints formHints(OpHints h) {
42+
if (h == null) return new ImmutableHints(new String[0]);
43+
return new ImmutableHints(h.hints());
44+
}
3545

3646
/** Gets the op's input parameters. */
3747
default List<Member<?>> inputs() {

‎scijava/scijava-ops/src/main/java/org/scijava/ops/conversionLoss/IdentityLossReporter.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
package org.scijava.ops.conversionLoss;
33

44
import org.scijava.ops.core.Op;
5-
import org.scijava.ops.simplify.Unsimplifiable;
5+
import org.scijava.ops.hints.BaseOpHints.Simplification;
6+
import org.scijava.ops.hints.OpHints;
67
import org.scijava.param.Parameter;
78
import org.scijava.plugin.Plugin;
89
import org.scijava.struct.ItemIO;
@@ -14,7 +15,7 @@
1415
* @author Gabriel Selzer
1516
* @param <T> - the type that is not being simplified.
1617
*/
17-
@Unsimplifiable
18+
@OpHints(hints = {Simplification.FORBIDDEN})
1819
@Plugin(type = Op.class, name = "lossReporter")
1920
@Parameter(key = "fromType")
2021
@Parameter(key = "toType")

‎scijava/scijava-ops/src/main/java/org/scijava/ops/conversionLoss/PrimitiveArrayLossReporters.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import org.scijava.ops.OpField;
44
import org.scijava.ops.core.OpCollection;
5-
import org.scijava.ops.simplify.Unsimplifiable;
5+
import org.scijava.ops.hints.OpHints;
6+
import org.scijava.ops.hints.BaseOpHints.Simplification;
67
import org.scijava.plugin.Plugin;
78

89
@Plugin(type = OpCollection.class)
@@ -12,11 +13,11 @@ public class PrimitiveArrayLossReporters {
1213
// @Plugin(type = Op.class)
1314
// static class ByteArrayIntArrayReporter implements LosslessReporter<Byte[], Integer[]> {}
1415

15-
@Unsimplifiable
16+
@OpHints(hints = {Simplification.FORBIDDEN})
1617
@OpField(names = "lossReporter")
1718
public final LossReporter<Byte[], Integer[]> bArrIArr = (from, to) -> 0.;
1819

19-
@Unsimplifiable
20+
@OpHints(hints = {Simplification.FORBIDDEN})
2021
@OpField(names = "lossReporter")
2122
public final LossReporter<Double[], Integer[]> dArrIArr = (from, to) -> 0.;
2223

‎scijava/scijava-ops/src/main/java/org/scijava/ops/conversionLoss/PrimitiveLossReporters.java‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import org.scijava.ops.OpField;
55
import org.scijava.ops.core.OpCollection;
6-
import org.scijava.ops.simplify.Unsimplifiable;
6+
import org.scijava.ops.hints.BaseOpHints.Simplification;
7+
import org.scijava.ops.hints.OpHints;
78
import org.scijava.plugin.Plugin;
89
import org.scijava.types.Nil;
910

@@ -29,58 +30,58 @@ default Double apply(Nil<T> from, Nil<R> to) {
2930
}
3031

3132

32-
@Unsimplifiable
33+
@OpHints(hints = {Simplification.FORBIDDEN})
3334
@OpField(names = "lossReporter")
3435
public final IntToDecimalReporter<Long, Double> LongDoubleReporter = (from, to) -> {
3536
long maxValue = Long.MAX_VALUE - 1;
3637
double converted = maxValue;
3738
return (double) Math.abs(maxValue - (long) converted);
3839
};
3940

40-
@Unsimplifiable
41+
@OpHints(hints = {Simplification.FORBIDDEN})
4142
@OpField(names = "lossReporter")
4243
public final LossReporter<Double, Long> DoubleLongReporter = (from, to) -> {
4344
double maxValue = Double.MAX_VALUE;
4445
long converted = (long) maxValue;
4546
return maxValue - converted;
4647
};
4748

48-
@Unsimplifiable
49+
@OpHints(hints = {Simplification.FORBIDDEN})
4950
@OpField(names = "lossReporter")
5051
public final LossReporter<Long, Integer> LongIntegerReporter = (from, to) -> {
5152
long maxValue = Long.MAX_VALUE;
5253
int converted = (int) maxValue;
5354
return (double) Math.abs(maxValue - converted);
5455
};
5556

56-
@Unsimplifiable
57+
@OpHints(hints = {Simplification.FORBIDDEN})
5758
@OpField(names = "lossReporter")
5859
public final LossReporter<Integer, Long> IntegerLongReporter = (from, to) -> {
5960
return 0.;
6061
};
6162

62-
@Unsimplifiable
63+
@OpHints(hints = {Simplification.FORBIDDEN})
6364
@OpField(names = "lossReporter")
6465
public final IntToDecimalReporter<Integer, Double> IntegerDoubleReporter = (from, to) -> {
6566
long maxValue = Integer.MAX_VALUE;
6667
double converted = maxValue;
6768
return (double) Math.abs(maxValue - (long) converted);
6869
};
6970

70-
@Unsimplifiable
71+
@OpHints(hints = {Simplification.FORBIDDEN})
7172
@OpField(names = "lossReporter")
7273
public final LossReporter<Double, Integer> DoubleIntegerReporter = (from, to) -> {
7374
double maxValue = Double.MAX_VALUE;
7475
int converted = (int) maxValue;
7576
return maxValue - converted;
7677
};
7778

78-
@Unsimplifiable
79+
@OpHints(hints = {Simplification.FORBIDDEN})
7980
@OpField(names = "lossReporter")
8081
public final LossReporter<Number, Double> NumberDoubleReporter = (from,
8182
to) -> LongDoubleReporter.apply(Nil.of(Long.class), Nil.of(Double.class));
8283

83-
@Unsimplifiable
84+
@OpHints(hints = {Simplification.FORBIDDEN})
8485
@OpField(names = "lossReporter")
8586
public final LossReporter<Number, Long> NumberLongReporter = (from,
8687
to) -> DoubleLongReporter.apply(Nil.of(Double.class), Nil.of(Long.class));

0 commit comments

Comments
 (0)