@Override public NodeTransformationProposal reactToEmptyChild(IntermediateQuery query, EmptyNode emptyChild) { /* * All the children expected the given empty child */ ImmutableList<QueryNode> children = query.getChildrenStream(this) .filter(c -> c != emptyChild) .collect(ImmutableCollectors.toList()); switch (children.size()) { case 0: return new NodeTransformationProposalImpl(DECLARE_AS_EMPTY, emptyChild.getVariables()); case 1: return new NodeTransformationProposalImpl(REPLACE_BY_UNIQUE_NON_EMPTY_CHILD, children.get(0), ImmutableSet.of()); default: return new NodeTransformationProposalImpl(NO_LOCAL_CHANGE, ImmutableSet.of()); } }
/** * Extract the bindings from the union node, searching recursively for bindings in its children. * Ignore conflicting definitions of variables and return only the common bindings between the subtree */ private PartialExtraction extractBindingsFromUnionNode(IntermediateQuery query, UnionNode currentNode) { ImmutableList<ImmutableMap<Variable, ImmutableTerm>> childrenBindings = query.getChildrenStream(currentNode) .map(c -> extractBindings(query, c).getMergedBindings() .collect(ImmutableCollectors.toMap())) .collect(ImmutableCollectors.toList()); ImmutableSet<Map.Entry<Variable, ImmutableTerm>> mergedBindings = childrenBindings.stream() .reduce((c1, c2) -> combineUnionChildBindings(query, c1, c2)) .orElseThrow(() -> new IllegalStateException("A union must have children")) .entrySet(); ImmutableSet<Variable> mergedVariables = mergedBindings.stream() .map(e -> e.getKey()) .collect(ImmutableCollectors.toSet()); ImmutableSet<Variable> conflictingVariables = childrenBindings.stream() .flatMap(c -> c.keySet().stream()) .filter(v -> !mergedVariables.contains(v)) .collect(ImmutableCollectors.toSet()); return new PartialExtraction(mergedBindings.stream(), conflictingVariables); }
@Override public NodeTransformationProposal reactToTrueChildRemovalProposal(IntermediateQuery query, TrueNode trueChild) { ImmutableList<QueryNode> remainingChildren = query.getChildrenStream(this) .filter(c -> c != trueChild) .collect(ImmutableCollectors.toList()); switch (remainingChildren.size()) { case 0: return new NodeTransformationProposalImpl(DECLARE_AS_TRUE, ImmutableSet.of()); case 1: Optional<ImmutableExpression> condition = getOptionalFilterCondition(); if (condition.isPresent()) { return new NodeTransformationProposalImpl( REPLACE_BY_NEW_NODE, query.getFactory().createFilterNode(condition.get()), ImmutableSet.of() ); } return new NodeTransformationProposalImpl(REPLACE_BY_UNIQUE_NON_EMPTY_CHILD, remainingChildren.get(0),ImmutableSet.of()); default: return new NodeTransformationProposalImpl(NO_LOCAL_CHANGE, ImmutableSet.of()); } }