Skip to content

Commit

Permalink
Merge pull request eclipse-rdf4j#4222 from eclipse/main
Browse files Browse the repository at this point in the history
Merge main into develop
  • Loading branch information
hmottestad authored Oct 27, 2022
2 parents 683463c + 10f4e7b commit 511339d
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public ValidationQuery generateSparqlValidationQuery(ConnectionsGroup connection
return ValidationQuery.Deactivated.getInstance();
}

if (!getPath().isSupported()) {
logger.error("Unsupported SHACL feature detected: {}. Shape ignored!\n{}", path, this);
return ValidationQuery.Deactivated.getInstance();
}

ValidationQuery validationQuery = constraintComponents.stream()
.map(c -> {
ValidationQuery validationQuery1 = c.generateSparqlValidationQuery(connectionsGroup,
Expand Down Expand Up @@ -177,38 +182,14 @@ public PlanNode generateTransactionalValidationPlan(ConnectionsGroup connections
return EmptyNode.getInstance();
}

PlanNode union = EmptyNode.getInstance();
if (!getPath().isSupported()) {
logger.error("Unsupported SHACL feature detected: {}. Shape ignored!\n{}", path, this);
return EmptyNode.getInstance();
}

// if (negatePlan) {
// assert overrideTargetNode == null : "Negated property shape with override target is not supported at the moment!";
//
// PlanNode ret = EmptyNode.getInstance();
//
// for (ConstraintComponent constraintComponent : constraintComponents) {
// PlanNode planNode = constraintComponent.generateTransactionalValidationPlan(connectionsGroup,
// logValidationPlans, () -> getAllLocalTargetsPlan(connectionsGroup, negatePlan), negateChildren,
// false, Scope.propertyShape);
//
// PlanNode allTargetsPlan = getAllLocalTargetsPlan(connectionsGroup, negatePlan);
//
// Unique invalid = Unique.getInstance(planNode);
//
// PlanNode discardedLeft = new InnerJoin(allTargetsPlan, invalid)
// .getDiscardedLeft(BufferedPlanNode.class);
//
// ret = UnionNode.getInstance(ret, discardedLeft);
//
// }
//
// return ret;
//
// }
PlanNode union = EmptyNode.getInstance();

for (ConstraintComponent constraintComponent : constraintComponents) {
if (!getPath().isSupported()) {
logger.error("Unsupported path detected. Shape ignored!\n" + this);
continue;
}

PlanNode validationPlanNode = constraintComponent
.generateTransactionalValidationPlan(connectionsGroup, validationSettings, overrideTargetNode,
Expand Down Expand Up @@ -276,6 +257,17 @@ public Path getPath() {
return path;
}

@Override
public boolean requiresEvaluation(ConnectionsGroup connectionsGroup, Scope scope, Resource[] dataGraph,
StatementMatcher.StableRandomVariableProvider stableRandomVariableProvider) {
if (!getPath().isSupported()) {
logger.error("Unsupported SHACL feature detected: {}. Shape ignored!\n{}", path, this);
return false;
}

return super.requiresEvaluation(connectionsGroup, scope, dataGraph, stableRandomVariableProvider);
}

@Override
public ConstraintComponent deepClone() {
PropertyShape nodeShape = new PropertyShape(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ public String toString() {
private static Stream<TestCase> findTestCases(String testCase, ExpectedResult baseCase) {
String shacl;

try (InputStream resourceAsStream = AbstractShaclTest.class.getClassLoader()
.getResourceAsStream(testCase + "/shacl.trig")) {
try (InputStream resourceAsStream = getResourceAsStream(testCase + "/shacl.trig")) {
assert Objects.nonNull(resourceAsStream) : "Could not find: " + testCase + "/shacl.trig";
shacl = IOUtils.toString(resourceAsStream, StandardCharsets.UTF_8);

Expand All @@ -347,7 +346,7 @@ private static Stream<TestCase> findTestCases(String testCase, ExpectedResult ba
return Stream.empty();
}

return Arrays.stream(new File(resource.getFile()).list())
return Arrays.stream(Objects.requireNonNull(new File(resource.getFile()).list()))
.filter(s -> !s.startsWith("."))
.sorted()
.map(caseName -> {
Expand Down Expand Up @@ -443,8 +442,6 @@ void runTestCase(TestCase testCase, IsolationLevel isolationLevel, boolean prelo

}

Model validationReportActual = new LinkedHashModel();

for (File queryFile : testCase.getQueries()) {
try {
String query = FileUtils.readFileToString(queryFile, StandardCharsets.UTF_8);
Expand All @@ -466,8 +463,6 @@ void runTestCase(TestCase testCase, IsolationLevel isolationLevel, boolean prelo

exception = true;
logger.debug(sailException.getMessage());
validationReportActual = ((ShaclSailValidationException) sailException.getCause())
.validationReportAsModel();
printResults(sailException);
}
} catch (IOException e) {
Expand Down Expand Up @@ -506,17 +501,18 @@ private void printTestCase(TestCase testCase) {

private static void testValidationReport(String dataPath, Model validationReportActual) {
try {
InputStream resourceAsStream = AbstractShaclTest.class.getClassLoader()
.getResourceAsStream(dataPath + "report.ttl");
InputStream resourceAsStream = getResourceAsStream(dataPath + "report.ttl");
if (resourceAsStream == null) {
logger.error(dataPath + "report.ttl did not exist. Creating an empty file!");
logger.warn(dataPath + "report.ttl did not exist, attempting to create an empty file!");

String file = Objects.requireNonNull(AbstractShaclTest.class.getClassLoader()
.getResource(dataPath))
.getFile()
.replace("/target/test-classes/", "/src/test/resources/");
boolean newFile = new File(file + "report.ttl").createNewFile();

if (!newFile) {
logger.error(dataPath + "report.ttl did not exist and could not create an empty file!");
}
}
Model validationReportExpected = getModel(resourceAsStream);

Expand All @@ -533,6 +529,10 @@ private static void testValidationReport(String dataPath, Model validationReport
}
}

private static InputStream getResourceAsStream(String dataPath) {
return AbstractShaclTest.class.getClassLoader().getResourceAsStream(dataPath);
}

private static void writeActualModelToExpectedModelForDevPurposes(String dataPath, Model report)
throws IOException {
String file = Objects.requireNonNull(AbstractShaclTest.class.getClassLoader()
Expand Down Expand Up @@ -631,8 +631,7 @@ void referenceImplementationTestCaseValidation(TestCase testCase) {
org.apache.jena.rdf.model.Model data = JenaUtil.createMemoryModel();

if (testCase.hasInitialData()) {
try (InputStream resourceAsStream = AbstractShaclTest.class.getClassLoader()
.getResourceAsStream(testCase.getInitialData())) {
try (InputStream resourceAsStream = getResourceAsStream(testCase.getInitialData())) {
data.read(resourceAsStream, "", org.apache.jena.util.FileUtils.langTurtle);
} catch (IOException e) {
throw new IllegalStateException(e);
Expand Down Expand Up @@ -668,8 +667,7 @@ void referenceImplementationTestCaseValidation(TestCase testCase) {
RDFFormat.TRIG);

try {
InputStream resourceAsStream = AbstractShaclTest.class.getClassLoader()
.getResourceAsStream(testCase.getTestCasePath() + "report.ttl");
InputStream resourceAsStream = getResourceAsStream(testCase.getTestCasePath() + "report.ttl");

Model validationReportActual = getModel(resourceAsStream);

Expand Down Expand Up @@ -717,8 +715,7 @@ private static Model getModel(InputStream resourceAsStream) throws IOException {

private static void checkShapesConformToW3cShaclRecommendation(org.apache.jena.rdf.model.Model shacl) {
org.apache.jena.rdf.model.Model w3cShacl = JenaUtil.createMemoryModel();
try (InputStream resourceAsStream = AbstractShaclTest.class.getClassLoader()
.getResourceAsStream("w3cshacl.ttl")) {
try (InputStream resourceAsStream = getResourceAsStream("w3cshacl.ttl")) {
w3cShacl.read(resourceAsStream, "", org.apache.jena.util.FileUtils.langTurtle);
} catch (IOException e) {
throw new IllegalStateException(e);
Expand Down Expand Up @@ -870,7 +867,7 @@ private void printFile(String filename) {
try {
System.out.println("### " + filename + " ###");
String s = IOUtils.toString(
Objects.requireNonNull(AbstractShaclTest.class.getClassLoader().getResourceAsStream(filename)),
Objects.requireNonNull(getResourceAsStream(filename)),
StandardCharsets.UTF_8);

s = removeLeadingPrefixStatements(s);
Expand All @@ -883,29 +880,30 @@ private void printFile(String filename) {
}

private static String removeLeadingPrefixStatements(String s) {
String[] split = s.split("\n");
s = "";
String[] splitByNewLine = s.split("\n");

boolean skippingPrefixes = true;

for (String s1 : split) {
StringBuilder stringBuilder = new StringBuilder();
for (String line : splitByNewLine) {
if (skippingPrefixes) {
if (!(s1.trim().equals("") ||
s1.trim().toLowerCase().startsWith("@prefix") ||
s1.trim().toLowerCase().startsWith("@base") ||
s1.trim().toLowerCase().startsWith("prefix"))) {
if (!(line.trim().equals("") ||
line.trim().toLowerCase().startsWith("@prefix") ||
line.trim().toLowerCase().startsWith("@base") ||
line.trim().toLowerCase().startsWith("prefix"))) {
skippingPrefixes = false;
}
}

if (!skippingPrefixes) {
s += s1 + "\n";
stringBuilder.append(line).append("\n");
}

}
return s;
return stringBuilder.toString();
}

void runTestCaseSingleTransaction(TestCase testCase, IsolationLevel isolationLevel) {
void runTestCaseSingleTransaction(TestCase testCase) {

SailRepository shaclRepository = getShaclSail(testCase, true);

Expand All @@ -915,7 +913,7 @@ void runTestCaseSingleTransaction(TestCase testCase, IsolationLevel isolationLev
Model validationReportActual = new LinkedHashModel();

try (SailRepositoryConnection shaclSailConnection = shaclRepository.getConnection()) {
shaclSailConnection.begin(isolationLevel);
shaclSailConnection.begin(IsolationLevels.NONE);

for (File queryFile : testCase.getQueries()) {
try {
Expand Down Expand Up @@ -1028,7 +1026,6 @@ void runParsingTest(TestCase testCase) {
.getDataAndRelease();

Model actual = new DynamicModelFactory().createEmptyModel();
HashSet<Resource> dedupe = new HashSet<>();
shapes.forEach(shape -> shape.toModel(actual));

Model expected = new LinkedHashModel(testCase.getShacl());
Expand Down Expand Up @@ -1093,8 +1090,8 @@ private void printResults(ValidationReport report) {
}

private void printResults(RepositoryException sailException) {
ValidationReport validationReport = ((ShaclSailValidationException) sailException.getCause())
.getValidationReport();
var shaclSailValidationException = (ShaclSailValidationException) sailException.getCause();
ValidationReport validationReport = shaclSailValidationException.getValidationReport();
printResults(validationReport);
}

Expand Down Expand Up @@ -1159,27 +1156,6 @@ void runWithAutomaticLogging(Runnable r) {
}
}

/**
* Sort and output testCasePaths
*
* @param args
*/
public static void main(String[] args) {

System.out.println("\n\tprivate static final List<String> testCasePaths = Stream.of(");
String testCasesString = testCasePaths
.stream()
.map(a -> "\t\t\"" + a + "\"")
.reduce((a, b) -> a + ",\n" + b)
.orElse("");

System.out.println(testCasesString);
System.out.println("\t)\n" +
"\t\t.distinct()\n" +
"\t\t.sorted()\n" +
"\t\t.collect(Collectors.toList());");
}

enum ExpectedResult {
valid,
invalid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
package org.eclipse.rdf4j.sail.shacl;

import org.eclipse.rdf4j.common.transaction.IsolationLevel;
import org.eclipse.rdf4j.common.transaction.IsolationLevels;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand All @@ -30,7 +29,7 @@ public void test(TestCase testCase, IsolationLevel isolationLevel) {
@ParameterizedTest
@MethodSource("testCases")
public void testSingleTransaction(TestCase testCase) {
runWithAutomaticLogging(() -> runTestCaseSingleTransaction(testCase, IsolationLevels.NONE));
runWithAutomaticLogging(() -> runTestCaseSingleTransaction(testCase));
}

@ParameterizedTest
Expand Down
Loading

0 comments on commit 511339d

Please sign in to comment.