if (vba != null) {
intermediate = getVarBuckets(tp, vba);
if (intermediate.keySet().containsAll(joinPartnersTP)) {
boolean flag = true;
for (final Variable v : joinPartnersTP) {
final Literal min = (minima == null) ? null : minima
.get(v);
if (intermediate.get(v) == null
|| min != null
&& (intermediate.get(v).minimum == null || min
.compareToNotNecessarilySPARQLSpecificationConform(intermediate
.get(v).minimum) != 0)) {
flag = false;
break;
}
final Literal max = (maxima == null) ? null : maxima
.get(v);
if (max != null
&& (intermediate.get(v).maximum == null || max
.compareToNotNecessarilySPARQLSpecificationConform(intermediate
.get(v).maximum) != 0)) {
flag = false;
break;
}
}
if (flag) {
intermediate.keySet().retainAll(joinPartnersTP);
// if (classBindings == BindingsArrayVarMinMax.class) {
for (final Variable v : intermediate.keySet()) {
final VarBucket vb = intermediate.get(v);
final Literal l[] = new Literal[vb.selectivityOfInterval
.size()];
int indexLiteral = 0;
for (final Entry entry : vb.selectivityOfInterval) {
l[indexLiteral] = entry.literal;
indexLiteral++;
}
tp.addHistogram(v, l, vb.getSum());
}
// }
return intermediate;
}
}
}
}
final Map<Variable, VarBucket> result = new HashMap<Variable, VarBucket>();
for (final Variable v : joinPartnersTP) {
final Literal min = (minima == null) ? null : minima.get(v);
final Literal max = (maxima == null) ? null : maxima.get(v);
if (intermediate != null && intermediate.containsKey(v)) {
boolean flag = true;
if (intermediate.get(v) == null
|| min != null
&& (intermediate.get(v).minimum == null || min
.compareToNotNecessarilySPARQLSpecificationConform(intermediate
.get(v).minimum) != 0)) {
flag = false;
}
if (max != null
&& (intermediate.get(v).maximum == null || max
.compareToNotNecessarilySPARQLSpecificationConform(intermediate
.get(v).maximum) != 0)) {
flag = false;
}
if (flag) {
result.put(v, intermediate.get(v));
continue;
}
}
try {
// get the graph constraint from the super class.
// If it is null, a default graph is used, if not null a named
// one
// is used
final Item graphConstraintItem = this.getGraphConstraint();
// get a collection of indices using the determined graph
// constraint
final Collection<Indices> indicesC = this.root.dataset.indexingRDFGraphs(
graphConstraintItem, false, false, this.root);
if ((indicesC != null) && !(indicesC.size() == 0)) {
final Triple key = getKey(tp, null);
final Collection<URILiteral> namedGraphs = new ArrayList<URILiteral>();
// if the graph constraint is not null (which means that a
// named
// graph is used)
if (graphConstraintItem != null) {
if (graphConstraintItem instanceof Variable) {
final Variable graphConstraint = (Variable) graphConstraintItem;
// check if named graphs were provided at query time
if (this.root.namedGraphs != null
&& this.root.namedGraphs.size() > 0) {
// Convert the named graphs' names into
// URILiterals
// to be applicable
// later on
for (final String name : this.root.namedGraphs) {
final Indices indices = this.root.dataset
.getNamedGraphIndices(LiteralFactory
.createURILiteralWithoutLazyLiteral(name));
final URILiteral rdfName = indices
.getRdfName();
if (namedGraphs.contains(rdfName)) {
final TriplePattern ztp = new TriplePattern(
graphConstraint.equals(tp
.getPos(0)) ? rdfName
: tp.getPos(0),
graphConstraint.equals(tp
.getPos(1)) ? rdfName
: tp.getPos(1),
graphConstraint.equals(tp
.getPos(2)) ? rdfName
: tp.getPos(2));
final Triple zkey = getKey(ztp, null);
final Triple keyMinimum = this.getKey(ztp,
null, minima);
final Triple keyMaximum = this.getKey(ztp,
null, maxima);
final VarBucket vb = this.getVarBucket(v,
ztp, zkey, keyMinimum,
keyMaximum,
(SixIndices) indices);
if (vb != null) {
final VarBucket previous_vb = result
.get(v);
if (previous_vb != null) {
vb.add(previous_vb);
}
vb.minimum = (minima == null) ? null
: minima.get(v);
vb.maximum = (maxima == null) ? null
: maxima.get(v);
result.put(v, vb);
}
}
}
}
// otherwise there might have been named graphs
// added
// during the evaluation
else {
// get all indices of named graphs and bind them
// to
// the graph constraint
final Collection<Indices> dataSetIndices = this.root.dataset
.getNamedGraphIndices();
if (dataSetIndices != null) {
for (final Indices indices : dataSetIndices) {
final TriplePattern ztp = new TriplePattern(
graphConstraint.equals(tp
.getPos(0)) ? indices
.getRdfName() : tp
.getPos(0),
graphConstraint.equals(tp
.getPos(1)) ? indices
.getRdfName() : tp
.getPos(1),
graphConstraint.equals(tp
.getPos(2)) ? indices
.getRdfName() : tp
.getPos(2));
final Triple zkey = getKey(ztp, null);
final Triple keyMinimum = this.getKey(ztp,
null, minima);
final Triple keyMaximum = this.getKey(ztp,
null, maxima);
final VarBucket vb = this.getVarBucket(v,
ztp, zkey, keyMinimum,
keyMaximum,
(SixIndices) indices);
if (vb != null) {
final VarBucket previous_vb = result
.get(v);
if (previous_vb != null) {
vb.add(previous_vb);
}
vb.minimum = (minima == null) ? null
: minima.get(v);
vb.maximum = (maxima == null) ? null
: maxima.get(v);
result.put(v, vb);
}
}
}
}
}
// if the graph constraint is an URILiteral fetch the
// matching indices object
// but do not bind anything
else {
final Triple keyMinimum = this.getKey(tp, null, minima);
final Triple keyMaximum = this.getKey(tp, null, maxima);
for (final Indices indices : indicesC) {
final URILiteral rdfName = indices.getRdfName();
if (namedGraphs.contains(rdfName)) {
final VarBucket vb = this.getVarBucket(v, tp,
key, keyMinimum, keyMaximum,
(SixIndices) indices);
if (vb != null) {
final VarBucket previous_vb = result
.get(v);
if (previous_vb != null) {
vb.add(previous_vb);
}
vb.minimum = (minima == null) ? null
: minima.get(v);
vb.maximum = (maxima == null) ? null
: maxima.get(v);
result.put(v, vb);
}
}
}
}
} else {
if (indicesC != null) {
// deal with special case: several default graphs!
// if (triplePatterns.size() != 1) {
//log.error("Can only process one triple pattern!");
// }
final Triple keyMinimum = this.getKey(tp, null, minima);
final Triple keyMaximum = this.getKey(tp, null, maxima);
for (final Indices indices : indicesC) {
final VarBucket vb = this.getVarBucket(v, tp, key,
keyMinimum, keyMaximum,
(SixIndices) indices);
if (vb != null) {
vb.minimum = (minima == null) ? null
: minima.get(v);
vb.maximum = (maxima == null) ? null
: maxima.get(v);
final VarBucket previous_vb = result.get(v);
if (previous_vb != null) {
vb.add(previous_vb);
}
result.put(v, vb);
}
}
}
}
}
} catch (final Exception e) {
System.err.println("Error while joining triple patterns: "+ e);
e.printStackTrace();
return null;
}
}
// if (classBindings == BindingsArrayVarMinMax.class) {
for (final Variable v : result.keySet()) {
final VarBucket vb = result.get(v);
final Literal l[] = new Literal[vb.selectivityOfInterval.size()];
int indexLiteral = 0;
for (final Entry entry : vb.selectivityOfInterval) {
l[indexLiteral] = entry.literal;
indexLiteral++;
}