Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | import * as vscode from 'vscode';
import { DataFactory as N3DataFactory } from 'n3';
import { DatasetCore, Quad, Term } from '@rdfjs/types';
import { RdfStore } from 'rdf-stores';
import { Validator } from 'shacl-engine';
import { container } from 'tsyringe';
import { _RDF, _SH, _XSD, Store } from '@faubulous/mentor-rdf';
import { QuadContext } from '@faubulous/mentor-rdf-parsers';
import { TurtleDocument } from '@src/languages';
import { ServiceToken } from '@src/services/tokens';
import { IDocumentContextService } from '@src/services/document';
import { IDocumentContext } from '@src/services/document/document-context.interface';
import { getConfig } from '@src/utilities/vscode/config';
import { WorkspaceUri } from '@src/providers/workspace-uri';
import { ShaclDiagnosticsMapper } from './shacl-diagnostics-mapper';
import { migrateShaclValidationConfig, resolveEffectiveShapeGraphs, ShaclValidationConfiguration } from './shacl-validation-configuration';
/**
* A read-only DatasetCore view over a subset of graphs in the internal Store.
* Avoids copying triples by delegating match() and iteration directly to the store.
*/
// TODO: Move this into mentor-rdf
class StoreDatasetView implements DatasetCore {
private readonly _graphUris: readonly string[];
private readonly _s: Term | null;
private readonly _p: Term | null;
private readonly _o: Term | null;
constructor(
private readonly _store: Store,
graphUris: readonly string[],
s: Term | null = null,
p: Term | null = null,
o: Term | null = null
) {
this._graphUris = graphUris;
this._s = s;
this._p = p;
this._o = o;
}
get size(): number {
let n = 0;
for (const _ of this) {
n++;
}
return n;
}
add(_quad: Quad): this {
return this;
}
delete(_quad: Quad): this {
return this;
}
has(quad: Quad): boolean {
for (const q of this) {
if (q.equals(quad)) {
return true;
}
}
return false;
}
match(s?: Term | null, p?: Term | null, o?: Term | null, g?: Term | null): DatasetCore {
const graphUris = g != null
? (this._graphUris.includes(g.value) ? [g.value] : [])
: this._graphUris as string[];
return new StoreDatasetView(this._store, graphUris, s ?? null, p ?? null, o ?? null);
}
[Symbol.iterator](): Iterator<Quad> {
return this._store.matchAll(
this._graphUris as string[],
this._s as any,
this._p as any,
this._o as any,
false
);
}
}
/**
* A combined RDF/JS factory that provides DataFactory methods plus a dataset() method,
* which is required by shacl-engine's Validator.
*/
const rdfFactory = {
...N3DataFactory,
// N3's literal() throws when passed null (vs. undefined) as the language/datatype argument.
// shacl-engine calls factory.literal(text, message.language || null) when there is no
// language tag, so we normalize null to undefined here.
literal(value: string, languageOrDataType?: any) {
return N3DataFactory.literal(value, languageOrDataType ?? undefined);
},
dataset(): DatasetCore {
return RdfStore.createDefault().asDataset();
}
};
/**
* Result of a SHACL validation operation.
*/
export interface ShaclValidationResult {
/**
* Whether the data conforms to all shapes.
*/
conforms: boolean;
/**
* The validation report as an RDF dataset.
*/
reportDataset: DatasetCore;
/**
* Individual validation results.
*/
results: ShaclValidationResultEntry[];
}
/**
* An individual SHACL validation result entry.
*/
export interface ShaclValidationResultEntry {
/**
* The focus node that was validated.
*/
focusNode: string;
/**
* The severity of the violation (sh:Violation, sh:Warning, sh:Info).
*/
severity: string;
/**
* The constraint component that triggered the result.
*/
constraintComponent: string;
/**
* The result message(s).
*/
messages: string[];
/**
* The result path (property), if applicable.
*/
path?: string;
/**
* The value that caused the violation, if applicable.
*/
value?: string;
/**
* The source shape URI.
*/
sourceShape: string;
}
/**
* Service for validating RDF documents against SHACL shapes.
*/
export class ShaclValidationService implements vscode.Disposable {
private readonly _diagnosticCollection: vscode.DiagnosticCollection;
private readonly _diagnosticsMapper: ShaclDiagnosticsMapper;
private readonly _disposables: vscode.Disposable[] = [];
/**
* Stores the last validation result per document URI for report export.
*/
private readonly _lastResults = new Map<string, ShaclValidationResult>();
private readonly _onDidValidate = new vscode.EventEmitter<vscode.Uri>();
/**
* Fired when a validation completes (or results are cleared) for a document.
*/
readonly onDidValidate: vscode.Event<vscode.Uri> = this._onDidValidate.event;
private get _store() {
return container.resolve<Store>(ServiceToken.Store);
}
private get _contextService() {
return container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
}
constructor() {
this._diagnosticCollection = vscode.languages.createDiagnosticCollection('mentor-shacl');
this._diagnosticsMapper = new ShaclDiagnosticsMapper();
const context = container.resolve<vscode.ExtensionContext>(ServiceToken.ExtensionContext);
context.subscriptions.push(this);
// Clear diagnostics when a document is closed.
this._disposables.push(
vscode.workspace.onDidCloseTextDocument(doc => {
this._diagnosticCollection.delete(doc.uri);
this._lastResults.delete(doc.uri.toString());
this._onDidValidate.fire(doc.uri);
})
);
}
/**
* Get the effective shape graph URIs for a given document.
*/
getEffectiveShapeGraphs(documentUri: vscode.Uri): string[] {
const shaclConfig = getConfig('shacl');
const validationConfig = shaclConfig.get<ShaclValidationConfiguration>('validation', {});
const wsUri = WorkspaceUri.toWorkspaceUri(documentUri);
const key = wsUri ? WorkspaceUri.toCanonicalString(wsUri) : documentUri.toString();
return resolveEffectiveShapeGraphs(validationConfig, key);
}
/**
* Validate a document against the specified shape files.
* @param documentUri The URI of the document to validate.
* @param shapeFileUris Workspace-relative paths to SHACL shape files. If empty, effective shapes are used.
* @returns The validation result or undefined if no shapes are available.
*/
async validateDocument(documentUri: vscode.Uri, shapeFileUris?: string[]): Promise<ShaclValidationResult | undefined> {
const context = this._contextService.contexts[documentUri.toString()];
if (!context) {
vscode.window.showInformationMessage('No document context available. Please open the document first.');
return undefined;
}
const shapeGraphUris = shapeFileUris?.length ? shapeFileUris : this.getEffectiveShapeGraphs(documentUri);
if (shapeGraphUris.length === 0) {
vscode.window.showInformationMessage('No SHACL shape files configured for this document.');
return undefined;
}
for (const graphUri of shapeGraphUris) {
if (!this._store.hasGraph(graphUri)) {
vscode.window.showWarningMessage(`Shape graph does not exist: ${graphUri}`);
}
}
const fileName = documentUri.path.split('/').pop() ?? documentUri.toString();
const statusBarMessage = vscode.window.setStatusBarMessage(`$(loading~spin) Running SHACL validation for: ${fileName}`);
const statusBarMessageStartTime = Date.now();
const minStatusBarMessageDurationMs = 300;
// Create read-only views over the store — no triple copying needed.
const shapesDataset = new StoreDatasetView(this._store, shapeGraphUris);
const dataDataset = new StoreDatasetView(this._store, context.graphs);
// Run SHACL validation
const validator = new Validator(shapesDataset, { factory: rdfFactory });
// Yield once so VS Code can paint the status bar spinner before validation starts.
await new Promise(resolve => setTimeout(resolve, 0));
try {
const report = await validator.validate({ dataset: dataDataset });
// Map results
const result: ShaclValidationResult = {
conforms: report.conforms,
reportDataset: report.dataset,
results: this._mapResults(report.results)
};
// Cache and publish diagnostics
this._lastResults.set(documentUri.toString(), result);
const quadContexts = context instanceof TurtleDocument ? context.getQuadContexts() : undefined;
this._publishDiagnostics(documentUri, context, result, quadContexts);
this._onDidValidate.fire(documentUri);
return result;
} catch (error) {
vscode.window.showErrorMessage(`SHACL validation failed: ${error}`);
return undefined;
} finally {
const elapsedMs = Date.now() - statusBarMessageStartTime;
if (elapsedMs < minStatusBarMessageDurationMs) {
await new Promise(resolve => setTimeout(resolve, minStatusBarMessageDurationMs - elapsedMs));
}
statusBarMessage.dispose();
}
}
/**
* Get the last validation result for a document.
*/
getLastResult(documentUri: vscode.Uri): ShaclValidationResult | undefined {
return this._lastResults.get(documentUri.toString());
}
/**
* Clear validation diagnostics for a document.
*/
clearDiagnostics(documentUri: vscode.Uri): void {
this._diagnosticCollection.delete(documentUri);
this._lastResults.delete(documentUri.toString());
this._onDidValidate.fire(documentUri);
}
/**
* Get the validation report as plain text.
*/
getReportAsText(documentUri: vscode.Uri): string | undefined {
const result = this._lastResults.get(documentUri.toString());
if (!result) {
return undefined;
}
const lines: string[] = [];
lines.push(`SHACL Validation Report`);
lines.push(`Conforms: ${result.conforms}`);
lines.push(`Results: ${result.results.length}`);
lines.push('');
for (const r of result.results) {
lines.push(` Focus Node: ${r.focusNode}`);
lines.push(` Severity: ${this._severityLabel(r.severity)}`);
if (r.path) {
lines.push(` Path: ${r.path}`);
}
for (const msg of r.messages) {
lines.push(` Message: ${msg}`);
}
if (r.value) {
lines.push(` Value: ${r.value}`);
}
lines.push(` Shape: ${r.sourceShape}`);
lines.push('');
}
return lines.join('\n');
}
/**
* Get the validation report as a Turtle string.
*/
async getReportAsTurtle(documentUri: vscode.Uri): Promise<string | undefined> {
const result = this._lastResults.get(documentUri.toString());
if (!result || !result.reportDataset) {
return undefined;
}
// Use the store's serialization capabilities to write the report dataset as Turtle.
const tempStore = new Store();
const tempGraphUri = 'urn:shacl:report';
for (const q of result.reportDataset) {
tempStore.add(rdfFactory.quad(q.subject, q.predicate, q.object, rdfFactory.namedNode(tempGraphUri)));
}
return tempStore.serializeGraph(tempGraphUri, 'text/turtle', undefined, {
'sh': _SH,
'xsd': _XSD,
'rdf': _RDF
});
}
private _mapResults(results: any[]): ShaclValidationResultEntry[] {
return results.map(r => ({
focusNode: r.focusNode?.term?.value ?? r.focusNode?.value ?? '',
severity: r.severity?.value ?? '',
constraintComponent: r.constraintComponent?.value ?? '',
messages: (r.message ?? []).map((m: any) => m.value ?? String(m)),
path: r.path?.[0]?.predicates?.[0]?.value,
value: r.value?.term?.value ?? r.value?.value,
sourceShape: r.shape?.ptr?.term?.value ?? ''
}));
}
private _publishDiagnostics(documentUri: vscode.Uri, context: IDocumentContext, result: ShaclValidationResult, quadContexts?: QuadContext[]): void {
const diagnostics = this._diagnosticsMapper.mapToDiagnostics(result, context, quadContexts);
this._diagnosticCollection.set(documentUri, diagnostics);
}
private _severityLabel(severity: string): string {
if (severity.endsWith('Violation')) return 'Violation';
if (severity.endsWith('Warning')) return 'Warning';
if (severity.endsWith('Info')) return 'Info';
return severity;
}
/**
* Migrates SHACL validation settings for renamed/moved files or folders.
*
* Keys in `mentor.shacl.validation` are workspace-relative `workspace:///...` URIs.
* Only renames for files whose old URI can be resolved to a workspace-relative URI
* are migrated — this prevents cross-workspace key contamination since the settings
* are stored globally and may contain keys from prior workspace sessions.
*/
async migrateShaclSettings(files: ReadonlyArray<{ oldUri: vscode.Uri; newUri: vscode.Uri }>): Promise<void> {
const renames: { oldKey: string; newKey: string }[] = [];
for (const { oldUri, newUri } of files) {
const oldWorkspaceUri = WorkspaceUri.toWorkspaceUri(oldUri);
if (!oldWorkspaceUri) {
// File is outside the current workspace root — skip to avoid
// accidentally migrating keys from a different workspace session.
continue;
}
const newWorkspaceUri = WorkspaceUri.toWorkspaceUri(newUri);
const oldKey = WorkspaceUri.toCanonicalString(oldWorkspaceUri);
const newKey = newWorkspaceUri
? WorkspaceUri.toCanonicalString(newWorkspaceUri)
: newUri.toString();
renames.push({ oldKey, newKey });
}
if (renames.length === 0) {
return;
}
const shacl = vscode.workspace.getConfiguration('mentor.shacl');
const current = shacl.get<ShaclValidationConfiguration>('validation', {});
const migrated = migrateShaclValidationConfig(current, renames);
await shacl.update('validation', migrated, vscode.ConfigurationTarget.Global);
}
dispose(): void {
this._diagnosticCollection.dispose();
for (const d of this._disposables) {
d.dispose();
}
}
}
|