This repository has been archived by the owner on Feb 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from eclipse/issues/1306_targetNode
sh:targetNode closes eclipse-rdf4j/rdf4j#1306
- Loading branch information
Showing
87 changed files
with
1,287 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
shacl/src/main/java/org/eclipse/rdf4j/sail/shacl/AST/TargetNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
shacl/src/main/java/org/eclipse/rdf4j/sail/shacl/planNodes/SetFilterNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.