Skip to content

Commit a7c2308

Browse files
gselzerhinerm
authored andcommitted
Add dependencyException
This allows us to be more clear about the dependency chain that was not able to be matched
1 parent f400f8c commit a7c2308

3 files changed

Lines changed: 233 additions & 8 deletions

File tree

‎scijava/scijava-ops/src/main/java/org/scijava/ops/impl/DefaultOpEnvironment.java‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.scijava.ops.function.Computers.Arity1;
6969
import org.scijava.ops.matcher.DefaultOpMatcher;
7070
import org.scijava.ops.matcher.MatchingResult;
71+
import org.scijava.ops.matcher.DependencyMatchingException;
7172
import org.scijava.ops.matcher.MatchingUtils;
7273
import org.scijava.ops.matcher.OpAdaptationInfo;
7374
import org.scijava.ops.matcher.OpCandidate;
@@ -501,11 +502,14 @@ private List<Object> resolveOpDependencies(OpInfo info, Map<TypeVariable<?>, Typ
501502
final OpRef dependencyRef = inferOpRef(dependency, typeVarAssigns);
502503
try {
503504
resolvedDependencies.add(findOpInstance(dependencyRef, dependency.isAdaptable(), false));
504-
} catch (final Exception e) {
505-
throw new OpMatchingException("Could not find Op that matches requested Op dependency:" + "\nOp class: "
506-
+ info.implementationName() + //
507-
"\nDependency identifier: " + dependency.getKey() + //
508-
"\n\n Attempted request:\n" + dependencyRef, e);
505+
}
506+
catch (final OpMatchingException e) {
507+
String message = DependencyMatchingException.message(info
508+
.implementationName(), dependency.getKey(), dependencyRef);
509+
if (e instanceof DependencyMatchingException) {
510+
throw new DependencyMatchingException(message, (DependencyMatchingException) e);
511+
}
512+
throw new DependencyMatchingException(message);
509513
}
510514
}
511515
return resolvedDependencies;
@@ -532,6 +536,7 @@ private List<Object> resolveOpDependencies(OpInfo info, Map<TypeVariable<?>, Typ
532536
*/
533537
private OpCandidate adaptOp(OpRef ref) throws OpMatchingException {
534538

539+
List<DependencyMatchingException> depExceptions = new ArrayList<>();
535540
for (final OpInfo adaptor : infos("adapt")) {
536541
Type adaptTo = adaptor.output().getType();
537542
Map<TypeVariable<?>, Type> map = new HashMap<>();
@@ -572,14 +577,24 @@ private OpCandidate adaptOp(OpRef ref) throws OpMatchingException {
572577
adaptedCandidate.setStatus(StatusCode.MATCH);
573578
return adaptedCandidate;
574579
}
580+
catch (DependencyMatchingException d) {
581+
depExceptions.add(d);
582+
}
575583
catch (OpMatchingException e1) {
576584
log.trace(e1);
577585
}
578586
}
579587

580-
// no adaptors available.
581-
throw new OpMatchingException(
582-
"Op adaptation failed: no adaptable Ops of type " + ref.getName());
588+
//no adaptors available.
589+
if (depExceptions.size() == 0) {
590+
throw new OpMatchingException(
591+
"Op adaptation failed: no adaptable Ops of type " + ref.getName());
592+
}
593+
StringBuilder sb = new StringBuilder();
594+
for (DependencyMatchingException d : depExceptions) {
595+
sb.append("\n\n" + d.getMessage());
596+
}
597+
throw new DependencyMatchingException(sb.toString());
583598
}
584599

585600
private boolean adaptOpOutputSatisfiesRefTypes(Type adaptTo, Map<TypeVariable<?>, Type> map, OpRef ref) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
package org.scijava.ops.matcher;
3+
4+
/**
5+
* An {@link OpMatchingException} caused by another {@code OpMatchingException}
6+
* thrown when resolving dependencies. We illustrate the need for
7+
* {@code DependencyMatchingException} with an example: Suppose Op {@code A} has
8+
* an dependency on Op {@code B}, which in turn has a dependency on Op
9+
* {@code C}. Suppose further that {@code A} and {@code B} are found, but no
10+
* {@code C} is found to satisfy {@code B}. This will result in an
11+
* {@link OpMatchingException} thrown for Op {@code A}. We would want to know:
12+
* <ol>
13+
* <li>that the request for {@code A} was not fulfilled
14+
* <li>that no match could be found for {@code C}, which was the cause for (1)
15+
* </ol>
16+
* This logic can be generalized for an arbitrarily long Op chain.
17+
*
18+
* TODO: Consider supporting non-DependencyMatchingException {@code cause}s.
19+
*
20+
* @author Gabriel Selzer
21+
*/
22+
public class DependencyMatchingException extends OpMatchingException {
23+
24+
public DependencyMatchingException(final String message) {
25+
super(message);
26+
}
27+
28+
public static String message(String dependentOp, final String dependencyName, OpRef dependency) {
29+
return "Could not instantiate dependency of: " + dependentOp +
30+
"\nDependency identifier: " + dependencyName + //
31+
"\n\nAttempted request:\n" + dependency;
32+
}
33+
34+
public DependencyMatchingException(final String message,
35+
final DependencyMatchingException cause)
36+
{
37+
super(message, cause);
38+
}
39+
40+
@Override
41+
public String getMessage() {
42+
if (getCause() == null) return super.getMessage();
43+
return super.getMessage() + "\n\nCause:\n\n" +
44+
getCause().getMessage();
45+
}
46+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package org.scijava.ops.matcher;
2+
3+
import java.util.function.Function;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.scijava.ops.function.Computers;
8+
import org.scijava.ops.AbstractTestEnvironment;
9+
import org.scijava.ops.OpDependency;
10+
import org.scijava.ops.OpField;
11+
import org.scijava.ops.core.Op;
12+
import org.scijava.ops.core.OpCollection;
13+
import org.scijava.param.Parameter;
14+
import org.scijava.plugin.Plugin;
15+
import org.scijava.struct.ItemIO;
16+
17+
@Plugin(type = OpCollection.class)
18+
public class DefaultMatchingErrorTest extends AbstractTestEnvironment {
19+
20+
@OpField(names = "test.duplicateOp")
21+
public final Function<Double, Double> duplicateA = (in) -> in;
22+
23+
@OpField(names = "test.duplicateOp")
24+
public final Function<Double, Double> duplicateB = (in) -> in;
25+
26+
@Test
27+
public void duplicateErrorRegressionTest() {
28+
try {
29+
ops.op("test.duplicateOp").inType(Double.class).outType(Double.class)
30+
.function();
31+
Assert.fail();
32+
}
33+
catch (IllegalArgumentException e) {
34+
Assert.assertTrue(e.getMessage().startsWith(
35+
"org.scijava.ops.matcher.OpMatchingException: " +
36+
"Multiple 'test.duplicateOp/java.util.function.Function<java.lang.Double, java.lang.Double>' " +
37+
"ops of priority 0.0:"));
38+
}
39+
}
40+
41+
@Test
42+
public void missingDependencyRegressionTest() {
43+
try {
44+
ops.op("test.missingDependencyOp").input(1.).outType(Double.class)
45+
.apply();
46+
Assert.fail("Expected DependencyMatchingException");
47+
}
48+
catch (IllegalArgumentException e) {
49+
Throwable cause = e.getCause();
50+
Assert.assertTrue(cause instanceof DependencyMatchingException);
51+
String message = cause.getMessage();
52+
Assert.assertTrue(message.contains("Name: \"test.nonexistingOp\""));
53+
}
54+
}
55+
56+
/**
57+
* Assert the DependencyMatchingException contains both dependencies in the chain
58+
*/
59+
@Test
60+
public void missingNestedDependencyRegressionTest() {
61+
try {
62+
ops.op("test.outsideOp").input(1.).outType(Double.class).apply();
63+
Assert.fail("Expected DependencyMatchingException");
64+
}
65+
catch (IllegalArgumentException e) {
66+
Throwable cause = e.getCause();
67+
Assert.assertTrue(cause instanceof DependencyMatchingException);
68+
String message = cause.getMessage();
69+
Assert.assertTrue(message.contains("Name: \"test.missingDependencyOp\""));
70+
Assert.assertTrue(message.contains("Name: \"test.nonexistingOp\""));
71+
}
72+
}
73+
74+
@Test
75+
public void missingDependencyViaAdaptationTest() {
76+
Double[] d = new Double[0];
77+
try {
78+
ops.op("test.adaptMissingDep").input(d).outType(Double[].class).apply();
79+
Assert.fail("Expected DependencyMatchingException");
80+
} catch (IllegalArgumentException e) {
81+
Throwable cause = e.getCause();
82+
Assert.assertTrue(cause instanceof DependencyMatchingException);
83+
String message = cause.getMessage();
84+
Assert.assertTrue(message.contains("adapted"));
85+
Assert.assertTrue(message.contains("Name: \"test.nonexistingOp\""));
86+
87+
}
88+
}
89+
90+
}
91+
92+
@Plugin(type = Op.class, name = "test.furtherOutsideOp")
93+
@Parameter(key = "in")
94+
@Parameter(key = "out", itemIO = ItemIO.OUTPUT)
95+
class FurtherDependentOp implements Function<Double, Double> {
96+
97+
@OpDependency(name = "test.outsideOp")
98+
private Function<Double, Double> op;
99+
100+
@Override
101+
public Double apply(Double t) {
102+
return op.apply(t);
103+
}
104+
105+
}
106+
107+
@Plugin(type = Op.class, name = "test.outsideOp")
108+
@Parameter(key = "in")
109+
@Parameter(key = "out", itemIO = ItemIO.OUTPUT)
110+
class DependentOp implements Function<Double, Double> {
111+
112+
@OpDependency(name = "test.missingDependencyOp")
113+
private Function<Double, Double> op;
114+
115+
@Override
116+
public Double apply(Double t) {
117+
return op.apply(t);
118+
}
119+
120+
}
121+
122+
@Plugin(type = Op.class, name = "test.missingDependencyOp")
123+
@Parameter(key = "in")
124+
@Parameter(key = "out", itemIO = ItemIO.OUTPUT)
125+
class MissingDependencyOp implements Function<Double, Double> {
126+
127+
@OpDependency(name = "test.nonexistingOp")
128+
private Function<Double, Double> op;
129+
130+
@Override
131+
public Double apply(Double t) {
132+
return op.apply(t);
133+
}
134+
135+
}
136+
137+
@Plugin(type = Op.class, name = "test.adaptMissingDep")
138+
@Parameter(key = "in")
139+
@Parameter(key = "out", itemIO = ItemIO.OUTPUT)
140+
class MissingDependencyOpArr1 implements Computers.Arity1<Double[], Double[]> {
141+
142+
@OpDependency(name = "test.nonexistingOp")
143+
private Function<Double, Double> op;
144+
145+
@Override
146+
public void compute(Double[] t, Double[] out) {
147+
}
148+
149+
}
150+
151+
@Plugin(type = Op.class, name = "test.adaptMissingDep")
152+
@Parameter(key = "in")
153+
@Parameter(key = "out", itemIO = ItemIO.OUTPUT)
154+
class MissingDependencyOpArr2 implements Function<Double, Double> {
155+
156+
@OpDependency(name = "test.nonexistingOp")
157+
private Function<Double, Double> op;
158+
159+
@Override
160+
public Double apply(Double t) {
161+
return op.apply(t);
162+
}
163+
164+
}

0 commit comments

Comments
 (0)