-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
215 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Jose Emilio Labra Gayo | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,7 @@ | ||
# ShExS Jena | ||
|
||
This module that wraps the Scala version of ShEx to be used with Jena Models | ||
|
||
Although the Apache Jena already implements a built-in ShEx validator, | ||
this is wrapper around the Scala implementation which may be useful to compare results from different implementations. | ||
|
79 changes: 79 additions & 0 deletions
79
modules/shexsrdf4j/src/main/scala/es/weso/shexsrdf4j/ShExsRDF4jValidator.pending
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,79 @@ | ||
package es.weso.shexsrdf4j | ||
|
||
import es.weso.shex.Schema | ||
import es.weso.rdf.nodes.RDFNode | ||
import es.weso.shex.ShapeLabel | ||
import es.weso.shapemaps.ResultShapeMap | ||
import es.weso.shex.ResolvedSchema | ||
import es.weso.shex.validator._ | ||
import cats.effect.IO | ||
import es.weso.shex.validator.ExternalResolver._ | ||
import es.weso.rdf.nodes.IRI | ||
import cats._ | ||
import cats.data._ | ||
import cats.implicits._ | ||
import cats.implicits._ | ||
import cats.effect.unsafe.implicits.global | ||
import es.weso.utils.IOUtils._ | ||
import es.weso.shapemaps._ | ||
import cats.data.NonEmptyList | ||
import es.weso.utils.VerboseLevel | ||
import java.io.InputStream | ||
import org.eclipse.rdf4j.model.Model | ||
import es.weso.rdf.rdf4j.RDFAsRDF4jModel | ||
|
||
|
||
case class ShExsRDF4jValidator(schema: Schema) { | ||
|
||
/** Validates a node with a shape label in the schema | ||
*/ | ||
def validateNodeShapeSync( | ||
model: Model, | ||
node: String, | ||
shape: String | ||
): ResultShapeMap = { | ||
val verbose = VerboseLevel.Nothing | ||
val cmp: IO[ResultShapeMap] = RDFAsRDF4jModel.empty.flatMap( | ||
_.use(builder => | ||
for { | ||
nodeIri <- fromES(IRI.fromString(node)) | ||
rdf <- RDFAsRDF4jModel.fromModel(model, None, None, Map()) | ||
resolvedSchema <- ResolvedSchema.resolve(schema, None, verbose) | ||
validator = Validator(resolvedSchema, NoAction, builder) | ||
result <- validator.validateNodeShape(rdf, nodeIri, shape, verbose) | ||
resultShapeMap <- result.toResultShapeMap | ||
} yield resultShapeMap | ||
) | ||
) | ||
cmp.unsafeRunSync() | ||
} | ||
|
||
def validateShapeMapSync( | ||
model: Model, | ||
shapeMap: String | ||
): ResultShapeMap = { | ||
val verbose = VerboseLevel.Nothing | ||
val cmp: IO[ResultShapeMap] = RDFAsRDF4jModel.empty.flatMap( | ||
_.use(builder => | ||
for { | ||
rdf <- RDFAsRDF4jModel.fromModel(model, None, None, Map()) | ||
resolvedSchema <- ResolvedSchema.resolve(schema, None, verbose) | ||
validator = Validator(resolvedSchema, NoAction, builder) | ||
rdfPrefixMap <- rdf.getPrefixMap | ||
shapeMap <- fromESNel( | ||
ShapeMap.fromString(shapeMap, "COMPACT", None, rdfPrefixMap, schema.prefixMap) | ||
) | ||
fixedShapeMap <- ShapeMap.fixShapeMap(shapeMap, rdf, rdfPrefixMap, schema.prefixMap) | ||
result <- validator.validateShapeMap(rdf, fixedShapeMap, verbose) | ||
resultShapeMap <- result.toResultShapeMap | ||
} yield resultShapeMap | ||
) | ||
) | ||
cmp.unsafeRunSync() | ||
} | ||
|
||
def fromESNel[A](e: Either[NonEmptyList[String], A]): IO[A] = | ||
fromES(e.leftMap(es => es.toList.mkString(","))) | ||
} | ||
|
||
|
24 changes: 24 additions & 0 deletions
24
modules/shexsrdf4j/src/main/scala/es/weso/shexsrdf4j/ShExsRDF4jValidatorBuilder.pending
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,24 @@ | ||
package es.weso.shexsrdf4j | ||
|
||
import es.weso.shex.Schema | ||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
import java.io.InputStream | ||
|
||
object ShExsRDF4jValidatorBuilder { | ||
|
||
def fromStringSync(schemaStr: String, format: String = "SHEXC"): ShExsRDF4jValidator = { | ||
val cmp: IO[ShExsRDF4jValidator] = for { | ||
schema <- Schema.fromString(schemaStr, format, None, None) | ||
} yield ShExsRDF4jValidator(schema) | ||
cmp.unsafeRunSync() | ||
} | ||
|
||
def fromInputStreamSync(is: InputStream, format: String = "SHEXC"): ShExsRDf4jValidator = { | ||
val cmp: IO[ShExsRDF4jValidator] = for { | ||
schema <- Schema.fromInputStream(is, format, None, None) | ||
} yield ShExsRDF4jValidator(schema) | ||
cmp.unsafeRunSync() | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
modules/shexsrdf4j/src/test/java/es/weso/shexsrdf4j/ShExsValidatorTest.pending
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,84 @@ | ||
package es.weso.shexsrdf4j; | ||
import static org.junit.Assert.assertEquals; | ||
import org.junit.Test; | ||
import es.weso.shapemaps.*; | ||
import org.apache.jena.rdf.model.Model; | ||
import org.apache.jena.rdf.model.ModelFactory; | ||
import org.apache.commons.io.IOUtils; | ||
import es.weso.utils.VerboseLevel; | ||
import es.weso.rdf.nodes.IRI; | ||
import es.weso.rdf.nodes.RDFNode; | ||
import java.net.*; | ||
import es.weso.shapemaps.IRILabel; | ||
|
||
public class ShExsValidatorTest { | ||
|
||
@Test | ||
public void exampleAddition() { | ||
assertEquals(1 + 1, 2); | ||
} | ||
|
||
@Test | ||
public void exampleValidatorPass() throws URISyntaxException { | ||
String schemaStr = | ||
"prefix : <http://example.org/>\n" + | ||
":S {\n" + | ||
" :p .\n" + | ||
"}"; | ||
|
||
String rdfStr = | ||
"prefix : <http://example.org/>\n" + | ||
":x :p 1 ." ; | ||
|
||
ShExsJenaValidator validator = | ||
ShExsJenaValidatorBuilder.fromStringSync(schemaStr,"ShExC"); | ||
|
||
// Create Jena Model from RDF String | ||
Model model = ModelFactory.createDefaultModel() | ||
.read(IOUtils.toInputStream(rdfStr, "UTF-8"), null, "TURTLE"); | ||
|
||
ResultShapeMap result = | ||
validator.validateNodeShapeSync(model, | ||
"http://example.org/x", | ||
"http://example.org/S"); | ||
|
||
RDFNode x = new IRI(new URI("http://example.org/x")); | ||
IRILabel s = new IRILabel(new IRI(new URI("http://example.org/S"))); | ||
|
||
// System.out.println("ResultShapeMap = " + result); | ||
// System.out.println("ConformantShapes(x) = " + result.getConformantShapes(x)); | ||
// System.out.println("ConformantShapes(x).contains(s) = " + result.getConformantShapes(x).contains(s)); | ||
assertEquals(true, result.getConformantShapes(x).contains(s)); | ||
} | ||
|
||
@Test | ||
public void exampleValidatorFails() throws URISyntaxException { | ||
String schemaStr = | ||
"prefix : <http://example.org/>\n" + | ||
":S {\n" + | ||
" :p .\n" + | ||
"}"; | ||
|
||
String rdfStr = | ||
"prefix : <http://example.org/>\n" + | ||
":x :other 1 ." ; | ||
|
||
ShExsJenaValidator validator = | ||
ShExsJenaValidatorBuilder.fromStringSync(schemaStr,"ShExC"); | ||
|
||
// Create Jena Model from RDF String | ||
Model model = ModelFactory.createDefaultModel() | ||
.read(IOUtils.toInputStream(rdfStr, "UTF-8"), null, "TURTLE"); | ||
|
||
ResultShapeMap result = | ||
validator.validateNodeShapeSync(model, | ||
"http://example.org/x", | ||
"http://example.org/S"); | ||
|
||
RDFNode x = new IRI(new URI("http://example.org/x")); | ||
IRILabel s = new IRILabel(new IRI(new URI("http://example.org/S"))); | ||
|
||
assertEquals(false, result.getConformantShapes(x).contains(s)); | ||
} | ||
|
||
} |