Skip to content

Commit 1a0351e

Browse files
Treiblesschorlectrueden
authored andcommitted
Resolve circular service dependency
1 parent 910e928 commit 1a0351e

5 files changed

Lines changed: 22 additions & 29 deletions

File tree

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.scijava.log.LogService;
4444
import org.scijava.ops.core.Op;
4545
import org.scijava.ops.core.OpCollection;
46-
import org.scijava.ops.matcher.MatchingResult;
4746
import org.scijava.ops.matcher.OpCandidate;
4847
import org.scijava.ops.matcher.OpClassInfo;
4948
import org.scijava.ops.matcher.OpFieldInfo;
@@ -166,14 +165,14 @@ public <T> T findOpInstance(final String opName, final Nil<T> specialType, final
166165

167166
try {
168167
// Find single match which matches the specified types
169-
OpCandidate match = findTypeMatch(ref);
168+
OpCandidate match = matcher.findSingleMatch(this, ref);
170169
return (T) match.createOp(secondaryArgs);
171170
} catch (OpMatchingException e) {
172171
log.debug("No matching Op for request: " + ref + "\n");
173172
log.debug("Attempting Op transformation...");
174173

175174
// If we can't find an op matching the original request, we try to find a transformation
176-
OpTransformationCandidate transformation = transformer.findTransfromation(ref);
175+
OpTransformationCandidate transformation = transformer.findTransfromation(this, ref);
177176
if (transformation == null) {
178177
log.debug("No matching Op transformation found");
179178
throw new IllegalArgumentException(e);
@@ -211,14 +210,6 @@ public <T> T findOp(final String opName, final Nil<T> specialType, final Nil<?>[
211210
return findOpInstance(opName, specialType, inTypes, new Nil[] { outType }, secondaryArgs);
212211
}
213212

214-
public MatchingResult findTypeMatches(final OpRef ref) {
215-
return matcher.findMatch(this, ref);
216-
}
217-
218-
public OpCandidate findTypeMatch(final OpRef ref) throws OpMatchingException {
219-
return findTypeMatches(ref).singleMatch();
220-
}
221-
222213
private Type[] toTypes(Nil<?>... nils) {
223214
return Arrays.stream(nils).filter(n -> n != null).map(n -> n.getType()).toArray(Type[]::new);
224215
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public class DefaultOpTypeMatchingService extends AbstractService implements OpT
6363

6464
@Parameter
6565
private LogService log;
66+
67+
@Override
68+
public OpCandidate findSingleMatch(final OpEnvironment ops, final OpRef ref) throws OpMatchingException {
69+
return findMatch(ops, ref).singleMatch();
70+
}
6671

6772
@Override
6873
public MatchingResult findMatch(final OpEnvironment ops, final OpRef ref) {

src/main/java/org/scijava/ops/matcher/OpTypeMatchingService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
//TODO javadoc
4343
public interface OpTypeMatchingService extends SciJavaService {
4444

45+
OpCandidate findSingleMatch(final OpEnvironment ops, final OpRef ref) throws OpMatchingException;
46+
4547
MatchingResult findMatch(OpEnvironment ops, OpRef ref);
4648

4749
MatchingResult findMatch(OpEnvironment ops, List<OpRef> refs);

src/main/java/org/scijava/ops/transform/DefaultOpTransformerService.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
import java.util.ArrayList;
3636
import java.util.List;
3737

38-
import org.scijava.ops.OpService;
38+
import org.scijava.ops.OpEnvironment;
3939
import org.scijava.ops.matcher.OpCandidate;
4040
import org.scijava.ops.matcher.OpMatchingException;
4141
import org.scijava.ops.matcher.OpRef;
42+
import org.scijava.ops.matcher.OpTypeMatchingService;
4243
import org.scijava.plugin.AbstractSingletonService;
44+
import org.scijava.plugin.Parameter;
4345
import org.scijava.plugin.Plugin;
4446
import org.scijava.service.Service;
4547

@@ -53,13 +55,11 @@
5355
public final class DefaultOpTransformerService extends AbstractSingletonService<OpTransformer>
5456
implements OpTransformerService {
5557

56-
//TODO why does this not work?
57-
// @Parameter
58-
private OpService opService;
58+
@Parameter
59+
private OpTypeMatchingService matcher;
5960

6061
@Override
6162
public List<OpTransformation> getTansformationsTo(OpRef toRef) {
62-
init();
6363
List<OpTransformation> transforms = new ArrayList<>();
6464

6565
for (OpTransformer ot: getInstances()) {
@@ -72,30 +72,29 @@ public List<OpTransformation> getTansformationsTo(OpRef toRef) {
7272
}
7373

7474
@Override
75-
public OpTransformationCandidate findTransfromation(OpRef ref) {
76-
init();
75+
public OpTransformationCandidate findTransfromation(OpEnvironment opEnv, OpRef ref) {
7776
List<OpTransformation> ts = getTansformationsTo(ref);
7877
for (OpTransformation t : ts) {
79-
OpTransformationCandidate match = findTransfromation(opService, t, 0);
78+
OpTransformationCandidate match = findTransfromation(opEnv, t, 0);
8079
if (match != null) {
8180
return match;
8281
}
8382
}
8483
return null;
8584
}
8685

87-
private OpTransformationCandidate findTransfromation(OpService opService, OpTransformation candidate, int depth) {
86+
private OpTransformationCandidate findTransfromation(OpEnvironment opEnv, OpTransformation candidate, int depth) {
8887
if (candidate == null || depth > 1) {
8988
return null;
9089
} else {
9190
OpRef fromRef = candidate.getSource();
9291
try {
93-
OpCandidate match = opService.findTypeMatch(fromRef);
92+
OpCandidate match = matcher.findSingleMatch(opEnv, fromRef);
9493
return new OpTransformationCandidate(match, candidate);
9594
} catch (OpMatchingException e) {
9695
List<OpTransformation> ts = getTansformationsTo(fromRef);
9796
for (OpTransformation t : ts) {
98-
OpTransformationCandidate cand = findTransfromation(opService, t.chain(candidate), depth + 1);
97+
OpTransformationCandidate cand = findTransfromation(opEnv, t.chain(candidate), depth + 1);
9998
if (cand != null) {
10099
return cand;
101100
}
@@ -104,10 +103,4 @@ private OpTransformationCandidate findTransfromation(OpService opService, OpTran
104103
}
105104
return null;
106105
}
107-
108-
private void init() {
109-
if (opService == null) {
110-
opService = context().getService(OpService.class);
111-
}
112-
}
113106
}

src/main/java/org/scijava/ops/transform/OpTransformerService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44

5+
import org.scijava.ops.OpEnvironment;
56
import org.scijava.ops.OpService;
67
import org.scijava.ops.matcher.OpRef;
78
import org.scijava.plugin.SingletonService;
@@ -40,8 +41,9 @@ default Class<OpTransformer> getPluginType() {
4041
* Computer and return it.
4142
*
4243
* @param opService
44+
* @param opEnv the env to supply ops
4345
* @param ref the ref which should be the target of the transformation to look for
4446
* @return
4547
*/
46-
OpTransformationCandidate findTransfromation(OpRef ref);
48+
OpTransformationCandidate findTransfromation(OpEnvironment opEnv, OpRef ref);
4749
}

0 commit comments

Comments
 (0)