@Override public Optional<IQ> mergeDefinitions(Collection<IQ> predicateDefinitions, ImmutableQueryModifiers topModifiers) { return mergeDefinitions(predicateDefinitions, Optional.of(topModifiers)); }
@Override public Stream<IntermediateQuery> splitUnion(IntermediateQuery query) { return findFirstSplittableUnion(query) .map(unionNode -> split(query, unionNode) .flatMap(this::lift)) .orElseGet(() -> Stream.of(query)); }
@Override public ImmutableSet<ImmutableTerm> extract(Variable variable, IntermediateQuery query) { return extract(variable, query.getRootNode(), query); }
substitute( initialVariable, currentVariable, .flatMap(n -> getDefinitions( initialVariable, fakeClone,
@Override public ExecutorRegistry getExecutorRegistry() { if (executorRegistry == null) { executorRegistry = new StandardExecutorRegistry(getInjector(), generateOptimizationConfigurationMap()); } return executorRegistry; }
private void insertChildTree(QueryNode parent, QueryNode child, IQTree childTree, IntermediateQueryBuilder queryBuilder, int position) { if (parent instanceof BinaryOrderedOperatorNode) { queryBuilder.addChild(parent, child, (position == 0)? LEFT : RIGHT); } else { queryBuilder.addChild(parent, child); } insertChildren(child, childTree.getChildren(), queryBuilder); } }
@Override public IQ convert(IntermediateQuery query) { IQTree tree = convertTree(query, query.getRootNode()); return iqFactory.createIQ(query.getProjectionAtom(), tree); }
@Override public <R extends ProposalResults, P extends QueryOptimizationProposal<R>> ProposalExecutor<P, R> getExecutor( P proposal) { Optional<Class<? extends ProposalExecutor>> optionalExecutorClass = getProposalExecutorInterface(proposal.getClass()); if (optionalExecutorClass.isPresent()) { return injector.getInstance(optionalExecutorClass.get()); } else { throw new InvalidOntopConfigurationException("No executor found for a proposal of the type " + proposal.getClass()); } }
private Stream<IntermediateQuery> split(IntermediateQuery query, UnionNode unionNode) { return query.getChildren(unionNode).stream() .map(child -> split(query, unionNode, child)); }
/** * Recursive */ private void insertChildren(QueryNode parentNode, ImmutableList<IQTree> childrenTrees, IntermediateQueryBuilder queryBuilder) { AtomicInteger i = new AtomicInteger(0); childrenTrees.stream() .map(IQTree::getRootNode) .map(n -> queryBuilder.contains(n) ? n.clone() : n) .forEach(t -> insertChildTree(parentNode, t, childrenTrees.get(i.get()), queryBuilder, i.getAndIncrement())); }
private Optional<UnionNode> findFirstSplittableUnion(IntermediateQuery query) { Queue<QueryNode> nodesToVisit = new LinkedList<>(query.getChildren(query.getRootNode())); while(!nodesToVisit.isEmpty()) { QueryNode node = nodesToVisit.poll(); if (node instanceof UnionNode) { return Optional.of((UnionNode)node); } else { nodesToVisit.addAll(extractChildrenToVisit(query, node)); } } return Optional.empty(); }
/** * Given a query q, a variable x and a starting node n projecting x, * retrieves all substitutions (from ConstructionNodes) generating a value for x in the subtree of q * rooted in n. * Note that unions may cause multiple such substitutions to be returned. * <p> * The intended usage of this method it to determine the type of a variable from the returned substitution. * Therefore this method is robust to simple variable renaming, * but only until a more complex substitution is found. * E.g. if [x/x2], [x2/x3] and [x3/URI("http://myURI{}", x4] are three substitutions found in that order in a same * branch, * then x/URI("http://myURI{}", x4) will be the only output substitution for that branch. * <p> * This method does not support composition otherwise. * E.g. if [x/+(x1,x2)] and [x2/x3] are found in that order in a branch, * then [x/+(x1,x2)] is returned. */ @Override public ImmutableSet<ImmutableTerm> extract(Variable variable, QueryNode node, IntermediateQuery query) { if (query.contains(node)) { if (query.getVariables(node).contains(variable)) { return getDefinitions(variable, variable, node, query) .collect(ImmutableCollectors.toSet()); } throw new IllegalArgumentException("The node should project the variable"); } throw new IllegalArgumentException("The node should be in the query"); }
VariableOccurrenceAnalyzer analyzer = new NaiveVariableOccurrenceAnalyzerImpl();
@Override public Optional<IntermediateQuery> mergeDefinitions(Collection<IntermediateQuery> predicateDefinitions, ImmutableQueryModifiers topModifiers) { return mergeDefinitions(predicateDefinitions, Optional.of(topModifiers)); }
@Override public ExecutorRegistry getExecutorRegistry() { if (executorRegistry == null) { executorRegistry = new StandardExecutorRegistry(getInjector(), generateOptimizationConfigurationMap()); } return executorRegistry; }
@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(); }
/** * 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); }
@Override public <R extends ProposalResults, P extends QueryOptimizationProposal<R>> ProposalExecutor<P, R> getExecutor( P proposal) { Optional<Class<? extends ProposalExecutor>> optionalExecutorClass = getProposalExecutorInterface(proposal.getClass()); if (optionalExecutorClass.isPresent()) { return injector.getInstance(optionalExecutorClass.get()); } else { throw new InvalidOntopConfigurationException("No executor found for a proposal of the type " + proposal.getClass()); } }
@Override public Optional<IQ> mergeDefinitions(Collection<IQ> predicateDefinitions) { return mergeDefinitions(predicateDefinitions, Optional.empty()); }
@Override public Optional<IntermediateQuery> mergeDefinitions(Collection<IntermediateQuery> predicateDefinitions) { return mergeDefinitions(predicateDefinitions, Optional.empty()); }