Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
eclipse-rdf4j/rdf4j#1298 made rdfs reasoner configurable and added ba…
Browse files Browse the repository at this point in the history
…ck tests

Signed-off-by: Håvard Ottestad <hmottestad@gmail.com>
  • Loading branch information
hmottestad committed Feb 19, 2019
1 parent 152ac17 commit e70aa52
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 43 deletions.
11 changes: 11 additions & 0 deletions shacl/src/main/java/org/eclipse/rdf4j/sail/shacl/ShaclSail.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ public class ShaclSail extends NotifyingSailWrapper {

private boolean cacheSelectNodes = ShaclSailConfig.CACHE_SELECT_NODES_DEFAULT;

private boolean rdfsSubClassReasoning = ShaclSailConfig.RDFS_SUB_CLASS_REASONING_DEFAULT;

static {
try {
SH_OR_UPDATE_QUERY = IOUtils.toString(
Expand Down Expand Up @@ -472,4 +474,13 @@ public boolean isCacheSelectNodes() {
public void setCacheSelectNodes(boolean cacheSelectNodes) {
this.cacheSelectNodes = cacheSelectNodes;
}


public boolean isRdfsSubClassReasoning() {
return rdfsSubClassReasoning;
}

public void setRdfsSubClassReasoning(boolean rdfsSubClassReasoning) {
this.rdfsSubClassReasoning = rdfsSubClassReasoning;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class ShaclSailConnection extends NotifyingSailConnectionWrapper implemen

public Stats stats;

RdfsSubClassOfReasoner rdfsSubClassOfReasoner = new RdfsSubClassOfReasoner();
RdfsSubClassOfReasoner rdfsSubClassOfReasoner;

private boolean preparedHasRun = false;

Expand Down Expand Up @@ -252,7 +252,9 @@ private List<Tuple> validate() {
return Collections.emptyList();
}

rdfsSubClassOfReasoner = RdfsSubClassOfReasoner.createReasoner(this);
if(sail.isRdfsSubClassReasoning()) {
rdfsSubClassOfReasoner = RdfsSubClassOfReasoner.createReasoner(this);
}

try {
validating = true;
Expand Down Expand Up @@ -309,6 +311,7 @@ private List<Tuple> validate() {
}
} finally {
validating = false;
rdfsSubClassOfReasoner = null;
}
}

Expand Down Expand Up @@ -337,7 +340,7 @@ void fillAddedAndRemovedStatementRepositories() {
addedStatementsSet
.stream()
.filter(statement -> !removedStatementsSet.contains(statement))
.flatMap(statement -> rdfsSubClassOfReasoner.forwardChain(statement))
.flatMap(statement -> rdfsSubClassOfReasoner == null ? Stream.of(statement) : rdfsSubClassOfReasoner.forwardChain(statement))
.forEach(statement -> connection.addStatement(statement.getSubject(), statement.getPredicate(), statement.getObject(), statement.getContext()));
connection.commit();
}
Expand All @@ -347,7 +350,7 @@ void fillAddedAndRemovedStatementRepositories() {
removedStatementsSet
.stream()
.filter(statement -> !addedStatementsSet.contains(statement))
.flatMap(statement -> rdfsSubClassOfReasoner.forwardChain(statement))
.flatMap(statement -> rdfsSubClassOfReasoner == null ? Stream.of(statement) : rdfsSubClassOfReasoner.forwardChain(statement))
.forEach(statement -> connection.addStatement(statement.getSubject(), statement.getPredicate(), statement.getObject(), statement.getContext()));
connection.commit();
}
Expand Down Expand Up @@ -469,7 +472,7 @@ public boolean isBaseSailEmpty() {

@Override
public CloseableIteration<? extends Statement, SailException> getStatements(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts) throws SailException {
if (includeInferred && validating && obj instanceof Resource && RDF.TYPE.equals(pred)) {
if (rdfsSubClassOfReasoner != null && includeInferred && validating && obj instanceof Resource && RDF.TYPE.equals(pred)) {
Set<Resource> inferredTypes = rdfsSubClassOfReasoner.backwardsChain((Resource) obj);
if (!inferredTypes.isEmpty()) {

Expand Down Expand Up @@ -546,7 +549,7 @@ public void close() throws SailException {
public boolean hasStatement(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts) throws SailException {
boolean hasStatement = super.hasStatement(subj, pred, obj, includeInferred, contexts);

if (includeInferred && validating && obj instanceof Resource && RDF.TYPE.equals(pred)) {
if (rdfsSubClassOfReasoner != null && includeInferred && validating && obj instanceof Resource && RDF.TYPE.equals(pred)) {
return hasStatement | rdfsSubClassOfReasoner
.backwardsChain((Resource) obj)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.LOG_VALIDATION_VIOLATIONS;
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.NAMESPACE;
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.PARALLEL_VALIDATION;
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.RDFS_SUB_CLASS_REASONING;
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.UNDEFINED_TARGET_VALIDATES_ALL_SUBJECTS;
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.VALIDATION_ENABLED;

Expand All @@ -49,6 +50,8 @@ public class ShaclSailConfig extends AbstractDelegatingSailImplConfig {

public static final boolean GLOBAL_LOG_VALIDATION_EXECUTION_DEFAULT = false;

public static final boolean RDFS_SUB_CLASS_REASONING_DEFAULT = true;

private boolean parallelValidation = PARALLEL_VALIDATION_DEFAULT;

private boolean undefinedTargetValidatesAllSubjects = UNDEFINED_TARGET_VALIDATES_ALL_SUBJECTS_DEFAULT;
Expand All @@ -65,6 +68,8 @@ public class ShaclSailConfig extends AbstractDelegatingSailImplConfig {

private boolean globalLogValidationExecution = GLOBAL_LOG_VALIDATION_EXECUTION_DEFAULT;

private boolean rdfsSubClassReasoning = RDFS_SUB_CLASS_REASONING_DEFAULT;

public ShaclSailConfig() {
super(ShaclSailFactory.SAIL_TYPE);
}
Expand Down Expand Up @@ -133,22 +138,28 @@ public void setGlobalLogValidationExecution(boolean globalLogValidationExecution
this.globalLogValidationExecution = globalLogValidationExecution;
}

public boolean isRdfsSubClassReasoning() {
return rdfsSubClassReasoning;
}

public void setRdfsSubClassReasoning(boolean rdfsSubClassReasoning) {
this.rdfsSubClassReasoning = rdfsSubClassReasoning;
}

@Override
public Resource export(Model m) {
Resource implNode = super.export(m);

m.setNamespace("sail-shacl", NAMESPACE);
m.add(implNode, PARALLEL_VALIDATION, BooleanLiteral.valueOf(isParallelValidation()));
m.add(implNode, UNDEFINED_TARGET_VALIDATES_ALL_SUBJECTS,
BooleanLiteral.valueOf(isUndefinedTargetValidatesAllSubjects()));
m.add(implNode, UNDEFINED_TARGET_VALIDATES_ALL_SUBJECTS, BooleanLiteral.valueOf(isUndefinedTargetValidatesAllSubjects()));
m.add(implNode, LOG_VALIDATION_PLANS, BooleanLiteral.valueOf(isLogValidationPlans()));
m.add(implNode, LOG_VALIDATION_VIOLATIONS, BooleanLiteral.valueOf(isLogValidationViolations()));
m.add(implNode, IGNORE_NO_SHAPES_LOADED_EXCEPTION,
BooleanLiteral.valueOf(isIgnoreNoShapesLoadedException()));
m.add(implNode, IGNORE_NO_SHAPES_LOADED_EXCEPTION, BooleanLiteral.valueOf(isIgnoreNoShapesLoadedException()));
m.add(implNode, VALIDATION_ENABLED, BooleanLiteral.valueOf(isValidationEnabled()));
m.add(implNode, CACHE_SELECT_NODES, BooleanLiteral.valueOf(isCacheSelectNodes()));
m.add(implNode, GLOBAL_LOG_VALIDATION_EXECUTION,
BooleanLiteral.valueOf(isGlobalLogValidationExecution()));
m.add(implNode, GLOBAL_LOG_VALIDATION_EXECUTION, BooleanLiteral.valueOf(isGlobalLogValidationExecution()));
m.add(implNode, RDFS_SUB_CLASS_REASONING, BooleanLiteral.valueOf(isRdfsSubClassReasoning()));
return implNode;
}

Expand All @@ -173,6 +184,8 @@ public void parse(Model m, Resource implNode) throws SailConfigException {
l -> setCacheSelectNodes(l.booleanValue()));
Models.objectLiteral(m.filter(implNode, GLOBAL_LOG_VALIDATION_EXECUTION, null)).ifPresent(
l -> setGlobalLogValidationExecution(l.booleanValue()));
Models.objectLiteral(m.filter(implNode, RDFS_SUB_CLASS_REASONING, null)).ifPresent(
l -> setRdfsSubClassReasoning(l.booleanValue()));
}
catch (IllegalArgumentException e) {
throw new SailConfigException("error parsing Sail configuration", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class ShaclSailSchema {
/** <code>http://rdf4j.org/config/sail/shacl#globalLogValidationExecution</code> */
public final static IRI GLOBAL_LOG_VALIDATION_EXECUTION = create("globalLogValidationExecution");

/** <code>http://rdf4j.org/config/sail/shacl#rdfsSubClassReasoning</code> */
public final static IRI RDFS_SUB_CLASS_REASONING = create("rdfsSubClassReasoning");


private static final IRI create(String localName) {
return SimpleValueFactory.getInstance().createIRI(NAMESPACE, localName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,37 @@
abstract public class AbstractShaclTest {

private static final List<String> testCasePaths = Arrays.asList(
// "test-cases/complex/dcat",
// "test-cases/complex/foaf",
// "test-cases/datatype/simple",
// "test-cases/minLength/simple",
// "test-cases/maxLength/simple",
// "test-cases/pattern/simple",
// "test-cases/languageIn/simple",
// "test-cases/nodeKind/simple",
// "test-cases/minCount/simple",
// "test-cases/maxCount/simple",
// "test-cases/or/inheritance",
// "test-cases/or/inheritance-deep",
// "test-cases/or/inheritance-deep-minCountMaxCount",
// "test-cases/or/inheritanceNodeShape",
// "test-cases/or/datatype",
// "test-cases/or/minCountMaxCount",
// "test-cases/or/maxCount",
// "test-cases/or/minCount",
// "test-cases/or/nodeKindMinLength",
// "test-cases/or/implicitAnd",
// "test-cases/or/datatypeDifferentPaths",
// "test-cases/minExclusive/simple",
// "test-cases/minExclusive/dateVsTime",
// "test-cases/maxExclusive/simple",
// "test-cases/minInclusive/simple",
// "test-cases/maxInclusive/simple",
// "test-cases/implicitTargetClass/simple",
// "test-cases/class/simple",
"test-cases/complex/dcat",
"test-cases/complex/foaf",
"test-cases/datatype/simple",
"test-cases/minLength/simple",
"test-cases/maxLength/simple",
"test-cases/pattern/simple",
"test-cases/languageIn/simple",
"test-cases/nodeKind/simple",
"test-cases/minCount/simple",
"test-cases/maxCount/simple",
"test-cases/or/inheritance",
"test-cases/or/inheritance-deep",
"test-cases/or/inheritance-deep-minCountMaxCount",
"test-cases/or/inheritanceNodeShape",
"test-cases/or/datatype",
"test-cases/or/minCountMaxCount",
"test-cases/or/maxCount",
"test-cases/or/minCount",
"test-cases/or/nodeKindMinLength",
"test-cases/or/implicitAnd",
"test-cases/or/datatypeDifferentPaths",
"test-cases/minExclusive/simple",
"test-cases/minExclusive/dateVsTime",
"test-cases/maxExclusive/simple",
"test-cases/minInclusive/simple",
"test-cases/maxInclusive/simple",
"test-cases/implicitTargetClass/simple",
"test-cases/class/simple",
"test-cases/class/subclass",
// "test-cases/or/class",
// "test-cases/or/datatype2",
"test-cases/or/class",
"test-cases/or/datatype2",
"test-cases/or/minCountDifferentPath"
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.LOG_VALIDATION_PLANS;
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.LOG_VALIDATION_VIOLATIONS;
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.PARALLEL_VALIDATION;
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.RDFS_SUB_CLASS_REASONING;
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.UNDEFINED_TARGET_VALIDATES_ALL_SUBJECTS;
import static org.eclipse.rdf4j.sail.shacl.config.ShaclSailSchema.VALIDATION_ENABLED;

Expand Down Expand Up @@ -52,6 +53,7 @@ public void defaultsCorrectlySet() {
assertThat(subject.isValidationEnabled()).isTrue();
assertThat(subject.isCacheSelectNodes()).isTrue();
assertThat(subject.isGlobalLogValidationExecution()).isFalse();
assertThat(subject.isRdfsSubClassReasoning()).isTrue();
}

@Test
Expand All @@ -66,7 +68,8 @@ public void parseFromModelSetValuesCorrectly() {
.add(IGNORE_NO_SHAPES_LOADED_EXCEPTION, true)
.add(VALIDATION_ENABLED, true)
.add(CACHE_SELECT_NODES, true)
.add(GLOBAL_LOG_VALIDATION_EXECUTION, true);
.add(GLOBAL_LOG_VALIDATION_EXECUTION, true)
.add(RDFS_SUB_CLASS_REASONING, false);
// @formatter:on

subject.parse(mb.build(), implNode);
Expand All @@ -79,6 +82,8 @@ public void parseFromModelSetValuesCorrectly() {
assertThat(subject.isValidationEnabled()).isTrue();
assertThat(subject.isCacheSelectNodes()).isTrue();
assertThat(subject.isGlobalLogValidationExecution()).isTrue();
assertThat(subject.isRdfsSubClassReasoning()).isFalse();

}

@Test
Expand Down Expand Up @@ -112,6 +117,7 @@ public void exportAddsAllConfigData() {
assertThat(m.contains(node, VALIDATION_ENABLED, null)).isTrue();
assertThat(m.contains(node, CACHE_SELECT_NODES, null)).isTrue();
assertThat(m.contains(node, GLOBAL_LOG_VALIDATION_EXECUTION, null)).isTrue();
assertThat(m.contains(node, RDFS_SUB_CLASS_REASONING, null)).isTrue();
}

}

0 comments on commit e70aa52

Please sign in to comment.