Repository navigation
Op Reduction, Rebased - #58
Conversation
|
@gselzer I tried to get started on this PR by: because I think the only commit that is actually new here is the tip. But there are conflicts. I'll take another crack at it next week. Once I resolve the conflicts, my next steps will be:
|
eaf3fe7 to
8ac3b05
Compare
|
I have now completed (1) and (2) above. @hinerm When next you are reviewing Ops PRs, this one is ready for a look! |
8ac3b05 to
def32d9
Compare
This PR introduces the ability to declare optional parameters in an Op's
**functional method** using the Optional annotation. The motivation
behind this implementation was to maintain from ImageJ Ops the ability
to declare optional parameters without the complexity of field
injection.
Once one parameter (or more) has been declared optional for some Op,
scijava ops will create a set of ReducedOpInfos for that Op; if an Op
has n optional parameters, we will then have n+1 OpInfos that delegate
to that Op (n of which being ReducedOpInfos). These ReducedOpInfos can
then be matched, just as any other OpInfo. **We maintain ImageJ Ops'
behavior of matching subsets of optional parameters in a left-to-right
manner**; this means that if there is an OpMethod:
public Double foo(Double a, @optional Double b, @optional Double c) {}
we can match this Op using:
* a
* a and b
* a, b, and c.
We do not allow the matching of a and c, as when b and c are the same
type, passing a Double would provide no indication whether that Double
should be assigned to b or c. To be deterministic we assume that
optional parameters are always left off **right to left**.
Using this paradigm, users can write a class Op with optional parameters
as
@plugin(type = Op.class, name = "foo")
public class OptionalArgClass implements BiFunction<Double, Double, Double> {
public Double apply( Double in1, @optional in2) {
if (in2 == null) in2 = <reasonable default>;
...
}
}
When this Op is called *without* the optional parameter, the
ReducedOpInfo will use Javassist to create a *wrapper* for
OptionalArgClass, passing null for each optional parameter that is
omitted in the matched ReducedOpInfo. *note that the Op author is then
responsible for null-checking any optional arguments*.
Using the OpBuilder syntax, this Op can then be called using *either*:
Double d1 = 1.0;
Double d2 = 2.0;
Double o = ops.op("foo").input(d1, d2).outType(Double.class).apply();
or:
Double d1 = 1.0;
Double o = ops.op("foo").input(d1).outType(Double.class).apply();
Ops written as methods can similarly be written with optional
parameters:
@OpMethod(names = "bar", type = BiFunction.class)
public Double barMethod(Double in1, @optional Double in2) {
if (in2 == null) in2 = <reasonable default>;
...
}
When writing an Op as a Field, there are two options.
One can write the Op as either an anonymous Class:
@opfield(names = "foobar")
public final BiFunction<Double, Double, Double> foobar = new BiFunction<> {
public Double apply( Double in1, @optional in2) {
if (in2 == null) in2 = <reasonable default>;
...
}
}
or, with the help of a specialized interface, using a lambda:
public interface BiFunctionWithOptional<I1, I2, I3, O> extends Functions.Arity3<I1, I2, I3, O> {
@OverRide
public O apply(I1 in1, I2 in2, @optional I3 in3);
}
@opfield(names = "baz")
public final BiFunctionWithOptional<Double, Double, Double, Double> baz = (in1, in2, in3) -> {
if (in3 == null) in3 = <reasonable default>;
...
};
To promote extensibility, each OpInfo is reduced using a InfoReducer
plugin. InfoReducers are designed to be able to reduce OpInfos backed by
one of a set of functional types (for example, a FunctionReducer should
reduce all Functions). For each functional type an InfoReducer can
reduce, it should be able to produce an OpInfo for any number of
optional parameters. So if, for example, an InfoReducer is able to
reduce a Functions.Arity3, it should be able to reduce that Arity3 into
a BiFunction, a Function, and a Producer. OpInfo reduction is performed
at plugin discovery time, and the Javassist wrapper is created when the
Op is matched.
Closes scijava/scijava#14.
Signed-off-by: Curtis Rueden <ctrueden@wisc.edu>
def32d9 to
a274309
Compare
hinerm
left a comment
There was a problem hiding this comment.
Overall this is looking good and the tests are great to prove that everything is working!
|
@hinerm Thanks so much for digging into this in detail! Do you think it would make sense for you to take a crack at addressing your own comments, by pushing additional commits? They look relatively minor to me, and you already have your nose in it, whereas @gselzer would need to dust off his brain. Or if you don't feel comfortable making the changes yourself, maybe a pair programming session is in order? Alternately: up until this point we were merging every PR in the chain "as is" as long as things fundamentally work, with corrections going onto later PRs. I have no strong preference whether we do that here as well; since the changes are minor it is unlikely to seriously hose up later rebases. But noting these changes to do later on top of a further along main branch is also fine. |
yep I can just make the changes @ctrueden |
Eliminates the copy-and-paste between Function/Computer reducer and ensures consistency between both.
Hints are immutable so instead of creating a new set each time they're requested, let's cache them once.
23dcf30 to
c5b0171
Compare
This PR rebases the work of #32 on top of #57, making it much easier to merge.
Please see #32 for a description of the workings.