|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2023 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + ******************************************************************************/ |
| 11 | + |
| 12 | +package org.eclipse.rdf4j.sail.shacl; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertThrows; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.StringReader; |
| 18 | + |
| 19 | +import org.eclipse.rdf4j.query.Update; |
| 20 | +import org.eclipse.rdf4j.repository.RepositoryException; |
| 21 | +import org.eclipse.rdf4j.repository.sail.SailRepository; |
| 22 | +import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection; |
| 23 | +import org.eclipse.rdf4j.rio.RDFFormat; |
| 24 | +import org.eclipse.rdf4j.sail.memory.MemoryStore; |
| 25 | +import org.junit.jupiter.api.Assertions; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +public class SparqlConstraintTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testFailureBinding() throws IOException { |
| 32 | + |
| 33 | + SailRepository sailRepository = new SailRepository(new ShaclSail(new MemoryStore())); |
| 34 | + |
| 35 | + try (SailRepositoryConnection connection = sailRepository.getConnection()) { |
| 36 | + connection.add(new StringReader("" + |
| 37 | + "@prefix : <http://example.com/data/> .\n" + |
| 38 | + "@prefix ont: <http://example.com/ontology#> .\n" + |
| 39 | + "@prefix vocsh: <http://example.org/shape/> .\n" + |
| 40 | + "@prefix so: <http://www.ontotext.com/semantic-object/> .\n" + |
| 41 | + "@prefix affected: <http://www.ontotext.com/semantic-object/affected> .\n" + |
| 42 | + "@prefix res: <http://www.ontotext.com/semantic-object/result/> .\n" + |
| 43 | + "@prefix dct: <http://purl.org/dc/terms/> .\n" + |
| 44 | + "@prefix gn: <http://www.geonames.org/ontology#> .\n" + |
| 45 | + "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n" + |
| 46 | + "@prefix puml: <http://plantuml.com/ontology#> .\n" + |
| 47 | + "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n" + |
| 48 | + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n" + |
| 49 | + "@prefix skos: <http://www.w3.org/2004/02/skos/core#> .\n" + |
| 50 | + "@prefix void: <http://rdfs.org/ns/void#> .\n" + |
| 51 | + "@prefix wgs84: <http://www.w3.org/2003/01/geo/wgs84_pos#> .\n" + |
| 52 | + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n" + |
| 53 | + "@prefix sh: <http://www.w3.org/ns/shacl#> .\n" + |
| 54 | + "@prefix dash: <http://datashapes.org/dash#> .\n" + |
| 55 | + "@prefix rsx: <http://rdf4j.org/shacl-extensions#> .\n" + |
| 56 | + "@prefix ec: <http://www.ontotext.com/connectors/entity-change#> .\n" + |
| 57 | + "@prefix ecinst: <http://www.ontotext.com/connectors/entity-change/instance#> .\n" + |
| 58 | + "@prefix rdf4j: <http://rdf4j.org/schema/rdf4j#> .\n" + |
| 59 | + "@prefix ex: <http://example.com/ns#> .\n" + |
| 60 | + "\n" + |
| 61 | + "rdf4j:SHACLShapeGraph {\n" + |
| 62 | + "\n" + |
| 63 | + "ex:\n" + |
| 64 | + "\tsh:declare [\n" + |
| 65 | + "\t\tsh:prefix \"ex\" ;\n" + |
| 66 | + "\t\tsh:namespace \"http://example.com/ns#\"^^xsd:anyURI ;\n" + |
| 67 | + "\t] ;\n" + |
| 68 | + "\tsh:declare [\n" + |
| 69 | + "\t\tsh:prefix \"schema\" ;\n" + |
| 70 | + "\t\tsh:namespace \"http://schema.org/\"^^xsd:anyURI ;\n" + |
| 71 | + "\t] .\n" + |
| 72 | + "\n" + |
| 73 | + " ex:LanguageExampleShape\n" + |
| 74 | + " \ta sh:NodeShape ;\n" + |
| 75 | + " \tsh:targetClass ex:Country ;\n" + |
| 76 | + " \tsh:sparql [\n" + |
| 77 | + " \t\ta sh:SPARQLConstraint ; # This triple is optional\n" + |
| 78 | + " \t\tsh:message \"Values are literals with German language tag.\" ;\n" + |
| 79 | + " \t\tsh:prefixes ex: ;\n" + |
| 80 | + " \t\tsh:deactivated false ;\n" + |
| 81 | + " \t\tsh:select \"\"\"\n" + |
| 82 | + " \t\t\tSELECT $this (ex:germanLabel AS ?path) ?value ?failure\n" + |
| 83 | + " \t\t\tWHERE {\n" + |
| 84 | + " \t\t\t\t$this ex:germanLabel ?value .\n" + |
| 85 | + " \t\t\t\tBIND(isIri(?value) as ?failure)\n" + |
| 86 | + " \t\t\t}\n" + |
| 87 | + " \t\t\t\"\"\" ;\n" + |
| 88 | + " \t] .\n" + |
| 89 | + "}\n"), RDFFormat.TRIG); |
| 90 | + |
| 91 | + Update update = connection.prepareUpdate("PREFIX ex: <http://example.com/ns#>\n" + |
| 92 | + "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" + |
| 93 | + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + |
| 94 | + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + |
| 95 | + "PREFIX sh: <http://www.w3.org/ns/shacl#>\n" + |
| 96 | + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + |
| 97 | + "\n" + |
| 98 | + "INSERT DATA {\n" + |
| 99 | + "ex:InvalidCountry a ex:Country .\n" + |
| 100 | + "ex:InvalidCountry ex:germanLabel ex:invalidValue .\n" + |
| 101 | + "}\n"); |
| 102 | + |
| 103 | + // assert exception is thrown |
| 104 | + RepositoryException repositoryException = assertThrows(RepositoryException.class, update::execute); |
| 105 | + Throwable cause = repositoryException.getCause().getCause(); |
| 106 | + Assertions.assertEquals( |
| 107 | + "org.eclipse.rdf4j.sail.shacl.ast.ShaclSparqlConstraintFailureException: The ?failure variable was true for <http://example.com/ns#InvalidCountry> in shape <http://example.com/ns#LanguageExampleShape> with result resultBindingSet: [this=http://example.com/ns#InvalidCountry;value=http://example.com/ns#invalidValue;failure=\"true\"^^<http://www.w3.org/2001/XMLSchema#boolean>;path=http://example.com/ns#germanLabel] and dataGraph: [] and query:PREFIX schema: <http://schema.org/> \n" |
| 108 | + + |
| 109 | + "PREFIX ex: <http://example.com/ns#> \n" + |
| 110 | + "\n" + |
| 111 | + "\n" + |
| 112 | + "\n" + |
| 113 | + " \t\t\tSELECT $this (ex:germanLabel AS ?path) ?value ?failure\n" + |
| 114 | + " \t\t\tWHERE {\n" + |
| 115 | + " \t\t\t\t$this ex:germanLabel ?value .\n" + |
| 116 | + " \t\t\t\tBIND(isIri(?value) as ?failure)\n" + |
| 117 | + " \t\t\t}\n" + |
| 118 | + " \t\t\t", |
| 119 | + cause.toString()); |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments