New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(rdf-api): Add a general-purpose SHACL validation utility (DSP-930) #1762
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@prefix ex: <http://example.com/ns#> . | ||
@prefix sh: <http://www.w3.org/ns/shacl#> . | ||
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | ||
@prefix foaf: <http://xmlns.com/foaf/0.1/> . | ||
|
||
ex:PersonShape a sh:NodeShape ; | ||
sh:targetClass foaf:Person ; | ||
sh:property ex:PersonShapeProperty . | ||
|
||
ex:PersonShapeProperty sh:path foaf:age ; | ||
sh:datatype xsd:int ; | ||
sh:maxCount 1 ; | ||
sh:minCount 1 . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
webapi/src/main/scala/org/knora/webapi/messages/util/rdf/AbstractShaclValidator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright © 2015-2019 the contributors (see Contributors.md). | ||
* | ||
* This file is part of Knora. | ||
* | ||
* Knora is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Knora is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public | ||
* License along with Knora. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package org.knora.webapi.messages.util.rdf | ||
|
||
import java.io.File | ||
import java.nio.file.Path | ||
|
||
import org.knora.webapi.exceptions.AssertionException | ||
|
||
/** | ||
* An abstract base class for classes that validate RDF models using SHACL shapes. | ||
* | ||
* @param baseDir the base directory that SHACL graphs are loaded from. | ||
* @param rdfFormatUtil an [[RdfFormatUtil]]. | ||
* @tparam ShaclGraphT an implementation-specific representation of a graph of SHACL shapes. | ||
*/ | ||
abstract class AbstractShaclValidator[ShaclGraphT](baseDir: File, private val rdfFormatUtil: RdfFormatUtil) extends ShaclValidator { | ||
|
||
/** | ||
* A map of relative paths to objects representing graphs of SHACL shapes. | ||
*/ | ||
private val shaclGraphs: Map[Path, ShaclGraphT] = if (baseDir.exists) { | ||
loadShaclGraphs(baseDir, baseDir) | ||
} else { | ||
Map.empty | ||
} | ||
|
||
def validate(rdfModel: RdfModel, shaclPath: Path): ShaclValidationResult = { | ||
validateWithShaclGraph( | ||
rdfModel = rdfModel, | ||
shaclGraph = shaclGraphs.getOrElse(shaclPath, throw AssertionException(s"SHACL graph $shaclPath not found")) | ||
) | ||
} | ||
|
||
/** | ||
* Recursively loads graphs of SHACL shapes from a directory and its subdirectories. | ||
* | ||
* @param baseDir the base directory that SHACL graphs are loaded from. | ||
* @param dir the base directory or a subdirectory. | ||
* @return a map of file paths (relative to the base directory) to graphs of SHACL shapes. | ||
*/ | ||
private def loadShaclGraphs(baseDir: File, dir: File): Map[Path, ShaclGraphT] = { | ||
// Parse the files representing SHACL graphs in this directory. | ||
val modelsInDir: Map[Path, ShaclGraphT] = dir.listFiles.collect { | ||
case file: File if file.isFile && file.getName.endsWith(".ttl") => | ||
// Map each SHACL file's relative path to a ShapesT containing the SHACL graph. | ||
val relativePath: Path = baseDir.toPath.relativize(file.toPath) | ||
val shaclModel: RdfModel = rdfFormatUtil.fileToRdfModel(file = file, rdfFormat = Turtle) | ||
relativePath -> rdfModelToShaclGraph(shaclModel) | ||
}.toMap | ||
|
||
// Recurse in subdirectories. | ||
modelsInDir ++ dir.listFiles.filter(_.isDirectory).flatMap { | ||
subDir => loadShaclGraphs(baseDir = baseDir, dir = subDir) | ||
} | ||
} | ||
|
||
/** | ||
* Validates the default graph of an [[RdfModel]] using a graph of SHACL shapes. | ||
* | ||
* @param rdfModel the [[RdfModel]] to be validated. | ||
* @param shaclGraph a graph of SHACL shapes. | ||
* @return the validation result. | ||
*/ | ||
protected def validateWithShaclGraph(rdfModel: RdfModel, shaclGraph: ShaclGraphT): ShaclValidationResult | ||
|
||
/** | ||
* Converts the default graph of an [[RdfModel]] to a [[ShaclGraphT]]. | ||
* | ||
* @param rdfModel an [[RdfModel]] whose default graph contains SHACL shapes. | ||
* @return a [[ShaclGraphT]] representing the SHACL shapes. | ||
*/ | ||
protected def rdfModelToShaclGraph(rdfModel: RdfModel): ShaclGraphT | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like the Shacl shapes are actually loaded from
dir
directory and its subdirectories, andbaseDir
is only used to get its path to make therelativePath
. Can you please change the comments forbaseDir
anddir
in scaladocs?I also wonder, if only the path of the
baseDir
is needed here, why not just using its path as the parameter of this function, I meantwo directory parameters are a bit confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the SHACL shapes are loaded from
baseDir
and its subdirectories.File
andPath
are basically the same thing, butFile
is the oldjava.io
API which I'm more used to, andPath
is the newerjava.nio
API. The consensus in the Java community seems to be that seems thatPath
should be used instead ofFile
for everything nowadays (https://stackoverflow.com/a/26658436). I guess we should do a PR sometime to usejava.nio
instead ofjava.io
everywhere. For now, I've refactoredAbstractShaclValidator
to usejava.nio
to walk the directory tree, which seems clearer.