private ImmutableSubstitution<NonVariableTerm> combine(ImmutableSubstitution<NonVariableTerm> l, ImmutableSubstitution<NonVariableTerm> r) { return l.union(r) .orElseThrow(() -> new MinorOntopInternalBugException( "Unexpected conflict between " + l + " and " + r)); }
private ImmutableExpression computeExpression(ImmutableList<? extends VariableOrGroundTerm> formerRightArguments, ImmutableList<? extends VariableOrGroundTerm> newRightArguments) { Stream<ImmutableExpression> expressions = IntStream.range(0, formerRightArguments.size()) .filter(i -> !formerRightArguments.get(i).equals(newRightArguments.get(i))) .boxed() .map(i -> termFactory.getImmutableExpression(EQ, newRightArguments.get(i), formerRightArguments.get(i))); return ImmutabilityTools.foldBooleanExpressions(expressions) .orElseThrow(() -> new MinorOntopInternalBugException("A boolean expression was expected")); }
private IQ mergeDefinitions(Collection<IQ> assertions) { return queryMerger.mergeDefinitions(assertions) .orElseThrow(() -> new MinorOntopInternalBugException("Could not merge assertions: " + assertions)); }
private ImmutableExpression computeExpression(ImmutableList<? extends VariableOrGroundTerm> formerRightArguments, ImmutableList<? extends VariableOrGroundTerm> newRightArguments) { Stream<ImmutableExpression> expressions = IntStream.range(0, formerRightArguments.size()) .filter(i -> !formerRightArguments.get(i).equals(newRightArguments.get(i))) .boxed() .map(i -> termFactory.getImmutableExpression(EQ, newRightArguments.get(i), formerRightArguments.get(i))); return immutabilityTools.foldBooleanExpressions(expressions) .orElseThrow(() -> new MinorOntopInternalBugException("A boolean expression was expected")); }
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); }
/** * Lifts the substitution in the absence of a LJ condition */ private LeftJoinNode liftSubstitution(LeftJoinNode normalizedLeftJoin, ImmutableSubstitution<ImmutableTerm> remainingRightSubstitution, IntermediateQuery query) { SubstitutionPropagationProposal<LeftJoinNode> proposal = new SubstitutionPropagationProposalImpl<>( normalizedLeftJoin, remainingRightSubstitution); try { return query.applyProposal(proposal, true) .getOptionalNewNode() .orElseThrow(() -> new MinorOntopInternalBugException("Was not expected to modify the LJ node")); } catch (EmptyQueryException e) { throw new MinorOntopInternalBugException("This substitution propagation was not expected " + "to make the query be empty"); } }
@Override public IQTree insertAbove(IQTree childTree, IntermediateQueryFactory iqFactory) { ImmutableList<UnaryOperatorNode> modifierNodes = extractModifierNodes(iqFactory); return modifierNodes.reverse().stream() .reduce(childTree, (t, n) -> iqFactory.createUnaryIQTree(n, t), (t1, t2) -> {throw new MinorOntopInternalBugException("Merging should never been executed"); }); }
/** * Lifts the substitution in the absence of a LJ condition */ private LeftJoinNode liftSubstitution(LeftJoinNode normalizedLeftJoin, ImmutableSubstitution<ImmutableTerm> remainingRightSubstitution, IntermediateQuery query) { SubstitutionPropagationProposal<LeftJoinNode> proposal = new SubstitutionPropagationProposalImpl<>( normalizedLeftJoin, remainingRightSubstitution); try { NodeCentricOptimizationResults<LeftJoinNode> results = query.applyProposal(proposal, true); return results .getNewNodeOrReplacingChild() // The LJ is expected to be the child of a construction node .flatMap(query::getFirstChild) .filter(n -> n instanceof LeftJoinNode) .map(n -> (LeftJoinNode) n) .orElseThrow(() -> new MinorOntopInternalBugException("Was expected to insert a construction node " + "followed by a LJ")); } catch (EmptyQueryException e) { throw new MinorOntopInternalBugException("This substitution propagation was not expected " + "to make the query be empty"); } }
private OrderByNode.OrderComparator convertComparator(OrderByNode.OrderComparator comparator, ImmutableSubstitution<ImmutableTerm> substitution) { NonGroundTerm term = comparator.getTerm(); if (term instanceof Variable) return comparator; Optional<Variable> correspondingVariable = substitution.getImmutableMap().entrySet().stream() .filter(e -> e.getValue().equals(term)) .map(Map.Entry::getKey) .findFirst(); return correspondingVariable .map(v -> iqFactory.createOrderComparator(v, comparator.isAscending())) .orElseThrow(() -> new MinorOntopInternalBugException("The order condition " + term + " is not projected by the construction node with " + substitution + " and therefore" + "cannot be translated into the internal Datalog-like structure")); } }
private RDFPredicate extractRDFPredicate(CQIE mappingAssertion) { Function headAtom = mappingAssertion.getHead(); if (!(headAtom.getFunctionSymbol() instanceof RDFAtomPredicate)) throw new MinorOntopInternalBugException("Mapping assertion without an RDFAtomPredicate found"); RDFAtomPredicate predicate = (RDFAtomPredicate) headAtom.getFunctionSymbol(); ImmutableList<ImmutableTerm> arguments = headAtom.getTerms().stream() .map(immutabilityTools::convertIntoImmutableTerm) .collect(ImmutableCollectors.toList()); return predicate.getClassIRI(arguments) .map(iri -> new RDFPredicate(true, iri)) .orElseGet(() -> predicate.getPropertyIRI(arguments) .map(i -> new RDFPredicate(false, i)) .orElseThrow(() -> new MinorOntopInternalBugException("Could not extract a predicate IRI from " + headAtom))); }
/** * */ private IQ liftUnionsInTree(IQ query) { VariableGenerator variableGenerator = query.getVariableGenerator(); // Non-final IQTree previousTree; IQTree newTree = query.getTree(); int i=0; do { previousTree = newTree; newTree = liftTree(previousTree, variableGenerator) .liftBinding(variableGenerator); } while (!newTree.equals(previousTree) && (++i < ITERATION_BOUND)); if (i >= ITERATION_BOUND) throw new MinorOntopInternalBugException(getClass().getName() + " did not converge after " + ITERATION_BOUND + " iterations"); return newTree.equals(query.getTree()) ? query : iqFactory.createIQ(query.getProjectionAtom(), newTree); }
.filter(n -> n instanceof LeftJoinNode) .map(n -> (LeftJoinNode) n) .orElseThrow(() -> new MinorOntopInternalBugException("Was expected to keep the LJ node " + "under a fresh construction node")); } catch (EmptyQueryException e) { throw new MinorOntopInternalBugException("This substitution propagation was not expected " + "to make the query be empty");
@Override public VariableNullability getVariableNullability(IQTree child) { VariableNullability childNullability = child.getVariableNullability(); VariableGenerator variableGenerator = coreUtilsFactory.createVariableGenerator( Sets.union(projectedVariables, child.getVariables()).immutableCopy()); /* * The substitutions are split by nesting level */ VariableNullability nullabilityBeforeProjectingOut = splitSubstitution(substitution, variableGenerator) .reduce(childNullability, (n, s) -> updateVariableNullability(s, n), (n1, n2) -> { throw new MinorOntopInternalBugException("vns are not expected to be combined"); }); /* * Projects away irrelevant variables */ ImmutableSet<ImmutableSet<Variable>> nullableGroups = nullabilityBeforeProjectingOut.getNullableGroups().stream() .map(g -> g.stream() .filter(projectedVariables::contains) .collect(ImmutableCollectors.toSet())) .filter(g -> !g.isEmpty()) .collect(ImmutableCollectors.toSet()); return new VariableNullabilityImpl(nullableGroups); }
/** * Recursive */ @Override public IQTree convertTree(IntermediateQuery query, QueryNode rootNode) { if (rootNode instanceof LeafIQTree) { return (LeafIQTree) rootNode; } else if (rootNode instanceof UnaryOperatorNode) { // Recursive IQTree childTree = convertTree(query, query.getFirstChild(rootNode).get()); return iqFactory.createUnaryIQTree((UnaryOperatorNode) rootNode, childTree); } else if (rootNode instanceof BinaryNonCommutativeOperatorNode) { IQTree leftChildTree = convertTree(query, query.getChild(rootNode, LEFT).get()); IQTree rightChildTree = convertTree(query, query.getChild(rootNode, RIGHT).get()); return iqFactory.createBinaryNonCommutativeIQTree((BinaryNonCommutativeOperatorNode) rootNode, leftChildTree, rightChildTree); } else if (rootNode instanceof NaryOperatorNode) { ImmutableList<IQTree> childTrees = query.getChildren(rootNode).stream() .map(c -> convertTree(query, c)) .collect(ImmutableCollectors.toList()); return iqFactory.createNaryIQTree((NaryOperatorNode) rootNode, childTrees); } throw new MinorOntopInternalBugException("Unexpected type of query node: " + rootNode); }
RDF4JConstructTemplate(ParsedQuery pq) { TupleExpr topExpression = pq.getTupleExpr(); // NB: the slice is not relevant for the construct template // (will be taken into account in the SELECT query fragment) Slice slice = (topExpression instanceof Slice) ? (Slice) topExpression : null; TupleExpr firstNonSliceExpression = (slice == null) ? topExpression : slice.getArg(); if (!(firstNonSliceExpression instanceof Reduced)) // TODO: throw a better exception? throw new MinorOntopInternalBugException("Was expecting a Reduced instead of: " + firstNonSliceExpression); projection = ((Reduced) firstNonSliceExpression).getArg(); TupleExpr texpr = (projection instanceof MultiProjection) ? ((MultiProjection) projection).getArg() : ((Projection) projection).getArg(); extension = (texpr!= null && texpr instanceof Extension) ? texpr : null; }
return query.applyProposal(proposal, true) .getOptionalNewNode() .orElseThrow(() -> new MinorOntopInternalBugException("Was not expected to modify the LJ node")); } catch (EmptyQueryException e) { throw new MinorOntopInternalBugException("This substitution propagation was not expected " + "to make the query be empty");
throw new MinorOntopInternalBugException("A functional term was expected for the object: " + objectTerm); throw new MinorOntopInternalBugException("No IRI could extracted from " + targetAtom);
.orElse(newSubTreeRootNode); default: throw new MinorOntopInternalBugException("Unexpected local action for propagating renamings down to a " + "subtree " + rootRenamingResults.getLocalAction());
throw new MinorOntopInternalBugException("Bug: at this point the lj must not have a joining condition");
throw new MinorOntopInternalBugException("Bug: at this point the lj must not have a joining condition");