use of com.b2international.snowowl.core.internal.validation.ValidationThreadPool 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.internal.validation.ValidationThreadPool in project snow-owl by b2ihealthcare.
the class SnomedValidationIssueDetailTest method setup.
@Before
public void setup() {
final Index index = Indexes.createIndex(UUID.randomUUID().toString(), getMapper(), new Mappings(ValidationRule.class, ValidationIssue.class, ValidationWhiteList.class));
repository = new ValidationRepository(index);
ClassPathScanner scanner = new ClassPathScanner("com.b2international");
context = TestBranchContext.on(MAIN).with(ObjectMapper.class, getMapper()).with(EclParser.class, new DefaultEclParser(INJECTOR.getInstance(IParser.class), INJECTOR.getInstance(IResourceValidator.class))).with(EclSerializer.class, new DefaultEclSerializer(INJECTOR.getInstance(ISerializer.class))).with(Index.class, rawIndex()).with(RevisionIndex.class, index()).with(ValidationThreadPool.class, new ValidationThreadPool(1, 1, 1)).with(ValidationRepository.class, repository).with(ClassPathScanner.class, scanner).with(ValidationIssueDetailExtensionProvider.class, new ValidationIssueDetailExtensionProvider(scanner)).with(ResourceURIPathResolver.class, ResourceURIPathResolver.fromMap(Map.of("SNOMEDCT", Branch.MAIN_PATH))).build();
evaluator = new SnomedQueryValidationRuleEvaluator();
if (!ValidationRuleEvaluator.Registry.types().contains(evaluator.type())) {
ValidationRuleEvaluator.Registry.register(evaluator);
}
context.service(ValidationIssueDetailExtensionProvider.class).addExtension(new SnomedValidationIssueDetailExtension());
}
use of com.b2international.snowowl.core.internal.validation.ValidationThreadPool in project snow-owl by b2ihealthcare.
the class BaseValidationTest method setup.
@Before
public final void setup() {
final ClassPathScanner scanner = new ClassPathScanner("com.b2international");
Builder context = TestBranchContext.on(MAIN).with(ClassLoader.class, getClass().getClassLoader()).with(ClassPathScanner.class, scanner).with(Index.class, rawIndex()).with(RevisionIndex.class, index()).with(ObjectMapper.class, getMapper()).with(ValidationRepository.class, new ValidationRepository(rawIndex())).with(ValidationThreadPool.class, new ValidationThreadPool(1, 1, 1)).with(ValidationIssueDetailExtensionProvider.class, new ValidationIssueDetailExtensionProvider(scanner)).with(TerminologyRegistry.class, TerminologyRegistry.INSTANCE).with(ResourceURIPathResolver.class, ResourceURIPathResolver.fromMap(getTestCodeSystemPathMap())).with(ScriptEngine.Registry.class, new ScriptEngine.Registry(scanner));
configureContext(context);
this.context = context.build();
initializeData();
}
Aggregations