Return-Path: X-Original-To: apmail-jena-commits-archive@www.apache.org Delivered-To: apmail-jena-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0C66211AB8 for ; Fri, 23 May 2014 17:18:49 +0000 (UTC) Received: (qmail 8550 invoked by uid 500); 23 May 2014 17:18:49 -0000 Delivered-To: apmail-jena-commits-archive@jena.apache.org Received: (qmail 8524 invoked by uid 500); 23 May 2014 17:18:48 -0000 Mailing-List: contact commits-help@jena.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jena.apache.org Delivered-To: mailing list commits@jena.apache.org Received: (qmail 8515 invoked by uid 99); 23 May 2014 17:18:48 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 23 May 2014 17:18:48 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 23 May 2014 17:18:45 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 321EB2388860; Fri, 23 May 2014 17:18:25 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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... Date: Fri, 23 May 2014 17:18:24 -0000 To: commits@jena.apache.org From: andy@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140523171825.321EB2388860@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 iter = PathEval.eval(graph, subject, path) ; + Iterator 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 pathIter = PathEval.eval(graph, n, path) ; + Iterator 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 iter = PathEval.eval(graph, subject, path) ; + Iterator iter = PathEval.eval(graph, subject, path, execCxt.getContext()) ; Filter filter = new Filter() { @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 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 eval(Path path, Node node) { return PathEval.eval$(graph, node, path, this) ; } - - protected final void eval(Graph graph, Path path, Node node, Collection output) - { + + protected final void eval(Path path, Node node, Collection output) { PathEval.eval$(graph, node, path, this, output) ; } - + + // protected final void eval(Path path, Node node, Collection output) + protected abstract void flipDirection() ; + protected abstract boolean direction() ; - + protected abstract Collection collector() ; - - // protected abstract void doZero(Path pathStep, Node node, Collection output) ; - // protected abstract void doOne(Path pathStep, Node node, Collection output) ; - + + // protected abstract void doZero(Path pathStep, Node node, Collection + // output) ; + // protected abstract void doOne(Path pathStep, Node node, Collection + // output) ; + // --- Where we touch the graph // Because it SP? or ?PO, no duplicates occur, so works for both strategies. - protected final Iterator doOne(Graph graph, Node node, Node property) - { + protected final Iterator doOne(Node node, Node property) { Iterator iter2 = null ; - if ( direction() ) - { - Iter iter1 = Iter.iter(graph.find(node, property, Node.ANY)) ; + if ( direction() ) { + Iter iter1 = Iter.iter(graphFind(node, property, Node.ANY)) ; iter2 = iter1.map(PathEngine.selectObject) ; - } - else - { - Iter iter1 = Iter.iter(graph.find(Node.ANY, property, node)) ; + } else { + Iter 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 output) ; // path{*} : default implementation - protected void doZeroOrMoreN(Path pathStep, Node node, Collection output) - { doZeroOrMore(pathStep, node, output) ; } + protected void doZeroOrMoreN(Path pathStep, Node node, Collection output) { + doZeroOrMore(pathStep, node, output) ; + } // path{+} : default implementation - protected void doOneOrMoreN(Path pathStep, Node node, Collection output) - { doOneOrMore(pathStep, node, output) ; } + protected void doOneOrMoreN(Path pathStep, Node node, Collection output) { + doOneOrMore(pathStep, node, output) ; + } protected abstract void doZero(Path path, Node node, Collection output) ; - + // {N,M} and variations - + protected abstract void doFixedLengthPath(Path pathStep, Node node, long fixedLength, Collection output) ; protected abstract void doMultiLengthPath(Path pathStep, Node node, long min, long max, Collection output) ; - - protected final void fill(Iterator iter, Collection output) - { - for ( ; iter.hasNext() ; ) + + protected final void fill(Iterator iter, Collection output) { + for (; iter.hasNext();) output.add(iter.next()) ; } - - protected static long dec(long x) { return (x<=0) ? x : x-1 ; } - - protected static Transform selectSubject = new Transform() { + + protected static long dec(long x) { + return (x <= 0) ? x : x - 1 ; + } + + protected static Transform selectSubject = new Transform() { @Override - public Node convert(Triple triple) - { - return triple.getSubject() ; - } + public Node convert(Triple triple) { return triple.getSubject() ; } } ; + protected static Transform selectPredicate = new Transform() { @Override - public Node convert(Triple triple) - { - return triple.getPredicate() ; - } + public Node convert(Triple triple) { return triple.getPredicate() ; } } ; - protected static Transform selectObject = new Transform() { + + protected static Transform selectObject = new Transform() { @Override - public Node convert(Triple triple) - { - return triple.getObject() ; - } + public Node convert(Triple triple) { return triple.getObject() ; } } ; - - protected static Iterator stepExcludeForwards(Graph graph , Node node , List excludedNodes ) - { - Iter iter1 = forwardLinks(graph, node, excludedNodes) ; + protected Iterator stepExcludeForwards(Node node, List excludedNodes) { + Iter iter1 = forwardLinks(node, excludedNodes) ; Iter r1 = iter1.map(selectObject) ; return r1 ; } - protected static Iterator stepExcludeBackwards(Graph graph , Node node , List excludedNodes ) - { - Iter iter1 = backwardLinks(graph, node, excludedNodes) ; + protected Iterator stepExcludeBackwards(Node node, List excludedNodes) { + Iter iter1 = backwardLinks(node, excludedNodes) ; Iter r1 = iter1.map(selectSubject) ; return r1 ; } - protected static Iter forwardLinks(Graph graph, Node x, Collection excludeProperties) - { - Iter iter1 = Iter.iter(graph.find(x, Node.ANY, Node.ANY)) ; + protected Iter forwardLinks(Node x, Collection excludeProperties) { + Iter iter1 = Iter.iter(graphFind(x, Node.ANY, Node.ANY)) ; if ( excludeProperties != null ) iter1 = iter1.filter(new FilterExclude(excludeProperties)) ; return iter1 ; } - protected static Iter backwardLinks(Graph graph, Node x, Collection excludeProperties) - { - Iter iter1 = Iter.iter(graph.find(Node.ANY, Node.ANY, x)) ; + protected Iter backwardLinks(Node x, Collection excludeProperties) { + Iter iter1 = Iter.iter(graphFind(Node.ANY, Node.ANY, x)) ; if ( excludeProperties != null ) iter1 = iter1.filter(new FilterExclude(excludeProperties)) ; return iter1 ; } -} - + protected Iterator graphFind(Node s, Node p, Node o) { + return graphFind(graph, s, p, o) ; + } + + static Binding binding = BindingFactory.binding() ; + + private/* package */static Iterator 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 collector() - { return new ArrayList() ; } - //{ return new HashSet() ; } - + protected Collection collector() { + return new ArrayList() ; + // { return new HashSet() ; } + } + + @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 output) - { + protected void doAlt(Path pathStepLeft, Path pathStepRight, Node node, Collection output) { // Must be duplicate supressing. Collection nodes = new HashSet() ; // 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 output) - { + protected void doSeq(Path pathStepLeft, Path pathStepRight, Node node, Collection output) { Path part1 = forwardMode ? pathStepLeft : pathStepRight ; Path part2 = forwardMode ? pathStepRight : pathStepLeft ; - + Collection nodes = collector() ; - eval(graph, part1, node, nodes) ; + eval(part1, node, nodes) ; Collection nodes2 = new HashSet() ; - 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 nodes, Collection acc) - { - for ( ; nodes.hasNext() ; ) - { + private static void fillUnique(Iterator nodes, Collection 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 output) - { + protected void doMultiLengthPath(Path pathStep, Node node, long min1, long max1, Collection 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 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 visited, - Collection output) - { + protected void doMultiLengthPath(Path pathStep, Node node, long length, Collection visited, Collection 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 iter = eval(graph, pathStep, node) ; - for ( ; iter.hasNext() ; ) - { + Iterator 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, Collectionvisited) -// { + +// // Do {0,length} +// private void doFixedLengthPath(Path path, Node node, int length, Collection 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 output) - { + protected void doFixedLengthPath(Path pathStep, Node node, long fixedLength, Collection output) { // Special for small? -// if ( fixedLength < 3 ) -// {} + // if ( fixedLength < 3 ) + // {} Collection visited = collector() ; - - if ( fixedLength == 0 ) - { + + if ( fixedLength == 0 ) { doZero(pathStep, node, output) ; return ; } - if ( fixedLength == 1 ) - { - Iter iter = eval(graph, pathStep, node) ; - for ( Node n : iter ) - { + if ( fixedLength == 1 ) { + Iter iter = eval(pathStep, node) ; + for (Node n : iter) { if ( !output.contains(n) ) output.add(n) ; } return ; } // Loop, not recurse. - Iter iter = eval(graph, pathStep, node) ; - for ( Node n : iter ) - doFixedLengthPath(pathStep, n, fixedLength-1, output) ; + Iter iter = eval(pathStep, node) ; + for (Node n : iter) + doFixedLengthPath(pathStep, n, fixedLength - 1, output) ; return ; } - @Override - protected void doZeroOrMore(Path path, Node node, Collection output) - { + protected void doZeroOrMore(Path path, Node node, Collection output) { // Reuse "output" - Collection visited = new LinkedList() ; //new HashSet() ; - ALP1(graph, forwardMode, 0, -1, node, path, visited) ; + Collection visited = new LinkedList() ; // new + // HashSet() ; + ALP1(forwardMode, 0, -1, node, path, visited) ; output.addAll(visited) ; } @Override - protected void doOneOrMore(Path path, Node node, Collection output) - { + protected void doOneOrMore(Path path, Node node, Collection output) { // Reuse "output" - Collection visited = new LinkedList() ; // new HashSet() ; + Collection visited = new LinkedList() ; // new + // HashSet() ; // Do one step without including. - Iter iter1 = eval(graph, path, node) ; - for ( ; iter1.hasNext() ; ) - { - Node n1 = iter1.next(); - ALP1(graph, forwardMode, 0, -1, n1, path, visited) ; + Iter 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 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 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 iter1 = eval(graph, path, node) ; + Iter 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 output) - { + protected void doNegatedPropertySet(P_NegPropSet pathNotOneOf, Node node, Collection output) { // X !(:a|:b|^:c|^:d) Y = { X !(:a|:b) Y } UNION { Y !(:c|:d) X } - if ( pathNotOneOf.getFwdNodes().size() > 0 ) - { - Iterator nodes1 = stepExcludeForwards(graph, node, pathNotOneOf.getFwdNodes()) ; + if ( pathNotOneOf.getFwdNodes().size() > 0 ) { + Iterator nodes1 = stepExcludeForwards(node, pathNotOneOf.getFwdNodes()) ; fillUnique(nodes1, output) ; } - if ( pathNotOneOf.getBwdNodes().size() > 0 ) - { - Iterator nodes2 = stepExcludeBackwards(graph, node, pathNotOneOf.getBwdNodes()) ; + if ( pathNotOneOf.getBwdNodes().size() > 0 ) { + Iterator nodes2 = stepExcludeBackwards(node, pathNotOneOf.getBwdNodes()) ; fillUnique(nodes2, output) ; } } @Override - protected void doZeroOrOne(Path pathStep, Node node, Collection output) - { - eval(graph, pathStep, node, output) ; - if ( ! output.contains(node) ) + protected void doZeroOrOne(Path pathStep, Node node, Collection output) { + eval(pathStep, node, output) ; + if ( !output.contains(node) ) output.add(node) ; } @Override - protected void doZero(Path path, Node node, Collection output) - { + protected void doZero(Path path, Node node, Collection 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 collector() { return new ArrayList() ; } + protected Collection collector() { + return new ArrayList() ; + } @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 output) - { + protected void doNegatedPropertySet(P_NegPropSet pathNotOneOf, Node node, Collection output) { // X !(:a|:b|^:c|^:d) Y = { X !(:a|:b) Y } UNION { Y !(:c|:d) X } - if ( pathNotOneOf.getFwdNodes().size() > 0 ) - { - Iterator nodes1 = stepExcludeForwards(graph, node, pathNotOneOf.getFwdNodes()) ; + if ( pathNotOneOf.getFwdNodes().size() > 0 ) { + Iterator nodes1 = stepExcludeForwards(node, pathNotOneOf.getFwdNodes()) ; fill(nodes1, output) ; } - if ( pathNotOneOf.getBwdNodes().size() > 0 ) - { - Iterator nodes2 = stepExcludeBackwards(graph, node, pathNotOneOf.getBwdNodes()) ; + if ( pathNotOneOf.getBwdNodes().size() > 0 ) { + Iterator nodes2 = stepExcludeBackwards(node, pathNotOneOf.getBwdNodes()) ; fill(nodes2, output) ; } } @Override - protected void doAlt(Path pathStepLeft, Path pathStepRight, Node node, Collection output) - { + protected void doAlt(Path pathStepLeft, Path pathStepRight, Node node, Collection output) { // Try both sizes, accumulate into output. - Iterator iter = eval(graph, pathStepLeft, node) ; + Iterator 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 output) - { + protected void doSeq(Path pathStepLeft, Path pathStepRight, Node node, Collection output) { Path part1 = forwardMode ? pathStepLeft : pathStepRight ; Path part2 = forwardMode ? pathStepRight : pathStepLeft ; // Feed one side into the other - Iter iter = eval(graph, part1, node) ; - for ( Node n : iter ) - eval(graph, part2, n, output) ; + Iter 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 output) - { + protected void doMultiLengthPath(Path pathStep, Node node, long min1, long max1, Collection 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 iter = eval(graph, p1, node) ; + Iterator iter = eval(p1, node) ; // Moved on one step - for ( ; iter.hasNext() ; ) - { + for (; iter.hasNext();) { Node n2 = iter.next() ; - Iterator iter2 = eval(graph, p2, n2) ; + Iterator 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 output) - { - if ( fixedLength == 0 ) - { + protected void doFixedLengthPath(Path pathStep, Node node, long fixedLength, Collection output) { + if ( fixedLength == 0 ) { output.add(node) ; return ; } // P_Mod(path, count, count) // One step. - Iterator iter = eval(graph, pathStep, node) ; + Iterator 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 iter2 = eval(graph, nextPath, n2) ; + Iterator iter2 = eval(nextPath, n2) ; fill(iter2, output) ; } } @Override - protected void doZeroOrOne(Path pathStep, Node node, Collection output) - { + protected void doZeroOrOne(Path pathStep, Node node, Collection output) { doZero(pathStep, node, output) ; doOne(pathStep, node, output) ; } - private void doOne(Path path, Node node, Collection output) - { - Iterator iter = eval(graph, path, node) ; + private void doOne(Path path, Node node, Collection output) { + Iterator iter = eval(path, node) ; fill(iter, output) ; } @Override - protected void doZero(Path path, Node node, Collection output) - { + protected void doZero(Path path, Node node, Collection output) { output.add(node) ; } @Override - protected void doZeroOrMore(Path path, Node node, Collection output) - { + protected void doZeroOrMore(Path path, Node node, Collection output) { Set visited = new HashSet() ; ALP(node, path, visited, output) ; } @Override - protected void doOneOrMore(Path path, Node node, Collection output) - { + protected void doOneOrMore(Path path, Node node, Collection output) { Set visited = new HashSet() ; // Do one step without including. - Iterator iter1 = eval(graph, path, node) ; - for ( ; iter1.hasNext() ; ) - { - Node n1 = iter1.next(); + Iterator 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 visited, Collection output) - { - if ( visited.contains(node) ) return ; + private void ALP(Node node, Path path, Collection visited, Collection 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 iter1 = eval(graph, path, node) ; + Iterator 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 output) - { + protected void doSeq(Path pathStepLeft, Path pathStepRight, Node node, Collection output) { Path part1 = forwardMode ? pathStepLeft : pathStepRight ; Path part2 = forwardMode ? pathStepRight : pathStepLeft ; - + // Feed one side into the other - Iter iter = eval(graph, part1, node) ; - for ( Node n : iter ) - eval(graph, part2, n, output) ; + Iter iter = eval(part1, node) ; + for (Node n : iter) + eval(part2, n, output) ; } @Override - protected void doAlt(Path pathStepLeft, Path pathStepRight, Node node, Collection output) - { + protected void doAlt(Path pathStepLeft, Path pathStepRight, Node node, Collection output) { // Try both sizes, accumulate into output. - Iterator iter = eval(graph, pathStepLeft, node) ; + Iterator 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 output) - { - if ( pathNotOneOf.getFwdNodes().size() > 0 ) - { - Iterator nodes1 = stepExcludeForwards(graph, node, pathNotOneOf.getFwdNodes()) ; + protected void doNegatedPropertySet(P_NegPropSet pathNotOneOf, Node node, Collection output) { + if ( pathNotOneOf.getFwdNodes().size() > 0 ) { + Iterator nodes1 = stepExcludeForwards(node, pathNotOneOf.getFwdNodes()) ; fill(nodes1, output) ; } - if ( pathNotOneOf.getBwdNodes().size() > 0 ) - { - Iterator nodes2 = stepExcludeBackwards(graph, node, pathNotOneOf.getBwdNodes()) ; + if ( pathNotOneOf.getBwdNodes().size() > 0 ) { + Iterator nodes2 = stepExcludeBackwards(node, pathNotOneOf.getBwdNodes()) ; fill(nodes2, output) ; } } @Override - protected void doZeroOrOne(Path pathStep, Node node, Collection output) - { + protected void doZeroOrOne(Path pathStep, Node node, Collection output) { // Force unique evaluation. Collection x = new HashSet() ; - eval(graph, pathStep, node, x) ; + eval(pathStep, node, x) ; x.add(node) ; output.addAll(x) ; } @Override - protected void doZeroOrMore(Path pathStep, Node node, Collection output) - { + protected void doZeroOrMore(Path pathStep, Node node, Collection output) { // Reuse "output" - Collection visited = new LinkedList() ; //new HashSet() ; - ALP_1(graph, forwardMode, 0, -1, node, pathStep, visited) ; + Collection visited = new LinkedList() ; // new + // HashSet() ; + ALP_1(forwardMode, 0, -1, node, pathStep, visited) ; output.addAll(visited) ; } @Override - protected void doOneOrMore(Path pathStep, Node node, Collection output) - { + protected void doOneOrMore(Path pathStep, Node node, Collection output) { // Reuse "output" - Collection visited = new LinkedList() ; // new HashSet() ; + Collection visited = new LinkedList() ; // new + // HashSet() ; // Do one step without including. - //TODO switch to PathEngine1 for the sub-step as we only need uniques. - Iter 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 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 output) - { + protected void doZero(Path path, Node node, Collection 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 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 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 iter1 = eval(graph, path, node) ; + Iter 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 output) - { + protected void doZeroOrMoreN(Path pathStep, Node node, Collection output) { Set visited = new HashSet() ; ALP_N(node, pathStep, visited, output) ; } @Override - protected void doOneOrMoreN(Path pathStep, Node node, Collection output) - { + protected void doOneOrMoreN(Path pathStep, Node node, Collection output) { Set visited = new HashSet() ; // Do one step without including. - Iterator iter1 = eval(graph, pathStep, node) ; - for ( ; iter1.hasNext() ; ) - { - Node n1 = iter1.next(); + Iterator 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 visited, Collection 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 visited, Collection 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 iter1 = eval(graph, path, node) ; + + Iterator 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 output) - { + protected void doMultiLengthPath(Path pathStep, Node node, long min1, long max1, Collection 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 iter = eval(graph, p1, node) ; + Iterator iter = eval(p1, node) ; // Moved on one step - for ( ; iter.hasNext() ; ) - { + for (; iter.hasNext();) { Node n2 = iter.next() ; - Iterator iter2 = eval(graph, p2, n2) ; + Iterator 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 output) - { + protected void doFixedLengthPath(Path pathStep, Node node, long fixedLength, Collection output) { // Not SPARQL - if ( fixedLength == 0 ) - { + if ( fixedLength == 0 ) { output.add(node) ; return ; } // P_Mod(path, count, count) // One step. - Iterator iter = eval(graph, pathStep, node) ; + Iterator 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 iter2 = eval(graph, nextPath, n2) ; + Iterator 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 collector() { return new ArrayList() ; } - + protected Collection collector() { + return new ArrayList() ; + } } - 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 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 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 evalN(Graph graph, Node node, Path path) - { + /** Evaluate a path : SPARQL semantics */ + static public Iterator 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 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 evalN(Graph graph, Node node, Path path) { return eval$(graph, node, path, new PathEngineN(graph, true)) ; } - /** Evaluate a path : counting semantics */ - static public Iterator evalReverseN(Graph graph, Node node, Path path) - { + /** Evaluate a path : counting semantics */ + static public Iterator evalReverseN(Graph graph, Node node, Path path) { return eval$(graph, node, path, new PathEngineN(graph, false)) ; } - /** Evaluate a path : unique results */ - static public Iterator eval1(Graph graph, Node node, Path path) - { + /** Evaluate a path : unique results */ + static public Iterator eval1(Graph graph, Node node, Path path) { return eval$(graph, node, path, new PathEngine1(graph, true)) ; } - /** Evaluate a path : unique results */ - static public Iterator evalReverse1(Graph graph, Node node, Path path) - { + /** Evaluate a path : unique results */ + static public Iterator 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 acc) - { + /** Evaluate a path */ + static void eval$(Graph graph, Node node, Path path, PathEngine engine, Collection acc) { PathEvaluator evaluator = new PathEvaluator(graph, node, acc, engine) ; path.visit(evaluator) ; } - /** Evaluate a path */ - static Iter eval$(Graph graph, Node node, Path path, PathEngine engine) - { + /** Evaluate a path */ + static Iter eval$(Graph graph, Node node, Path path, PathEngine engine) { Collection acc = new ArrayList() ; 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 output ; - private PathEngine engine ; - - protected PathEvaluator(Graph g, Node n, Collection output, PathEngine engine) - { - this.graph = g ; + private PathEngine engine ; + + protected PathEvaluator(Graph g, Node n, Collection output, PathEngine engine) { + this.graph = g ; this.node = n ; this.output = output ; this.engine = engine ; } - - // Overrides - - // Work functions. - - protected final void fill(Iterator iter) - { - for ( ; iter.hasNext() ; ) + + protected final void fill(Iterator 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 nodes = engine.doOne(graph, node, pathNode.getNode()) ; + public void visit(P_Link pathNode) { + Iterator nodes = engine.doOne(node, pathNode.getNode()) ; fill(nodes) ; } - + @Override - public void visit(P_ReverseLink pathNode) - { + public void visit(P_ReverseLink pathNode) { engine.flipDirection() ; - Iterator nodes = engine.doOne(graph, node, pathNode.getNode()) ; + Iterator 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 { private Collection excludes ; - public FilterExclude(Collection excludes) { this.excludes = excludes ; } + + public FilterExclude(Collection 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 between(Node x, Node z) - { - Iter iter1 = Iter.iter(graph.find(x, Node.ANY, z)) ; - return iter1 ; + + final protected Iter between(Node x, Node z) { + return Iter.iter(engine.graphFind(x, Node.ANY, z)) ; } - - final protected void doZero(Path path, Node node, Collection output) - { + final protected void doZero(Path path, Node node, Collection 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 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 results = Iter.toList(resultsIter) ; List 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 nodes1 = Iter.toList(PathEval.eval(graph, start, PathFactory.pathDistinct(path))) ; - List nodes2 = Iter.toList(PathEval.eval(graph, start, path)) ; + List nodes1 = Iter.toList(PathEval.eval(graph, start, PathFactory.pathDistinct(path), ARQ.getContext() )) ; + List nodes2 = Iter.toList(PathEval.eval(graph, start, path, ARQ.getContext() )) ; List expected = new ArrayList() ; for ( String n : results ) expected.add(parse(n)) ;