Skip to content

Commit 615f4ea

Browse files
Treiblesschorlegselzer
authored andcommitted
Refactor matching service
1 parent 252958e commit 615f4ea

7 files changed

Lines changed: 67 additions & 55 deletions

File tree

src/main/java/org/scijava/ops/base/DefaultOpMatchingService.java renamed to src/main/java/org/scijava/ops/base/DefaultOpTypeMatchingService.java

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* @author Curtis Rueden
5454
*/
5555
@Plugin(type = Service.class)
56-
public class DefaultOpMatchingService extends AbstractService implements OpMatchingService {
56+
public class DefaultOpTypeMatchingService extends AbstractService implements OpTypeMatchingService {
5757

5858
@Parameter
5959
private Context context;
@@ -64,19 +64,17 @@ public class DefaultOpMatchingService extends AbstractService implements OpMatch
6464
// -- OpMatchingService methods --
6565

6666
@Override
67-
public OpCandidate findMatch(final OpEnvironment ops, final OpRef ref) {
67+
public MatchingResult findMatch(final OpEnvironment ops, final OpRef ref) {
6868
return findMatch(ops, Collections.singletonList(ref));
6969
}
7070

7171
@Override
72-
public OpCandidate findMatch(final OpEnvironment ops, final List<OpRef> refs) {
72+
public MatchingResult findMatch(final OpEnvironment ops, final List<OpRef> refs) {
7373
// find candidates with matching name & type
7474
final List<OpCandidate> candidates = findCandidates(ops, refs);
7575
assertCandidatesFound(candidates, refs.get(0));
7676
// narrow down candidates to the exact matches
77-
final List<OpCandidate> matches = filterMatches(candidates);
78-
79-
return singleMatch(candidates, matches);
77+
return new MatchingResult(candidates, filterMatches(candidates));
8078
}
8179

8280
@Override
@@ -354,44 +352,6 @@ private int findCastLevels(final OpCandidate candidate, final Type[] paddedArgs)
354352
return level;
355353
}
356354

357-
/**
358-
* Extracts and returns the single match from the given list of matches. If
359-
* there is not exactly one match, an {@link IllegalArgumentException} is
360-
* thrown with an analysis of the problem(s).
361-
* <p>
362-
* Helper method of {@link #findMatch}.
363-
* </p>
364-
*
365-
* @param candidates
366-
* The original unfiltered list of candidates, used during the
367-
* analysis if there was a problem finding exactly one match.
368-
* @param matches
369-
* The list of matching candidates.
370-
* @return The single matching candidate, with its module initialized.
371-
* @throws IllegalArgumentException
372-
* If there is not exactly one matching candidate.
373-
*/
374-
private OpCandidate singleMatch(final List<OpCandidate> candidates, final List<OpCandidate> matches) {
375-
if (matches.size() == 1) {
376-
// a single match: initialize and return it
377-
OpCandidate match = matches.get(0);
378-
if (log.isDebug()) {
379-
log.debug("Selected '" + match.getRef().getLabel() + "' op: " + match.opInfo().opClass().getName());
380-
}
381-
382-
// // initialize the op, if appropriate
383-
// if (m.object() instanceof Initializable) {
384-
// ((Initializable) m.object()).initialize();
385-
// }
386-
387-
return matches.get(0);
388-
}
389-
390-
// final String analysis = OpUtils.matchInfo(candidates, matches);
391-
// TODO
392-
throw new IllegalArgumentException("TODO dump analysis");
393-
}
394-
395355
/**
396356
* Verifies that the given candidate's module is valid.
397357
* <p>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.scijava.ops.base;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Class representing the result from type matching done by the
7+
* {@link OpTypeMatchingService}. Contains the original candidates which match
8+
* the types specified by {@link OpRef} and the final matches that match all
9+
* inputs, outputs, and arguments.
10+
*
11+
* @author David Kolb
12+
*/
13+
public class MatchingResult {
14+
15+
private final List<OpCandidate> candidates;
16+
private final List<OpCandidate> matches;
17+
18+
public MatchingResult(final List<OpCandidate> candidates, final List<OpCandidate> matches) {
19+
this.candidates = candidates;
20+
this.matches = matches;
21+
}
22+
23+
public List<OpCandidate> getCandidates() {
24+
return candidates;
25+
}
26+
27+
public List<OpCandidate> getMatches() {
28+
return matches;
29+
}
30+
31+
public OpCandidate singleMatch() {
32+
if (matches.size() == 1) {
33+
// if (log.isDebug()) {
34+
// log.debug("Selected '" + match.getRef().getLabel() + "' op: " +
35+
// match.opInfo().opClass().getName());
36+
// }
37+
38+
// TODO: DO we still need this initialization?
39+
// // initialize the op, if appropriate
40+
// if (m.object() instanceof Initializable) {
41+
// ((Initializable) m.object()).initialize();
42+
// }
43+
44+
return matches.get(0);
45+
}
46+
47+
// final String analysis = OpUtils.matchInfo(candidates, matches);
48+
// TODO
49+
throw new IllegalArgumentException("TODO dump analysis");
50+
}
51+
}

‎src/main/java/org/scijava/ops/base/OpCandidate.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636

3737
/**
3838
* Container class for a possible operation match between an {@link OpRef} and
39-
* an {@link OpInfo}, as computed by the {@link OpMatchingService}.
39+
* an {@link OpInfo}, as computed by the {@link OpTypeMatchingService}.
4040
*
4141
* @author Curtis Rueden
42-
* @see OpMatchingService
42+
* @see OpTypeMatchingService
4343
*/
4444
public class OpCandidate {
4545

‎src/main/java/org/scijava/ops/base/OpMatchingUtil.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.scijava.types.Nil;
3939

4040
/**
41-
* Utility class for {@link OpMatchingService}.
41+
* Utility class for {@link OpTypeMatchingService}.
4242
*
4343
* @author Leon Yang
4444
*/

‎src/main/java/org/scijava/ops/base/OpRef.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* Data structure which identifies an op by name and/or type(s) and/or argument
4141
* type(s), along with a list of input arguments.
4242
* <p>
43-
* With the help of the {@link OpMatchingService}, an {@code OpRef} holds all
43+
* With the help of the {@link OpTypeMatchingService}, an {@code OpRef} holds all
4444
* information needed to create an appropriate {@link Op}.
4545
* </p>
4646
*

‎src/main/java/org/scijava/ops/base/OpService.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.lang.reflect.Type;
3232
import java.util.ArrayList;
3333
import java.util.Collection;
34+
import java.util.List;
3435

3536
import org.scijava.InstantiableException;
3637
import org.scijava.log.LogService;
@@ -57,16 +58,16 @@ public class OpService extends AbstractService implements SciJavaService, OpEnvi
5758
private PluginService pluginService;
5859

5960
@Parameter
60-
private OpMatchingService matcher;
61+
private OpTypeMatchingService matcher;
6162

6263
@Parameter
6364
private LogService log;
6465

6566
public StructInstance<?> op(OpRef ref) {
66-
final OpCandidate match = matcher.findMatch(this, ref);
67-
return match.createOp();
67+
final MatchingResult match = matcher.findMatch(this, ref);
68+
return match.singleMatch().createOp();
6869
}
69-
70+
7071
@Override
7172
public Collection<OpInfo> infos() {
7273
// TODO: Consider maintaining an efficient OpInfo data structure.

src/main/java/org/scijava/ops/base/OpMatchingService.java renamed to src/main/java/org/scijava/ops/base/OpTypeMatchingService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*
4242
* @author Curtis Rueden
4343
*/
44-
public interface OpMatchingService extends SciJavaService {
44+
public interface OpTypeMatchingService extends SciJavaService {
4545

4646
/**
4747
* Finds and initializes the best module matching the given op name and/or
@@ -58,7 +58,7 @@ public interface OpMatchingService extends SciJavaService {
5858
* if there is no match, or if there is more than one match at
5959
* the same priority.
6060
*/
61-
OpCandidate findMatch(OpEnvironment ops, OpRef ref);
61+
MatchingResult findMatch(OpEnvironment ops, OpRef ref);
6262

6363
/**
6464
* Finds and initializes the best module matching any of the given op name
@@ -75,7 +75,7 @@ public interface OpMatchingService extends SciJavaService {
7575
* if there is no match, or if there is more than one match at
7676
* the same priority.
7777
*/
78-
OpCandidate findMatch(OpEnvironment ops, List<OpRef> refs);
78+
MatchingResult findMatch(OpEnvironment ops, List<OpRef> refs);
7979

8080
/**
8181
* Builds a list of candidate ops which might match the given op reference.

0 commit comments

Comments
 (0)