Java Code Examples for org.eclipse.rdf4j.query.algebra.Var#isAnonymous()

The following examples show how to use org.eclipse.rdf4j.query.algebra.Var#isAnonymous() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrepareOwnedTupleExpr.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void appendVar(StringBuilder builder, Var var) {
	if (var.hasValue() && var.isAnonymous()) {
		Value value = var.getValue();
		writeValue(builder, value);
	} else {
		String varName = var.getName();
		appendVar(builder, varName);
	}
	builder.append(" ");
}
 
Example 2
Source File: PrepareOwnedTupleExpr.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void appendFilter(StringBuilder builder, Var var) {
	if (var.hasValue() && !var.isAnonymous()) {
		builder.append("\nFILTER sameTerm(");
		appendVar(builder, var.getName());
		builder.append(", ");
		writeValue(builder, var.getValue());
		builder.append(")");
	}
}
 
Example 3
Source File: SparqlValueExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(Var theVar) throws Exception {
	if (theVar.isAnonymous() && !theVar.hasValue()) {
		mBuffer.append("?").append(BaseTupleExprRenderer.scrubVarName(theVar.getName()));
	} else if (theVar.hasValue()) {
		mBuffer.append(RenderUtils.getSPARQLQueryString(theVar.getValue()));
	} else {
		mBuffer.append("?").append(theVar.getName());
	}
}
 
Example 4
Source File: SerqlValueExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(Var theVar) throws Exception {
	if (theVar.isAnonymous() && !theVar.hasValue()) {
		mBuffer.append(BaseTupleExprRenderer.scrubVarName(theVar.getName().substring(1)));
	} else if (theVar.hasValue()) {
		mBuffer.append(RenderUtils.getSerqlQueryString(theVar.getValue()));
	} else {
		mBuffer.append(theVar.getName());
	}
}
 
Example 5
Source File: UpdateExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Modify visit(ASTDeleteWhere node, Object data) throws VisitorException {
	// Collect delete clause triples
	GraphPattern parentGP = graphPattern;
	graphPattern = new GraphPattern();

	// inherit scope & context
	graphPattern.setStatementPatternScope(parentGP.getStatementPatternScope());
	graphPattern.setContextVar(parentGP.getContextVar());

	for (int i = 0; i < node.jjtGetNumChildren(); i++) {
		node.jjtGetChild(i).jjtAccept(this, data);
	}

	where = graphPattern.buildTupleExpr();
	graphPattern = parentGP;

	TupleExpr deleteExpr = where.clone();

	// FIXME we should adapt the grammar so we can avoid doing this
	// post-processing.
	VarCollector collector = new VarCollector();
	deleteExpr.visit(collector);
	for (Var var : collector.getCollectedVars()) {
		if (var.isAnonymous() && !var.hasValue()) {
			throw new VisitorException("DELETE WHERE may not contain blank nodes");
		}
	}

	Modify modify = new Modify(deleteExpr, null, where);

	return modify;
}
 
Example 6
Source File: UpdateExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public TupleExpr visit(ASTDeleteClause node, Object data) throws VisitorException {
	TupleExpr result = (TupleExpr) data;

	// Collect construct triples
	GraphPattern parentGP = graphPattern;

	graphPattern = new GraphPattern();

	// inherit scope & context
	graphPattern.setStatementPatternScope(parentGP.getStatementPatternScope());
	graphPattern.setContextVar(parentGP.getContextVar());

	for (int i = 0; i < node.jjtGetNumChildren(); i++) {
		node.jjtGetChild(i).jjtAccept(this, data);
	}

	TupleExpr deleteExpr = graphPattern.buildTupleExpr();
	Map<String, Object> tripleVars = TripleRefCollector.process(where);

	// FIXME we should adapt the grammar so we can avoid doing this in
	// post-processing.
	VarCollector collector = new VarCollector();
	deleteExpr.visit(collector);
	for (Var var : collector.getCollectedVars()) {
		// skip vars that are provided by ValueExprTripleRef - added as Extentsion
		if (tripleVars.containsKey(var.getName())) {
			continue;
		}
		if (var.isAnonymous() && !var.hasValue()) {
			// blank node in delete pattern, not allowed by SPARQL spec.
			throw new VisitorException("DELETE clause may not contain blank nodes");
		}
	}

	graphPattern = parentGP;

	return deleteExpr;

}
 
Example 7
Source File: SPARQLValueExprRenderer.java    From semagrow with Apache License 2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(Var theVar)
        throws Exception
{
    if (theVar.isAnonymous() && !theVar.hasValue()) {
        mBuffer.append("?").append(BaseTupleExprRenderer.scrubVarName(theVar.getName()));
    }
    else if (theVar.hasValue()) {
        mBuffer.append(SPARQLQueryStringUtil.toSPARQL(theVar.getValue()));
    }
    else {
        mBuffer.append("?").append(theVar.getName());
    }
}
 
Example 8
Source File: PrepareOwnedTupleExpr.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(StatementPattern node) throws RepositoryException {
	StringBuilder builder = new StringBuilder();
	Scope scope = node.getScope();
	Var subj = node.getSubjectVar();
	Var pred = node.getPredicateVar();
	Var obj = node.getObjectVar();
	Var ctx = node.getContextVar();
	boolean cokay = ctx == null && scope.equals(Scope.DEFAULT_CONTEXTS)
			|| ctx != null && scope.equals(Scope.NAMED_CONTEXTS);
	boolean sokay = !subj.hasValue() || subj.isAnonymous() || subj.getValue() instanceof IRI;
	boolean ookay = !obj.hasValue() || obj.isAnonymous() || obj.getValue() instanceof IRI
			|| obj.getValue() instanceof Literal;
	if (cokay && sokay && ookay) {
		variables.clear();
		if (ctx != null) {
			builder.append("GRAPH ");
			appendVar(builder, ctx.getName());
			builder.append(" {\n");
		}
		appendVar(builder, subj);
		appendVar(builder, pred);
		appendVar(builder, obj);
		builder.append(" .");
		appendFilter(builder, subj);
		appendFilter(builder, pred);
		appendFilter(builder, obj);
		if (ctx != null) {
			if (ctx.hasValue()) {
				builder.append("\nFILTER sameTerm(");
				appendVar(builder, ctx.getName());
				builder.append(", ");
				writeValue(builder, ctx.getValue());
				builder.append(")\n");
			}
			builder.append("}");
		}
		this.pattern = builder.toString();
		this.patternNode = node;
	} else {
		this.patternNode = null; // NOPMD
	}
}
 
Example 9
Source File: ConstantOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Var var) {
	if (!var.isAnonymous()) {
		varNames.add(var.getName());
	}
}
 
Example 10
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Object visit(ASTBind node, Object data) throws VisitorException {
	// bind expression
	Object child0 = node.jjtGetChild(0).jjtAccept(this, data);
	ValueExpr ve = child0 instanceof ValueExpr ? (ValueExpr) child0
			: (child0 instanceof TripleRef) ? ((TripleRef) child0).getExprVar() : null;
	if (ve == null) {
		throw new IllegalArgumentException("Unexpected expressin on bind");
	}

	// name to bind the expression outcome to
	Node aliasNode = node.jjtGetChild(1);
	String alias = ((ASTVar) aliasNode).getName();

	Extension extension = new Extension();
	extension.addElement(new ExtensionElem(ve, alias));

	TupleExpr result = null;
	TupleExpr arg = graphPattern.buildTupleExpr();

	// check if alias is not previously used.
	if (arg.getBindingNames().contains(alias)) {
		// SES-2314 we need to doublecheck that the reused varname is not
		// just
		// for an anonymous var or a constant.
		VarCollector collector = new VarCollector();
		arg.visit(collector);
		for (Var v : collector.getCollectedVars()) {
			if (alias.equals(v.getName())) {
				if (!v.isConstant() && !v.isAnonymous()) {
					throw new VisitorException(String.format("BIND clause alias '%s' was previously used", alias));
				}
				break;
			}
		}
	}

	if (arg instanceof Filter) {
		result = arg;
		// we need to push down the extension so that filters can operate on
		// the BIND expression.
		while (((Filter) arg).getArg() instanceof Filter) {
			arg = ((Filter) arg).getArg();
		}

		extension.setArg(((Filter) arg).getArg());
		((Filter) arg).setArg(extension);
	} else {
		extension.setArg(arg);
		result = extension;
	}

	GraphPattern replacementGP = new GraphPattern(graphPattern);
	replacementGP.addRequiredTE(result);

	graphPattern = replacementGP;

	return result;
}