@Override public IQTree liftBinding(ImmutableList<IQTree> children, VariableGenerator variableGenerator, IQProperties currentIQProperties) { ImmutableList<IQTree> liftedChildren = children.stream() .map(c -> c.liftBinding(variableGenerator)) .filter(c -> !c.isDeclaredAsEmpty()) .map(c -> projectAwayUnnecessaryVariables(c, currentIQProperties)) .collect(ImmutableCollectors.toList()); switch (liftedChildren.size()) { case 0: return iqFactory.createEmptyNode(projectedVariables); case 1: return liftedChildren.get(0); default: return liftBindingFromLiftedChildren(liftedChildren, variableGenerator, currentIQProperties); } }
@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 convertResults2IQTree(ImmutableSet<Variable> projectedVariables, ChildLiftingState liftingState, IQProperties currentIQProperties) { AscendingSubstitutionNormalization ascendingNormalization = normalizeAscendingSubstitution( liftingState.getComposedAscendingSubstitution(), projectedVariables); Optional<ConstructionNode> topConstructionNode = ascendingNormalization.generateTopConstructionNode(); IQTree subTree = ascendingNormalization.normalizeChild( Optional.of(liftingState.rightChild) .filter(rightChild -> !rightChild.isDeclaredAsEmpty()) .filter(rightChild -> !(rightChild.getRootNode() instanceof TrueNode)) // LJ .map(rightChild -> (IQTree) iqFactory.createBinaryNonCommutativeIQTree( iqFactory.createLeftJoinNode(liftingState.ljCondition), liftingState.leftChild, liftingState.rightChild, currentIQProperties.declareLifted())) // Left child .orElse(liftingState.leftChild)); return topConstructionNode .map(n -> (IQTree) iqFactory.createUnaryIQTree(n, subTree, currentIQProperties.declareLifted())) .orElse(subTree); }
private ChildLiftingState liftRightChild(ChildLiftingState state, VariableGenerator variableGenerator) { IQTree liftedRightChild = state.rightChild.liftBinding(variableGenerator); if (!(liftedRightChild.getRootNode() instanceof ConstructionNode)) { if (state.rightChild.equals(liftedRightChild)) return state; return new ChildLiftingState(state.leftChild, liftedRightChild, state.ljCondition.filter(c -> !liftedRightChild.isDeclaredAsEmpty()), state.ascendingSubstitution); } ConstructionNode rightConstructionNode = (ConstructionNode) liftedRightChild.getRootNode(); IQTree rightGrandChild = ((UnaryIQTree) liftedRightChild).getChild(); ImmutableSubstitution<ImmutableTerm> rightSubstitution = rightConstructionNode.getSubstitution(); return liftRightChild(state, rightGrandChild, rightSubstitution, variableGenerator); }
private IQTree liftBindingAfterPropagatingCondition(IQTree childIQTree, VariableGenerator variableGenerator, IQProperties currentIQProperties) { IQTree liftedChildIQTree = childIQTree.liftBinding(variableGenerator); QueryNode childRoot = liftedChildIQTree.getRootNode(); if (childRoot instanceof ConstructionNode) return liftBinding((ConstructionNode) childRoot, (UnaryIQTree) liftedChildIQTree, currentIQProperties, variableGenerator); else if (liftedChildIQTree.isDeclaredAsEmpty()) { return liftedChildIQTree; } else return iqFactory.createUnaryIQTree(this, liftedChildIQTree, currentIQProperties.declareLifted()); }
if (unfoldedIQ.getTree().isDeclaredAsEmpty()) throw new EmptyQueryException(); log.debug("Unfolded query: \n" + unfoldedIQ.toString());
@Override public IQTree liftBinding(IQTree childIQTree, VariableGenerator variableGenerator, IQProperties currentIQProperties) { IQTree liftedChildIQTree = childIQTree.liftBinding(variableGenerator); QueryNode liftedChildRoot = liftedChildIQTree.getRootNode(); if (liftedChildRoot instanceof ConstructionNode) return liftBinding((ConstructionNode) liftedChildRoot, (UnaryIQTree) liftedChildIQTree, currentIQProperties); else if (liftedChildIQTree.isDeclaredAsEmpty()) { return iqFactory.createEmptyNode(projectedVariables); } else return iqFactory.createUnaryIQTree(this, liftedChildIQTree, currentIQProperties.declareLifted()); }
return updatedRightChild.isDeclaredAsEmpty() ? updatedLeftChild : iqFactory.createBinaryNonCommutativeIQTree( if (updatedRightChild.isDeclaredAsEmpty()) { ImmutableSet<Variable> leftVariables = updatedLeftChild.getVariables(); ImmutableSet<Variable> projectedVariables = Sets.union(leftVariables,
@Override public IQTree liftBinding(IQTree initialLeftChild, IQTree initialRightChild, VariableGenerator variableGenerator, IQProperties currentIQProperties) { ImmutableSet<Variable> projectedVariables = Stream.of(initialLeftChild, initialRightChild) .flatMap(c -> c.getVariables().stream()) .collect(ImmutableCollectors.toSet()); IQTree liftedLeftChild = initialLeftChild.liftBinding(variableGenerator); if (liftedLeftChild.isDeclaredAsEmpty()) return iqFactory.createEmptyNode(projectedVariables); // Non-final ChildLiftingState liftingState = liftLeftChild(liftedLeftChild, initialRightChild, getOptionalFilterCondition(), variableGenerator); boolean hasConverged = false; int i = 0; while ((!hasConverged) && (i++ < MAX_ITERATIONS)) { ChildLiftingState newLiftingState = liftRightChild( optimizeLeftJoinCondition(liftingState, variableGenerator), variableGenerator); hasConverged = liftingState.equals(newLiftingState); liftingState = newLiftingState; } if (i >= MAX_ITERATIONS) throw new MinorOntopInternalBugException("LJ.liftBinding() did not converge after " + i); return convertResults2IQTree(projectedVariables, liftingState, currentIQProperties); }
private IQ canonizeWithJoin(IQ assertion, IntensionalQueryMerger intensionalQueryMerger, Position pos) { Optional<Variable> replacedVar = getReplacedVar(assertion, pos); if (replacedVar.isPresent()) { Variable newVariable = createFreshVariable(assertion, intensionalQueryMerger, replacedVar.get()); IntensionalDataNode idn = getIDN(replacedVar.get(), newVariable); RDFAtomPredicate pred = getRDFAtomPredicate(assertion.getProjectionAtom()); DistinctVariableOnlyDataAtom projAtom = atomFactory.getDistinctVariableOnlyDataAtom( pred, replaceProjVars( pred, assertion.getProjectionAtom().getArguments(), pos, newVariable)); IQ intensionalCanonizedQuery = iqFactory.createIQ( projAtom, getIntensionalCanonizedTree(assertion, projAtom, idn)); IQ canonizedQuery = intensionalQueryMerger.optimize(intensionalCanonizedQuery) .liftBinding(); return canonizedQuery.getTree().isDeclaredAsEmpty() // No matching canonical IRI template ? assertion : canonizedQuery; } return assertion; }
private ChildLiftingState liftLeftChild(IQTree liftedLeftChild, IQTree rightChild, Optional<ImmutableExpression> ljCondition, VariableGenerator variableGenerator) { if (liftedLeftChild.getRootNode() instanceof ConstructionNode) { ConstructionNode leftConstructionNode = (ConstructionNode) liftedLeftChild.getRootNode(); IQTree leftGrandChild = ((UnaryIQTree) liftedLeftChild).getChild(); try { return liftRegularChildBinding(leftConstructionNode, 0, leftGrandChild, ImmutableList.of(liftedLeftChild,rightChild), leftGrandChild.getVariables(), ljCondition, variableGenerator, this::convertIntoChildLiftingResults); } /* * Replaces the LJ by the left child */ catch (UnsatisfiableConditionException e) { EmptyNode newRightChild = iqFactory.createEmptyNode(rightChild.getVariables()); return new ChildLiftingState(leftGrandChild, newRightChild, Optional.empty(), leftConstructionNode.getSubstitution()); } } else if (liftedLeftChild.isDeclaredAsEmpty()) return new ChildLiftingState(liftedLeftChild, iqFactory.createEmptyNode(rightChild.getVariables()), Optional.empty(), substitutionFactory.getSubstitution()); else return new ChildLiftingState(liftedLeftChild, rightChild, ljCondition, substitutionFactory.getSubstitution()); }
@Override public IntermediateQuery convert(IQ query, DBMetadata dbMetadata, ExecutorRegistry executorRegistry) throws EmptyQueryException { if (query.getTree().isDeclaredAsEmpty()) throw new EmptyQueryException(); IntermediateQueryBuilder queryBuilder = iqFactory.createIQBuilder(dbMetadata, executorRegistry); IQTree topTree = query.getTree(); QueryNode rootNode = topTree.getRootNode(); queryBuilder.init(query.getProjectionAtom(), rootNode); insertChildren(rootNode, topTree.getChildren(), queryBuilder); return queryBuilder.build(); }