public void setGraphRestriction(BinaryUnion<Variable, IRI> graphRestriction) { // if (this.graphRestriction == null) { if (graphRestriction != null && graphRestriction.isFirstType() && gatherVariables().contains(graphRestriction.getFirst()) && !getProjectedVariables().contains(graphRestriction.getFirst())) { // graph restriction variable already used in the local graph pattern // and is not visible outside // we need to // 1) use a new variable v (not used in this pattern or its ancestors) for the graph // restriction // 2) project v // 3) set the graph restriction to v // 4) add in this subselect pattern a filter ?v = ?graphRestriction Set<Variable> vars = HashSetFactory.make(); Pattern top = getTopAncestor(); if (top != null) { vars.addAll(Planner.gatherInScopeVariables(top)); if (top.getOptionalPatterns() != null) { for (Pattern op : top.getOptionalPatterns()) { vars.addAll(Planner.gatherInScopeVariables(op)); } } } vars.addAll(gatherVariables()); String prefix = "graphRestrictionGeneratedVar_"; int id = OCUtils.nextAvailableSuffixVariable(OCUtils.getVariables(vars), prefix); Variable v = new Variable(prefix + id); this.graphRestriction = new BinaryUnion<Variable, IRI>(); this.graphRestriction.setFirst(v); if (selectClause.getProjectedVariables() == null || selectClause.getProjectedVariables().isEmpty()) { for (ProjectedVariable pv : gatherRealProjectedVariables()) { selectClause.addProjectedVariable(pv); } } selectClause.addProjectedVariable(new ProjectedVariable(v.getName())); // getParent().addFilter(new RelationalExpression(new // VariableExpression(graphRestriction.getFirst().getName()), // new VariableExpression(v.getName()), ERelationalOp.EQUAL)); } else { this.graphRestriction = graphRestriction; } } }
public Set<Variable> gatherProjectedVariables() { Set<Variable> allVars = new LinkedHashSet<Variable>(); List<ProjectedVariable> pvList = selectClause.getProjectedVariables(); if (pvList != null && pvList.size() > 0) { for (ProjectedVariable pv : pvList) { allVars.add(pv.getVariable()); } return allVars; } else { allVars.addAll(Planner.gatherInScopeVariables(graphPattern)); if (graphPattern.getOptionalPatterns() != null) { for (Pattern op : graphPattern.getOptionalPatterns()) { allVars.addAll(Planner.gatherInScopeVariables(op)); } } return allVars; } }