You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2014/05/23 19:18:24 UTC
svn commit: r1597133 - in /jena/trunk/jena-arq/src:
main/java/com/hp/hpl/jena/sparql/path/
main/java/com/hp/hpl/jena/sparql/path/eval/
main/java/com/hp/hpl/jena/sparql/pfunction/
main/java/com/hp/hpl/jena/sparql/procedure/ test/java/com/hp/hpl/jena/spa...
Author: andy
Date: Fri May 23 17:18:24 2014
New Revision: 1597133
URL: http://svn.apache.org/r1597133
Log:
Make sure all path evaluation go through a single point to access the graph data.
General reformat and tidy up.
Modified:
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/PathLib.java
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngine.java
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngine1.java
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngineN.java
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngineSPARQL.java
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEval.java
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEvaluator.java
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/pfunction/PropertyFunctionRegistry.java
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/procedure/ProcEval.java
jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/path/TestPath.java
jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/path/TestPath2.java
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/PathLib.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/PathLib.java?rev=1597133&r1=1597132&r2=1597133&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/PathLib.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/PathLib.java Fri May 23 17:18:24 2014
@@ -155,12 +155,12 @@ public class PathLib
if ( Var.isVar(s) )
{
// Var subject, concrete object - do backwards.
- iter = PathEval.evalReverse(graph, o, path) ;
+ iter = PathEval.evalReverse(graph, o, path, execCxt.getContext()) ;
endNode = s ;
}
else
{
- iter = PathEval.eval(graph, s, path) ;
+ iter = PathEval.eval(graph, s, path, execCxt.getContext()) ;
endNode = o ;
}
return _execTriplePath(binding, iter, endNode, execCxt) ;
@@ -183,14 +183,14 @@ public class PathLib
Node n = iter.next() ;
results.add(BindingFactory.binding(binding, var, n)) ;
}
- return new QueryIterPlainWrapper(results.iterator()) ;
+ return new QueryIterPlainWrapper(results.iterator(), execCxt) ;
}
// Subject and object are nodes.
private static QueryIterator groundedPath(Binding binding, Graph graph, Node subject, Path path, Node object,
ExecutionContext execCxt)
{
- Iterator<Node> iter = PathEval.eval(graph, subject, path) ;
+ Iterator<Node> iter = PathEval.eval(graph, subject, path, execCxt.getContext()) ;
// Now count the number of matches.
int count = 0 ;
@@ -201,7 +201,7 @@ public class PathLib
count++ ;
}
- return new QueryIterYieldN(count, binding) ;
+ return new QueryIterYieldN(count, binding, execCxt) ;
}
// Brute force evaluation of a TriplePath where neither subject nor object are bound
@@ -215,7 +215,7 @@ public class PathLib
{
Node n = iter.next() ;
Binding b2 = BindingFactory.binding(binding, sVar, n) ;
- Iterator<Node> pathIter = PathEval.eval(graph, n, path) ;
+ Iterator<Node> pathIter = PathEval.eval(graph, n, path, execCxt.getContext()) ;
QueryIterator qIter = _execTriplePath(b2, pathIter, oVar, execCxt) ;
qIterCat.add(qIter) ;
}
@@ -233,7 +233,7 @@ public class PathLib
{
Node n = iter.next() ;
Binding b2 = BindingFactory.binding(binding, var, n) ;
- int x = existsPath(graph, n, path, n) ;
+ int x = existsPath(graph, n, path, n, execCxt) ;
if ( x > 0 )
{
QueryIterator qIter = new QueryIterYieldN(x, b2, execCxt) ;
@@ -243,11 +243,11 @@ public class PathLib
return qIterCat ;
}
- private static int existsPath(Graph graph, Node subject, Path path, final Node object)
+ private static int existsPath(Graph graph, Node subject, Path path, final Node object, ExecutionContext execCxt)
{
if ( ! subject.isConcrete() || !object.isConcrete() )
throw new ARQInternalErrorException("Non concrete node for existsPath evaluation") ;
- Iterator<Node> iter = PathEval.eval(graph, subject, path) ;
+ Iterator<Node> iter = PathEval.eval(graph, subject, path, execCxt.getContext()) ;
Filter<Node> filter = new Filter<Node>() { @Override public boolean accept(Node node) { return Lib.equal(node, object) ; } } ;
// See if we got to the node we're interested in finishing at.
iter = Iter.filter(iter, filter) ;
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngine.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngine.java?rev=1597133&r1=1597132&r2=1597133&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngine.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngine.java Fri May 23 17:18:24 2014
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package com.hp.hpl.jena.sparql.path.eval;
+package com.hp.hpl.jena.sparql.path.eval ;
import java.util.Collection ;
import java.util.Iterator ;
@@ -28,46 +28,58 @@ import org.apache.jena.atlas.iterator.Tr
import com.hp.hpl.jena.graph.Graph ;
import com.hp.hpl.jena.graph.Node ;
import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.core.Var ;
+import com.hp.hpl.jena.sparql.engine.binding.Binding ;
+import com.hp.hpl.jena.sparql.engine.binding.BindingFactory ;
import com.hp.hpl.jena.sparql.path.P_NegPropSet ;
import com.hp.hpl.jena.sparql.path.Path ;
import com.hp.hpl.jena.sparql.path.eval.PathEvaluator.FilterExclude ;
+import com.hp.hpl.jena.sparql.util.Context ;
-abstract public class PathEngine {
+abstract public class PathEngine
+{
- protected final Iter<Node> eval(Graph graph, Path path, Node node)
- {
+ private final Graph graph ;
+ private final Context context ;
+
+ protected PathEngine(Graph graph, Context context) {
+ this.graph = graph ;
+ this.context = context ;
+ }
+
+ protected final Iter<Node> eval(Path path, Node node) {
return PathEval.eval$(graph, node, path, this) ;
}
-
- protected final void eval(Graph graph, Path path, Node node, Collection<Node> output)
- {
+
+ protected final void eval(Path path, Node node, Collection<Node> output) {
PathEval.eval$(graph, node, path, this, output) ;
}
-
+
+ // protected final void eval(Path path, Node node, Collection<Node> output)
+
protected abstract void flipDirection() ;
+
protected abstract boolean direction() ;
-
+
protected abstract Collection<Node> collector() ;
-
- // protected abstract void doZero(Path pathStep, Node node, Collection<Node> output) ;
- // protected abstract void doOne(Path pathStep, Node node, Collection<Node> output) ;
-
+
+ // protected abstract void doZero(Path pathStep, Node node, Collection<Node>
+ // output) ;
+ // protected abstract void doOne(Path pathStep, Node node, Collection<Node>
+ // output) ;
+
// --- Where we touch the graph
// Because it SP? or ?PO, no duplicates occur, so works for both strategies.
- protected final Iterator<Node> doOne(Graph graph, Node node, Node property)
- {
+ protected final Iterator<Node> doOne(Node node, Node property) {
Iterator<Node> iter2 = null ;
- if ( direction() )
- {
- Iter<Triple> iter1 = Iter.iter(graph.find(node, property, Node.ANY)) ;
+ if ( direction() ) {
+ Iter<Triple> iter1 = Iter.iter(graphFind(node, property, Node.ANY)) ;
iter2 = iter1.map(PathEngine.selectObject) ;
- }
- else
- {
- Iter<Triple> iter1 = Iter.iter(graph.find(Node.ANY, property, node)) ;
+ } else {
+ Iter<Triple> iter1 = Iter.iter(graphFind(Node.ANY, property, node)) ;
iter2 = iter1.map(PathEngine.selectSubject) ;
}
-
+
return iter2 ;
}
@@ -87,81 +99,93 @@ abstract public class PathEngine {
protected abstract void doNegatedPropertySet(P_NegPropSet pathNotOneOf, Node node, Collection<Node> output) ;
// path{*} : default implementation
- protected void doZeroOrMoreN(Path pathStep, Node node, Collection<Node> output)
- { doZeroOrMore(pathStep, node, output) ; }
+ protected void doZeroOrMoreN(Path pathStep, Node node, Collection<Node> output) {
+ doZeroOrMore(pathStep, node, output) ;
+ }
// path{+} : default implementation
- protected void doOneOrMoreN(Path pathStep, Node node, Collection<Node> output)
- { doOneOrMore(pathStep, node, output) ; }
+ protected void doOneOrMoreN(Path pathStep, Node node, Collection<Node> output) {
+ doOneOrMore(pathStep, node, output) ;
+ }
protected abstract void doZero(Path path, Node node, Collection<Node> output) ;
-
+
// {N,M} and variations
-
+
protected abstract void doFixedLengthPath(Path pathStep, Node node, long fixedLength, Collection<Node> output) ;
protected abstract void doMultiLengthPath(Path pathStep, Node node, long min, long max, Collection<Node> output) ;
-
- protected final void fill(Iterator<Node> iter, Collection<Node> output)
- {
- for ( ; iter.hasNext() ; )
+
+ protected final void fill(Iterator<Node> iter, Collection<Node> output) {
+ for (; iter.hasNext();)
output.add(iter.next()) ;
}
-
- protected static long dec(long x) { return (x<=0) ? x : x-1 ; }
-
- protected static Transform<Triple, Node> selectSubject = new Transform<Triple, Node>() {
+
+ protected static long dec(long x) {
+ return (x <= 0) ? x : x - 1 ;
+ }
+
+ protected static Transform<Triple, Node> selectSubject = new Transform<Triple, Node>() {
@Override
- public Node convert(Triple triple)
- {
- return triple.getSubject() ;
- }
+ public Node convert(Triple triple) { return triple.getSubject() ; }
} ;
+
protected static Transform<Triple, Node> selectPredicate = new Transform<Triple, Node>() {
@Override
- public Node convert(Triple triple)
- {
- return triple.getPredicate() ;
- }
+ public Node convert(Triple triple) { return triple.getPredicate() ; }
} ;
- protected static Transform<Triple, Node> selectObject = new Transform<Triple, Node>() {
+
+ protected static Transform<Triple, Node> selectObject = new Transform<Triple, Node>() {
@Override
- public Node convert(Triple triple)
- {
- return triple.getObject() ;
- }
+ public Node convert(Triple triple) { return triple.getObject() ; }
} ;
-
- protected static Iterator<Node> stepExcludeForwards(Graph graph , Node node , List<Node> excludedNodes )
- {
- Iter<Triple> iter1 = forwardLinks(graph, node, excludedNodes) ;
+ protected Iterator<Node> stepExcludeForwards(Node node, List<Node> excludedNodes) {
+ Iter<Triple> iter1 = forwardLinks(node, excludedNodes) ;
Iter<Node> r1 = iter1.map(selectObject) ;
return r1 ;
}
- protected static Iterator<Node> stepExcludeBackwards(Graph graph , Node node , List<Node> excludedNodes )
- {
- Iter<Triple> iter1 = backwardLinks(graph, node, excludedNodes) ;
+ protected Iterator<Node> stepExcludeBackwards(Node node, List<Node> excludedNodes) {
+ Iter<Triple> iter1 = backwardLinks(node, excludedNodes) ;
Iter<Node> r1 = iter1.map(selectSubject) ;
return r1 ;
}
- protected static Iter<Triple> forwardLinks(Graph graph, Node x, Collection<Node> excludeProperties)
- {
- Iter<Triple> iter1 = Iter.iter(graph.find(x, Node.ANY, Node.ANY)) ;
+ protected Iter<Triple> forwardLinks(Node x, Collection<Node> excludeProperties) {
+ Iter<Triple> iter1 = Iter.iter(graphFind(x, Node.ANY, Node.ANY)) ;
if ( excludeProperties != null )
iter1 = iter1.filter(new FilterExclude(excludeProperties)) ;
return iter1 ;
}
- protected static Iter<Triple> backwardLinks(Graph graph, Node x, Collection<Node> excludeProperties)
- {
- Iter<Triple> iter1 = Iter.iter(graph.find(Node.ANY, Node.ANY, x)) ;
+ protected Iter<Triple> backwardLinks(Node x, Collection<Node> excludeProperties) {
+ Iter<Triple> iter1 = Iter.iter(graphFind(Node.ANY, Node.ANY, x)) ;
if ( excludeProperties != null )
iter1 = iter1.filter(new FilterExclude(excludeProperties)) ;
return iter1 ;
}
-}
-
+ protected Iterator<Triple> graphFind(Node s, Node p, Node o) {
+ return graphFind(graph, s, p, o) ;
+ }
+
+ static Binding binding = BindingFactory.binding() ;
+
+ private/* package */static Iterator<Triple> graphFind(Graph graph, Node s, Node p, Node o) {
+ // This is the only place this is called.
+ // It means we can add property functions here.s
+ return graph.find(s, p, o) ;
+ }
+
+ private static Node arg(Node x, String name) {
+ if ( x == null || Node.ANY.equals(x) ) { return Var.alloc(name) ; }
+ return x ;
+ }
+
+ private static Node value(Node x, Binding b) {
+ if ( !Var.isVar(x) )
+ return x ;
+ return b.get(Var.alloc(x)) ;
+ }
+}
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngine1.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngine1.java?rev=1597133&r1=1597132&r2=1597133&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngine1.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngine1.java Fri May 23 17:18:24 2014
@@ -16,9 +16,8 @@
* limitations under the License.
*/
-package com.hp.hpl.jena.sparql.path.eval;
+package com.hp.hpl.jena.sparql.path.eval ;
-//import java.util.ArrayList ;
import java.util.* ;
import org.apache.jena.atlas.iterator.Iter ;
@@ -34,72 +33,69 @@ import com.hp.hpl.jena.sparql.path.Path
* This class exists for experimentation.
* It is written to get the right results - not necessarily with maximum efficiency.
*/
+
final class PathEngine1 extends PathEngine
{
- private final Graph graph ;
private boolean forwardMode ;
- public PathEngine1(Graph graph, boolean forward)
- {
- this.graph = graph ;
+ public PathEngine1(Graph graph, boolean forward) {
+ super(graph, null) ;
this.forwardMode = forward ;
}
-
+
// Choose the underlying impl - different choice for debugging.
@Override
- protected Collection<Node> collector()
- { return new ArrayList<Node>() ; }
- //{ return new HashSet<Node>() ; }
-
+ protected Collection<Node> collector() {
+ return new ArrayList<Node>() ;
+ // { return new HashSet<Node>() ; }
+ }
+
+
@Override
- protected void flipDirection()
- { forwardMode = ! forwardMode ; }
+ protected void flipDirection() {
+ forwardMode = !forwardMode ;
+ }
@Override
- protected boolean direction()
- { return forwardMode ; }
+ protected boolean direction() {
+ return forwardMode ;
+ }
@Override
- protected void doAlt(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output)
- {
+ protected void doAlt(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output) {
// Must be duplicate supressing.
Collection<Node> nodes = new HashSet<Node>() ;
// Insert directly.
- eval(graph, pathStepLeft, node, nodes) ;
+ eval(pathStepLeft, node, nodes) ;
// Need to reduce/check other side.
- eval(graph, pathStepRight, node, nodes) ;
+ eval(pathStepRight, node, nodes) ;
output.addAll(nodes) ;
}
@Override
- protected void doSeq(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output)
- {
+ protected void doSeq(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output) {
Path part1 = forwardMode ? pathStepLeft : pathStepRight ;
Path part2 = forwardMode ? pathStepRight : pathStepLeft ;
-
+
Collection<Node> nodes = collector() ;
- eval(graph, part1, node, nodes) ;
+ eval(part1, node, nodes) ;
Collection<Node> nodes2 = new HashSet<Node>() ;
- for ( Node n : nodes )
- eval(graph, part2, n, nodes2) ;
+ for (Node n : nodes)
+ eval(part2, n, nodes2) ;
output.addAll(nodes2) ;
}
// Can use .addAll if collector is set-like.
- private static void fillUnique(Iterator<Node> nodes, Collection<Node> acc)
- {
- for ( ; nodes.hasNext() ; )
- {
+ private static void fillUnique(Iterator<Node> nodes, Collection<Node> acc) {
+ for (; nodes.hasNext();) {
Node n = nodes.next() ;
if ( !acc.contains(n) )
acc.add(n) ;
}
}
-
@Override
- protected void doMultiLengthPath(Path pathStep, Node node, long min1, long max1, Collection<Node> output)
- {
+ protected void doMultiLengthPath(Path pathStep, Node node, long min1, long max1, Collection<Node> output) {
// This algrothim can be used for counting {n,m}
// abstract ALP(=>rename?) , doFixedLength
@@ -115,22 +111,19 @@ final class PathEngine1 extends PathEngi
else
collectStartPoints.add(node) ;
- //System.out.println("Start points: "+collectStartPoints) ;
+ // System.out.println("Start points: "+collectStartPoints) ;
+
+ // {N,M} = {N} then {0,M-N}
+ int length = (int)(max1 - min1) ;
- // {N,M} = {N} then {0,M-N}
- int length = (int)(max1-min1) ;
-
Collection<Node> visited = collector() ;
-
- for ( Node n : collectStartPoints )
+
+ for (Node n : collectStartPoints)
doMultiLengthPath(pathStep, n, length, visited, output) ;
}
// {0,length}
- protected void doMultiLengthPath(Path pathStep, Node node, long length,
- Collection<Node> visited,
- Collection<Node> output)
- {
+ protected void doMultiLengthPath(Path pathStep, Node node, long length, Collection<Node> visited, Collection<Node> output) {
if ( visited.contains(node) )
return ;
visited.add(node) ;
@@ -138,128 +131,115 @@ final class PathEngine1 extends PathEngi
if ( length == 0 )
return ;
-
+
// One step.
- Iterator<Node> iter = eval(graph, pathStep, node) ;
- for ( ; iter.hasNext() ; )
- {
+ Iterator<Node> iter = eval(pathStep, node) ;
+ for (; iter.hasNext();) {
Node m = iter.next() ;
if ( visited.contains(m) )
continue ;
- doMultiLengthPath(pathStep, m, length-1, visited, output) ;
+ doMultiLengthPath(pathStep, m, length - 1, visited, output) ;
}
}
-
-// // Do {0,length}
-// private void doFixedLengthPath(Path path, Node node, int length, Collection<Node>visited)
-// {
+
+// // Do {0,length}
+// private void doFixedLengthPath(Path path, Node node, int length, Collection<Node> visited) {
// System.out.printf("doModPath (%d) %s\n", length, node) ;
-// ALP1(graph, forwardMode, 0, length, node, path, visited) ;
+// ALP1(forwardMode, 0, length, node, path, visited) ;
// }
-
@Override
- protected void doFixedLengthPath(Path pathStep, Node node, long fixedLength, Collection<Node> output)
- {
+ protected void doFixedLengthPath(Path pathStep, Node node, long fixedLength, Collection<Node> output) {
// Special for small?
-// if ( fixedLength < 3 )
-// {}
+ // if ( fixedLength < 3 )
+ // {}
Collection<Node> visited = collector() ;
-
- if ( fixedLength == 0 )
- {
+
+ if ( fixedLength == 0 ) {
doZero(pathStep, node, output) ;
return ;
}
- if ( fixedLength == 1 )
- {
- Iter<Node> iter = eval(graph, pathStep, node) ;
- for ( Node n : iter )
- {
+ if ( fixedLength == 1 ) {
+ Iter<Node> iter = eval(pathStep, node) ;
+ for (Node n : iter) {
if ( !output.contains(n) )
output.add(n) ;
}
return ;
}
// Loop, not recurse.
- Iter<Node> iter = eval(graph, pathStep, node) ;
- for ( Node n : iter )
- doFixedLengthPath(pathStep, n, fixedLength-1, output) ;
+ Iter<Node> iter = eval(pathStep, node) ;
+ for (Node n : iter)
+ doFixedLengthPath(pathStep, n, fixedLength - 1, output) ;
return ;
}
-
@Override
- protected void doZeroOrMore(Path path, Node node, Collection<Node> output)
- {
+ protected void doZeroOrMore(Path path, Node node, Collection<Node> output) {
// Reuse "output"
- Collection<Node> visited = new LinkedList<Node>() ; //new HashSet<Node>() ;
- ALP1(graph, forwardMode, 0, -1, node, path, visited) ;
+ Collection<Node> visited = new LinkedList<Node>() ; // new
+ // HashSet<Node>() ;
+ ALP1(forwardMode, 0, -1, node, path, visited) ;
output.addAll(visited) ;
}
@Override
- protected void doOneOrMore(Path path, Node node, Collection<Node> output)
- {
+ protected void doOneOrMore(Path path, Node node, Collection<Node> output) {
// Reuse "output"
- Collection<Node> visited = new LinkedList<Node>() ; // new HashSet<Node>() ;
+ Collection<Node> visited = new LinkedList<Node>() ; // new
+ // HashSet<Node>() ;
// Do one step without including.
- Iter<Node> iter1 = eval(graph, path, node) ;
- for ( ; iter1.hasNext() ; )
- {
- Node n1 = iter1.next();
- ALP1(graph, forwardMode, 0, -1, n1, path, visited) ;
+ Iter<Node> iter1 = eval(path, node) ;
+ for (; iter1.hasNext();) {
+ Node n1 = iter1.next() ;
+ ALP1(forwardMode, 0, -1, n1, path, visited) ;
}
output.addAll(visited) ;
}
- private void ALP1(Graph graph, boolean forwardMode, int stepCount, int maxStepCount, Node node, Path path, Collection<Node> visited)
- {
- if ( false ) System.out.printf("ALP1 node=%s\n visited=%s\n output=%s\n", node, visited) ;
- if ( maxStepCount >=0 && stepCount > maxStepCount ) return ;
- if ( visited.contains(node) ) return ;
+ private void ALP1(boolean forwardMode, int stepCount, int maxStepCount, Node node, Path path, Collection<Node> visited) {
+ if ( false )
+ System.out.printf("ALP1 node=%s\n visited=%s\n output=%s\n", node, visited) ;
+ if ( maxStepCount >= 0 && stepCount > maxStepCount )
+ return ;
+ if ( visited.contains(node) )
+ return ;
- if ( ! visited.add(node) )
+ if ( !visited.add(node) )
return ;
- Iter<Node> iter1 = eval(graph, path, node) ;
+ Iter<Node> iter1 = eval(path, node) ;
// For each step, add to results and recurse.
- for ( ; iter1.hasNext() ; )
- {
- Node n1 = iter1.next();
- ALP1(graph, forwardMode, stepCount+1, maxStepCount, n1, path, visited) ;
+ for (; iter1.hasNext();) {
+ Node n1 = iter1.next() ;
+ ALP1(forwardMode, stepCount + 1, maxStepCount, n1, path, visited) ;
}
- // Different from ALP-counting.
+ // Different from ALP-counting.
// visited.remove(node) ;
}
@Override
- protected void doNegatedPropertySet(P_NegPropSet pathNotOneOf, Node node, Collection<Node> output)
- {
+ protected void doNegatedPropertySet(P_NegPropSet pathNotOneOf, Node node, Collection<Node> output) {
// X !(:a|:b|^:c|^:d) Y = { X !(:a|:b) Y } UNION { Y !(:c|:d) X }
- if ( pathNotOneOf.getFwdNodes().size() > 0 )
- {
- Iterator<Node> nodes1 = stepExcludeForwards(graph, node, pathNotOneOf.getFwdNodes()) ;
+ if ( pathNotOneOf.getFwdNodes().size() > 0 ) {
+ Iterator<Node> nodes1 = stepExcludeForwards(node, pathNotOneOf.getFwdNodes()) ;
fillUnique(nodes1, output) ;
}
- if ( pathNotOneOf.getBwdNodes().size() > 0 )
- {
- Iterator<Node> nodes2 = stepExcludeBackwards(graph, node, pathNotOneOf.getBwdNodes()) ;
+ if ( pathNotOneOf.getBwdNodes().size() > 0 ) {
+ Iterator<Node> nodes2 = stepExcludeBackwards(node, pathNotOneOf.getBwdNodes()) ;
fillUnique(nodes2, output) ;
}
}
@Override
- protected void doZeroOrOne(Path pathStep, Node node, Collection<Node> output)
- {
- eval(graph, pathStep, node, output) ;
- if ( ! output.contains(node) )
+ protected void doZeroOrOne(Path pathStep, Node node, Collection<Node> output) {
+ eval(pathStep, node, output) ;
+ if ( !output.contains(node) )
output.add(node) ;
}
@Override
- protected void doZero(Path path, Node node, Collection<Node> output)
- {
+ protected void doZero(Path path, Node node, Collection<Node> output) {
if ( !output.contains(node) )
output.add(node) ;
}
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngineN.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngineN.java?rev=1597133&r1=1597132&r2=1597133&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngineN.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngineN.java Fri May 23 17:18:24 2014
@@ -16,13 +16,9 @@
* limitations under the License.
*/
-package com.hp.hpl.jena.sparql.path.eval;
+package com.hp.hpl.jena.sparql.path.eval ;
-import java.util.ArrayList ;
-import java.util.Collection ;
-import java.util.HashSet ;
-import java.util.Iterator ;
-import java.util.Set ;
+import java.util.* ;
import org.apache.jena.atlas.iterator.Iter ;
@@ -33,7 +29,6 @@ import com.hp.hpl.jena.sparql.path.P_Mod
import com.hp.hpl.jena.sparql.path.P_NegPropSet ;
import com.hp.hpl.jena.sparql.path.Path ;
-// ----
/** Path evaluator that produces duplicates.
* This is NOT SPARQL semantics.
* This class exists for experimentation.
@@ -42,69 +37,65 @@ import com.hp.hpl.jena.sparql.path.Path
final class PathEngineN extends PathEngine
{
- private final Graph graph ;
private boolean forwardMode ;
- public PathEngineN(Graph graph, boolean forward)
- {
- this.graph = graph ;
+ public PathEngineN(Graph graph, boolean forward) {
+ super(graph, null) ;
this.forwardMode = forward ;
}
-
+
@Override
- protected Collection<Node> collector() { return new ArrayList<Node>() ; }
+ protected Collection<Node> collector() {
+ return new ArrayList<Node>() ;
+ }
@Override
- protected void flipDirection()
- { forwardMode = ! forwardMode ; }
+ protected void flipDirection() {
+ forwardMode = !forwardMode ;
+ }
@Override
- protected boolean direction()
- { return forwardMode ; }
-
+ protected boolean direction() {
+ return forwardMode ;
+ }
+
@Override
- protected void doNegatedPropertySet(P_NegPropSet pathNotOneOf, Node node, Collection<Node> output)
- {
+ protected void doNegatedPropertySet(P_NegPropSet pathNotOneOf, Node node, Collection<Node> output) {
// X !(:a|:b|^:c|^:d) Y = { X !(:a|:b) Y } UNION { Y !(:c|:d) X }
- if ( pathNotOneOf.getFwdNodes().size() > 0 )
- {
- Iterator<Node> nodes1 = stepExcludeForwards(graph, node, pathNotOneOf.getFwdNodes()) ;
+ if ( pathNotOneOf.getFwdNodes().size() > 0 ) {
+ Iterator<Node> nodes1 = stepExcludeForwards(node, pathNotOneOf.getFwdNodes()) ;
fill(nodes1, output) ;
}
- if ( pathNotOneOf.getBwdNodes().size() > 0 )
- {
- Iterator<Node> nodes2 = stepExcludeBackwards(graph, node, pathNotOneOf.getBwdNodes()) ;
+ if ( pathNotOneOf.getBwdNodes().size() > 0 ) {
+ Iterator<Node> nodes2 = stepExcludeBackwards(node, pathNotOneOf.getBwdNodes()) ;
fill(nodes2, output) ;
}
}
@Override
- protected void doAlt(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output)
- {
+ protected void doAlt(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output) {
// Try both sizes, accumulate into output.
- Iterator<Node> iter = eval(graph, pathStepLeft, node) ;
+ Iterator<Node> iter = eval(pathStepLeft, node) ;
fill(iter, output) ;
- iter = eval(graph, pathStepRight, node) ;
+ iter = eval(pathStepRight, node) ;
fill(iter, output) ;
}
@Override
- protected void doSeq(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output)
- {
+ protected void doSeq(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output) {
Path part1 = forwardMode ? pathStepLeft : pathStepRight ;
Path part2 = forwardMode ? pathStepRight : pathStepLeft ;
// Feed one side into the other
- Iter<Node> iter = eval(graph, part1, node) ;
- for ( Node n : iter )
- eval(graph, part2, n, output) ;
+ Iter<Node> iter = eval(part1, node) ;
+ for (Node n : iter)
+ eval(part2, n, output) ;
}
@Override
- protected void doMultiLengthPath(Path pathStep, Node node, long min1, long max1, Collection<Node> output)
- {
+ protected void doMultiLengthPath(Path pathStep, Node node, long min1, long max1, Collection<Node> output) {
// Why not always reduce {N,M} to {N} and {0,M-N}
- // Why not iterate, not recurse, for {N,}
+ // Why not iterate, not recurse, for {N,}
// -- optimizer wil have expanded this so only in unoptimized mode.
if ( min1 == P_Mod.UNSET )
@@ -115,7 +106,7 @@ final class PathEngineN extends PathEngi
// This code is for p{n,m} and :p{,n} inc :p{0,n}
// and for :p{N,}
- //if ( max1 == P_Mod.UNSET ) max1 = 0 ;
+ // if ( max1 == P_Mod.UNSET ) max1 = 0 ;
if ( min1 == 0 )
output.add(node) ;
@@ -128,28 +119,29 @@ final class PathEngineN extends PathEngi
long max2 = dec(max1) ;
// TODO Rewrite
- Path p1 = pathStep ;
+ Path p1 = pathStep ;
Path p2 = new P_Mod(pathStep, min2, max2) ;
- if ( !forwardMode )
- {
- // Reverse order. Do the second bit first.
- Path tmp = p1 ;
- p1 = p2 ; p2 = tmp ;
- // This forces execution to be in the order that it's written, when working backwards.
- // {N,*} is {*} then {N} backwards != do {N} then do {*} as cardinality of the
+ if ( !forwardMode ) {
+ // Reverse order. Do the second bit first.
+ Path tmp = p1 ;
+ p1 = p2 ;
+ p2 = tmp ;
+ // This forces execution to be in the order that it's written, when
+ // working backwards.
+ // {N,*} is {*} then {N} backwards != do {N} then do {*} as
+ // cardinality of the
// two operations is different.
}
// ****
// One step.
- Iterator<Node> iter = eval(graph, p1, node) ;
+ Iterator<Node> iter = eval(p1, node) ;
// Moved on one step
- for ( ; iter.hasNext() ; )
- {
+ for (; iter.hasNext();) {
Node n2 = iter.next() ;
- Iterator<Node> iter2 = eval(graph, p2, n2) ;
+ Iterator<Node> iter2 = eval(p2, n2) ;
fill(iter2, output) ;
}
@@ -157,86 +149,77 @@ final class PathEngineN extends PathEngi
}
@Override
- protected void doFixedLengthPath(Path pathStep, Node node, long fixedLength, Collection<Node> output)
- {
- if ( fixedLength == 0 )
- {
+ protected void doFixedLengthPath(Path pathStep, Node node, long fixedLength, Collection<Node> output) {
+ if ( fixedLength == 0 ) {
output.add(node) ;
return ;
}
// P_Mod(path, count, count)
// One step.
- Iterator<Node> iter = eval(graph, pathStep, node) ;
+ Iterator<Node> iter = eval(pathStep, node) ;
// Build a path for all remaining steps.
long count2 = dec(fixedLength) ;
P_FixedLength nextPath = new P_FixedLength(pathStep, count2) ;
// For each element in the first step, do remaining step
- // Accumulate across everything from first step.
- for ( ; iter.hasNext() ; )
- {
+ // Accumulate across everything from first step.
+ for (; iter.hasNext();) {
Node n2 = iter.next() ;
- Iterator<Node> iter2 = eval(graph, nextPath, n2) ;
+ Iterator<Node> iter2 = eval(nextPath, n2) ;
fill(iter2, output) ;
}
}
@Override
- protected void doZeroOrOne(Path pathStep, Node node, Collection<Node> output)
- {
+ protected void doZeroOrOne(Path pathStep, Node node, Collection<Node> output) {
doZero(pathStep, node, output) ;
doOne(pathStep, node, output) ;
}
- private void doOne(Path path, Node node, Collection<Node> output)
- {
- Iterator<Node> iter = eval(graph, path, node) ;
+ private void doOne(Path path, Node node, Collection<Node> output) {
+ Iterator<Node> iter = eval(path, node) ;
fill(iter, output) ;
}
@Override
- protected void doZero(Path path, Node node, Collection<Node> output)
- {
+ protected void doZero(Path path, Node node, Collection<Node> output) {
output.add(node) ;
}
@Override
- protected void doZeroOrMore(Path path, Node node, Collection<Node> output)
- {
+ protected void doZeroOrMore(Path path, Node node, Collection<Node> output) {
Set<Node> visited = new HashSet<Node>() ;
ALP(node, path, visited, output) ;
}
@Override
- protected void doOneOrMore(Path path, Node node, Collection<Node> output)
- {
+ protected void doOneOrMore(Path path, Node node, Collection<Node> output) {
Set<Node> visited = new HashSet<Node>() ;
// Do one step without including.
- Iterator<Node> iter1 = eval(graph, path, node) ;
- for ( ; iter1.hasNext() ; )
- {
- Node n1 = iter1.next();
+ Iterator<Node> iter1 = eval(path, node) ;
+ for (; iter1.hasNext();) {
+ Node n1 = iter1.next() ;
ALP(n1, path, visited, output) ;
}
}
// This is the worker function for path*
- private void ALP(Node node, Path path, Collection<Node> visited, Collection<Node> output)
- {
- if ( visited.contains(node) ) return ;
+ private void ALP(Node node, Path path, Collection<Node> visited, Collection<Node> output) {
+ if ( visited.contains(node) )
+ return ;
- // If output is a set, then no point going on if node has been added to the results.
+ // If output is a set, then no point going on if node has been added to
+ // the results.
// If output includes duplicates, more solutions are generated
// "visited" is nodes on this path (see the matching .remove).
- if ( ! output.add(node) )
+ if ( !output.add(node) )
return ;
visited.add(node) ;
- Iterator<Node> iter1 = eval(graph, path, node) ;
+ Iterator<Node> iter1 = eval(path, node) ;
// For each step, add to results and recurse.
- for ( ; iter1.hasNext() ; )
- {
- Node n1 = iter1.next();
+ for (; iter1.hasNext();) {
+ Node n1 = iter1.next() ;
ALP(n1, path, visited, output) ;
}
visited.remove(node) ;
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngineSPARQL.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngineSPARQL.java?rev=1597133&r1=1597132&r2=1597133&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngineSPARQL.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEngineSPARQL.java Fri May 23 17:18:24 2014
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package com.hp.hpl.jena.sparql.path.eval;
+package com.hp.hpl.jena.sparql.path.eval ;
import java.util.* ;
@@ -28,167 +28,158 @@ import com.hp.hpl.jena.sparql.path.P_Fix
import com.hp.hpl.jena.sparql.path.P_Mod ;
import com.hp.hpl.jena.sparql.path.P_NegPropSet ;
import com.hp.hpl.jena.sparql.path.Path ;
+import com.hp.hpl.jena.sparql.util.Context ;
-/** Simple implementation */
-
+/** PathEngine, SPARQL semantics */
public class PathEngineSPARQL extends PathEngine
{
- private final Graph graph ;
private boolean forwardMode ;
-
- public PathEngineSPARQL(Graph graph, boolean forward)
- {
- this.graph = graph ;
+
+ public PathEngineSPARQL(Graph graph, Context context) {
+ this(graph, true, context) ;
+ }
+
+ /* package */PathEngineSPARQL(Graph graph, boolean forward, Context context) {
+ super(graph, context) ;
this.forwardMode = forward ;
}
-
+
@Override
- protected void doSeq(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output)
- {
+ protected void doSeq(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output) {
Path part1 = forwardMode ? pathStepLeft : pathStepRight ;
Path part2 = forwardMode ? pathStepRight : pathStepLeft ;
-
+
// Feed one side into the other
- Iter<Node> iter = eval(graph, part1, node) ;
- for ( Node n : iter )
- eval(graph, part2, n, output) ;
+ Iter<Node> iter = eval(part1, node) ;
+ for (Node n : iter)
+ eval(part2, n, output) ;
}
@Override
- protected void doAlt(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output)
- {
+ protected void doAlt(Path pathStepLeft, Path pathStepRight, Node node, Collection<Node> output) {
// Try both sizes, accumulate into output.
- Iterator<Node> iter = eval(graph, pathStepLeft, node) ;
+ Iterator<Node> iter = eval(pathStepLeft, node) ;
fill(iter, output) ;
- iter = eval(graph, pathStepRight, node) ;
+ iter = eval(pathStepRight, node) ;
fill(iter, output) ;
}
@Override
- protected void doNegatedPropertySet(P_NegPropSet pathNotOneOf, Node node, Collection<Node> output)
- {
- if ( pathNotOneOf.getFwdNodes().size() > 0 )
- {
- Iterator<Node> nodes1 = stepExcludeForwards(graph, node, pathNotOneOf.getFwdNodes()) ;
+ protected void doNegatedPropertySet(P_NegPropSet pathNotOneOf, Node node, Collection<Node> output) {
+ if ( pathNotOneOf.getFwdNodes().size() > 0 ) {
+ Iterator<Node> nodes1 = stepExcludeForwards(node, pathNotOneOf.getFwdNodes()) ;
fill(nodes1, output) ;
}
- if ( pathNotOneOf.getBwdNodes().size() > 0 )
- {
- Iterator<Node> nodes2 = stepExcludeBackwards(graph, node, pathNotOneOf.getBwdNodes()) ;
+ if ( pathNotOneOf.getBwdNodes().size() > 0 ) {
+ Iterator<Node> nodes2 = stepExcludeBackwards(node, pathNotOneOf.getBwdNodes()) ;
fill(nodes2, output) ;
}
}
@Override
- protected void doZeroOrOne(Path pathStep, Node node, Collection<Node> output)
- {
+ protected void doZeroOrOne(Path pathStep, Node node, Collection<Node> output) {
// Force unique evaluation.
Collection<Node> x = new HashSet<Node>() ;
- eval(graph, pathStep, node, x) ;
+ eval(pathStep, node, x) ;
x.add(node) ;
output.addAll(x) ;
}
@Override
- protected void doZeroOrMore(Path pathStep, Node node, Collection<Node> output)
- {
+ protected void doZeroOrMore(Path pathStep, Node node, Collection<Node> output) {
// Reuse "output"
- Collection<Node> visited = new LinkedList<Node>() ; //new HashSet<Node>() ;
- ALP_1(graph, forwardMode, 0, -1, node, pathStep, visited) ;
+ Collection<Node> visited = new LinkedList<Node>() ; // new
+ // HashSet<Node>() ;
+ ALP_1(forwardMode, 0, -1, node, pathStep, visited) ;
output.addAll(visited) ;
}
@Override
- protected void doOneOrMore(Path pathStep, Node node, Collection<Node> output)
- {
+ protected void doOneOrMore(Path pathStep, Node node, Collection<Node> output) {
// Reuse "output"
- Collection<Node> visited = new LinkedList<Node>() ; // new HashSet<Node>() ;
+ Collection<Node> visited = new LinkedList<Node>() ; // new
+ // HashSet<Node>() ;
// Do one step without including.
- //TODO switch to PathEngine1 for the sub-step as we only need uniques.
- Iter<Node> iter1 = eval(graph, pathStep, node) ;
- for ( ; iter1.hasNext() ; )
- {
- Node n1 = iter1.next();
- ALP_1(graph, forwardMode, 0, -1, n1, pathStep, visited) ;
+ // TODO switch to PathEngine1 for the sub-step as we only need uniques.
+ Iter<Node> iter1 = eval(pathStep, node) ;
+ for (; iter1.hasNext();) {
+ Node n1 = iter1.next() ;
+ ALP_1(forwardMode, 0, -1, n1, pathStep, visited) ;
}
output.addAll(visited) ;
}
-
+
@Override
- protected void doZero(Path path, Node node, Collection<Node> output)
- {
+ protected void doZero(Path path, Node node, Collection<Node> output) {
// Not SPARQL
output.add(node) ;
}
- //TODO switch to PathEngine1 for the sub-step as we only need uniques.
- private void ALP_1(Graph graph, boolean forwardMode, int stepCount, int maxStepCount, Node node, Path path, Collection<Node> visited)
- {
- if ( false ) System.out.printf("ALP1 node=%s\n visited=%s\n output=%s\n", node, visited) ;
- if ( maxStepCount >=0 && stepCount > maxStepCount ) return ;
- if ( visited.contains(node) ) return ;
+ // TODO ?? switch to PathEngine1 for the sub-step as we only need uniques.
+ private void ALP_1(boolean forwardMode, int stepCount, int maxStepCount, Node node, Path path, Collection<Node> visited) {
+ if ( false )
+ System.out.printf("ALP1 node=%s\n visited=%s\n output=%s\n", node, visited) ;
+ if ( maxStepCount >= 0 && stepCount > maxStepCount )
+ return ;
+ if ( visited.contains(node) )
+ return ;
- if ( ! visited.add(node) )
+ if ( !visited.add(node) )
return ;
- Iter<Node> iter1 = eval(graph, path, node) ;
+ Iter<Node> iter1 = eval(path, node) ;
// For each step, add to results and recurse.
- for ( ; iter1.hasNext() ; )
- {
- Node n1 = iter1.next();
- ALP_1(graph, forwardMode, stepCount+1, maxStepCount, n1, path, visited) ;
+ for (; iter1.hasNext();) {
+ Node n1 = iter1.next() ;
+ ALP_1(forwardMode, stepCount + 1, maxStepCount, n1, path, visited) ;
}
}
@Override
- protected void doZeroOrMoreN(Path pathStep, Node node, Collection<Node> output)
- {
+ protected void doZeroOrMoreN(Path pathStep, Node node, Collection<Node> output) {
Set<Node> visited = new HashSet<Node>() ;
ALP_N(node, pathStep, visited, output) ;
}
@Override
- protected void doOneOrMoreN(Path pathStep, Node node, Collection<Node> output)
- {
+ protected void doOneOrMoreN(Path pathStep, Node node, Collection<Node> output) {
Set<Node> visited = new HashSet<Node>() ;
// Do one step without including.
- Iterator<Node> iter1 = eval(graph, pathStep, node) ;
- for ( ; iter1.hasNext() ; )
- {
- Node n1 = iter1.next();
+ Iterator<Node> iter1 = eval(pathStep, node) ;
+ for (; iter1.hasNext();) {
+ Node n1 = iter1.next() ;
ALP_N(n1, pathStep, visited, output) ;
}
}
// This is the worker function for path{*}
- private void ALP_N(Node node, Path path, Collection<Node> visited, Collection<Node> output)
- {
- if ( visited.contains(node) ) return ;
-
- // If output is a set, then no point going on if node has been added to the results.
+ private void ALP_N(Node node, Path path, Collection<Node> visited, Collection<Node> output) {
+ if ( visited.contains(node) )
+ return ;
+
+ // If output is a set, then no point going on if node has been added to
+ // the results.
// If output includes duplicates, more solutions are generated
// "visited" is nodes on this path (see the matching .remove).
- if ( ! output.add(node) )
+ if ( !output.add(node) )
return ;
-
+
visited.add(node) ;
-
- Iterator<Node> iter1 = eval(graph, path, node) ;
+
+ Iterator<Node> iter1 = eval(path, node) ;
// For each step, add to results and recurse.
- for ( ; iter1.hasNext() ; )
- {
- Node n1 = iter1.next();
+ for (; iter1.hasNext();) {
+ Node n1 = iter1.next() ;
ALP_N(n1, path, visited, output) ;
}
visited.remove(node) ;
}
@Override
- protected void doMultiLengthPath(Path pathStep, Node node, long min1, long max1, Collection<Node> output)
- {
+ protected void doMultiLengthPath(Path pathStep, Node node, long min1, long max1, Collection<Node> output) {
// Not SPARQL
// Why not always reduce {N,M} to {N} and {0,M-N}
- // Why not iterate, not recurse, for {N,}
+ // Why not iterate, not recurse, for {N,}
// -- optimizer will have expanded this so only in unoptimized mode.
if ( min1 == P_Mod.UNSET )
@@ -199,7 +190,7 @@ public class PathEngineSPARQL extends Pa
// This code is for p{n,m} and :p{,n} inc :p{0,n}
// and for :p{N,}
- //if ( max1 == P_Mod.UNSET ) max1 = 0 ;
+ // if ( max1 == P_Mod.UNSET ) max1 = 0 ;
if ( min1 == 0 )
output.add(node) ;
@@ -212,28 +203,29 @@ public class PathEngineSPARQL extends Pa
long max2 = dec(max1) ;
// TODO Rewrite
- Path p1 = pathStep ;
+ Path p1 = pathStep ;
Path p2 = new P_Mod(pathStep, min2, max2) ;
- if ( !forwardMode )
- {
- // Reverse order. Do the second bit first.
- Path tmp = p1 ;
- p1 = p2 ; p2 = tmp ;
- // This forces execution to be in the order that it's written, when working backwards.
- // {N,*} is {*} then {N} backwards != do {N} then do {*} as cardinality of the
+ if ( !forwardMode ) {
+ // Reverse order. Do the second bit first.
+ Path tmp = p1 ;
+ p1 = p2 ;
+ p2 = tmp ;
+ // This forces execution to be in the order that it's written, when
+ // working backwards.
+ // {N,*} is {*} then {N} backwards != do {N} then do {*} as
+ // cardinality of the
// two operations is different.
}
// ****
// One step.
- Iterator<Node> iter = eval(graph, p1, node) ;
+ Iterator<Node> iter = eval(p1, node) ;
// Moved on one step
- for ( ; iter.hasNext() ; )
- {
+ for (; iter.hasNext();) {
Node n2 = iter.next() ;
- Iterator<Node> iter2 = eval(graph, p2, n2) ;
+ Iterator<Node> iter2 = eval(p2, n2) ;
fill(iter2, output) ;
}
@@ -241,40 +233,39 @@ public class PathEngineSPARQL extends Pa
}
@Override
- protected void doFixedLengthPath(Path pathStep, Node node, long fixedLength, Collection<Node> output)
- {
+ protected void doFixedLengthPath(Path pathStep, Node node, long fixedLength, Collection<Node> output) {
// Not SPARQL
- if ( fixedLength == 0 )
- {
+ if ( fixedLength == 0 ) {
output.add(node) ;
return ;
}
// P_Mod(path, count, count)
// One step.
- Iterator<Node> iter = eval(graph, pathStep, node) ;
+ Iterator<Node> iter = eval(pathStep, node) ;
// Build a path for all remaining steps.
long count2 = dec(fixedLength) ;
P_FixedLength nextPath = new P_FixedLength(pathStep, count2) ;
// For each element in the first step, do remaining step
- // Accumulate across everything from first step.
- for ( ; iter.hasNext() ; )
- {
+ // Accumulate across everything from first step.
+ for (; iter.hasNext();) {
Node n2 = iter.next() ;
- Iterator<Node> iter2 = eval(graph, nextPath, n2) ;
+ Iterator<Node> iter2 = eval(nextPath, n2) ;
fill(iter2, output) ;
}
}
@Override
- protected void flipDirection()
- { forwardMode = ! forwardMode ; }
+ protected void flipDirection() {
+ forwardMode = !forwardMode ;
+ }
@Override
- protected boolean direction()
- { return forwardMode ; }
+ protected boolean direction() {
+ return forwardMode ;
+ }
@Override
- protected Collection<Node> collector() { return new ArrayList<Node>() ; }
-
+ protected Collection<Node> collector() {
+ return new ArrayList<Node>() ;
+ }
}
-
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEval.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEval.java?rev=1597133&r1=1597132&r2=1597133&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEval.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEval.java Fri May 23 17:18:24 2014
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package com.hp.hpl.jena.sparql.path.eval;
+package com.hp.hpl.jena.sparql.path.eval ;
import java.util.ArrayList ;
import java.util.Collection ;
@@ -27,64 +27,55 @@ import org.apache.jena.atlas.iterator.It
import com.hp.hpl.jena.graph.Graph ;
import com.hp.hpl.jena.graph.Node ;
import com.hp.hpl.jena.sparql.path.Path ;
+import com.hp.hpl.jena.sparql.util.Context ;
/** Path evaluation - public interface */
public class PathEval
{
- /** Evaluate a path : SPARQL semantics */
- static public Iterator<Node> eval(Graph graph, Node node, Path path)
- {
- return eval$(graph, node, path, new PathEngineSPARQL(graph, true)) ;
- //return eval$(graph, node, path, new PathEngineN(graph, true)) ;
- }
-
- /** Evaluate a path */
- static public Iterator<Node> evalReverse(Graph graph, Node node, Path path)
- {
- return eval$(graph, node, path, new PathEngineSPARQL(graph, false)) ;
- //return eval$(graph, node, path, new PathEngineN(graph, false)) ;
- }
-
-
- /** Evaluate a path : counting semantics */
- static public Iterator<Node> evalN(Graph graph, Node node, Path path)
- {
+ /** Evaluate a path : SPARQL semantics */
+ static public Iterator<Node> eval(Graph graph, Node node, Path path, Context context) {
+ return eval$(graph, node, path, new PathEngineSPARQL(graph, true, context)) ;
+ // return eval$(graph, node, path, new PathEngineN(graph, true)) ;
+ }
+
+ /** Evaluate a path */
+ static public Iterator<Node> evalReverse(Graph graph, Node node, Path path, Context context) {
+ return eval$(graph, node, path, new PathEngineSPARQL(graph, false, context)) ;
+ // return eval$(graph, node, path, new PathEngineN(graph, false)) ;
+ }
+
+ /** Evaluate a path : counting semantics */
+ static public Iterator<Node> evalN(Graph graph, Node node, Path path) {
return eval$(graph, node, path, new PathEngineN(graph, true)) ;
}
- /** Evaluate a path : counting semantics */
- static public Iterator<Node> evalReverseN(Graph graph, Node node, Path path)
- {
+ /** Evaluate a path : counting semantics */
+ static public Iterator<Node> evalReverseN(Graph graph, Node node, Path path) {
return eval$(graph, node, path, new PathEngineN(graph, false)) ;
}
- /** Evaluate a path : unique results */
- static public Iterator<Node> eval1(Graph graph, Node node, Path path)
- {
+ /** Evaluate a path : unique results */
+ static public Iterator<Node> eval1(Graph graph, Node node, Path path) {
return eval$(graph, node, path, new PathEngine1(graph, true)) ;
}
- /** Evaluate a path : unique results */
- static public Iterator<Node> evalReverse1(Graph graph, Node node, Path path)
- {
+ /** Evaluate a path : unique results */
+ static public Iterator<Node> evalReverse1(Graph graph, Node node, Path path) {
return eval$(graph, node, path, new PathEngine1(graph, false)) ;
}
- /** Evaluate a path */
- static void eval$(Graph graph, Node node, Path path, PathEngine engine, Collection<Node> acc)
- {
+ /** Evaluate a path */
+ static void eval$(Graph graph, Node node, Path path, PathEngine engine, Collection<Node> acc) {
PathEvaluator evaluator = new PathEvaluator(graph, node, acc, engine) ;
path.visit(evaluator) ;
}
- /** Evaluate a path */
- static Iter<Node> eval$(Graph graph, Node node, Path path, PathEngine engine)
- {
+ /** Evaluate a path */
+ static Iter<Node> eval$(Graph graph, Node node, Path path, PathEngine engine) {
Collection<Node> acc = new ArrayList<Node>() ;
PathEvaluator evaluator = new PathEvaluator(graph, node, acc, engine) ;
path.visit(evaluator) ;
return Iter.iter(acc) ;
}
-
}
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEvaluator.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEvaluator.java?rev=1597133&r1=1597132&r2=1597133&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEvaluator.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/path/eval/PathEvaluator.java Fri May 23 17:18:24 2014
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package com.hp.hpl.jena.sparql.path.eval;
+package com.hp.hpl.jena.sparql.path.eval ;
import java.util.Collection ;
import java.util.Iterator ;
@@ -33,189 +33,158 @@ import com.hp.hpl.jena.sparql.sse.writer
final class PathEvaluator implements PathVisitor
{
- protected final Graph graph ;
- protected final Node node ;
+ protected final Graph graph ;
+ protected final Node node ;
protected final Collection<Node> output ;
- private PathEngine engine ;
-
- protected PathEvaluator(Graph g, Node n, Collection<Node> output, PathEngine engine)
- {
- this.graph = g ;
+ private PathEngine engine ;
+
+ protected PathEvaluator(Graph g, Node n, Collection<Node> output, PathEngine engine) {
+ this.graph = g ;
this.node = n ;
this.output = output ;
this.engine = engine ;
}
-
- // Overrides
-
- // Work functions.
-
- protected final void fill(Iterator<Node> iter)
- {
- for ( ; iter.hasNext() ; )
+
+ protected final void fill(Iterator<Node> iter) {
+ for (; iter.hasNext();)
output.add(iter.next()) ;
}
-
+
// These operations yield the same results regardless of counting
// (their subpaths may not).
@Override
- public void visit(P_Link pathNode)
- {
- Iterator<Node> nodes = engine.doOne(graph, node, pathNode.getNode()) ;
+ public void visit(P_Link pathNode) {
+ Iterator<Node> nodes = engine.doOne(node, pathNode.getNode()) ;
fill(nodes) ;
}
-
+
@Override
- public void visit(P_ReverseLink pathNode)
- {
+ public void visit(P_ReverseLink pathNode) {
engine.flipDirection() ;
- Iterator<Node> nodes = engine.doOne(graph, node, pathNode.getNode()) ;
+ Iterator<Node> nodes = engine.doOne(node, pathNode.getNode()) ;
fill(nodes) ;
engine.flipDirection() ;
}
-
+
@Override
- public void visit(P_Inverse inversePath)
- {
- //boolean b = forwardMode ;
+ public void visit(P_Inverse inversePath) {
+ // boolean b = forwardMode ;
// Flip direction and evaluate
engine.flipDirection() ;
- engine.eval(graph, inversePath.getSubPath(), node, output) ;
+ engine.eval(inversePath.getSubPath(), node, output) ;
engine.flipDirection() ;
}
-
+
@Override
- public void visit(P_NegPropSet pathNotOneOf)
- {
+ public void visit(P_NegPropSet pathNotOneOf) {
engine.doNegatedPropertySet(pathNotOneOf, node, output) ;
}
@Override
- public void visit(P_Mod pathMod)
- {
+ public void visit(P_Mod pathMod) {
// do..Or.. need to take a visited set.
-
- if ( pathMod.isZeroOrMore() )
- {
+
+ if ( pathMod.isZeroOrMore() ) {
// :p{0,}
engine.doOneOrMoreN(pathMod.getSubPath(), node, output) ;
return ;
}
- if ( pathMod.isOneOrMore() )
- {
+ if ( pathMod.isOneOrMore() ) {
engine.doOneOrMoreN(pathMod.getSubPath(), node, output) ;
return ;
}
-
+
if ( pathMod.isFixedLength() )
engine.doFixedLengthPath(pathMod.getSubPath(), node, pathMod.getFixedLength(), output) ;
else
- engine.doMultiLengthPath(pathMod.getSubPath(),
- node,
- pathMod.getMin(),
- pathMod.getMax(),
- output) ;
+ engine.doMultiLengthPath(pathMod.getSubPath(), node, pathMod.getMin(), pathMod.getMax(), output) ;
}
@Override
- public void visit(P_FixedLength pFixedLength)
- {
- engine.doFixedLengthPath(pFixedLength.getSubPath(), node, pFixedLength.getCount(), output) ;
+ public void visit(P_FixedLength pFixedLength) {
+ engine.doFixedLengthPath(pFixedLength.getSubPath(), node, pFixedLength.getCount(), output) ;
}
-
+
@Override
- public void visit(P_ZeroOrOne path)
- {
+ public void visit(P_ZeroOrOne path) {
engine.doZeroOrOne(path.getSubPath(), node, output) ;
}
-
+
@Override
- public void visit(P_ZeroOrMore1 path)
- {
+ public void visit(P_ZeroOrMore1 path) {
engine.doZeroOrMore(path.getSubPath(), node, output) ;
}
-
+
@Override
- public void visit(P_ZeroOrMoreN path)
- {
+ public void visit(P_ZeroOrMoreN path) {
engine.doZeroOrMoreN(path.getSubPath(), node, output) ;
}
-
@Override
- public void visit(P_OneOrMore1 path)
- {
+ public void visit(P_OneOrMore1 path) {
engine.doOneOrMore(path.getSubPath(), node, output) ;
}
-
+
@Override
- public void visit(P_OneOrMoreN path)
- {
+ public void visit(P_OneOrMoreN path) {
engine.doOneOrMoreN(path.getSubPath(), node, output) ;
}
-
+
@Override
- public void visit(P_Alt pathAlt)
- {
+ public void visit(P_Alt pathAlt) {
engine.doAlt(pathAlt.getLeft(), pathAlt.getRight(), node, output) ;
}
-
+
@Override
- public void visit(P_Distinct pathDistinct)
- {
+ public void visit(P_Distinct pathDistinct) {
PathEngine engine2 = engine ;
engine = new PathEngine1(graph, engine.direction()) ;
- engine.eval(graph, pathDistinct.getSubPath(), node, output) ;
+ engine.eval(pathDistinct.getSubPath(), node, output) ;
engine = engine2 ;
}
@Override
- public void visit(P_Multi pathMulti)
- {
+ public void visit(P_Multi pathMulti) {
PathEngine engine2 = engine ;
engine = new PathEngineN(graph, engine.direction()) ;
- engine.eval(graph, pathMulti.getSubPath(), node, output) ;
+ engine.eval(pathMulti.getSubPath(), node, output) ;
engine = engine2 ;
}
@Override
- public void visit(P_Shortest path)
- {
+ public void visit(P_Shortest path) {
throw new ARQNotImplemented(WriterPath.asString(path)) ;
}
@Override
- public void visit(P_Seq pathSeq)
- {
+ public void visit(P_Seq pathSeq) {
engine.doSeq(pathSeq.getLeft(), pathSeq.getRight(), node, output) ;
}
-
- // Other operations can produce duplicates and so may be executed in
- // different ways depending on cardibnality requirements.
-
+
+ // Other operations can produce duplicates and so may be executed in
+ // different ways depending on cardibnality requirements.
+
protected static class FilterExclude implements Filter<Triple>
{
private Collection<Node> excludes ;
- public FilterExclude(Collection <Node> excludes) { this.excludes = excludes ; }
+
+ public FilterExclude(Collection<Node> excludes) {
+ this.excludes = excludes ;
+ }
+
@Override
- public boolean accept(Triple triple)
- {
- return ! excludes.contains(triple.getPredicate()) ;
+ public boolean accept(Triple triple) {
+ return !excludes.contains(triple.getPredicate()) ;
}
}
-
- final protected Iter<Triple> between(Node x, Node z)
- {
- Iter<Triple> iter1 = Iter.iter(graph.find(x, Node.ANY, z)) ;
- return iter1 ;
+
+ final protected Iter<Triple> between(Node x, Node z) {
+ return Iter.iter(engine.graphFind(x, Node.ANY, z)) ;
}
-
- final protected void doZero(Path path, Node node, Collection<Node> output)
- {
+ final protected void doZero(Path path, Node node, Collection<Node> output) {
// Ignores path.
output.add(node) ;
}
-
}
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/pfunction/PropertyFunctionRegistry.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/pfunction/PropertyFunctionRegistry.java?rev=1597133&r1=1597132&r2=1597133&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/pfunction/PropertyFunctionRegistry.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/pfunction/PropertyFunctionRegistry.java Fri May 23 17:18:24 2014
@@ -55,7 +55,7 @@ public class PropertyFunctionRegistry
return (PropertyFunctionRegistry)context.get(ARQConstants.registryPropertyFunctions) ;
}
- /** Get the PropertyFunctionRegistry, defailting to the global one */
+ /** Get the PropertyFunctionRegistry, defaulting to the global one */
public static PropertyFunctionRegistry chooseRegistry(Context context)
{
PropertyFunctionRegistry registry = PropertyFunctionRegistry.get(context) ;
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/procedure/ProcEval.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/procedure/ProcEval.java?rev=1597133&r1=1597132&r2=1597133&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/procedure/ProcEval.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/procedure/ProcEval.java Fri May 23 17:18:24 2014
@@ -64,7 +64,7 @@ public class ProcEval
public static Procedure build(Node procId, PropFuncArg subjArg, PropFuncArg objArg, ExecutionContext execCxt)
{
Context context = execCxt.getContext() ;
- PropertyFunctionRegistry reg = choosePropFuncRegistry(context) ;
+ PropertyFunctionRegistry reg = PropertyFunctionRegistry.chooseRegistry(context) ;
PropertyFunctionFactory f = reg.get(procId.getURI()) ;
PropertyFunction pf = f.create(procId.getURI()) ;
pf.build(subjArg, procId, objArg, execCxt) ;
@@ -73,22 +73,6 @@ public class ProcEval
}
- static public PropertyFunctionRegistry choosePropFuncRegistry(Context context)
- {
- PropertyFunctionRegistry registry = PropertyFunctionRegistry.get(context) ;
- if ( registry == null )
- registry = PropertyFunctionRegistry.get() ;
- return registry ;
- }
-
- // ----
-
-// /** Evaluate a procedure */
-// public static QueryIterator eval(QueryIterator queryIterator, Procedure proc)
-// {
-// return eval(queryIterator, proc, null) ;
-// }
-//
/** Evaluate a procedure */
public static QueryIterator eval(QueryIterator queryIterator, Procedure proc, ExecutionContext execCxt)
{
Modified: jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/path/TestPath.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/path/TestPath.java?rev=1597133&r1=1597132&r2=1597133&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/path/TestPath.java (original)
+++ jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/path/TestPath.java Fri May 23 17:18:24 2014
@@ -333,7 +333,7 @@ public class TestPath extends BaseTest
{
Path p = PathParser.parse(string, pmap) ;
Iterator<Node> resultsIter =
- directionForward ? PathEval.eval(graph, start, p) : PathEval.evalReverse(graph, start, p) ;
+ directionForward ? PathEval.eval(graph, start, p, ARQ.getContext()) : PathEval.evalReverse(graph, start, p, ARQ.getContext()) ;
List<Node> results = Iter.toList(resultsIter) ;
List<Node> expected = Arrays.asList(expectedNodes) ;
// Unordered by counting equality.
Modified: jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/path/TestPath2.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/path/TestPath2.java?rev=1597133&r1=1597132&r2=1597133&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/path/TestPath2.java (original)
+++ jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/path/TestPath2.java Fri May 23 17:18:24 2014
@@ -30,6 +30,7 @@ import org.junit.Test ;
import com.hp.hpl.jena.graph.Graph ;
import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.query.ARQ ;
import com.hp.hpl.jena.sparql.path.eval.PathEval ;
import com.hp.hpl.jena.sparql.sse.SSE ;
@@ -112,8 +113,8 @@ public class TestPath2 extends BaseTest
String ps = "(prefix "+prefixes+" "+pathStr+")" ;
Path path = SSE.parsePath(ps) ;
- List<Node> nodes1 = Iter.toList(PathEval.eval(graph, start, PathFactory.pathDistinct(path))) ;
- List<Node> nodes2 = Iter.toList(PathEval.eval(graph, start, path)) ;
+ List<Node> nodes1 = Iter.toList(PathEval.eval(graph, start, PathFactory.pathDistinct(path), ARQ.getContext() )) ;
+ List<Node> nodes2 = Iter.toList(PathEval.eval(graph, start, path, ARQ.getContext() )) ;
List<Node> expected = new ArrayList<Node>() ;
for ( String n : results )
expected.add(parse(n)) ;