|
| 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; |
| 15 | + |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.AllowedLanguagesConstraint; |
| 19 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.AllowedValuesConstraint; |
| 20 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.AndConstraint; |
| 21 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.ClassConstraint; |
| 22 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.Constraint; |
| 23 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.DatatypeConstraint; |
| 24 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.DisjointConstraint; |
| 25 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.EqualsConstraint; |
| 26 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.HasValueConstraint; |
| 27 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.LessThanConstraint; |
| 28 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.LessThanOrEqualsConstraint; |
| 29 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.MaxCountConstraint; |
| 30 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.MaxExclusiveConstraint; |
| 31 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.MaxInclusiveConstraint; |
| 32 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.MaxLengthConstraint; |
| 33 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.MinCountConstraint; |
| 34 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.MinExclusiveConstraint; |
| 35 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.MinInclusiveConstraint; |
| 36 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.MinLengthConstraint; |
| 37 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.NodeConstraint; |
| 38 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.NodeKindConstraint; |
| 39 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.NotConstraint; |
| 40 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.OrConstraint; |
| 41 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.PatternConstraint; |
| 42 | +import org.eclipse.esmf.aspectmodel.shacl.constraint.XoneConstraint; |
| 43 | +import org.eclipse.esmf.aspectmodel.shacl.violation.EvaluationContext; |
| 44 | + |
| 45 | +import org.apache.jena.rdf.model.Property; |
| 46 | + |
| 47 | +/** |
| 48 | + * Creates a short textual summary of a {@link Shape} or a {@link Constraint}. |
| 49 | + */ |
| 50 | +public class ShapeSummarizer implements Shape.Visitor<String>, Constraint.Visitor<String> { |
| 51 | + private final EvaluationContext context; |
| 52 | + |
| 53 | + public ShapeSummarizer( final EvaluationContext context ) { |
| 54 | + this.context = context; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String visitNodeShape( final Shape.Node nodeShape ) { |
| 59 | + return "%s: %s".formatted( nodeShape.attributes().uri().map( context::shortUri ).orElse( "(anonymous node shape)" ), |
| 60 | + nodeShape.attributes().constraints().stream().map( constraint -> |
| 61 | + constraint.accept( this ) ).collect( Collectors.joining( ", " ) ) ); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String visitPropertyShape( final Shape.Property propertyShape ) { |
| 66 | + return "%s with path %s: %s".formatted( |
| 67 | + propertyShape.attributes().uri().map( context::shortUri ).orElse( "(anonymous property shape)" ), propertyShape.path(), |
| 68 | + propertyShape.attributes().constraints().stream().map( constraint -> |
| 69 | + constraint.accept( this ) ).collect( Collectors.joining( ", " ) ) ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Default implementation for those constraints that either have no args (e.g. sh:closed) or for which the args should be |
| 74 | + * excluded since they could be too long (e.g. sh:SPARQLConstraint). |
| 75 | + * |
| 76 | + * @param constraint the constraint |
| 77 | + * @return a short string representation of the constraint |
| 78 | + */ |
| 79 | + @Override |
| 80 | + public String visit( final Constraint constraint ) { |
| 81 | + return constraint.name(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String visitAllowedLanguagesConstraint( final AllowedLanguagesConstraint constraint ) { |
| 86 | + return "%s (allowed languages: %s)".formatted( constraint.name(), constraint.allowedLanguages() ); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String visitAllowedValuesConstraint( final AllowedValuesConstraint constraint ) { |
| 91 | + return "%s (allowed values: %s)".formatted( constraint.name(), constraint.allowedValues().stream().map( context::value ).toList() ); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String visitAndConstraint( final AndConstraint constraint ) { |
| 96 | + return "%s (with referenced shapes: %s)".formatted( constraint.name(), |
| 97 | + constraint.shapes().stream().map( shape -> shape.accept( this ) ) ); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String visitClassConstraint( final ClassConstraint constraint ) { |
| 102 | + return "%s (allowed class: %s)".formatted( constraint.name(), context.shortUri( constraint.allowedClass().getURI() ) ); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String visitDatatypeConstraint( final DatatypeConstraint constraint ) { |
| 107 | + return "%s (allowed type: %s)".formatted( constraint.name(), context.shortUri( constraint.allowedTypeUri() ) ); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String visitDisjointConstraint( final DisjointConstraint constraint ) { |
| 112 | + return "%s (disjoint with: %s which has value %s)".formatted( constraint.name(), |
| 113 | + context.shortUri( constraint.otherProperty().getURI() ), otherPropertyValue( constraint.otherProperty() ) ); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String visitEqualsConstraint( final EqualsConstraint constraint ) { |
| 118 | + return "%s (equal to: %s which has value %s)".formatted( constraint.name(), |
| 119 | + context.shortUri( constraint.otherProperty().getURI() ), otherPropertyValue( constraint.otherProperty() ) ); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String visitHasValueConstraint( final HasValueConstraint constraint ) { |
| 124 | + return "%s (allowed value: %s)".formatted( constraint.name(), context.value( constraint.allowedValue() ) ); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public String visitLessThanConstraint( final LessThanConstraint constraint ) { |
| 129 | + return "%s (less than to: %s which has value %s)".formatted( constraint.name(), |
| 130 | + context.shortUri( constraint.otherProperty().getURI() ), otherPropertyValue( constraint.otherProperty() ) ); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public String visitLessThanOrEqualsConstraint( final LessThanOrEqualsConstraint constraint ) { |
| 135 | + return "%s (less than or equal to: %s which has value %s)".formatted( constraint.name(), |
| 136 | + context.shortUri( constraint.otherProperty().getURI() ), otherPropertyValue( constraint.otherProperty() ) ); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public String visitMaxCountConstraint( final MaxCountConstraint constraint ) { |
| 141 | + return "%s (max occurrences: %d)".formatted( constraint.name(), constraint.maxCount() ); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public String visitMaxExclusiveConstraint( final MaxExclusiveConstraint constraint ) { |
| 146 | + return "%s (max value: %s)".formatted( constraint.name(), context.value( constraint.maxValue() ) ); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public String visitMaxInclusiveConstraint( final MaxInclusiveConstraint constraint ) { |
| 151 | + return "%s (max value: %s)".formatted( constraint.name(), context.value( constraint.maxValue() ) ); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public String visitMaxLengthConstraint( final MaxLengthConstraint constraint ) { |
| 156 | + return "%s (max length: %d)".formatted( constraint.name(), constraint.maxLength() ); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public String visitMinCountConstraint( final MinCountConstraint constraint ) { |
| 161 | + return "%s (min occurrences: %d)".formatted( constraint.name(), constraint.minCount() ); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public String visitMinExclusiveConstraint( final MinExclusiveConstraint constraint ) { |
| 166 | + return "%s (min value: %s)".formatted( constraint.name(), context.value( constraint.minValue() ) ); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public String visitMinInclusiveConstraint( final MinInclusiveConstraint constraint ) { |
| 171 | + return "%s (min value: %s)".formatted( constraint.name(), context.value( constraint.minValue() ) ); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public String visitMinLengthConstraint( final MinLengthConstraint constraint ) { |
| 176 | + return "%s (min length: %d)".formatted( constraint.name(), constraint.minLength() ); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public String visitNodeConstraint( final NodeConstraint constraint ) { |
| 181 | + return "%s (referenced shape: %s)".formatted( constraint.name(), constraint.targetShape().get().accept( this ) ); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public String visitNodeKindConstraint( final NodeKindConstraint constraint ) { |
| 186 | + return "%s (allowed node kind: %s)".formatted( constraint.name(), constraint.allowedNodeKind().humanReadable() ); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public String visitNotConstraint( final NotConstraint constraint ) { |
| 191 | + return "%s (negated: %s)".formatted( constraint.name(), constraint.constraint().accept( this ) ); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public String visitOrConstraint( final OrConstraint constraint ) { |
| 196 | + return "%s (with referenced shapes: %s)".formatted( constraint.name(), |
| 197 | + constraint.shapes().stream().map( shape -> shape.accept( this ) ) ); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public String visitPatternConstraint( final PatternConstraint constraint ) { |
| 202 | + return "%s (pattern: %s)".formatted( constraint.name(), constraint.pattern() ); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public String visitXoneConstraint( final XoneConstraint constraint ) { |
| 207 | + return "%s (with referenced shapes: %s)".formatted( constraint.name(), |
| 208 | + constraint.shapes().stream().map( shape -> shape.accept( this ) ) ); |
| 209 | + } |
| 210 | + |
| 211 | + protected String otherPropertyValue( final Property property ) { |
| 212 | + return context.value( context.element().getProperty( property ).getObject() ); |
| 213 | + } |
| 214 | +} |
0 commit comments