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

Commit

Permalink
Merge pull request #176 from eclipse/issues/1306_targetNode
Browse files Browse the repository at this point in the history
sh:targetNode closes eclipse-rdf4j/rdf4j#1306
  • Loading branch information
hmottestad authored Mar 1, 2019
2 parents c82d17d + c571c9e commit 3167d96
Show file tree
Hide file tree
Showing 87 changed files with 1,287 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ public PlanNode getPlan(ShaclSailConnection shaclSailConnection, NodeShape nodeS
PlanNode innerJoin = new LoggingNode(innerJoinHolder.getJoined(BufferedPlanNode.class), "");
PlanNode discardedRight = new LoggingNode(innerJoinHolder.getDiscardedRight(BufferedPlanNode.class), "");

if (nodeShape instanceof TargetClass) {
PlanNode typeFilterPlan = new LoggingNode(((TargetClass) nodeShape).getTypeFilterPlan(shaclSailConnection, discardedRight), "");
PlanNode typeFilterPlan = new LoggingNode(nodeShape.getTargetFilter(shaclSailConnection, discardedRight), "");

innerJoin = new LoggingNode(new Unique(new UnionNode(innerJoin, typeFilterPlan)), "");
innerJoin = new LoggingNode(new Unique(new UnionNode(innerJoin, typeFilterPlan)), "");

}

// also add anything that matches the path from the previousConnection, eg. if you add ":peter a foaf:Person", and ":peter foaf:knows :steve" is already added
PlanNode bulkedExternalLeftOuter = new LoggingNode(new BulkedExternalLeftOuterJoin(new LoggingNode(nodeShape.getPlanAddedStatements(shaclSailConnection, nodeShape), ""), shaclSailConnection, path.getQuery("?a", "?c", null), true), "");
Expand All @@ -105,7 +103,7 @@ public PlanNode getPlan(ShaclSailConnection shaclSailConnection, NodeShape nodeS
// filter by type against the base sail
PlanNode invalidTuplesDueToDataAddedThatMatchesTargetOrPath = new LoggingNode(new ExternalTypeFilterNode(shaclSailConnection, classResource, addedStatementsTypeFilter, 1, false), "");

if(shaclSailConnection.stats.hasRemoved()) {
if (shaclSailConnection.stats.hasRemoved()) {

// Handle when a type statement has been removed, first get all removed type statements that match the classResource for this shape
PlanNode removedTypeStatements = new LoggingNode(new Select(shaclSailConnection.getRemovedStatements(), "?a a <" + classResource + ">"), "removedTypeStatements");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ public PlanNode getPlan(ShaclSailConnection shaclSailConnection, NodeShape nodeS

PlanNode planAddedStatements1 = new LoggingNode(super.getPlanAddedStatements(shaclSailConnection, nodeShape), "");


if (nodeShape instanceof TargetClass) {
planAddedStatements1 = new LoggingNode(((TargetClass) nodeShape).getTypeFilterPlan(shaclSailConnection, planAddedStatements1), "");
}
planAddedStatements1 = new LoggingNode(nodeShape.getTargetFilter(shaclSailConnection, planAddedStatements1), "");

PlanNode mergeNode = new LoggingNode(new UnionNode(planAddedStatements, planAddedStatements1), "");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public String toString() {
@Override
public PlanNode getPlan(ShaclSailConnection shaclSailConnection, NodeShape nodeShape, boolean printPlans, PlanNode overrideTargetNode) {

if(overrideTargetNode != null){
if (overrideTargetNode != null) {
PlanNode allStatements = new LoggingNode(new BulkedExternalLeftOuterJoin(overrideTargetNode, shaclSailConnection, path.getQuery("?a", "?c", null), false), "");
PlanNode groupBy = new LoggingNode(new GroupByCount(allStatements), "");

Expand All @@ -75,11 +75,7 @@ public PlanNode getPlan(ShaclSailConnection shaclSailConnection, NodeShape nodeS
if (!optimizeWhenNoStatementsRemoved || shaclSailConnection.stats.hasRemoved()) {
PlanNode planRemovedStatements = new LoggingNode(new TrimTuple(new LoggingNode(super.getPlanRemovedStatements(shaclSailConnection, nodeShape), ""), 0, 1), "");

PlanNode filteredPlanRemovedStatements = planRemovedStatements;

if (nodeShape instanceof TargetClass) {
filteredPlanRemovedStatements = new LoggingNode(((TargetClass) nodeShape).getTypeFilterPlan(shaclSailConnection, planRemovedStatements), "");
}
PlanNode filteredPlanRemovedStatements = new LoggingNode(nodeShape.getTargetFilter(shaclSailConnection, planRemovedStatements), "");

PlanNode planAddedStatements = new LoggingNode(nodeShape.getPlanAddedStatements(shaclSailConnection, nodeShape), "");

Expand All @@ -90,9 +86,8 @@ public PlanNode getPlan(ShaclSailConnection shaclSailConnection, NodeShape nodeS

PlanNode planAddedStatements1 = super.getPlanAddedStatements(shaclSailConnection, nodeShape);

if (nodeShape instanceof TargetClass) {
planAddedStatements1 = new LoggingNode(((TargetClass) nodeShape).getTypeFilterPlan(shaclSailConnection, planAddedStatements1), "");
}
planAddedStatements1 = new LoggingNode((nodeShape).getTargetFilter(shaclSailConnection, planAddedStatements1), "");

topNode = new LoggingNode(new UnionNode(unique, planAddedStatements1), "");


Expand All @@ -106,9 +101,8 @@ public PlanNode getPlan(ShaclSailConnection shaclSailConnection, NodeShape nodeS

PlanNode addedByPath = new LoggingNode(getPlanAddedStatements(shaclSailConnection, nodeShape), "");

if (nodeShape instanceof TargetClass) {
addedByPath = new LoggingNode(((TargetClass) nodeShape).getTypeFilterPlan(shaclSailConnection, addedByPath), "");
}
addedByPath = new LoggingNode((nodeShape).getTargetFilter(shaclSailConnection, addedByPath), "");

topNode = new LoggingNode(new UnionNode(planAddedForShape, addedByPath), "");

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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.ShaclSail;
Expand Down Expand Up @@ -95,6 +96,8 @@ public static List<NodeShape> getShapes(SailRepositoryConnection connection, Sha
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);
} else {
if(sail.isUndefinedTargetValidatesAllSubjects()) {
return new NodeShape(shapeId, connection); // target class nodeShapes are the only supported nodeShapes
Expand All @@ -110,10 +113,18 @@ 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();
}

public PlanNode getTargetFilter(NotifyingSailConnection shaclSailConnection, PlanNode parent) {
return parent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ static public PlanNode getGenericSingleObjectPlan(ShaclSailConnection shaclSailC

PlanNode discardedRight = innerJoin.getDiscardedRight(BufferedPlanNode.class);

if (nodeShape instanceof TargetClass) {
PlanNode typeFilterPlan = new LoggingNode(((TargetClass) nodeShape).getTypeFilterPlan(shaclSailConnection, discardedRight), "");
PlanNode typeFilterPlan = new LoggingNode(nodeShape.getTargetFilter(shaclSailConnection, discardedRight), "");

top = new LoggingNode(new UnionNode(top, typeFilterPlan), "");
}


PlanNode bulkedExternalInnerJoin = new LoggingNode(new BulkedExternalInnerJoin(new LoggingNode(nodeShape.getPlanAddedStatements(shaclSailConnection, nodeShape), ""), shaclSailConnection, pathPropertyShape.path.getQuery("?a", "?c", null), true), "");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public PlanNode getPlanAddedStatements(ShaclSailConnection shaclSailConnection,

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

@Override
Expand All @@ -83,7 +83,8 @@ public String getQuery(String subjectVariable, String objectVariable, RdfsSubCla
return "BIND(rdf:type as ?b1) \n BIND(<" + targetClass + "> as "+objectVariable+") \n "+subjectVariable+" ?b1 "+objectVariable+". \n";
}

public PlanNode getTypeFilterPlan(NotifyingSailConnection shaclSailConnection, PlanNode parent) {
@Override
public PlanNode getTargetFilter(NotifyingSailConnection shaclSailConnection, PlanNode parent) {
return new ExternalTypeFilterNode(shaclSailConnection, targetClass, parent, 0, true);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*******************************************************************************
* 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.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.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* The AST (Abstract Syntax Tree) node
*
* @author Heshan Jayasinghe
*/
public class TargetNode extends NodeShape {

Set<Value> targetNodeList;

TargetNode(Resource id, SailRepositoryConnection connection) {
super(id, connection);

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

}

@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);
}

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

}

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

@Override
public boolean requiresEvaluation(SailConnection addedStatements, SailConnection removedStatements) {
return true;
}

@Override
public String getQuery(String subjectVariable, String objectVariable, RdfsSubClassOfReasoner rdfsSubClassOfReasoner) {

return targetNodeList.stream()
.map(r -> "{{ select * where {BIND(<" + r + "> as " + subjectVariable + "). " + subjectVariable + " ?b1 " + objectVariable + " .}}}")
.reduce((a, b) -> a + " UNION " + b)
.get();

}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ public static List<IRI> getSupportedShaclPredicates() {
SHACL.MIN_INCLUSIVE,
SHACL.MAX_EXCLUSIVE,
SHACL.MAX_INCLUSIVE,
SHACL.CLASS
SHACL.CLASS,
SHACL.TARGET_NODE
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.eclipse.rdf4j.sail.shacl.planNodes;

import org.apache.commons.lang.StringEscapeUtils;
import org.eclipse.rdf4j.common.iteration.CloseableIteration;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.sail.SailException;

import java.util.Arrays;
import java.util.Set;

public class SetFilterNode implements PlanNode {

private Set<Value> targetNodeList;
private PlanNode parent;
private int index;
private boolean returnValid;
private boolean printed;

public SetFilterNode(Set<Value> targetNodeList, PlanNode parent, int index, boolean returnValid) {
this.targetNodeList = targetNodeList;
this.parent = parent;
this.index = index;
this.returnValid = returnValid;
}

@Override
public CloseableIteration<Tuple, SailException> iterator() {
return new CloseableIteration<Tuple, SailException>() {

CloseableIteration<Tuple, SailException> iterator = parent.iterator();

Tuple next;

private void calulateNext(){
while(next == null && iterator.hasNext()){
Tuple temp = iterator.next();
boolean contains = targetNodeList.contains(temp.getlist().get(index));
if(returnValid && contains){
next = temp;
}else if (!returnValid && !contains){
next = temp;
}
}
}

@Override
public void close() throws SailException {
iterator.close();
}

@Override
public boolean hasNext() throws SailException {
calulateNext();
return next != null;
}

@Override
public Tuple next() throws SailException {
calulateNext();

Tuple temp = next;
next = null;

return temp;
}

@Override
public void remove() throws SailException {

}
};
}

@Override
public int depth() {
return parent.depth() + 1;
}

@Override
public void getPlanAsGraphvizDot(StringBuilder stringBuilder) {
if (printed) {
return;
}
printed = true;
stringBuilder.append(getId() + " [label=\"" + StringEscapeUtils.escapeJava(this.toString()) + "\"];").append("\n");
stringBuilder.append(parent.getId() + " -> " + getId()).append("\n");
parent.getPlanAsGraphvizDot(stringBuilder);
}

@Override
public String getId() {
return System.identityHashCode(this) + "";
}


@Override
public IteratorData getIteratorDataType() {
return parent.getIteratorDataType();
}

@Override
public String toString() {
return "SetFilterNode{" +
"targetNodeList=" + Arrays.toString(targetNodeList.toArray()) +
", index=" + index +
", returnValid=" + returnValid +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ abstract public class AbstractShaclTest {
"test-cases/languageIn/simple",
"test-cases/nodeKind/simple",
"test-cases/minCount/simple",
"test-cases/minCount/targetNode",
"test-cases/maxCount/simple",
"test-cases/maxCount/targetNode",
"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/datatypeTargetNode",
"test-cases/or/minCountMaxCount",
"test-cases/or/maxCount",
"test-cases/or/minCount",
Expand All @@ -68,9 +71,12 @@ abstract public class AbstractShaclTest {
"test-cases/implicitTargetClass/simple",
"test-cases/class/simple",
"test-cases/class/subclass",
"test-cases/class/targetNode",
"test-cases/or/class",
"test-cases/or/datatype2",
"test-cases/or/minCountDifferentPath"
"test-cases/or/minCountDifferentPath",
"test-cases/datatype/targetNode"

);

final String testCasePath;
Expand Down
Loading

0 comments on commit 3167d96

Please sign in to comment.