Java Code Examples for org.eclipse.rdf4j.query.algebra.ValueConstant#getValue()

The following examples show how to use org.eclipse.rdf4j.query.algebra.ValueConstant#getValue() . 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: FilterRangeVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(final Filter node) throws Exception {
    super.meet(node);

    final ValueExpr arg = node.getCondition();
    if (arg instanceof FunctionCall) {
        final FunctionCall fc = (FunctionCall) arg;
        if (RANGE.stringValue().equals(fc.getURI())) {
            //range(?var, start, end)
            final List<ValueExpr> valueExprs = fc.getArgs();
            if (valueExprs.size() != 3) {
                throw new QueryEvaluationException("org.apache:range must have 3 parameters: variable, start, end");
            }
            final Var var = (Var) valueExprs.get(0);
            final ValueConstant startVc = (ValueConstant) valueExprs.get(1);
            final ValueConstant endVc = (ValueConstant) valueExprs.get(2);
            final Value start = startVc.getValue();
            final Value end = endVc.getValue();
            rangeValues.put(var, new RangeValue(start, end));
            node.setCondition(new ValueConstant(BooleanLiteral.TRUE));
        }
    }
}
 
Example 2
Source File: PeriodicQueryUtil.java    From rya with Apache License 2.0 6 votes vote down vote up
private static TimeUnit getTimeUnit(ValueConstant val) {
    Preconditions.checkArgument(val.getValue() instanceof IRI);
    IRI uri = (IRI) val.getValue();
    Preconditions.checkArgument(uri.getNamespace().equals(temporalNameSpace));

    switch (uri.getLocalName()) {
    case "days":
        return TimeUnit.DAYS;
    case "hours":
        return TimeUnit.HOURS;
    case "minutes":
        return TimeUnit.MINUTES;
    default:
        throw new IllegalArgumentException("Invalid time unit for Periodic Function.");
    }
}
 
Example 3
Source File: SailUpdateExecutor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param add
 * @param uc
 * @throws SailException
 */
protected void executeAdd(Add add, UpdateContext uc, int maxExecTime) throws SailException {
	ValueConstant sourceGraph = add.getSourceGraph();
	ValueConstant destinationGraph = add.getDestinationGraph();

	Resource source = sourceGraph != null ? (Resource) sourceGraph.getValue() : null;
	Resource destination = destinationGraph != null ? (Resource) destinationGraph.getValue() : null;

	if (source == null && destination == null || (source != null && source.equals(destination))) {
		// source and destination are the same, copy is a null-operation.
		return;
	}

	// get all statements from source and add them to destination
	CloseableIteration<? extends Statement, SailException> statements = null;
	try {
		statements = con.getStatements(null, null, null, uc.isIncludeInferred(), (Resource) source);

		if (maxExecTime > 0) {
			statements = new TimeLimitIteration<Statement, SailException>(statements, 1000L * maxExecTime) {

				@Override
				protected void throwInterruptedException() throws SailException {
					throw new SailException("execution took too long");
				}
			};
		}

		while (statements.hasNext()) {
			Statement st = statements.next();
			con.addStatement(uc, st.getSubject(), st.getPredicate(), st.getObject(), (Resource) destination);
		}
	} finally {
		if (statements != null) {
			statements.close();
		}
	}
}
 
Example 4
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Retrieve the associated Value (if any) for the given valueExpr.
 *
 * @param valueExpr
 * @return the value of the given ValueExpr, or null if no value exists.
 * @throws IllegalArgumentException if the supplied ValueExpr is null or of an unexpected type.
 */
protected Value getValueForExpr(ValueExpr valueExpr) {
	if (valueExpr instanceof Var) {
		return ((Var) valueExpr).getValue();
	} else if (valueExpr instanceof ValueConstant) {
		ValueConstant vc = (ValueConstant) valueExpr;
		return vc.getValue();
	} else if (valueExpr == null) {
		throw new IllegalArgumentException("valueExpr is null");
	} else {
		throw new IllegalArgumentException("valueExpr is a: " + valueExpr.getClass());
	}
}
 
Example 5
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Object visit(ASTFunctionCall node, Object data) throws VisitorException {
	ValueConstant uriNode = (ValueConstant) node.jjtGetChild(0).jjtAccept(this, null);
	IRI functionURI = (IRI) uriNode.getValue();

	FunctionCall functionCall = new FunctionCall(functionURI.toString());

	for (int i = 1; i < node.jjtGetNumChildren(); i++) {
		Node argNode = node.jjtGetChild(i);
		functionCall.addArg(castToValueExpr(argNode.jjtAccept(this, null)));
	}

	return functionCall;
}
 
Example 6
Source File: PeriodicQueryUtil.java    From rya with Apache License 2.0 5 votes vote down vote up
private static double parseTemporalDuration(ValueConstant valConst) {
    Value val = valConst.getValue();
    Preconditions.checkArgument(val instanceof Literal);
    Literal literal = (Literal) val;
    String stringVal = literal.getLabel();
    IRI dataType = literal.getDatatype();
    Preconditions.checkArgument(dataType.equals(XMLSchema.DECIMAL) || dataType.equals(XMLSchema.DOUBLE)
            || dataType.equals(XMLSchema.FLOAT) || dataType.equals(XMLSchema.INTEGER) || dataType.equals(XMLSchema.INT));
    return Double.parseDouble(stringVal);
}
 
Example 7
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Value evaluate(ValueConstant valueConstant, BindingSet bindings)
		throws QueryEvaluationException {
	return valueConstant.getValue();
}
 
Example 8
Source File: SailUpdateExecutor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @param copy
 * @param uc
 * @throws SailException
 */
protected void executeCopy(Copy copy, UpdateContext uc, int maxExecutionTime) throws SailException {
	ValueConstant sourceGraph = copy.getSourceGraph();
	ValueConstant destinationGraph = copy.getDestinationGraph();

	Resource source = sourceGraph != null ? (Resource) sourceGraph.getValue() : null;
	Resource destination = destinationGraph != null ? (Resource) destinationGraph.getValue() : null;

	if (source == null && destination == null || (source != null && source.equals(destination))) {
		// source and destination are the same, copy is a null-operation.
		return;
	}

	// clear destination
	final long start = System.currentTimeMillis();
	con.clear((Resource) destination);
	final long clearTime = (System.currentTimeMillis() - start) / 1000;

	if (maxExecutionTime > 0) {
		if (clearTime > maxExecutionTime) {
			throw new SailException("execution took too long");
		}
	}

	// get all statements from source and add them to destination
	CloseableIteration<? extends Statement, SailException> statements = null;
	try {
		statements = con.getStatements(null, null, null, uc.isIncludeInferred(), (Resource) source);

		if (maxExecutionTime > 0) {
			statements = new TimeLimitIteration<Statement, SailException>(statements,
					1000L * (maxExecutionTime - clearTime)) {

				@Override
				protected void throwInterruptedException() throws SailException {
					throw new SailException("execution took too long");
				}
			};
		}

		while (statements.hasNext()) {
			Statement st = statements.next();
			con.addStatement(uc, st.getSubject(), st.getPredicate(), st.getObject(), (Resource) destination);
		}
	} finally {
		if (statements != null) {
			statements.close();
		}
	}
}
 
Example 9
Source File: SailUpdateExecutor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @param move
 * @param uc
 * @throws SailException
 */
protected void executeMove(Move move, UpdateContext uc, int maxExecutionTime) throws SailException {
	ValueConstant sourceGraph = move.getSourceGraph();
	ValueConstant destinationGraph = move.getDestinationGraph();

	Resource source = sourceGraph != null ? (Resource) sourceGraph.getValue() : null;
	Resource destination = destinationGraph != null ? (Resource) destinationGraph.getValue() : null;

	if (source == null && destination == null || (source != null && source.equals(destination))) {
		// source and destination are the same, move is a null-operation.
		return;
	}

	// clear destination
	final long start = System.currentTimeMillis();
	con.clear((Resource) destination);
	final long clearTime = (System.currentTimeMillis() - start) / 1000;

	if (maxExecutionTime > 0 && clearTime > maxExecutionTime) {
		throw new SailException("execution took too long");
	}

	// remove all statements from source and add them to destination
	CloseableIteration<? extends Statement, SailException> statements = null;

	try {
		statements = con.getStatements(null, null, null, uc.isIncludeInferred(), (Resource) source);
		if (maxExecutionTime > 0) {
			statements = new TimeLimitIteration<Statement, SailException>(statements,
					1000L * (maxExecutionTime - clearTime)) {

				@Override
				protected void throwInterruptedException() throws SailException {
					throw new SailException("execution took too long");
				}
			};
		}

		while (statements.hasNext()) {
			Statement st = statements.next();
			con.addStatement(uc, st.getSubject(), st.getPredicate(), st.getObject(), (Resource) destination);
			con.removeStatement(uc, st.getSubject(), st.getPredicate(), st.getObject(), (Resource) source);
		}
	} finally {
		if (statements != null) {
			statements.close();
		}
	}
}
 
Example 10
Source File: SailUpdateExecutor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @param clearExpr
 * @param uc
 * @throws SailException
 */
protected void executeClear(Clear clearExpr, UpdateContext uc, int maxExecutionTime) throws SailException {
	try {
		ValueConstant graph = clearExpr.getGraph();

		if (graph != null) {
			Resource context = (Resource) graph.getValue();
			con.clear(context);
		} else {
			Scope scope = clearExpr.getScope();
			if (Scope.NAMED_CONTEXTS.equals(scope)) {
				CloseableIteration<? extends Resource, SailException> contextIDs = null;
				try {
					contextIDs = con.getContextIDs();
					if (maxExecutionTime > 0) {
						contextIDs = new TimeLimitIteration<Resource, SailException>(contextIDs,
								1000L * maxExecutionTime) {

							@Override
							protected void throwInterruptedException() throws SailException {
								throw new SailException("execution took too long");
							}
						};
					}
					while (contextIDs.hasNext()) {
						con.clear(contextIDs.next());
					}
				} finally {
					if (contextIDs != null) {
						contextIDs.close();
					}
				}
			} else if (Scope.DEFAULT_CONTEXTS.equals(scope)) {
				con.clear((Resource) null);
			} else {
				con.clear();
			}
		}
	} catch (SailException e) {
		if (!clearExpr.isSilent()) {
			throw e;
		}
	}
}
 
Example 11
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 2 votes vote down vote up
/**
 * Evaluate a {@link ValueConstant} query model node.
 * @param valueConstant
 * @param bindings the set of named value bindings
 * @return the {@link Value} of {@code valueConstant}
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(ValueConstant valueConstant, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    return valueConstant.getValue();
}