@Override public IQTree applyDescendingSubstitution(ImmutableSubstitution<? extends VariableOrGroundTerm> descendingSubstitution, Optional<ImmutableExpression> constraint, IQTree child) { return iqFactory.createUnaryIQTree(this, child.applyDescendingSubstitution(descendingSubstitution, constraint)); }
public IQTree normalizeChild(IQTree child) { return downRenamingSubstitution.isEmpty() ? child : child.applyDescendingSubstitution(downRenamingSubstitution, Optional.empty()); }
@Override public IQTree applyDescendingSubstitution(ImmutableSubstitution<? extends VariableOrGroundTerm> descendingSubstitution, Optional<ImmutableExpression> constraint, IQTree child) { return iqFactory.createUnaryIQTree(this, child.applyDescendingSubstitution(descendingSubstitution, constraint)); }
/** * TODO: should we try to preserve the children order? */ private LiftingStepResults convertIntoLiftingStepResults( ImmutableList<IQTree> liftedChildren, IQTree selectedGrandChild, int selectedChildPosition, Optional<ImmutableExpression> newCondition, ImmutableSubstitution<ImmutableTerm> ascendingSubstitution, ImmutableSubstitution<? extends VariableOrGroundTerm> descendingSubstitution) { ImmutableList<IQTree> newChildren = IntStream.range(0, liftedChildren.size()) .boxed() .map(i -> i == selectedChildPosition ? selectedGrandChild.applyDescendingSubstitution(descendingSubstitution, newCondition) : liftedChildren.get(i).applyDescendingSubstitution(descendingSubstitution, newCondition)) .collect(ImmutableCollectors.toList()); return new LiftingStepResults(ascendingSubstitution, newChildren, newCondition, false); }
private ChildLiftingState convertIntoChildLiftingResults( ImmutableList<IQTree> children, IQTree leftGrandChild, int leftChildPosition, Optional<ImmutableExpression> ljCondition, ImmutableSubstitution<ImmutableTerm> ascendingSubstitution, ImmutableSubstitution<? extends VariableOrGroundTerm> descendingSubstitution) { if (children.size() != 2) throw new MinorOntopInternalBugException("Two children were expected, not " + children); IQTree newRightChild = children.get(1) .applyDescendingSubstitution(descendingSubstitution, ljCondition); return new ChildLiftingState(leftGrandChild, newRightChild, ljCondition, ascendingSubstitution); }
/** * * TODO: better handle the constraint * * Returns the new child */ private IQTree propagateDescendingSubstitutionToChild(IQTree child, PropagationResults<VariableOrGroundTerm> tauFPropagationResults, Optional<ImmutableExpression> constraint) throws EmptyTreeException { Optional<ImmutableExpression> descendingConstraint = computeChildConstraint(tauFPropagationResults.theta, constraint); return Optional.of(tauFPropagationResults.delta) .filter(delta -> !delta.isEmpty()) .map(delta -> child.applyDescendingSubstitution(delta, descendingConstraint)) .orElse(child); }
@Override public IQTree applyDescendingSubstitution(ImmutableSubstitution<? extends VariableOrGroundTerm> descendingSubstitution, Optional<ImmutableExpression> constraint, IQTree child) { OrderByNode newOrderByTree = applySubstitution(descendingSubstitution); IQTree newChild = child.applyDescendingSubstitution(descendingSubstitution, constraint); return iqFactory.createUnaryIQTree(newOrderByTree, newChild); }
@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); } }
private IQTree propagateDownCondition(Optional<ImmutableExpression> initialConstraint, IQTree leftChild, IQTree rightChild) { IQTree newLeftChild = initialConstraint .map(leftChild::propagateDownConstraint) .orElse(leftChild); ImmutableSet<Variable> leftVariables = leftChild.getVariables(); try { ExpressionAndSubstitution conditionSimplificationResults = simplifyCondition(getOptionalFilterCondition(), leftVariables); Optional<ImmutableExpression> rightConstraint = computeDownConstraint(initialConstraint, conditionSimplificationResults); IQTree newRightChild = Optional.of(conditionSimplificationResults.substitution) .filter(s -> !s.isEmpty()) .map(s -> rightChild.applyDescendingSubstitution(s, rightConstraint)) .orElseGet(() -> rightConstraint .map(rightChild::propagateDownConstraint) .orElse(rightChild)); LeftJoinNode newLeftJoin = conditionSimplificationResults.optionalExpression.equals(getOptionalFilterCondition()) ? this : conditionSimplificationResults.optionalExpression .map(iqFactory::createLeftJoinNode) .orElseGet(iqFactory::createLeftJoinNode); return iqFactory.createBinaryNonCommutativeIQTree(newLeftJoin, newLeftChild, newRightChild); } catch (UnsatisfiableConditionException e) { return newLeftChild; } }
private IQTree propagateDownCondition(IQTree child, Optional<ImmutableExpression> initialConstraint) { try { ExpressionAndSubstitution conditionSimplificationResults = simplifyCondition(Optional.of(getFilterCondition()), ImmutableSet.of()); Optional<ImmutableExpression> downConstraint = computeDownConstraint(initialConstraint, conditionSimplificationResults); IQTree newChild = Optional.of(conditionSimplificationResults.substitution) .filter(s -> !s.isEmpty()) .map(s -> child.applyDescendingSubstitution(s, downConstraint)) .orElseGet(() -> downConstraint .map(child::propagateDownConstraint) .orElse(child)); IQTree filterLevelTree = conditionSimplificationResults.optionalExpression .map(e -> e.equals(getFilterCondition()) ? this : iqFactory.createFilterNode(e)) .map(filterNode -> (IQTree) iqFactory.createUnaryIQTree(filterNode, newChild)) .orElse(newChild); return Optional.of(conditionSimplificationResults.substitution) .filter(s -> !s.isEmpty()) .map(s -> (ImmutableSubstitution<ImmutableTerm>)(ImmutableSubstitution<?>)s) .map(s -> iqFactory.createConstructionNode(child.getVariables(), s)) .map(c -> (IQTree) iqFactory.createUnaryIQTree(c, filterLevelTree)) .orElse(filterLevelTree); } catch (UnsatisfiableConditionException e) { return iqFactory.createEmptyNode(child.getVariables()); } }
.filter(s -> !s.isEmpty()) .map(s -> children.stream() .map(child -> child.applyDescendingSubstitution(s, downConstraint)) .collect(ImmutableCollectors.toList())
private IQTree propagate(IQTree subTree, ImmutableSubstitution<? extends ImmutableTerm> substitution) throws InvalidQueryOptimizationProposalException { IQTree newSubTree = subTree.applyDescendingSubstitution(substitution.getVariableOrGroundTermFragment(), Optional.empty()); if (substitution.getNonGroundFunctionalTermFragment().getDomain().stream() .anyMatch(v -> newSubTree.getVariables().contains(v))) throw new InvalidQueryOptimizationProposalException("Non ground functional terms are not supported for propagation down " + "(only for up)"); ConstructionNode constructionNode = iqFactory.createConstructionNode( Sets.union(newSubTree.getVariables(), substitution.getDomain()).immutableCopy(), (ImmutableSubstitution<ImmutableTerm>) substitution); return iqFactory.createUnaryIQTree(constructionNode, newSubTree); } }
.applyDescendingSubstitution(descendingSubstitution, constraint); IQTree updatedLeftChild = leftChild.applyDescendingSubstitution(descendingSubstitution, constraint); .composeWith2(descendingSubstitution); IQTree updatedRightChild = rightChild.applyDescendingSubstitution(rightDescendingSubstitution, newConstraint); IQTree updatedRightChild = rightChild.applyDescendingSubstitution(descendingSubstitution, constraint); if (updatedRightChild.isDeclaredAsEmpty()) { ImmutableSet<Variable> leftVariables = updatedLeftChild.getVariables();
.composeWith2(expressionAndSubstitution.substitution); IQTree newChild = child.applyDescendingSubstitution(downSubstitution, downConstraint); IQTree filterLevelTree = expressionAndSubstitution.optionalExpression .map(iqFactory::createFilterNode)
state.ascendingSubstitution); IQTree updatedRightChild = rightChild.applyDescendingSubstitution(downSubstitution, simplificationResults.optionalExpression);
.map(c -> c.applyDescendingSubstitution(downSubstitution, downConstraint)) .collect(ImmutableCollectors.toList());
IQTree newGrandChild = grandChildIQTree.applyDescendingSubstitution( expressionAndSubstitution.substitution, expressionAndSubstitution.optionalExpression);
/** * TODO: find a better name */ private IQTree updateChild(UnaryIQTree liftedChildTree, ImmutableSubstitution<ImmutableTerm> mergedSubstitution, ImmutableSet<Variable> projectedVariables) { ConstructionNode constructionNode = (ConstructionNode) liftedChildTree.getRootNode(); ConstructionNodeTools.NewSubstitutionPair substitutionPair = constructionTools.traverseConstructionNode( mergedSubstitution, constructionNode.getSubstitution(), constructionNode.getVariables(), projectedVariables); // NB: this is expected to be ok given that the expected compatibility of the merged substitution with // this construction node ImmutableSubstitution<? extends VariableOrGroundTerm> descendingSubstitution = substitutionFactory.getSubstitution( (ImmutableMap<Variable, ? extends VariableOrGroundTerm>)(ImmutableMap<Variable, ?>) substitutionPair.propagatedSubstitution.getImmutableMap()); IQTree newChild = liftedChildTree.getChild() .applyDescendingSubstitution(descendingSubstitution, Optional.empty()); ConstructionNode newConstructionNode = iqFactory.createConstructionNode(projectedVariables, substitutionPair.bindings); return substitutionPair.bindings.isEmpty() ? newChild : iqFactory.createUnaryIQTree(newConstructionNode, newChild); }
/** * Does NOT look for intensional data nodes inside the definitions * */ private IQTree replaceIntensionalData(IntensionalDataNode dataNode, IQ definition) { InjectiveVar2VarSubstitution renamingSubstitution = substitutionFactory.generateNotConflictingRenaming( variableGenerator, definition.getTree().getKnownVariables()); IQ renamedIQ; if(renamingSubstitution.isEmpty()){ renamedIQ = definition; } else { QueryRenamer queryRenamer = transformerFactory.createRenamer(renamingSubstitution); renamedIQ = queryRenamer.transform(definition); } ImmutableSubstitution<VariableOrGroundTerm> descendingSubstitution = extractSubstitution( renamingSubstitution.applyToDistinctVariableOnlyDataAtom(renamedIQ.getProjectionAtom()), dataNode.getProjectionAtom()); return renamedIQ.getTree() .applyDescendingSubstitution(descendingSubstitution, Optional.empty()) .liftBinding(variableGenerator); }