@Override public UnionNode transform(UnionNode unionNode){ return iqFactory.createUnionNode(renameProjectedVars(unionNode.getVariables())); // return unionNode.clone(); }
@Override public UnionNode transform(UnionNode unionNode){ return iqFactory.createUnionNode(renameProjectedVars(unionNode.getVariables())); // return unionNode.clone(); }
private Optional<ExplicitVariableProjectionNode> getProjectorReplacementNode(ExplicitVariableProjectionNode replacedNode, ImmutableExpression expressionToPropagate) { if (expressionToPropagate.getVariables().size() == 0) { return Optional.empty(); } ImmutableSet.Builder<Variable> allProjectedVariablesBuilder = ImmutableSet.builder(); allProjectedVariablesBuilder.addAll(replacedNode.getVariables()); allProjectedVariablesBuilder.addAll(expressionToPropagate.getVariables()); if (replacedNode instanceof UnionNode) { return Optional.of(iqFactory.createUnionNode(allProjectedVariablesBuilder.build())); } if (replacedNode instanceof ConstructionNode) { return Optional.of(iqFactory.createConstructionNode(allProjectedVariablesBuilder.build(), ((ConstructionNode) replacedNode).getSubstitution())); } throw new IllegalStateException("Unsupported node type"); }
@Override public IQTree applyDescendingSubstitutionWithoutOptimizing( ImmutableSubstitution<? extends VariableOrGroundTerm> descendingSubstitution, ImmutableList<IQTree> children) { ImmutableSet<Variable> updatedProjectedVariables = constructionTools.computeNewProjectedVariables( descendingSubstitution, projectedVariables); ImmutableList<IQTree> updatedChildren = children.stream() .map(c -> c.applyDescendingSubstitutionWithoutOptimizing(descendingSubstitution)) .collect(ImmutableCollectors.toList()); UnionNode newRootNode = iqFactory.createUnionNode(updatedProjectedVariables); return iqFactory.createNaryIQTree(newRootNode, updatedChildren); }
private Optional<ExplicitVariableProjectionNode> getProjectorReplacementNode(ExplicitVariableProjectionNode replacedNode, ImmutableExpression expressionToPropagate) { if (expressionToPropagate.getVariables().size() == 0) { return Optional.empty(); } ImmutableSet.Builder<Variable> allProjectedVariablesBuilder = ImmutableSet.builder(); allProjectedVariablesBuilder.addAll(replacedNode.getVariables()); allProjectedVariablesBuilder.addAll(expressionToPropagate.getVariables()); if (replacedNode instanceof UnionNode) { return Optional.of(iqFactory.createUnionNode(allProjectedVariablesBuilder.build())); } if (replacedNode instanceof ConstructionNode) { return Optional.of(iqFactory.createConstructionNode(allProjectedVariablesBuilder.build(), ((ConstructionNode) replacedNode).getSubstitution(), ((ConstructionNode) replacedNode).getOptionalModifiers())); } throw new IllegalStateException("Unsupported node type"); }
private IQTree liftUnionChild(int childIndex, NaryIQTree newUnionChild, ImmutableList<IQTree> initialChildren) { UnionNode newUnionNode = iqFactory.createUnionNode(initialChildren.stream() .flatMap(c -> c.getVariables().stream()) .collect(ImmutableCollectors.toSet())); return iqFactory.createNaryIQTree(newUnionNode, newUnionChild.getChildren().stream() .map(unionGrandChild -> createJoinSubtree(childIndex, unionGrandChild, initialChildren)) .collect(ImmutableCollectors.toList())); }
@Override public IQTree applyDescendingSubstitution(ImmutableSubstitution<? extends VariableOrGroundTerm> descendingSubstitution, Optional<ImmutableExpression> constraint, ImmutableList<IQTree> children) { ImmutableSet<Variable> updatedProjectedVariables = constructionTools.computeNewProjectedVariables( descendingSubstitution, projectedVariables); ImmutableList<IQTree> updatedChildren = children.stream() .map(c -> c.applyDescendingSubstitution(descendingSubstitution, constraint)) .filter(c -> !c.isDeclaredAsEmpty()) .collect(ImmutableCollectors.toList()); switch (updatedChildren.size()) { case 0: return iqFactory.createEmptyNode(updatedProjectedVariables); case 1: return updatedChildren.get(0); default: UnionNode newRootNode = iqFactory.createUnionNode(updatedProjectedVariables); return iqFactory.createNaryIQTree(newRootNode, updatedChildren); } }
@Override public IQTree liftIncompatibleDefinitions(Variable variable, IQTree leftChild, IQTree rightChild) { if (leftChild.getVariables().contains(variable)) { IQTree liftedLeftChild = leftChild.liftIncompatibleDefinitions(variable); QueryNode leftChildRoot = liftedLeftChild.getRootNode(); if (leftChildRoot instanceof UnionNode && ((UnionNode) leftChildRoot).hasAChildWithLiftableDefinition(variable, leftChild.getChildren())) { UnionNode newUnionNode = iqFactory.createUnionNode( Stream.of(leftChild, rightChild) .flatMap(c -> c.getVariables().stream()) .collect(ImmutableCollectors.toSet())); return iqFactory.createNaryIQTree(newUnionNode, liftedLeftChild.getChildren().stream() .map(unionChild -> (IQTree) iqFactory.createBinaryNonCommutativeIQTree(this, unionChild, rightChild)) .collect(ImmutableCollectors.toList())); } } // By default, nothing lifted return iqFactory.createBinaryNonCommutativeIQTree(this, leftChild, rightChild); }
UnionNode newUnionNode = iqFactory.createUnionNode(unionVariables);
.collect(ImmutableCollectors.toList()); IQTree unionTree = iqFactory.createNaryIQTree(iqFactory.createUnionNode(projectionAtom.getVariables()), unionChildren);
@Override public IQTree liftIncompatibleDefinitions(Variable variable, IQTree child) { if (!childVariables.contains(variable)) { return iqFactory.createUnaryIQTree(this, child); } IQTree newChild = child.liftIncompatibleDefinitions(variable); QueryNode newChildRoot = newChild.getRootNode(); /* * Lift the union above the construction node */ if ((newChildRoot instanceof UnionNode) && ((UnionNode) newChildRoot).hasAChildWithLiftableDefinition(variable, newChild.getChildren())) { ImmutableList<IQTree> grandChildren = newChild.getChildren(); ImmutableList<IQTree> newChildren = grandChildren.stream() .map(c -> (IQTree) iqFactory.createUnaryIQTree(this, c)) .collect(ImmutableCollectors.toList()); UnionNode newUnionNode = iqFactory.createUnionNode(getVariables()); return iqFactory.createNaryIQTree(newUnionNode, newChildren); } return iqFactory.createUnaryIQTree(this, newChild); }
queryBuilder.init(projectionAtom, rootNode); UnionNode unionNode = iqFactory.createUnionNode(projectionAtom.getVariables()); queryBuilder.addChild(rootNode, unionNode);
ImmutableSet<Variable> retainedVariables = proposal.getRetainedVariables(); if (focusNode instanceof UnionNode) { UnionNode replacingNode = iqFactory.createUnionNode(retainedVariables); treeComponent.replaceNode(focusNode, replacingNode); return new NodeCentricOptimizationResultsImpl<>(query, replacingNode);
@Override public IQTree transformConstruction(IQTree tree, ConstructionNode rootCn, IQTree child) { IQTree transformedChild = child.acceptTransformer(this); QueryNode transformedChildRoot = transformedChild.getRootNode(); // if the child is a union, lift it if (transformedChildRoot instanceof UnionNode) { return iqFactory.createNaryIQTree( iqFactory.createUnionNode(rootCn.getVariables()), transformedChild.getChildren().stream() .map(t -> iqFactory.createUnaryIQTree(rootCn, t)) .collect(ImmutableCollectors.toList()) ); } // if the child is a construction node, merge it if (transformedChildRoot instanceof ConstructionNode) { return rootCn.liftBinding( transformedChild, variableGenerator, iqFactory.createIQProperties() ); } return iqFactory.createUnaryIQTree(rootCn, transformedChild); }
ImmutableSet<Variable> retainedVariables = proposal.getRetainedVariables(); if (focusNode instanceof UnionNode) { UnionNode replacingNode = iqFactory.createUnionNode(retainedVariables); treeComponent.replaceNode(focusNode, replacingNode); return new NodeCentricOptimizationResultsImpl<>(query, replacingNode);
private UnionNode liftUnionNode(UnionLiftProposal proposal, IntermediateQuery query, QueryTreeComponent treeComponent) { QueryNode targetNode = proposal.getTargetNode(); UnionNode focusNode = proposal.getFocusNode(); UnionNode newTopUnionNode = iqFactory.createUnionNode(query.getVariables(targetNode)); IntermediateQuery querySnapshot = query.createSnapshot(); treeComponent.replaceSubTree(targetNode, newTopUnionNode); querySnapshot.getChildren(focusNode) .forEach(c -> appendUnionChildBranch(c, focusNode, targetNode, newTopUnionNode, query, querySnapshot, treeComponent)); return newTopUnionNode; }
private UnionNode liftUnionNode(UnionLiftProposal proposal, IntermediateQuery query, QueryTreeComponent treeComponent) { QueryNode targetNode = proposal.getTargetNode(); UnionNode focusNode = proposal.getFocusNode(); UnionNode newTopUnionNode = iqFactory.createUnionNode(query.getVariables(targetNode)); IntermediateQuery querySnapshot = query.createSnapshot(); treeComponent.replaceSubTree(targetNode, newTopUnionNode); querySnapshot.getChildren(focusNode) .forEach(c -> appendUnionChildBranch(c, focusNode, targetNode, newTopUnionNode, query, querySnapshot, treeComponent)); return newTopUnionNode; }
UnionNode updatedUnion = iqFactory.createUnionNode(bufferNode.getVariables());