Search in sources :

Example 1 with ComponentIdentifier

use of com.b2international.snowowl.core.ComponentIdentifier in project snow-owl by b2ihealthcare.

the class BranchCompareRequestTest method compareBranchWithDeletedComponents.

@Test
public void compareBranchWithDeletedComponents() throws Exception {
    final Set<ComponentIdentifier> componentIdsToDelete = prepareBranchWithNewChanges(branchPath);
    final String taskBranchPath = createBranch(branchPath, "taskBranch");
    final ComponentIdentifier concept = componentIdsToDelete.stream().filter(ci -> ci.getComponentType() == SnomedConcept.TYPE).findFirst().get();
    SnomedRequests.prepareDeleteConcept(concept.getComponentId()).build(taskBranchPath, RestExtensions.USER, "Delete concept on task branch").execute(bus).getSync();
    // compare task branch and its parent
    final BranchCompareResult compare = compare(branchPath, taskBranchPath);
    assertThat(compare.getNewComponents()).isEmpty();
    assertThat(compare.getChangedComponents()).isEmpty();
    assertThat(compare.getDeletedComponents()).containsAll(componentIdsToDelete);
    assertTrue("branchPath^ expression search should return the original concept state", SnomedRequests.prepareSearchConcept().one().filterById(concept.getComponentId()).build(taskBranchPath + RevisionIndex.BASE_REF_CHAR).execute(bus).getSync().first().isPresent());
    assertTrue("branchPath expression search should return nothing", SnomedRequests.prepareSearchConcept().one().filterById(concept.getComponentId()).build(taskBranchPath).execute(bus).getSync().first().isEmpty());
}
Also used : BranchCompareResult(com.b2international.snowowl.core.branch.compare.BranchCompareResult) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) Test(org.junit.Test)

Example 2 with ComponentIdentifier

use of com.b2international.snowowl.core.ComponentIdentifier in project snow-owl by b2ihealthcare.

the class BranchCompareRequestTest method prepareBranchWithNewChanges.

private Set<ComponentIdentifier> prepareBranchWithNewChanges(String branchPath) {
    final IBranchPath branch = BranchPathUtils.createPath(branchPath);
    final String newConceptId = createNewConcept(branch);
    final SnomedConcept concept = getComponent(branch, SnomedComponentType.CONCEPT, newConceptId, "descriptions(expand(members())),relationships()").extract().as(SnomedConcept.class);
    final Set<ComponentIdentifier> newIds = newHashSet();
    newIds.add(ComponentIdentifier.of(SnomedConcept.TYPE, concept.getId()));
    for (SnomedDescription description : concept.getDescriptions()) {
        newIds.add(ComponentIdentifier.of(SnomedDescription.TYPE, description.getId()));
        for (SnomedReferenceSetMember member : description.getMembers()) {
            newIds.add(ComponentIdentifier.of(SnomedReferenceSetMember.TYPE, member.getId()));
        }
    }
    for (SnomedRelationship relationship : concept.getRelationships()) {
        newIds.add(ComponentIdentifier.of(SnomedRelationship.TYPE, relationship.getId()));
    }
    return newIds;
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) IBranchPath(com.b2international.snowowl.core.api.IBranchPath)

Example 3 with ComponentIdentifier

use of com.b2international.snowowl.core.ComponentIdentifier in project snow-owl by b2ihealthcare.

the class SnomedCompareRestRequestTest method prepareNewChanges.

private Set<ComponentIdentifier> prepareNewChanges(String conceptId, IBranchPath branchPath) {
    final SnomedConcept concept = getComponent(branchPath, SnomedComponentType.CONCEPT, conceptId, "descriptions(expand(members())),relationships()").extract().as(SnomedConcept.class);
    final Set<ComponentIdentifier> newIds = newHashSet();
    newIds.add(ComponentIdentifier.of(SnomedConcept.TYPE, concept.getId()));
    for (SnomedDescription description : concept.getDescriptions()) {
        newIds.add(ComponentIdentifier.of(SnomedDescription.TYPE, description.getId()));
        for (SnomedReferenceSetMember member : description.getMembers()) {
            newIds.add(ComponentIdentifier.of(SnomedReferenceSetMember.TYPE, member.getId()));
        }
    }
    for (SnomedRelationship relationship : concept.getRelationships()) {
        newIds.add(ComponentIdentifier.of(SnomedRelationship.TYPE, relationship.getId()));
    }
    return newIds;
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept)

Example 4 with ComponentIdentifier

use of com.b2international.snowowl.core.ComponentIdentifier in project snow-owl by b2ihealthcare.

the class ValidationRepositoryContext method commit.

void commit() {
    if (!newObjects.isEmpty() || !objectsToDelete.isEmpty()) {
        final Set<String> ruleIdsAffectedByDeletion = Sets.newHashSet();
        service(ValidationRepository.class).write(writer -> {
            writer.putAll(newObjects);
            final Multimap<String, ComponentIdentifier> addToWhitelist = HashMultimap.create();
            newObjects.stream().filter(ValidationWhiteList.class::isInstance).map(ValidationWhiteList.class::cast).forEach(whitelist -> addToWhitelist.put(whitelist.getRuleId(), whitelist.getComponentIdentifier()));
            if (!addToWhitelist.isEmpty()) {
                ExpressionBuilder filter = Expressions.builder();
                for (String ruleId : addToWhitelist.keySet()) {
                    filter.should(Expressions.builder().filter(Expressions.exactMatch(ValidationIssue.Fields.RULE_ID, ruleId)).filter(Expressions.matchAny(ValidationIssue.Fields.AFFECTED_COMPONENT_ID, addToWhitelist.get(ruleId).stream().map(ComponentIdentifier::getComponentId).collect(Collectors.toSet()))).build());
                }
                writer.bulkUpdate(new BulkUpdate<>(ValidationIssue.class, filter.build(), ValidationIssue.Scripts.WHITELIST, ImmutableMap.of("whitelisted", true)));
            }
            final Multimap<String, ComponentIdentifier> removeFromWhitelist = HashMultimap.create();
            ValidationRequests.whiteList().prepareSearch().all().filterByIds(ImmutableSet.copyOf(objectsToDelete.get(ValidationWhiteList.class))).build().execute(this).forEach(whitelist -> removeFromWhitelist.put(whitelist.getRuleId(), whitelist.getComponentIdentifier()));
            if (!removeFromWhitelist.isEmpty()) {
                ExpressionBuilder filter = Expressions.builder();
                for (String ruleId : removeFromWhitelist.keySet()) {
                    ruleIdsAffectedByDeletion.add(ruleId);
                    filter.should(Expressions.builder().filter(Expressions.exactMatch(ValidationIssue.Fields.RULE_ID, ruleId)).filter(Expressions.matchAny(ValidationIssue.Fields.AFFECTED_COMPONENT_ID, removeFromWhitelist.get(ruleId).stream().map(ComponentIdentifier::getComponentId).collect(Collectors.toSet()))).build());
                }
                writer.bulkUpdate(new BulkUpdate<>(ValidationIssue.class, filter.build(), ValidationIssue.Scripts.WHITELIST, ImmutableMap.of("whitelisted", false)));
            }
            final Map<Class<?>, Set<String>> docsToDelete = newHashMap();
            objectsToDelete.asMap().forEach((type, ids) -> docsToDelete.put(type, ImmutableSet.copyOf(ids)));
            writer.removeAll(docsToDelete);
            writer.commit();
            return null;
        });
        if (!newObjects.isEmpty()) {
            final Set<String> addedWhiteLists = newHashSet();
            final Set<String> affectedRuleIds = newHashSet();
            newObjects.forEach((doc) -> {
                if (doc instanceof ValidationWhiteList) {
                    ValidationWhiteList whitelistedDoc = (ValidationWhiteList) doc;
                    affectedRuleIds.add(whitelistedDoc.getRuleId());
                    addedWhiteLists.add(whitelistedDoc.getId());
                }
            });
            if (!addedWhiteLists.isEmpty()) {
                WhiteListNotification.added(addedWhiteLists, affectedRuleIds).publish(service(IEventBus.class));
            }
        }
        if (!objectsToDelete.isEmpty()) {
            final Set<String> removedWhiteLists = ImmutableSet.copyOf(objectsToDelete.get(ValidationWhiteList.class));
            if (!removedWhiteLists.isEmpty()) {
                WhiteListNotification.removed(removedWhiteLists, ruleIdsAffectedByDeletion).publish(service(IEventBus.class));
            }
        }
    }
}
Also used : Set(java.util.Set) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) ValidationWhiteList(com.b2international.snowowl.core.validation.whitelist.ValidationWhiteList) ValidationIssue(com.b2international.snowowl.core.validation.issue.ValidationIssue) IEventBus(com.b2international.snowowl.eventbus.IEventBus)

Example 5 with ComponentIdentifier

use of com.b2international.snowowl.core.ComponentIdentifier 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());
}
Also used : ValidationIssueDetailExtensionProvider(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtensionProvider) java.util(java.util) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) Stopwatch(com.google.common.base.Stopwatch) Promise(com.b2international.snowowl.core.events.util.Promise) LoggerFactory(org.slf4j.LoggerFactory) ValidationIssue(com.b2international.snowowl.core.validation.issue.ValidationIssue) ValidationIssueDetailExtension(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtension) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) Permission(com.b2international.snowowl.core.identity.Permission) CompareUtils(com.b2international.commons.CompareUtils) ValidationRuleSearchRequestBuilder(com.b2international.snowowl.core.validation.rule.ValidationRuleSearchRequestBuilder) com.google.common.collect(com.google.common.collect) ValidationRepository(com.b2international.snowowl.core.internal.validation.ValidationRepository) ResourceURI(com.b2international.snowowl.core.ResourceURI) ValidationRule(com.b2international.snowowl.core.validation.rule.ValidationRule) ValidationWhiteListSearchRequestBuilder(com.b2international.snowowl.core.validation.whitelist.ValidationWhiteListSearchRequestBuilder) ValidationThreadPool(com.b2international.snowowl.core.internal.validation.ValidationThreadPool) Logger(org.slf4j.Logger) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Request(com.b2international.snowowl.core.events.Request) ComponentURI(com.b2international.snowowl.core.uri.ComponentURI) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java.util.stream.Collectors) Writer(com.b2international.index.Writer) ValidationRuleEvaluator(com.b2international.snowowl.core.validation.eval.ValidationRuleEvaluator) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) ValidationRules(com.b2international.snowowl.core.validation.rule.ValidationRules) BranchContext(com.b2international.snowowl.core.domain.BranchContext) ValidationRuleSearchRequestBuilder(com.b2international.snowowl.core.validation.rule.ValidationRuleSearchRequestBuilder) ValidationThreadPool(com.b2international.snowowl.core.internal.validation.ValidationThreadPool) Stopwatch(com.google.common.base.Stopwatch) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) ValidationIssueDetailExtension(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtension) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ResourceURI(com.b2international.snowowl.core.ResourceURI) ValidationIssue(com.b2international.snowowl.core.validation.issue.ValidationIssue) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) IOException(java.io.IOException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) ValidationRule(com.b2international.snowowl.core.validation.rule.ValidationRule) Promise(com.b2international.snowowl.core.events.util.Promise) ValidationRules(com.b2international.snowowl.core.validation.rule.ValidationRules) ValidationRuleEvaluator(com.b2international.snowowl.core.validation.eval.ValidationRuleEvaluator) ValidationIssueDetailExtensionProvider(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtensionProvider)

Aggregations

ComponentIdentifier (com.b2international.snowowl.core.ComponentIdentifier)13 BranchCompareResult (com.b2international.snowowl.core.branch.compare.BranchCompareResult)6 Test (org.junit.Test)6 ValidationIssue (com.b2international.snowowl.core.validation.issue.ValidationIssue)2 ValidationWhiteListSearchRequestBuilder (com.b2international.snowowl.core.validation.whitelist.ValidationWhiteListSearchRequestBuilder)2 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)2 SnomedDescription (com.b2international.snowowl.snomed.core.domain.SnomedDescription)2 SnomedRelationship (com.b2international.snowowl.snomed.core.domain.SnomedRelationship)2 SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)2 CompareUtils (com.b2international.commons.CompareUtils)1 Writer (com.b2international.index.Writer)1 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)1 ObjectId (com.b2international.index.revision.ObjectId)1 RevisionCompare (com.b2international.index.revision.RevisionCompare)1 RevisionCompareDetail (com.b2international.index.revision.RevisionCompareDetail)1 RevisionIndex (com.b2international.index.revision.RevisionIndex)1 ResourceURI (com.b2international.snowowl.core.ResourceURI)1 TerminologyResource (com.b2international.snowowl.core.TerminologyResource)1 IBranchPath (com.b2international.snowowl.core.api.IBranchPath)1 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)1