Repository: jena Updated Branches: refs/heads/master 3a6faee81 -> e5d9db53e
Convert to a single format style Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/e5d9db53 Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/e5d9db53 Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/e5d9db53 Branch: refs/heads/master Commit: e5d9db53e3bf63a97c24171d2e75cae049a65b8a Parents: 3a6faee Author: Andy Seaborne <a...@apache.org> Authored: Tue Oct 18 14:12:19 2016 +0100 Committer: Andy Seaborne <a...@apache.org> Committed: Tue Oct 18 14:12:19 2016 +0100 ---------------------------------------------------------------------- .../ReorderTransformationSubstitution.java | 155 ++++++++----------- 1 file changed, 63 insertions(+), 92 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/e5d9db53/jena-arq/src/main/java/org/apache/jena/sparql/engine/optimizer/reorder/ReorderTransformationSubstitution.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/engine/optimizer/reorder/ReorderTransformationSubstitution.java b/jena-arq/src/main/java/org/apache/jena/sparql/engine/optimizer/reorder/ReorderTransformationSubstitution.java index 1f74c9c..9dc6c65 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/engine/optimizer/reorder/ReorderTransformationSubstitution.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/engine/optimizer/reorder/ReorderTransformationSubstitution.java @@ -47,17 +47,15 @@ public abstract class ReorderTransformationSubstitution implements ReorderTransf public ReorderTransformationSubstitution() {} @Override - public BasicPattern reorder(BasicPattern pattern) - { + public BasicPattern reorder(BasicPattern pattern) { return reorderIndexes(pattern).reorder(pattern) ; } @Override - public final ReorderProc reorderIndexes(BasicPattern pattern) - { - if (pattern.size() <= 1 ) + public final ReorderProc reorderIndexes(BasicPattern pattern) { + if ( pattern.size() <= 1 ) return ReorderLib.identityProc() ; - + List<Triple> triples = pattern.getList() ; // Could merge into the conversion step to do the rewrite WRT a Binding. @@ -72,8 +70,7 @@ public abstract class ReorderTransformationSubstitution implements ReorderTransf return proc ; } - protected List<PatternTriple> modifyComponents(List<PatternTriple> components) - { + protected List<PatternTriple> modifyComponents(List<PatternTriple> components) { return components ; } @@ -86,18 +83,16 @@ public abstract class ReorderTransformationSubstitution implements ReorderTransf } ; } - protected ReorderProc reorder(List<Triple> triples, List<PatternTriple> components) - { + protected ReorderProc reorder(List<Triple> triples, List<PatternTriple> components) { int N = components.size() ; int numReorder = N ; // Maybe choose 4, say, and copy over the rest. int indexes[] = new int[N] ; if ( DEBUG ) - log.debug("Reorder: "+Iter.asString(components, formatter())) ; - + log.debug("Reorder: " + Iter.asString(components, formatter())) ; + int idx = 0 ; - for ( ; idx < numReorder ; idx++ ) - { + for ( ; idx < numReorder ; idx++ ) { int j = chooseNext(components) ; if ( j < 0 ) break ; @@ -106,92 +101,75 @@ public abstract class ReorderTransformationSubstitution implements ReorderTransf update(triple, components) ; components.set(j, null) ; } - - // Copy over the remainder (if any) - for ( int i = 0 ; i < components.size() ; i++ ) - { + + // Copy over the remainder (if any) + for ( int i = 0 ; i < components.size() ; i++ ) { if ( components.get(i) != null ) indexes[idx++] = i ; } if ( triples.size() != idx ) - throw new ARQException(String.format("Inconsistency: number of triples (%d) does not equal to number of indexes processed (%d)", triples.size(), idx)) ; - + throw new ARQException(String.format("Inconsistency: number of triples (%d) does not equal to number of indexes processed (%d)", + triples.size(), idx)) ; + ReorderProc proc = new ReorderProcIndexes(indexes) ; - + return proc ; - } - + } -// private int findFirst(List<PatternTriple> pTriples) -// { -// for ( int i = 0 ; i < pTriples.size() ; i++ ) -// if ( pTriples.get(i) != null ) -// return i ; -// return -1 ; -// } - /** Return index of next pattern triple */ - protected int chooseNext(List<PatternTriple> pTriples) - { - if ( DEBUG ) - { + protected int chooseNext(List<PatternTriple> pTriples) { + if ( DEBUG ) { int i = -1 ; - StringBuilder buff = new StringBuilder() ; - for ( PatternTriple pt : pTriples ) - { + StringBuilder buff = new StringBuilder() ; + for ( PatternTriple pt : pTriples ) { i++ ; - if ( pt == null ) - { + if ( pt == null ) { buff.append(String.format(" %d : null\n", i)) ; continue ; } double w2 = weight(pt) ; buff.append(String.format(" %d %8.0f : %s\n", i, w2, printAbbrev(pt))) ; } - String x = StrUtils.noNewlineEnding(buff.toString()); - log.debug(">> Input\n"+x) ; + String x = StrUtils.noNewlineEnding(buff.toString()) ; + log.debug(">> Input\n" + x) ; } - - int idx = processPTriples(pTriples, null) ; - - if ( DEBUG ) - { + + int idx = processPTriples(pTriples, null) ; + + if ( DEBUG ) { String x = printAbbrev(pTriples.get(idx)) ; x = StrUtils.noNewlineEnding(x) ; - log.debug("<< Output\n "+x) ; + log.debug("<< Output\n " + x) ; } return idx ; } /** Return all the indexes of pattern triples of the least weight. */ - protected List<Integer> chooseAll(List<PatternTriple> pTriples) - { + protected List<Integer> chooseAll(List<PatternTriple> pTriples) { List<Integer> results = new ArrayList<>(pTriples.size()) ; processPTriples(pTriples, results) ; return results ; } - - /** Return the index of the first, least triple; optionally accumulate all indexes of the same least weight */ - private int processPTriples(List<PatternTriple> pTriples, List<Integer> results) - { + + /** + * Return the index of the first, least triple; optionally accumulate all indexes of + * the same least weight + */ + private int processPTriples(List<PatternTriple> pTriples, List<Integer> results) { double min = Double.MAX_VALUE ; // Current minimum int N = pTriples.size() ; int idx = -1 ; - - for ( int i = 0 ; i < N ; i++ ) - { + + for ( int i = 0 ; i < N ; i++ ) { PatternTriple pt = pTriples.get(i) ; if ( pt == null ) continue ; double x = weight(pt) ; - if ( x < 0 ) - { + if ( x < 0 ) { // **** DefaultChoice choice = defaultChoice(pt) ; - if ( choice != null ) - { - switch (choice) - { + if ( choice != null ) { + switch (choice) { case FIRST : // Weight very low so it goes to front. x = 0.01 ; @@ -208,29 +186,28 @@ public abstract class ReorderTransformationSubstitution implements ReorderTransf } } - // Not found. No default action. - // Make sure something is returned but otherwise ignore this pattern (goes last). - if ( idx == -1 ) - { + // Not found. No default action. + // Make sure something is returned but otherwise ignore this pattern (goes + // last). + if ( idx == -1 ) { idx = i ; - if ( results != null ) results.add(i) ; + if ( results != null ) + results.add(i) ; } - // Do nothing. Does not update min so will be not be in results. + // Do nothing. Does not update min so will be not be in results. continue ; } - - if ( x == min ) - { - if ( results != null ) results.add(i) ; + + if ( x == min ) { + if ( results != null ) + results.add(i) ; continue ; } - - if ( x < min ) - { + + if ( x < min ) { min = x ; idx = i ; - if ( results != null ) - { + if ( results != null ) { results.clear() ; results.add(i) ; } @@ -238,7 +215,7 @@ public abstract class ReorderTransformationSubstitution implements ReorderTransf } return idx ; } - + /** Return the weight of the pattern, or -1 if no knowledge for it */ protected abstract double weight(PatternTriple pt) ; @@ -256,24 +233,20 @@ public abstract class ReorderTransformationSubstitution implements ReorderTransf protected double defaultWeight(PatternTriple pt) { return -1 ; } /** Update components to note any variables from triple */ - protected static void update(Triple triple, List<PatternTriple> components) - { + protected static void update(Triple triple, List<PatternTriple> components) { for ( PatternTriple elt : components ) if ( elt != null ) update(triple, elt) ; } - private static void update(Triple triple, PatternTriple tuple) - { + private static void update(Triple triple, PatternTriple tuple) { update(triple.getSubject(), tuple) ; update(triple.getPredicate(), tuple) ; update(triple.getObject(), tuple) ; } - private static void update(Node node, PatternTriple elt) - { - if ( Var.isVar(node) ) - { + private static void update(Node node, PatternTriple elt) { + if ( Var.isVar(node) ) { if ( node.equals(elt.subject.getNode()) ) elt.subject = PatternElements.TERM ; if ( node.equals(elt.predicate.getNode()) ) @@ -284,15 +257,13 @@ public abstract class ReorderTransformationSubstitution implements ReorderTransf } /** Update based on a variable/value (c.f. Substitute.substitute) */ - protected static void update(Var var, Node value, List<PatternTriple> components) - { + protected static void update(Var var, Node value, List<PatternTriple> components) { for ( PatternTriple elt : components ) if ( elt != null ) update(var, value, elt) ; } - - private static void update(Var var, Node value, PatternTriple elt) - { + + private static void update(Var var, Node value, PatternTriple elt) { if ( var.equals(elt.subject.getNode()) ) elt.subject = Item.createNode(value) ; if ( var.equals(elt.predicate.getNode()) )