Skip to content

Commit ffe0410

Browse files
Treiblesschorlegselzer
authored andcommitted
Fix op order according to priority and add test
1 parent 5404f8e commit ffe0410

3 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎src/main/java/org/scijava/ops/matcher/DefaultOpTypeMatchingService.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.lang.reflect.TypeVariable;
3434
import java.util.ArrayList;
3535
import java.util.Collections;
36+
import java.util.Comparator;
3637
import java.util.HashMap;
3738
import java.util.List;
3839
import java.util.function.Predicate;
@@ -51,6 +52,8 @@
5152
import org.scijava.util.Types;
5253
import org.scijava.util.Types.TypeVarInfo;
5354

55+
import com.google.common.collect.Lists;
56+
5457
/**
5558
* Default service for finding ops which match a request.
5659
*
@@ -103,7 +106,12 @@ public List<OpCandidate> findCandidates(final OpEnvironment ops, final List<OpRe
103106
@Override
104107
public List<OpCandidate> filterMatches(final List<OpCandidate> candidates) {
105108
final List<OpCandidate> validCandidates = checkCandidates(candidates);
106-
109+
110+
// List of valid candidates needs to be sorted according to priority.
111+
// This is used as an optimization in order to not look at ops with
112+
// lower priority than the already found one.
113+
validCandidates.sort((c1, c2) -> Double.compare(c1.opInfo().priority(), c2.opInfo().priority()));
114+
107115
List<OpCandidate> matches;
108116
matches = filterMatches(validCandidates, (cand) -> typesPerfectMatch(cand));
109117
if (!matches.isEmpty())
@@ -175,7 +183,8 @@ private List<OpCandidate> checkCandidates(final List<OpCandidate> candidates) {
175183

176184
/**
177185
* Filters specified list of candidates using specified predicate. This
178-
* method stops filtering when the priority decreases.
186+
* method stops filtering when the priority decreases. Expects list of candidates
187+
* to be sorted in non-ascending order.
179188
*
180189
* @param candidates
181190
* the candidates to filter

‎src/main/java/org/scijava/ops/math/MathOpCollection.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class MathOpCollection {
2424
@Parameter(key = "result", type = ItemIO.OUTPUT)
2525
public static final BiFunction<Double, Double, Double> addDoublesFunction = (x, y) -> x + y;
2626

27-
@OpField(names = MathOps.ADD, priority = Priority.HIGH)
27+
@OpField(names = MathOps.ADD, priority = Priority.EXTREMELY_HIGH)
2828
@Parameter(key = "number1")
2929
@Parameter(key = "number2")
3030
@Parameter(key = "result", type = ItemIO.OUTPUT)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* #%L
3+
* SciJava Operations: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2018 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.ops;
31+
import static org.junit.Assert.assertTrue;
32+
import java.util.function.BiFunction;
33+
34+
import org.junit.Test;
35+
import org.scijava.param.ValidityException;
36+
import org.scijava.types.Nil;
37+
38+
public class OpPriorityTest extends AbstractTestEnvironment {
39+
40+
Nil<Double> nilDouble = new Nil<Double>() {
41+
};
42+
43+
@Test
44+
public void testOpCollection() throws ValidityException {
45+
BiFunction<Double, Double, Double> divFunction = ops().findOp( //
46+
"math.add", new Nil<BiFunction<Double, Double, Double>>() {
47+
}, //
48+
new Nil[] { nilDouble, nilDouble }, //
49+
nilDouble//
50+
);
51+
52+
// The found op is assumed to come from the MathOpCollection
53+
assertTrue(divFunction.getClass().getName().contains("MathOpCollection$$Lambda"));
54+
}
55+
}

0 commit comments

Comments
 (0)