jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [45/51] [partial] jena git commit: Rename com.hp.hpl.jena.* as org.apache.jena.*
Date Thu, 23 Apr 2015 19:57:01 GMT
http://git-wip-us.apache.org/repos/asf/jena/blob/56534eac/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/AlgebraGenerator.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/AlgebraGenerator.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/AlgebraGenerator.java
deleted file mode 100644
index ca2d36d..0000000
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/AlgebraGenerator.java
+++ /dev/null
@@ -1,676 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hp.hpl.jena.sparql.algebra;
-
-import java.util.* ;
-
-import org.apache.jena.atlas.lib.Pair ;
-import org.apache.jena.atlas.logging.Log ;
-
-import com.hp.hpl.jena.graph.Node ;
-import com.hp.hpl.jena.graph.Triple ;
-import com.hp.hpl.jena.query.ARQ ;
-import com.hp.hpl.jena.query.Query ;
-import com.hp.hpl.jena.query.SortCondition ;
-import com.hp.hpl.jena.sparql.ARQInternalErrorException ;
-import com.hp.hpl.jena.sparql.algebra.op.* ;
-import com.hp.hpl.jena.sparql.algebra.optimize.TransformSimplify ;
-import com.hp.hpl.jena.sparql.core.* ;
-import com.hp.hpl.jena.sparql.engine.binding.Binding ;
-import com.hp.hpl.jena.sparql.expr.* ;
-import com.hp.hpl.jena.sparql.path.PathLib ;
-import com.hp.hpl.jena.sparql.syntax.* ;
-import com.hp.hpl.jena.sparql.util.Context ;
-import com.hp.hpl.jena.sparql.util.Utils ;
-
-/**
- * Class used to compile SPARQL queries into SPARQL algebra
- *
- */
-public class AlgebraGenerator 
-{
-    // Fixed filter position means leave exactly where it is syntactically (illegal SPARQL)
-    // Helpful only to write exactly what you mean and test the full query compiler.
-    private boolean fixedFilterPosition = false ;
-    private Context context ;
-    private final int subQueryDepth ;
-    
-    // simplifyInAlgebraGeneration=true is the alternative reading of
-    // the DAWG Algebra translation algorithm. 
-
-    // If we simplify during algebra generation, it changes the SPARQL for OPTIONAL {{ FILTER }}
-    // The  {{}} results in (join unit (filter ...)) the filter is not moved
-    // into the LeftJoin.  
-    
-    static final private boolean applySimplification = true ;                   // False allows raw algebra to be generated (testing) 
-    static final private boolean simplifyTooEarlyInAlgebraGeneration = false ;  // False is the correct setting. 
-
-    /**
-     * Create a new generator
-     * @param context Context
-     */
-    public AlgebraGenerator(Context context)
-    {
-        this (context != null ? context : ARQ.getContext().copy(), 0) ;
-    }
-    
-    /**
-     * Create a new generator
-     */
-    public AlgebraGenerator() { this(null) ; } 
-    
-    protected AlgebraGenerator(Context context, int depth)
-    {
-        this.context = context ;
-        this.subQueryDepth = depth ;
-    }
-    
-    //-- Public operations.  Do not call recursively (call compileElement).
-    // These operations apply the simplification step which is done, once, at the end.
-    
-    /**
-     * Compile a query
-     * <p>
-     * <strong>DO NOT</strong> call recursively
-     * </p>
-     * @param query Query to compile
-     * @return Compiled algebra
-     */
-    public Op compile(Query query)
-    {
-        Op op = compile(query.getQueryPattern()) ;     // Not compileElement - may need to apply simplification.
-        
-        op = compileModifiers(query, op) ;
-        return op ;
-    }
-    
-    protected static Transform simplify = new TransformSimplify() ;
-    
-    /**
-     * Compile any structural element
-     * @param elt Element
-     * @return Compiled algebra
-     */
-    public Op compile(Element elt)
-    {
-        Op op = compileElement(elt) ;
-        Op op2 = op ;
-        if ( ! simplifyTooEarlyInAlgebraGeneration && applySimplification && simplify != null )
-            op2 = simplify(op) ;
-        return op2;
-    }
-    
-    protected static Op simplify(Op op)
-    {
-        return Transformer.transform(simplify, op) ;
-    }
-
-    // This is the operation to call for recursive application.
-    protected Op compileElement(Element elt)
-    {
-        if ( elt instanceof ElementGroup )
-            return compileElementGroup((ElementGroup)elt) ;
-      
-        if ( elt instanceof ElementUnion )
-            return compileElementUnion((ElementUnion)elt) ;
-      
-        if ( elt instanceof ElementNamedGraph )
-            return compileElementGraph((ElementNamedGraph)elt) ; 
-      
-        if ( elt instanceof ElementService )
-            return compileElementService((ElementService)elt) ; 
-        
-        // This is only here for queries built programmatically
-        // (triple patterns not in a group) 
-        if ( elt instanceof ElementTriplesBlock )
-            return compileBasicPattern(((ElementTriplesBlock)elt).getPattern()) ;
-        
-        // Ditto.
-        if ( elt instanceof ElementPathBlock )
-            return compilePathBlock(((ElementPathBlock)elt).getPattern()) ;
-
-        if ( elt instanceof ElementSubQuery )
-            return compileElementSubquery((ElementSubQuery)elt) ; 
-        
-        if ( elt instanceof ElementData )
-            return compileElementData((ElementData)elt) ; 
-
-        if ( elt == null )
-            return OpNull.create() ;
-
-        return compileUnknownElement(elt, "compile(Element)/Not a structural element: "+Utils.className(elt));
-    }
-    
-    /**
-     * Future extension point to allow the algebra generator to be more easily extended to understand new {@link Element} implementations
-     * that user defined language extensions may introduce.
-     * <p>
-     * This default implementation will throw an error
-     * </p>
-     * @param elt Element
-     * @param error Error message if unable to compile the given element type
-     * @return Algebra
-     */
-    protected Op compileUnknownElement(Element elt, String error)
-    {
-        broken(error) ;
-        return null;
-    }
-    
-    //Produce the algebra for a single group.
-    //<a href="http://www.w3.org/TR/rdf-sparql-query/#sparqlQuery">Translation to the SPARQL Algebra</a>
-    //
-    // Step : (URI resolving and triple pattern syntax forms) was done during parsing
-    // Step : Collection FILTERS [prepareGroup]
-    // Step : (Paths) e.g. simple links become triple patterns. Done later in [compileOneInGroup]
-    // Step : (BGPs) Merge PathBlocks - these are SPARQL 1.1's TriplesBlock   [prepareGroup]
-    // Step : (BIND/LET) Associate with BGP [??]
-    // Step : (Groups and unions) Was done during parsing to get ElementUnion.
-    // Step : Graph Patterns [compileOneInGroup]
-    // Step : Filters [here]
-    // Simplification: Done later 
-    // If simplification is done now, it changes OPTIONAL { { ?x :p ?w . FILTER(?w>23) } } because it removes the
-    //   (join Z (filter...)) that in turn stops the filter getting moved into the LeftJoin.  
-    //   It need a depth of 2 or more {{ }} for this to happen. 
-    
-    protected Op compileElementGroup(ElementGroup groupElt)
-    {
-        Pair<List<Expr>, List<Element>> pair = prepareGroup(groupElt) ;
-        List<Expr> filters = pair.getLeft() ;
-        List<Element> groupElts = pair.getRight() ;
-
-        // Compile the consolidated group elements.
-        // "current" is the completed part only - there may be thing pushed into the accumulator.
-        Op current = OpTable.unit() ;
-        Deque<Op> acc = new ArrayDeque<>() ;
-
-        for ( Element elt : groupElts )
-        {
-            if ( elt != null )
-            {
-                current = compileOneInGroup( elt, current, acc );
-            }
-        }
-        
-        // Deal with any remaining ops.
-        //current = joinOpAcc(current, acc) ;
-        
-        if ( filters != null )
-        {
-            // Put filters round the group.
-            for ( Expr expr : filters )
-                current = OpFilter.filter(expr, current) ;
-        }
-        return current ;
-    }
-
-    /* Extract filters, merge adjacent BGPs, do BIND.
-     * When extracting filters, BGP or PathBlocks may become adjacent
-     * so merge them into one. 
-     * Return a list of elements with any filters at the end. 
-     */
-    
-    protected Pair<List<Expr>, List<Element>> prepareGroup(ElementGroup groupElt)
-    {
-        List<Element> groupElts = new ArrayList<>() ;
-        
-        PathBlock currentPathBlock = null ;
-        List<Expr> filters = null ;
-        
-        for (Element elt : groupElt.getElements() )
-        {
-            if ( ! fixedFilterPosition && elt instanceof ElementFilter )
-            {
-                // For fixed position filters, drop through to general element processing.
-                // It's also illegal SPARQL - filters operate over the whole group.
-                ElementFilter f = (ElementFilter)elt ;
-                if ( filters == null )
-                    filters = new ArrayList<>() ;
-                filters.add(f.getExpr()) ;
-                // Collect filters but do not place them yet.
-                continue ;
-            }
-
-            // The parser does not generate ElementTriplesBlock (SPARQL 1.1) 
-            // but SPARQL 1.0 does and also we cope for programmatically built queries
-            
-            if ( elt instanceof ElementTriplesBlock )
-            {
-                ElementTriplesBlock etb = (ElementTriplesBlock)elt ;
-
-                if ( currentPathBlock == null )
-                {
-                    ElementPathBlock etb2 = new ElementPathBlock() ;
-                    currentPathBlock = etb2.getPattern() ;
-                    groupElts.add(etb2) ;
-                }
-                
-                for ( Triple t : etb.getPattern())
-                    currentPathBlock.add(new TriplePath(t)) ;
-                continue ;
-            }
-            
-            // To PathLib
-            
-            if ( elt instanceof ElementPathBlock )
-            {
-                ElementPathBlock epb = (ElementPathBlock)elt ;
-                
-                if ( currentPathBlock == null )
-                {
-                    ElementPathBlock etb2 = new ElementPathBlock() ;
-                    currentPathBlock = etb2.getPattern() ;
-                    groupElts.add(etb2) ;
-                }
-
-                currentPathBlock.addAll(epb.getPattern()) ;
-                continue ;
-            }
-            
-            // else
-            
-            // Not BGP, path or filters.
-            // Clear any BGP-related triple accumulators.
-            currentPathBlock = null ;
-            // Add this element
-            groupElts.add(elt) ;
-        }
-        return Pair.create(filters, groupElts) ;
-    }
-    
-//    /** Flush the op accumulator - and clear it */
-//    private void accumulate(Deque<Op> acc, Op op) { acc.addLast(op) ; }
-//
-//    /** Accumulate stored ops, return unit if none. */
-//    private Op popAccumulated(Deque<Op> acc)
-//    {
-//        if ( acc.size() == 0 )
-//            return OpTable.unit() ; 
-//        
-//        Op joined = null ;
-//        // First first to last.
-//        for ( Op op : acc )
-//            joined = OpJoin.create(joined,op) ;
-//        acc.clear() ;
-//        return joined ; 
-//    }
-//    
-//    /** Join stored ops to the current state */
-//    private Op joinOpAcc(Op current, Deque<Op> acc)
-//    {
-//        if ( acc.size() == 0 ) return current ;
-//        Op joined = current ;
-//        // First first to last.
-//        for ( Op op : acc )
-//            joined = OpJoin.create(joined,op) ;
-//        acc.clear() ;
-//        return joined ; 
-//    }
-    
-    protected Op compileOneInGroup(Element elt, Op current, Deque<Op> acc)
-    {
-//            // Coming into the general block.
-//            if ( elt instanceof ElementTriplesBlock )
-//            {
-//                ElementTriplesBlock etb = (ElementTriplesBlock)elt ;
-//                Op op = compileBasicPattern(etb.getPattern()) ;
-//                return join(current, op) ;
-//            }
-//
-//            if ( elt instanceof ElementPathBlock )
-//            {
-//                ElementPathBlock epb = (ElementPathBlock)elt ;
-//                Op op = compilePathBlock(epb.getPattern()) ;
-//                return join(current, op) ;
-//            }
-//            
-        // Elements that group so far and evaluate over that. 
-        
-        if ( elt instanceof ElementAssign )
-        {
-            ElementAssign assign = (ElementAssign)elt ;
-            return OpAssign.assign(current, assign.getVar(), assign.getExpr()) ;
-        }
-
-        if ( elt instanceof ElementBind )
-        {
-            ElementBind bind = (ElementBind)elt ;
-            return OpExtend.create(current, bind.getVar(), bind.getExpr()) ;
-        }
-
-        if ( elt instanceof ElementOptional )
-        {
-            ElementOptional eltOpt = (ElementOptional)elt ;
-            return compileElementOptional(eltOpt, current) ;
-        }
-        
-        if ( elt instanceof ElementMinus )
-        {
-            ElementMinus elt2 = (ElementMinus)elt ;
-            Op op = compileElementMinus(current, elt2) ;
-            return op ;
-        }
-
-        // All elements that simply "join" into the algebra.
-        if ( elt instanceof ElementGroup        || 
-             elt instanceof ElementNamedGraph   ||
-             elt instanceof ElementService      ||
-             elt instanceof ElementUnion        || 
-             elt instanceof ElementSubQuery     ||
-             elt instanceof ElementData         ||
-             elt instanceof ElementTriplesBlock ||
-             elt instanceof ElementPathBlock
-            )
-        {
-            Op op = compileElement(elt) ;
-            return join(current, op) ;
-        }
-        
-        // Specials.
-        
-        if ( elt instanceof ElementExists )
-        {
-            ElementExists elt2 = (ElementExists)elt ;
-            Op op = compileElementExists(current, elt2) ;
-            return op ;
-        }
-        
-        if ( elt instanceof ElementNotExists )
-        {
-            ElementNotExists elt2 = (ElementNotExists)elt ;
-            Op op = compileElementNotExists(current, elt2) ;
-            return op ;
-        }
-        
-        // Filters were collected together by prepareGroup
-        // This only handles filters left in place by some magic. 
-        if ( elt instanceof ElementFilter )
-        {
-            ElementFilter f = (ElementFilter)elt ;
-            return OpFilter.filter(f.getExpr(), current) ;
-        }
-    
-//        // SPARQL 1.1 UNION -- did not make it into SPARQL 
-//        if ( elt instanceof ElementUnion )
-//        {
-//            ElementUnion elt2 = (ElementUnion)elt ;
-//            if ( elt2.getElements().size() == 1 )
-//            {
-//                Op op = compileElementUnion(current, elt2) ;
-//                return op ;
-//            }
-//        }
-        
-        
-        return compileUnknownElement(elt, "compile/Element not recognized: "+Utils.className(elt));
-    }
-
-    protected Op compileElementUnion(ElementUnion el)
-    { 
-        Op current = null ;
-        
-        for ( Element subElt: el.getElements() )
-        {
-            Op op = compileElement(subElt) ;
-            current = union(current, op) ;
-        }
-        return current ;
-    }
-
-    protected Op compileElementNotExists(Op current, ElementNotExists elt2)
-    {
-        Op op = compile(elt2.getElement()) ;    // "compile", not "compileElement" -- do simpliifcation  
-        Expr expr = new E_Exists(elt2, op) ;
-        expr = new E_LogicalNot(expr) ;
-        return OpFilter.filter(expr, current) ;
-    }
-
-    protected Op compileElementExists(Op current, ElementExists elt2)
-    {
-        Op op = compile(elt2.getElement()) ;    // "compile", not "compileElement" -- do simpliifcation 
-        Expr expr = new E_Exists(elt2, op) ;
-        return OpFilter.filter(expr, current) ;
-    }
-
-    protected Op compileElementMinus(Op current, ElementMinus elt2)
-    {
-        Op op = compile(elt2.getMinusElement()) ;
-        Op opMinus = OpMinus.create(current, op) ;
-        return opMinus ;
-    }
-
-    protected Op compileElementData(ElementData elt)
-    {
-        return OpTable.create(elt.getTable()) ;
-    }
-
-    protected Op compileElementUnion(Op current, ElementUnion elt2)
-    {
-        // Special SPARQL 1.1 case.
-        Op op = compile(elt2.getElements().get(0)) ;
-        Op opUnion = OpUnion.create(current, op) ;
-        return opUnion ;
-    }
-
-    protected Op compileElementOptional(ElementOptional eltOpt, Op current)
-    {
-        Element subElt = eltOpt.getOptionalElement() ;
-        Op op = compileElement(subElt) ;
-        
-        ExprList exprs = null ;
-        if ( op instanceof OpFilter )
-        {
-            OpFilter f = (OpFilter)op ;
-            //f = OpFilter.tidy(f) ;  // Collapse filter(filter(..))
-            Op sub = f.getSubOp() ;
-            if ( sub instanceof OpFilter )
-                broken("compile/Optional/nested filters - unfinished") ; 
-            exprs = f.getExprs() ;
-            op = sub ;
-        }
-        current = OpLeftJoin.create(current, op, exprs) ;
-        return current ;
-    }
-    
-    protected Op compileBasicPattern(BasicPattern pattern)
-    {
-        return new OpBGP(pattern) ;
-    }
-    
-    protected Op compilePathBlock(PathBlock pathBlock)
-    {
-        // Empty path block : the parser does not generate this case.
-        if ( pathBlock.size() == 0 )
-            return OpTable.unit() ;
-
-        // Always turns the most basic paths to triples.
-        return PathLib.pathToTriples(pathBlock) ;
-    }
-
-    protected Op compileElementGraph(ElementNamedGraph eltGraph)
-    {
-        Node graphNode = eltGraph.getGraphNameNode() ;
-        Op sub = compileElement(eltGraph.getElement()) ;
-        return new OpGraph(graphNode, sub) ;
-    }
-
-    protected Op compileElementService(ElementService eltService)
-    {
-        Node serviceNode = eltService.getServiceNode() ;
-        Op sub = compileElement(eltService.getElement()) ;
-        return new OpService(serviceNode, sub, eltService, eltService.getSilent()) ;
-    }
-    
-    protected Op compileElementSubquery(ElementSubQuery eltSubQuery)
-    {
-        AlgebraGenerator gen = new AlgebraGenerator(context, subQueryDepth+1) ;
-        return gen.compile(eltSubQuery.getQuery()) ;
-    }
-    
-    /** Compile query modifiers */
-    protected Op compileModifiers(Query query, Op pattern)
-    {
-         /* The modifier order in algebra is:
-          * 
-          * Limit/Offset
-          *   Distinct/reduce
-          *     project
-          *       OrderBy
-          *         Bindings
-          *           having
-          *             select expressions
-          *               group
-          */
-        
-        // Preparation: sort SELECT clause into assignments and projects.
-        VarExprList projectVars = query.getProject() ;
-        
-        VarExprList exprs = new VarExprList() ;     // Assignments to be done.
-        List<Var> vars = new ArrayList<>() ;     // projection variables
-        
-        Op op = pattern ;
-        
-        // ---- GROUP BY
-        
-        if ( query.hasGroupBy() )
-        {
-            // When there is no GroupBy but there are some aggregates, it's a group of no variables.
-            op = new OpGroup(op, query.getGroupBy(), query.getAggregators()) ;
-        }
-        
-        //---- Assignments from SELECT and other places (so available to ORDER and HAVING)
-        // Now do assignments from expressions 
-        // Must be after "group by" has introduced it's variables.
-        
-        // Look for assignments in SELECT expressions.
-        if ( ! projectVars.isEmpty() && ! query.isQueryResultStar())
-        {
-            // Don't project for QueryResultStar so initial bindings show
-            // through in SELECT *
-            if ( projectVars.size() == 0 && query.isSelectType() )
-                Log.warn(this,"No project variables") ;
-            // Separate assignments and variable projection.
-            for ( Var v : query.getProject().getVars() )
-            {
-                Expr e = query.getProject().getExpr(v) ;
-                if ( e != null )
-                {
-                    Expr e2 = ExprLib.replaceAggregateByVariable(e) ;
-                    exprs.add(v, e2) ;
-                }
-                // Include in project
-                vars.add(v) ;
-            }
-        }
-        
-        // ---- Assignments from SELECT and other places (so available to ORDER and HAVING)
-        for ( Var v : exprs.getVars() ) {
-            Expr e = exprs.getExpr(v) ;
-            op = OpExtend.create(op, v, e) ;
-        }
-
-        // ---- HAVING
-        if ( query.hasHaving() )
-        {
-            for (Expr expr : query.getHavingExprs())
-            {
-                // HAVING expression to refer to the aggregate via the variable.
-                Expr expr2 = ExprLib.replaceAggregateByVariable(expr) ; 
-                op = OpFilter.filter(expr2 , op) ;
-            }
-        }
-        // ---- VALUES
-        if ( query.hasValues() )
-        {
-            Table table = TableFactory.create(query.getValuesVariables()) ;
-            for ( Binding binding : query.getValuesData() )
-                table.addBinding(binding) ;
-            OpTable opTable = OpTable.create(table) ;
-            op = OpJoin.create(op, opTable) ;
-        }
-        
-        // ---- ToList
-        if ( context.isTrue(ARQ.generateToList) )
-            // Listify it.
-            op = new OpList(op) ;
-        
-        // ---- ORDER BY
-        if ( query.getOrderBy() != null )
-        {
-            List<SortCondition> scList = new ArrayList<>() ;
-
-            // Aggregates in ORDER BY
-            for ( SortCondition sc : query.getOrderBy() )
-            {
-                Expr e = sc.getExpression() ;
-                e = ExprLib.replaceAggregateByVariable(e) ;
-                scList.add(new SortCondition(e, sc.getDirection())) ;
-                
-            }
-            op = new OpOrder(op, scList) ;
-        }
-        
-        // ---- PROJECT
-        // No projection => initial variables are exposed.
-        // Needed for CONSTRUCT and initial bindings + SELECT *
-        
-        if ( vars.size() > 0 )
-            op = new OpProject(op, vars) ;
-        
-        // ---- DISTINCT
-        if ( query.isDistinct() )
-            op = OpDistinct.create(op) ;
-        
-        // ---- REDUCED
-        if ( query.isReduced() )
-            op = OpReduced.create(op) ;
-        
-        // ---- LIMIT/OFFSET
-        if ( query.hasLimit() || query.hasOffset() )
-            op = new OpSlice(op, query.getOffset() /*start*/, query.getLimit()/*length*/) ;
-        
-        return op ;
-    }
-
-    // -------- 
-    
-    protected static Op join(Op current, Op newOp)
-    { 
-        if ( simplifyTooEarlyInAlgebraGeneration && applySimplification )
-            return OpJoin.createReduce(current, newOp) ;
-        
-        return OpJoin.create(current, newOp) ;
-    }
-
-    protected Op sequence(Op current, Op newOp)
-    {
-        return OpSequence.create(current, newOp) ;
-    }
-    
-    protected Op union(Op current, Op newOp)
-    {
-        return OpUnion.create(current, newOp) ;
-    }
-    
-    protected final void broken(String msg)
-    {
-        //System.err.println("AlgebraGenerator: "+msg) ;
-        throw new ARQInternalErrorException(msg) ;
-    }
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/56534eac/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/AlgebraQuad.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/AlgebraQuad.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/AlgebraQuad.java
deleted file mode 100644
index 08519c7..0000000
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/AlgebraQuad.java
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hp.hpl.jena.sparql.algebra;
-
-import java.util.ArrayDeque ;
-import java.util.Collection ;
-import java.util.Deque ;
-
-import com.hp.hpl.jena.graph.Node ;
-import com.hp.hpl.jena.sparql.ARQConstants ;
-import com.hp.hpl.jena.sparql.algebra.op.* ;
-import com.hp.hpl.jena.sparql.core.Quad ;
-import com.hp.hpl.jena.sparql.core.Var ;
-import com.hp.hpl.jena.sparql.core.VarAlloc ;
-
-/** 
- * Helper class for converting an algebra expression into a quad form 
- * */
-public class AlgebraQuad
-{
-    // Transform to a quad form:
-    //   + BGPs go to quad patterns
-    //   + Drop (previous) OpGraph 
-    //   + Paths (complex - simple ones are flatten elsewhere) go to (graph (path ...)) [later: quad paths] 
-
-    // Done as a before/after pair to run the stack of graph nodes for rewrite.
-    // Need to be careful of use of a variable in GRAPH ?g { .. } and then use ?g inside the pattern.
-    
-    private AlgebraQuad() { }
-
-    public static Op quadize(Op op)
-    {
-        final Deque<QuadSlot> stack = new ArrayDeque<>() ;
-        QuadSlot qSlot = new QuadSlot(Quad.defaultGraphNodeGenerated, Quad.defaultGraphNodeGenerated) ;  
-        stack.push(qSlot) ;             // Starting condition
-        
-        OpVisitor before = new Pusher(stack) ;
-        OpVisitor after = new Popper(stack) ;
-        
-        TransformQuadGraph qg = new TransformQuadGraph(stack, before, after) ;
-        return Transformer.transformSkipService(qg, op, before, after) ;
-    }
-    
-    /** This is the record of the transformation.
-     *  The rewriteGraphName is the node to put in the graph slot of the quad.
-     *  The actualGraphName is the node used in SPARQL.
-     *  If they are the same (by ==), the quadrewrite is OK as is.
-     *  If they are different (and that means they are variables)
-     *  an assign is done after the execution of the graph pattern block. 
-     */
-    static class QuadSlot
-    {   // Oh scala, where art thou!
-        final Node actualGraphName ;
-        final Node rewriteGraphName ;
-        QuadSlot(Node actualGraphName, Node rewriteGraphName)
-        {
-            this.actualGraphName = actualGraphName ;
-            this.rewriteGraphName = rewriteGraphName ;
-        }
-        @Override public String toString() { return "actualGraphName="+actualGraphName+" rewriteGraphName="+rewriteGraphName ; } 
-    }
-    
-    private static class Pusher extends OpVisitorBase
-    {
-        Deque<QuadSlot> stack ;
-        VarAlloc varAlloc = new VarAlloc(ARQConstants.allocVarQuad) ;
-        Pusher(Deque<QuadSlot> stack) { this.stack = stack ; }
-        @Override
-        public void visit(OpGraph opGraph)
-        {
-            // Name in SPARQL
-            Node gn = opGraph.getNode() ;
-            // Name in rewrite
-            Node gnQuad = gn ;
-            
-            if ( Var.isVar(gn) )
-            {
-                Collection<Var> vars = OpVars.mentionedVars(opGraph.getSubOp()) ;
-                if ( vars.contains(gn) )
-                    gnQuad = varAlloc.allocVar() ;
-            }
-            stack.push(new QuadSlot(gn, gnQuad)) ;
-        }
-    }
-    
-    private static class Popper extends OpVisitorBase
-    {
-        Deque<QuadSlot> stack ;
-        Popper(Deque<QuadSlot> stack) { this.stack = stack ; }
-        @Override
-        public void visit(OpGraph opGraph)
-        {
-            // The final work is done in the main vistor, 
-            // which is called after the subnode has been 
-            // rewritten.
-            stack.pop() ;
-        }
-    }    
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/56534eac/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/BeforeAfterVisitor.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/BeforeAfterVisitor.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/BeforeAfterVisitor.java
deleted file mode 100644
index 312186c..0000000
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/BeforeAfterVisitor.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hp.hpl.jena.sparql.algebra;
-
-import com.hp.hpl.jena.sparql.algebra.op.* ;
-
-public class BeforeAfterVisitor extends OpVisitorByType//implements OpVisitor
-{
-    OpVisitor beforeVisitor = null ;
-    OpVisitor afterVisitor = null ;
-    OpVisitor mainVisitor = null ;
-    
-    public BeforeAfterVisitor(OpVisitor mainVisitor ,
-                              OpVisitor beforeVisitor, 
-                              OpVisitor afterVisitor) 
-    {
-        this.mainVisitor = mainVisitor ;
-        this.beforeVisitor = beforeVisitor ;
-        this.afterVisitor = afterVisitor ;
-    }
-    
-    private void before(Op op) { 
-        if ( beforeVisitor != null )
-            op.visit(beforeVisitor) ;
-    }
-
-    private void after(Op op) {
-        if ( afterVisitor != null )
-            op.visit(afterVisitor) ;
-    }
-
-    @Override
-    protected void visit0(Op0 op) { 
-        before(op) ; op.visit(mainVisitor) ; after(op) ;
-    }
-
-    @Override
-    protected void visit1(Op1 op) { 
-        before(op) ; op.visit(mainVisitor) ; after(op) ;
-    }
-
-    @Override
-    protected void visit2(Op2 op) { 
-        before(op) ; op.visit(mainVisitor) ; after(op) ;
-    }
-
-    @Override
-    protected void visitN(OpN op) { 
-        before(op) ; op.visit(mainVisitor) ; after(op) ;
-    }
-
-    @Override
-    protected void visitFilter(OpFilter op) { 
-        before(op) ; op.visit(mainVisitor) ; after(op) ;
-    }
-
-    @Override
-    protected void visitLeftJoin(OpLeftJoin op) { 
-        before(op) ; op.visit(mainVisitor) ; after(op) ;
-    }
-
-    @Override
-    protected void visitExt(OpExt op) { 
-        before(op) ; op.visit(mainVisitor) ; after(op) ;
-    }
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/56534eac/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/JoinType.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/JoinType.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/JoinType.java
deleted file mode 100644
index 45418cb..0000000
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/JoinType.java
+++ /dev/null
@@ -1,21 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hp.hpl.jena.sparql.algebra;
-
-public enum JoinType { PLAIN, LEFT /*, RIGHT , /*FULL* /OUTER*/}

http://git-wip-us.apache.org/repos/asf/jena/blob/56534eac/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/Op.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/Op.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/Op.java
deleted file mode 100644
index 434e174..0000000
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/Op.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hp.hpl.jena.sparql.algebra;
-
-import org.apache.jena.atlas.io.Printable ;
-
-import com.hp.hpl.jena.sparql.util.Named ;
-import com.hp.hpl.jena.sparql.util.NodeIsomorphismMap ;
-import com.hp.hpl.jena.sparql.util.PrintSerializable ;
-
-public interface Op extends PrintSerializable, Printable, Named
-{
-    public void visit(OpVisitor opVisitor) ;
-    public boolean equalTo(Op other, NodeIsomorphismMap labelMap) ;
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/56534eac/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpAsQuery.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpAsQuery.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpAsQuery.java
deleted file mode 100644
index 5b41c1e..0000000
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpAsQuery.java
+++ /dev/null
@@ -1,727 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hp.hpl.jena.sparql.algebra;
-
-import java.util.* ;
-
-import org.apache.jena.atlas.lib.NotImplemented ;
-
-import com.hp.hpl.jena.graph.Node ;
-import com.hp.hpl.jena.graph.Triple ;
-import com.hp.hpl.jena.query.Query ;
-import com.hp.hpl.jena.query.QueryFactory ;
-import com.hp.hpl.jena.query.SortCondition ;
-import com.hp.hpl.jena.query.Syntax ;
-import com.hp.hpl.jena.sparql.ARQInternalErrorException ;
-import com.hp.hpl.jena.sparql.ARQNotImplemented ;
-import com.hp.hpl.jena.sparql.algebra.op.* ;
-import com.hp.hpl.jena.sparql.core.BasicPattern ;
-import com.hp.hpl.jena.sparql.core.Quad ;
-import com.hp.hpl.jena.sparql.core.Var ;
-import com.hp.hpl.jena.sparql.core.VarExprList ;
-import com.hp.hpl.jena.sparql.engine.QueryIterator ;
-import com.hp.hpl.jena.sparql.expr.* ;
-import com.hp.hpl.jena.sparql.pfunction.PropFuncArg ;
-import com.hp.hpl.jena.sparql.syntax.* ;
-import com.hp.hpl.jena.sparql.util.graph.GraphList ;
-import com.hp.hpl.jena.vocabulary.RDF ;
-
-/** Convert an Op expression in SPARQL syntax, that is, the reverse of algebra generation */   
-public class OpAsQuery
-{
-    public static Query asQuery(Op op)
-    {        
-        Converter converter = new Converter(op) ;
-        return converter.convert();
-    }
-    
-    private static Set<Var> allocProjectVars()
-    {
-        return new LinkedHashSet<>() ;
-    }
-
-    private static void addProjectVar(Collection<Var> vars, Var var)
-    {
-        // Must add uniquely.  Couple to allocProjectVars
-        //if (!vars.contains(var)) 
-        vars.add(var) ;
-    }
-    
-    public static class Converter implements OpVisitor
-    {
-        private Query query ;
-        private Op op ;
-        private Element element = null ;
-        private ElementGroup currentGroup = null ;
-        private Deque<ElementGroup> stack = new ArrayDeque<>() ;
-        private Collection<Var> projectVars = allocProjectVars();
-        private Map<Var, Expr> varExpression = new HashMap<>() ;
-        private int groupDepth = 0;
-        private boolean inProject = false;
-        private boolean hasRun = false;
-        
-        public Converter(Op op)
-        {
-            this.query = QueryFactory.create() ;
-            this.op = op;
-            currentGroup = new ElementGroup() ;
-        }
-        
-        Query convert() {
-            if (hasRun) {
-                return this.query;
-            } else {
-                try {
-                    op.visit(this) ;
-                    
-                    Collection<Var> vars = this.projectVars;
-                    query.setQueryResultStar(vars.isEmpty());   // SELECT * unless we are projecting
-        
-                    Iterator<Var> iter = vars.iterator();
-                    for (; iter.hasNext();) {
-                        Var var = iter.next();           
-                        if (this.varExpression.containsKey(var))
-                            query.addResultVar(var, this.varExpression.get(var));
-                        else
-                            query.addResultVar(var);
-                    }
-                    
-                    ElementGroup eg = this.currentGroup ;                   
-                    query.setQueryPattern(eg) ;
-                    query.setQuerySelectType() ;
-                    query.setResultVars() ;                     // Variables from the group.
-                    return query ; 
-                } finally {
-                    this.hasRun = true;
-                }
-            }
-        }
-
-        Element asElement(Op op)
-        {
-            ElementGroup g = asElementGroup(op) ;
-            if ( g.getElements().size() == 1 )
-                return g.getElements().get(0) ;
-            return g ;
-        }
-        
-        ElementGroup asElementGroup(Op op)
-        {
-            startSubGroup() ;
-            op.visit(this) ;
-            return endSubGroup() ;
-        }
-
-        @Override
-        public void visit(OpBGP opBGP)
-        {
-            currentGroup().addElement(process(opBGP.getPattern())) ;
-        }
-
-//        public void visit(OpPropFunc opPropFunc)
-//        {
-//            OpBGP opBGP = opPropFunc.getBGP() ;
-//            currentGroup().addElement(process(opBGP.getPattern())) ;
-//        }
-        
-        @Override
-        public void visit(OpTriple opTriple)
-        { currentGroup().addElement(process(opTriple.getTriple())) ; }
-
-        @Override
-        public void visit(OpQuad opQuad)
-        { throw new ARQNotImplemented("OpQuad") ; }
-
-
-        @Override
-        public void visit(OpProcedure opProcedure)
-        {
-            throw new ARQNotImplemented("OpProcedure") ;
-        }
-        
-        @Override
-        public void visit(OpPropFunc opPropFunc)
-        {
-            Node s = processPropFuncArg(opPropFunc.getSubjectArgs()) ;
-            Node o = processPropFuncArg(opPropFunc.getObjectArgs()) ;
-            Triple t = new Triple(s, opPropFunc.getProperty(), o) ;
-            currentGroup().addElement(process(t)) ;
-        }
-        
-        private Node processPropFuncArg(PropFuncArg args)
-        {
-            if ( args.isNode() )
-                return args.getArg() ;
-
-            // List ...
-            List<Node> list = args.getArgList() ;
-            if ( list.size() == 0 )
-                return RDF.Nodes.nil ;
-            BasicPattern bgp = new BasicPattern() ;
-            Node head = GraphList.listToTriples(list, bgp) ;
-            currentGroup().addElement(process(bgp)) ;
-            return head ;
-        }
-        
-        // There is one special case to consider:
-        // A path expression was expaned into a OpSequence during Algenra generation.
-        // The simple path expressions become an OpSequence that could be recombined
-        // into on ElementPathBlock 
-        
-        @Override
-        public void visit(OpSequence opSequence)
-        {
-            ElementGroup g = currentGroup() ;
-            boolean nestGroup = ! g.isEmpty() ;
-            if ( nestGroup )
-            {
-                startSubGroup() ;
-                g = currentGroup() ;
-            }
-            
-            for ( Op op : opSequence.getElements() )
-            {
-                Element e = asElement(op) ;
-                g.addElement(e) ;
-            }
-            
-            if ( nestGroup )
-                endSubGroup() ;
-            return ;
-        }
-        
-        @Override
-        public void visit(OpDisjunction opDisjunction)
-        {
-            throw new ARQNotImplemented("OpDisjunction") ;
-        }
-
-        private Element process(BasicPattern pattern)
-        {
-            // The different SPARQL versions use different internal structures for BGPs.
-            if ( query.getSyntax() == Syntax.syntaxSPARQL_10 )
-            {
-                ElementTriplesBlock e = new ElementTriplesBlock() ;
-                for (Triple t : pattern)
-                    // Leave bNode variables as they are
-                    // Query serialization will deal with them. 
-                    e.addTriple(t) ;
-                return e ;
-            }
-            
-            if ( query.getSyntax() == Syntax.syntaxSPARQL_11 ||
-                 query.getSyntax() == Syntax.syntaxARQ )
-            {
-                ElementPathBlock e = new ElementPathBlock() ;
-                for (Triple t : pattern)
-                    // Leave bNode variables as they are
-                    // Query serialization will deal with them. 
-                    e.addTriple(t) ;
-                return e ;
-            }
-            
-            throw new ARQInternalErrorException("Unrecognized syntax: "+query.getSyntax()) ;
-            
-        }
-        
-        private ElementTriplesBlock process(Triple triple)
-        {
-            // Unsubtle
-            ElementTriplesBlock e = new ElementTriplesBlock() ;
-            e.addTriple(triple) ;
-            return e ;
-        }
-        
-        @Override
-        public void visit(OpQuadPattern quadPattern)
-        {
-            Node graphNode = quadPattern.getGraphNode();
-            if (graphNode.equals(Quad.defaultGraphNodeGenerated)) {
-                currentGroup().addElement(process(quadPattern.getBasicPattern())) ;
-            } else {
-                startSubGroup();
-                Element e = asElement(new OpBGP(quadPattern.getBasicPattern())) ;
-                endSubGroup();
-                
-                //If not element group make it one
-                if (!(e instanceof ElementGroup)) {
-                    ElementGroup g = new ElementGroup();
-                    g.addElement(e);
-                    e = g;
-                }
-                
-                Element graphElt = new ElementNamedGraph(graphNode, e) ;
-                currentGroup().addElement(graphElt) ;
-            }
-        }
-
-        @Override
-        public void visit(OpQuadBlock quadBlock)
-        {
-            // Gather into OpQuadPatterns.
-            throw new NotImplemented("OpQuadBlock") ;
-        }
-
-        @Override
-        public void visit(OpPath opPath)
-        {
-            ElementPathBlock epb = new ElementPathBlock() ;
-            epb.addTriplePath(opPath.getTriplePath()) ;
-            ElementGroup g = currentGroup() ;
-            g.addElement(epb) ;
-        }
-
-        @Override
-        public void visit(OpJoin opJoin)
-        {
-            // Keep things clearly separated.
-            Element eLeft = asElement(opJoin.getLeft()) ;
-            Element eRight = asElementGroup(opJoin.getRight()) ;
-            
-            ElementGroup g = currentGroup() ;
-            g.addElement(eLeft) ;
-            g.addElement(eRight) ;
-            return ;
-        }
-
-        private static boolean emptyGroup(Element element)
-        {
-            if ( ! ( element instanceof ElementGroup ) )
-                return false ;
-            ElementGroup eg = (ElementGroup)element ;
-            return eg.isEmpty() ;
-        }
-        
-        @Override
-        public void visit(OpLeftJoin opLeftJoin)
-        {
-            Element eLeft = asElement(opLeftJoin.getLeft()) ;
-            ElementGroup eRight = asElementGroup(opLeftJoin.getRight()) ;
-            
-            if ( opLeftJoin.getExprs() != null )
-            {
-                for ( Expr expr : opLeftJoin.getExprs() )
-                {
-                    ElementFilter f = new ElementFilter(expr) ;
-                    eRight.addElement(f) ;
-                }
-            }
-            ElementGroup g = currentGroup() ;
-            if ( ! emptyGroup(eLeft) )
-                g.addElement(eLeft) ;
-            ElementOptional opt = new ElementOptional(eRight) ;
-            g.addElement(opt) ;
-        }
-
-        @Override
-        public void visit(OpDiff opDiff)
-        { throw new ARQNotImplemented("OpDiff") ; }
-
-        @Override
-        public void visit(OpMinus opMinus) {
-            Element eLeft = asElement(opMinus.getLeft()) ;
-            Element eRight = asElementGroup(opMinus.getRight()) ;
-            ElementMinus elMinus = new ElementMinus(eRight) ;
-            ElementGroup g = currentGroup() ;
-            if ( ! emptyGroup(eLeft) )
-                g.addElement(eLeft) ;
-            g.addElement(elMinus) ;
-        }
-
-        @Override
-        public void visit(OpUnion opUnion)
-        {
-            Element eLeft = asElementGroup(opUnion.getLeft()) ;
-            Element eRight = asElementGroup(opUnion.getRight()) ;
-            if ( eLeft instanceof ElementUnion )
-            {
-                ElementUnion elUnion = (ElementUnion)eLeft ;
-                elUnion.addElement(eRight) ;
-                return ;
-            }
-            
-//            if ( eRight instanceof ElementUnion )
-//            {
-//                ElementUnion elUnion = (ElementUnion)eRight ;
-//                elUnion.getElements().add(0, eLeft) ;
-//                return ;
-//            }
-            
-            ElementUnion elUnion = new ElementUnion() ;
-            elUnion.addElement(eLeft) ;
-            elUnion.addElement(eRight) ;
-            currentGroup().addElement(elUnion) ;
-        }
-
-        @Override
-        public void visit(OpConditional opCondition)
-        { throw new ARQNotImplemented("OpCondition") ; }
-
-        @Override
-        public void visit(OpFilter opFilter)
-        {
-            // (filter .. (filter ( ... ))   (non-canonicalizing OpFilters)
-            // Inner gets Grouped unnecessarily. 
-            Element e = asElement(opFilter.getSubOp()) ;
-            if ( currentGroup() != e )
-                currentGroup().addElement(e) ;
-            element = currentGroup() ;      // Was cleared by asElement. 
-            
-            ExprList exprs = opFilter.getExprs() ;
-            for ( Expr expr : exprs )
-            {
-                ElementFilter f = new ElementFilter(expr) ;
-                currentGroup().addElement(f) ;
-            }
-        }
-
-        @Override
-        public void visit(OpGraph opGraph)
-        {
-            startSubGroup() ;
-            Element e = asElement(opGraph.getSubOp()) ;
-            endSubGroup() ;
-            
-            //If not element group make it one
-            if (!(e instanceof ElementGroup)) {
-                ElementGroup g = new ElementGroup();
-                g.addElement(e);
-                e = g;
-            }
-            
-            Element graphElt = new ElementNamedGraph(opGraph.getNode(), e) ;
-            currentGroup().addElement(graphElt) ;
-        }
-
-        @Override
-        public void visit(OpService opService)
-        { 
-            // Hmm - if the subnode has been optimized, we may fail.
-            Op op = opService.getSubOp() ;
-            Element x = asElement(opService.getSubOp()) ; 
-            Element elt = new ElementService(opService.getService(), x, opService.getSilent()) ;
-            currentGroup().addElement(elt) ;
-        }
-        
-        @Override
-        public void visit(OpDatasetNames dsNames)
-        { throw new ARQNotImplemented("OpDatasetNames") ; }
-
-        @Override
-        public void visit(OpTable opTable)
-        { 
-            // This will go in a group so simply forget it. 
-            if ( opTable.isJoinIdentity() ) return ;
-            
-            // Put in a VALUES
-            // This may be related to the grpup of the overall query.
-            
-            ElementData el = new ElementData() ; 
-            el.getVars().addAll(opTable.getTable().getVars()) ;
-            QueryIterator qIter = opTable.getTable().iterator(null) ;
-            while(qIter.hasNext())
-                el.getRows().add(qIter.next()) ;
-            qIter.close() ;
-            currentGroup().addElement(el) ;
-        }
-
-        @Override
-        public void visit(OpExt opExt)
-        {
-//            Op op = opExt.effectiveOp() ;
-//            // This does not work in all cases.
-//            op.visit(this) ;
-            throw new ARQNotImplemented("OpExt") ;
-        }
-
-        @Override
-        public void visit(OpNull opNull)
-        { throw new ARQNotImplemented("OpNull") ; }
-
-        @Override
-        public void visit(OpLabel opLabel)
-        {
-            if ( opLabel.hasSubOp() )
-                opLabel.getSubOp().visit(this) ;
-        }
-
-        @Override
-        public void visit(OpAssign opAssign)
-        {
-            opAssign.getSubOp().visit(this) ;
-            
-            // Go through each var and get the assigned expression
-            for ( Var v : opAssign.getVarExprList().getVars() )
-            {
-                Expr e = opAssign.getVarExprList().getExpr(v);
-                
-                // Substitute group aggregate expressions in for generated vars
-                SubExprForVar sefr = new SubExprForVar(varExpression);
-                Expr tr = ExprTransformer.transform(sefr, e);
-                
-                // If in top level we defer assignment to SELECT section
-                // This also covers the GROUP recombine
-                if (inTopLevel()) {
-                    if (!inGroupRecombine(opAssign)) {
-                        // If not wrapped over a Group then we need to ensure we add the variable
-                        // to the list or otherwise the BIND will not round trip
-                        // Note - This does mean top level BIND will manifest as a project expression
-                        //        rather than a BIND but this is semantically equivalent so is not an issue
-                        addProjectVar(projectVars, v) ;
-                    }
-                    varExpression.put(v, tr);
-                } else {
-                    Element elt = new ElementAssign(v, e) ;
-                    ElementGroup g = currentGroup() ;
-                    g.addElement(elt) ;
-                }
-            }
-        }
-
-        @Override
-        public void visit(OpExtend opExtend)
-        { 
-            opExtend.getSubOp().visit(this) ;
-            
-            // Go through each var and get the assigned expression
-            for ( Var v : opExtend.getVarExprList().getVars() )
-            {
-                Expr e = opExtend.getVarExprList().getExpr(v);
-                
-                // Substitute group aggregate expressions in for generated vars
-                Expr tr = ExprTransformer.transform(new SubExprForVar(varExpression), e);
-                
-                // If in top level we defer assignment to SELECT section
-                // This also covers the GROUP recombine
-                if (inTopLevel()) {
-                    if (groupDepth == 0 || inGroupRecombine(opExtend)) {
-                        if (!inGroupRecombine(opExtend)) {
-                            // If not wrapped over a Group then we need to ensure we add the variable
-                            // to the list or otherwise the BIND will not round trip
-                            // Note - This does mean top level BIND will manifest as a project expression
-                            //        rather than a BIND but this is semantically equivalent so is not an issue
-                            addProjectVar(projectVars, v) ;
-                        }
-                        varExpression.put(v, tr);
-                    } else {
-                        Element elt = new ElementBind(v, tr) ;
-                        ElementGroup g = currentGroup() ;
-                        g.addElement(elt);
-                    }
-                } else {
-                    Element elt = new ElementBind(v, tr) ;
-                    ElementGroup g = currentGroup() ;
-                    g.addElement(elt) ;
-                }
-            }
-        }
-
-        
-        @Override
-        public void visit(OpList opList)
-        { /* No action */ }
-
-        @Override
-        public void visit(OpOrder opOrder)
-        {
-            List<SortCondition> x = opOrder.getConditions() ;
-            for ( SortCondition sc : x )
-                query.addOrderBy(sc);
-            opOrder.getSubOp().visit(this) ;
-        }
-
-        @Override
-        public void visit(OpProject opProject)
-        {
-            if (inProject) {
-                // If we've already inside a project then we are reconstructing a sub-query
-                // Create a new converter and call on the sub-op to get the sub-query
-                Converter subConverter = new Converter(opProject);
-                ElementSubQuery subQuery = new ElementSubQuery(subConverter.convert());
-                ElementGroup g = currentGroup();
-                g.addElement(subQuery);
-            } else {
-                // Defer adding result vars until the end.
-                // OpGroup generates dupes otherwise
-                this.projectVars = allocProjectVars() ;
-                this.projectVars.addAll(opProject.getVars());
-                inProject = true;
-                opProject.getSubOp().visit(this) ;
-                inProject = false;
-            }
-        }
-
-        @Override
-        public void visit(OpReduced opReduced)
-        { 
-            query.setReduced(true) ;
-            opReduced.getSubOp().visit(this) ;
-        }
-
-        @Override
-        public void visit(OpDistinct opDistinct)
-        { 
-            query.setDistinct(true) ;
-            opDistinct.getSubOp().visit(this) ;
-        }
-
-        @Override
-        public void visit(OpSlice opSlice)
-        {
-            if ( opSlice.getStart() != Query.NOLIMIT )
-                query.setOffset(opSlice.getStart()) ;
-            if ( opSlice.getLength() != Query.NOLIMIT )
-                query.setLimit(opSlice.getLength()) ;
-            opSlice.getSubOp().visit(this) ;
-        }
-
-        @Override
-        public void visit(OpGroup opGroup) {               
-            List<ExprAggregator> a = opGroup.getAggregators();
-            
-            // Aggregators are broken up in the algebra, split between a
-            // group and an assignment (extend or assign) using a generated var.
-            // We record them here and insert later.
-            for (ExprAggregator ea : a) {
-                // Substitute generated var for actual
-                Var givenVar = ea.getAggVar().asVar();
-                // Copy aggregator across (?)
-                Expr myAggr = query.allocAggregate(ea.getAggregator());
-                varExpression.put(givenVar, myAggr);
-            }
-
-            VarExprList b = opGroup.getGroupVars();
-            for (Var v : b.getVars()) {
-                Expr e = b.getExpr(v);
-
-                if (e != null) {
-                    query.addGroupBy(v, e);
-
-                } else {
-                    query.addGroupBy(v);
-
-                }
-            }
-            groupDepth++;
-            opGroup.getSubOp().visit(this);
-            groupDepth--;
-        }
-
-        @Override
-        public void visit(OpTopN opTop)
-        { throw new ARQNotImplemented("OpTopN") ; }
-        
-        private Element lastElement()
-        {
-            ElementGroup g = currentGroup ;
-            if ( g == null || g.getElements().size() == 0 )
-                return null ;
-            int len = g.getElements().size() ;
-            return g.getElements().get(len-1) ;
-        }
-
-        private void startSubGroup()
-        {
-            push(currentGroup) ;
-            ElementGroup g = new ElementGroup() ;
-            currentGroup = g ;
-        }
-        
-        private ElementGroup endSubGroup()
-        {
-            ElementGroup g = pop() ;
-            ElementGroup r = currentGroup ;
-            currentGroup = g ;
-            return r ;
-        }
-        
-//        private void endCurrentGroup()
-//        {
-//            currentGroup = null ;
-//            element = null ; //??
-//        }
-        
-        private ElementGroup currentGroup()
-        {
-//            if ( currentGroup == null )
-//                startSubGroup() ;
-            return currentGroup ;
-        }
-        
-        private ElementGroup peek()
-        {
-            if ( stack.size() == 0 )
-                return null ;
-            return stack.peek();
-        }
-        private ElementGroup pop() { return stack.pop(); }
-        private void push(ElementGroup el) { stack.push(el); }
-        private boolean inTopLevel() { return stack.size() == 0; }
-        
-        private boolean inGroupRecombine(OpExtend op) {
-            Op subOp = op.getSubOp();
-            if (subOp instanceof OpExtend) {
-                return inGroupRecombine((OpExtend)subOp);
-            } else if (subOp instanceof OpAssign) {
-                return inGroupRecombine((OpAssign)subOp);
-            } else if (subOp instanceof OpGroup) {
-                return true;
-            } else {
-                return false;
-            }
-        }
-        
-        private boolean inGroupRecombine(OpAssign op) {
-            Op subOp = op.getSubOp();
-            if (subOp instanceof OpExtend) {
-                return inGroupRecombine((OpExtend)subOp);
-            } else if (subOp instanceof OpAssign) {
-                return inGroupRecombine((OpAssign)subOp);
-            } else if (subOp instanceof OpGroup) {
-                return true;
-            } else {
-                return false;
-            }
-        }
-    }
-    
-    /**
-     * This class is used to take substitute an expressions for variables
-     * in another expression. It is used to stick grouping expressions back
-     * together.
-     */
-    public static class SubExprForVar extends ExprTransformCopy {
-        private final Map<Var, Expr> varExpr;
-        private boolean subOccurred = false;
-        public SubExprForVar(Map<Var, Expr> varExpr) {
-            this.varExpr = varExpr;
-        }
-        
-        public boolean didChange() { return subOccurred; }
-        
-        @Override
-        public Expr transform(ExprVar var) {
-            if (varExpr.containsKey(var.asVar())) {
-                subOccurred = true;
-                return varExpr.get(var.asVar()).deepCopy();
-            }
-            else return var.deepCopy();
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/56534eac/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpExtBuilder.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpExtBuilder.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpExtBuilder.java
deleted file mode 100644
index 7896528..0000000
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpExtBuilder.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hp.hpl.jena.sparql.algebra;
-
-import com.hp.hpl.jena.sparql.algebra.op.OpExt ;
-import com.hp.hpl.jena.sparql.sse.ItemList ;
-
-public interface OpExtBuilder
-{
-    /** Name of operators created by this ExtBuilder */
-    public String getTagName() ;
-    /** The remaining arguments */
-    public OpExt make(ItemList argList) ;
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/56534eac/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpExtRegistry.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpExtRegistry.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpExtRegistry.java
deleted file mode 100644
index f470824..0000000
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpExtRegistry.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hp.hpl.jena.sparql.algebra;
-
-import java.util.HashMap ;
-import java.util.Map ;
-
-import com.hp.hpl.jena.sparql.ARQException ;
-import com.hp.hpl.jena.sparql.algebra.op.OpExt ;
-import com.hp.hpl.jena.sparql.sse.ItemList ;
-import com.hp.hpl.jena.sparql.sse.Tags ;
-import com.hp.hpl.jena.sparql.sse.builders.BuilderOp ;
-
-/** Manage extension algebra operations */
-public class OpExtRegistry
-{
-    // Known extensions.
-    static Map<String, OpExtBuilder> extensions = new HashMap<>() ;
-    
-    // Wire in (ext NAME ...) form
-    static { BuilderOp.add(Tags.tagExt, new BuildExtExt()) ; }
-    
-    public static void register(OpExtBuilder builder)
-    {
-        extensions.put(builder.getTagName(), builder) ;
-
-        if ( BuilderOp.contains(builder.getTagName()) )
-            throw new ARQException("Tag '"+builder.getTagName()+"' already defined") ;
-        BuilderOp.add(builder.getTagName(), new BuildExt2()) ;
-    }
-    
-    
-    public static void unregister(String subtag)
-    {
-        extensions.remove(subtag) ;
-    }
-    
-    public static OpExtBuilder builder(String tag) { return extensions.get(tag) ; }
-
-    public static Op buildExt(String tag, ItemList args)
-    {
-        OpExtBuilder b = builder(tag) ;
-        OpExt ext = b.make(args) ;  // Arguments 2 onwards
-        return ext ;
-    }
-    
-    // (ext NAME ...) form
-    static public class BuildExtExt implements BuilderOp.Build 
-    { 
-        @Override
-        public Op make(ItemList list)
-        {
-            // 0 is the "ext"
-            String subtag = list.get(1).getSymbol() ;
-            list = list.sublist(2) ;
-            return buildExt(subtag, list) ; 
-        }
-    }
-    
-    // (NAME ...) form
-    static public class BuildExt2 implements BuilderOp.Build 
-    { 
-        @Override
-        public Op make(ItemList list)
-        {
-            String subtag = list.get(0).getSymbol() ;
-            list = list.sublist(1) ;
-            return buildExt(subtag, list) ; 
-        }
-    }
-    
-    
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/56534eac/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpLib.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpLib.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpLib.java
deleted file mode 100644
index 537cc1f..0000000
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpLib.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hp.hpl.jena.sparql.algebra;
-
-import com.hp.hpl.jena.sparql.algebra.op.Op1 ;
-import com.hp.hpl.jena.sparql.algebra.op.OpDistinct ;
-import com.hp.hpl.jena.sparql.algebra.op.OpOrder ;
-import com.hp.hpl.jena.sparql.algebra.op.OpProject ;
-import com.hp.hpl.jena.sparql.algebra.op.OpReduced ;
-import com.hp.hpl.jena.sparql.algebra.op.OpSlice ;
-
-// NB Operations take and return nulls for convenience.
-
-public class OpLib
-{
-    public static Op sub(Op1 op) { return op==null ? null : op.getSubOp() ; }
-    
-    public static boolean isProject(Op op) { return op instanceof OpProject ; } 
-    public static OpProject asProject(Op op)
-    {  return isProject(op) ? (OpProject)op : null ; }
-    
-    public static boolean isDistinct(Op op) { return op instanceof OpDistinct ; } 
-    public static OpDistinct asDistinct(Op op)
-    {  return isDistinct(op) ? (OpDistinct)op : null ; }
-
-    public static boolean isReduced(Op op) { return op instanceof OpReduced ; } 
-    public static OpReduced asReduced(Op op)
-    {  return isReduced(op) ? (OpReduced)op : null ; }
-
-    public static boolean isOrder(Op op) { return op instanceof OpOrder ; } 
-    public static OpOrder asOrder(Op op)
-    {  return isOrder(op) ? (OpOrder)op : null ; }
-
-    public static boolean isSlice(Op op) { return op instanceof OpSlice ; } 
-    public static OpSlice asSlice(Op op)
-    {  return isSlice(op) ? (OpSlice)op : null ; }
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/56534eac/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpPrefixesUsed.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpPrefixesUsed.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpPrefixesUsed.java
deleted file mode 100644
index 0ac9ac7..0000000
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpPrefixesUsed.java
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hp.hpl.jena.sparql.algebra;
-
-import java.util.Map ;
-
-import com.hp.hpl.jena.graph.Node ;
-import com.hp.hpl.jena.graph.Triple ;
-import com.hp.hpl.jena.shared.PrefixMapping ;
-import com.hp.hpl.jena.shared.impl.PrefixMappingImpl ;
-import com.hp.hpl.jena.sparql.algebra.op.OpBGP ;
-import com.hp.hpl.jena.sparql.algebra.op.OpFilter ;
-import com.hp.hpl.jena.sparql.algebra.op.OpGraph ;
-import com.hp.hpl.jena.sparql.algebra.op.OpQuadPattern ;
-import com.hp.hpl.jena.sparql.core.BasicPattern ;
-
-public class OpPrefixesUsed
-{
-    static public PrefixMapping used(Op op, PrefixMapping pmap)
-    {
-        PrefixMapping pmap2 = new PrefixMappingImpl() ;
-        PrefixFinder finder = new PrefixFinder(pmap2, pmap.getNsPrefixMap()) ;
-        OpWalker.walk(op, finder) ;
-        return pmap2 ;
-    }
-    
-    
-    static class PrefixFinder extends OpVisitorBase
-    {
-        Map<String, String> pmap ;
-        PrefixMapping usedPMap ;
-        
-        public PrefixFinder(PrefixMapping pmap2, Map<String, String> pmap)
-        {
-            this.pmap = pmap ;
-            this.usedPMap = pmap2 ;
-        }
-        
-        @Override
-        public void visit(OpGraph opGraph)
-        {
-            node(opGraph.getNode()) ;
-        }
-        
-        @Override
-        public void visit(OpQuadPattern quadPattern)
-        {
-            node(quadPattern.getGraphNode()) ;
-            visit(quadPattern.getBasicPattern()) ;
-        }
-        
-        @Override
-        public void visit(OpBGP opBGP)
-        {
-            BasicPattern p = opBGP.getPattern() ;
-            visit(opBGP.getPattern()) ;
-        }
-        
-        private void visit(BasicPattern pattern)
-        {
-            for ( Triple t : pattern )
-            {
-                node(t.getSubject()) ;
-                node(t.getPredicate()) ;
-                node(t.getObject()) ;
-            }
-        }
-        
-        @Override
-        public void visit(OpFilter opFilter)
-        {
-            // Do more
-        }
-        
-        private void node(Node n)
-        {
-            if ( ! n.isURI() ) return ;
-            String uri = n.getURI() ;
-            
-            if ( usedPMap.shortForm(uri) != uri )
-                return ;
-
-            for ( Map.Entry<String, String> e : pmap.entrySet() )
-            {
-                String k = e.getKey();
-                String v = e.getValue();
-
-                if ( uri.startsWith( v ) )
-                {
-                    usedPMap.setNsPrefix( k, v );
-                    return;
-                }
-            }
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/56534eac/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpVars.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpVars.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpVars.java
deleted file mode 100644
index 00bdccc..0000000
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpVars.java
+++ /dev/null
@@ -1,428 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.hp.hpl.jena.sparql.algebra ;
-
-import static com.hp.hpl.jena.sparql.core.Vars.addVar ;
-
-import java.util.* ;
-
-import org.apache.jena.atlas.lib.SetUtils ;
-import org.apache.jena.atlas.lib.Tuple ;
-
-import com.hp.hpl.jena.graph.Node ;
-import com.hp.hpl.jena.graph.Triple ;
-import com.hp.hpl.jena.sparql.algebra.OpWalker.WalkerVisitor ;
-import com.hp.hpl.jena.sparql.algebra.op.* ;
-import com.hp.hpl.jena.sparql.core.BasicPattern ;
-import com.hp.hpl.jena.sparql.core.Var ;
-import com.hp.hpl.jena.sparql.expr.ExprVars ;
-import com.hp.hpl.jena.sparql.pfunction.PropFuncArg ;
-import com.hp.hpl.jena.sparql.util.VarUtils ;
-
-/** Get vars for a pattern */
-
-public class OpVars
-{
-    // Choose the default collector - LinkedHashSet is predictable and
-    // keeps the "found" order
-    private static Set<Var> collector() {
-        return new LinkedHashSet<>() ;
-    }
-
-    public static Set<Var> visibleVars(Op op) {
-        Set<Var> acc = collector() ;
-        visibleVars(op, acc) ;
-        return acc ;
-    }
-
-    public static void visibleVars(Op op, Set<Var> acc) {
-        OpVarsPattern visitor = new OpVarsPattern(acc, true) ;
-        OpWalker.walk(new WalkerVisitorVisible(visitor, acc), op) ;
-    }
-    
-    /** The set of variables that wil be in every solution of this Op */
-    public static Set<Var> fixedVars(Op op) {
-        Set<Var> acc = collector() ;
-        fixedVars(op, acc) ;
-        return acc ;
-    }
-
-    public static void fixedVars(Op op, Set<Var> acc) {
-        OpVarsPattern visitor = new OpVarsPattern(acc, true) ;
-        OpWalker.walk(new WalkerVisitorFixed(visitor, acc), op) ;
-    }
-    
-    @SuppressWarnings("unchecked")
-    public static Tuple<Set<Var>> mentionedVarsByPosition(Op op) {
-        Set<Var> graphAcc = collector() ;
-        Set<Var> subjAcc = collector() ;
-        Set<Var> predAcc = collector() ;
-        Set<Var> objAcc = collector() ;
-        Set<Var> unknownAcc = collector() ;
-        OpVarsPatternWithPositions visitor = new OpVarsPatternWithPositions(graphAcc, subjAcc, predAcc, objAcc, unknownAcc, false);
-        OpWalker.walk(op, visitor);
-        return Tuple.createTuple(graphAcc, subjAcc, predAcc, objAcc, unknownAcc);
-    }
-    
-    @SuppressWarnings("unchecked")
-    public static Tuple<Set<Var>> mentionedVarsByPosition(Op... ops) {
-        Set<Var> graphAcc = collector() ;
-        Set<Var> subjAcc = collector() ;
-        Set<Var> predAcc = collector() ;
-        Set<Var> objAcc = collector() ;
-        Set<Var> unknownAcc = collector() ;
-        OpVarsPatternWithPositions visitor = new OpVarsPatternWithPositions(graphAcc, subjAcc, predAcc, objAcc, unknownAcc, false);
-        for (Op op : ops)
-            OpWalker.walk(op, visitor);
-        return Tuple.createTuple(graphAcc, subjAcc, predAcc, objAcc, unknownAcc);
-    }
-
-    // All mentioned variables regardless of scope/visibility.
-    public static Collection<Var> mentionedVars(Op op) {
-        Set<Var> acc = collector() ;
-        mentionedVars(op, acc) ;
-        return acc ;
-    }
-
-    // All mentioned variables regardless of scope/visibility.
-    public static void mentionedVars(Op op, Set<Var> acc) {
-        OpVarsMentioned visitor = new OpVarsMentioned(acc) ;
-        OpWalker.walk(op, visitor) ;
-    }
-
-    /** Do project and don't walk into it. MINUS vars aren't visible either */
-    private static class WalkerVisitorVisible extends WalkerVisitor 
-    {
-        private final Collection<Var> acc ;
-
-        public WalkerVisitorVisible(OpVarsPattern visitor, Collection<Var> acc) {
-            super(visitor) ;
-            this.acc = acc ;
-        }
-
-        @Override
-        public void visit(OpProject op) {
-            before(op) ;
-            // Skip Project subop.
-            acc.addAll(op.getVars()) ;
-            after(op) ;
-        }
-
-        @Override
-        public void visit(OpMinus op) {
-            before(op) ;
-            if (op.getLeft() != null)
-                op.getLeft().visit(this) ;
-            // Skip right.
-            // if ( op.getRight() != null ) op.getRight().visit(this) ;
-            if (visitor != null)
-                op.visit(visitor) ;
-            after(op) ;
-        }
-    }
-
-    // Only consider variables that are visible and definitely defined.
-    // OPTIONAL (2 forms) and UNION are the interesting cases.
-    private static class WalkerVisitorFixed extends WalkerVisitor
-    {
-        private final Collection<Var> acc ;
-
-        public WalkerVisitorFixed(OpVarsPattern visitor, Collection<Var> acc) {
-            super(visitor) ;
-            this.acc = acc ;
-        }
-        
-        @Override
-        public void visit(OpLeftJoin x) {
-            x.getLeft().visit(this);
-        }
-
-        @Override
-        public void visit(OpConditional x) {
-            x.getLeft().visit(this);
-        }
-
-        @Override
-        public void visit(OpUnion x) {
-            Set<Var> left = fixedVars(x.getLeft()) ;
-            Set<Var> right = fixedVars(x.getRight()) ;
-            Set<Var> r = SetUtils.intersection(left,  right) ;
-            acc.addAll(r) ;
-        }
-        
-        @Override
-        public void visit(OpProject op) {
-            before(op) ;
-            // Skip Project subop.
-            acc.addAll(op.getVars()) ;
-            after(op) ;
-        }
-
-        @Override
-        public void visit(OpMinus op) {
-            before(op) ;
-            if (op.getLeft() != null)
-                op.getLeft().visit(this) ;
-            // Skip right.
-            // if ( op.getRight() != null ) op.getRight().visit(this) ;
-            if (visitor != null)
-                op.visit(visitor) ;
-            after(op) ;
-        }
-    }
-
-    private static class OpVarsPattern extends OpVisitorBase
-    {
-        // The possibly-set-vars
-        protected Set<Var> acc ;
-        final boolean      visibleOnly ;
-
-        OpVarsPattern(Set<Var> acc, boolean visibleOnly) {
-            this.acc = acc ;
-            this.visibleOnly = visibleOnly ;
-        }
-
-        @Override
-        public void visit(OpBGP opBGP) {
-            VarUtils.addVars(acc, opBGP.getPattern()) ;
-        }
-
-        @Override
-        public void visit(OpPath opPath) {
-            addVar(acc, opPath.getTriplePath().getSubject()) ;
-            addVar(acc, opPath.getTriplePath().getObject()) ;
-        }
-
-        @Override
-        public void visit(OpQuadPattern quadPattern) {
-            addVar(acc, quadPattern.getGraphNode()) ;
-            VarUtils.addVars(acc, quadPattern.getBasicPattern()) ;
-//            // Pure quading
-//            for (Iterator<Quad> iter = quadPattern.getQuads().iterator(); iter.hasNext();) {
-//                Quad quad = iter.next() ;
-//                addVarsFromQuad(acc, quad) ;
-//            }
-        }
-
-        @Override
-        public void visit(OpGraph opGraph) {
-            addVar(acc, opGraph.getNode()) ;
-        }
-
-        @Override
-        public void visit(OpDatasetNames dsNames) {
-            addVar(acc, dsNames.getGraphNode()) ;
-        }
-
-        @Override
-        public void visit(OpTable opTable) {
-            // Only the variables with values in the tables
-            // (When building, undefs didn't get into bindings so no variable
-            // mentioned)
-            Table t = opTable.getTable() ;
-            acc.addAll(t.getVars()) ;
-        }
-
-        @Override
-        public void visit(OpProject opProject) {
-            // The walker (WalkerVisitorVisible) handles this
-            // for visible variables, not mentioned variable colelcting.
-            // The visibleOnly/clear is simply to be as general as possible.
-            if (visibleOnly)
-                acc.clear() ;
-            acc.addAll(opProject.getVars()) ;
-        }
-
-        @Override
-        public void visit(OpAssign opAssign) {
-            acc.addAll(opAssign.getVarExprList().getVars()) ;
-        }
-
-        @Override
-        public void visit(OpExtend opExtend) {
-            acc.addAll(opExtend.getVarExprList().getVars()) ;
-        }
-
-        @Override
-        public void visit(OpPropFunc opPropFunc) {
-            addvars(opPropFunc.getSubjectArgs()) ;
-            addvars(opPropFunc.getObjectArgs()) ;
-        }
-
-        private void addvars(PropFuncArg pfArg) {
-            if (pfArg.isNode()) {
-                addVar(acc, pfArg.getArg()) ;
-                return ;
-            }
-            for (Node n : pfArg.getArgList())
-                addVar(acc, n) ;
-        }
-
-        @Override
-        public void visit(OpProcedure opProc) {
-            opProc.getArgs().varsMentioned(acc) ;
-        }
-
-    }
-    
-    private static class OpVarsPatternWithPositions extends OpVisitorBase
-    {
-        // The possibly-set-vars
-        protected Set<Var> graphAcc, subjAcc, predAcc, objAcc, unknownAcc ;
-        final boolean      visibleOnly ;
-
-        OpVarsPatternWithPositions(Set<Var> graphAcc, Set<Var> subjAcc, Set<Var> predAcc, Set<Var> objAcc, Set<Var> unknownAcc, boolean visibleOnly) {
-            this.graphAcc = graphAcc;
-            this.subjAcc = subjAcc;
-            this.predAcc = predAcc;
-            this.objAcc = objAcc;
-            this.unknownAcc = unknownAcc;
-            this.visibleOnly = visibleOnly ;
-        }
-
-        @Override
-        public void visit(OpBGP opBGP) {
-            vars(opBGP.getPattern()) ;
-        }
-
-        @Override
-        public void visit(OpPath opPath) {
-            addVar(subjAcc, opPath.getTriplePath().getSubject()) ;
-            addVar(objAcc, opPath.getTriplePath().getObject()) ;
-        }
-
-        @Override
-        public void visit(OpQuadPattern quadPattern) {
-            addVar(graphAcc, quadPattern.getGraphNode()) ;
-            vars(quadPattern.getBasicPattern()) ;
-        }
-
-        @Override
-        public void visit(OpGraph opGraph) {
-            addVar(graphAcc, opGraph.getNode()) ;
-        }
-
-        @Override
-        public void visit(OpDatasetNames dsNames) {
-            addVar(graphAcc, dsNames.getGraphNode()) ;
-        }
-
-        @Override
-        public void visit(OpTable opTable) {
-            // Only the variables with values in the tables
-            // (When building, undefs didn't get into bindings so no variable
-            // mentioned)
-            Table t = opTable.getTable() ;
-            // Treat as unknown position
-            unknownAcc.addAll(t.getVars()) ;
-        }
-
-        @Override
-        public void visit(OpProject opProject) {
-            // The walker (WalkerVisitorVisible) handles this
-            // for visible variables, not mentioned variable collecting.
-            // The visibleOnly/clear is simply to be as general as possible.
-            List<Var> vs = opProject.getVars();
-            if (visibleOnly) {
-                clear(graphAcc, vs);
-                clear(subjAcc, vs);
-                clear(predAcc, vs);
-                clear(objAcc, vs);
-                
-            }
-            for (Var v : vs) {
-                if (!graphAcc.contains(v) && !subjAcc.contains(v) && !predAcc.contains(v) && !objAcc.contains(v)) {
-                    addVar(unknownAcc, v);
-                }
-            }
-        }
-
-        @Override
-        public void visit(OpAssign opAssign) {
-            // Unknown position
-            unknownAcc.addAll(opAssign.getVarExprList().getVars()) ;
-        }
-
-        @Override
-        public void visit(OpExtend opExtend) {
-            // Unknown position
-            unknownAcc.addAll(opExtend.getVarExprList().getVars()) ;
-        }
-
-        @Override
-        public void visit(OpPropFunc opPropFunc) {
-            addvars(subjAcc, opPropFunc.getSubjectArgs()) ;
-            addvars(objAcc, opPropFunc.getObjectArgs()) ;
-        }
-
-        private void addvars(Set<Var> acc, PropFuncArg pfArg) {
-            if (pfArg.isNode()) {
-                addVar(acc, pfArg.getArg()) ;
-                return ;
-            }
-            for (Node n : pfArg.getArgList())
-                addVar(acc, n) ;
-        }
-
-        @Override
-        public void visit(OpProcedure opProc) {
-            unknownAcc.addAll(OpVars.mentionedVars(opProc));
-        }
-        
-        private void vars(BasicPattern bp) {
-            for (Triple t : bp.getList())
-            {
-                addVar(subjAcc, t.getSubject());
-                addVar(predAcc, t.getPredicate());
-                addVar(objAcc, t.getObject());
-            }
-        }
-        
-        private void clear(Set<Var> acc, List<Var> visible) {
-            List<Var> toRemove = new ArrayList<>();
-            for (Var found : acc)
-            {
-                if (!visible.contains(found)) {
-                    toRemove.add(found);
-                }
-            }
-            for (Var v : toRemove) {
-                acc.remove(v);
-            }
-        }
-
-    }
-
-    private static class OpVarsMentioned extends OpVarsPattern
-    {
-        OpVarsMentioned(Set<Var> acc) {
-            super(acc, false) ;
-        }
-
-        @Override
-        public void visit(OpFilter opFilter) {
-            opFilter.getExprs().varsMentioned(acc) ;
-        }
-
-        @Override
-        public void visit(OpOrder opOrder) {
-            ExprVars.varsMentioned(acc, opOrder.getConditions()) ;
-        }
-    }
-}


Mime
View raw message