Skip to content

Commit 3462f7c

Browse files
committed
Skeleton started
1 parent 8a5edec commit 3462f7c

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed

modules/shexsrdf4j/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Jose Emilio Labra Gayo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

modules/shexsrdf4j/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ShExS Jena
2+
3+
This module that wraps the Scala version of ShEx to be used with Jena Models
4+
5+
Although the Apache Jena already implements a built-in ShEx validator,
6+
this is wrapper around the Scala implementation which may be useful to compare results from different implementations.
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package es.weso.shexsrdf4j
2+
3+
import es.weso.shex.Schema
4+
import es.weso.rdf.nodes.RDFNode
5+
import es.weso.shex.ShapeLabel
6+
import es.weso.shapemaps.ResultShapeMap
7+
import es.weso.shex.ResolvedSchema
8+
import es.weso.shex.validator._
9+
import cats.effect.IO
10+
import es.weso.shex.validator.ExternalResolver._
11+
import es.weso.rdf.nodes.IRI
12+
import cats._
13+
import cats.data._
14+
import cats.implicits._
15+
import cats.implicits._
16+
import cats.effect.unsafe.implicits.global
17+
import es.weso.utils.IOUtils._
18+
import es.weso.shapemaps._
19+
import cats.data.NonEmptyList
20+
import es.weso.utils.VerboseLevel
21+
import java.io.InputStream
22+
import org.eclipse.rdf4j.model.Model
23+
import es.weso.rdf.rdf4j.RDFAsRDF4jModel
24+
25+
26+
case class ShExsRDF4jValidator(schema: Schema) {
27+
28+
/** Validates a node with a shape label in the schema
29+
*/
30+
def validateNodeShapeSync(
31+
model: Model,
32+
node: String,
33+
shape: String
34+
): ResultShapeMap = {
35+
val verbose = VerboseLevel.Nothing
36+
val cmp: IO[ResultShapeMap] = RDFAsRDF4jModel.empty.flatMap(
37+
_.use(builder =>
38+
for {
39+
nodeIri <- fromES(IRI.fromString(node))
40+
rdf <- RDFAsRDF4jModel.fromModel(model, None, None, Map())
41+
resolvedSchema <- ResolvedSchema.resolve(schema, None, verbose)
42+
validator = Validator(resolvedSchema, NoAction, builder)
43+
result <- validator.validateNodeShape(rdf, nodeIri, shape, verbose)
44+
resultShapeMap <- result.toResultShapeMap
45+
} yield resultShapeMap
46+
)
47+
)
48+
cmp.unsafeRunSync()
49+
}
50+
51+
def validateShapeMapSync(
52+
model: Model,
53+
shapeMap: String
54+
): ResultShapeMap = {
55+
val verbose = VerboseLevel.Nothing
56+
val cmp: IO[ResultShapeMap] = RDFAsRDF4jModel.empty.flatMap(
57+
_.use(builder =>
58+
for {
59+
rdf <- RDFAsRDF4jModel.fromModel(model, None, None, Map())
60+
resolvedSchema <- ResolvedSchema.resolve(schema, None, verbose)
61+
validator = Validator(resolvedSchema, NoAction, builder)
62+
rdfPrefixMap <- rdf.getPrefixMap
63+
shapeMap <- fromESNel(
64+
ShapeMap.fromString(shapeMap, "COMPACT", None, rdfPrefixMap, schema.prefixMap)
65+
)
66+
fixedShapeMap <- ShapeMap.fixShapeMap(shapeMap, rdf, rdfPrefixMap, schema.prefixMap)
67+
result <- validator.validateShapeMap(rdf, fixedShapeMap, verbose)
68+
resultShapeMap <- result.toResultShapeMap
69+
} yield resultShapeMap
70+
)
71+
)
72+
cmp.unsafeRunSync()
73+
}
74+
75+
def fromESNel[A](e: Either[NonEmptyList[String], A]): IO[A] =
76+
fromES(e.leftMap(es => es.toList.mkString(",")))
77+
}
78+
79+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package es.weso.shexsrdf4j
2+
3+
import es.weso.shex.Schema
4+
import cats.effect.IO
5+
import cats.effect.unsafe.implicits.global
6+
import java.io.InputStream
7+
8+
object ShExsRDF4jValidatorBuilder {
9+
10+
def fromStringSync(schemaStr: String, format: String = "SHEXC"): ShExsRDF4jValidator = {
11+
val cmp: IO[ShExsRDF4jValidator] = for {
12+
schema <- Schema.fromString(schemaStr, format, None, None)
13+
} yield ShExsRDF4jValidator(schema)
14+
cmp.unsafeRunSync()
15+
}
16+
17+
def fromInputStreamSync(is: InputStream, format: String = "SHEXC"): ShExsRDf4jValidator = {
18+
val cmp: IO[ShExsRDF4jValidator] = for {
19+
schema <- Schema.fromInputStream(is, format, None, None)
20+
} yield ShExsRDF4jValidator(schema)
21+
cmp.unsafeRunSync()
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package es.weso.shexsrdf4j;
2+
import static org.junit.Assert.assertEquals;
3+
import org.junit.Test;
4+
import es.weso.shapemaps.*;
5+
import org.apache.jena.rdf.model.Model;
6+
import org.apache.jena.rdf.model.ModelFactory;
7+
import org.apache.commons.io.IOUtils;
8+
import es.weso.utils.VerboseLevel;
9+
import es.weso.rdf.nodes.IRI;
10+
import es.weso.rdf.nodes.RDFNode;
11+
import java.net.*;
12+
import es.weso.shapemaps.IRILabel;
13+
14+
public class ShExsValidatorTest {
15+
16+
@Test
17+
public void exampleAddition() {
18+
assertEquals(1 + 1, 2);
19+
}
20+
21+
@Test
22+
public void exampleValidatorPass() throws URISyntaxException {
23+
String schemaStr =
24+
"prefix : <http://example.org/>\n" +
25+
":S {\n" +
26+
" :p .\n" +
27+
"}";
28+
29+
String rdfStr =
30+
"prefix : <http://example.org/>\n" +
31+
":x :p 1 ." ;
32+
33+
ShExsJenaValidator validator =
34+
ShExsJenaValidatorBuilder.fromStringSync(schemaStr,"ShExC");
35+
36+
// Create Jena Model from RDF String
37+
Model model = ModelFactory.createDefaultModel()
38+
.read(IOUtils.toInputStream(rdfStr, "UTF-8"), null, "TURTLE");
39+
40+
ResultShapeMap result =
41+
validator.validateNodeShapeSync(model,
42+
"http://example.org/x",
43+
"http://example.org/S");
44+
45+
RDFNode x = new IRI(new URI("http://example.org/x"));
46+
IRILabel s = new IRILabel(new IRI(new URI("http://example.org/S")));
47+
48+
// System.out.println("ResultShapeMap = " + result);
49+
// System.out.println("ConformantShapes(x) = " + result.getConformantShapes(x));
50+
// System.out.println("ConformantShapes(x).contains(s) = " + result.getConformantShapes(x).contains(s));
51+
assertEquals(true, result.getConformantShapes(x).contains(s));
52+
}
53+
54+
@Test
55+
public void exampleValidatorFails() throws URISyntaxException {
56+
String schemaStr =
57+
"prefix : <http://example.org/>\n" +
58+
":S {\n" +
59+
" :p .\n" +
60+
"}";
61+
62+
String rdfStr =
63+
"prefix : <http://example.org/>\n" +
64+
":x :other 1 ." ;
65+
66+
ShExsJenaValidator validator =
67+
ShExsJenaValidatorBuilder.fromStringSync(schemaStr,"ShExC");
68+
69+
// Create Jena Model from RDF String
70+
Model model = ModelFactory.createDefaultModel()
71+
.read(IOUtils.toInputStream(rdfStr, "UTF-8"), null, "TURTLE");
72+
73+
ResultShapeMap result =
74+
validator.validateNodeShapeSync(model,
75+
"http://example.org/x",
76+
"http://example.org/S");
77+
78+
RDFNode x = new IRI(new URI("http://example.org/x"));
79+
IRILabel s = new IRILabel(new IRI(new URI("http://example.org/S")));
80+
81+
assertEquals(false, result.getConformantShapes(x).contains(s));
82+
}
83+
84+
}

0 commit comments

Comments
 (0)