Skip to content
This repository was archived by the owner on Oct 26, 2026. It is now read-only.

Op Reduction, Rebased - #58

Merged
hinerm merged 5 commits into
mainfrom
scijava/scijava-ops-engine/op-reduction-rebased
Nov 18, 2022
Merged

hinerm merged 5 commits into
mainfrom
scijava/scijava-ops-engine/op-reduction-rebased

Conversation

@gselzer

@gselzer gselzer commented Mar 24, 2022

Copy link
Copy Markdown
Member

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.

@ctrueden

ctrueden commented Sep 7, 2022

Copy link
Copy Markdown
Member

@gselzer I tried to get started on this PR by:

gh pr checkout 58
git reset --hard main
git cherry-pick eaf3fe75d938c14326f4f94fe027e97ec33d7ad2

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:

  1. Verify that everything still actually works.
  2. Rewrite the commit message to include all the good details written up in Enable Optional Parameters Using Reduction #32.
  3. Review the actual patch, commenting on things we should follow up on later.
  4. Merge it.

@ctrueden
ctrueden force-pushed the scijava/scijava-ops-engine/op-reduction-rebased branch from eaf3fe7 to 8ac3b05 Compare October 31, 2022 20:12
@ctrueden

Copy link
Copy Markdown
Member

I have now completed (1) and (2) above. @hinerm When next you are reviewing Ops PRs, this one is ready for a look!

@ctrueden
ctrueden requested a review from hinerm October 31, 2022 20:13
@ctrueden
ctrueden force-pushed the scijava/scijava-ops-engine/op-reduction-rebased branch from 8ac3b05 to def32d9 Compare November 8, 2022 14:44
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>
@hinerm
hinerm force-pushed the scijava/scijava-ops-engine/op-reduction-rebased branch from def32d9 to a274309 Compare November 14, 2022 20:07

@hinerm hinerm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is looking good and the tests are great to prove that everything is working!

@ctrueden

ctrueden commented Nov 16, 2022 •

Copy link
Copy Markdown
Member

@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.

@hinerm

hinerm commented Nov 16, 2022

Copy link
Copy Markdown
Member

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?

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.
@hinerm
hinerm force-pushed the scijava/scijava-ops-engine/op-reduction-rebased branch 2 times, most recently from 23dcf30 to c5b0171 Compare November 18, 2022 17:53
@hinerm
hinerm merged commit 2adac43 into main Nov 18, 2022
@hinerm
hinerm deleted the scijava/scijava-ops-engine/op-reduction-rebased branch November 18, 2022 18:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants