Skip to content

Commit

Permalink
refactor(snomed.reasoner): Turn equivalent concept merger...
Browse files Browse the repository at this point in the history
...implementations into components, introduce registry
  • Loading branch information
apeteri committed May 17, 2024
1 parent bd29612 commit 4d2dc94
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import com.b2international.index.Index;
import com.b2international.snowowl.core.RepositoryManager;
import com.b2international.snowowl.core.config.SnowOwlConfiguration;
import com.b2international.snowowl.core.plugin.ClassPathScanner;
import com.b2international.snowowl.core.plugin.Component;
import com.b2international.snowowl.core.repository.TerminologyRepositoryConfigurer;
import com.b2international.snowowl.core.setup.Environment;
import com.b2international.snowowl.core.setup.Plugin;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration;
import com.b2international.snowowl.snomed.reasoner.classification.ClassificationTracker;
import com.b2international.snowowl.snomed.reasoner.equivalence.IEquivalentConceptMerger;
import com.b2international.snowowl.snomed.reasoner.index.*;

/**
Expand All @@ -45,8 +47,10 @@ public void run(final SnowOwlConfiguration configuration, final Environment env)
final int maximumReasonerRuns = snomedConfig.getMaxReasonerRuns();
final long classificationCleanUpInterval = snomedConfig.getClassificationCleanUpInterval();
final ClassificationTracker classificationTracker = new ClassificationTracker(repositoryIndex, maximumReasonerRuns, TimeUnit.MINUTES.toMillis(classificationCleanUpInterval));

env.services().registerService(ClassificationTracker.class, classificationTracker);

final ClassPathScanner scanner = env.service(ClassPathScanner.class);
env.services().registerService(IEquivalentConceptMerger.Registry.class, new IEquivalentConceptMerger.Registry(scanner));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
package com.b2international.snowowl.snomed.reasoner.equivalence;

import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.b2international.snowowl.core.plugin.ClassPathScanner;
import com.b2international.snowowl.core.plugin.Component;
import com.b2international.snowowl.snomed.core.domain.SnomedConcept;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;

/**
* Implementations of this interface can define a customized way for
Expand All @@ -45,6 +50,16 @@ public interface IEquivalentConceptMerger {
*/
String PREFIX_UPDATED = "U_";

/**
* @return the name of this equivalent concept merging strategy (must be unique for each implementation)
*/
String getName();

/**
* @return the priority of the implementation, highest priority wins
*/
int getPriority();

/**
* Adds changes to the specified bulk request builder that effectively merges
* equivalent concepts into their corresponding suggested replacement.
Expand Down Expand Up @@ -84,11 +99,36 @@ public interface IEquivalentConceptMerger {
*
* @since 6.14
*/
@Component
class Default implements IEquivalentConceptMerger {

@Override
public String getName() {
return "default";

Check warning on line 107 in snomed/com.b2international.snowowl.snomed.reasoner/src/com/b2international/snowowl/snomed/reasoner/equivalence/IEquivalentConceptMerger.java

View check run for this annotation

Codecov / codecov/patch

snomed/com.b2international.snowowl.snomed.reasoner/src/com/b2international/snowowl/snomed/reasoner/equivalence/IEquivalentConceptMerger.java#L107

Added line #L107 was not covered by tests
}

@Override
public int getPriority() {
return 0;

Check warning on line 112 in snomed/com.b2international.snowowl.snomed.reasoner/src/com/b2international/snowowl/snomed/reasoner/equivalence/IEquivalentConceptMerger.java

View check run for this annotation

Codecov / codecov/patch

snomed/com.b2international.snowowl.snomed.reasoner/src/com/b2international/snowowl/snomed/reasoner/equivalence/IEquivalentConceptMerger.java#L112

Added line #L112 was not covered by tests
}

@Override
public Set<String> merge(final Multimap<SnomedConcept, SnomedConcept> equivalentConcepts) {
return Collections.emptySet();
}
}

final class Registry {
private final List<IEquivalentConceptMerger> implsByPriority;

public Registry(ClassPathScanner scanner) {
this.implsByPriority = Ordering.natural()
.onResultOf(IEquivalentConceptMerger::getPriority)
.immutableSortedCopy(scanner.getComponentsByInterface(IEquivalentConceptMerger.class));
}

public IEquivalentConceptMerger getHighestPriority() {
return Iterables.getLast(implsByPriority);

Check warning on line 131 in snomed/com.b2international.snowowl.snomed.reasoner/src/com/b2international/snowowl/snomed/reasoner/equivalence/IEquivalentConceptMerger.java

View check run for this annotation

Codecov / codecov/patch

snomed/com.b2international.snowowl.snomed.reasoner/src/com/b2international/snowowl/snomed/reasoner/equivalence/IEquivalentConceptMerger.java#L131

Added line #L131 was not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@
import java.util.Set;
import java.util.stream.Collectors;

import jakarta.validation.constraints.NotNull;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import jakarta.validation.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -49,7 +46,6 @@
import com.b2international.snowowl.core.identity.User;
import com.b2international.snowowl.core.internal.locks.DatastoreLockContextDescriptions;
import com.b2international.snowowl.core.locks.Locks;
import com.b2international.snowowl.core.plugin.Extensions;
import com.b2international.snowowl.core.repository.RepositoryRequests;
import com.b2international.snowowl.core.request.CommitResult;
import com.b2international.snowowl.snomed.common.SnomedRf2Headers;
Expand All @@ -69,6 +65,9 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

/**
* Represents a request that saves pre-recorded changes of a classification,
* usually running in a remote job.
Expand Down Expand Up @@ -439,15 +438,10 @@ private Set<String> mergeEquivalentConcepts(final BranchContext context,
return Collections.emptySet();
}

IEquivalentConceptMerger merger = Extensions.getFirstPriorityExtension(
IEquivalentConceptMerger.EXTENSION_POINT,
IEquivalentConceptMerger.class);
if (merger == null) {
merger = new IEquivalentConceptMerger.Default();
}

final String mergerName = merger.getClass().getSimpleName();
LOG.info("Reasoner service will use {} for equivalent concept merging.", mergerName);
final IEquivalentConceptMerger.Registry mergerRegistry = context.service(IEquivalentConceptMerger.Registry.class);
final IEquivalentConceptMerger merger = mergerRegistry.getHighestPriority();
final String mergerName = merger.getName();
LOG.info("Reasoner service will use '{}' implementation for equivalent concept merging.", mergerName);

Check warning on line 444 in snomed/com.b2international.snowowl.snomed.reasoner/src/com/b2international/snowowl/snomed/reasoner/request/SaveJobRequest.java

View check run for this annotation

Codecov / codecov/patch

snomed/com.b2international.snowowl.snomed.reasoner/src/com/b2international/snowowl/snomed/reasoner/request/SaveJobRequest.java#L441-L444

Added lines #L441 - L444 were not covered by tests

final Set<String> conceptIdsToSkip = merger.merge(equivalentConcepts);
final Set<String> conceptIdsToKeep = equivalentConcepts.keySet()
Expand Down

0 comments on commit 4d2dc94

Please sign in to comment.