Skip to content

Commit db47dbe

Browse files
committed
Add proper violation for sh:xone
1 parent 330a2d4 commit db47dbe

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/constraint/XoneConstraint.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.jena.rdf.model.RDFNode;
2020
import org.eclipse.esmf.aspectmodel.shacl.violation.EvaluationContext;
2121
import org.eclipse.esmf.aspectmodel.shacl.violation.Violation;
22+
import org.eclipse.esmf.aspectmodel.shacl.violation.XoneViolation;
2223

2324
/**
2425
* Implements <a href="https://www.w3.org/TR/shacl/#XoneConstraintComponent">sh:xone</a>
@@ -32,7 +33,7 @@ public List<Violation> apply( final RDFNode rdfNode, final EvaluationContext con
3233
if ( numberOfEmptyViolationLists == 1 ) {
3334
return List.of();
3435
}
35-
return violationsPerConstraint.stream().flatMap( Collection::stream ).toList();
36+
return List.of( new XoneViolation( context, violationsPerConstraint.stream().flatMap( Collection::stream ).toList() ) );
3637
}
3738

3839
@Override

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/violation/Violation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ default T visitNotViolation( final NotViolation violation ) {
148148
return visit( violation );
149149
}
150150

151+
default T visitXoneViolation( final XoneViolation violation ) {
152+
return visit( violation );
153+
}
154+
151155
default T visitJsViolation( final JsConstraintViolation violation ) {
152156
return visit( violation );
153157
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
3+
*
4+
* See the AUTHORS file(s) distributed with this work for additional
5+
* information regarding authorship.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
10+
*
11+
* SPDX-License-Identifier: MPL-2.0
12+
*/
13+
14+
package org.eclipse.esmf.aspectmodel.shacl.violation;
15+
16+
import java.util.List;
17+
import java.util.stream.Collectors;
18+
import java.util.stream.IntStream;
19+
20+
public record XoneViolation(EvaluationContext context, List<Violation> violations) implements Violation {
21+
public static final String ERROR_CODE = "ERR_XONE";
22+
23+
@Override
24+
public String errorCode() {
25+
return ERROR_CODE;
26+
}
27+
28+
@Override
29+
public String message() {
30+
return "One of the following violations" +
31+
(context.property().isPresent() ? " for " + propertyName() : "")
32+
+ " must be fixed: " +
33+
IntStream.range( 0, violations.size() )
34+
.mapToObj( i -> String.format( "(%d) %s", i + 1, violations().get( i ).message().replaceAll( "\\.$", "" ) ) )
35+
.collect( Collectors.joining( ", " ) ) + ".";
36+
}
37+
38+
@Override
39+
public <T> T accept( final Visitor<T> visitor ) {
40+
return visitor.visitXoneViolation( this );
41+
}
42+
}

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/validation/services/ViolationRustLikeFormatter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.eclipse.esmf.aspectmodel.shacl.violation.UniqueLanguageViolation;
4444
import org.eclipse.esmf.aspectmodel.shacl.violation.ValueFromListViolation;
4545
import org.eclipse.esmf.aspectmodel.shacl.violation.Violation;
46+
import org.eclipse.esmf.aspectmodel.shacl.violation.XoneViolation;
4647

4748
public class ViolationRustLikeFormatter extends ViolationFormatter {
4849

@@ -202,6 +203,13 @@ public String visitNotViolation( final NotViolation violation ) {
202203
violation.message(), rawModel );
203204
}
204205

206+
@Override
207+
public String visitXoneViolation( final XoneViolation violation ) {
208+
return formatter.constructDetailedMessage(
209+
violation.context().property().isPresent() ? violation.context().property().get() : violation.context().element(),
210+
violation.message(), rawModel );
211+
}
212+
205213
@Override
206214
public String visitJsViolation( final JsConstraintViolation violation ) {
207215
return formatter.constructDetailedMessage(

0 commit comments

Comments
 (0)