Skip to content
Browse files

Begin to form into a BGP solver.

  • Loading branch information...
1 parent 422518e commit 196dc8445d814e8319e4330f8b6a5f53baf80c6f Andy Seaborne committed
View
93 src/main/java/projects/merge/Main.java
@@ -25,9 +25,7 @@
import org.openjena.atlas.lib.Tuple ;
import org.openjena.atlas.logging.Log ;
-import com.hp.hpl.jena.graph.Node ;
import com.hp.hpl.jena.graph.Triple ;
-import com.hp.hpl.jena.sparql.core.Var ;
import com.hp.hpl.jena.sparql.sse.SSE ;
import com.hp.hpl.jena.tdb.base.file.Location ;
import com.hp.hpl.jena.tdb.index.TupleIndex ;
@@ -55,8 +53,11 @@ public static void main(String[] args)
TupleIndex [] indexes = { POS, PSO } ;
// Add "columns" to indexes.
- Tuple<ColNames> primary = Tuple.create(S,P,O) ;
- System.out.println(POS.getColumnMap().map(primary)) ;
+ if ( false )
+ {
+ Tuple<ColNames> primary = Tuple.create(S,P,O) ;
+ System.out.println(POS.getColumnMap().map(primary)) ;
+ }
// Setup
@@ -68,7 +69,6 @@ public static void main(String[] args)
test("(?x <p> ?x)", "(?x <q> ?v)", indexes, PSO, PSO) ;
test("(?a <p> ?b)", "(?c <q> ?d)", indexes, PSO, PSO) ;
-
}
private static void test(String tripleStr1, String tripleStr2, TupleIndex[] indexes, TupleIndex index1, TupleIndex index2)
@@ -83,7 +83,7 @@ private static void test(String tripleStr1, String tripleStr2, TupleIndex[] inde
System.out.println() ;
//System.out.println("{"+triple1+"} {"+triple2+"}") ;
- MergeAction action = choose(triple1, triple2, indexes) ;
+ MergeActionIdxIdx action = MergeLib.calcMergeAction(triple1, triple2, indexes) ;
if ( action == null )
{
@@ -99,85 +99,4 @@ private static void test(String tripleStr1, String tripleStr2, TupleIndex[] inde
System.out.println("** "+action) ;
System.out.println() ;
}
-
- private static MergeAction choose(Triple triple1, Triple triple2, TupleIndex[] indexes)
- {
- IndexAccess[] access1 = access(triple1, indexes) ;
- IndexAccess[] access2 = access(triple2, indexes) ;
-
-// System.out.println(Arrays.asList(access1)) ;
-// System.out.println(Arrays.asList(access2)) ;
-
- MergeAction action = null ;
- for ( IndexAccess a1 : access1 )
- {
- if ( a1 == null ) continue ;
- for ( IndexAccess a2 : access2 )
- {
- if ( a2 == null ) continue ;
- if ( a1.getVar().equals(a2.getVar()))
- {
- MergeAction action2 = new MergeAction(a1,a2) ;
- // Longest prefixes.
- if ( action == null )
- action = action2 ;
- else
- {
- System.out.println("Choose: "+action+" // "+action2) ;
- // Choose one with most prefixing.
- int len1 = action.getIndexAccess1().getPrefixLen()+action.getIndexAccess2().getPrefixLen() ;
- int len2 = action2.getIndexAccess1().getPrefixLen()+action2.getIndexAccess2().getPrefixLen() ;
- if ( len2 == len1 )
- {
- // Example: same var uses more than once in a triple.
- if ( action2.getIndexAccess1().getIndex() == action2.getIndexAccess2().getIndex() )
- // Prefer same index.
- // Better - special action.
- action = action2 ;
- }
- else if ( len2 > len1 )
- action = action2 ;
- // else do nothing.
- }
-
-
-
- }
- }
- }
- return action ;
- }
-
- private static IndexAccess[] access(Triple _triple, TupleIndex[] indexes)
- {
- IndexAccess[] accesses = new IndexAccess[indexes.length] ;
- Tuple<Node> triple = tripleAsTuple(_triple) ;
- int i = 0 ;
- for ( TupleIndex idx : indexes )
- {
- IndexAccess a = access(triple, idx) ;
- accesses[i++] = a ;
- }
-
- return accesses ;
- }
-
- private static IndexAccess access(Tuple<Node> triple, TupleIndex idx)
- {
- Tuple<Node> t = idx.getColumnMap().map(triple) ;
- for ( int i = 0 ; i < triple.size() ; i++ )
- {
- Node n = t.get(i) ;
- if ( Var.isVar(n) )
- return new IndexAccess(idx, i, Var.alloc(n)) ;
- }
- return null ;
- }
-
- private static Tuple<Node> tripleAsTuple(Triple triple)
- {
- return Tuple.create(triple.getSubject(),
- triple.getPredicate(),
- triple.getObject()) ;
- }
}
View
17 ...main/java/projects/merge/MergeAction.java → ...ava/projects/merge/MergeActionIdxIdx.java
@@ -18,12 +18,15 @@
package projects.merge;
-public class MergeAction
+import com.hp.hpl.jena.sparql.core.Var ;
+
+public class MergeActionIdxIdx
{
private IndexAccess indexAccess1 ;
private IndexAccess indexAccess2 ;
+ private Var var ;
- public MergeAction(IndexAccess indexAccess1, IndexAccess indexAccess2)
+ public MergeActionIdxIdx(IndexAccess indexAccess1, IndexAccess indexAccess2)
{
super() ;
this.indexAccess1 = indexAccess1 ;
@@ -31,6 +34,7 @@ public MergeAction(IndexAccess indexAccess1, IndexAccess indexAccess2)
if ( ! indexAccess1.getVar().equals(indexAccess2.getVar()) )
// May relax this and allow (?x = ?y AS ?z)
throw new InternalError("IndexAcceses in a merge must be joining the same variable") ;
+ this.var = indexAccess1.getVar() ;
}
public IndexAccess getIndexAccess1()
@@ -43,14 +47,21 @@ public IndexAccess getIndexAccess2()
return indexAccess2 ;
}
+ public Var getVar()
+ {
+ return var ;
+ }
+
@Override
public String toString()
{
StringBuilder builder = new StringBuilder() ;
- builder.append("MergeAction [") ;
+ builder.append("Merge [") ;
builder.append(indexAccess1) ;
builder.append(",") ;
builder.append(indexAccess2) ;
+// builder.append(",") ;
+// builder.append(var) ;
builder.append("]") ;
return builder.toString() ;
}
View
57 src/main/java/projects/merge/MergeActionVarIdx.java
@@ -0,0 +1,57 @@
+/**
+ * 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 projects.merge;
+
+import com.hp.hpl.jena.sparql.core.Var ;
+
+public class MergeActionVarIdx
+{
+
+ private Var var ;
+ private IndexAccess indexAccess ;
+
+ public MergeActionVarIdx(Var var, IndexAccess indexAccess)
+ {
+ this.var = var ;
+ this.indexAccess = indexAccess ;
+ }
+
+ public Var getVar()
+ {
+ return var ;
+ }
+
+ public IndexAccess getIndexAccess()
+ {
+ return indexAccess ;
+ }
+
+ @Override
+ public String toString()
+ {
+ StringBuilder builder = new StringBuilder() ;
+ builder.append("Merge [") ;
+ builder.append(var) ;
+ builder.append(",") ;
+ builder.append(indexAccess) ;
+ builder.append("]") ;
+ return builder.toString() ;
+ }
+}
+
View
122 src/main/java/projects/merge/MergeLib.java
@@ -0,0 +1,122 @@
+/**
+ * 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 projects.merge;
+
+import org.openjena.atlas.lib.Tuple ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.core.Var ;
+import com.hp.hpl.jena.tdb.index.TupleIndex ;
+
+public class MergeLib
+{
+
+ public static MergeActionIdxIdx calcMergeAction(Triple triple1, Triple triple2, TupleIndex[] indexes)
+ {
+ IndexAccess[] access1 = access(triple1, indexes) ;
+ IndexAccess[] access2 = access(triple2, indexes) ;
+
+// System.out.println(Arrays.asList(access1)) ;
+// System.out.println(Arrays.asList(access2)) ;
+ // Special case? access1.length=1, access2.length=1
+
+ MergeActionIdxIdx action = null ;
+ for ( IndexAccess a1 : access1 )
+ {
+ if ( a1 == null ) continue ;
+ for ( IndexAccess a2 : access2 )
+ {
+ if ( a2 == null ) continue ;
+ if ( a1.getVar().equals(a2.getVar()))
+ {
+ MergeActionIdxIdx action2 = new MergeActionIdxIdx(a1,a2) ;
+ // Longest prefixes.
+ if ( action == null )
+ action = action2 ;
+ else
+ {
+ //System.out.println("Choose: "+action+" // "+action2) ;
+ // Choose one with most prefixing.
+ int len1 = action.getIndexAccess1().getPrefixLen()+action.getIndexAccess2().getPrefixLen() ;
+ int len2 = action2.getIndexAccess1().getPrefixLen()+action2.getIndexAccess2().getPrefixLen() ;
+ if ( len2 == len1 )
+ {
+ // Example: same var uses more than once in a triple.
+ if ( action2.getIndexAccess1().getIndex() == action2.getIndexAccess2().getIndex() )
+ // Prefer same index.
+ // Better - special action.
+ action = action2 ;
+ }
+ else if ( len2 > len1 )
+ action = action2 ;
+ // else do nothing.
+ }
+ }
+ }
+ }
+ return action ;
+ }
+
+ public static MergeActionVarIdx calcMergeAction(Var var, Triple triple, TupleIndex[] indexes)
+ {
+ IndexAccess[] access = access(triple, indexes) ;
+ for ( IndexAccess a : access )
+ {
+ if ( var.equals(a.getVar()) )
+ return new MergeActionVarIdx(var, a) ;
+ }
+ return null ;
+ }
+
+ private static IndexAccess[] access(Triple _triple, TupleIndex[] indexes)
+ {
+ IndexAccess[] accesses = new IndexAccess[indexes.length] ;
+ Tuple<Node> triple = tripleAsTuple(_triple) ;
+ int i = 0 ;
+ for ( TupleIndex idx : indexes )
+ {
+ IndexAccess a = access(triple, idx) ;
+ accesses[i++] = a ;
+ }
+
+ return accesses ;
+ }
+
+ private static IndexAccess access(Tuple<Node> triple, TupleIndex idx)
+ {
+ Tuple<Node> t = idx.getColumnMap().map(triple) ;
+ for ( int i = 0 ; i < triple.size() ; i++ )
+ {
+ Node n = t.get(i) ;
+ if ( Var.isVar(n) )
+ return new IndexAccess(idx, i, Var.alloc(n)) ;
+ }
+ return null ;
+ }
+
+ private static Tuple<Node> tripleAsTuple(Triple triple)
+ {
+ return Tuple.create(triple.getSubject(),
+ triple.getPredicate(),
+ triple.getObject()) ;
+ }
+
+}
+
View
66 src/main/java/projects/merge/OpExecutorMerge.java
@@ -0,0 +1,66 @@
+/**
+ * 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 projects.merge;
+
+import java.util.Iterator ;
+import java.util.List ;
+
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.algebra.op.OpBGP ;
+import com.hp.hpl.jena.sparql.core.BasicPattern ;
+import com.hp.hpl.jena.sparql.core.Var ;
+import com.hp.hpl.jena.tdb.index.TupleIndex ;
+import com.hp.hpl.jena.tdb.solver.BindingNodeId ;
+import com.hp.hpl.jena.tdb.store.DatasetGraphTDB ;
+
+public class OpExecutorMerge
+{
+ // (real) default graph only for now.
+ public Iterator<BindingNodeId> execute(OpBGP opBGP, DatasetGraphTDB dsg)
+ {
+ BasicPattern bgp = opBGP.getPattern() ;
+ List<Triple> triples = bgp.getList() ;
+
+ if (triples.size() == 0 )
+ {}
+ if (triples.size() == 1 )
+ {}
+ Triple triple1 = triples.get(0) ;
+ Triple triple2 = triples.get(1) ;
+
+ TupleIndex[] indexes = dsg.getTripleTable().getNodeTupleTable().getTupleTable().getIndexes() ;
+
+ MergeActionIdxIdx action = MergeLib.calcMergeAction(triple1, triple2, indexes) ;
+
+ // perform action in NodeId space, get iterator of Bindings.
+
+ Iterator<BindingNodeId> chain = null ;
+ // which comes out in sorted var order.
+
+ Var v = action.getVar() ;
+ for ( int i = 2 ; i < triples.size() ; )
+ {
+ // Next triples.
+ Triple triple = triples.get(i) ;
+ // Is it joined by a common variable?
+ MergeActionVarIdx action2 = MergeLib.calcMergeAction(v, triple, indexes) ;
+ }
+ return null ;
+ }
+}

0 comments on commit 196dc84

Please sign in to comment.
Something went wrong with that request. Please try again.