org.eclipse.rdf4j.model.impl.BooleanLiteral Java Examples

The following examples show how to use org.eclipse.rdf4j.model.impl.BooleanLiteral. 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: StrEnds.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 2) {
		throw new ValueExprEvaluationException("STRENDS requires 2 arguments, got " + args.length);
	}

	Value leftVal = args[0];
	Value rightVal = args[1];

	if (leftVal instanceof Literal && rightVal instanceof Literal) {
		Literal leftLit = (Literal) leftVal;
		Literal rightLit = (Literal) rightVal;

		if (QueryEvaluationUtil.compatibleArguments(leftLit, rightLit)) {

			String leftLexVal = leftLit.getLabel();
			String rightLexVal = rightLit.getLabel();

			return BooleanLiteral.valueOf(leftLexVal.endsWith(rightLexVal));
		} else {
			throw new ValueExprEvaluationException("incompatible operands for STRENDS function");
		}
	} else {
		throw new ValueExprEvaluationException("STRENDS function expects literal operands");
	}
}
 
Example #3
Source File: ShaclSailConfig.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Resource export(Model m) {
	Resource implNode = super.export(m);

	m.setNamespace("sail-shacl", NAMESPACE);
	m.add(implNode, PARALLEL_VALIDATION, BooleanLiteral.valueOf(isParallelValidation()));
	m.add(implNode, UNDEFINED_TARGET_VALIDATES_ALL_SUBJECTS,
			BooleanLiteral.valueOf(isUndefinedTargetValidatesAllSubjects()));
	m.add(implNode, LOG_VALIDATION_PLANS, BooleanLiteral.valueOf(isLogValidationPlans()));
	m.add(implNode, LOG_VALIDATION_VIOLATIONS, BooleanLiteral.valueOf(isLogValidationViolations()));
	m.add(implNode, IGNORE_NO_SHAPES_LOADED_EXCEPTION, BooleanLiteral.valueOf(isIgnoreNoShapesLoadedException()));
	m.add(implNode, VALIDATION_ENABLED, BooleanLiteral.valueOf(isValidationEnabled()));
	m.add(implNode, CACHE_SELECT_NODES, BooleanLiteral.valueOf(isCacheSelectNodes()));
	m.add(implNode, GLOBAL_LOG_VALIDATION_EXECUTION, BooleanLiteral.valueOf(isGlobalLogValidationExecution()));
	m.add(implNode, RDFS_SUB_CLASS_REASONING, BooleanLiteral.valueOf(isRdfsSubClassReasoning()));
	m.add(implNode, PERFORMANCE_LOGGING, BooleanLiteral.valueOf(isPerformanceLogging()));
	m.add(implNode, SERIALIZABLE_VALIDATION, BooleanLiteral.valueOf(isSerializableValidation()));
	return implNode;
}
 
Example #4
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Clear node) throws RDFHandlerException {
	if (node.isSilent()) {
		handler.handleStatement(valueFactory.createStatement(subject, SP.SILENT_PROPERTY, BooleanLiteral.TRUE));
	}

	if (node.getGraph() != null) {
		handler.handleStatement(
				valueFactory.createStatement(subject, SP.GRAPH_IRI_PROPERTY, node.getGraph().getValue()));
	} else if (node.getScope() != null) {
		switch (node.getScope()) {
		case DEFAULT_CONTEXTS:
			handler.handleStatement(
					valueFactory.createStatement(subject, SP.DEFAULT_PROPERTY, BooleanLiteral.TRUE));
			break;
		case NAMED_CONTEXTS:
			handler.handleStatement(
					valueFactory.createStatement(subject, SP.NAMED_PROPERTY, BooleanLiteral.TRUE));
			break;
		}
	} else {
		handler.handleStatement(valueFactory.createStatement(subject, SP.ALL_PROPERTY, BooleanLiteral.TRUE));
	}
}
 
Example #5
Source File: SfEqualsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void matchesMultiPolygonWKT() throws IOException {

	String polygon = IOUtils.toString(
			getClass().getResourceAsStream(
					"/org/eclipse/rdf4j/query/algebra/evaluation/function/geosparql/sfequals_polygon.txt"),
			Charset.defaultCharset());
	String multiPolygon = IOUtils.toString(
			getClass().getResourceAsStream(
					"/org/eclipse/rdf4j/query/algebra/evaluation/function/geosparql/sfequals_multipolygon.txt"),
			Charset.defaultCharset());

	Literal polygonLit = f.createLiteral(polygon, GEO.WKT_LITERAL);
	Literal multiPolygonLit = f.createLiteral(multiPolygon, GEO.WKT_LITERAL);

	Literal result = (Literal) testedFunction().evaluate(f, multiPolygonLit, polygonLit);

	assertNotNull(result);
	assertThat(result).isEqualTo(BooleanLiteral.TRUE);
}
 
Example #6
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Value evaluate(CompareAny node, BindingSet bindings)
		throws QueryEvaluationException {
	Value leftValue = evaluate(node.getArg(), bindings);

	// Result is false until a match has been found
	boolean result = false;

	// Use first binding name from tuple expr to compare values
	String bindingName = node.getSubQuery().getBindingNames().iterator().next();

	try (CloseableIteration<BindingSet, QueryEvaluationException> iter = evaluate(node.getSubQuery(), bindings)) {
		while (!result && iter.hasNext()) {
			BindingSet bindingSet = iter.next();

			Value rightValue = bindingSet.getValue(bindingName);

			try {
				result = QueryEvaluationUtil.compare(leftValue, rightValue, node.getOperator());
			} catch (ValueExprEvaluationException e) {
				// ignore, maybe next value will match
			}
		}
	}

	return BooleanLiteral.valueOf(result);
}
 
Example #7
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Value evaluate(LangMatches node, BindingSet bindings)
		throws QueryEvaluationException {
	Value langTagValue = evaluate(node.getLeftArg(), bindings);
	Value langRangeValue = evaluate(node.getRightArg(), bindings);

	if (QueryEvaluationUtil.isSimpleLiteral(langTagValue) && QueryEvaluationUtil.isSimpleLiteral(langRangeValue)) {
		String langTag = ((Literal) langTagValue).getLabel();
		String langRange = ((Literal) langRangeValue).getLabel();

		boolean result = false;
		if (langRange.equals("*")) {
			result = langTag.length() > 0;
		} else if (langTag.length() == langRange.length()) {
			result = langTag.equalsIgnoreCase(langRange);
		} else if (langTag.length() > langRange.length()) {
			// check if the range is a prefix of the tag
			String prefix = langTag.substring(0, langRange.length());
			result = prefix.equalsIgnoreCase(langRange) && langTag.charAt(langRange.length()) == '-';
		}

		return BooleanLiteral.valueOf(result);
	}

	throw new ValueExprEvaluationException();

}
 
Example #8
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Determines whether the language tag or the node matches the language argument of the node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(LangMatches node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value langTagValue = evaluate(node.getLeftArg(), bindings);
    Value langRangeValue = evaluate(node.getRightArg(), bindings);
    if (QueryEvaluationUtil.isSimpleLiteral(langTagValue)
            && QueryEvaluationUtil.isSimpleLiteral(langRangeValue)) {
        String langTag = ((Literal) langTagValue).getLabel();
        String langRange = ((Literal) langRangeValue).getLabel();
        boolean result = false;
        if (langRange.equals("*")) {
            result = langTag.length() > 0;
        } else if (langTag.length() == langRange.length()) {
            result = langTag.equalsIgnoreCase(langRange);
        } else if (langTag.length() > langRange.length()) {
            // check if the range is a prefix of the tag
            String prefix = langTag.substring(0, langRange.length());
            result = prefix.equalsIgnoreCase(langRange) && langTag.charAt(langRange.length()) == '-';
        }
        return BooleanLiteral.valueOf(result);
    }
    throw new ValueExprEvaluationException();
}
 
Example #9
Source File: HasAllObjects.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	QueryPreparer qp = getCurrentQueryPreparer();
	if (args.length != 3) {
		throw new ValueExprEvaluationException(
				String.format("%s requires 3 argument, got %d", getURI(), args.length));
	}
	Resource subj = (Resource) args[0];
	IRI pred = (IRI) args[1];
	Resource list = (Resource) args[2];
	try {
		Iteration<Value, QueryEvaluationException> iter = TripleSources.list(list, qp.getTripleSource());
		while (iter.hasNext()) {
			Value obj = iter.next();
			if (TripleSources.single(subj, pred, obj, qp.getTripleSource()) == null) {
				return BooleanLiteral.FALSE;
			}
		}
	} catch (QueryEvaluationException e) {
		throw new ValueExprEvaluationException(e);
	}
	return BooleanLiteral.TRUE;
}
 
Example #10
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Value evaluate(In node, BindingSet bindings) throws QueryEvaluationException {
	Value leftValue = evaluate(node.getArg(), bindings);

	// Result is false until a match has been found
	boolean result = false;

	// Use first binding name from tuple expr to compare values
	String bindingName = node.getSubQuery().getBindingNames().iterator().next();

	try (CloseableIteration<BindingSet, QueryEvaluationException> iter = evaluate(node.getSubQuery(), bindings)) {
		while (!result && iter.hasNext()) {
			BindingSet bindingSet = iter.next();

			Value rightValue = bindingSet.getValue(bindingName);

			result = leftValue == null && rightValue == null || leftValue != null && leftValue.equals(rightValue);
		}
	}

	return BooleanLiteral.valueOf(result);
}
 
Example #11
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate an {@link In} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(In node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value leftValue = evaluate(node.getArg(), bindings);
    // Result is false until a match has been found
    boolean result = false;
    // Use first binding name from tuple expr to compare values
    String bindingName = node.getSubQuery().getBindingNames().iterator().next();
    try (CloseableIteration<BindingSet, QueryEvaluationException> iter = parentStrategy.evaluate(node.getSubQuery(), bindings)) {
        while (result == false && iter.hasNext()) {
            BindingSet bindingSet = iter.next();
            Value rightValue = bindingSet.getValue(bindingName);
            result = leftValue == null && rightValue == null || leftValue != null
                    && leftValue.equals(rightValue);
        }
    }
    return BooleanLiteral.valueOf(result);
}
 
Example #12
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate a {@link CompareAny} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(CompareAny node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value leftValue = evaluate(node.getArg(), bindings);
    // Result is false until a match has been found
    boolean result = false;
    // Use first binding name from tuple expr to compare values
    String bindingName = node.getSubQuery().getBindingNames().iterator().next();
    try (CloseableIteration<BindingSet, QueryEvaluationException> iter = parentStrategy.evaluate(node.getSubQuery(), bindings)) {
        while (result == false && iter.hasNext()) {
            BindingSet bindingSet = iter.next();
            Value rightValue = bindingSet.getValue(bindingName);
            try {
                result = QueryEvaluationUtil.compare(leftValue, rightValue, node.getOperator());
            } catch (ValueExprEvaluationException e) {
                // ignore, maybe next value will match
            }
        }
    }
    return BooleanLiteral.valueOf(result);
}
 
Example #13
Source File: FederationEvalStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Value evaluate(ConjunctiveFilterExpr node, BindingSet bindings)
		throws ValueExprEvaluationException, QueryEvaluationException {

	ValueExprEvaluationException error = null;

	for (FilterExpr expr : node.getExpressions()) {

		try {
			Value v = evaluate(expr.getExpression(), bindings);
			if (QueryEvaluationUtil.getEffectiveBooleanValue(v) == false) {
				return BooleanLiteral.FALSE;
			}
		} catch (ValueExprEvaluationException e) {
			error = e;
		}
	}

	if (error != null) {
		throw error;
	}

	return BooleanLiteral.TRUE;
}
 
Example #14
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate a {@link CompareAll} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(CompareAll node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value leftValue = evaluate(node.getArg(), bindings);
    // Result is true until a mismatch has been found
    boolean result = true;
    // Use first binding name from tuple expr to compare values
    String bindingName = node.getSubQuery().getBindingNames().iterator().next();
    try (CloseableIteration<BindingSet, QueryEvaluationException> iter = parentStrategy.evaluate(node.getSubQuery(), bindings)) {
        while (result == true && iter.hasNext()) {
            BindingSet bindingSet = iter.next();
            Value rightValue = bindingSet.getValue(bindingName);
            try {
                result = QueryEvaluationUtil.compare(leftValue, rightValue, node.getOperator());
            } catch (ValueExprEvaluationException e) {
                // Exception thrown by ValueCompare.isTrue(...)
                result = false;
            }
        }
    }
    return BooleanLiteral.valueOf(result);
}
 
Example #15
Source File: FederationEvalStrategy.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
public Value evaluate(ConjunctiveFilterExpr node, BindingSet bindings) {
	
	ValueExprEvaluationException error = null;
	
	for (FilterExpr expr : node.getExpressions()) {
		
		try {
			Value v = evaluate(expr.getExpression(), bindings);
			if (QueryEvaluationUtil.getEffectiveBooleanValue(v) == false) {
				return BooleanLiteral.FALSE;
			}
		} catch (ValueExprEvaluationException e) {
			error = e;
		}
	}
	
	if (error!=null)
		throw error;
	
	return BooleanLiteral.TRUE;
}
 
Example #16
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluate a {@link Bound} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return {@link BooleanLiteral#TRUE} if the node can be evaluated or {@link BooleanLiteral#FALSE} if an exception occurs
 * @throws QueryEvaluationException
 */
private Value evaluate(Bound node, BindingSet bindings) throws QueryEvaluationException {
    try {
        evaluate(node.getArg(), bindings);
        return BooleanLiteral.TRUE;
    } catch (ValueExprEvaluationException e) {
        return BooleanLiteral.FALSE;
    }
}
 
Example #17
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Value evaluate(Compare node, BindingSet bindings)
		throws QueryEvaluationException {
	Value leftVal = evaluate(node.getLeftArg(), bindings);
	Value rightVal = evaluate(node.getRightArg(), bindings);

	return BooleanLiteral.valueOf(QueryEvaluationUtil.compare(leftVal, rightVal, node.getOperator()));
}
 
Example #18
Source File: ConstantOptimizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testAndOptimization() throws RDF4JException {
	String query = "prefix ex: <ex:>" + "select ?a ?b ?c\n" + "where {\n" + " bind((?a && ?b) as ?c) \n" + "}";

	ParsedQuery pq = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, query, null);

	TupleExpr original = pq.getTupleExpr();

	final AlgebraFinder finder = new AlgebraFinder();
	original.visit(finder);
	assertTrue(finder.logicalAndfound);

	// reset for re-use on optimized query
	finder.reset();

	QueryBindingSet constants = new QueryBindingSet();
	constants.addBinding("a", BooleanLiteral.TRUE);
	constants.addBinding("b", BooleanLiteral.FALSE);

	EvaluationStrategy strategy = new StrictEvaluationStrategy(new EmptyTripleSource(), null);
	TupleExpr optimized = optimize(pq.getTupleExpr().clone(), constants, strategy);

	optimized.visit(finder);
	assertFalse("optimized query should no longer contain && operator", finder.logicalAndfound);

	CloseableIteration<BindingSet, QueryEvaluationException> result = strategy.evaluate(optimized,
			new EmptyBindingSet());
	assertNotNull(result);
	assertTrue(result.hasNext());
	BindingSet bindings = result.next();
	assertTrue(bindings.hasBinding("a"));
	assertTrue(bindings.hasBinding("b"));
	assertTrue(bindings.hasBinding("c"));
}
 
Example #19
Source File: ConcatTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void nonStringLiteralHandling() {
	try {
		concatFunc.evaluate(vf, RDF.TYPE, BooleanLiteral.TRUE);
		fail("CONCAT expected to fail on non-stringliteral argument");
	} catch (ValueExprEvaluationException e) {
		// ignore, expected
	}
}
 
Example #20
Source File: MemoryStoreConfig.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Resource export(Model graph) {
	Resource implNode = super.export(graph);

	graph.setNamespace("ms", NAMESPACE);
	if (persist) {
		graph.add(implNode, PERSIST, BooleanLiteral.TRUE);
	}

	if (syncDelay != 0) {
		graph.add(implNode, SYNC_DELAY, SimpleValueFactory.getInstance().createLiteral(syncDelay));
	}

	return implNode;
}
 
Example #21
Source File: ExtendedEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Value evaluate(Compare node, BindingSet bindings)
		throws ValueExprEvaluationException, QueryEvaluationException {
	Value leftVal = evaluate(node.getLeftArg(), bindings);
	Value rightVal = evaluate(node.getRightArg(), bindings);

	// return result of non-strict comparisson.
	return BooleanLiteral.valueOf(QueryEvaluationUtil.compare(leftVal, rightVal, node.getOperator(), false));
}
 
Example #22
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Create node) throws RDFHandlerException {
	if (node.isSilent()) {
		handler.handleStatement(valueFactory.createStatement(subject, SP.SILENT_PROPERTY, BooleanLiteral.TRUE));
	}

	if (node.getGraph() != null) {
		handler.handleStatement(
				valueFactory.createStatement(subject, SP.GRAPH_IRI_PROPERTY, node.getGraph().getValue()));
	}
}
 
Example #23
Source File: StrStarts.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 2) {
		throw new ValueExprEvaluationException("STRSTARTS requires 2 arguments, got " + args.length);
	}

	Value leftVal = args[0];
	Value rightVal = args[1];

	if (leftVal instanceof Literal && rightVal instanceof Literal) {
		Literal leftLit = (Literal) leftVal;
		Literal rightLit = (Literal) rightVal;

		if (QueryEvaluationUtil.compatibleArguments(leftLit, rightLit)) {

			String leftLexVal = leftLit.getLabel();
			String rightLexVal = rightLit.getLabel();

			return BooleanLiteral.valueOf(leftLexVal.startsWith(rightLexVal));
		} else {
			throw new ValueExprEvaluationException("incompatible operands for STRSTARTS function");
		}
	} else {
		throw new ValueExprEvaluationException("STRSTARTS function expects literal operands");
	}

}
 
Example #24
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Value evaluate(Bound node, BindingSet bindings) throws QueryEvaluationException {
	try {
		Value argValue = evaluate(node.getArg(), bindings);
		return BooleanLiteral.valueOf(argValue != null);
	} catch (ValueExprEvaluationException e) {
		return BooleanLiteral.FALSE;
	}
}
 
Example #25
Source File: ConstantOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Bound bound) {
	super.meet(bound);

	if (bound.getArg().hasValue()) {
		// variable is always bound
		bound.replaceWith(new ValueConstant(BooleanLiteral.TRUE));
	}
}
 
Example #26
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Value evaluate(CompareAll node, BindingSet bindings)
		throws QueryEvaluationException {
	Value leftValue = evaluate(node.getArg(), bindings);

	// Result is true until a mismatch has been found
	boolean result = true;

	// Use first binding name from tuple expr to compare values
	String bindingName = node.getSubQuery().getBindingNames().iterator().next();

	try (CloseableIteration<BindingSet, QueryEvaluationException> iter = evaluate(node.getSubQuery(), bindings)) {
		while (result && iter.hasNext()) {
			BindingSet bindingSet = iter.next();

			Value rightValue = bindingSet.getValue(bindingName);

			try {
				result = QueryEvaluationUtil.compare(leftValue, rightValue, node.getOperator());
			} catch (ValueExprEvaluationException e) {
				// Exception thrown by ValueCompare.isTrue(...)
				result = false;
			}
		}
	}

	return BooleanLiteral.valueOf(result);
}
 
Example #27
Source File: WikiDataReification.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public boolean exists(RepositoryConnection aConnection, KnowledgeBase akb,
        KBStatement aStatement)
{
    // According to the Wikidata reification scheme, the predicate of the secondary triple
    // corresponds to the predicate of the primary triple with the prefix replaced, e.g.
    // p:P186 -> ps:P186
    String propStatementIri = aStatement.getProperty().getIdentifier().replace(PREFIX_PROP,
            PREFIX_PROP_STATEMENT);
    
    Iri subject = iri(aStatement.getInstance().getIdentifier());
    Iri pred1 =  iri(aStatement.getProperty().getIdentifier());
    Variable stmt = var("stmt");
    Iri pred2 =  iri(propStatementIri);
    RdfObject value = object(
            valueMapper.mapStatementValue(aStatement, SimpleValueFactory.getInstance()));
    
    String query = String.join("\n",
            "SELECT * { BIND ( EXISTS {",
            subject.has(pred1, stmt).getQueryString(),
            stmt.has(pred2, value).getQueryString(),
            "} AS ?result ) }");
    
    TupleQuery tupleQuery = aConnection.prepareTupleQuery(SPARQL, query);

    try (TupleQueryResult result = tupleQuery.evaluate()) {
        return ((BooleanLiteral) result.next().getBinding("result").getValue()).booleanValue();
    }
}
 
Example #28
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Determines whether the operand (a variable) contains a numeric datatyped literal, i.e. a literal with datatype
 * xsd:float, xsd:double, xsd:decimal, or a derived datatype of xsd:decimal.
 *
 * @return <tt>true</tt> if the operand contains a numeric datatyped literal, <tt>false</tt> otherwise.
 */
public Value evaluate(IsNumeric node, BindingSet bindings)
		throws QueryEvaluationException {
	Value argValue = evaluate(node.getArg(), bindings);

	if (argValue instanceof Literal) {
		Literal lit = (Literal) argValue;
		IRI datatype = lit.getDatatype();

		return BooleanLiteral.valueOf(XMLDatatypeUtil.isNumericDatatype(datatype));
	} else {
		return BooleanLiteral.FALSE;
	}

}
 
Example #29
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Value evaluate(ListMemberOperator node, BindingSet bindings)
		throws QueryEvaluationException {
	List<ValueExpr> args = node.getArguments();
	Value leftValue = evaluate(args.get(0), bindings);

	boolean result = false;
	ValueExprEvaluationException typeError = null;
	for (int i = 1; i < args.size(); i++) {
		ValueExpr arg = args.get(i);
		try {
			Value rightValue = evaluate(arg, bindings);
			result = leftValue == null && rightValue == null;
			if (!result) {
				result = QueryEvaluationUtil.compare(leftValue, rightValue, CompareOp.EQ);
			}
			if (result) {
				break;
			}
		} catch (ValueExprEvaluationException caught) {
			typeError = caught;
		}
	}

	if (typeError != null && !result) {
		// cf. SPARQL spec a type error is thrown if the value is not in the
		// list and one of the list members caused a type error in the
		// comparison.
		throw typeError;
	}

	return BooleanLiteral.valueOf(result);
}
 
Example #30
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Value evaluate(SameTerm node, BindingSet bindings)
		throws QueryEvaluationException {
	Value leftVal = evaluate(node.getLeftArg(), bindings);
	Value rightVal = evaluate(node.getRightArg(), bindings);

	return BooleanLiteral.valueOf(leftVal != null && leftVal.equals(rightVal));
}