Skip to content

Commit

Permalink
Merge pull request #179 from eclipse/issues/1312_shacl_cleanup
Browse files Browse the repository at this point in the history
Issues/1312 shacl cleanup closes #1312
  • Loading branch information
hmottestad authored Mar 1, 2019
2 parents 3167d96 + 241d835 commit 19e69a7
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ public static class Factory {
public static List<NodeShape> getShapes(SailRepositoryConnection connection, ShaclSail sail) {
try (Stream<Statement> stream = Iterations.stream(connection.getStatements(null, RDF.TYPE, SHACL.NODE_SHAPE))) {
return stream.map(Statement::getSubject).map(shapeId -> {
if (hasTargetClass(shapeId, connection)) {
return new TargetClass(shapeId, connection);
} else if (hasTargetNode(shapeId, connection)) {
return new TargetNode(shapeId, connection);

ShaclProperties shaclProperties = new ShaclProperties(shapeId, connection);

if (shaclProperties.targetClass != null) {
return new TargetClass(shapeId, connection, shaclProperties.targetClass);
} else if (!shaclProperties.targetNode.isEmpty()) {
return new TargetNode(shapeId, connection, shaclProperties.targetNode);
} else {
if(sail.isUndefinedTargetValidatesAllSubjects()) {
return new NodeShape(shapeId, connection); // target class nodeShapes are the only supported nodeShapes
Expand All @@ -110,15 +113,8 @@ public static List<NodeShape> getShapes(SailRepositoryConnection connection, Sha
}
}

private static boolean hasTargetClass(Resource shapeId, SailRepositoryConnection connection) {
return connection.hasStatement(shapeId, SHACL.TARGET_CLASS, null, true);
}

private static boolean hasTargetNode(Resource shapeId, SailRepositoryConnection connection) {
return connection.hasStatement(shapeId, SHACL.TARGET_NODE, null, true);
}
}

@Override
public String toString() {
return id.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class ShaclProperties {
Expand All @@ -32,6 +34,9 @@ public class ShaclProperties {

String pattern;

Resource targetClass;
List<Value> targetNode = new ArrayList<>(0);


public ShaclProperties(Resource propertyShapeId, SailRepositoryConnection connection) {

Expand Down Expand Up @@ -119,11 +124,20 @@ public ShaclProperties(Resource propertyShapeId, SailRepositoryConnection connec
pattern = object.stringValue();
break;
case "http://www.w3.org/ns/shacl#class":
if (pattern != null) {
if (clazz != null) {
throw new IllegalStateException("sh:class aleady populated");
}
clazz = (Resource) object;
break;
case "http://www.w3.org/ns/shacl#targetNode":
targetNode.add(object);
break;
case "http://www.w3.org/ns/shacl#targetClass":
if (targetClass != null) {
throw new IllegalStateException("sh:targetClass aleady populated");
}
targetClass = (Resource) object;
break;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*******************************************************************************
* Copyright (c) 2018 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/

package org.eclipse.rdf4j.sail.shacl.AST;

import org.eclipse.rdf4j.sail.shacl.ShaclSailConnection;
Expand All @@ -10,6 +18,7 @@
import org.eclipse.rdf4j.sail.shacl.planNodes.UnBufferedPlanNode;
import org.eclipse.rdf4j.sail.shacl.planNodes.UnionNode;


public class StandardisedPlanHelper {

interface FilterAttacher {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,23 @@
import java.util.stream.Stream;

/**
* The AST (Abstract Syntax Tree) node
* sh:targetClass
*
* @author Heshan Jayasinghe
*/
public class TargetClass extends NodeShape {

Resource targetClass;
private final Resource targetClass;

TargetClass(Resource id, SailRepositoryConnection connection) {
TargetClass(Resource id, SailRepositoryConnection connection, Resource targetClass) {
super(id, connection);

try (Stream<Statement> stream = Iterations.stream(connection.getStatements(id, SHACL.TARGET_CLASS, null))) {
targetClass = stream.map(Statement::getObject).map(v -> (Resource) v).findAny().orElseThrow(() -> new RuntimeException("Expected to find sh:targetClass on " + id));
}

this.targetClass = targetClass;
}

@Override
public PlanNode getPlan(ShaclSailConnection shaclSailConnection, NodeShape nodeShape, boolean printPlans, PlanNode overrideTargetNode) {
return new TrimTuple(new LoggingNode(new Select(shaclSailConnection, getQuery("?a", "?c", shaclSailConnection.getRdfsSubClassOfReasoner())), ""), 0, 1);
PlanNode parent = shaclSailConnection.getCachedNodeFor(new Select(shaclSailConnection, getQuery("?a", "?c", shaclSailConnection.getRdfsSubClassOfReasoner())));
return new TrimTuple(new LoggingNode(parent, ""), 0, 1);
}

@Override
Expand All @@ -59,7 +56,8 @@ public PlanNode getPlanAddedStatements(ShaclSailConnection shaclSailConnection,

@Override
public PlanNode getPlanRemovedStatements(ShaclSailConnection shaclSailConnection, NodeShape nodeShape) {
return new TrimTuple(new Select(shaclSailConnection.getRemovedStatements(), getQuery("?a", "?c", null)), 0,1);
PlanNode parent = shaclSailConnection.getCachedNodeFor(new Select(shaclSailConnection.getRemovedStatements(), getQuery("?a", "?c", null)));
return new TrimTuple(parent, 0,1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,55 @@

package org.eclipse.rdf4j.sail.shacl.AST;

import org.eclipse.rdf4j.common.iteration.Iterations;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.SHACL;
import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection;
import org.eclipse.rdf4j.sail.NotifyingSailConnection;
import org.eclipse.rdf4j.sail.SailConnection;
import org.eclipse.rdf4j.sail.shacl.RdfsSubClassOfReasoner;
import org.eclipse.rdf4j.sail.shacl.ShaclSailConnection;
import org.eclipse.rdf4j.sail.shacl.planNodes.ExternalTypeFilterNode;
import org.eclipse.rdf4j.sail.shacl.planNodes.LoggingNode;
import org.eclipse.rdf4j.sail.shacl.planNodes.PlanNode;
import org.eclipse.rdf4j.sail.shacl.planNodes.Select;
import org.eclipse.rdf4j.sail.shacl.planNodes.SetFilterNode;
import org.eclipse.rdf4j.sail.shacl.planNodes.TrimTuple;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* The AST (Abstract Syntax Tree) node
* sh:targetNode
*
* @author Heshan Jayasinghe
* @author Håvard Ottestad
*/
public class TargetNode extends NodeShape {

Set<Value> targetNodeList;
private final Set<Value> targetNodeSet;

TargetNode(Resource id, SailRepositoryConnection connection) {
TargetNode(Resource id, SailRepositoryConnection connection, List<Value> targetNode) {
super(id, connection);

try (Stream<Statement> stream = Iterations.stream(connection.getStatements(id, SHACL.TARGET_NODE, null))) {
targetNodeList = stream.map(Statement::getObject).collect(Collectors.toSet());
}

this.targetNodeSet = new HashSet<>(targetNode);
assert !this.targetNodeSet.isEmpty();
}

@Override
public PlanNode getPlan(ShaclSailConnection shaclSailConnection, NodeShape nodeShape, boolean printPlans, PlanNode overrideTargetNode) {
return new TrimTuple(new LoggingNode(new Select(shaclSailConnection, getQuery("?a", "?c", shaclSailConnection.getRdfsSubClassOfReasoner())), ""), 0, 1);
PlanNode parent = shaclSailConnection.getCachedNodeFor(new Select(shaclSailConnection, getQuery("?a", "?c", shaclSailConnection.getRdfsSubClassOfReasoner())));
return new TrimTuple(new LoggingNode(parent, ""), 0, 1);
}

@Override
public PlanNode getPlanAddedStatements(ShaclSailConnection shaclSailConnection, NodeShape nodeShape) {
return new TrimTuple(new LoggingNode(new Select(shaclSailConnection.getAddedStatements(), getQuery("?a", "?c", null)), ""), 0, 1);
PlanNode parent = shaclSailConnection.getCachedNodeFor(new Select(shaclSailConnection.getAddedStatements(), getQuery("?a", "?c", null)));
return new TrimTuple(new LoggingNode(parent, ""), 0, 1);

}

@Override
public PlanNode getPlanRemovedStatements(ShaclSailConnection shaclSailConnection, NodeShape nodeShape) {
return new TrimTuple(new Select(shaclSailConnection.getRemovedStatements(), getQuery("?a", "?c", null)), 0, 1);
PlanNode parent = shaclSailConnection.getCachedNodeFor(new Select(shaclSailConnection.getRemovedStatements(), getQuery("?a", "?c", null)));
return new TrimTuple(parent, 0, 1);
}

@Override
Expand All @@ -73,7 +67,7 @@ public boolean requiresEvaluation(SailConnection addedStatements, SailConnection
@Override
public String getQuery(String subjectVariable, String objectVariable, RdfsSubClassOfReasoner rdfsSubClassOfReasoner) {

return targetNodeList.stream()
return targetNodeSet.stream()
.map(r -> "{{ select * where {BIND(<" + r + "> as " + subjectVariable + "). " + subjectVariable + " ?b1 " + objectVariable + " .}}}")
.reduce((a, b) -> a + " UNION " + b)
.get();
Expand All @@ -82,7 +76,7 @@ public String getQuery(String subjectVariable, String objectVariable, RdfsSubCla

@Override
public PlanNode getTargetFilter(NotifyingSailConnection shaclSailConnection, PlanNode parent) {
return new LoggingNode(new SetFilterNode(targetNodeList, parent, 0, true), "targetNode filter");
return new LoggingNode(new SetFilterNode(targetNodeSet, parent, 0, true), "targetNode filter");
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*******************************************************************************
* Copyright (c) 2018 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/

package org.eclipse.rdf4j.sail.shacl;

import org.eclipse.rdf4j.model.vocabulary.RDF4J;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*******************************************************************************
* Copyright (c) 2018 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/

package org.eclipse.rdf4j.sail.shacl;

import org.eclipse.rdf4j.common.iteration.Iterations;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*******************************************************************************
* Copyright (c) 2018 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/
package org.eclipse.rdf4j.sail.shacl.planNodes;

import org.apache.commons.lang.StringEscapeUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*******************************************************************************
* Copyright (c) 2018 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/
package org.eclipse.rdf4j.sail.shacl.planNodes;

import org.apache.commons.lang.StringEscapeUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*******************************************************************************
* Copyright (c) 2018 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/
package org.eclipse.rdf4j.sail.shacl.planNodes;

public enum IteratorData {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*******************************************************************************
* Copyright (c) 2018 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/

package org.eclipse.rdf4j.sail.shacl.planNodes;

public interface MultiStreamPlanNode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*******************************************************************************
* Copyright (c) 2018 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/

package org.eclipse.rdf4j.sail.shacl.planNodes;

public interface PushablePlanNode extends PlanNode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*******************************************************************************
* Copyright (c) 2018 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/

package org.eclipse.rdf4j.sail.shacl.planNodes;

import org.apache.commons.lang.StringEscapeUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*******************************************************************************
* Copyright (c) 2018 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/

package org.eclipse.rdf4j.sail.shacl.planNodes;

import org.apache.commons.lang.StringEscapeUtils;
Expand Down

0 comments on commit 19e69a7

Please sign in to comment.