use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.
the class QueryNodesToTupleExpr method getNewJoin.
// build newJoin node given remaining joinArgs and chain of filters
private static TupleExpr getNewJoin(List<QueryModelNode> args, List<Filter> filterChain) {
TupleExpr newJoin;
TupleExpr tempJoin;
final List<TupleExpr> joinArgs = Lists.newArrayList();
for (QueryModelNode q : args) {
if (q instanceof TupleExpr) {
joinArgs.add(0, (TupleExpr) q);
} else {
throw new IllegalArgumentException("Invalid query node!");
}
}
if (joinArgs.size() > 1) {
TupleExpr left = joinArgs.remove(0);
TupleExpr right = joinArgs.remove(0);
tempJoin = getJoin(left, right);
for (int i = joinArgs.size() - 1; i >= 0; i--) {
tempJoin = getJoin(tempJoin, joinArgs.get(i));
}
if (filterChain.size() == 0) {
newJoin = tempJoin;
} else if (filterChain.size() == 1) {
newJoin = filterChain.get(0);
((Filter) newJoin).setArg(tempJoin);
} else {
newJoin = filterChain.get(0);
filterChain.get(1).setArg(tempJoin);
}
} else if (joinArgs.size() == 1) {
tempJoin = joinArgs.get(0);
if (filterChain.size() == 0) {
newJoin = tempJoin;
} else if (filterChain.size() == 1) {
newJoin = filterChain.get(0);
((Filter) newJoin).setArg(tempJoin);
} else {
newJoin = filterChain.get(0);
filterChain.get(1).setArg(tempJoin);
}
} else {
throw new IllegalStateException("JoinArgs size cannot be zero.");
}
return newJoin;
}
use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.
the class OptionalJoinSegment method replaceWithExternalSet.
/**
* This method matches the ordered nodes returned by
* {@link OptionalJoinSegment #getOrderedNodes()} for nodeToReplace with a subset of
* the ordered nodes for this OptionalJoinSegment. The order of the nodes for
* nodeToReplace must match the order of the nodes as a subset of
* orderedNodes
*
* @param segment
* - nodes to be replaced by ExternalSet node
* @param set
* - ExternalSet node that will replace specified query nodes
*/
@Override
public boolean replaceWithExternalSet(QuerySegment<T> segment, T set) {
Preconditions.checkNotNull(segment != null);
Preconditions.checkNotNull(set);
if (!containsQuerySegment(segment)) {
return false;
}
List<QueryModelNode> nodeList = segment.getOrderedNodes();
int begin = orderedNodes.indexOf(nodeList.get(0));
// TODO this assumes no duplicate nodes
if (begin < 0 || begin + nodeList.size() > orderedNodes.size() || !nodeList.equals(orderedNodes.subList(begin, begin + nodeList.size()))) {
return false;
}
orderedNodes.removeAll(nodeList);
orderedNodes.add(begin, set);
unorderedNodes.removeAll(nodeList);
unorderedNodes.add(set);
for (QueryModelNode q : nodeList) {
if (q instanceof ValueExpr) {
conditionMap.remove(q);
}
}
return true;
}
use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.
the class QueryNodeConsolidator method updateLeftJoinNodes.
// updates the var counts in specified left join index
private void updateLeftJoinNodes(PositionNode node, int finalPos) {
if (node.getNode() instanceof ValueExpr) {
return;
}
int diff = finalPos - node.getPosition();
if (diff == 0) {
return;
}
if (node.isOptional) {
leftJoinPosSet.remove(node);
FlattenedOptional optional = (FlattenedOptional) node.getNode();
if (diff < 0) {
for (int i = node.getPosition() - 1; i > finalPos - 1; i--) {
QueryModelNode tempNode = queryNodes.get(i);
if (tempNode instanceof ValueExpr) {
continue;
}
optional.addArg((TupleExpr) tempNode);
}
} else {
for (int i = node.getPosition() + 1; i < finalPos + 1; i++) {
QueryModelNode tempNode = queryNodes.get(i);
if (tempNode instanceof ValueExpr) {
continue;
}
optional.removeArg((TupleExpr) tempNode);
}
}
node.setNode(optional);
// FlattenedOptional equals does not take into account var counts
// The following three lines update the var count in the optional in
// list
int index = queryNodes.indexOf(optional);
queryNodes.remove(optional);
queryNodes.add(index, optional);
leftJoinPosSet.add(node);
} else {
TupleExpr te = (TupleExpr) node.getNode();
SortedSet<PositionNode> optionals;
if (diff < 0) {
optionals = leftJoinPosSet.subSet(new PositionNode(finalPos), true, node, false);
for (PositionNode pNode : optionals) {
FlattenedOptional optional = (FlattenedOptional) pNode.getNode();
optional.removeArg(te);
}
} else {
optionals = leftJoinPosSet.subSet(node, false, new PositionNode(finalPos), true);
for (PositionNode pNode : optionals) {
FlattenedOptional optional = (FlattenedOptional) pNode.getNode();
optional.addArg(te);
}
}
}
}
use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.
the class SparqlFluoQueryBuilder method getConstructGraphVarOrder.
private static VariableOrder getConstructGraphVarOrder(final Reduced node) {
// get child node
final QueryModelNode child = node.getArg();
Preconditions.checkArgument(child instanceof Projection || child instanceof MultiProjection);
final UnaryTupleOperator unary = (UnaryTupleOperator) child;
// get ProjectionElemList to build ConstructGraph
final List<ProjectionElemList> projections = new ArrayList<>();
if (unary instanceof Projection) {
projections.add(((Projection) unary).getProjectionElemList());
} else {
projections.addAll(((MultiProjection) unary).getProjections());
}
return getConstructGraphVarOrder(projections);
}
use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.
the class GeneralizedExternalProcessor method getVarNames.
private static Set<String> getVarNames(Collection<QueryModelNode> nodes) {
List<String> tempVars;
Set<String> nodeVarNames = Sets.newHashSet();
for (QueryModelNode s : nodes) {
tempVars = VarCollector.process(s);
for (String t : tempVars) {
nodeVarNames.add(t);
}
}
return nodeVarNames;
}
Aggregations