use of com.b2international.snowowl.core.ResourceURI in project snow-owl by b2ihealthcare.
the class ValueSetMemberSearchSnomedReferenceSetTest method filterByRefsetUri.
@Test
public void filterByRefsetUri() throws Exception {
SnomedReferenceSetMembers members = SnomedRequests.prepareSearchMember().all().filterByRefSet(Concepts.REFSET_DESCRIPTION_TYPE).build(CODESYSTEM).execute(Services.bus()).getSync(1, TimeUnit.MINUTES);
ValueSetMembers setMembers = CodeSystemRequests.prepareSearchMembers().all().filterByValueSet(ComponentURI.of(CODESYSTEM, SnomedConcept.REFSET_TYPE, Concepts.REFSET_DESCRIPTION_TYPE).toString()).buildAsync().execute(Services.bus()).getSync(1, TimeUnit.MINUTES);
assertThat(setMembers.getTotal()).isEqualTo(members.getTotal());
assertThat(setMembers.stream().allMatch(m -> SnomedContentRule.SNOMEDCT.equals(m.getReferencedComponentURI().resourceUri())));
Set<String> setMemberSourceCodes = FluentIterable.from(setMembers).transform(m -> m.getReferencedComponentURI().identifier()).toSet();
Set<String> setMemberSourceTerms = FluentIterable.from(setMembers).transform(m -> m.getReferencedComponentURI().identifier()).toSet();
assertThat(setMemberSourceCodes.contains(Concepts.TEXT_DEFINITION));
assertThat(setMemberSourceCodes.contains(Concepts.FULLY_SPECIFIED_NAME));
assertThat(setMemberSourceCodes.contains(Concepts.SYNONYM));
assertThat(setMemberSourceTerms.contains(DEFINITION));
assertThat(setMemberSourceTerms.contains(FSN));
assertThat(setMemberSourceTerms.contains(SYNONYM));
}
use of com.b2international.snowowl.core.ResourceURI in project snow-owl by b2ihealthcare.
the class SnomedConceptMapSearchRequestEvaluator method toCollectionResource.
private ConceptMapMappings toCollectionResource(SnomedReferenceSetMembers referenceSetMembers, ResourceURI uri, ServiceProvider context, Options search, SnomedDisplayTermType snomedDisplayTermType) {
final Set<String> refSetsToFetch = referenceSetMembers.stream().map(SnomedReferenceSetMember::getRefsetId).collect(Collectors.toSet());
final Map<String, SnomedConcept> refSetsById = SnomedRequests.prepareSearchConcept().all().filterByIds(refSetsToFetch).setLocales(search.getList(OptionKey.LOCALES, ExtendedLocale.class)).setExpand("pt(),referenceSet()").build(uri).execute(context.service(IEventBus.class)).getSync(1, TimeUnit.MINUTES).stream().collect(Collectors.toMap(SnomedConcept::getId, concept -> concept));
final Map<String, ComponentURI> targetComponentsByRefSetId = getTargetComponentsByRefSetId(context, refSetsById);
List<ConceptMapMapping> mappings = referenceSetMembers.stream().filter(m -> SnomedConcept.TYPE.equals(m.getReferencedComponent().getComponentType())).map(m -> {
return toMapping(m, uri, targetComponentsByRefSetId.get(m.getRefsetId()), snomedDisplayTermType, refSetsById);
}).collect(Collectors.toList());
if (!mappings.isEmpty()) {
final Map<String, Concept> conceptsById = Multimaps.index(mappings, mapping -> mapping.getTargetComponentURI().resourceUri()).asMap().entrySet().stream().filter(entry -> !TerminologyRegistry.UNSPECIFIED.equals(entry.getKey().getResourceId())).map(entry -> {
final Set<String> idsToFetch = entry.getValue().stream().map(map -> map.getTargetComponentURI().identifier()).collect(Collectors.toSet());
return CodeSystemRequests.prepareSearchConcepts().all().filterByCodeSystemUri(entry.getKey()).filterByIds(idsToFetch).buildAsync().execute(context.service(IEventBus.class)).getSync(5, TimeUnit.MINUTES).stream().collect(Collectors.toMap(Concept::getId, c -> c));
}).flatMap(map -> map.entrySet().stream()).collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue(), (concept1, concept2) -> concept1));
mappings = mappings.stream().map(mapping -> {
final String mapTargetId = mapping.getTargetComponentURI().identifier();
if (conceptsById.containsKey(mapTargetId) && !mapping.getTargetComponentURI().isUnspecified()) {
final Concept concept = conceptsById.get(mapTargetId);
return mapping.toBuilder().targetTerm(concept.getTerm()).targetIconId(concept.getIconId()).build();
} else {
return mapping;
}
}).collect(Collectors.toList());
}
return new ConceptMapMappings(mappings, referenceSetMembers.getSearchAfter(), referenceSetMembers.getLimit(), referenceSetMembers.getTotal());
}
use of com.b2international.snowowl.core.ResourceURI in project snow-owl by b2ihealthcare.
the class ValidateRequest method doValidate.
private ValidationResult doValidate(BranchContext context, Writer index) throws IOException {
final String branchPath = context.path();
ValidationRuleSearchRequestBuilder req = ValidationRequests.rules().prepareSearch();
TerminologyResource resource = context.service(TerminologyResource.class);
ResourceURI resourceURI = resource.getResourceURI(branchPath);
if (!CompareUtils.isEmpty(ruleIds)) {
req.filterByIds(ruleIds);
}
final ValidationRules rules = req.all().build().execute(context);
final ValidationThreadPool pool = context.service(ValidationThreadPool.class);
final BlockingQueue<IssuesToPersist> issuesToPersistQueue = Queues.newLinkedBlockingDeque();
final List<Promise<Object>> validationPromises = Lists.newArrayList();
// evaluate selected rules
for (ValidationRule rule : rules) {
checkArgument(rule.getCheckType() != null, "CheckType is missing for rule " + rule.getId());
final ValidationRuleEvaluator evaluator = ValidationRuleEvaluator.Registry.get(rule.getType());
if (evaluator != null) {
validationPromises.add(pool.submit(rule.getCheckType(), () -> {
Stopwatch w = Stopwatch.createStarted();
try {
LOG.info("Executing rule '{}'...", rule.getId());
final List<?> evaluationResponse = evaluator.eval(context, rule, ruleParameters);
issuesToPersistQueue.offer(new IssuesToPersist(rule.getId(), evaluationResponse));
LOG.info("Execution of rule '{}' successfully completed in '{}'.", rule.getId(), w);
// TODO report successfully executed validation rule
} catch (Exception e) {
// TODO report failed validation rule
LOG.error("Execution of rule '{}' failed after '{}'.", rule.getId(), w, e);
}
}));
}
}
final Set<String> ruleIds = rules.stream().map(ValidationRule::getId).collect(Collectors.toSet());
final Multimap<String, ComponentIdentifier> whiteListedEntries = fetchWhiteListEntries(context, ruleIds);
final Promise<List<Object>> promise = Promise.all(validationPromises);
while (!promise.isDone() || !issuesToPersistQueue.isEmpty()) {
if (!issuesToPersistQueue.isEmpty()) {
final Collection<IssuesToPersist> issuesToPersist = newArrayList();
issuesToPersistQueue.drainTo(issuesToPersist);
if (!issuesToPersist.isEmpty()) {
final List<String> rulesToPersist = issuesToPersist.stream().map(itp -> itp.ruleId).collect(Collectors.toList());
LOG.info("Persisting issues generated by rules '{}'...", rulesToPersist);
// persist new issues generated by rules so far, extending them using the Issue Extension API
int persistedIssues = 0;
final Multimap<String, ValidationIssue> issuesToExtendWithDetailsByToolingId = HashMultimap.create();
for (IssuesToPersist ruleIssues : Iterables.consumingIterable(issuesToPersist)) {
final String ruleId = ruleIssues.ruleId;
final List<ValidationIssue> existingRuleIssues = ValidationRequests.issues().prepareSearch().all().filterByResourceUri(resourceURI).filterByRule(ruleId).build().execute(context).getItems();
final Set<String> issueIdsToDelete = Sets.newHashSet();
final Map<ComponentIdentifier, ValidationIssue> existingIsssuesByComponentIdentifier = new HashMap<>();
for (ValidationIssue issue : existingRuleIssues) {
if (existingIsssuesByComponentIdentifier.containsKey(issue.getAffectedComponent())) {
issueIdsToDelete.add(issue.getId());
} else {
existingIsssuesByComponentIdentifier.put(issue.getAffectedComponent(), issue);
}
}
// remove all processed whitelist entries
final Collection<ComponentIdentifier> ruleWhiteListEntries = whiteListedEntries.removeAll(ruleId);
final String toolingId = rules.stream().filter(rule -> ruleId.equals(rule.getId())).findFirst().get().getToolingId();
for (ValidationIssueDetails issueDetails : ruleIssues.issueDetails) {
final ValidationIssue validationIssue;
ComponentIdentifier componentIdentifier = issueDetails.affectedComponentId;
if (!existingIsssuesByComponentIdentifier.containsKey(componentIdentifier)) {
validationIssue = new ValidationIssue(UUID.randomUUID().toString(), ruleId, ComponentURI.of(resourceURI, componentIdentifier), ruleWhiteListEntries.contains(componentIdentifier));
} else {
final ValidationIssue issueToCopy = existingIsssuesByComponentIdentifier.get(componentIdentifier);
validationIssue = new ValidationIssue(issueToCopy.getId(), issueToCopy.getRuleId(), ComponentURI.of(resourceURI, issueToCopy.getAffectedComponent()), ruleWhiteListEntries.contains(issueToCopy.getAffectedComponent()));
existingIsssuesByComponentIdentifier.remove(componentIdentifier);
}
validationIssue.setDetails(ValidationIssueDetails.HIGHLIGHT_DETAILS, issueDetails.stylingDetails);
issuesToExtendWithDetailsByToolingId.put(toolingId, validationIssue);
persistedIssues++;
}
existingRuleIssues.stream().filter(issue -> existingIsssuesByComponentIdentifier.containsKey(issue.getAffectedComponent())).forEach(issue -> issueIdsToDelete.add(issue.getId()));
if (!issueIdsToDelete.isEmpty()) {
index.removeAll(Collections.singletonMap(ValidationIssue.class, issueIdsToDelete));
}
}
for (String toolingId : issuesToExtendWithDetailsByToolingId.keySet()) {
final ValidationIssueDetailExtension extensions = context.service(ValidationIssueDetailExtensionProvider.class).getExtensions(toolingId);
final Collection<ValidationIssue> issues = issuesToExtendWithDetailsByToolingId.removeAll(toolingId);
extensions.extendIssues(context, issues, ruleParameters);
for (ValidationIssue issue : issues) {
index.put(issue);
}
}
index.commit();
LOG.info("Persisted '{}' issues generated by rules '{}'.", persistedIssues, rulesToPersist);
}
} else {
try {
// wait at least number of rules * 50ms for the next responses
Thread.sleep(Math.min(((long) ruleIds.size()) * 100, POLL_INTERVAL_MAX));
} catch (InterruptedException e) {
throw new SnowowlRuntimeException(e);
}
}
}
// TODO return ValidationResult object with status and new issue IDs as set
return new ValidationResult(context.info().id(), context.path());
}
use of com.b2international.snowowl.core.ResourceURI in project snow-owl by b2ihealthcare.
the class ComponentURI method of.
public static ComponentURI of(String uri) {
if (Strings.isNullOrEmpty(uri)) {
return ComponentURI.UNSPECIFIED;
}
final List<String> parts = SLASH_SPLITTER.splitToList(uri);
if (parts.size() < 4) {
throw new BadRequestException("A component uri consists of at least four parts (resourceType/resourceId/componentType/componentId). Arg was: %s", uri);
}
int terminologyComponentTypeIndex = parts.size() - 2;
int componentIdIndex = parts.size() - 1;
ResourceURI resourceURI = new ResourceURI(SLASH_JOINER.join(parts.subList(0, terminologyComponentTypeIndex)));
String componentType = parts.get(terminologyComponentTypeIndex);
String componentId = parts.get(componentIdIndex);
return new ComponentURI(resourceURI, componentType, componentId);
}
use of com.b2international.snowowl.core.ResourceURI in project snow-owl by b2ihealthcare.
the class SnomedContentRule method createCodeSystemIfNotExist.
private void createCodeSystemIfNotExist() {
final IEventBus eventBus = Services.bus();
final CodeSystems codeSystems = CodeSystemRequests.prepareSearchCodeSystem().setLimit(0).filterById(codeSystemId.getResourceId()).buildAsync().execute(eventBus).getSync();
if (codeSystems.getTotal() > 0) {
return;
}
final ResourceURI extensionOf = Optional.ofNullable(this.extensionOf).or(() -> {
return ResourceRequests.prepareSearchVersion().filterByResource(SNOMEDCT).sortBy(Sort.fieldDesc(VersionDocument.Fields.EFFECTIVE_TIME)).setLimit(1).buildAsync().execute(eventBus).getSync().first().map(Version::getVersionResourceURI);
}).orElse(null);
CodeSystemRequests.prepareNewCodeSystem().setId(codeSystemId.getResourceId()).setBranchPath(SNOMEDCT.equals(codeSystemId) ? Branch.MAIN_PATH : null).setUrl(SNOMEDCT.equals(codeSystemId) ? SnomedTerminologyComponentConstants.SNOMED_URI_SCT + "/" + Concepts.MODULE_SCT_CORE : SnomedTerminologyComponentConstants.SNOMED_URI_SCT + "/" + codeSystemId.getResourceId()).setDescription("description").setExtensionOf(extensionOf).setLanguage("ENG").setTitle(codeSystemId.getResourceId()).setOid("oid:" + codeSystemId).setOwner("https://b2i.sg").setToolingId(SnomedTerminologyComponentConstants.TOOLING_ID).setSettings(Map.of(SnomedTerminologyComponentConstants.CODESYSTEM_LANGUAGE_CONFIG_KEY, List.of(Map.of("languageTag", "en", "languageRefSetIds", Lists.newArrayList(Concepts.REFSET_LANGUAGE_TYPE_UK, Concepts.REFSET_LANGUAGE_TYPE_US))))).build(RestExtensions.USER, String.format("Create code system %s", codeSystemId)).execute(eventBus).getSync(1, TimeUnit.MINUTES);
}
Aggregations