Skip to content

Commit 19d4920

Browse files
committed
Fix code smells and typos
1 parent 7c0b69a commit 19d4920

26 files changed

+123
-157
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,16 @@ private boolean isList( final Model model, final RDFNode node ) {
260260
return node.equals( RDF.nil ) || (node.isResource() && model.contains( node.asResource(), RDF.rest, (RDFNode) null ));
261261
}
262262

263-
private Statement findListHead( Statement listElement ) {
263+
private Statement findListHead( final Statement listElement ) {
264+
Statement listElementStatement = listElement;
264265
while ( true ) {
265-
final StmtIterator iter = listElement.getModel().listStatements( null, RDF.rest, listElement.getSubject() );
266+
final StmtIterator iter = listElementStatement.getModel().listStatements( null, RDF.rest, listElementStatement.getSubject() );
266267
if ( !iter.hasNext() ) {
267268
break;
268269
}
269-
listElement = iter.nextStatement();
270+
listElementStatement = iter.nextStatement();
270271
}
271-
return listElement;
272+
return listElementStatement;
272273
}
273274

274275
private boolean formatList( final RDFNode listNode ) {
@@ -347,17 +348,17 @@ private void formatText( final String reconstructedText ) {
347348
currentColumn += reconstructedText.length();
348349
}
349350

350-
private static SmartToken extractToken( final RDFNode node ) {
351-
final Node n = node.asNode();
352-
if ( n instanceof AnyNode an ) {
351+
private static SmartToken extractToken( final RDFNode rdfNode ) {
352+
final Node node = rdfNode.asNode();
353+
if ( node instanceof final AnyNode an ) {
353354
return an.getToken();
354-
} else if ( n instanceof BlankNode bn ) {
355+
} else if ( node instanceof final BlankNode bn ) {
355356
return bn.getToken();
356-
} else if ( n instanceof LiteralNode ln ) {
357+
} else if ( node instanceof final LiteralNode ln ) {
357358
return ln.getToken();
358-
} else if ( n instanceof UriNode un ) {
359+
} else if ( node instanceof final UriNode un ) {
359360
return un.getToken();
360-
} else if ( n instanceof VariableNode vn ) {
361+
} else if ( node instanceof final VariableNode vn ) {
361362
return vn.getToken();
362363
} else {
363364
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* Vocabulary for the Shapes Constraint Language (SHACL)
2323
*/
2424
public class SHACL implements Namespace {
25-
public final String NS = "http://www.w3.org/ns/shacl#";
25+
public static final String NS = "http://www.w3.org/ns/shacl#";
2626

2727
@Override
2828
public String getUri() {

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
import org.apache.jena.rdf.model.Statement;
3535
import org.apache.jena.vocabulary.RDF;
3636
import org.eclipse.esmf.aspectmodel.resolver.services.VersionedModel;
37-
import org.slf4j.Logger;
38-
import org.slf4j.LoggerFactory;
39-
4037
import org.eclipse.esmf.aspectmodel.shacl.constraint.Constraint;
4138
import org.eclipse.esmf.aspectmodel.shacl.constraint.MinCountConstraint;
4239
import org.eclipse.esmf.aspectmodel.shacl.constraint.SparqlConstraint;
@@ -52,7 +49,6 @@
5249
* results only for this specific resource.
5350
*/
5451
public class ShaclValidator {
55-
private static final Logger LOG = LoggerFactory.getLogger( ShaclValidator.class );
5652
private final List<Shape.Node> shapes;
5753
private final Map<Resource, List<Shape.Node>> shapesWithClassTargets;
5854
private final Model shapesModel;
@@ -78,7 +74,7 @@ public ShaclValidator( final Model shapesModel ) {
7874

7975
/**
8076
* Validates a model element using the SHACL shapes the validator was initialized with.
81-
* If you have more than one element to validate, prefer the method {@link this.validateElements( final List<Resource> )} to calling this method in a loop
77+
* If you have more than one element to validate, prefer the method {@link #validateElements(List)} to calling this method in a loop
8278
* for better performance.
8379
* {@link Resource#getModel()} on the element must not return null, i.e., the resource may not be created using
8480
* {@link org.apache.jena.rdf.model.ResourceFactory#createProperty(String)}, but instead must be created via {@link Model#createResource(String)}.
@@ -129,7 +125,8 @@ public List<Violation> validateModel( final VersionedModel model ) {
129125
private Map<Resource, List<Shape.Node>> findSparqlTargets( final Model model ) {
130126
final Map<Resource, List<Shape.Node>> resourceShapes = new HashMap<>();
131127
for ( final Shape.Node shape : targetSparqlShapes() ) {
132-
final List<Resource> shapeTargets = querySparqlTargets( model, shape.attributes().targetSparql().get() );
128+
final List<Resource> shapeTargets = querySparqlTargets( model, shape.attributes().targetSparql().orElseThrow( () ->
129+
new RuntimeException( "SPARQL node shape is missing a target SPARQL expression" ) ) );
133130
for ( final Resource node : shapeTargets ) {
134131
addResourceShape( resourceShapes, node, shape );
135132
}
@@ -161,7 +158,7 @@ private List<Resource> querySparqlTargets( final Model model, final Query query
161158
}
162159

163160
public List<Violation> validateElements( final List<Resource> elements ) {
164-
final Map<Resource, List<Shape.Node>> sparqlTargets = elements.size() > 0 ? findSparqlTargets( elements.get( 0 ).getModel() ) : Map.of();
161+
final Map<Resource, List<Shape.Node>> sparqlTargets = !elements.isEmpty() ? findSparqlTargets( elements.get( 0 ).getModel() ) : Map.of();
165162
return elements.stream().flatMap( element -> validateElement( element, sparqlTargets, element.getModel() ).stream() ).toList();
166163
}
167164

@@ -186,7 +183,7 @@ public List<Violation> validateShapeForElement( final Resource element, final Sh
186183

187184
// MinCount needs to be handled separately: If the property is not used at all on the target node, but a MinCount constraints >= 1
188185
// exists, a violation must be emitted even though no value for the property exists
189-
if ( reachableNodes.isEmpty() && constraint instanceof MinCountConstraint && property.path() instanceof PredicatePath predicatePath ) {
186+
if ( reachableNodes.isEmpty() && constraint instanceof MinCountConstraint && property.path() instanceof final PredicatePath predicatePath ) {
190187
final Property rdfProperty = resolvedModel.createProperty( predicatePath.predicate().getURI() );
191188
final EvaluationContext context = new EvaluationContext( element, shape, Optional.of( rdfProperty ), List.of(), this, resolvedModel );
192189
violations.addAll( constraint.apply( null, context ) );

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ record Node(
7979
}
8080

8181
/**
82-
* Implements <code>sh:property</code>
82+
* Implements {@code sh:property}
8383
*/
8484
record Property(
8585
Attributes attributes,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
public sealed interface Target permits Target.Class, Target.Node, Target.ObjectsOf, Target.Sparql, Target.SubjectsOf {
2323
/**
24-
* Implements <code>sh:targetClass</code>. This means that this shape applies to elements of the given class.
24+
* Implements {@code sh:targetClass}. This means that this shape applies to elements of the given class.
2525
* @param class_ the referred class
2626
*/
2727
record Class(Resource class_) implements Target {
@@ -34,14 +34,14 @@ record ObjectsOf(org.apache.jena.rdf.model.Property property) implements Target
3434
}
3535

3636
/**
37-
* Implements <code>sh:targetSubjectsOf</code>. This means that the shape applies to all elements that use a certain property.
37+
* Implements {@code sh:targetSubjectsOf}. This means that the shape applies to all elements that use a certain property.
3838
* @param property the given property
3939
*/
4040
record SubjectsOf(org.apache.jena.rdf.model.Property property) implements Target {
4141
}
4242

4343
/**
44-
* Implements <code>sh:target [ a sh:SPARQLTarget ... ]</code>. This means that the shape applies to all elements returned
44+
* Implements {@code sh:target [ a sh:SPARQLTarget ... ]}. This means that the shape applies to all elements returned
4545
* by a given SPARQL query
4646
* @param query the given SPARQL query
4747
*/

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ public List<Violation> apply( final RDFNode rdfNode, final EvaluationContext con
4444
}
4545

4646
final Resource resource = rdfNode.asResource();
47-
final PathNodeRetriever retriever = new PathNodeRetriever();
48-
// All statements that lead to (i.e. have as their object) nodes that are reachable via the paths of the property shapes
49-
final List<Statement> allowedNodes = shapeNode.properties().stream()
50-
.flatMap( property -> property.path().accept( resource, retriever ).stream() )
51-
.toList();
52-
5347
final FirstEffectiveProperty allowedFirstProperties = new FirstEffectiveProperty();
5448
final Set<Property> allowedProperties = shapeNode.properties().stream()
5549
.flatMap( property -> property.path().accept( resource, allowedFirstProperties ).stream() ).collect( Collectors.toSet() );

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public boolean canBeUsedOnNodeShapes() {
3636

3737
@Override
3838
public List<Violation> apply( final RDFNode rdfNode, final EvaluationContext context ) {
39+
//noinspection DuplicatedCode
3940
final List<Violation> nodeKindViolations = new NodeKindConstraint( Shape.NodeKind.Literal ).apply( rdfNode, context );
4041
if ( !nodeKindViolations.isEmpty() ) {
4142
return nodeKindViolations;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public boolean canBeUsedOnNodeShapes() {
3737

3838
@Override
3939
public List<Violation> apply( final RDFNode rdfNode, final EvaluationContext context ) {
40+
//noinspection DuplicatedCode
4041
final List<Violation> nodeKindViolations = new NodeKindConstraint( Shape.NodeKind.Literal ).apply( rdfNode, context );
4142
if ( !nodeKindViolations.isEmpty() ) {
4243
return nodeKindViolations;

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,14 @@
3030
public record NodeConstraint(Optional<Path> path, Shape.Node targetShape) implements Constraint {
3131
@Override
3232
public List<Violation> apply( final RDFNode rdfNode, final EvaluationContext context ) {
33-
// An empty path means that the node constraint is used inside a node shape, i.e., we just apply the target shape to the context element
34-
if ( path.isEmpty() ) {
35-
return context.validator().validateShapeForElement( context.element(), targetShape, context.resolvedModel() );
36-
}
37-
3833
// Having a path means that the node constraint is used inside a property shape, i.e., it applies to the element the
3934
// shape's path points to
40-
return path.get().accept( context.element(), context.validator().getPathNodeRetriever() ).stream()
35+
return path.map( value -> value.accept( context.element(), context.validator().getPathNodeRetriever() ).stream()
4136
.filter( statement -> statement.getObject().isResource() )
4237
.map( Statement::getResource )
4338
.flatMap( element -> context.validator().validateShapeForElement( element, targetShape, context.resolvedModel() ).stream() )
44-
.toList();
39+
// An empty path means that the node constraint is used inside a node shape, i.e., we just apply the target shape to the context element
40+
.toList() ).orElseGet( () -> context.validator().validateShapeForElement( context.element(), targetShape, context.resolvedModel() ) );
4541
}
4642

4743
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public <T> T accept( final Visitor<T> visitor ) {
7474
}
7575

7676
/**
77-
* Perform proper query substitutions; unfortunately the substutions done by {@see org.apache.jena.query.ParameterizedSparqlString} are not always correct.
77+
* Perform proper query substitutions; unfortunately the substitutions done by {@see org.apache.jena.query.ParameterizedSparqlString} are not always correct.
7878
* @param query the query
7979
* @param substitutions the map of substitutions to perform
8080
* @return the updated query

0 commit comments

Comments
 (0)