-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
227 changed files
with
11,025 additions
and
3,573 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
...lgebra/src/main/java/org/aksw/jena_sparql_api/algebra/transform/TransformCopyAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
package org.aksw.jena_sparql_api.algebra.transform; | ||
|
||
import java.util.List; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
import org.apache.jena.sparql.algebra.Op; | ||
import org.apache.jena.sparql.algebra.TransformCopy; | ||
import org.apache.jena.sparql.algebra.op.Op1; | ||
import org.apache.jena.sparql.algebra.op.Op2; | ||
import org.apache.jena.sparql.algebra.op.OpAssign; | ||
import org.apache.jena.sparql.algebra.op.OpConditional; | ||
import org.apache.jena.sparql.algebra.op.OpDiff; | ||
import org.apache.jena.sparql.algebra.op.OpDisjunction; | ||
import org.apache.jena.sparql.algebra.op.OpDistinct; | ||
import org.apache.jena.sparql.algebra.op.OpExt; | ||
import org.apache.jena.sparql.algebra.op.OpExtend; | ||
import org.apache.jena.sparql.algebra.op.OpFilter; | ||
import org.apache.jena.sparql.algebra.op.OpGraph; | ||
import org.apache.jena.sparql.algebra.op.OpGroup; | ||
import org.apache.jena.sparql.algebra.op.OpJoin; | ||
import org.apache.jena.sparql.algebra.op.OpLabel; | ||
import org.apache.jena.sparql.algebra.op.OpLeftJoin; | ||
import org.apache.jena.sparql.algebra.op.OpList; | ||
import org.apache.jena.sparql.algebra.op.OpMinus; | ||
import org.apache.jena.sparql.algebra.op.OpN; | ||
import org.apache.jena.sparql.algebra.op.OpOrder; | ||
import org.apache.jena.sparql.algebra.op.OpProcedure; | ||
import org.apache.jena.sparql.algebra.op.OpProject; | ||
import org.apache.jena.sparql.algebra.op.OpPropFunc; | ||
import org.apache.jena.sparql.algebra.op.OpReduced; | ||
import org.apache.jena.sparql.algebra.op.OpSequence; | ||
import org.apache.jena.sparql.algebra.op.OpSlice; | ||
import org.apache.jena.sparql.algebra.op.OpTopN; | ||
import org.apache.jena.sparql.algebra.op.OpUnion; | ||
|
||
/** | ||
* An implementation of {@link TransformCopy} that forwards all calls to transformOpXXX methods | ||
* with the appropriate signature. | ||
* | ||
* @author raven | ||
* | ||
*/ | ||
public class TransformCopyAdapter | ||
extends TransformCopy | ||
{ | ||
public interface TriFunction<A, B, C, R> { | ||
R apply(A a, B b, C c); | ||
} | ||
|
||
protected <OPN extends OpN> Op transformOpN( | ||
OPN op, | ||
List<Op> subOps, | ||
BiFunction<? super OPN, ? super List<Op>, ? extends Op> fallback) { | ||
return fallback.apply(op, subOps); | ||
} | ||
|
||
protected <OP2 extends Op2> Op transformOp2( | ||
OP2 op, | ||
Op left, | ||
Op right, | ||
TriFunction<? super OP2, ? super Op, ? super Op, ? extends Op> fallback) { | ||
return fallback.apply(op, left, right); | ||
} | ||
|
||
protected <OP1 extends Op1> Op transformOp1( | ||
OP1 op, | ||
Op subOp, | ||
BiFunction<? super OP1, ? super Op, ? extends Op> fallback) { | ||
return fallback.apply(op, subOp); | ||
} | ||
|
||
protected Op execOpExt( | ||
OpExt opExt, | ||
Function<? super OpExt, ? extends Op> fallback) { | ||
return fallback.apply(opExt); | ||
} | ||
|
||
// Op1 | ||
@Override | ||
public Op transform(OpFilter op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpGraph op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpProcedure op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpPropFunc op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpLabel op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpAssign op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpExtend op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
// Op2 | ||
@Override | ||
public Op transform(OpJoin op, Op left, Op right) { | ||
return transformOp2(op, left, right, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpLeftJoin op, Op left, Op right) { | ||
return transformOp2(op, left, right, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpDiff op, Op left, Op right) { | ||
return transformOp2(op, left, right, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpMinus op, Op left, Op right) { | ||
return transformOp2(op, left, right, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpUnion op, Op left, Op right) { | ||
return transformOp2(op, left, right, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpConditional op, Op left, Op right) { | ||
return transformOp2(op, left, right, super::transform); | ||
} | ||
|
||
// OpN | ||
@Override | ||
public Op transform(OpSequence op, List<Op> elts) { | ||
return transformOpN(op, elts, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpDisjunction op, List<Op> elts) { | ||
return transformOpN(op, elts, super::transform); | ||
} | ||
|
||
// Extensions | ||
@Override | ||
public Op transform(OpExt opExt) { | ||
return execOpExt(opExt, super::transform); | ||
} | ||
|
||
// OpModifier | ||
@Override | ||
public Op transform(OpList op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpOrder op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpTopN op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpProject op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpDistinct op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpReduced op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpSlice op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
|
||
@Override | ||
public Op transform(OpGroup op, Op subOp) { | ||
return transformOp1(op, subOp, super::transform); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
...-algebra/src/main/java/org/aksw/jena_sparql_api/algebra/transform/TransformEvalTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package org.aksw.jena_sparql_api.algebra.transform; | ||
|
||
import java.util.List; | ||
import java.util.function.BiFunction; | ||
|
||
import org.apache.jena.query.ARQ; | ||
import org.apache.jena.sparql.ARQConstants; | ||
import org.apache.jena.sparql.algebra.Op; | ||
import org.apache.jena.sparql.algebra.Table; | ||
import org.apache.jena.sparql.algebra.op.Op1; | ||
import org.apache.jena.sparql.algebra.op.Op2; | ||
import org.apache.jena.sparql.algebra.op.OpN; | ||
import org.apache.jena.sparql.algebra.op.OpTable; | ||
import org.apache.jena.sparql.algebra.table.TableN; | ||
import org.apache.jena.sparql.core.DatasetGraph; | ||
import org.apache.jena.sparql.core.DatasetGraphFactory; | ||
import org.apache.jena.sparql.engine.ExecutionContext; | ||
import org.apache.jena.sparql.engine.QueryIterator; | ||
import org.apache.jena.sparql.engine.iterator.QueryIterRoot; | ||
import org.apache.jena.sparql.engine.main.OpExecutor; | ||
import org.apache.jena.sparql.engine.main.OpExecutorFactory; | ||
import org.apache.jena.sparql.util.Context; | ||
import org.apache.jena.sparql.util.NodeFactoryExtra; | ||
|
||
/** | ||
* A transformer that evaluates every op whose sub ops are all {@link OpTable} instances | ||
* to another {@link OpTable} instance. | ||
* Note, that this mechanism materializes every intermediate result as a table. | ||
* | ||
* A more efficient approach may be to evaluate sub-expressions of an op using an {@link OpExecutor} and | ||
* substituting the roots of these sub-expressions in op with the obtained results. | ||
* | ||
* @author raven | ||
* | ||
*/ | ||
public class TransformEvalTable | ||
extends TransformCopyAdapter | ||
{ | ||
protected OpExecutor opExecutor; | ||
protected ExecutionContext execCxt; | ||
|
||
public TransformEvalTable(OpExecutor opExecutor, ExecutionContext execCxt) { | ||
super(); | ||
this.opExecutor = opExecutor; | ||
this.execCxt = execCxt; | ||
} | ||
|
||
public static TransformEvalTable create() { | ||
OpExecutorFactory opExecutorFactory = OpExecutor.stdFactory; | ||
ExecutionContext execCxt = createExecCxt(opExecutorFactory); | ||
OpExecutor opExecutor = opExecutorFactory.create(execCxt); | ||
|
||
return new TransformEvalTable(opExecutor, execCxt); | ||
} | ||
|
||
//protected QueryIterRoot | ||
public static ExecutionContext createExecCxt(OpExecutorFactory opExecutorFactory) { | ||
Context cxt = ARQ.getContext().copy() ; | ||
cxt.set(ARQConstants.sysCurrentTime, NodeFactoryExtra.nowAsDateTime()) ; | ||
DatasetGraph dataset = DatasetGraphFactory.create(); | ||
ExecutionContext execCxt = new ExecutionContext(cxt, dataset.getDefaultGraph(), dataset, opExecutorFactory); | ||
|
||
return execCxt; | ||
} | ||
|
||
public OpTable exec(Op op) { | ||
QueryIterator queryIter = opExecutor.executeOp(op , QueryIterRoot.create(execCxt)); | ||
Table table = new TableN(queryIter); | ||
return OpTable.create(table); | ||
} | ||
|
||
@Override | ||
protected <OPN extends OpN> Op transformOpN(OPN op, List<Op> subOps, BiFunction<? super OPN, ? super List<Op>, ? extends Op> fallback) { | ||
Op result; | ||
|
||
boolean isAllTables = subOps.stream().allMatch(subOp -> subOp instanceof OpTable); | ||
if (isAllTables) { | ||
Op tmp = op.copy(subOps); | ||
result = exec(tmp); | ||
} else { | ||
result = fallback.apply(op, subOps); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
protected <OP2 extends Op2> Op transformOp2(OP2 op, Op left, Op right, TriFunction<? super OP2, ? super Op, ? super Op, ? extends Op> fallback) { | ||
Op result; | ||
|
||
if (left instanceof OpTable && right instanceof OpTable) { | ||
Op tmp = op.copy(left, right); | ||
result = exec(tmp); | ||
} else { | ||
result = fallback.apply(op, left, right); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
protected <OP1 extends Op1> Op transformOp1(OP1 op, Op subOp, BiFunction<? super OP1, ? super Op, ? extends Op> fallback) { | ||
Op result = null; | ||
|
||
if (subOp instanceof OpTable) { | ||
Op tmp = op.copy(subOp); | ||
result = exec(tmp); | ||
} else { | ||
result = fallback.apply(op, subOp); | ||
} | ||
|
||
return result; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...main/java/org/aksw/jena_sparql_api/algebra/transform/TransformExpandAggCountDistinct.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.aksw.jena_sparql_api.algebra.transform; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.apache.jena.sparql.algebra.Op; | ||
import org.apache.jena.sparql.algebra.TransformCopy; | ||
import org.apache.jena.sparql.algebra.op.OpDistinct; | ||
import org.apache.jena.sparql.algebra.op.OpGroup; | ||
import org.apache.jena.sparql.core.Var; | ||
import org.apache.jena.sparql.expr.ExprAggregator; | ||
import org.apache.jena.sparql.expr.aggregate.AggCount; | ||
import org.apache.jena.sparql.expr.aggregate.AggCountDistinct; | ||
import org.apache.jena.sparql.expr.aggregate.Aggregator; | ||
|
||
/** | ||
* Transform GROUP([count(distinct *) AS ?.0], subOp) | ||
* to GROUP([count(*) AS ?.0], DISTINCT(subOp)) | ||
* | ||
* Used to mitigate a bug in Virtuoso | ||
* | ||
* @author raven | ||
* | ||
*/ | ||
public class TransformExpandAggCountDistinct | ||
extends TransformCopy | ||
{ | ||
@Override | ||
public Op transform(OpGroup op, Op subOp) { | ||
|
||
Op tmp = null; | ||
|
||
List<ExprAggregator> eas = op.getAggregators(); | ||
if (eas.size() == 1) { | ||
ExprAggregator ea = eas.get(0); | ||
Var ev = ea.getVar(); | ||
Aggregator a = ea.getAggregator(); | ||
if (a instanceof AggCountDistinct) { | ||
tmp = new OpGroup( | ||
new OpDistinct(subOp), | ||
op.getGroupVars(), | ||
Collections.singletonList(new ExprAggregator(ev, new AggCount()))); | ||
} | ||
} | ||
|
||
Op result = tmp != null | ||
? tmp | ||
: super.transform(op, subOp); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.