Skip to content

Commit 1ec5d2f

Browse files
gselzerhinerm
authored andcommitted
Test simplification hints
1 parent c123087 commit 1ec5d2f

3 files changed

Lines changed: 84 additions & 2 deletions

File tree

scijava/scijava-ops/src/main/java/org/scijava/ops/hints/impl/SimplificationHints.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class SimplificationHints extends AbstractHints {
1111

1212
private SimplificationHints(Map<String, String> map) {
1313
super(map);
14+
setHint(Simplification.IN_PROGRESS);
1415
}
1516

1617
public static SimplificationHints generateHints(Hints hints) {
@@ -22,7 +23,6 @@ public static SimplificationHints generateHints(Hints hints) {
2223

2324
// add Simplifiable.NO
2425
SimplificationHints newHints = new SimplificationHints(map);
25-
newHints.setHint(Simplification.FORBIDDEN);
2626

2727
return newHints;
2828
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import org.scijava.ops.hints.BaseOpHints.Simplification;
7272
import org.scijava.ops.hints.impl.AdaptationHints;
7373
import org.scijava.ops.hints.impl.DefaultHints;
74+
import org.scijava.ops.hints.impl.SimplificationHints;
7475
import org.scijava.ops.matcher.DefaultOpMatcher;
7576
import org.scijava.ops.matcher.MatchingResult;
7677
import org.scijava.ops.matcher.DependencyMatchingException;
@@ -409,7 +410,9 @@ private SimplifiedOpRef getSimplifiedRef(OpRef ref, Hints hints)
409410

410411
private List<OpCandidate> generateSimplifiedCandidates(SimplifiedOpRef ref, Hints hints) {
411412
Class<?> functionalType = Types.raw(ref.getType());
412-
Stream<OpInfo> infoStream = StreamSupport.stream(infos(ref.getName(), hints).spliterator(), true);
413+
Hints simplificationHints = SimplificationHints.generateHints(hints);
414+
Stream<OpInfo> infoStream = StreamSupport.stream(infos(ref.getName(),
415+
simplificationHints).spliterator(), true);
413416
List<OpCandidate> candidates = infoStream.filter(info -> Types
414417
.isAssignable(Types.raw(info.opType()), functionalType))//
415418
.map(info -> generateSimpleInfo(ref, info))//
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
package org.scijava.ops.hints;
3+
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.function.Function;
7+
8+
import org.junit.Test;
9+
import org.scijava.ops.AbstractTestEnvironment;
10+
import org.scijava.ops.OpField;
11+
import org.scijava.ops.core.OpCollection;
12+
import org.scijava.ops.hints.BaseOpHints.Adaptation;
13+
import org.scijava.ops.hints.BaseOpHints.Simplification;
14+
import org.scijava.ops.hints.impl.DefaultHints;
15+
import org.scijava.ops.matcher.OpMatchingException;
16+
import org.scijava.plugin.Plugin;
17+
18+
@Plugin(type = OpCollection.class)
19+
public class SimplificationHintTest extends AbstractTestEnvironment {
20+
21+
@OpField(names = "test.simplification.hints")
22+
public final Function<Double[], Double[]> op = (in) -> new Double[in.length];
23+
24+
@Test
25+
public void testSimplification() {
26+
// make sure we can find the Op when adaptation is allowed
27+
Hints hints = new DefaultHints();
28+
hints.setHint(Simplification.ALLOWED);
29+
ops.env().setHints(hints);
30+
@SuppressWarnings("unused")
31+
Function<Integer[], Integer[]> adaptable = ops.op(
32+
"test.simplification.hints").inType(Integer[].class).outType(
33+
Integer[].class).function();
34+
// make sure we cannot find the Op when adaptation is not allowed
35+
hints.setHint(Simplification.FORBIDDEN);
36+
ops.env().setHints(hints);
37+
try {
38+
ops.op("test.simplification.hints").inType(Integer[].class).outType(
39+
Integer[].class).function();
40+
throw new IllegalStateException(
41+
"Simplification is forbidden - this op call should not match!");
42+
}
43+
catch (IllegalArgumentException e) {
44+
assertTrue(e.getCause() instanceof OpMatchingException);
45+
}
46+
47+
}
48+
49+
@OpHints(hints = { Simplification.FORBIDDEN })
50+
@OpField(names = "test.simplification.unsimplifiable")
51+
public final Function<Double[], Double[]> nonAdaptableOp = (
52+
in) -> new Double[in.length];
53+
54+
@Test
55+
public void testUnsimplifiableOp() {
56+
// make sure we can find the Op when adaptation is allowed
57+
Hints hints = new DefaultHints();
58+
hints.setHint(Simplification.ALLOWED);
59+
ops.env().setHints(hints);
60+
@SuppressWarnings("unused")
61+
Function<Double[], Double[]> adaptable = ops.op(
62+
"test.simplification.unsimplifiable").inType(Double[].class).outType(
63+
Double[].class).function();
64+
// make sure that we cannot match the Op via adaptation even when
65+
// simplification
66+
// is allowed (since it declares itself to be unsimplifiable)
67+
try {
68+
ops.op("test.simplification.unsimplifiable").inType(Integer[].class)
69+
.outType(Integer[].class).function();
70+
throw new IllegalStateException(
71+
"The only relevant Op is not simplifiable - this op call should not match!");
72+
}
73+
catch (IllegalArgumentException e) {
74+
assertTrue(e.getCause() instanceof OpMatchingException);
75+
}
76+
77+
}
78+
79+
}

0 commit comments

Comments
 (0)