@Override public boolean isSyntacticallyEquivalentTo(QueryNode node) { if (node instanceof UnionNode) { return projectedVariables.equals(((UnionNode)node).getVariables()); } return false; }
@Override public IQTree transformUnion(IQTree tree, UnionNode rootNode, ImmutableList<IQTree> children) { return transformNaryCommutativeNode(rootNode.acceptNodeTransformer(nodeTransformer), children); }
@Override public UnionNode transform(UnionNode unionNode) { return unionNode.clone(); }
@Override public IQTree liftIncompatibleDefinitions(Variable variable, ImmutableList<IQTree> children) { return IntStream.range(0, children.size()).boxed() .map(i -> Maps.immutableEntry(i, children.get(i))) .filter(e -> e.getValue().isConstructed(variable)) // index -> new child .map(e -> Maps.immutableEntry(e.getKey(), e.getValue().liftIncompatibleDefinitions(variable))) .filter(e -> { QueryNode newRootNode = e.getValue().getRootNode(); return (newRootNode instanceof UnionNode) && ((UnionNode) newRootNode).hasAChildWithLiftableDefinition(variable, e.getValue().getChildren()); }) .findFirst() .map(e -> liftUnionChild(e.getKey(), (NaryIQTree) e.getValue(), children)) .orElseGet(() -> iqFactory.createNaryIQTree(this, children)); }
@Override public IQTree liftIncompatibleDefinitions(Variable variable, IQTree child) { IQTree newChild = child.liftIncompatibleDefinitions(variable); QueryNode newChildRoot = newChild.getRootNode(); /* * Lift the union above the filter */ if ((newChildRoot instanceof UnionNode) && ((UnionNode) newChildRoot).hasAChildWithLiftableDefinition(variable, newChild.getChildren())) { UnionNode unionNode = (UnionNode) newChildRoot; ImmutableList<IQTree> grandChildren = newChild.getChildren(); ImmutableList<IQTree> newChildren = grandChildren.stream() .map(c -> (IQTree) iqFactory.createUnaryIQTree(this, c)) .collect(ImmutableCollectors.toList()); return iqFactory.createNaryIQTree(unionNode, newChildren); } return iqFactory.createUnaryIQTree(this, newChild); }
@Override public boolean isEquivalentTo(QueryNode queryNode) { if (!(queryNode instanceof UnionNode)) return false; return projectedVariables.equals(((UnionNode) queryNode).getVariables()); }
@Override public IQTree liftIncompatibleDefinitions(Variable variable, IQTree leftChild, IQTree rightChild) { if (leftChild.getVariables().contains(variable)) { IQTree liftedLeftChild = leftChild.liftIncompatibleDefinitions(variable); QueryNode leftChildRoot = liftedLeftChild.getRootNode(); if (leftChildRoot instanceof UnionNode && ((UnionNode) leftChildRoot).hasAChildWithLiftableDefinition(variable, leftChild.getChildren())) { UnionNode newUnionNode = iqFactory.createUnionNode( Stream.of(leftChild, rightChild) .flatMap(c -> c.getVariables().stream()) .collect(ImmutableCollectors.toSet())); return iqFactory.createNaryIQTree(newUnionNode, liftedLeftChild.getChildren().stream() .map(unionChild -> (IQTree) iqFactory.createBinaryNonCommutativeIQTree(this, unionChild, rightChild)) .collect(ImmutableCollectors.toList())); } } // By default, nothing lifted return iqFactory.createBinaryNonCommutativeIQTree(this, leftChild, rightChild); }
@Override public UnionNode transform(UnionNode unionNode) { return unionNode.clone(); }
@Override public boolean isSyntacticallyEquivalentTo(QueryNode node) { if (node instanceof UnionNode) { return projectedVariables.equals(((UnionNode)node).getVariables()); } return false; }
@Override public IQTree liftIncompatibleDefinitions(Variable variable, IQTree child) { if (!childVariables.contains(variable)) { return iqFactory.createUnaryIQTree(this, child); } IQTree newChild = child.liftIncompatibleDefinitions(variable); QueryNode newChildRoot = newChild.getRootNode(); /* * Lift the union above the construction node */ if ((newChildRoot instanceof UnionNode) && ((UnionNode) newChildRoot).hasAChildWithLiftableDefinition(variable, newChild.getChildren())) { ImmutableList<IQTree> grandChildren = newChild.getChildren(); ImmutableList<IQTree> newChildren = grandChildren.stream() .map(c -> (IQTree) iqFactory.createUnaryIQTree(this, c)) .collect(ImmutableCollectors.toList()); UnionNode newUnionNode = iqFactory.createUnionNode(getVariables()); return iqFactory.createNaryIQTree(newUnionNode, newChildren); } return iqFactory.createUnaryIQTree(this, newChild); }
@Override public UnionNode transform(UnionNode unionNode){ return iqFactory.createUnionNode(renameProjectedVars(unionNode.getVariables())); // return unionNode.clone(); }
@Override public UnionNode transform(UnionNode unionNode){ return iqFactory.createUnionNode(renameProjectedVars(unionNode.getVariables())); // return unionNode.clone(); }
@Override public void visit(UnionNode unionNode) { if (query.getChildren(unionNode).size() < 2) { throw new InvalidIntermediateQueryException("UNION node " + unionNode +" does not have at least 2 children node."); } ImmutableSet<Variable> unionProjectedVariables = unionNode.getVariables(); for (QueryNode child : query.getChildren(unionNode)) { ImmutableSet<Variable> childProjectedVariables = query.getVariables(child); if (!childProjectedVariables.containsAll(unionProjectedVariables)) { throw new InvalidIntermediateQueryException("This child " + child + " does not project all the variables " + "required by the UNION node (" + unionProjectedVariables + ")\n" + query); } } }
@Override public void visit(UnionNode unionNode) { if (query.getChildren(unionNode).size() < 2) { throw new InvalidIntermediateQueryException("UNION node " + unionNode +" does not have at least 2 children node."); } ImmutableSet<Variable> unionProjectedVariables = unionNode.getVariables(); for (QueryNode child : query.getChildren(unionNode)) { ImmutableSet<Variable> childProjectedVariables = query.getVariables(child); if (!childProjectedVariables.containsAll(unionProjectedVariables)) { throw new InvalidIntermediateQueryException("This child " + child + " does not project all the variables " + "required by the UNION node (" + unionProjectedVariables + ")\n" + query); } } }
freshHeadAtom = generateProjectionAtom(parentNode.get().getChildVariables()); } else { freshHeadAtom = generateProjectionAtom(((UnionNode) node).getVariables()); heads.add(new RuleHead(cn.getSubstitution(), freshHeadAtom, grandChild)); } else { ConstructionNode cn = iqFactory.createConstructionNode(((UnionNode) node).getVariables()); subQueryProjectionAtoms.put(cn, freshHeadAtom); heads.add(new RuleHead(cn.getSubstitution(), freshHeadAtom, Optional.ofNullable(child)));
Map<ImmutableCQ, IQTree> map = new HashMap<>(); ImmutableList<Variable> avs = ImmutableList.copyOf(rootNode.getVariables()); for (IQTree child : children) { ImmutableCQ cq;
DistinctVariableOnlyDataAtom freshHeadAtom = generateProjectionAtom(((UnionNode) node).getVariables()); heads.add(new RuleHead(cn.getSubstitution(), freshHeadAtom, grandChild)); } else { ConstructionNode cn = iqFactory.createConstructionNode(((UnionNode) node).getVariables()); subQueryProjectionAtoms.put(cn, freshHeadAtom); heads.add(new RuleHead(cn.getSubstitution(), freshHeadAtom, Optional.of(child)));